SluggableTest.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace Gedmo\Sluggable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Sluggable\Fixture\Article;
  6. /**
  7. * These are tests for sluggable behavior
  8. *
  9. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  10. * @package Gedmo.Sluggable
  11. * @link http://www.gediminasm.org
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class SluggableTest extends BaseTestCaseORM
  15. {
  16. const ARTICLE = 'Sluggable\\Fixture\\Article';
  17. private $articleId;
  18. protected function setUp()
  19. {
  20. parent::setUp();
  21. $evm = new EventManager;
  22. $evm->addEventSubscriber(new SluggableListener);
  23. $this->getMockSqliteEntityManager($evm);
  24. $this->populate();
  25. }
  26. /**
  27. * @test
  28. */
  29. function shouldInsertNewSlug()
  30. {
  31. $article = $this->em->find(self::ARTICLE, $this->articleId);
  32. $this->assertTrue($article instanceof Sluggable);
  33. $this->assertEquals($article->getSlug(), 'the-title-my-code');
  34. }
  35. /**
  36. * @test
  37. */
  38. function shouldBuildUniqueSlug()
  39. {
  40. for ($i = 0; $i < 12; $i++) {
  41. $article = new Article();
  42. $article->setTitle('the title');
  43. $article->setCode('my code');
  44. $this->em->persist($article);
  45. $this->em->flush();
  46. $this->em->clear();
  47. $this->assertEquals($article->getSlug(), 'the-title-my-code-' . ($i + 1));
  48. }
  49. }
  50. /**
  51. * @test
  52. */
  53. function shouldHandleUniqueSlugLimitedLength()
  54. {
  55. $long = 'the title the title the title the title the title the title the title';
  56. $article = new Article();
  57. $article->setTitle($long);
  58. $article->setCode('my code');
  59. $this->em->persist($article);
  60. $this->em->flush();
  61. $this->em->clear();
  62. for ($i = 0; $i < 12; $i++) {
  63. $article = new Article();
  64. $article->setTitle($long);
  65. $article->setCode('my code');
  66. $this->em->persist($article);
  67. $this->em->flush();
  68. $this->em->clear();
  69. $shorten = $article->getSlug();
  70. $this->assertEquals(64, strlen($shorten));
  71. $expected = 'the-title-the-title-the-title-the-title-the-title-the-title-the-';
  72. $expected = substr($expected, 0, 64 - (strlen($i+1) + 1)) . '-' . ($i+1);
  73. $this->assertEquals($shorten, $expected);
  74. }
  75. }
  76. /**
  77. * @test
  78. */
  79. function doubleDelimiterShouldBeRemoved()
  80. {
  81. $long = 'Sample long title which should be correctly slugged blablabla';
  82. $article = new Article();
  83. $article->setTitle($long);
  84. $article->setCode('my code');
  85. $article2 = new Article();
  86. $article2->setTitle($long);
  87. $article2->setCode('my code');
  88. $this->em->persist($article);
  89. $this->em->persist($article2);
  90. $this->em->flush();
  91. $this->em->clear();
  92. $this->assertEquals("sample-long-title-which-should-be-correctly-slugged-blablabla-my", $article->getSlug());
  93. // OLD IMPLEMENTATION PRODUCE SLUG sample-long-title-which-should-be-correctly-slugged-blablabla--1
  94. $this->assertEquals("sample-long-title-which-should-be-correctly-slugged-blablabla-1", $article2->getSlug());
  95. }
  96. /**
  97. * @test
  98. */
  99. function shouldHandleNumbersInSlug()
  100. {
  101. $article = new Article();
  102. $article->setTitle('the title');
  103. $article->setCode('my code 123');
  104. $this->em->persist($article);
  105. $this->em->flush();
  106. for ($i = 0; $i < 12; $i++) {
  107. $article = new Article();
  108. $article->setTitle('the title');
  109. $article->setCode('my code 123');
  110. $this->em->persist($article);
  111. $this->em->flush();
  112. $this->em->clear();
  113. $this->assertEquals($article->getSlug(), 'the-title-my-code-123-' . ($i + 1));
  114. }
  115. }
  116. /**
  117. * @test
  118. */
  119. function shouldUpdateSlug()
  120. {
  121. $article = $this->em->find(self::ARTICLE, $this->articleId);
  122. $article->setTitle('the title updated');
  123. $this->em->persist($article);
  124. $this->em->flush();
  125. $this->assertSame('the-title-updated-my-code', $article->getSlug());
  126. }
  127. /**
  128. * @test
  129. */
  130. function shouldBeAbleToForceRegenerationOfSlug()
  131. {
  132. $article = $this->em->find(self::ARTICLE, $this->articleId);
  133. $article->setSlug(null);
  134. $this->em->persist($article);
  135. $this->em->flush();
  136. $this->assertSame('the-title-my-code', $article->getSlug());
  137. }
  138. /**
  139. * @test
  140. */
  141. function shouldBeAbleToForceTheSlug()
  142. {
  143. $article = $this->em->find(self::ARTICLE, $this->articleId);
  144. $article->setSlug('my forced slug');
  145. $this->em->persist($article);
  146. $new = new Article;
  147. $new->setTitle('hey');
  148. $new->setCode('cc');
  149. $new->setSlug('forced');
  150. $this->em->persist($new);
  151. $this->em->flush();
  152. $this->assertSame('my-forced-slug', $article->getSlug());
  153. $this->assertSame('forced', $new->getSlug());
  154. }
  155. /**
  156. * @test
  157. */
  158. function shouldSolveGithubIssue45()
  159. {
  160. // persist new records with same slug
  161. $article = new Article;
  162. $article->setTitle('test');
  163. $article->setCode('code');
  164. $this->em->persist($article);
  165. $article2 = new Article;
  166. $article2->setTitle('test');
  167. $article2->setCode('code');
  168. $this->em->persist($article2);
  169. $this->em->flush();
  170. $this->assertEquals('test-code', $article->getSlug());
  171. $this->assertEquals('test-code-1', $article2->getSlug());
  172. }
  173. /**
  174. * @test
  175. */
  176. function shouldSolveGithubIssue57()
  177. {
  178. // slug matched by prefix
  179. $article = new Article;
  180. $article->setTitle('my');
  181. $article->setCode('slug');
  182. $this->em->persist($article);
  183. $article2 = new Article;
  184. $article2->setTitle('my');
  185. $article2->setCode('s');
  186. $this->em->persist($article2);
  187. $this->em->flush();
  188. $this->assertEquals('my-s', $article2->getSlug());
  189. }
  190. protected function getUsedEntityFixtures()
  191. {
  192. return array(
  193. self::ARTICLE,
  194. );
  195. }
  196. private function populate()
  197. {
  198. $article = new Article();
  199. $article->setTitle('the title');
  200. $article->setCode('my code');
  201. $this->em->persist($article);
  202. $this->em->flush();
  203. $this->em->clear();
  204. $this->articleId = $article->getId();
  205. }
  206. }