Skip to content

Commit

Permalink
Fix to translated content bug on MenuPathTokenProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
mmenozzi committed Jan 18, 2016
1 parent d1f1114 commit b372d1d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
4 changes: 3 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ services:

pugx_cmf_page.routing_auto.menu_path_provider:
class: PUGX\Cmf\PageBundle\RoutingAuto\TokenProvider\MenuPathTokenProvider
arguments: [ @cmf_routing_auto.slugifier ]
arguments:
- @cmf_routing_auto.slugifier
- @doctrine_phpcr.odm.default_document_manager
tags:
- { name: cmf_routing_auto.token_provider, alias: "menu_path" }

Expand Down
25 changes: 24 additions & 1 deletion RoutingAuto/TokenProvider/MenuPathTokenProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace PUGX\Cmf\PageBundle\RoutingAuto\TokenProvider;


use Doctrine\ODM\PHPCR\DocumentManager;
use Symfony\Cmf\Bundle\CoreBundle\Slugifier\SlugifierInterface;
use Symfony\Cmf\Bundle\MenuBundle\Doctrine\Phpcr\MenuNode;
use Symfony\Cmf\Component\RoutingAuto\TokenProviderInterface;
Expand All @@ -22,13 +23,22 @@ class MenuPathTokenProvider implements TokenProviderInterface
*/
private $slugifier;

/**
* @var DocumentManager
*/
private $documentManager;

private $locale;

/**
* MenuPathTokenProvider constructor.
* @param SlugifierInterface $slugifier
* @param DocumentManager $documentManager
*/
public function __construct(SlugifierInterface $slugifier)
public function __construct(SlugifierInterface $slugifier, DocumentManager $documentManager)
{
$this->slugifier = $slugifier;
$this->documentManager = $documentManager;
}

/**
Expand All @@ -43,6 +53,11 @@ public function provideValue(UriContext $uriContext, $options)
{
$path = array();
$subject = $uriContext->getSubjectObject();

if ($this->documentManager->isDocumentTranslatable($subject)) {
$this->locale = $subject->getLocale();
}

$this->traversePath($subject, $path);
if (empty($path)) {
return '';
Expand Down Expand Up @@ -91,7 +106,15 @@ private function traversePath($subject, array &$path)
if (!$parentNode instanceof MenuNode) {
return;
}

$parentSubject = $parentNode->getContent();
if ($this->locale && $this->documentManager->isDocumentTranslatable($parentSubject)) {
$meta = $this->documentManager->getMetadataFactory()->getMetadataFor(get_class($parentSubject));
$parentSubject = $this
->documentManager
->findTranslation($meta->getName(), $meta->getIdentifierValue($parentSubject), $this->locale);
}

$this->traversePath($parentSubject, $path);
}
}
17 changes: 13 additions & 4 deletions Tests/Unit/RoutingAuto/TokenProvider/MenuPathTokenProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function testProvideValueWithTwoLevelPath()
$slugifier = $this->mockSlugifier(
array('Subject' => 'subject', 'Parent Subject' => 'parent-subject')
);
$documentManager = $this->getMock('Doctrine\ODM\PHPCR\DocumentManager', array(), array(), '', false);
$subject = $this->getMock(
'PUGX\Cmf\PageBundle\Tests\Unit\RoutingAuto\TokenProvider\InitialTestSubjectInterface'
);
Expand All @@ -33,47 +34,55 @@ public function testProvideValueWithTwoLevelPath()
$menuNode = $this->mockMenuNode($parentMenuNode);
$subject->expects($this->any())->method('providePrimaryMenuNode')->willReturn($menuNode);
$uriContext = $this->mockUriContext($subject);
$provider = new MenuPathTokenProvider($slugifier);
$provider = new MenuPathTokenProvider($slugifier, $documentManager);
$path = $provider->provideValue($uriContext, array());
$this->assertEquals('parent-subject/subject', $path);
}

public function testProvideValueWithRouteTokenProviderSubjectButNoMenuProviderSubjectShouldReturnOnlyRouteToken()
{
$slugifier = $this->mockSlugifier(array('My Subject' => 'my-subject'));
$documentManager = $this->getMock('Doctrine\ODM\PHPCR\DocumentManager', array(), array(), '', false);
$subject = $this->getMock('PUGX\Cmf\PageBundle\RoutingAuto\TokenProvider\RouteTokenProviderInterface');
$subject->expects($this->any())->method('provideRouteToken')->willReturn('My Subject');
$uriContext = $this->mockUriContext($subject);
$provider = new MenuPathTokenProvider($slugifier);
$provider = new MenuPathTokenProvider($slugifier, $documentManager);
$path = $provider->provideValue($uriContext, array());
$this->assertEquals('my-subject', $path);
}

public function testProvideValueWithNoRouteTokenProviderSubjectButMenuProviderSubjectShouldReturnEmpty()
{
$slugifier = $this->mockSlugifier(array());
$documentManager = $this->getMock('Doctrine\ODM\PHPCR\DocumentManager', array(), array(), '', false);
$subject = $this->getMock('PUGX\Cmf\PageBundle\RoutingAuto\TokenProvider\PrimaryMenuNodeProviderInterface');
$parentContent = $this->getMock('PUGX\Cmf\PageBundle\RoutingAuto\TokenProvider\RouteTokenProviderInterface');
$parentContent->expects($this->any())->method('provideRouteToken')->willReturn('Parent Subject');
$parentMenuNode = $this->mockMenuNode(null, $parentContent);
$menuNode = $this->mockMenuNode($parentMenuNode, null);
$subject->expects($this->any())->method('providePrimaryMenuNode')->willReturn($menuNode);
$uriContext = $this->mockUriContext($subject);
$provider = new MenuPathTokenProvider($slugifier);
$provider = new MenuPathTokenProvider($slugifier, $documentManager);
$path = $provider->provideValue($uriContext, array());
$this->assertEquals('', $path);
}

public function testProvideValueWithNoRouteTokenProviderSubjectAndNoMenuProviderSubject()
{
$slugifier = $this->mockSlugifier(array());
$documentManager = $this->getMock('Doctrine\ODM\PHPCR\DocumentManager', array(), array(), '', false);
$subject = $this->getMock('\stdClass');
$uriContext = $this->mockUriContext($subject);
$provider = new MenuPathTokenProvider($slugifier);
$provider = new MenuPathTokenProvider($slugifier, $documentManager);
$path = $provider->provideValue($uriContext, array());
$this->assertEquals('', $path);
}

public function testProvideValueForTranslatedContent()
{
$this->markTestSkipped('TODO');
}

/**
* @param $parentObject
* @param null $content
Expand Down

0 comments on commit b372d1d

Please sign in to comment.