createUser(['ROLE_ADMIN']), $title, 'متن آزمایشی مقاله برای تست'); $blog->setStatus(Blog::STATUS_PUBLISHED); $this->em->persist($blog); $this->em->flush(); return $blog; } /** @return array */ 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('راهنمای تشخیص آلرژی دارویی'); // یکتا به‌ازای هر اجرا: topic_slug ستون unique است و دیتابیس تست بین // اجراها پاک نمی‌شود. $topicSlug = 'topic-' . substr($blog->getUuid(), 0, 8); $blog->setTopicSlug($topicSlug); $this->em->flush(); $body = $this->fetch($topicSlug); $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-' . bin2hex(random_bytes(4))); $this->em->persist($blog); $this->em->flush(); $this->fetch($blog->getTopicSlug()); $this->assertSame(404, $this->responseCode()); $this->fetch($blog->getUuid()); $this->assertSame(404, $this->responseCode()); } }