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

Commit e594a79

Browse files
committed
Merge pull request #142 from symfony-cmf/voter_webtests
[WIP]Voter webtests
2 parents 4e73478 + c4b2d86 commit e594a79

22 files changed

+472
-15
lines changed

DependencyInjection/CmfMenuExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function load(array $configs, ContainerBuilder $container)
4545
public function loadVoters($config, XmlFileLoader $loader, ContainerBuilder $container)
4646
{
4747
$loader->load('voters.xml');
48+
4849
if (isset($config['voters']['content_identity'])) {
4950
if (empty($config['voters']['content_identity']['content_key'])) {
5051
if (! class_exists('Symfony\\Cmf\\Bundle\\RoutingBundle\\Routing\\DynamicRouter')) {
@@ -59,7 +60,7 @@ public function loadVoters($config, XmlFileLoader $loader, ContainerBuilder $con
5960
$container->removeDefinition('cmf_menu.current_item_voter.content_identity');
6061
}
6162

62-
if (! isset($config['voters']['uri_prefix'])) {
63+
if (!array_key_exists('uri_prefix', $config['voters'])) {
6364
$container->removeDefinition('cmf_menu.current_item_voter.uri_prefix');
6465
}
6566
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\MenuBundle\Tests\Resources\Controller;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6+
use Symfony\Component\HttpFoundation\Request;
7+
use Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter;
8+
9+
/**
10+
* Cmi Tests
11+
*
12+
* Tests actions for current menu item functionality.
13+
*/
14+
class VoterController extends Controller
15+
{
16+
protected function getDm()
17+
{
18+
return $this->container->get('doctrine_phpcr.odm.document_manager');
19+
}
20+
21+
public function defaultAction(Request $request)
22+
{
23+
return $this->render('::tests/voter/default.html.twig');
24+
}
25+
26+
public function requestContentIdentityAction(Request $request)
27+
{
28+
$content = $request->get(DynamicRouter::CONTENT_KEY);
29+
if (!$content) {
30+
$content = $this->getDm()->find(null, '/test/content-1');
31+
$request->attributes->set(DynamicRouter::CONTENT_KEY, $content);
32+
33+
return $this->render('::tests/voter/requestContentVoterActive.html.twig', array('content' => $content));
34+
}
35+
36+
return $this->render('::tests/voter/requestContent.html.twig', array('content' => $content));
37+
}
38+
39+
public function blogAction(Request $request)
40+
{
41+
return $this->render('::tests/voter/blog.html.twig');
42+
}
43+
44+
public function articlesAction(Request $request)
45+
{
46+
return $this->render('::tests/voter/articles.html.twig');
47+
}
48+
49+
public function postAction(Request $request)
50+
{
51+
$content = $request->get(DynamicRouter::CONTENT_KEY);
52+
return $this->render('::tests/voter/post.html.twig', array('content' => $content));
53+
}
54+
55+
public function urlPrefixAction(Request $request)
56+
{
57+
return $this->render('::tests/voter/requestContent.html.twig', array('content' => $content));
58+
}
59+
}

Tests/Resources/DataFixtures/PHPCR/LoadMenuData.php

Lines changed: 129 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@
1515
use Doctrine\Common\DataFixtures\FixtureInterface;
1616
use Doctrine\Common\Persistence\ObjectManager;
1717
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
18+
use Doctrine\ODM\PHPCR\DocumentManager;
1819
use Symfony\Cmf\Bundle\MenuBundle\Doctrine\Phpcr\MenuNode;
1920
use Symfony\Cmf\Bundle\MenuBundle\Doctrine\Phpcr\Menu;
2021
use Symfony\Cmf\Bundle\MenuBundle\Tests\Resources\Document\Content;
22+
use Symfony\Cmf\Bundle\MenuBundle\Tests\Resources\Document\Post;
2123
use Doctrine\ODM\PHPCR\Document\Generic;
24+
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route;
25+
use PHPCR\Util\NodeHelper;
2226

2327
class LoadMenuData implements FixtureInterface, DependentFixtureInterface
2428
{
29+
protected $root;
30+
protected $menuRoot;
31+
protected $routeRoot;
32+
2533
public function getDependencies()
2634
{
2735
return array(
@@ -31,20 +39,29 @@ public function getDependencies()
3139

3240
public function load(ObjectManager $manager)
3341
{
34-
$content = new Content;
35-
$content->setTitle('Content 1');
36-
$content->setId('/test/content-1');
42+
$this->root = $manager->find(null, '/test');
43+
44+
NodeHelper::createPath($manager->getPhpcrSession(), '/test/menus');
45+
NodeHelper::createPath($manager->getPhpcrSession(), '/test/routes/contents');
46+
$this->menuRoot = $manager->find(null, '/test/menus');
47+
$this->routeRoot = $manager->find(null, '/test/routes');
3748

38-
$root = $manager->find(null, '/test');
39-
$menuRoot = new Generic;
40-
$menuRoot->setNodename('menus');
41-
$menuRoot->setParent($root);
42-
$manager->persist($menuRoot);
49+
$this->loadMainMenu($manager);
50+
$this->loadVoterMenu($manager);
51+
52+
$manager->flush();
53+
}
54+
55+
protected function loadMainMenu(DocumentManager $manager)
56+
{
57+
$content = new Content;
58+
$content->setTitle('Menu Item Content 1');
59+
$content->setId('/test/content-menu-item-1');
4360

4461
$menu = new Menu;
4562
$menu->setName('test-menu');
4663
$menu->setLabel('Test Menu');
47-
$menu->setParent($menuRoot);
64+
$menu->setParent($this->menuRoot);
4865
$manager->persist($menu);
4966

5067
$menuNode = new MenuNode;
@@ -94,7 +111,7 @@ public function load(ObjectManager $manager)
94111
$menu = new Menu;
95112
$menu->setName('another-menu');
96113
$menu->setLabel('Another Menu');
97-
$menu->setParent($menuRoot);
114+
$menu->setParent($this->menuRoot);
98115
$manager->persist($menu);
99116

100117
$menuNode = new MenuNode;
@@ -113,6 +130,107 @@ public function load(ObjectManager $manager)
113130
$manager->persist($menuNode);
114131

115132
$manager->persist($content);
116-
$manager->flush();
133+
}
134+
135+
protected function loadVoterMenu(DocumentManager $manager)
136+
{
137+
// test content
138+
$content = new Content;
139+
$content->setTitle('Content 1');
140+
$content->setId('/test/content-1');
141+
$manager->persist($content);
142+
143+
$route = new Route();
144+
$route->setId('/test/routes/contents/content-1');
145+
$route->setDefault('_controller', 'Symfony\Cmf\Bundle\MenuBundle\Tests\Resources\Controller\VoterController::requestContentIdentityAction');
146+
$route->setContent($content);
147+
$manager->persist($route);
148+
149+
// test blog
150+
$blog = new Content;
151+
$blog->setTitle('Blog');
152+
$blog->setId('/test/blog-1');
153+
$manager->persist($blog);
154+
155+
$route = new Route();
156+
$route->setId('/test/routes/blog');
157+
$route->setDefault('_controller', 'Symfony\Cmf\Bundle\MenuBundle\Tests\Resources\Controller\VoterController::blogAction');
158+
$route->setContent($blog);
159+
$manager->persist($route);
160+
161+
// test blog post
162+
$post = new Post;
163+
$post->setTitle('My Post');
164+
$post->setId('/test/blog-1/my-post');
165+
$manager->persist($post);
166+
167+
$route = new Route();
168+
$route->setId('/test/routes/blog/my-post');
169+
$route->setDefault('_controller', 'Symfony\Cmf\Bundle\MenuBundle\Tests\Resources\Controller\VoterController::postAction');
170+
$route->setContent($post);
171+
$manager->persist($route);
172+
173+
// test articles
174+
$articles = new Content;
175+
$articles->setTitle('Articles Index');
176+
$articles->setId('/test/articles');
177+
$manager->persist($articles);
178+
179+
$articlesRoute = new Route();
180+
$articlesRoute->setId('/test/routes/articles');
181+
$articlesRoute->setDefault('_controller', 'Symfony\Cmf\Bundle\MenuBundle\Tests\Resources\Controller\VoterController::articlesAction');
182+
$articlesRoute->setContent($articles);
183+
$articlesRoute->setOption('currentUriPrefix', '/articles');
184+
$manager->persist($articlesRoute);
185+
186+
$article1 = new Content();
187+
$article1->setTitle('Article 1');
188+
$article1->setId('/test/article-1');
189+
$manager->persist($article1);
190+
191+
$route = new Route();
192+
$route->setId('/test/routes/articles/some-category');
193+
$manager->persist($route);
194+
195+
$route = new Route();
196+
$route->setId('/test/routes/articles/some-category/article-1');
197+
$route->setDefault('_controller', 'Symfony\Cmf\Bundle\MenuBundle\Tests\Resources\Controller\VoterController::postAction');
198+
$route->setContent($article1);
199+
$manager->persist($route);
200+
201+
// menu items
202+
$menu = new Menu;
203+
$menu->setName('side-menu');
204+
$menu->setLabel('Side Menu');
205+
$menu->setParent($this->menuRoot);
206+
$manager->persist($menu);
207+
208+
$menuNode = new MenuNode;
209+
$menuNode->setParent($menu);
210+
$menuNode->setLabel('Default Behavior');
211+
$menuNode->setName('default');
212+
$menuNode->setRoute('current_menu_item_default');
213+
$manager->persist($menuNode);
214+
215+
$menuNode = new MenuNode;
216+
$menuNode->setParent($menu);
217+
$menuNode->setLabel('Request Content Identity Voter');
218+
$menuNode->setName('request-content-identity-voter');
219+
$menuNode->setContent($content);
220+
$manager->persist($menuNode);
221+
222+
$menuNode = new MenuNode;
223+
$menuNode->setParent($menu);
224+
$menuNode->setLabel('URI Prefix Voter');
225+
$menuNode->setName('uri-prefix-voter');
226+
$menuNode->setContent($articlesRoute);
227+
$manager->persist($menuNode);
228+
229+
$menuNode = new MenuNode;
230+
$menuNode->setParent($menu);
231+
$menuNode->setLabel('Request Parent Content Identity Voter');
232+
$menuNode->setName('request-parent-content-identity-voter');
233+
$menuNode->setContent($blog);
234+
$manager->persist($menuNode);
117235
}
118236
}

Tests/Resources/Document/Content.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
class Content implements MenuNodeReferrersInterface, RouteReferrersReadInterface
2828
{
2929
/**
30-
* @PHPCRODM\Id()
30+
* @PHPCRODM\Id(strategy="assigned")
3131
*/
3232
protected $id;
3333

@@ -36,6 +36,11 @@ class Content implements MenuNodeReferrersInterface, RouteReferrersReadInterface
3636
*/
3737
protected $title;
3838

39+
/**
40+
* @PHPCRODM\ParentDocument()
41+
*/
42+
protected $parent;
43+
3944
/**
4045
* @PHPCRODM\Referrers(
4146
* referringDocument="Symfony\Cmf\Bundle\MenuBundle\Doctrine\Phpcr\MenuNode",
@@ -45,9 +50,18 @@ class Content implements MenuNodeReferrersInterface, RouteReferrersReadInterface
4550
*/
4651
protected $menuNodes;
4752

53+
/**
54+
* @PHPCRODM\Referrers(
55+
* referringDocument="Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route",
56+
* referencedBy="content"
57+
* )
58+
*/
59+
protected $routes;
60+
4861
public function __construct()
4962
{
5063
$this->menuNodes = new ArrayCollection();
64+
$this->routes = new ArrayCollection();
5165
}
5266

5367
public function getId()
@@ -80,13 +94,26 @@ public function addMenuNode(NodeInterface $menuNode)
8094
$this->menuNodes->add($menuNode);
8195
}
8296

97+
public function addRoute($route)
98+
{
99+
$this->routes->add($route);
100+
}
101+
102+
83103
public function removeMenuNode(NodeInterface $menuNode)
84104
{
85105
$this->menuNodes->remove($menuNode);
86106
}
87107

88108
public function getRoutes()
89109
{
90-
return array(new Route('http://www.example.com/content'));
110+
foreach ($this->routes as $route) {
111+
}
112+
return $this->routes;
113+
}
114+
115+
public function getParent()
116+
{
117+
return $this->parent;
91118
}
92119
}

Tests/Resources/Document/Post.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\MenuBundle\Tests\Resources\Document;
4+
5+
use Doctrine\Common\Collections\ArrayCollection;
6+
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
7+
8+
use Knp\Menu\NodeInterface;
9+
10+
use Symfony\Component\Routing\Route;
11+
use Symfony\Cmf\Component\Routing\RouteReferrersReadInterface;
12+
use Symfony\Cmf\Bundle\MenuBundle\Model\MenuNodeReferrersInterface;
13+
14+
/**
15+
* @PHPCRODM\Document(referenceable=true)
16+
*/
17+
class Post extends Content
18+
{
19+
}

Tests/Resources/app/AppKernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function configure()
2121
public function registerContainerConfiguration(LoaderInterface $loader)
2222
{
2323
$loader->load(__DIR__.'/config/config.php');
24-
$loader->load(__DIR__.'/config/admin-test.xml');
24+
$loader->load(__DIR__.'/config/test-services.xml');
2525
}
2626

2727
}

Tests/Resources/app/Resources/views/index.html.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<ul>
55
<li><a href="{{ path('render_test') }}">Render test</a></li>
66
<li>@todo: Publish Workflow</li>
7+
<li><a href="{{ path('current_menu_item_default') }}">Current Menu Item</a></li>
78
</ul>
89
<h2>About</h2>
910
<p>This test application is built into the MenuBundle. You can easily run

Tests/Resources/app/Resources/views/layout.html.twig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
<html>
22
<head>
33
<title>MenuBundle Test Application</title>
4+
<style>
5+
nav.side li {
6+
border: 1px black dashed;
7+
background-color: #eee;
8+
margin: 2px;
9+
width: 196px;
10+
list-style: none;
11+
}
12+
13+
nav.side li.current {
14+
background-color: green;
15+
}
16+
17+
nav.side li.current a {
18+
font-weight: bold;
19+
color: white;
20+
}
21+
</style>
422
</head>
523
<body>
624
<section id="header">

0 commit comments

Comments
 (0)