123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- <?php
- namespace Gedmo\Translatable;
- use Doctrine\Common\EventManager;
- use Tool\BaseTestCaseORM;
- use Translatable\Fixture\Article;
- use Translatable\Fixture\Comment;
- /**
- * These are tests for translatable behavior
- *
- * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
- * @link http://www.gediminasm.org
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- class TranslatableEntityDefaultTranslationTest extends BaseTestCaseORM
- {
- const ARTICLE = 'Translatable\\Fixture\\Article';
- const COMMENT = 'Translatable\\Fixture\\Comment';
- const TRANSLATION = 'Gedmo\\Translatable\\Entity\\Translation';
- private $translatableListener;
- protected function setUp()
- {
- parent::setUp();
- $evm = new EventManager;
- $this->translatableListener = new TranslatableListener();
- $this->translatableListener->setTranslatableLocale('translatedLocale');
- $this->translatableListener->setDefaultLocale('defaultLocale');
- $evm->addEventSubscriber($this->translatableListener);
- $conn = array(
- 'driver' => 'pdo_mysql',
- 'host' => '127.0.0.1',
- 'dbname' => 'test',
- 'user' => 'root',
- 'password' => 'nimda'
- );
- //$this->getMockCustomEntityManager($conn, $evm);
- $this->getMockSqliteEntityManager($evm);
- $this->repo = $this->em->getRepository(self::TRANSLATION);
- }
- // --- Tests for default translation overruling the translated entity
- // property ------------------------------------------------------------
- function testTranslatedPropertyWithoutPersistingDefault()
- {
- $this->translatableListener->setPersistDefaultLocaleTranslation( false );
- $entity = new Article;
- $this->repo
- ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
- ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
- ;
- $this->assertSame('title translatedLocale', $entity->getTitle());
- }
- function testTranslatedPropertyWithoutPersistingDefaultResorted()
- {
- $this->translatableListener->setPersistDefaultLocaleTranslation( false );
- $entity = new Article;
- $this->repo
- ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
- ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
- ;
- $this->assertSame('title translatedLocale', $entity->getTitle());
- }
- function testTranslatedPropertyWithPersistingDefault()
- {
- $this->translatableListener->setPersistDefaultLocaleTranslation( true );
- $entity = new Article;
- $this->repo
- ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
- ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
- ;
- $this->assertSame('title translatedLocale', $entity->getTitle());
- }
- function testTranslatedPropertyWithPersistingDefaultResorted()
- {
- $this->translatableListener->setPersistDefaultLocaleTranslation( true );
- $entity = new Article;
- $this->repo
- ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
- ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
- ;
- $this->assertSame('title translatedLocale', $entity->getTitle());
- }
- // --- Tests for default translation making it into the entity's
- // database row --------------------------------------------------------
- function testOnlyDefaultTranslationWithoutPersistingDefault()
- {
- $this->translatableListener->setPersistDefaultLocaleTranslation( false );
- $entity = new Article;
- $this->repo
- ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
- ;
- $this->em->persist($entity);
- $this->em->flush();
- $this->em->clear();
- $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
- $this->assertCount(0, $trans);
- $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
- $this->assertCount(1, $articles);
- $this->assertEquals('title defaultLocale', $articles[0]['title']);
- }
- function testOnlyDefaultTranslationWithPersistingDefault()
- {
- $this->translatableListener->setPersistDefaultLocaleTranslation( true );
- $entity = new Article;
- $this->repo
- ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
- ;
- $this->em->persist($entity);
- $this->em->flush();
- $this->em->clear();
- $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
- $this->assertCount(1, $trans);
- $this->assertSame('title defaultLocale', $trans['defaultLocale']['title']);
- $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
- $this->assertCount(1, $articles);
- $this->assertEquals('title defaultLocale', $articles[0]['title']);
- }
- function testUpdateTranslationInDefaultLocale()
- {
- $this->translatableListener->setPersistDefaultLocaleTranslation( false );
- $entity = new Article;
- $this->repo
- ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
- ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale');
- $this->em->persist($entity);
- $this->em->flush();
- $this->em->clear();
- $entity = $this->em->find(self::ARTICLE, 1);
- $entity->setTranslatableLocale('translatedLocale');
- $this->em->refresh($entity);
- $this->repo
- ->translate($entity, 'title', 'defaultLocale', 'update title defaultLocale');
- $this->em->flush();
- $qb = $this->em->createQueryBuilder('a');
- $qb->select('a')
- ->from(self::ARTICLE, 'a')
- ->where('a.id = 1');
- $fields = $qb->getQuery()->getArrayResult();
- $this->assertEquals( 'update title defaultLocale', $fields[0]['title']);
- }
- function testUpdateTranslationWithPersistingInDefaultLocale()
- {
- $this->translatableListener->setPersistDefaultLocaleTranslation( true );
- $entity = new Article;
- $this->repo
- ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
- ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale');
- $this->em->persist($entity);
- $this->em->flush();
- $this->em->clear();
- $entity = $this->em->find(self::ARTICLE, 1);
- $entity->setTranslatableLocale('translatedLocale');
- $this->em->refresh($entity);
- $this->repo
- ->translate($entity, 'title', 'defaultLocale', 'update title defaultLocale');
- $this->em->flush();
- $qb = $this->em->createQueryBuilder('a');
- $qb->select('a')
- ->from(self::ARTICLE, 'a')
- ->where('a.id = 1');
- $fields = $qb->getQuery()->getArrayResult();
- $this->assertEquals( 'update title defaultLocale', $fields[0]['title']);
- }
- /**
- * As this test does not provide a default translation, we assert
- * that a translated value is picked as default value
- */
- function testOnlyEntityTranslationWithoutPersistingDefault()
- {
- $this->translatableListener->setPersistDefaultLocaleTranslation( false );
- $entity = new Article;
- $this->repo
- ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
- ;
- $this->em->persist($entity);
- $this->em->flush();
- $this->em->clear();
- $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
- $this->assertCount(1, $trans);
- $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
- $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
- $this->assertCount(1, $articles);
- $this->assertEquals('title translatedLocale', $articles[0]['title']);
- }
- /**
- * As this test does not provide a default translation, we assert
- * that a translated value is picked as default value
- */
- function testOnlyEntityTranslationWithPersistingDefault()
- {
- $this->translatableListener->setPersistDefaultLocaleTranslation( true );
- $entity = new Article;
- $this->repo
- ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
- ;
- $this->em->persist($entity);
- $this->em->flush();
- $this->em->clear();
- $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
- $this->assertCount(1, $trans);
- $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
- $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
- $this->assertCount(1, $articles);
- $this->assertEquals('title translatedLocale', $articles[0]['title']);
- }
- function testDefaultAndEntityTranslationWithoutPersistingDefault()
- {
- $this->translatableListener->setPersistDefaultLocaleTranslation( false );
- $entity = new Article;
- $this->repo
- ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
- ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
- ;
- $this->em->persist($entity);
- $this->em->flush();
- $this->em->clear();
- $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
- $this->assertCount(1, $trans);
- $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
- $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
- $this->assertCount(1, $articles);
- $this->assertEquals('title defaultLocale', $articles[0]['title']);
- }
- function testDefaultAndEntityTranslationWithoutPersistingDefaultResorted()
- {
- $this->translatableListener->setPersistDefaultLocaleTranslation( false );
- $entity = new Article;
- $this->repo
- ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
- ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
- ;
- $this->em->persist($entity);
- $this->em->flush();
- $this->em->clear();
- $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
- $this->assertCount(1, $trans);
- $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
- $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
- $this->assertCount(1, $articles);
- $this->assertEquals('title defaultLocale', $articles[0]['title']);
- }
- function testDefaultAndEntityTranslationWithPersistingDefault()
- {
- $this->translatableListener->setPersistDefaultLocaleTranslation( true );
- $entity = new Article;
- $this->repo
- ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
- ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
- ;
- $this->em->persist($entity);
- $this->em->flush();
- $this->em->clear();
- $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
- $this->assertCount(2, $trans);
- $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
- $this->assertSame('title defaultLocale', $trans['defaultLocale']['title']);
- $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
- $this->assertCount(1, $articles);
- $this->assertEquals('title defaultLocale', $articles[0]['title']);
- }
- function testDefaultAndEntityTranslationWithPersistingDefaultResorted()
- {
- $this->translatableListener->setPersistDefaultLocaleTranslation( true );
- $entity = new Article;
- $this->repo
- ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
- ->translate($entity, 'title', 'defaultLocale' , 'title defaultLocale' )
- ;
- $this->em->persist($entity);
- $this->em->flush();
- $this->em->clear();
- $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
- $this->assertCount(2, $trans);
- $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
- $this->assertSame('title defaultLocale', $trans['defaultLocale']['title']);
- $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
- $this->assertCount(1, $articles);
- $this->assertEquals('title defaultLocale', $articles[0]['title']);
- }
- function testTwoFieldsWithoutPersistingDefault()
- {
- $this->translatableListener->setPersistDefaultLocaleTranslation( false );
- $entity = new Article;
- $this->repo
- ->translate($entity, 'title' , 'translatedLocale', 'title translatedLocale' )
- ->translate($entity, 'title' , 'defaultLocale' , 'title defaultLocale' )
- ->translate($entity, 'content', 'translatedLocale', 'content translatedLocale')
- ->translate($entity, 'content', 'defaultLocale' , 'content defaultLocale' )
- ;
- $this->em->persist($entity);
- $this->em->flush();
- $this->em->clear();
- $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
- $this->assertCount(1, $trans);
- $this->assertSame('title translatedLocale' , $trans['translatedLocale']['title']);
- $this->assertSame('content translatedLocale', $trans['translatedLocale']['content']);
- $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
- $this->assertCount(1, $articles);
- $this->assertEquals('title defaultLocale' , $articles[0]['title'] );
- $this->assertEquals('content defaultLocale', $articles[0]['content']);
- }
- function testTwoFieldsWithoutPersistingDefaultResorted()
- {
- $this->translatableListener->setPersistDefaultLocaleTranslation( false );
- $entity = new Article;
- $this->repo
- ->translate($entity, 'title' , 'defaultLocale' , 'title defaultLocale' )
- ->translate($entity, 'title' , 'translatedLocale', 'title translatedLocale' )
- ->translate($entity, 'content', 'defaultLocale' , 'content defaultLocale' )
- ->translate($entity, 'content', 'translatedLocale', 'content translatedLocale')
- ;
- $this->em->persist($entity);
- $this->em->flush();
- $this->em->clear();
- $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
- $this->assertCount(1, $trans);
- $this->assertSame('title translatedLocale' , $trans['translatedLocale']['title']);
- $this->assertSame('content translatedLocale', $trans['translatedLocale']['content']);
- $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
- $this->assertCount(1, $articles);
- $this->assertEquals('title defaultLocale' , $articles[0]['title'] );
- $this->assertEquals('content defaultLocale', $articles[0]['content']);
- }
- function testTwoFieldsWithPersistingDefault()
- {
- $this->translatableListener->setPersistDefaultLocaleTranslation( true );
- $entity = new Article;
- $this->repo
- ->translate($entity, 'title' , 'translatedLocale', 'title translatedLocale' )
- ->translate($entity, 'title' , 'defaultLocale' , 'title defaultLocale' )
- ->translate($entity, 'content', 'translatedLocale', 'content translatedLocale')
- ->translate($entity, 'content', 'defaultLocale' , 'content defaultLocale' )
- ;
- $this->em->persist($entity);
- $this->em->flush();
- $this->em->clear();
- $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
- $this->assertCount(2, $trans);
- $this->assertSame('title translatedLocale' , $trans['translatedLocale']['title']);
- $this->assertSame('title defaultLocale' , $trans['defaultLocale']['title']);
- $this->assertSame('content translatedLocale', $trans['translatedLocale']['content']);
- $this->assertSame('content defaultLocale' , $trans['defaultLocale']['content']);
- $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
- $this->assertCount(1, $articles);
- $this->assertEquals('title defaultLocale' , $articles[0]['title'] );
- $this->assertEquals('content defaultLocale', $articles[0]['content']);
- }
- function testTwoFieldsWithPersistingDefaultResorted()
- {
- $this->translatableListener->setPersistDefaultLocaleTranslation( true );
- $entity = new Article;
- $this->repo
- ->translate($entity, 'title' , 'defaultLocale' , 'title defaultLocale' )
- ->translate($entity, 'title' , 'translatedLocale', 'title translatedLocale' )
- ->translate($entity, 'content', 'defaultLocale' , 'content defaultLocale' )
- ->translate($entity, 'content', 'translatedLocale', 'content translatedLocale')
- ;
- $this->em->persist($entity);
- $this->em->flush();
- $this->em->clear();
- $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
- $this->assertCount(2, $trans);
- $this->assertSame('title translatedLocale' , $trans['translatedLocale']['title']);
- $this->assertSame('title defaultLocale' , $trans['defaultLocale']['title']);
- $this->assertSame('content translatedLocale', $trans['translatedLocale']['content']);
- $this->assertSame('content defaultLocale' , $trans['defaultLocale']['content']);
- $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
- $this->assertCount(1, $articles);
- $this->assertEquals('title defaultLocale' , $articles[0]['title'] );
- $this->assertEquals('content defaultLocale', $articles[0]['content']);
- }
- // --- Fixture related methods ---------------------------------------------
- protected function getUsedEntityFixtures()
- {
- return array(
- self::ARTICLE,
- self::TRANSLATION
- );
- }
- }
|