TranslatableManySlugTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace Gedmo\Sluggable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\Common\Util\Debug,
  6. Gedmo\Translatable\Translatable,
  7. Gedmo\Translatable\Entity\Translation,
  8. Gedmo\Translatable\TranslatableListener,
  9. Sluggable\Fixture\TransArticleManySlug;
  10. /**
  11. * These are tests for Sluggable behavior
  12. *
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @link http://www.gediminasm.org
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class TranslatableManySlugTest extends BaseTestCaseORM
  18. {
  19. private $articleId;
  20. private $translatableListener;
  21. const ARTICLE = 'Sluggable\\Fixture\\TransArticleManySlug';
  22. const TRANSLATION = 'Gedmo\\Translatable\\Entity\\Translation';
  23. protected function setUp()
  24. {
  25. parent::setUp();
  26. $evm = new EventManager;
  27. $this->translatableListener = new TranslatableListener();
  28. $this->translatableListener->setTranslatableLocale('en_US');
  29. $evm->addEventSubscriber(new SluggableListener);
  30. $evm->addEventSubscriber($this->translatableListener);
  31. $this->getMockSqliteEntityManager($evm);
  32. $this->populate();
  33. }
  34. public function testSlugAndTranslation()
  35. {
  36. $article = $this->em->find(self::ARTICLE, $this->articleId);
  37. $this->assertTrue($article instanceof Translatable && $article instanceof Sluggable);
  38. $this->assertEquals('the-title-my-code', $article->getSlug());
  39. $this->assertEquals('the-unique-title', $article->getUniqueSlug());
  40. $repo = $this->em->getRepository(self::TRANSLATION);
  41. $translations = $repo->findTranslations($article);
  42. $this->assertCount(0, $translations);
  43. $article = $this->em->find(self::ARTICLE, $this->articleId);
  44. $article->setTranslatableLocale('de_DE');
  45. $article->setCode('code in de');
  46. $article->setTitle('title in de');
  47. $this->em->persist($article);
  48. $this->em->flush();
  49. $this->em->clear();
  50. $repo = $this->em->getRepository(self::TRANSLATION);
  51. $translations = $repo->findTranslations($article);
  52. $this->assertCount(1, $translations);
  53. $this->assertArrayHasKey('de_DE', $translations);
  54. $this->assertCount(3, $translations['de_DE']);
  55. $this->assertEquals('title in de', $translations['de_DE']['title']);
  56. $this->assertArrayHasKey('slug', $translations['de_DE']);
  57. $this->assertEquals('title-in-de-code-in-de', $translations['de_DE']['slug']);
  58. }
  59. public function testUniqueness()
  60. {
  61. $a0 = new TransArticleManySlug;
  62. $a0->setTitle('the title');
  63. $a0->setCode('my code');
  64. $a0->setUniqueTitle('title');
  65. $this->em->persist($a0);
  66. $a1 = new TransArticleManySlug;
  67. $a1->setTitle('the title');
  68. $a1->setCode('my code');
  69. $a1->setUniqueTitle('title');
  70. $this->em->persist($a1);
  71. $this->em->flush();
  72. $this->assertEquals('title', $a0->getUniqueSlug());
  73. $this->assertEquals('title-1', $a1->getUniqueSlug());
  74. // if its translated maybe should be different
  75. $this->assertEquals('the-title-my-code-1', $a0->getSlug());
  76. $this->assertEquals('the-title-my-code-2', $a1->getSlug());
  77. }
  78. protected function getUsedEntityFixtures()
  79. {
  80. return array(
  81. self::ARTICLE,
  82. self::TRANSLATION
  83. );
  84. }
  85. private function populate()
  86. {
  87. $article = new TransArticleManySlug();
  88. $article->setTitle('the title');
  89. $article->setCode('my code');
  90. $article->setUniqueTitle('the unique title');
  91. $this->em->persist($article);
  92. $this->em->flush();
  93. $this->em->clear();
  94. $this->articleId = $article->getId();
  95. }
  96. }