TranslatableEntityCollectionTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace Gedmo\Translatable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Translatable\Fixture\Article;
  6. use Translatable\Fixture\Comment;
  7. /**
  8. * These are tests for translatable behavior
  9. *
  10. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  11. * @link http://www.gediminasm.org
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class TranslatableEntityCollectionTest extends BaseTestCaseORM
  15. {
  16. const ARTICLE = 'Translatable\\Fixture\\Article';
  17. const COMMENT = 'Translatable\\Fixture\\Comment';
  18. const TRANSLATION = 'Gedmo\\Translatable\\Entity\\Translation';
  19. private $translatableListener;
  20. protected function setUp()
  21. {
  22. parent::setUp();
  23. $evm = new EventManager;
  24. $this->translatableListener = new TranslatableListener();
  25. $this->translatableListener->setTranslatableLocale('en_us');
  26. $this->translatableListener->setDefaultLocale('en_us');
  27. $evm->addEventSubscriber($this->translatableListener);
  28. $conn = array(
  29. 'driver' => 'pdo_mysql',
  30. 'host' => '127.0.0.1',
  31. 'dbname' => 'test',
  32. 'user' => 'root',
  33. 'password' => 'nimda'
  34. );
  35. //$this->getMockCustomEntityManager($conn, $evm);
  36. $this->getMockSqliteEntityManager($evm);
  37. }
  38. /**
  39. * @test
  40. */
  41. function shouldEnsureSolvedIssue234()
  42. {
  43. $this->translatableListener->setTranslatableLocale('de');
  44. $this->translatableListener->setDefaultLocale('en');
  45. $this->translatableListener->setPersistDefaultLocaleTranslation(true);
  46. $repo = $this->em->getRepository(self::TRANSLATION);
  47. $entity = new Article;
  48. $entity->setTitle('he'); // is translated to de
  49. $repo
  50. ->translate($entity, 'title', 'en', 'my article en')
  51. ->translate($entity, 'title', 'es', 'my article es')
  52. ->translate($entity, 'title', 'fr', 'my article fr')
  53. ->translate($entity, 'title', 'de', 'my article de')
  54. ;
  55. $this->em->persist($entity);
  56. $this->em->flush();
  57. $this->em->clear();
  58. $trans = $repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  59. $this->assertCount(4, $trans);
  60. $this->assertSame('my article de', $trans['de']['title']); // overrides "he" which would be used if translate for de not called
  61. $this->assertSame('my article es', $trans['es']['title']);
  62. $this->assertSame('my article fr', $trans['fr']['title']);
  63. $this->assertSame('my article en', $trans['en']['title']);
  64. }
  65. /**
  66. * @test
  67. */
  68. function shouldPersistMultipleTranslations()
  69. {
  70. $this->populate();
  71. $repo = $this->em->getRepository(self::TRANSLATION);
  72. $sport = $this->em->getRepository(self::ARTICLE)->find(1);
  73. $translations = $repo->findTranslations($sport);
  74. $this->assertCount(2, $translations);
  75. $this->assertArrayHasKey('de_de', $translations);
  76. $this->assertArrayHasKey('title', $translations['de_de']);
  77. $this->assertArrayHasKey('content', $translations['de_de']);
  78. $this->assertEquals('sport de', $translations['de_de']['title']);
  79. $this->assertEquals('content de', $translations['de_de']['content']);
  80. $this->assertArrayHasKey('ru_ru', $translations);
  81. $this->assertArrayHasKey('title', $translations['ru_ru']);
  82. $this->assertArrayHasKey('content', $translations['ru_ru']);
  83. $this->assertEquals('sport ru', $translations['ru_ru']['title']);
  84. $this->assertEquals('content ru', $translations['ru_ru']['content']);
  85. }
  86. /**
  87. * @test
  88. */
  89. function shouldUpdateTranslation()
  90. {
  91. $this->populate();
  92. $repo = $this->em->getRepository(self::TRANSLATION);
  93. $sport = $this->em->getRepository(self::ARTICLE)->find(1);
  94. $repo
  95. ->translate($sport, 'title', 'ru_ru', 'sport ru change')
  96. ->translate($sport, 'content', 'ru_ru', 'content ru change')
  97. ;
  98. $this->em->flush();
  99. $translations = $repo->findTranslations($sport);
  100. $this->assertCount(2, $translations);
  101. $this->assertArrayHasKey('ru_ru', $translations);
  102. $this->assertArrayHasKey('title', $translations['ru_ru']);
  103. $this->assertArrayHasKey('content', $translations['ru_ru']);
  104. $this->assertEquals('sport ru change', $translations['ru_ru']['title']);
  105. $this->assertEquals('content ru change', $translations['ru_ru']['content']);
  106. }
  107. /**
  108. * @test
  109. */
  110. function shouldUpdateMultipleTranslations()
  111. {
  112. $this->populate();
  113. $repo = $this->em->getRepository(self::TRANSLATION);
  114. $sport = $this->em->getRepository(self::ARTICLE)->find(1);
  115. $repo
  116. ->translate($sport, 'title', 'lt_lt', 'sport lt')
  117. ->translate($sport, 'content', 'lt_lt', 'content lt')
  118. ->translate($sport, 'title', 'ru_ru', 'sport ru change')
  119. ->translate($sport, 'content', 'ru_ru', 'content ru change')
  120. ->translate($sport, 'title', 'en_us', 'sport en update')
  121. ->translate($sport, 'content', 'en_us', 'content en update')
  122. ;
  123. $this->em->flush();
  124. $this->assertEquals('sport en update', $sport->getTitle());
  125. $this->assertEquals('content en update', $sport->getContent());
  126. $translations = $repo->findTranslations($sport);
  127. $this->assertCount(3, $translations);
  128. $this->assertArrayHasKey('de_de', $translations);
  129. $this->assertArrayHasKey('title', $translations['de_de']);
  130. $this->assertArrayHasKey('content', $translations['de_de']);
  131. $this->assertEquals('sport de', $translations['de_de']['title']);
  132. $this->assertEquals('content de', $translations['de_de']['content']);
  133. $this->assertArrayHasKey('ru_ru', $translations);
  134. $this->assertArrayHasKey('title', $translations['ru_ru']);
  135. $this->assertArrayHasKey('content', $translations['ru_ru']);
  136. $this->assertEquals('sport ru change', $translations['ru_ru']['title']);
  137. $this->assertEquals('content ru change', $translations['ru_ru']['content']);
  138. $this->assertArrayHasKey('lt_lt', $translations);
  139. $this->assertArrayHasKey('title', $translations['lt_lt']);
  140. $this->assertArrayHasKey('content', $translations['lt_lt']);
  141. $this->assertEquals('sport lt', $translations['lt_lt']['title']);
  142. $this->assertEquals('content lt', $translations['lt_lt']['content']);
  143. }
  144. private function populate()
  145. {
  146. $repo = $this->em->getRepository(self::TRANSLATION);
  147. $sport = new Article;
  148. $sport->setTitle('Sport');
  149. $sport->setContent('about sport');
  150. $repo
  151. ->translate($sport, 'title', 'de_de', 'sport de')
  152. ->translate($sport, 'content', 'de_de', 'content de')
  153. ->translate($sport, 'title', 'ru_ru', 'sport ru')
  154. ->translate($sport, 'content', 'ru_ru', 'content ru')
  155. ;
  156. $this->em->persist($sport);
  157. $this->em->flush();
  158. }
  159. protected function getUsedEntityFixtures()
  160. {
  161. return array(
  162. self::ARTICLE,
  163. self::TRANSLATION,
  164. self::COMMENT
  165. );
  166. }
  167. }