TranslatableEntityDefaultTranslationTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. /**
  8. * These are tests for translatable behavior
  9. *
  10. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  11. * @package Gedmo.Translatable
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class TranslatableEntityDefaultTranslationTest extends BaseTestCaseORM
  16. {
  17. const ARTICLE = 'Translatable\\Fixture\\Article';
  18. const COMMENT = 'Translatable\\Fixture\\Comment';
  19. const TRANSLATION = 'Gedmo\\Translatable\\Entity\\Translation';
  20. private $translatableListener;
  21. protected function setUp()
  22. {
  23. parent::setUp();
  24. $evm = new EventManager;
  25. $this->translatableListener = new TranslatableListener();
  26. $this->translatableListener->setTranslatableLocale('translatedLocale');
  27. $this->translatableListener->setDefaultLocale('defaultLocale');
  28. $evm->addEventSubscriber($this->translatableListener);
  29. $conn = array(
  30. 'driver' => 'pdo_mysql',
  31. 'host' => '127.0.0.1',
  32. 'dbname' => 'test',
  33. 'user' => 'root',
  34. 'password' => 'nimda'
  35. );
  36. //$this->getMockCustomEntityManager($conn, $evm);
  37. $this->getMockSqliteEntityManager($evm);
  38. $this->repo = $this->em->getRepository(self::TRANSLATION);
  39. }
  40. // --- Tests for default translation overruling the translated entity
  41. // property ------------------------------------------------------------
  42. function testTranslatedPropertyWithoutPersistingDefault()
  43. {
  44. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  45. $entity = new Article;
  46. $this->repo
  47. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  48. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  49. ;
  50. $this->assertSame('title translatedLocale', $entity->getTitle());
  51. }
  52. function testTranslatedPropertyWithoutPersistingDefaultResorted()
  53. {
  54. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  55. $entity = new Article;
  56. $this->repo
  57. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  58. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  59. ;
  60. $this->assertSame('title translatedLocale', $entity->getTitle());
  61. }
  62. function testTranslatedPropertyWithPersistingDefault()
  63. {
  64. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  65. $entity = new Article;
  66. $this->repo
  67. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  68. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  69. ;
  70. $this->assertSame('title translatedLocale', $entity->getTitle());
  71. }
  72. function testTranslatedPropertyWithPersistingDefaultResorted()
  73. {
  74. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  75. $entity = new Article;
  76. $this->repo
  77. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  78. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  79. ;
  80. $this->assertSame('title translatedLocale', $entity->getTitle());
  81. }
  82. // --- Tests for default translation making it into the entity's
  83. // database row --------------------------------------------------------
  84. function testOnlyDefaultTranslationWithoutPersistingDefault()
  85. {
  86. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  87. $entity = new Article;
  88. $this->repo
  89. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  90. ;
  91. $this->em->persist($entity);
  92. $this->em->flush();
  93. $this->em->clear();
  94. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  95. $this->assertCount(0, $trans);
  96. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  97. $this->assertCount(1, $articles);
  98. $this->assertEquals('title defaultLocale', $articles[0]['title']);
  99. }
  100. function testOnlyDefaultTranslationWithPersistingDefault()
  101. {
  102. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  103. $entity = new Article;
  104. $this->repo
  105. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  106. ;
  107. $this->em->persist($entity);
  108. $this->em->flush();
  109. $this->em->clear();
  110. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  111. $this->assertCount(1, $trans);
  112. $this->assertSame('title defaultLocale', $trans['defaultLocale']['title']);
  113. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  114. $this->assertCount(1, $articles);
  115. $this->assertEquals('title defaultLocale', $articles[0]['title']);
  116. }
  117. /**
  118. * As this test does not provide a default translation, we assert
  119. * that a translated value is picked as default value
  120. */
  121. function testOnlyEntityTranslationWithoutPersistingDefault()
  122. {
  123. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  124. $entity = new Article;
  125. $this->repo
  126. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  127. ;
  128. $this->em->persist($entity);
  129. $this->em->flush();
  130. $this->em->clear();
  131. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  132. $this->assertCount(1, $trans);
  133. $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
  134. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  135. $this->assertCount(1, $articles);
  136. $this->assertEquals('title translatedLocale', $articles[0]['title']);
  137. }
  138. /**
  139. * As this test does not provide a default translation, we assert
  140. * that a translated value is picked as default value
  141. */
  142. function testOnlyEntityTranslationWithPersistingDefault()
  143. {
  144. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  145. $entity = new Article;
  146. $this->repo
  147. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  148. ;
  149. $this->em->persist($entity);
  150. $this->em->flush();
  151. $this->em->clear();
  152. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  153. $this->assertCount(1, $trans);
  154. $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
  155. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  156. $this->assertCount(1, $articles);
  157. $this->assertEquals('title translatedLocale', $articles[0]['title']);
  158. }
  159. function testDefaultAndEntityTranslationWithoutPersistingDefault()
  160. {
  161. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  162. $entity = new Article;
  163. $this->repo
  164. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  165. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  166. ;
  167. $this->em->persist($entity);
  168. $this->em->flush();
  169. $this->em->clear();
  170. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  171. $this->assertCount(1, $trans);
  172. $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
  173. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  174. $this->assertCount(1, $articles);
  175. $this->assertEquals('title defaultLocale', $articles[0]['title']);
  176. }
  177. function testDefaultAndEntityTranslationWithoutPersistingDefaultResorted()
  178. {
  179. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  180. $entity = new Article;
  181. $this->repo
  182. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  183. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  184. ;
  185. $this->em->persist($entity);
  186. $this->em->flush();
  187. $this->em->clear();
  188. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  189. $this->assertCount(1, $trans);
  190. $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
  191. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  192. $this->assertCount(1, $articles);
  193. $this->assertEquals('title defaultLocale', $articles[0]['title']);
  194. }
  195. function testDefaultAndEntityTranslationWithPersistingDefault()
  196. {
  197. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  198. $entity = new Article;
  199. $this->repo
  200. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  201. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  202. ;
  203. $this->em->persist($entity);
  204. $this->em->flush();
  205. $this->em->clear();
  206. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  207. $this->assertCount(2, $trans);
  208. $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
  209. $this->assertSame('title defaultLocale', $trans['defaultLocale']['title']);
  210. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  211. $this->assertCount(1, $articles);
  212. $this->assertEquals('title defaultLocale', $articles[0]['title']);
  213. }
  214. function testDefaultAndEntityTranslationWithPersistingDefaultResorted()
  215. {
  216. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  217. $entity = new Article;
  218. $this->repo
  219. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  220. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  221. ;
  222. $this->em->persist($entity);
  223. $this->em->flush();
  224. $this->em->clear();
  225. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  226. $this->assertCount(2, $trans);
  227. $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
  228. $this->assertSame('title defaultLocale', $trans['defaultLocale']['title']);
  229. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  230. $this->assertCount(1, $articles);
  231. $this->assertEquals('title defaultLocale', $articles[0]['title']);
  232. }
  233. // --- Fixture related methods ---------------------------------------------
  234. protected function getUsedEntityFixtures()
  235. {
  236. return array(
  237. self::ARTICLE,
  238. self::TRANSLATION
  239. );
  240. }
  241. }