PersonalTranslationTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace Gedmo\Translatable;
  3. use Doctrine\Common\EventManager;
  4. use Doctrine\ORM\Query;
  5. use Gedmo\Translatable\TranslatableListener;
  6. use Gedmo\Translatable\Query\TreeWalker\TranslationWalker;
  7. use Tool\BaseTestCaseORM;
  8. use Translatable\Fixture\Personal\Article;
  9. use Translatable\Fixture\Personal\PersonalArticleTranslation;
  10. /**
  11. * These are tests for translatable behavior
  12. *
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @link http://www.gediminasm.org
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class PersonalTranslationTest extends BaseTestCaseORM
  18. {
  19. const ARTICLE = 'Translatable\Fixture\Personal\Article';
  20. const TRANSLATION = 'Translatable\Fixture\Personal\PersonalArticleTranslation';
  21. const TREE_WALKER_TRANSLATION = 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker';
  22. private $translatableListener;
  23. protected function setUp()
  24. {
  25. parent::setUp();
  26. $evm = new EventManager;
  27. $this->translatableListener = new TranslatableListener();
  28. $this->translatableListener->setTranslatableLocale('en');
  29. $this->translatableListener->setDefaultLocale('en');
  30. $evm->addEventSubscriber($this->translatableListener);
  31. $conn = array(
  32. 'driver' => 'pdo_mysql',
  33. 'host' => '127.0.0.1',
  34. 'dbname' => 'test',
  35. 'user' => 'root',
  36. 'password' => 'nimda'
  37. );
  38. //$this->getMockCustomEntityManager($conn, $evm);
  39. $this->getMockSqliteEntityManager($evm);
  40. }
  41. /**
  42. * @test
  43. */
  44. function shouldPersistDefaultLocaleTranslationIfRequired()
  45. {
  46. $this->translatableListener->setPersistDefaultLocaleTranslation(true);
  47. $this->populate();
  48. $article = $this->em->find(self::ARTICLE, array('id' => 1));
  49. $translations = $article->getTranslations();
  50. $this->assertCount(3, $translations);
  51. }
  52. /**
  53. * @test
  54. */
  55. function shouldCreateTranslations()
  56. {
  57. $this->populate();
  58. $article = $this->em->find(self::ARTICLE, array('id' => 1));
  59. $translations = $article->getTranslations();
  60. $this->assertCount(2, $translations);
  61. }
  62. /**
  63. * @test
  64. */
  65. function shouldTranslateTheRecord()
  66. {
  67. $this->populate();
  68. $this->translatableListener->setTranslatableLocale('lt');
  69. $this->startQueryLog();
  70. $article = $this->em->find(self::ARTICLE, array('id' => 1));
  71. $sqlQueriesExecuted = $this->queryAnalyzer->getExecutedQueries();
  72. $this->assertCount(2, $sqlQueriesExecuted);
  73. $this->assertEquals('SELECT t0.id AS id1, t0.locale AS locale2, t0.field AS field3, t0.content AS content4, t0.object_id AS object_id5 FROM article_translations t0 WHERE t0.object_id = 1', $sqlQueriesExecuted[1]);
  74. $this->assertEquals('lt', $article->getTitle());
  75. }
  76. /**
  77. * @test
  78. */
  79. function shouldCascadeDeletionsByForeignKeyConstraints()
  80. {
  81. if ($this->em->getConnection()->getDatabasePlatform()->getName() == 'sqlite') {
  82. $this->markTestSkipped('Foreign key constraints does not map in sqlite.');
  83. }
  84. $this->populate();
  85. $this->em->createQuery('DELETE FROM '.self::ARTICLE.' a')->getSingleScalarResult();
  86. $trans = $this->em->getRepository(self::TRANSLATION)->findAll();
  87. $this->assertCount(0, $trans);
  88. }
  89. /**
  90. * @test
  91. */
  92. function shouldOverrideTranslationInEntityBeingTranslated()
  93. {
  94. $this->translatableListener->setDefaultLocale('de');
  95. $article = new Article;
  96. $article->setTitle('override');
  97. $enTranslation = new PersonalArticleTranslation;
  98. $enTranslation
  99. ->setField('title')
  100. ->setContent('en')
  101. ->setObject($article)
  102. ->setLocale('en')
  103. ;
  104. $this->em->persist($enTranslation);
  105. $this->em->persist($article);
  106. $this->em->flush();
  107. $trans = $this->em->createQuery('SELECT t FROM '.self::TRANSLATION.' t')->getArrayResult();
  108. $this->assertCount(1, $trans);
  109. $this->assertEquals('override', $trans[0]['content']);
  110. }
  111. /**
  112. * Covers issue #438
  113. * @test
  114. */
  115. function shouldPersistDefaultLocaleValue()
  116. {
  117. $this->translatableListener->setTranslatableLocale('de');
  118. $article = new Article;
  119. $article->setTitle('de');
  120. $deTranslation = new PersonalArticleTranslation;
  121. $deTranslation
  122. ->setField('title')
  123. ->setContent('de')
  124. ->setObject($article)
  125. ->setLocale('de')
  126. ;
  127. $this->em->persist($deTranslation);
  128. $enTranslation = new PersonalArticleTranslation;
  129. $enTranslation
  130. ->setField('title')
  131. ->setContent('en')
  132. ->setObject($article)
  133. ->setLocale('en')
  134. ;
  135. $this->em->persist($enTranslation);
  136. $this->em->persist($article);
  137. $this->em->flush();
  138. $this->translatableListener->setTranslatableLocale('en');
  139. $articles = $this->em->createQuery('SELECT t FROM '.self::ARTICLE.' t')->getArrayResult();
  140. $this->assertEquals('en', $articles[0]['title']);
  141. $trans = $this->em->createQuery('SELECT t FROM '.self::TRANSLATION.' t')->getArrayResult();
  142. $this->assertCount(2, $trans);
  143. foreach ($trans as $item){
  144. $this->assertEquals($item['locale'], $item['content']);
  145. }
  146. }
  147. /**
  148. * @test
  149. */
  150. function shouldFindFromIdentityMap()
  151. {
  152. $article = new Article;
  153. $article->setTitle('en');
  154. $ltTranslation = new PersonalArticleTranslation;
  155. $ltTranslation
  156. ->setField('title')
  157. ->setContent('lt')
  158. ->setObject($article)
  159. ->setLocale('lt')
  160. ;
  161. $this->em->persist($ltTranslation);
  162. $this->em->persist($article);
  163. $this->em->flush();
  164. $this->startQueryLog();
  165. $this->translatableListener->setTranslatableLocale('lt');
  166. $article->setTitle('change lt');
  167. $this->em->persist($article);
  168. $this->em->flush();
  169. $sqlQueriesExecuted = $this->queryAnalyzer->getExecutedQueries();
  170. $this->assertCount(3, $sqlQueriesExecuted); // one update, transaction start - commit
  171. $this->assertEquals("UPDATE article_translations SET content = 'change lt' WHERE id = 1", $sqlQueriesExecuted[1]);
  172. }
  173. /**
  174. * @test
  175. */
  176. function shouldBeAbleToUseTranslationQueryHint()
  177. {
  178. $this->populate();
  179. $dql = 'SELECT a.title FROM ' . self::ARTICLE . ' a';
  180. $query = $this
  181. ->em->createQuery($dql)
  182. ->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION)
  183. ->setHint(TranslatableListener::HINT_TRANSLATABLE_LOCALE, 'lt')
  184. ;
  185. $this->startQueryLog();
  186. $result = $query->getArrayResult();
  187. $this->assertCount(1, $result);
  188. $this->assertEquals('lt', $result[0]['title']);
  189. $sqlQueriesExecuted = $this->queryAnalyzer->getExecutedQueries();
  190. $this->assertCount(1, $sqlQueriesExecuted);
  191. $this->assertEquals("SELECT t1_.content AS title0 FROM Article a0_ LEFT JOIN article_translations t1_ ON t1_.locale = 'lt' AND t1_.field = 'title' AND t1_.object_id = a0_.id", $sqlQueriesExecuted[0]);
  192. }
  193. private function populate()
  194. {
  195. $article = new Article;
  196. $article->setTitle('en');
  197. $this->em->persist($article);
  198. $this->em->flush();
  199. $this->translatableListener->setTranslatableLocale('de');
  200. $article->setTitle('de');
  201. $ltTranslation = new PersonalArticleTranslation;
  202. $ltTranslation
  203. ->setField('title')
  204. ->setContent('lt')
  205. ->setObject($article)
  206. ->setLocale('lt')
  207. ;
  208. $this->em->persist($ltTranslation);
  209. $this->em->persist($article);
  210. $this->em->flush();
  211. $this->em->clear();
  212. }
  213. protected function getUsedEntityFixtures()
  214. {
  215. return array(
  216. self::ARTICLE,
  217. self::TRANSLATION
  218. );
  219. }
  220. }