EntityTranslationTableTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Gedmo\Translatable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\Common\Util\Debug,
  6. Translatable\Fixture\PersonTranslation,
  7. Translatable\Fixture\Person;
  8. /**
  9. * These are tests for translatable behavior
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class EntityTranslationTableTest extends BaseTestCaseORM
  16. {
  17. const PERSON = 'Translatable\\Fixture\\Person';
  18. const TRANSLATION = 'Translatable\\Fixture\\PersonTranslation';
  19. private $translatableListener;
  20. protected function setUp()
  21. {
  22. parent::setUp();
  23. $evm = new EventManager;
  24. $this->translatableListener = new TranslatableListener();
  25. $this->translatableListener->setTranslatableLocale('en_us');
  26. $this->translatableListener->setDefaultLocale('en_us');
  27. $evm->addEventSubscriber($this->translatableListener);
  28. $this->getMockSqliteEntityManager($evm);
  29. }
  30. public function testFixtureGeneratedTranslations()
  31. {
  32. $person = new Person;
  33. $person->setName('name in en');
  34. $this->em->persist($person);
  35. $this->em->flush();
  36. $this->em->clear();
  37. $repo = $this->em->getRepository(self::TRANSLATION);
  38. $this->assertTrue($repo instanceof Entity\Repository\TranslationRepository);
  39. $translations = $repo->findTranslations($person);
  40. //As Translate locale and Default locale are the same, no records should be present in translations table
  41. $this->assertCount(0, $translations);
  42. // test second translations
  43. $person = $this->em->find(self::PERSON, $person->getId());
  44. $this->translatableListener->setTranslatableLocale('de_de');
  45. $person->setName('name in de');
  46. $this->em->persist($person);
  47. $this->em->flush();
  48. $this->em->clear();
  49. $translations = $repo->findTranslations($person);
  50. //Only one translation should be present
  51. $this->assertCount(1, $translations);
  52. $this->assertArrayHasKey('de_de', $translations);
  53. $this->assertArrayHasKey('name', $translations['de_de']);
  54. $this->assertEquals('name in de', $translations['de_de']['name']);
  55. $this->translatableListener->setTranslatableLocale('en_us');
  56. }
  57. /**
  58. * Covers issue #438
  59. * @test
  60. */
  61. function shouldPersistDefaultLocaleValue()
  62. {
  63. $this->translatableListener->setPersistDefaultLocaleTranslation(true);
  64. $this->translatableListener->setTranslatableLocale('de');
  65. $person = new Person;
  66. $person->setName('de');
  67. $repo = $this->em->getRepository(self::TRANSLATION);
  68. $repo
  69. ->translate($person, 'name', 'de', 'de')
  70. ->translate($person, 'name', 'en_us', 'en_us')
  71. ;
  72. $this->em->persist($person);
  73. $this->em->flush();
  74. $this->translatableListener->setTranslatableLocale('en_us');
  75. $articles = $this->em->createQuery('SELECT p FROM ' . self::PERSON . ' p')->getArrayResult();
  76. $this->assertEquals('en_us', $articles[0]['name']);
  77. $trans = $this->em->createQuery('SELECT t FROM ' . self::TRANSLATION . ' t')->getArrayResult();
  78. $this->assertCount(2, $trans);
  79. foreach ($trans as $item) {
  80. $this->assertEquals($item['locale'], $item['content']);
  81. }
  82. }
  83. protected function getUsedEntityFixtures()
  84. {
  85. return array(
  86. self::PERSON,
  87. self::TRANSLATION
  88. );
  89. }
  90. }