TranslatableEntityDefaultTranslationTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. function testTwoFieldsWithoutPersistingDefault()
  234. {
  235. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  236. $entity = new Article;
  237. $this->repo
  238. ->translate($entity, 'title' , 'translatedLocale', 'title translatedLocale' )
  239. ->translate($entity, 'title' , 'defaultLocale' , 'title defaultLocale' )
  240. ->translate($entity, 'content', 'translatedLocale', 'content translatedLocale')
  241. ->translate($entity, 'content', 'defaultLocale' , 'content defaultLocale' )
  242. ;
  243. $this->em->persist($entity);
  244. $this->em->flush();
  245. $this->em->clear();
  246. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  247. $this->assertCount(1, $trans);
  248. $this->assertSame('title translatedLocale' , $trans['translatedLocale']['title']);
  249. $this->assertSame('content translatedLocale', $trans['translatedLocale']['content']);
  250. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  251. $this->assertCount(1, $articles);
  252. $this->assertEquals('title defaultLocale' , $articles[0]['title'] );
  253. $this->assertEquals('content defaultLocale', $articles[0]['content']);
  254. }
  255. function testTwoFieldsWithoutPersistingDefaultResorted()
  256. {
  257. $this->translatableListener->setPersistDefaultLocaleTranslation( false );
  258. $entity = new Article;
  259. $this->repo
  260. ->translate($entity, 'title' , 'defaultLocale' , 'title defaultLocale' )
  261. ->translate($entity, 'title' , 'translatedLocale', 'title translatedLocale' )
  262. ->translate($entity, 'content', 'defaultLocale' , 'content defaultLocale' )
  263. ->translate($entity, 'content', 'translatedLocale', 'content translatedLocale')
  264. ;
  265. $this->em->persist($entity);
  266. $this->em->flush();
  267. $this->em->clear();
  268. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  269. $this->assertCount(1, $trans);
  270. $this->assertSame('title translatedLocale' , $trans['translatedLocale']['title']);
  271. $this->assertSame('content translatedLocale', $trans['translatedLocale']['content']);
  272. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  273. $this->assertCount(1, $articles);
  274. $this->assertEquals('title defaultLocale' , $articles[0]['title'] );
  275. $this->assertEquals('content defaultLocale', $articles[0]['content']);
  276. }
  277. function testTwoFieldsWithPersistingDefault()
  278. {
  279. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  280. $entity = new Article;
  281. $this->repo
  282. ->translate($entity, 'title' , 'translatedLocale', 'title translatedLocale' )
  283. ->translate($entity, 'title' , 'defaultLocale' , 'title defaultLocale' )
  284. ->translate($entity, 'content', 'translatedLocale', 'content translatedLocale')
  285. ->translate($entity, 'content', 'defaultLocale' , 'content defaultLocale' )
  286. ;
  287. $this->em->persist($entity);
  288. $this->em->flush();
  289. $this->em->clear();
  290. $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
  291. $this->assertCount(2, $trans);
  292. $this->assertSame('title translatedLocale' , $trans['translatedLocale']['title']);
  293. $this->assertSame('title defaultLocale' , $trans['defaultLocale']['title']);
  294. $this->assertSame('content translatedLocale', $trans['translatedLocale']['content']);
  295. $this->assertSame('content defaultLocale' , $trans['defaultLocale']['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 testTwoFieldsWithPersistingDefaultResorted()
  302. {
  303. $this->translatableListener->setPersistDefaultLocaleTranslation( true );
  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(2, $trans);
  316. $this->assertSame('title translatedLocale' , $trans['translatedLocale']['title']);
  317. $this->assertSame('title defaultLocale' , $trans['defaultLocale']['title']);
  318. $this->assertSame('content translatedLocale', $trans['translatedLocale']['content']);
  319. $this->assertSame('content defaultLocale' , $trans['defaultLocale']['content']);
  320. $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
  321. $this->assertCount(1, $articles);
  322. $this->assertEquals('title defaultLocale' , $articles[0]['title'] );
  323. $this->assertEquals('content defaultLocale', $articles[0]['content']);
  324. }
  325. // --- Fixture related methods ---------------------------------------------
  326. protected function getUsedEntityFixtures()
  327. {
  328. return array(
  329. self::ARTICLE,
  330. self::TRANSLATION
  331. );
  332. }
  333. }