Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #199 from kea/tree-move-bug
Browse files Browse the repository at this point in the history
Returned value of "move" method should be compatible with Cmf PhpcrTree
  • Loading branch information
lsmith77 committed Oct 9, 2013
2 parents eadc73e + e27b110 commit b664d27
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
91 changes: 91 additions & 0 deletions Tests/Tree/PhpcrOdmTreeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/*
* This file is part of the Sonata package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\DoctrinePHPCRAdminBundle\Tests\Tree;

use Sonata\DoctrinePHPCRAdminBundle\Tree\PhpcrOdmTree;

class PhpcrOdmTreeTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->dm = $this->getMockBuilder('Doctrine\\ODM\\PHPCR\\DocumentManager')->disableOriginalConstructor()->getMock();
$this->dm->expects($this->once())
->method('find')
->will($this->returnValue(new \stdClass()));

$this->defaultModelManager = $this->getMockBuilder('Sonata\\DoctrinePHPCRAdminBundle\\Model\\ModelManager')->disableOriginalConstructor()->getMock();
$this->translator = $this->getMockBuilder('Symfony\\Component\\Translation\\TranslatorInterface')->disableOriginalConstructor()->getMock();
$this->assetHelper = $this->getMockBuilder('Symfony\\Component\\Templating\\Helper\\CoreAssetsHelper')->disableOriginalConstructor()->getMock();

$this->pool = $this->getMockBuilder('Sonata\\AdminBundle\\Admin\\Pool')->disableOriginalConstructor()->getMock();
}

public function testMoveWithoutAdmin()
{
$movedPath = '/cms/to-move';
$targetPath = '/cms/target/moved';
$urlSafeId = 'urlSafeId';

$this->defaultModelManager->expects($this->once())
->method('getNormalizedIdentifier')
->will($this->returnValue($targetPath));
$this->defaultModelManager->expects($this->once())
->method('getUrlsafeIdentifier')
->will($this->returnValue($urlSafeId));
$this->pool->expects($this->once())
->method('getAdminByClass')
->will($this->returnValue(null));

$tree = new PhpcrOdmTree(
$this->dm,
$this->defaultModelManager,
$this->pool,
$this->translator,
$this->assetHelper,
array()
);

$this->assertEquals(
array('id' => $targetPath, 'url_safe_id' => $urlSafeId),
$tree->move($movedPath, $targetPath));
}

function testMoveWithAdmin()
{
$movedPath = '/cms/to-move';
$targetPath = '/cms/target/moved';
$urlSafeId = 'urlSafeId';

$admin = $this->getMockBuilder('Sonata\\DoctrinePHPCRAdminBundle\\Admin\\Admin')->disableOriginalConstructor()->getMock();
$admin->expects($this->once())
->method('getNormalizedIdentifier')
->will($this->returnValue($targetPath));
$admin->expects($this->once())
->method('getUrlsafeIdentifier')
->will($this->returnValue($urlSafeId));
$this->pool->expects($this->once())
->method('getAdminByClass')
->will($this->returnValue($admin));

$tree = new PhpcrOdmTree(
$this->dm,
$this->defaultModelManager,
$this->pool,
$this->translator,
$this->assetHelper,
array()
);
$this->assertEquals(
array('id' => $targetPath, 'url_safe_id' => $urlSafeId),
$tree->move($movedPath, $targetPath));
}
}
11 changes: 10 additions & 1 deletion Tree/PhpcrOdmTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,16 @@ public function move($movedPath, $targetPath)
$this->dm->move($document, $resultingPath);
$this->dm->flush();

return $resultingPath;
$admin = $this->getAdmin($document);
if (null !== $admin) {
$id = $admin->getNormalizedIdentifier($document);
$urlSafeId = $admin->getUrlsafeIdentifier($document);
} else {
$id = $this->defaultModelManager->getNormalizedIdentifier($document);
$urlSafeId = $this->defaultModelManager->getUrlsafeIdentifier($document);
}

return array('id' => $id, 'url_safe_id' => $urlSafeId);
}

/**
Expand Down

0 comments on commit b664d27

Please sign in to comment.