TranslatableTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. use Translatable\Fixture\Sport;
  8. /**
  9. * These are tests for translatable behavior
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @package Gedmo.Translatable
  13. * @link http://www.gediminasm.org
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. class TranslatableTest extends BaseTestCaseORM
  17. {
  18. const ARTICLE = 'Translatable\\Fixture\\Article';
  19. const SPORT = 'Translatable\\Fixture\\Sport';
  20. const COMMENT = 'Translatable\\Fixture\\Comment';
  21. const TRANSLATION = 'Gedmo\\Translatable\\Entity\\Translation';
  22. private $articleId;
  23. private $translatableListener;
  24. protected function setUp()
  25. {
  26. parent::setUp();
  27. $evm = new EventManager;
  28. $this->translatableListener = new TranslationListener();
  29. $this->translatableListener->setTranslatableLocale('en_us');
  30. $evm->addEventSubscriber($this->translatableListener);
  31. $this->getMockSqliteEntityManager($evm);
  32. $this->populate();
  33. }
  34. public function testFixtureGeneratedTranslations()
  35. {
  36. $repo = $this->em->getRepository(self::TRANSLATION);
  37. $this->assertTrue($repo instanceof Entity\Repository\TranslationRepository);
  38. $article = $this->em->find(self::ARTICLE, $this->articleId);
  39. $this->assertTrue($article instanceof Translatable);
  40. $translations = $repo->findTranslations($article);
  41. $this->assertEquals(count($translations), 1);
  42. $this->assertArrayHasKey('en_us', $translations);
  43. $this->assertArrayHasKey('content', $translations['en_us']);
  44. $this->assertEquals('content in en', $translations['en_us']['content']);
  45. $this->assertArrayHasKey('title', $translations['en_us']);
  46. $this->assertEquals('title in en', $translations['en_us']['title']);
  47. $comments = $article->getComments();
  48. $this->assertEquals(count($comments), 2);
  49. foreach ($comments as $num => $comment) {
  50. $translations = $repo->findTranslations($comment);
  51. $this->assertEquals(count($translations), 1);
  52. $this->assertArrayHasKey('en_us', $translations);
  53. $number = $num + 1;
  54. $this->assertArrayHasKey('subject', $translations['en_us']);
  55. $expected = "subject{$number} in en";
  56. $this->assertEquals($expected, $translations['en_us']['subject']);
  57. $this->assertArrayHasKey('message', $translations['en_us']);
  58. $expected = "message{$number} in en";
  59. $this->assertEquals($expected, $translations['en_us']['message']);
  60. }
  61. // test default locale
  62. $this->translatableListener->setDefaultLocale('en_us');
  63. $article = $this->em->find(self::ARTICLE, $this->articleId);
  64. $article->setTranslatableLocale('de_de');
  65. $article->setContent('content in de');
  66. $article->setTitle('title in de');
  67. $this->em->persist($article);
  68. $this->em->flush();
  69. $this->em->clear();
  70. $qb = $this->em->createQueryBuilder();
  71. $qb->select('art')
  72. ->from(self::ARTICLE, 'art')
  73. ->where('art.id = :id');
  74. $q = $qb->getQuery();
  75. $result = $q->execute(
  76. array('id' => $article->getId()),
  77. \Doctrine\ORM\Query::HYDRATE_ARRAY
  78. );
  79. $this->assertEquals(1, count($result));
  80. $this->assertEquals($result[0]['title'], 'title in en');
  81. $this->assertEquals($result[0]['content'], 'content in en');
  82. $repo = $this->em->getRepository(self::TRANSLATION);
  83. $translations = $repo->findTranslations($article);
  84. $this->assertEquals(count($translations), 2);
  85. $this->assertArrayHasKey('de_de', $translations);
  86. $this->assertArrayHasKey('content', $translations['de_de']);
  87. $this->assertEquals('content in de', $translations['de_de']['content']);
  88. $this->assertArrayHasKey('title', $translations['de_de']);
  89. $this->assertEquals('title in de', $translations['de_de']['title']);
  90. $this->translatableListener->setDefaultLocale('');
  91. // test second translations
  92. $article = $this->em->find(self::ARTICLE, $this->articleId);
  93. $this->translatableListener->setDefaultLocale('en_us');
  94. $article->setTranslatableLocale('de_de');
  95. $article->setContent('content in de');
  96. $article->setTitle('title in de');
  97. $comments = $article->getComments();
  98. foreach ($comments as $comment) {
  99. $number = preg_replace("@[^\d]+@", '', $comment->getSubject());
  100. $comment->setTranslatableLocale('de_de');
  101. $comment->setSubject("subject{$number} in de");
  102. $comment->setMessage("message{$number} in de");
  103. $this->em->persist($comment);
  104. }
  105. $this->em->persist($article);
  106. $this->em->flush();
  107. $this->em->clear();
  108. $repo = $this->em->getRepository(self::TRANSLATION);
  109. $translations = $repo->findTranslations($article);
  110. $this->assertEquals(count($translations), 2);
  111. $this->assertArrayHasKey('de_de', $translations);
  112. $this->assertArrayHasKey('content', $translations['de_de']);
  113. $this->assertEquals('content in de', $translations['de_de']['content']);
  114. $this->assertArrayHasKey('title', $translations['de_de']);
  115. $this->assertEquals('title in de', $translations['de_de']['title']);
  116. $comments = $article->getComments();
  117. $this->assertEquals(count($comments), 2);
  118. foreach ($comments as $comment) {
  119. $translations = $repo->findTranslations($comment);
  120. $this->assertEquals(count($translations), 2);
  121. $this->assertArrayHasKey('de_de', $translations);
  122. $number = preg_replace("@[^\d]+@", '', $comment->getSubject());
  123. $this->assertArrayHasKey('subject', $translations['de_de']);
  124. $expected = "subject{$number} in de";
  125. $this->assertEquals($expected, $translations['de_de']['subject']);
  126. $this->assertArrayHasKey('message', $translations['de_de']);
  127. $expected = "message{$number} in de";
  128. $this->assertEquals($expected, $translations['de_de']['message']);
  129. }
  130. $this->translatableListener->setTranslatableLocale('en_us');
  131. $article = $this->em->find(self::ARTICLE, $this->articleId);
  132. $this->assertEquals($article->getTitle(), 'title in en');
  133. $this->assertEquals($article->getContent(), 'content in en');
  134. $comments = $article->getComments();
  135. foreach ($comments as $comment) {
  136. $number = preg_replace("@[^\d]+@", '', $comment->getSubject());
  137. $this->assertEquals($comment->getSubject(), "subject{$number} in en");
  138. $this->assertEquals($comment->getMessage(), "message{$number} in en");
  139. }
  140. // test deletion
  141. $article = $this->em->find(self::ARTICLE, $this->articleId);
  142. $this->em->remove($article);
  143. $this->em->flush();
  144. $translations = $repo->findTranslations($article);
  145. $this->assertEquals(0, count($translations));
  146. }
  147. /**
  148. * Translation fallback, related to issue #9 on github
  149. */
  150. /*public function testTranslationFallback()
  151. {
  152. $this->translatableListener->setTranslationFallback(false);
  153. $this->translatableListener->setTranslatableLocale('ru_RU');
  154. $article = $this->em->find(self::ARTICLE, $this->articleId);
  155. $this->assertFalse((bool)$article->getTitle());
  156. $this->assertFalse((bool)$article->getContent());
  157. foreach ($article->getComments() as $comment) {
  158. $this->assertFalse((bool)$comment->getSubject());
  159. $this->assertFalse((bool)$comment->getMessage());
  160. }
  161. $this->em->clear();
  162. $this->translatableListener->setTranslationFallback(true);
  163. $article = $this->em->find(self::ARTICLE, $this->articleId);
  164. $this->assertEquals($article->getTitle(), 'title in en');
  165. $this->assertEquals($article->getContent(), 'content in en');
  166. }
  167. public function testGithubIssue64()
  168. {
  169. $judo = new Sport;
  170. $judo->setTitle('Judo');
  171. $judo->setDescription('Whatever');
  172. $this->em->persist($judo);
  173. $this->em->flush();
  174. $this->translatableListener->setTranslatableLocale('de_de');
  175. $judo->setTitle('Judo');
  176. $judo->setDescription('Something in changeset');
  177. $this->em->persist($judo);
  178. $this->em->flush();
  179. $repo = $this->em->getRepository(self::TRANSLATION);
  180. $translations = $repo->findTranslations($judo);
  181. $this->assertEquals(2, count($translations));
  182. // now without any changeset
  183. $this->translatableListener->setTranslatableLocale('ru_ru');
  184. $judo->setTitle('Judo');
  185. $this->em->persist($judo);
  186. $this->em->flush();
  187. // this will not add additional translation, because it cannot be tracked
  188. // without anything in changeset
  189. $translations = $repo->findTranslations($judo);
  190. $this->assertEquals(2, count($translations));
  191. }*/
  192. protected function getUsedEntityFixtures()
  193. {
  194. return array(
  195. self::ARTICLE,
  196. self::TRANSLATION,
  197. self::COMMENT,
  198. self::SPORT
  199. );
  200. }
  201. private function populate()
  202. {
  203. $article = new Article();
  204. $article->setTitle('title in en');
  205. $article->setContent('content in en');
  206. $comment1 = new Comment();
  207. $comment1->setSubject('subject1 in en');
  208. $comment1->setMessage('message1 in en');
  209. $comment2 = new Comment();
  210. $comment2->setSubject('subject2 in en');
  211. $comment2->setMessage('message2 in en');
  212. $article->addComment($comment1);
  213. $article->addComment($comment2);
  214. $this->em->persist($article);
  215. $this->em->persist($comment1);
  216. $this->em->persist($comment2);
  217. $this->em->flush();
  218. $this->articleId = $article->getId();
  219. $this->em->clear();
  220. }
  221. }