EntityTranslationTableTest.php 3.6KB

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