Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 2cedfef

Browse files
committed
Merge pull request #225 from symfony-cmf/avoid-errors-on-menuprovider-has
prevent exceptions on menu names that are not valid node names
2 parents 448c074 + 4e30444 commit 2cedfef

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ matrix:
2020
env: SYMFONY_VERSION=2.6.*
2121

2222
before_script:
23+
- if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then echo "memory_limit = -1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini; fi
2324
- composer self-update
2425
- composer require symfony/symfony:${SYMFONY_VERSION} --prefer-source
2526
- vendor/symfony-cmf/testing/bin/travis/phpcr_odm_doctrine_dbal.sh

Provider/PhpcrMenuProvider.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\Common\Persistence\ManagerRegistry;
1515
use Doctrine\ODM\PHPCR\DocumentManager;
16+
use PHPCR\RepositoryException;
1617
use Symfony\Component\HttpFoundation\Request;
1718
use PHPCR\PathNotFoundException;
1819
use PHPCR\Util\PathHelper;
@@ -214,9 +215,20 @@ protected function find($name, array $options, $throw)
214215
return false;
215216
}
216217

217-
$path = PathHelper::absolutizePath($name, $this->getMenuRoot());
218218
$dm = $this->getObjectManager();
219219
$session = $dm->getPhpcrSession();
220+
221+
try {
222+
$path = PathHelper::absolutizePath($name, $this->getMenuRoot());
223+
PathHelper::assertValidAbsolutePath($path, false, true, $session->getNamespacePrefixes());
224+
} catch (RepositoryException $e) {
225+
if ($throw) {
226+
throw $e;
227+
}
228+
229+
return false;
230+
}
231+
220232
if ($this->getPrefetch() > 0) {
221233
try {
222234
if (

Tests/Unit/Provider/PhpcrMenuProviderTest.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ private function getDmMock($path)
2222
->method('getNode')
2323
->with($path)
2424
;
25+
$session->expects($this->once())
26+
->method('getNamespacePrefixes')
27+
->will($this->returnValue(array('jcr', 'nt')))
28+
;
2529
$dm = $this
2630
->getMockBuilder('Doctrine\ODM\PHPCR\DocumentManager')
2731
->disableOriginalConstructor()
@@ -90,15 +94,53 @@ public function testHas($menuRoot, $name, $expectedPath)
9094
$menuRoot
9195
);
9296

93-
$provider->has($name);
97+
$this->assertTrue($provider->has($name));
98+
}
99+
100+
public function testHasNot()
101+
{
102+
$session = $this->getMock('PHPCR\SessionInterface');
103+
$session->expects($this->never())
104+
->method('getNode')
105+
;
106+
$session->expects($this->any())
107+
->method('getNamespacePrefixes')
108+
->will($this->returnValue(array('jcr', 'nt')))
109+
;
110+
$objectManager = $this
111+
->getMockBuilder('Doctrine\ODM\PHPCR\DocumentManager')
112+
->disableOriginalConstructor()
113+
->getMock()
114+
;
115+
$objectManager->expects($this->any())
116+
->method('getPhpcrSession')
117+
->will($this->returnValue($session))
118+
;
119+
$objectManager->expects($this->never())
120+
->method('find')
121+
;
122+
123+
$managerRegistry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
124+
$managerRegistry->expects($this->any())
125+
->method('getManager')
126+
->will($this->returnValue($objectManager));
127+
128+
$provider = new PhpcrMenuProvider(
129+
$this->getMock('Knp\Menu\FactoryInterface'),
130+
$managerRegistry,
131+
'/foo'
132+
);
133+
134+
$this->assertFalse($provider->has('notavalidnamespace:bar'));
135+
$this->assertFalse($provider->has('not:a:valid:name'));
94136
}
95137

96138
public function getMenuTests()
97139
{
98140
return array(
99141
array('/test/menu', 'foo', '/test/menu/foo'),
100142
array('/test/menu', '/another/menu/path', '/another/menu/path'),
143+
array('/test/menu', 'jcr:namespaced', '/test/menu/jcr:namespaced'),
101144
);
102145
}
103-
104146
}

0 commit comments

Comments
 (0)