This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #288 from symfony-cmf/depth_information_in_sitemap…
…_1.3 Depth information in sitemap 1.3
- Loading branch information
Showing
10 changed files
with
339 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony CMF package. | ||
* | ||
* (c) 2011-2016 Symfony CMF | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Cmf\Bundle\SeoBundle\Sitemap; | ||
|
||
use Doctrine\Bundle\PHPCRBundle\ManagerRegistry; | ||
use Doctrine\Common\Util\ClassUtils; | ||
use Doctrine\ODM\PHPCR\DocumentManager; | ||
use Symfony\Cmf\Bundle\SeoBundle\Model\UrlInformation; | ||
|
||
/** | ||
* This guesser will add the depth of a document persisted on a phpcr node. | ||
* | ||
* @author Maximilian Berghoff <Maximilian.Berghoff@mayflower.de> | ||
*/ | ||
class DepthGuesser implements GuesserInterface | ||
{ | ||
/** | ||
* @var ManagerRegistry | ||
*/ | ||
private $managerRegistry; | ||
|
||
/** | ||
* @var int The depth of the content base path as the depth offset. | ||
*/ | ||
private $offset; | ||
|
||
/** | ||
* DepthGuesser constructor. | ||
* | ||
* @param ManagerRegistry $managerRegistry | ||
* @param $contentBasePath | ||
*/ | ||
public function __construct(ManagerRegistry $managerRegistry, $contentBasePath) | ||
{ | ||
$this->managerRegistry = $managerRegistry; | ||
$this->offset = ('/' === $contentBasePath) ? 1 : substr_count($contentBasePath, '/') + 1; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function guessValues(UrlInformation $urlInformation, $object, $sitemap) | ||
{ | ||
if (null !== $urlInformation->getDepth()) { | ||
return; | ||
} | ||
|
||
$manager = $this->managerRegistry->getManagerForClass(ClassUtils::getRealClass(get_class($object))); | ||
if (!$manager instanceof DocumentManager) { | ||
return; | ||
} | ||
|
||
$node = $manager->getNodeForDocument($object); | ||
if (null === $node) { | ||
return; | ||
} | ||
$urlInformation->setDepth($node->getDepth() - $this->offset); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
Tests/Functional/Doctrine/Phpcr/SitemapDocumentProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony CMF package. | ||
* | ||
* (c) 2011-2016 Symfony CMF | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Cmf\Bundle\SeoBundle\Tests\Functional\Doctrine\Phpcr; | ||
|
||
use Symfony\Cmf\Bundle\SeoBundle\Doctrine\Phpcr\SitemapDocumentProvider; | ||
use Symfony\Cmf\Component\Testing\Functional\BaseTestCase; | ||
|
||
/** | ||
* @author Maximilian Berghoff <Maximilian.Berghoff@mayflower.de> | ||
*/ | ||
class SitemapDocumentProviderTest extends BaseTestCase | ||
{ | ||
/** | ||
* @var SitemapDocumentProvider | ||
*/ | ||
private $documentProvider; | ||
|
||
public function setUp() | ||
{ | ||
$this->db('PHPCR')->createTestNode(); | ||
$this->dm = $this->db('PHPCR')->getOm(); | ||
$this->base = $this->dm->find(null, '/test'); | ||
$this->db('PHPCR')->loadFixtures(array( | ||
'Symfony\Cmf\Bundle\SeoBundle\Tests\Resources\DataFixtures\Phpcr\LoadSitemapData', | ||
)); | ||
|
||
$this->documentProvider = new SitemapDocumentProvider($this->dm); | ||
} | ||
|
||
public function testDocumentOrder() | ||
{ | ||
$documents = $this->documentProvider->load('default'); | ||
|
||
$paths = array(); | ||
foreach ($documents as $document) { | ||
$paths[] = $document->getId(); | ||
} | ||
|
||
$this->assertEquals( | ||
array( | ||
'/test/content/sitemap-aware', | ||
'/test/content/sitemap-aware-non-publish', | ||
'/test/content/sitemap-aware-publish', | ||
), | ||
$paths | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony CMF package. | ||
* | ||
* (c) 2011-2016 Symfony CMF | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Cmf\Bundle\SeoBundle\Tests\Unit\Sitemap; | ||
|
||
use Doctrine\Bundle\PHPCRBundle\ManagerRegistry; | ||
use Doctrine\ODM\PHPCR\DocumentManager; | ||
use PHPCR\NodeInterface; | ||
use Symfony\Cmf\Bundle\SeoBundle\Model\UrlInformation; | ||
use Symfony\Cmf\Bundle\SeoBundle\Sitemap\DepthGuesser; | ||
use Symfony\Cmf\Bundle\SeoBundle\Sitemap\GuesserInterface; | ||
|
||
/** | ||
* @author Maximilian Berghoff <Maximilian.Berghoff@mayflower.de> | ||
*/ | ||
class DepthGuesserTest extends GuesserTestCase | ||
{ | ||
/** | ||
* @var \stdClass | ||
*/ | ||
protected $object; | ||
|
||
/** | ||
* @var ManagerRegistry | ||
*/ | ||
protected $managerRegistry; | ||
|
||
/** | ||
* @var DocumentManager | ||
*/ | ||
protected $documentManager; | ||
|
||
/** | ||
* @var DepthGuesser | ||
*/ | ||
protected $guesser; | ||
|
||
/** | ||
* @var NodeInterface | ||
*/ | ||
protected $node; | ||
|
||
/** | ||
* Create the guesser for this test. | ||
* | ||
* @return GuesserInterface | ||
*/ | ||
protected function createGuesser() | ||
{ | ||
$this->buildMocks(); | ||
|
||
$this->managerRegistry | ||
->expects($this->any()) | ||
->method('getManagerForClass') | ||
->with(get_class($this->object)) | ||
->will($this->returnValue($this->documentManager)); | ||
$this->documentManager | ||
->expects($this->any()) | ||
->method('getNodeForDocument') | ||
->with($this->object) | ||
->willReturn($this->node); | ||
$this->node | ||
->expects($this->any()) | ||
->method('getDepth') | ||
->will($this->returnValue(3)); | ||
|
||
return $this->guesser; | ||
} | ||
|
||
/** | ||
* @return object | ||
*/ | ||
protected function createData() | ||
{ | ||
return $this->object; | ||
} | ||
|
||
/** | ||
* Provide list of fields in UrlInformation covered by this guesser. | ||
* | ||
* @return array | ||
*/ | ||
protected function getFields() | ||
{ | ||
return array('depth'); | ||
} | ||
|
||
/** | ||
* Method to extract mock building. | ||
* | ||
* @param string $contentBasePath | ||
*/ | ||
private function buildMocks($contentBasePath = '/cms/test') | ||
{ | ||
$this->managerRegistry = $this->getMockBuilder('Doctrine\Bundle\PHPCRBundle\ManagerRegistry') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$this->documentManager = $this->getMockBuilder('Doctrine\ODM\PHPCR\DocumentManager') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$this->node = $this->getMock('PHPCR\NodeInterface'); | ||
$this->object = new \stdClass(); | ||
$this->guesser = new DepthGuesser($this->managerRegistry, $contentBasePath); | ||
} | ||
|
||
public function testNullOnNoManager() | ||
{ | ||
$this->buildMocks(); | ||
$this->managerRegistry | ||
->expects($this->any()) | ||
->method('getManagerForClass') | ||
->with(get_class($this->object)) | ||
->will($this->returnValue(null)); | ||
$urlInformation = new UrlInformation(); | ||
$this->guesser->guessValues($urlInformation, $this->object, 'default'); | ||
|
||
$this->assertNull($urlInformation->getDepth()); | ||
} | ||
|
||
public function testDepthOffsetCalculation() | ||
{ | ||
$this->buildMocks(); | ||
|
||
$this->managerRegistry | ||
->expects($this->any()) | ||
->method('getManagerForClass') | ||
->with(get_class($this->object)) | ||
->will($this->returnValue($this->documentManager)); | ||
$this->documentManager | ||
->expects($this->any()) | ||
->method('getNodeForDocument') | ||
->with($this->object) | ||
->willReturn($this->node); | ||
$this->node | ||
->expects($this->any()) | ||
->method('getDepth') | ||
->will($this->returnValue(4)); | ||
$urlInformation = new UrlInformation(); | ||
$this->guesser->guessValues($urlInformation, $this->object, 'default'); | ||
|
||
$this->assertEquals(1, $urlInformation->getDepth()); | ||
} | ||
|
||
public function testRootEdgeCase() | ||
{ | ||
$this->buildMocks('/'); | ||
|
||
$this->managerRegistry | ||
->expects($this->any()) | ||
->method('getManagerForClass') | ||
->with(get_class($this->object)) | ||
->will($this->returnValue($this->documentManager)); | ||
$this->documentManager | ||
->expects($this->any()) | ||
->method('getNodeForDocument') | ||
->with($this->object) | ||
->willReturn($this->node); | ||
$this->node | ||
->expects($this->any()) | ||
->method('getDepth') | ||
->will($this->returnValue(4)); | ||
$urlInformation = new UrlInformation(); | ||
$this->guesser->guessValues($urlInformation, $this->object, 'default'); | ||
|
||
$this->assertEquals(3, $urlInformation->getDepth()); | ||
} | ||
} |
Oops, something went wrong.