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

Commit 6627ba4

Browse files
authored
Merge pull request #287 from symfony-cmf/issue-286/child-restrictions
Restrict children type of Menu and MenuNode
2 parents 01fcbf0 + ef1112f commit 6627ba4

File tree

7 files changed

+116
-2
lines changed

7 files changed

+116
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Changelog
22
=========
33

4+
* **2017-02-09**: [BC BREAK] Added child restrictions to the `Menu` and `MenuNode` documents.
5+
See the UPGRADE guide for detailed information.
6+
47
2.1.0-RC1
58
---------
69

UPGRADE-2.1.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,9 @@
6767
- cmf_sonata_admin_integration.menu.menu_admin
6868
- cmf_sonata_admin_integration.menu.node_admin
6969
```
70+
71+
# Doctrine PHPCR ODM
72+
73+
* Only `MenuNode` documents are allowed as children of the `Menu` and
74+
`MenuNode` documents. This behaviour can be changed by overriding the
75+
`child-class` setting of the PHPCR ODM mapping.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"symfony/framework-bundle": "^2.8|^3.0",
1717
"symfony-cmf/core-bundle": "^2.0",
1818
"doctrine/phpcr-bundle": "^1.1",
19-
"doctrine/phpcr-odm": "^1.4|^2.0",
19+
"doctrine/phpcr-odm": "^1.4.2",
2020
"knplabs/knp-menu-bundle": "^2.0.0",
2121
"knplabs/knp-menu": "^2.0.0"
2222
},

src/Resources/config/doctrine-phpcr/Menu.phpcr.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
name="Symfony\Cmf\Bundle\MenuBundle\Doctrine\Phpcr\Menu"
1010
referenceable="true"
1111
>
12+
<child-class name="Symfony\Cmf\Bundle\MenuBundle\Doctrine\Phpcr\MenuNode"/>
1213
</document>
1314

1415
</doctrine-mapping>

src/Resources/config/doctrine-phpcr/MenuNode.phpcr.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
name="Symfony\Cmf\Bundle\MenuBundle\Doctrine\Phpcr\MenuNode"
1010
referenceable="true"
1111
>
12+
<child-class name="Symfony\Cmf\Bundle\MenuBundle\Doctrine\Phpcr\MenuNode"/>
1213
</document>
1314

1415
</doctrine-mapping>

tests/Functional/Doctrine/Phpcr/MenuNodeTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Cmf\Bundle\MenuBundle\Tests\Functional\Doctrine\Phpcr;
1313

14+
use Doctrine\ODM\PHPCR\Document\Generic;
1415
use Doctrine\ODM\PHPCR\DocumentManager;
1516
use Symfony\Cmf\Bundle\MenuBundle\Doctrine\Phpcr\MenuNode;
1617
use Symfony\Cmf\Bundle\MenuBundle\Tests\Resources\Document\Content;
@@ -29,7 +30,7 @@ class MenuNodeTest extends BaseTestCase
2930
*/
3031
private $child1;
3132

32-
public function setUp()
33+
protected function setUp()
3334
{
3435
$this->db('PHPCR')->createTestNode();
3536

@@ -157,4 +158,23 @@ public function testMenuNode()
157158
$menuNode = $this->dm->find(null, '/test/test-node');
158159
$this->assertCount(0, $menuNode->getChildren());
159160
}
161+
162+
/**
163+
* @expectedException \Doctrine\ODM\PHPCR\Exception\OutOfBoundsException
164+
* @expectedExceptionMessage Allowed child classes "Symfony\Cmf\Bundle\MenuBundle\Doctrine\Phpcr\MenuNode"
165+
*/
166+
public function testPersistInvalidChild()
167+
{
168+
$menuNode = new MenuNode();
169+
$menuNode->setName('menu-node');
170+
$menuNode->setParentDocument($this->rootDocument);
171+
$this->dm->persist($menuNode);
172+
173+
$generic = new Generic();
174+
$generic->setParentDocument($menuNode);
175+
$generic->setNodename('invalid');
176+
$this->dm->persist($generic);
177+
178+
$this->dm->flush();
179+
}
160180
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2017 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\MenuBundle\Tests\Functional\Doctrine\Phpcr;
13+
14+
use Doctrine\ODM\PHPCR\Document\Generic;
15+
use Doctrine\ODM\PHPCR\DocumentManager;
16+
use Symfony\Cmf\Bundle\MenuBundle\Doctrine\Phpcr\Menu;
17+
use Symfony\Cmf\Bundle\MenuBundle\Doctrine\Phpcr\MenuNode;
18+
use Symfony\Cmf\Component\Testing\Functional\BaseTestCase;
19+
20+
class MenuTest extends BaseTestCase
21+
{
22+
/**
23+
* @var DocumentManager
24+
*/
25+
private $dm;
26+
private $rootDocument;
27+
28+
protected function setUp()
29+
{
30+
$this->db('PHPCR')->createTestNode();
31+
32+
$this->dm = $this->db('PHPCR')->getOm();
33+
$this->rootDocument = $this->dm->find(null, '/test');
34+
}
35+
36+
public function testPersist()
37+
{
38+
$menu = new Menu();
39+
$menu->setPosition($this->rootDocument, 'main');
40+
$this->dm->persist($menu);
41+
42+
$menuNode = new MenuNode();
43+
$menuNode->setName('home');
44+
$menu->addChild($menuNode);
45+
$this->dm->persist($menuNode);
46+
47+
$this->dm->flush();
48+
$this->dm->clear();
49+
50+
$menu = $this->dm->find(null, '/test/main');
51+
52+
$this->assertNotNull($menu);
53+
$this->assertEquals('main', $menu->getName());
54+
55+
$children = $menu->getChildren();
56+
$this->assertCount(1, $children);
57+
$this->assertEquals('home', $children[0]->getName());
58+
}
59+
60+
/**
61+
* @dataProvider getInvalidChildren
62+
* @expectedException \Doctrine\ODM\PHPCR\Exception\OutOfBoundsException
63+
*/
64+
public function testPersistInvalidChild($invalidChild)
65+
{
66+
$menu = new Menu();
67+
$menu->setPosition($this->rootDocument, 'main');
68+
$this->dm->persist($menu);
69+
70+
$invalidChild->setParentDocument($menu);
71+
$this->dm->persist($invalidChild);
72+
73+
$this->dm->flush();
74+
}
75+
76+
public function getInvalidChildren()
77+
{
78+
return [
79+
[(new Menu())->setName('invalid')],
80+
[(new Generic())->setNodename('invalid')],
81+
];
82+
}
83+
}

0 commit comments

Comments
 (0)