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

adjust to publish workflow refactoring #92

Merged
merged 3 commits into from
Jul 15, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions ContentAwareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishWorkflowChecker;

use Psr\Log\LoggerInterface;

use Symfony\Cmf\Bundle\MenuBundle\Voter\VoterInterface;
use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishWorkflowCheckerInterface;

/**
* This factory builds menu items from the menu nodes and builds urls based on
Expand Down Expand Up @@ -52,9 +53,16 @@ class ContentAwareFactory extends RouterAwareFactory
private $logger;

/**
* @var PublishWorkflowCheckerInterface
* @var SecurityContextInterface
*/
private $publishChecker;
private $securityContext;

/**
* The permission to check for when doing the publish workflow check.
*
* @var string
*/
private $publishWorkflowPermission = PublishWorkflowChecker::VIEW_ATTRIBUTE;

/**
* Whether to return null or a MenuItem without any URL if no URL can be
Expand All @@ -65,22 +73,23 @@ class ContentAwareFactory extends RouterAwareFactory
private $allowEmptyItems;

/**
* @param ContainerInterface $container to fetch the request in order to determine
* whether this is the current menu item
* @param UrlGeneratorInterface $generator for the parent class
* @param UrlGeneratorInterface $contentRouter to generate routes when
* content is set
* @param SecurityContextInterface $securityContext the publish workflow
* checker to check if menu items are published.
* @param LoggerInterface $logger
*/
public function __construct(
UrlGeneratorInterface $generator,
UrlGeneratorInterface $contentRouter,
PublishWorkflowCheckerInterface $publishChecker,
SecurityContextInterface $securityContext,
LoggerInterface $logger
)
{
parent::__construct($generator);
$this->contentRouter = $contentRouter;
$this->publishChecker = $publishChecker;
$this->securityContext = $securityContext;
$this->logger = $logger;
$this->linkTypes = array('route', 'uri', 'content');
}
Expand All @@ -107,6 +116,17 @@ public function setAllowEmptyItems($allowEmptyItems)
$this->allowEmptyItems = $allowEmptyItems;
}

/**
* What attribute to use in the publish workflow check. This typically
* is VIEW or VIEW_ANONYMOUS.
*
* @param string $attribute
*/
public function setPublishWorkflowPermission($attribute)
{
$this->publishWorkflowPermission = $attribute;
}

