TranslatableTest.php 11 KB

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