SluggableTest.php 7.2 KB

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