TranslatableEntityDefaultTranslationTest.php 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. function testUpdateTranslationInDefaultLocale()
  118. {
  119. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  120. $entity = new Article;
  121. $this->repo
  122. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  123. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale');
  124. $this->em->persist($entity);
  125. $this->em->flush();
  126. $this->em->clear();
  127. $entity = $this->em->find(self::ARTICLE, 1);
  128. $entity->setTranslatableLocale('translatedLocale');
  129. $this->em->refresh($entity);
  130. $this->repo
  131. ->translate($entity, 'title', 'defaultLocale', 'update title defaultLocale');
  132. $this->em->flush();
  133. $qb = $this->em->createQueryBuilder('a');
  134. $qb->select('a')
  135. ->from(self::ARTICLE, 'a')
  136. ->where('a.id = 1');
  137. $fields = $qb->getQuery()->getArrayResult();
  138. $this->assertEquals( 'update title defaultLocale', $fields[0]['title']);
  139. }
  140. function testUpdateTranslationWithPersistingInDefaultLocale()
  141. {
  142. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  143. $entity = new Article;
  144. $this->repo
  145. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  146. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale');
  147. $this->em->persist($entity);
  148. $this->em->flush();
  149. $this->em->clear();
  150. $entity = $this->em->find(self::ARTICLE, 1);
  151. $entity->setTranslatableLocale('translatedLocale');
  152. $this->em->refresh($entity);
  153. $this->repo
  154. ->translate($entity, 'title', 'defaultLocale', 'update title defaultLocale');
  155. $this->em->flush();
  156. $qb = $this->em->createQueryBuilder('a');
  157. $qb->select('a')
  158. ->from(self::ARTICLE, 'a')
  159. ->where('a.id = 1');
  160. $fields = $qb->getQuery()->getArrayResult();
  161. $this->assertEquals( 'update title defaultLocale', $fields[0]['title']);
  162. }
  163. /**
  164. * As this test does not provide a default translation, we assert
  165. * that a translated value is picked as default value
  166. */
  167. function testOnlyEntityTranslationWithoutPersistingDefault()
  168. {
  169. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  170. $entity = new Article;
  171. $this->repo
  172. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  173. ;
  174. $this->em->persist($entity);
  175. $this->em->flush();
  176. $this->em->clear();
  177. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  178. $this->assertCount(1, $trans);
  179. $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
  180. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  181. $this->assertCount(1, $articles);
  182. $this->assertEquals('title translatedLocale', $articles[0]['title']);
  183. }
  184. /**
  185. * As this test does not provide a default translation, we assert
  186. * that a translated value is picked as default value
  187. */
  188. function testOnlyEntityTranslationWithPersistingDefault()
  189. {
  190. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  191. $entity = new Article;
  192. $this->repo
  193. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  194. ;
  195. $this->em->persist($entity);
  196. $this->em->flush();
  197. $this->em->clear();
  198. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  199. $this->assertCount(1, $trans);
  200. $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
  201. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  202. $this->assertCount(1, $articles);
  203. $this->assertEquals('title translatedLocale', $articles[0]['title']);
  204. }
  205. function testDefaultAndEntityTranslationWithoutPersistingDefault()
  206. {
  207. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  208. $entity = new Article;
  209. $this->repo
  210. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  211. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  212. ;
  213. $this->em->persist($entity);
  214. $this->em->flush();
  215. $this->em->clear();
  216. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  217. $this->assertCount(1, $trans);
  218. $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
  219. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  220. $this->assertCount(1, $articles);
  221. $this->assertEquals('title defaultLocale', $articles[0]['title']);
  222. }
  223. function testDefaultAndEntityTranslationWithoutPersistingDefaultResorted()
  224. {
  225. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  226. $entity = new Article;
  227. $this->repo
  228. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  229. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  230. ;
  231. $this->em->persist($entity);
  232. $this->em->flush();
  233. $this->em->clear();
  234. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  235. $this->assertCount(1, $trans);
  236. $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
  237. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  238. $this->assertCount(1, $articles);
  239. $this->assertEquals('title defaultLocale', $articles[0]['title']);
  240. }
  241. function testDefaultAndEntityTranslationWithPersistingDefault()
  242. {
  243. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  244. $entity = new Article;
  245. $this->repo
  246. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  247. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  248. ;
  249. $this->em->persist($entity);
  250. $this->em->flush();
  251. $this->em->clear();
  252. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  253. $this->assertCount(2, $trans);
  254. $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
  255. $this->assertSame('title defaultLocale', $trans['defaultLocale']['title']);
  256. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  257. $this->assertCount(1, $articles);
  258. $this->assertEquals('title defaultLocale', $articles[0]['title']);
  259. }
  260. function testDefaultAndEntityTranslationWithPersistingDefaultResorted()
  261. {
  262. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  263. $entity = new Article;
  264. $this->repo
  265. ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
  266. ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
  267. ;
  268. $this->em->persist($entity);
  269. $this->em->flush();
  270. $this->em->clear();
  271. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  272. $this->assertCount(2, $trans);
  273. $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
  274. $this->assertSame('title defaultLocale', $trans['defaultLocale']['title']);
  275. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  276. $this->assertCount(1, $articles);
  277. $this->assertEquals('title defaultLocale', $articles[0]['title']);
  278. }
  279. function testTwoFieldsWithoutPersistingDefault()
  280. {
  281. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  282. $entity = new Article;
  283. $this->repo
  284. ->translate($entity, 'title' , 'translatedLocale', 'title translatedLocale' )
  285. ->translate($entity, 'title' , 'defaultLocale' , 'title defaultLocale' )
  286. ->translate($entity, 'content', 'translatedLocale', 'content translatedLocale')
  287. ->translate($entity, 'content', 'defaultLocale' , 'content defaultLocale' )
  288. ;
  289. $this->em->persist($entity);
  290. $this->em->flush();
  291. $this->em->clear();
  292. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  293. $this->assertCount(1, $trans);
  294. $this->assertSame('title translatedLocale' , $trans['translatedLocale']['title']);
  295. $this->assertSame('content translatedLocale', $trans['translatedLocale']['content']);
  296. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  297. $this->assertCount(1, $articles);
  298. $this->assertEquals('title defaultLocale' , $articles[0]['title'] );
  299. $this->assertEquals('content defaultLocale', $articles[0]['content']);
  300. }
  301. function testTwoFieldsWithoutPersistingDefaultResorted()
  302. {
  303. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  304. $entity = new Article;
  305. $this->repo
  306. ->translate($entity, 'title' , 'defaultLocale' , 'title defaultLocale' )
  307. ->translate($entity, 'title' , 'translatedLocale', 'title translatedLocale' )
  308. ->translate($entity, 'content', 'defaultLocale' , 'content defaultLocale' )
  309. ->translate($entity, 'content', 'translatedLocale', 'content translatedLocale')
  310. ;
  311. $this->em->persist($entity);
  312. $this->em->flush();
  313. $this->em->clear();
  314. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  315. $this->assertCount(1, $trans);
  316. $this->assertSame('title translatedLocale' , $trans['translatedLocale']['title']);
  317. $this->assertSame('content translatedLocale', $trans['translatedLocale']['content']);
  318. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  319. $this->assertCount(1, $articles);
  320. $this->assertEquals('title defaultLocale' , $articles[0]['title'] );
  321. $this->assertEquals('content defaultLocale', $articles[0]['content']);
  322. }
  323. function testTwoFieldsWithPersistingDefault()
  324. {
  325. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  326. $entity = new Article;
  327. $this->repo
  328. ->translate($entity, 'title' , 'translatedLocale', 'title translatedLocale' )
  329. ->translate($entity, 'title' , 'defaultLocale' , 'title defaultLocale' )
  330. ->translate($entity, 'content', 'translatedLocale', 'content translatedLocale')
  331. ->translate($entity, 'content', 'defaultLocale' , 'content defaultLocale' )
  332. ;
  333. $this->em->persist($entity);
  334. $this->em->flush();
  335. $this->em->clear();
  336. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  337. $this->assertCount(2, $trans);
  338. $this->assertSame('title translatedLocale' , $trans['translatedLocale']['title']);
  339. $this->assertSame('title defaultLocale' , $trans['defaultLocale']['title']);
  340. $this->assertSame('content translatedLocale', $trans['translatedLocale']['content']);
  341. $this->assertSame('content defaultLocale' , $trans['defaultLocale']['content']);
  342. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  343. $this->assertCount(1, $articles);
  344. $this->assertEquals('title defaultLocale' , $articles[0]['title'] );
  345. $this->assertEquals('content defaultLocale', $articles[0]['content']);
  346. }
  347. function testTwoFieldsWithPersistingDefaultResorted()
  348. {
  349. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  350. $entity = new Article;
  351. $this->repo
  352. ->translate($entity, 'title' , 'defaultLocale' , 'title defaultLocale' )
  353. ->translate($entity, 'title' , 'translatedLocale', 'title translatedLocale' )
  354. ->translate($entity, 'content', 'defaultLocale' , 'content defaultLocale' )
  355. ->translate($entity, 'content', 'translatedLocale', 'content translatedLocale')
  356. ;
  357. $this->em->persist($entity);
  358. $this->em->flush();
  359. $this->em->clear();
  360. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  361. $this->assertCount(2, $trans);
  362. $this->assertSame('title translatedLocale' , $trans['translatedLocale']['title']);
  363. $this->assertSame('title defaultLocale' , $trans['defaultLocale']['title']);
  364. $this->assertSame('content translatedLocale', $trans['translatedLocale']['content']);
  365. $this->assertSame('content defaultLocale' , $trans['defaultLocale']['content']);
  366. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  367. $this->assertCount(1, $articles);
  368. $this->assertEquals('title defaultLocale' , $articles[0]['title'] );
  369. $this->assertEquals('content defaultLocale', $articles[0]['content']);
  370. }
  371. // --- Fixture related methods ---------------------------------------------
  372. protected function getUsedEntityFixtures()
  373. {
  374. return array(
  375. self::ARTICLE,
  376. self::TRANSLATION
  377. );
  378. }
  379. }