TranslatableSlugTest.php 4.7 KB

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