Skip to content

Commit

Permalink
MAGETWO-47607: [Github] Sitemap generation in wrong folder when vhost…
Browse files Browse the repository at this point in the history
… is connected to pub folder
  • Loading branch information
Sergey Semenov committed Jun 12, 2017
1 parent 434c2d4 commit 6edb0da
Show file tree
Hide file tree
Showing 8 changed files with 373 additions and 4 deletions.
27 changes: 25 additions & 2 deletions app/code/Magento/Robots/Block/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,44 @@
*/
namespace Magento\Robots\Block;

use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\Framework\View\Element\Context;
use Magento\Robots\Model\Config\Value;
use Magento\Robots\Model\Robots;
use Magento\Store\Model\StoreResolver;

/**
* Prepares base content for robots.txt file
* Robots Block Class.
* Prepares base content for robots.txt and implements Page Cache functionality.
*
* @api
*/
class Data extends AbstractBlock
class Data extends AbstractBlock implements IdentityInterface
{
/**
* @var Robots
*/
private $robots;
/**
* @var StoreResolver
*/
private $storeResolver;

/**
* @param Context $context
* @param Robots $robots
* @param StoreResolver $storeResolver
* @param array $data
*/
public function __construct(
Context $context,
Robots $robots,
StoreResolver $storeResolver,
array $data = []
) {
$this->robots = $robots;
$this->storeResolver = $storeResolver;

parent::__construct($context, $data);
}
Expand All @@ -45,4 +56,16 @@ protected function _toHtml()
{
return $this->robots->getData();
}

/**
* Get unique page cache identities
*
* @return array
*/
public function getIdentities()
{
return [
Value::CACHE_TAG . '_' . $this->storeResolver->getCurrentStoreId(),
];
}
}
85 changes: 85 additions & 0 deletions app/code/Magento/Robots/Model/Config/Value.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Robots\Model\Config;

use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\Value as ConfigValue;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Registry;
use Magento\Store\Model\StoreResolver;

/**
* Backend model for design/search_engine_robots/custom_instructions configuration value.
* Required to implement Page Cache functionality.
*/
class Value extends ConfigValue implements IdentityInterface
{
/**
* Cache tag for robots.txt cached data
*/
const CACHE_TAG = 'robots';

/**
* Model cache tag for clear cache in after save and after delete
*
* @var string
*/
protected $_cacheTag = true;

/**
* @var StoreResolver
*/
private $storeResolver;

/**
* @param Context $context
* @param Registry $registry
* @param ScopeConfigInterface $config
* @param TypeListInterface $cacheTypeList
* @param StoreResolver $storeResolver
* @param AbstractResource|null $resource
* @param AbstractDb|null $resourceCollection
* @param array $data
*/
public function __construct(
Context $context,
Registry $registry,
ScopeConfigInterface $config,
TypeListInterface $cacheTypeList,
StoreResolver $storeResolver,
AbstractResource $resource = null,
AbstractDb $resourceCollection = null,
array $data = []
) {
$this->storeResolver = $storeResolver;

parent::__construct(
$context,
$registry,
$config,
$cacheTypeList,
$resource,
$resourceCollection,
$data
);
}

/**
* Get unique page cache identities
*
* @return array
*/
public function getIdentities()
{
return [
self::CACHE_TAG . '_' . $this->storeResolver->getCurrentStoreId(),
];
}
}
122 changes: 122 additions & 0 deletions app/code/Magento/Robots/Test/Unit/Block/DataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Robots\Test\Unit\Block;

class DataTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Robots\Block\Data
*/
private $block;

/**
* @var \Magento\Framework\View\Element\Context|\PHPUnit_Framework_MockObject_MockObject
*/
private $context;

/**
* @var \Magento\Robots\Model\Robots|\PHPUnit_Framework_MockObject_MockObject
*/
private $robots;

/**
* @var \Magento\Store\Model\StoreResolver|\PHPUnit_Framework_MockObject_MockObject
*/
private $storeResolver;

