TranslatableTest.php 11KB

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