/**
* Add a voter to decide on current item.
*
Expand Down Expand Up @@ -148,7 +168,7 @@ public function createFromNode(NodeInterface $node)
}

foreach ($node->getChildren() as $childNode) {
if (false === $this->publishChecker->checkIsPublished($childNode)) {
if (!$this->securityContext->isGranted($this->publishWorkflowPermission, $childNode)) {
continue;
}

Expand Down Expand Up @@ -197,8 +217,8 @@ public function createItem($name, array $options = array())
case 'content':
try {
$options['uri'] = $this->contentRouter->generate(
$options['content'],
$options['routeParameters'],
$options['content'],
$options['routeParameters'],
$options['routeAbsolute']
);
} catch (RouteNotFoundException $e) {
Expand Down
5 changes: 3 additions & 2 deletions Document/MenuNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
namespace Symfony\Cmf\Bundle\MenuBundle\Document;

use Knp\Menu\NodeInterface;
use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishWorkflowInterface;
use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishableWriteInterface;
use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishTimePeriodWriteInterface;

/**
* This class represents a menu node for the cmf.
*
* @author Uwe Jäger <[email protected]>
* @author Daniel Leech <[email protected]>
*/
class MenuNode implements NodeInterface, PublishWorkflowInterface
class MenuNode implements NodeInterface, PublishTimePeriodWriteInterface, PublishableWriteInterface
{
/**
* Id of this menu node.
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/phpcr-menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<service id="cmf_menu.factory" class="%cmf_menu.factory_class%">
<argument type="service" id="router"/>
<argument/> <!-- content url generator -->
<argument type="service" id="cmf_core.publish_workflow_checker"/>
<argument type="service" id="cmf_core.publish_workflow.checker"/>
<argument type="service" id="logger"/>
<call method="setAllowEmptyItems">
<argument>%cmf_menu.allow_empty_items%</argument>
Expand Down
42 changes: 27 additions & 15 deletions Tests/Unit/ContentAwareFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,35 @@

class ContentAwareFactoryTest extends \PHPUnit_Framework_Testcase
{
private $urlGenerator;
private $contentUrlGenerator;
private $securityContext;
private $logger;

private $node1;
private $node2;
private $node3;
private $content;

public function setUp()
{
$this->pwfc = $this->getMock(
'Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishWorkflowCheckerInterface'
);
$this->urlGenerator = $this->getMock(
'Symfony\Component\Routing\Generator\UrlGeneratorInterface'
);
$this->contentUrlGenerator = $this->getMock(
'Symfony\Component\Routing\Generator\UrlGeneratorInterface'
);
$this->securityContext = $this->getMock(
'Symfony\Component\Security\Core\SecurityContext'
);
$this->logger = $this->getMock(
'Psr\Log\LoggerInterface'
);

$this->factory = new ContentAwareFactory(
$this->urlGenerator,
$this->contentUrlGenerator,
$this->pwfc,
$this->securityContext,
$this->logger,
false // refactore this empty items option
);
Expand Down Expand Up @@ -77,9 +87,10 @@ public function testCreateFromNode($options)
->method('getChildren')
->will($this->returnValue(array()));

$mock = $this->pwfc->expects($this->at(0))
->method('checkIsPublished')
->with($this->node2);
$mock = $this->securityContext->expects($this->at(0))
->method('isGranted')
->with(array('VIEW', $this->node2))
;

if ($options['node2_is_published']) {
$mock->will($this->returnValue(true));
Expand All @@ -92,9 +103,10 @@ public function testCreateFromNode($options)
$mock->will($this->returnValue(false));
}

$this->pwfc->expects($this->at(1))
->method('checkIsPublished')
->with($this->node3);
$this->securityContext->expects($this->at(1))
->method('isGranted')
->with(array('VIEW', $this->node3))
;

$res = $this->factory->createFromNode($this->node1);
$this->assertInstanceOf('Knp\Menu\MenuItem', $res);
Expand Down Expand Up @@ -180,9 +192,9 @@ protected function prepareCreateItemTests($name, $options)
is_null($options['route']) &&
is_null($options['content']) &&
!in_array($options['linkType'], array(
'route',
'uri',
'content',
'route',
'uri',
'content',
''
))
) {
Expand All @@ -196,7 +208,7 @@ protected function prepareCreateItemTests($name, $options)
}

if (
null == $options['linkType'] &&
null == $options['linkType'] &&
empty($options['uri']) &&
!empty($options['route'])
) {
Expand All @@ -212,7 +224,7 @@ protected function prepareCreateItemTests($name, $options)
}

if (
null === $options['linkType'] &&
null === $options['linkType'] &&
empty($options['uri']) &&
empty($options['route']) &&
!empty($options['content'])
Expand Down
4 changes: 2 additions & 2 deletions Tests/Unit/Document/MenuNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ public function testAddChild()
$this->assertSame($c2, $ret);
}

public function testPublishWorkflowInterface()
public function testPublishTimePeriodInterface()
{
$startDate = new \DateTime('2013-01-01');
$endDate = new \DateTime('2013-02-01');

$n = new MenuNode;

$this->assertInstanceOf(
'Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishWorkflowInterface',
'Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishTimePeriodInterface',
$n
);

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"doctrine/phpcr-bundle": "1.0.*",
"doctrine/phpcr-odm": "1.0.*",
"knplabs/knp-menu-bundle": "1.1.*",
"symfony-cmf/core-bundle": "1.0.*"
"symfony-cmf/core-bundle": "~1.0.0-beta2"
},
"require-dev": {
"symfony-cmf/testing": "1.0.*",
Expand Down