feat(blog): enhance blog identifier resolution to support multiple identifiers and improve URL handling
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Blog;
|
||||
|
||||
use App\Blog\Entity\Blog;
|
||||
use App\Tests\ApiTestCase;
|
||||
|
||||
/**
|
||||
* A post's slug is derived from its title, so an edited title silently kills every
|
||||
* published link to it — Search Console reported those old URLs as 404s. The detail
|
||||
* endpoint therefore resolves any identifier the post has ever been addressed by and
|
||||
* always reports the canonical slug, which the public site uses to redirect.
|
||||
*/
|
||||
class BlogIdentifierResolutionTest extends ApiTestCase
|
||||
{
|
||||
private function makePublished(string $title): Blog
|
||||
{
|
||||
$blog = new Blog($this->createUser(['ROLE_ADMIN']), $title, 'متن آزمایشی مقاله برای تست');
|
||||
$blog->setStatus(Blog::STATUS_PUBLISHED);
|
||||
$this->em->persist($blog);
|
||||
$this->em->flush();
|
||||
|
||||
return $blog;
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function fetch(string $identifier): array
|
||||
{
|
||||
$this->client->request('GET', '/api/v1/blog/' . rawurlencode($identifier));
|
||||
|
||||
return json_decode($this->client->getResponse()->getContent(), true) ?? [];
|
||||
}
|
||||
|
||||
public function testResolvesByCurrentSlug(): void
|
||||
{
|
||||
$blog = $this->makePublished('راهنمای تشخیص آلرژی دارویی');
|
||||
|
||||
$body = $this->fetch($blog->getSlug());
|
||||
|
||||
$this->assertSame(200, $this->responseCode());
|
||||
$this->assertSame($blog->getUuid(), $body['data']['data']['uuid']);
|
||||
}
|
||||
|
||||
public function testResolvesByUuid(): void
|
||||
{
|
||||
$blog = $this->makePublished('راهنمای تشخیص آلرژی دارویی');
|
||||
|
||||
$this->fetch($blog->getUuid());
|
||||
|
||||
$this->assertSame(200, $this->responseCode());
|
||||
}
|
||||
|
||||
public function testResolvesByTopicSlug(): void
|
||||
{
|
||||
$blog = $this->makePublished('راهنمای تشخیص آلرژی دارویی');
|
||||
$blog->setTopicSlug('allergy-immunology-symptoms-901');
|
||||
$this->em->flush();
|
||||
|
||||
$body = $this->fetch('allergy-immunology-symptoms-901');
|
||||
|
||||
$this->assertSame(200, $this->responseCode());
|
||||
$this->assertSame($blog->getSlug(), $body['data']['data']['slug']);
|
||||
}
|
||||
|
||||
public function testOutdatedSlugStillResolvesThroughItsUuidSuffix(): void
|
||||
{
|
||||
$blog = $this->makePublished('راهنمای تشخیص آلرژی دارویی');
|
||||
$uuidPrefix = substr(str_replace('-', '', $blog->getUuid()), 0, 8);
|
||||
|
||||
// The title changed, so the human part of the URL is stale — only the suffix survives.
|
||||
$body = $this->fetch('old-latin-title-' . $uuidPrefix);
|
||||
|
||||
$this->assertSame(200, $this->responseCode());
|
||||
$this->assertSame($blog->getSlug(), $body['data']['data']['slug']);
|
||||
$this->assertNotSame('old-latin-title-' . $uuidPrefix, $body['data']['data']['slug']);
|
||||
}
|
||||
|
||||
public function testUnknownIdentifierStays404(): void
|
||||
{
|
||||
$this->makePublished('راهنمای تشخیص آلرژی دارویی');
|
||||
|
||||
$this->fetch('alamat-tahooe-mokarrar-dar-bimaran-sartani');
|
||||
|
||||
$this->assertSame(404, $this->responseCode());
|
||||
}
|
||||
|
||||
public function testNonHexSuffixIsNotTreatedAsUuidPrefix(): void
|
||||
{
|
||||
$this->makePublished('راهنمای تشخیص آلرژی دارویی');
|
||||
|
||||
$this->fetch('some-old-title-zzzzzzzz');
|
||||
|
||||
$this->assertSame(404, $this->responseCode());
|
||||
}
|
||||
|
||||
public function testDraftStaysHiddenEvenThroughFallbackIdentifiers(): void
|
||||
{
|
||||
$blog = new Blog($this->createUser(['ROLE_ADMIN']), 'پیشنویس منتشرنشده', 'متن آزمایشی مقاله برای تست');
|
||||
$blog->setTopicSlug('draft-topic-901');
|
||||
$this->em->persist($blog);
|
||||
$this->em->flush();
|
||||
|
||||
$this->fetch('draft-topic-901');
|
||||
$this->assertSame(404, $this->responseCode());
|
||||
|
||||
$this->fetch($blog->getUuid());
|
||||
$this->assertSame(404, $this->responseCode());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user