TranslatableSlugTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\TranslationListener,
  9. Sluggable\Fixture\TranslatableArticle,
  10. Sluggable\Fixture\Comment,
  11. Sluggable\Fixture\Page;
  12. /**
  13. * These are tests for Sluggable behavior
  14. *
  15. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  16. * @package Gedmo.Sluggable
  17. * @link http://www.gediminasm.org
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. class TranslatableSlugTest extends BaseTestCaseORM
  21. {
  22. private $articleId;
  23. private $translationListener;
  24. const ARTICLE = 'Sluggable\\Fixture\\TranslatableArticle';
  25. const COMMENT = 'Sluggable\\Fixture\\Comment';
  26. const PAGE = 'Sluggable\\Fixture\\Page';
  27. const TRANSLATION = 'Gedmo\\Translatable\\Entity\\Translation';
  28. protected function setUp()
  29. {
  30. parent::setUp();
  31. $evm = new EventManager;
  32. $this->translationListener = new TranslationListener();
  33. $this->translationListener->setTranslatableLocale('en_us');
  34. $evm->addEventSubscriber(new SluggableListener);
  35. $evm->addEventSubscriber($this->translationListener);
  36. $this->getMockSqliteEntityManager($evm);
  37. $this->populate();
  38. }
  39. public function testSlugAndTranslation()
  40. {
  41. $article = $this->em->find(self::ARTICLE, $this->articleId);
  42. $this->assertTrue($article instanceof Translatable && $article instanceof Sluggable);
  43. $this->assertEquals($article->getSlug(), 'the-title-my-code');
  44. $repo = $this->em->getRepository(self::TRANSLATION);
  45. $translations = $repo->findTranslations($article);
  46. $this->assertEquals(count($translations), 1);
  47. $this->assertArrayHasKey('en_us', $translations);
  48. $this->assertEquals(3, count($translations['en_us']));
  49. $this->assertArrayHasKey('code', $translations['en_us']);
  50. $this->assertEquals('my code', $translations['en_us']['code']);
  51. $this->assertArrayHasKey('title', $translations['en_us']);
  52. $this->assertEquals('the title', $translations['en_us']['title']);
  53. $this->assertArrayHasKey('slug', $translations['en_us']);
  54. $this->assertEquals('the-title-my-code', $translations['en_us']['slug']);
  55. $article = $this->em->find(self::ARTICLE, $this->articleId);
  56. $article->setTranslatableLocale('de_de');
  57. $article->setCode('code in de');
  58. $article->setTitle('title in de');
  59. $this->em->persist($article);
  60. $this->em->flush();
  61. $this->em->clear();
  62. $repo = $this->em->getRepository(self::TRANSLATION);
  63. $translations = $repo->findTranslations($article);
  64. $this->assertEquals(count($translations), 2);
  65. $this->assertArrayHasKey('de_de', $translations);
  66. $this->assertEquals(3, count($translations['de_de']));
  67. $this->assertArrayHasKey('code', $translations['de_de']);
  68. $this->assertEquals('code in de', $translations['de_de']['code']);
  69. $this->assertArrayHasKey('title', $translations['de_de']);
  70. $this->assertEquals('title in de', $translations['de_de']['title']);
  71. $this->assertArrayHasKey('slug', $translations['de_de']);
  72. $this->assertEquals('title-in-de-code-in-de', $translations['de_de']['slug']);
  73. }
  74. public function testConcurrentChanges()
  75. {
  76. $page = new Page;
  77. $page->setContent('cont test');
  78. $a0Page = new Page;
  79. $a0Page->setContent('bi vv');
  80. $article0 = $this->em->find(self::ARTICLE, $this->articleId);
  81. $article0->setCode('cell');
  82. $article0->setTitle('xx gg');
  83. $a0Page->addArticle($article0);
  84. $a0Comment = new Comment;
  85. $a0Comment->setMessage('the xx message');
  86. $article0->addComment($a0Comment);
  87. $this->em->persist($a0Comment);
  88. $this->em->persist($article0);
  89. $this->em->persist($a0Page);
  90. $article1 = new TranslatableArticle();
  91. $article1->setTitle('art1 test');
  92. $article1->setCode('cd1 test');
  93. $article2 = new TranslatableArticle();
  94. $article2->setTitle('art2 test');
  95. $article2->setCode('cd2 test');
  96. $page->addArticle($article1);
  97. $page->addArticle($article2);
  98. $comment1 = new Comment;
  99. $comment1->setMessage('mes1-test');
  100. $comment2 = new Comment;
  101. $comment2->setMessage('mes2 test');
  102. $article1->addComment($comment1);
  103. $article2->addComment($comment2);
  104. $this->em->persist($page);
  105. $this->em->persist($article1);
  106. $this->em->persist($article2);
  107. $this->em->persist($comment1);
  108. $this->em->persist($comment2);
  109. $this->em->flush();
  110. $this->em->clear();
  111. $this->assertEquals($page->getSlug(), 'Cont_Test');
  112. }
  113. protected function getUsedEntityFixtures()
  114. {
  115. return array(
  116. self::ARTICLE,
  117. self::COMMENT,
  118. self::PAGE,
  119. self::TRANSLATION
  120. );
  121. }
  122. private function populate()
  123. {
  124. $article = new TranslatableArticle();
  125. $article->setTitle('the title');
  126. $article->setCode('my code');
  127. $this->em->persist($article);
  128. $this->em->flush();
  129. $this->em->clear();
  130. $this->articleId = $article->getId();
  131. }
  132. }