From befceca216324d0d2174d53101e51249c7cbc796 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Sun, 23 Aug 2015 21:44:37 +0200 Subject: [PATCH 1/2] [#145] Make sure locale doesn't change after flush is called --- Adapter/PhpcrOdmAdapter.php | 3 +-- Doctrine/Phpcr/AutoRouteListener.php | 6 +++++ .../EventListener/AutoRouteListenerTest.php | 24 +++++++++++++++++-- Tests/Resources/Document/Post.php | 2 +- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Adapter/PhpcrOdmAdapter.php b/Adapter/PhpcrOdmAdapter.php index 320d9f5..e19a936 100644 --- a/Adapter/PhpcrOdmAdapter.php +++ b/Adapter/PhpcrOdmAdapter.php @@ -71,9 +71,8 @@ public function getLocales($contentDocument) public function translateObject($contentDocument, $locale) { $meta = $this->dm->getMetadataFactory()->getMetadataFor(get_class($contentDocument)); - $contentDocument = $this->dm->findTranslation($meta->getName(), $meta->getIdentifierValue($contentDocument), $locale); - return $contentDocument; + return $this->dm->findTranslation($meta->getName(), $meta->getIdentifierValue($contentDocument), $locale); } /** diff --git a/Doctrine/Phpcr/AutoRouteListener.php b/Doctrine/Phpcr/AutoRouteListener.php index 999ba87..91ce3f2 100644 --- a/Doctrine/Phpcr/AutoRouteListener.php +++ b/Doctrine/Phpcr/AutoRouteListener.php @@ -63,6 +63,7 @@ public function onFlush(ManagerEventArgs $args) $autoRoute = null; foreach ($updates as $document) { if ($this->isAutoRouteable($document)) { + $locale = $uow->getCurrentLocale($document); $uriContextCollection = new UriContextCollection($document); $arm->buildUriContextCollection($uriContextCollection); @@ -73,6 +74,11 @@ public function onFlush(ManagerEventArgs $args) $dm->persist($autoRoute); $uow->computeChangeSets(); } + + // reset locale to the original locale + if (null !== $locale) { + $dm->findTranslation(get_class($document), $uow->getDocumentId($document), $locale); + } } } diff --git a/Tests/Functional/EventListener/AutoRouteListenerTest.php b/Tests/Functional/EventListener/AutoRouteListenerTest.php index 036d2ef..911492b 100644 --- a/Tests/Functional/EventListener/AutoRouteListenerTest.php +++ b/Tests/Functional/EventListener/AutoRouteListenerTest.php @@ -10,7 +10,7 @@ */ -namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Functional\Subscriber; +namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Functional\EventListener; use Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Functional\BaseTestCase; use Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Blog; @@ -229,7 +229,6 @@ public function testMultilangArticle($data, $expectedPaths) $this->getDm()->flush(); $this->getDm()->clear(); - $articleTitles = array_values($data); $locales = array_keys($data); foreach ($expectedPaths as $i => $expectedPath) { @@ -271,6 +270,27 @@ public function provideUpdateMultilangArticle() ); } + public function testMultilangArticleRemainsSameLocale() + { + $article = new Article; + $article->path = '/test/article-1'; + $this->getDm()->persist($article); + + $article->title = 'Hello everybody!'; + $this->getDm()->bindTranslation($article, 'en'); + + $article->title = 'Bonjour le monde!'; + $this->getDm()->bindTranslation($article, 'fr'); + + // let current article be something else than the last bound locale + $this->getDm()->findTranslation(get_class($article), $this->getDm()->getUnitOfWork()->getDocumentId($article), 'en'); + + $this->getDm()->flush(); + $this->getDm()->clear(); + + $this->assertEquals('Hello everybody!', $article->title); + } + /** * @dataProvider provideUpdateMultilangArticle */ diff --git a/Tests/Resources/Document/Post.php b/Tests/Resources/Document/Post.php index 6b275a7..674c7c8 100644 --- a/Tests/Resources/Document/Post.php +++ b/Tests/Resources/Document/Post.php @@ -40,7 +40,7 @@ class Post public $blog; /** - * @PHPCR\NodeName() + * @PHPCR\Nodename() */ public $name; From 9fce43731f6e59c5fe8968a5078e61de1a31b116 Mon Sep 17 00:00:00 2001 From: dantleech Date: Fri, 9 Oct 2015 07:45:16 +0200 Subject: [PATCH 2/2] Fixed test --- Tests/Functional/EventListener/AutoRouteListenerTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Tests/Functional/EventListener/AutoRouteListenerTest.php b/Tests/Functional/EventListener/AutoRouteListenerTest.php index 911492b..8d5c74b 100644 --- a/Tests/Functional/EventListener/AutoRouteListenerTest.php +++ b/Tests/Functional/EventListener/AutoRouteListenerTest.php @@ -274,7 +274,9 @@ public function testMultilangArticleRemainsSameLocale() { $article = new Article; $article->path = '/test/article-1'; + $article->title = 'Good Day'; $this->getDm()->persist($article); + $this->getDm()->flush(); $article->title = 'Hello everybody!'; $this->getDm()->bindTranslation($article, 'en');