123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- <?php
- namespace Gedmo\Sluggable;
- use Doctrine\Common\EventManager;
- use Tool\BaseTestCaseORM;
- use Sluggable\Fixture\Article;
- /**
- * These are tests for sluggable behavior
- *
- * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
- * @link http://www.gediminasm.org
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- class SluggableTest extends BaseTestCaseORM
- {
- const ARTICLE = 'Sluggable\\Fixture\\Article';
- private $articleId;
- protected function setUp()
- {
- parent::setUp();
- $evm = new EventManager;
- $evm->addEventSubscriber(new SluggableListener);
- $this->getMockSqliteEntityManager($evm);
- $this->populate();
- }
- /**
- * @test
- */
- function shouldInsertNewSlug()
- {
- $article = $this->em->find(self::ARTICLE, $this->articleId);
- $this->assertTrue($article instanceof Sluggable);
- $this->assertEquals($article->getSlug(), 'the-title-my-code');
- }
- /**
- * @test
- */
- function shouldBuildUniqueSlug()
- {
- for ($i = 0; $i < 12; $i++) {
- $article = new Article();
- $article->setTitle('the title');
- $article->setCode('my code');
- $this->em->persist($article);
- $this->em->flush();
- $this->em->clear();
- $this->assertEquals($article->getSlug(), 'the-title-my-code-' . ($i + 1));
- }
- }
- /**
- * @test
- */
- function shouldHandleUniqueSlugLimitedLength()
- {
- $long = 'the title the title the title the title the title the title the title';
- $article = new Article();
- $article->setTitle($long);
- $article->setCode('my code');
- $this->em->persist($article);
- $this->em->flush();
- $this->em->clear();
- for ($i = 0; $i < 12; $i++) {
- $article = new Article();
- $article->setTitle($long);
- $article->setCode('my code');
- $this->em->persist($article);
- $this->em->flush();
- $this->em->clear();
- $shorten = $article->getSlug();
- $this->assertEquals(64, strlen($shorten));
- $expected = 'the-title-the-title-the-title-the-title-the-title-the-title-the-';
- $expected = substr($expected, 0, 64 - (strlen($i+1) + 1)) . '-' . ($i+1);
- $this->assertEquals($shorten, $expected);
- }
- }
- /**
- * @test
- */
- function doubleDelimiterShouldBeRemoved()
- {
- $long = 'Sample long title which should be correctly slugged blablabla';
- $article = new Article();
- $article->setTitle($long);
- $article->setCode('my code');
- $article2 = new Article();
- $article2->setTitle($long);
- $article2->setCode('my code');
- $this->em->persist($article);
- $this->em->persist($article2);
- $this->em->flush();
- $this->em->clear();
- $this->assertEquals("sample-long-title-which-should-be-correctly-slugged-blablabla-my", $article->getSlug());
- // OLD IMPLEMENTATION PRODUCE SLUG sample-long-title-which-should-be-correctly-slugged-blablabla--1
- $this->assertEquals("sample-long-title-which-should-be-correctly-slugged-blablabla-1", $article2->getSlug());
- }
- /**
- * @test
- */
- function shouldHandleNumbersInSlug()
- {
- $article = new Article();
- $article->setTitle('the title');
- $article->setCode('my code 123');
- $this->em->persist($article);
- $this->em->flush();
- for ($i = 0; $i < 12; $i++) {
- $article = new Article();
- $article->setTitle('the title');
- $article->setCode('my code 123');
- $this->em->persist($article);
- $this->em->flush();
- $this->em->clear();
- $this->assertEquals($article->getSlug(), 'the-title-my-code-123-' . ($i + 1));
- }
- }
- /**
- * @test
- */
- function shouldUpdateSlug()
- {
- $article = $this->em->find(self::ARTICLE, $this->articleId);
- $article->setTitle('the title updated');
- $this->em->persist($article);
- $this->em->flush();
- $this->assertSame('the-title-updated-my-code', $article->getSlug());
- }
- /**
- * @test
- */
- function shouldBeAbleToForceRegenerationOfSlug()
- {
- $article = $this->em->find(self::ARTICLE, $this->articleId);
- $article->setSlug(null);
- $this->em->persist($article);
- $this->em->flush();
- $this->assertSame('the-title-my-code', $article->getSlug());
- }
- /**
- * @test
- */
- function shouldBeAbleToForceTheSlug()
- {
- $article = $this->em->find(self::ARTICLE, $this->articleId);
- $article->setSlug('my-forced-slug');
- $this->em->persist($article);
- $new = new Article;
- $new->setTitle('hey');
- $new->setCode('cc');
- $new->setSlug('forced');
- $this->em->persist($new);
- $this->em->flush();
- $this->assertSame('my-forced-slug', $article->getSlug());
- $this->assertSame('forced', $new->getSlug());
- }
- /**
- * @test
- */
- function shouldSolveGithubIssue45()
- {
- // persist new records with same slug
- $article = new Article;
- $article->setTitle('test');
- $article->setCode('code');
- $this->em->persist($article);
- $article2 = new Article;
- $article2->setTitle('test');
- $article2->setCode('code');
- $this->em->persist($article2);
- $this->em->flush();
- $this->assertEquals('test-code', $article->getSlug());
- $this->assertEquals('test-code-1', $article2->getSlug());
- }
- /**
- * @test
- */
- function shouldSolveGithubIssue57()
- {
- // slug matched by prefix
- $article = new Article;
- $article->setTitle('my');
- $article->setCode('slug');
- $this->em->persist($article);
- $article2 = new Article;
- $article2->setTitle('my');
- $article2->setCode('s');
- $this->em->persist($article2);
- $this->em->flush();
- $this->assertEquals('my-s', $article2->getSlug());
- }
- /**
- * @test
- */
- function shouldAllowForcingEmptySlugAndRegenerateIfNullIssue807()
- {
- $article = $this->em->find(self::ARTICLE, $this->articleId);
- $article->setSlug('');
- $this->em->persist($article);
- $this->em->flush();
- $this->assertSame('', $article->getSlug());
- $article->setSlug(null);
- $this->em->persist($article);
- $this->em->flush();
- $this->assertSame('the-title-my-code', $article->getSlug());
- $same = new Article;
- $same->setTitle('any');
- $same->setCode('any');
- $same->setSlug('the-title-my-code');
- $this->em->persist($same);
- $this->em->flush();
- $this->assertSame('the-title-my-code-1', $same->getSlug());
- }
- protected function getUsedEntityFixtures()
- {
- return array(
- self::ARTICLE,
- );
- }
- private function populate()
- {
- $article = new Article();
- $article->setTitle('the title');
- $article->setCode('my code');
- $this->em->persist($article);
- $this->em->flush();
- $this->em->clear();
- $this->articleId = $article->getId();
- }
- }
|