浏览代码

Merge pull request #115 from oscarballadares/Issue114

Issue114. Translatable. TestCase. 3 children has the same parent, when updating children to another parent, and deleting the previous parent, 1st child will lose its translations.
Gediminas Morkevicius 14 年之前
父节点
当前提交
2086ec78d2

+ 57 - 0
tests/Gedmo/Translatable/Fixture/Issue114/Article.php

@@ -0,0 +1,57 @@
+<?php
+
+namespace Translatable\Fixture\Issue114;
+
+use Gedmo\Mapping\Annotation as Gedmo;
+use Doctrine\ORM\Mapping as ORM;
+
+/**
+ * @ORM\Entity
+ */
+class Article
+{
+    /**
+     * @ORM\Id
+     * @ORM\GeneratedValue
+     * @ORM\Column(type="integer")
+     */
+    private $id;
+
+    /**
+     * @Gedmo\Translatable
+     * @ORM\Column(name="title", type="string", length=128)
+     */
+    private $title;
+    
+    /**
+     * @ORM\ManyToOne(targetEntity="Category", inversedBy="articles")
+     */
+    private $category;
+
+    
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    public function setTitle($title)
+    {
+        $this->title = $title;
+    }
+
+    public function getTitle()
+    {
+        return $this->title;
+    }
+    
+    public function setCategory(Category $category)
+    {
+        $this->category = $category;
+    }
+    
+    public function getCategory()
+    {
+        return $this->category;
+    }
+}
+

+ 56 - 0
tests/Gedmo/Translatable/Fixture/Issue114/Category.php

@@ -0,0 +1,56 @@
+<?php
+
+namespace Translatable\Fixture\Issue114;
+
+use Gedmo\Mapping\Annotation as Gedmo;
+use Doctrine\ORM\Mapping as ORM;
+
+/**
+ * @ORM\Entity
+ */
+class Category
+{
+    /**
+     * @ORM\Id
+     * @ORM\GeneratedValue
+     * @ORM\Column(type="integer")
+     */
+    private $id;
+
+    /**
+     * @Gedmo\Translatable
+     * @ORM\Column(name="title", type="string", length=128)
+     */
+    private $title;
+    
+    /**
+     * @ORM\OneToMany(targetEntity="Article", mappedBy="category", cascade={"persist", "remove"})
+     */
+    private $articles;
+    
+    public function getId()
+    {
+        return $this->id;
+    }
+    
+    public function setTitle($title)
+    {
+        $this->title = $title;
+    }
+    
+    public function getTitle()
+    {
+        return $this->title;
+    }
+    
+    public function addArticle(Article $article)
+    {
+        $this->articles[] = $article;
+    }
+    
+    public function getArticles()
+    {
+        return $this->articles;
+    }
+}
+

+ 120 - 0
tests/Gedmo/Translatable/Issue/Issue114Test.php

@@ -0,0 +1,120 @@
+<?php
+
+namespace Gedmo\Translatable;
+
+use Doctrine\Common\EventManager;
+use Tool\BaseTestCaseORM;
+use Translatable\Fixture\Issue114\Article;
+use Translatable\Fixture\Issue114\Category;
+
+/**
+ * These are tests for translatable behavior
+ *
+ * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @package Gedmo.Translatable
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+class Issue114Test extends BaseTestCaseORM
+{ 
+    const CATEGORY =   'Translatable\\Fixture\\Issue114\\Category';
+    const ARTICLE = 'Translatable\\Fixture\\Issue114\\Article';    
+    const TRANSLATION = 'Gedmo\\Translatable\\Entity\\Translation';
+    
+    private $translatableListener;
+
+    protected function setUp()
+    {
+        parent::setUp();
+
+        $evm = new EventManager;
+        $this->translatableListener = new TranslationListener();
+        $this->translatableListener->setTranslatableLocale('en');
+        $this->translatableListener->setDefaultLocale('en');
+        $evm->addEventSubscriber($this->translatableListener);
+
+        $this->getMockSqliteEntityManager($evm);
+    }
+
+    public function testIssue114()
+    {
+        $repo = $this->em->getRepository(self::TRANSLATION);
+        
+        //Categories
+        $category1 = new Category;
+        $category1->setTitle('en category1');
+    
+        $category2 = new Category;
+        $category2->setTitle('en category2');
+        
+        $this->em->persist($category1);
+        $this->em->persist($category2);
+        $this->em->flush();
+
+        //Articles
+        $article1 = new Article;
+        $article1->setTitle('en article1');
+        $article1->setCategory($category1);
+        
+        $article2 = new Article;
+        $article2->setTitle('en article2');
+        $article2->setCategory($category1);
+        
+        $article3 = new Article;
+        $article3->setTitle('en article3');
+        $article3->setCategory($category1);
+        
+        $this->em->persist($article1);
+        $this->em->persist($article2);
+        $this->em->persist($article3);
+        $this->em->flush();
+        
+        $this->translatableListener->setTranslatableLocale('es');
+        
+        // Setting es_ES title.
+        $article1->setTitle('es article1');
+        $article2->setTitle('es article2');
+        $article3->setTitle('es article3');
+        
+        $this->em->persist($article1);
+        $this->em->persist($article2);
+        $this->em->persist($article3);
+       
+        $this->em->flush();
+        
+        // Updating articles' category
+        $article1->setCategory($category2);
+        $article2->setCategory($category2);
+        $article3->setCategory($category2);
+        
+        $this->em->persist($article1);
+        $this->em->persist($article2);
+        $this->em->persist($article3);
+        
+        $this->em->flush();
+        
+        // Removing $category1
+        $this->em->remove($category1);
+        $this->em->flush();
+        
+        $trans = $repo->findTranslations($article2);
+        $this->assertEquals(2, count($trans));
+        
+        $trans = $repo->findTranslations($article3);
+        $this->assertEquals(2, count($trans));
+        
+        $trans = $repo->findTranslations($article1);
+        $this->assertEquals(2, count($trans));
+           
+    }
+
+    protected function getUsedEntityFixtures()
+    {
+        return array(
+            self::CATEGORY,
+            self::ARTICLE,
+            self::TRANSLATION,
+            
+        );
+    }
+}