TranslatableTest.php 9.0 KB

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