/**
* @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $eventManagerMock;

protected function setUp()
{
$this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
->getMockForAbstractClass();

$this->context = $this->getMockBuilder(\Magento\Framework\View\Element\Context::class)
->disableOriginalConstructor()
->getMock();

$this->context->expects($this->any())
->method('getEventManager')
->willReturn($this->eventManagerMock);

$this->robots = $this->getMockBuilder(\Magento\Robots\Model\Robots::class)
->disableOriginalConstructor()
->getMock();

$this->storeResolver = $this->getMockBuilder(\Magento\Store\Model\StoreResolver::class)
->disableOriginalConstructor()
->getMock();

$this->block = new \Magento\Robots\Block\Data(
$this->context,
$this->robots,
$this->storeResolver
);
}

/**
* Check that toHtml() method returns specified text data
*/
public function testToHtml()
{
$expected = 'test';

$this->initEventManagerMock($expected);

$this->robots->expects($this->once())
->method('getData')
->willReturn($expected);

$this->assertEquals($expected, $this->block->toHtml());
}

/**
* Check that getIdentities() method returns specified cache tag
*/
public function testGetIdentities()
{
$storeId = 1;

$this->storeResolver->expects($this->once())
->method('getCurrentStoreId')
->willReturn($storeId);

$expected = [
\Magento\Robots\Model\Config\Value::CACHE_TAG . '_' . $storeId,
];
$this->assertEquals($expected, $this->block->getIdentities());
}

/**
* Initialize mock object of Event Manager
*
* @param string $data
* @return void
*/
protected function initEventManagerMock($data)
{
$this->eventManagerMock->expects($this->any())
->method('dispatch')
->willReturnMap([
[
'view_block_abstract_to_html_before',
[
'block' => $this->block,
],
],
[
'view_block_abstract_to_html_after',
[
'block' => $this->block,
'transport' => new \Magento\Framework\DataObject(['html' => $data]),
],
],
]);
}
}
85 changes: 85 additions & 0 deletions app/code/Magento/Robots/Test/Unit/Model/Config/ValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Robots\Test\Unit\Model\Config;

class ValueTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Robots\Model\Config\Value
*/
private $model;

/**
* @var \Magento\Framework\Model\Context|\PHPUnit_Framework_MockObject_MockObject
*/
private $context;

/**
* @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
*/
private $registry;

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $scopeConfig;

/**
* @var \Magento\Framework\App\Cache\TypeListInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $typeList;

/**
* @var \Magento\Store\Model\StoreResolver|\PHPUnit_Framework_MockObject_MockObject
*/
private $storeResolver;

protected function setUp()
{
$this->context = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
->disableOriginalConstructor()
->getMock();

$this->registry = $this->getMockBuilder(\Magento\Framework\Registry::class)
->disableOriginalConstructor()
->getMock();

$this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
->getMockForAbstractClass();

$this->typeList = $this->getMockBuilder(\Magento\Framework\App\Cache\TypeListInterface::class)
->getMockForAbstractClass();

$this->storeResolver = $this->getMockBuilder(\Magento\Store\Model\StoreResolver::class)
->disableOriginalConstructor()
->getMock();

$this->model = new \Magento\Robots\Model\Config\Value(
$this->context,
$this->registry,
$this->scopeConfig,
$this->typeList,
$this->storeResolver
);
}

/**
* Check that getIdentities() method returns specified cache tag
*/
public function testGetIdentities()
{
$storeId = 1;

$this->storeResolver->expects($this->once())
->method('getCurrentStoreId')
->willReturn($storeId);

$expected = [
\Magento\Robots\Model\Config\Value::CACHE_TAG . '_' . $storeId,
];
$this->assertEquals($expected, $this->model->getIdentities());
}
}
17 changes: 16 additions & 1 deletion app/code/Magento/Sitemap/Block/Robots.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
*/
namespace Magento\Sitemap\Block;

use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\Framework\View\Element\Context;
use Magento\Robots\Model\Config\Value;
use Magento\Sitemap\Helper\Data as SitemapHelper;
use Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory;
use Magento\Store\Model\StoreResolver;
Expand All @@ -15,7 +18,7 @@
*
* @api
*/
class Robots extends \Magento\Framework\View\Element\AbstractBlock
class Robots extends AbstractBlock implements IdentityInterface
{
/**
* @var StoreResolver
Expand Down Expand Up @@ -87,4 +90,16 @@ protected function _toHtml()

return $result;
}

/**
* Get unique page cache identities
*
* @return array
*/
public function getIdentities()
{
return [
Value::CACHE_TAG . '_' . $this->storeResolver->getCurrentStoreId(),
];
}
}
Loading

0 comments on commit 6edb0da

Please sign in to comment.