TranslatableEntityDefaultTranslationTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. * @link http://www.gediminasm.org
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class TranslatableEntityDefaultTranslationTest extends BaseTestCaseORM
  15. {
  16. const ARTICLE = 'Translatable\\Fixture\\Article';
  17. const COMMENT = 'Translatable\\Fixture\\Comment';
  18. const TRANSLATION = 'Gedmo\\Translatable\\Entity\\Translation';
  19. private $translatableListener;
  20. protected function setUp()
  21. {
  22. parent::setUp();
  23. $evm = new EventManager;
  24. $this->translatableListener = new TranslatableListener();
  25. $this->translatableListener->setTranslatableLocale('translatedLocale');
  26. $this->translatableListener->setDefaultLocale('defaultLocale');
  27. $evm->addEventSubscriber($this->translatableListener);
  28. $conn = array(
  29. 'driver' => 'pdo_mysql',
  30. 'host' => '127.0.0.1',
  31. 'dbname' => 'test',
  32. 'user' => 'root',
  33. 'password' => 'nimda'
  34. );
  35. //$this->getMockCustomEntityManager($conn, $evm);
  36. $this->getMockSqliteEntityManager($evm);
  37. $this->repo = $this->em->getRepository(self::TRANSLATION);
  38. }
  39. // --- Tests for default translation overruling the translated entity
  40. // property ------------------------------------------------------------
  41. function testTranslatedPropertyWithoutPersistingDefault()
  42. {
  43. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  44. $entity = new Article;
  45. $this->repo
  46. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  47. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  48. ;
  49. $this->assertSame('title translatedLocale', $entity->getTitle());
  50. }
  51. function testTranslatedPropertyWithoutPersistingDefaultResorted()
  52. {
  53. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  54. $entity = new Article;
  55. $this->repo
  56. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  57. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  58. ;
  59. $this->assertSame('title translatedLocale', $entity->getTitle());
  60. }
  61. function testTranslatedPropertyWithPersistingDefault()
  62. {
  63. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  64. $entity = new Article;
  65. $this->repo
  66. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  67. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  68. ;
  69. $this->assertSame('title translatedLocale', $entity->getTitle());
  70. }
  71. function testTranslatedPropertyWithPersistingDefaultResorted()
  72. {
  73. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  74. $entity = new Article;
  75. $this->repo
  76. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  77. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  78. ;
  79. $this->assertSame('title translatedLocale', $entity->getTitle());
  80. }
  81. // --- Tests for default translation making it into the entity's
  82. // database row --------------------------------------------------------
  83. function testOnlyDefaultTranslationWithoutPersistingDefault()
  84. {
  85. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  86. $entity = new Article;
  87. $this->repo
  88. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  89. ;
  90. $this->em->persist($entity);
  91. $this->em->flush();
  92. $this->em->clear();
  93. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  94. $this->assertCount(0, $trans);
  95. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  96. $this->assertCount(1, $articles);
  97. $this->assertEquals('title defaultLocale', $articles[0]['title']);
  98. }
  99. function testOnlyDefaultTranslationWithPersistingDefault()
  100. {
  101. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  102. $entity = new Article;
  103. $this->repo
  104. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  105. ;
  106. $this->em->persist($entity);
  107. $this->em->flush();
  108. $this->em->clear();
  109. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  110. $this->assertCount(1, $trans);
  111. $this->assertSame('title defaultLocale', $trans['defaultLocale']['title']);
  112. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  113. $this->assertCount(1, $articles);
  114. $this->assertEquals('title defaultLocale', $articles[0]['title']);
  115. }
  116. function testUpdateTranslationInDefaultLocale()
  117. {
  118. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  119. $entity = new Article;
  120. $this->repo
  121. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  122. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale');
  123. $this->em->persist($entity);
  124. $this->em->flush();
  125. $this->em->clear();
  126. $entity = $this->em->find(self::ARTICLE, 1);
  127. $entity->setTranslatableLocale('translatedLocale');
  128. $this->em->refresh($entity);
  129. $this->repo
  130. ->translate($entity, 'title', 'defaultLocale', 'update title defaultLocale');
  131. $this->em->flush();
  132. $qb = $this->em->createQueryBuilder('a');
  133. $qb->select('a')
  134. ->from(self::ARTICLE, 'a')
  135. ->where('a.id = 1');
  136. $fields = $qb->getQuery()->getArrayResult();
  137. $this->assertEquals( 'update title defaultLocale', $fields[0]['title']);
  138. }
  139. function testUpdateTranslationWithPersistingInDefaultLocale()
  140. {
  141. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  142. $entity = new Article;
  143. $this->repo
  144. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  145. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale');
  146. $this->em->persist($entity);
  147. $this->em->flush();
  148. $this->em->clear();
  149. $entity = $this->em->find(self::ARTICLE, 1);
  150. $entity->setTranslatableLocale('translatedLocale');
  151. $this->em->refresh($entity);
  152. $this->repo
  153. ->translate($entity, 'title', 'defaultLocale', 'update title defaultLocale');
  154. $this->em->flush();
  155. $qb = $this->em->createQueryBuilder('a');
  156. $qb->select('a')
  157. ->from(self::ARTICLE, 'a')
  158. ->where('a.id = 1');
  159. $fields = $qb->getQuery()->getArrayResult();
  160. $this->assertEquals( 'update title defaultLocale', $fields[0]['title']);
  161. }
  162. /**
  163. * As this test does not provide a default translation, we assert
  164. * that a translated value is picked as default value
  165. */
  166. function testOnlyEntityTranslationWithoutPersistingDefault()
  167. {
  168. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  169. $entity = new Article;
  170. $this->repo
  171. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  172. ;
  173. $this->em->persist($entity);
  174. $this->em->flush();
  175. $this->em->clear();
  176. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  177. $this->assertCount(1, $trans);
  178. $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
  179. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  180. $this->assertCount(1, $articles);
  181. $this->assertEquals('title translatedLocale', $articles[0]['title']);
  182. }
  183. /**
  184. * As this test does not provide a default translation, we assert
  185. * that a translated value is picked as default value
  186. */
  187. function testOnlyEntityTranslationWithPersistingDefault()
  188. {
  189. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  190. $entity = new Article;
  191. $this->repo
  192. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  193. ;
  194. $this->em->persist($entity);
  195. $this->em->flush();
  196. $this->em->clear();
  197. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  198. $this->assertCount(1, $trans);
  199. $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
  200. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  201. $this->assertCount(1, $articles);
  202. $this->assertEquals('title translatedLocale', $articles[0]['title']);
  203. }
  204. function testDefaultAndEntityTranslationWithoutPersistingDefault()
  205. {
  206. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  207. $entity = new Article;
  208. $this->repo
  209. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  210. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  211. ;
  212. $this->em->persist($entity);
  213. $this->em->flush();
  214. $this->em->clear();
  215. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  216. $this->assertCount(1, $trans);
  217. $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
  218. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  219. $this->assertCount(1, $articles);
  220. $this->assertEquals('title defaultLocale', $articles[0]['title']);
  221. }
  222. function testDefaultAndEntityTranslationWithoutPersistingDefaultResorted()
  223. {
  224. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  225. $entity = new Article;
  226. $this->repo
  227. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  228. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  229. ;
  230. $this->em->persist($entity);
  231. $this->em->flush();
  232. $this->em->clear();
  233. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  234. $this->assertCount(1, $trans);
  235. $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
  236. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  237. $this->assertCount(1, $articles);
  238. $this->assertEquals('title defaultLocale', $articles[0]['title']);
  239. }
  240. function testDefaultAndEntityTranslationWithPersistingDefault()
  241. {
  242. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  243. $entity = new Article;
  244. $this->repo
  245. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  246. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  247. ;
  248. $this->em->persist($entity);
  249. $this->em->flush();
  250. $this->em->clear();
  251. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  252. $this->assertCount(2, $trans);
  253. $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
  254. $this->assertSame('title defaultLocale', $trans['defaultLocale']['title']);
  255. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  256. $this->assertCount(1, $articles);
  257. $this->assertEquals('title defaultLocale', $articles[0]['title']);
  258. }
  259. function testDefaultAndEntityTranslationWithPersistingDefaultResorted()
  260. {
  261. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  262. $entity = new Article;
  263. $this->repo
  264. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  265. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  266. ;
  267. $this->em->persist($entity);
  268. $this->em->flush();
  269. $this->em->clear();
  270. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  271. $this->assertCount(2, $trans);
  272. $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
  273. $this->assertSame('title defaultLocale', $trans['defaultLocale']['title']);
  274. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  275. $this->assertCount(1, $articles);
  276. $this->assertEquals('title defaultLocale', $articles[0]['title']);
  277. }
  278. function testTwoFieldsWithoutPersistingDefault()
  279. {
  280. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  281. $entity = new Article;
  282. $this->repo
  283. ->translate($entity, 'title' , 'translatedLocale', 'title translatedLocale' )
  284. ->translate($entity, 'title' , 'defaultLocale' , 'title defaultLocale' )
  285. ->translate($entity, 'content', 'translatedLocale', 'content translatedLocale')
  286. ->translate($entity, 'content', 'defaultLocale' , 'content defaultLocale' )
  287. ;
  288. $this->em->persist($entity);
  289. $this->em->flush();
  290. $this->em->clear();
  291. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  292. $this->assertCount(1, $trans);
  293. $this->assertSame('title translatedLocale' , $trans['translatedLocale']['title']);
  294. $this->assertSame('content translatedLocale', $trans['translatedLocale']['content']);
  295. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  296. $this->assertCount(1, $articles);
  297. $this->assertEquals('title defaultLocale' , $articles[0]['title'] );
  298. $this->assertEquals('content defaultLocale', $articles[0]['content']);
  299. }
  300. function testTwoFieldsWithoutPersistingDefaultResorted()
  301. {
  302. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  303. $entity = new Article;
  304. $this->repo
  305. ->translate($entity, 'title' , 'defaultLocale' , 'title defaultLocale' )
  306. ->translate($entity, 'title' , 'translatedLocale', 'title translatedLocale' )
  307. ->translate($entity, 'content', 'defaultLocale' , 'content defaultLocale' )
  308. ->translate($entity, 'content', 'translatedLocale', 'content translatedLocale')
  309. ;
  310. $this->em->persist($entity);
  311. $this->em->flush();
  312. $this->em->clear();
  313. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  314. $this->assertCount(1, $trans);
  315. $this->assertSame('title translatedLocale' , $trans['translatedLocale']['title']);
  316. $this->assertSame('content translatedLocale', $trans['translatedLocale']['content']);
  317. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  318. $this->assertCount(1, $articles);
  319. $this->assertEquals('title defaultLocale' , $articles[0]['title'] );
  320. $this->assertEquals('content defaultLocale', $articles[0]['content']);
  321. }
  322. function testTwoFieldsWithPersistingDefault()
  323. {
  324. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  325. $entity = new Article;
  326. $this->repo
  327. ->translate($entity, 'title' , 'translatedLocale', 'title translatedLocale' )
  328. ->translate($entity, 'title' , 'defaultLocale' , 'title defaultLocale' )
  329. ->translate($entity, 'content', 'translatedLocale', 'content translatedLocale')
  330. ->translate($entity, 'content', 'defaultLocale' , 'content defaultLocale' )
  331. ;
  332. $this->em->persist($entity);
  333. $this->em->flush();
  334. $this->em->clear();
  335. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  336. $this->assertCount(2, $trans);
  337. $this->assertSame('title translatedLocale' , $trans['translatedLocale']['title']);
  338. $this->assertSame('title defaultLocale' , $trans['defaultLocale']['title']);
  339. $this->assertSame('content translatedLocale', $trans['translatedLocale']['content']);
  340. $this->assertSame('content defaultLocale' , $trans['defaultLocale']['content']);
  341. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  342. $this->assertCount(1, $articles);
  343. $this->assertEquals('title defaultLocale' , $articles[0]['title'] );
  344. $this->assertEquals('content defaultLocale', $articles[0]['content']);
  345. }
  346. function testTwoFieldsWithPersistingDefaultResorted()
  347. {
  348. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  349. $entity = new Article;
  350. $this->repo
  351. ->translate($entity, 'title' , 'defaultLocale' , 'title defaultLocale' )
  352. ->translate($entity, 'title' , 'translatedLocale', 'title translatedLocale' )
  353. ->translate($entity, 'content', 'defaultLocale' , 'content defaultLocale' )
  354. ->translate($entity, 'content', 'translatedLocale', 'content translatedLocale')
  355. ;
  356. $this->em->persist($entity);
  357. $this->em->flush();
  358. $this->em->clear();
  359. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  360. $this->assertCount(2, $trans);
  361. $this->assertSame('title translatedLocale' , $trans['translatedLocale']['title']);
  362. $this->assertSame('title defaultLocale' , $trans['defaultLocale']['title']);
  363. $this->assertSame('content translatedLocale', $trans['translatedLocale']['content']);
  364. $this->assertSame('content defaultLocale' , $trans['defaultLocale']['content']);
  365. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  366. $this->assertCount(1, $articles);
  367. $this->assertEquals('title defaultLocale' , $articles[0]['title'] );
  368. $this->assertEquals('content defaultLocale', $articles[0]['content']);
  369. }
  370. // --- Fixture related methods ---------------------------------------------
  371. protected function getUsedEntityFixtures()
  372. {
  373. return array(
  374. self::ARTICLE,
  375. self::TRANSLATION
  376. );
  377. }
  378. }