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

Commit

Permalink
Merge pull request #247 from symfony-cmf/feature/ISSUE-240_voter_for_…
Browse files Browse the repository at this point in the history
…sitemap_aware_docuemtents

implement voter for SitemapAwareDocuments
  • Loading branch information
ElectricMaxxx committed Jul 31, 2015
2 parents 755d8e2 + 52a1356 commit 6b82dc3
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 15 deletions.
5 changes: 5 additions & 0 deletions Resources/config/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<parameter key="cmf_seo.sitemap.guesser.default_change_frequency.class">Symfony\Cmf\Bundle\SeoBundle\Sitemap\DefaultChangeFrequencyGuesser</parameter>
<parameter key="cmf_seo.sitemap.voter_chain.class">Symfony\Cmf\Bundle\SeoBundle\Sitemap\VoterChain</parameter>
<parameter key="cmf_seo.sitemap.publish_workflow_voter.class">Symfony\Cmf\Bundle\SeoBundle\Sitemap\PublishWorkflowVoter</parameter>
<parameter key="cmf_seo.sitemap.voter.sitemap_aware_document.class">Symfony\Cmf\Bundle\SeoBundle\Sitemap\SitemapAwareDocumentVoter</parameter>
</parameters>

<services>
Expand Down Expand Up @@ -56,6 +57,10 @@
<tag name="cmf_seo.sitemap.voter" priority="-2"/>
</service>

<service id="cmf_seo.sitemap.voter.sitemap_aware_document" class="%cmf_seo.sitemap.voter.sitemap_aware_document.class%">
<tag name="cmf_seo.sitemap.voter" priority="-2"/>
</service>

<service id="cmf_seo.sitemap.provider" class="%cmf_seo.sitemap.url_information_provider.class%">
<argument type="service" id="cmf_seo.sitemap.loader_chain" />
<argument type="service" id="cmf_seo.sitemap.voter_chain" />
Expand Down
26 changes: 26 additions & 0 deletions Sitemap/SitemapAwareDocumentVoter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Symfony\Cmf\Bundle\SeoBundle\Sitemap;

use Symfony\Cmf\Bundle\SeoBundle\SitemapAwareInterface;

/**
* Implementing the SitemapAwareInterface will give the chance the let the document decide
* whether it wants to be displayed on sitemap or not.
*
* @author Maximilian Berghoff <[email protected]>
*/
class SitemapAwareDocumentVoter implements VoterInterface
{
/**
* {@inheritdoc}
*/
public function exposeOnSitemap($content, $sitemap)
{
if (!$content instanceof SitemapAwareInterface) {
return true;
}

return $content->isVisibleInSitemap($sitemap);
}
}
13 changes: 6 additions & 7 deletions SitemapElementInterface.php → SitemapAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@
namespace Symfony\Cmf\Bundle\SeoBundle;

/**
* Models implementing this interface can indicate whether they should be listed in the sitemap or not.
*
* For PHPCR-ODM documents used with the \Symfony\Cmf\Bundle\SeoBundle\Doctrine\Phpcr\SitemapUrlInformationProvider,
* make sure to call the property that stores this information "visible_for_sitemap",
* as the provider uses the property in the query rather than loading all documents and checking the method.
* Models implementing this interface can indicate whether they should be listed in the sitemap
* with a given sitemap name or not.
*
* @author Maximilian Berghoff <[email protected]>
*/
interface SitemapElementInterface
interface SitemapAwareInterface
{
/**
* Decision whether a document should be visible
* in sitemap or not.
*
* @param $sitemap
*
* @return bool
*/
public function isVisibleInSitemap();
public function isVisibleInSitemap($sitemap);
}
7 changes: 4 additions & 3 deletions Tests/Resources/Document/SitemapAwareContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
use Symfony\Cmf\Bundle\CoreBundle\Translatable\TranslatableInterface;
use Symfony\Cmf\Bundle\SeoBundle\SitemapElementInterface;
use Symfony\Cmf\Bundle\SeoBundle\SitemapAwareInterface;
use Symfony\Cmf\Component\Routing\RouteReferrersReadInterface;
use Symfony\Component\Routing\Route;

Expand All @@ -17,7 +17,7 @@
class SitemapAwareContent extends ContentBase implements
RouteReferrersReadInterface,
TranslatableInterface,
SitemapElementInterface
SitemapAwareInterface
{
/**
* @var string
Expand Down Expand Up @@ -56,9 +56,10 @@ public function __construct()
}

/**
* @param string $sitemap
* @return boolean
*/
public function isVisibleInSitemap()
public function isVisibleInSitemap($sitemap)
{
return $this->isVisibleForSitemap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Symfony\Cmf\Bundle\SeoBundle\Tests\Resources\Document;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishableInterface;
use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishTimePeriodInterface;
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route;
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
use Symfony\Cmf\Bundle\SeoBundle\SitemapElementInterface;
use Symfony\Cmf\Bundle\SeoBundle\SitemapAwareInterface;
use Symfony\Cmf\Component\Routing\RouteReferrersReadInterface;

/**
Expand All @@ -19,7 +19,7 @@ class SitemapAwareWithPublishWorkflowContent extends ContentBase implements
PublishableInterface,
PublishTimePeriodInterface,
RouteReferrersReadInterface,
SitemapElementInterface
SitemapAwareInterface
{
/**
* @var ArrayCollection|Route[]
Expand Down Expand Up @@ -61,9 +61,10 @@ public function __construct()
}

/**
* @return boolean
* @param string $sitemap
* @return bool
*/
public function isVisibleInSitemap()
public function isVisibleInSitemap($sitemap)
{
return $this->isVisibleForSitemap;
}
Expand Down
48 changes: 48 additions & 0 deletions Tests/Unit/Sitemap/SitemapAwareDocumentVoterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Symfony\Cmf\Bundle\SeoBundle\Tests\Unit\Sitemap;

use Symfony\Cmf\Bundle\SeoBundle\Sitemap\SitemapAwareDocumentVoter;
use Symfony\Cmf\Bundle\SeoBundle\Sitemap\VoterInterface;

/**
* @author Maximilian Berghoff <[email protected]>
*/
class SitemapAwareDocumentVoterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var VoterInterface
*/
protected $voter;
protected $sitemapAwareDocument;

public function setUp()
{
$this->voter = new SitemapAwareDocumentVoter();
$this->sitemapAwareDocument = $this->getMock('\Symfony\Cmf\Bundle\SeoBundle\SitemapAwareInterface');
}

public function testSitemapAwareDocumentShouldReturnTrueWhenDocumentIsExposed()
{
$this->sitemapAwareDocument
->expects($this->once())
->method('isVisibleInSitemap')
->will($this->returnValue(true));
$this->assertTrue($this->voter->exposeOnSitemap($this->sitemapAwareDocument, 'some-sitemap'));
}


public function testSitemapAwareDocumentShouldReturnFalseWhenDocumentIsNotExposed()
{
$this->sitemapAwareDocument
->expects($this->once())
->method('isVisibleInSitemap')
->will($this->returnValue(false));
$this->assertFalse($this->voter->exposeOnSitemap($this->sitemapAwareDocument, 'some-sitemap'));
}

public function testInvalidDocumentShouldReturnTrueToBeAwareForTheOtherVoters()
{
$this->assertTrue($this->voter->exposeOnSitemap(new \stdClass(), 'some-sitemap'));
}
}

0 comments on commit 6b82dc3

Please sign in to comment.