TranslatableTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. $this->em->clear();
  145. $translations = $repo->findTranslationsByObjectId($this->articleId);
  146. $this->assertEquals(0, count($translations));
  147. }
  148. /**
  149. * Translation fallback, related to issue #9 on github
  150. */
  151. public function testTranslationFallback()
  152. {
  153. $this->translatableListener->setTranslationFallback(false);
  154. $this->translatableListener->setTranslatableLocale('ru_RU');
  155. $article = $this->em->find(self::ARTICLE, $this->articleId);
  156. $this->assertFalse((bool)$article->getTitle());
  157. $this->assertFalse((bool)$article->getContent());
  158. foreach ($article->getComments() as $comment) {
  159. $this->assertFalse((bool)$comment->getSubject());
  160. $this->assertFalse((bool)$comment->getMessage());
  161. }
  162. $this->em->clear();
  163. $this->translatableListener->setTranslationFallback(true);
  164. $article = $this->em->find(self::ARTICLE, $this->articleId);
  165. $this->assertEquals($article->getTitle(), 'title in en');
  166. $this->assertEquals($article->getContent(), 'content in en');
  167. }
  168. public function testGithubIssue64()
  169. {
  170. $judo = new Sport;
  171. $judo->setTitle('Judo');
  172. $judo->setDescription('Whatever');
  173. $this->em->persist($judo);
  174. $this->em->flush();
  175. $this->translatableListener->setTranslatableLocale('de_de');
  176. $judo->setTitle('Judo');
  177. $judo->setDescription('Something in changeset');
  178. $this->em->persist($judo);
  179. $this->em->flush();
  180. $repo = $this->em->getRepository(self::TRANSLATION);
  181. $translations = $repo->findTranslations($judo);
  182. $this->assertEquals(2, count($translations));
  183. // now without any changeset
  184. $this->translatableListener->setTranslatableLocale('ru_ru');
  185. $judo->setTitle('Judo');
  186. $this->em->persist($judo);
  187. $this->em->flush();
  188. // this will not add additional translation, because it cannot be tracked
  189. // without anything in changeset
  190. $translations = $repo->findTranslations($judo);
  191. $this->assertEquals(2, count($translations));
  192. }
  193. protected function getUsedEntityFixtures()
  194. {
  195. return array(
  196. self::ARTICLE,
  197. self::TRANSLATION,
  198. self::COMMENT,
  199. self::SPORT
  200. );
  201. }
  202. private function populate()
  203. {
  204. $article = new Article();
  205. $article->setTitle('title in en');
  206. $article->setContent('content in en');
  207. $comment1 = new Comment();
  208. $comment1->setSubject('subject1 in en');
  209. $comment1->setMessage('message1 in en');
  210. $comment2 = new Comment();
  211. $comment2->setSubject('subject2 in en');
  212. $comment2->setMessage('message2 in en');
  213. $article->addComment($comment1);
  214. $article->addComment($comment2);
  215. $this->em->persist($article);
  216. $this->em->persist($comment1);
  217. $this->em->persist($comment2);
  218. $this->em->flush();
  219. $this->articleId = $article->getId();
  220. $this->em->clear();
  221. }
  222. }