Skip to content

Commit

Permalink
MAGETWO-67054: Issue #2802, #1146: Fixing sitemap generation folder #…
Browse files Browse the repository at this point in the history
…9094

 - Merge Pull Request #9094 from rodrigowebjump/magento2:2802-fix-sitemap
  • Loading branch information
Oleksii Korshenko committed Apr 12, 2017
2 parents 23f7456 + 3cc0e5a commit bf5e9c1
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
/.settings
atlassian*
/nbproject
/robots.txt
/pub/robots.txt
/sitemap
/sitemap.xml
/pub/sitemap
/pub/sitemap.xml
/.idea
/.gitattributes
/app/config_sandbox
Expand Down
11 changes: 8 additions & 3 deletions app/code/Magento/Config/Model/Config/Backend/Admin/Robots.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
*/
namespace Magento\Config\Model\Config\Backend\Admin;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot;
use Magento\Framework\App\ObjectManager;

class Robots extends \Magento\Framework\App\Config\Value
{
Expand All @@ -32,6 +33,7 @@ class Robots extends \Magento\Framework\App\Config\Value
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
* @param DocumentRoot $documentRoot
*/
public function __construct(
\Magento\Framework\Model\Context $context,
Expand All @@ -41,10 +43,13 @@ public function __construct(
\Magento\Framework\Filesystem $filesystem,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
array $data = [],
\Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot $documentRoot = null
) {
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
$this->_directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);

$documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(DocumentRoot::class);
$this->_directory = $filesystem->getDirectoryWrite($documentRoot->getPath());
$this->_file = 'robots.txt';
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Config\Model\Config\Reader\Source\Deployed;

use Magento\Framework\Config\ConfigOptionsListConstants;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\DeploymentConfig;

/**
* Class DocumentRoot
* @package Magento\Config\Model\Config\Reader\Source\Deployed
*/
class DocumentRoot
{
/**
* @var DeploymentConfig
*/
private $config;

/**
* DocumentRoot constructor.
* @param DeploymentConfig $config
*/
public function __construct(DeploymentConfig $config)
{
$this->config = $config;
}

/**
* A shortcut to load the document root path from the DirectoryList based on the
* deployment configuration.
*
* @return string
*/
public function getPath()
{
return $this->isPub() ? DirectoryList::PUB : DirectoryList::ROOT;
}

/**
* Returns whether the deployment configuration specifies that the document root is
* in the pub/ folder. This affects ares such as sitemaps and robots.txt (and will
* likely be extended to control other areas).
*
* @return bool
*/
public function isPub()
{
return (bool)$this->config->get(ConfigOptionsListConstants::CONFIG_PATH_DOCUMENT_ROOT_IS_PUB);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Config\Test\Unit\Model\Config\Reader\Source\Deployed;

use Magento\Config\Model\Config\Reader;
use Magento\Config\Model\Config\Reader\Source\Deployed\SettingChecker;
use Magento\Framework\App\Config;
use Magento\Framework\App\DeploymentConfig;
use Magento\Config\Model\Placeholder\PlaceholderInterface;
use Magento\Config\Model\Placeholder\PlaceholderFactory;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Config\ConfigOptionsListConstants;

/**
* Test class for checking settings that defined in config file
*/
class DocumentRootTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Config|\PHPUnit_Framework_MockObject_MockObject
*/
private $configMock;

/**
* @var Reader\Source\Deployed\DocumentRoot
*/
private $documentRoot;

public function setUp()
{
$this->configMock = $this->getMockBuilder(DeploymentConfig::class)
->disableOriginalConstructor()
->getMock();

$this->documentRoot = new Reader\Source\Deployed\DocumentRoot($this->configMock);
}

/**
* Ensures that the path returned matches the pub/ path.
*/
public function testGetPath()
{
$this->configMockSetForDocumentRootIsPub();

$this->assertSame(DirectoryList::PUB, $this->documentRoot->getPath());
}

/**
* Ensures that the deployment configuration returns the mocked value for
* the pub/ folder.
*/
public function testIsPub()
{
$this->configMockSetForDocumentRootIsPub();

$this->assertSame(true, $this->documentRoot->isPub());
}

private function configMockSetForDocumentRootIsPub()
{
$this->configMock->expects($this->any())
->method('get')
->willReturnMap([
[
ConfigOptionsListConstants::CONFIG_PATH_DOCUMENT_ROOT_IS_PUB,
null,
true
],
]);
}
}
16 changes: 14 additions & 2 deletions app/code/Magento/Sitemap/Block/Adminhtml/Grid/Renderer/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
namespace Magento\Sitemap\Block\Adminhtml\Grid\Renderer;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot;
use Magento\Framework\App\ObjectManager;

class Link extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
{
Expand All @@ -24,20 +26,29 @@ class Link extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRe
*/
protected $_sitemapFactory;

/**
* @var DocumentRoot
*/
protected $documentRoot;

/**
* @param \Magento\Backend\Block\Context $context
* @param \Magento\Sitemap\Model\SitemapFactory $sitemapFactory
* @param \Magento\Framework\Filesystem $filesystem
* @param array $data
* @param DocumentRoot $documentRoot
*/
public function __construct(
\Magento\Backend\Block\Context $context,
\Magento\Sitemap\Model\SitemapFactory $sitemapFactory,
\Magento\Framework\Filesystem $filesystem,
array $data = []
array $data = [],
DocumentRoot $documentRoot = null
) {
$this->_sitemapFactory = $sitemapFactory;
$this->_filesystem = $filesystem;
$this->documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(DocumentRoot::class);

parent::__construct($context, $data);
}

Expand All @@ -54,7 +65,8 @@ public function render(\Magento\Framework\DataObject $row)
$url = $this->escapeHtml($sitemap->getSitemapUrl($row->getSitemapPath(), $row->getSitemapFilename()));

$fileName = preg_replace('/^\//', '', $row->getSitemapPath() . $row->getSitemapFilename());
$directory = $this->_filesystem->getDirectoryRead(DirectoryList::ROOT);
$documentRootPath = $this->documentRoot->getPath();
$directory = $this->_filesystem->getDirectoryRead($documentRootPath);
if ($directory->isFile($fileName)) {
return sprintf('<a href="%1$s">%1$s</a>', $url);
}
Expand Down
8 changes: 5 additions & 3 deletions app/code/Magento/Sitemap/Model/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
// @codingStandardsIgnoreFile

namespace Magento\Sitemap\Model;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot;
use Magento\Framework\App\ObjectManager;

/**
* Sitemap model
Expand Down Expand Up @@ -179,11 +179,13 @@ public function __construct(
\Magento\Framework\Stdlib\DateTime $dateTime,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
\Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot $documentRoot = null,
array $data = []
) {
$this->_escaper = $escaper;
$this->_sitemapData = $sitemapData;
$this->_directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
$documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(DocumentRoot::class);
$this->_directory = $filesystem->getDirectoryWrite($documentRoot->getPath());
$this->_categoryFactory = $categoryFactory;
$this->_productFactory = $productFactory;
$this->_cmsFactory = $cmsFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
namespace Magento\Config\Model\Config\Backend\Admin;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot;

/**
* @magentoAppArea adminhtml
Expand Down Expand Up @@ -33,11 +33,11 @@ protected function setUp()
$this->model = $objectManager->create(\Magento\Config\Model\Config\Backend\Admin\Robots::class);
$this->model->setPath('design/search_engine_robots/custom_instructions');
$this->model->afterLoad();

$documentRootPath = $objectManager->get(DocumentRoot::class)->getPath();
$this->rootDirectory = $objectManager->get(
\Magento\Framework\Filesystem::class
)->getDirectoryRead(
DirectoryList::ROOT
);
)->getDirectoryRead($documentRootPath);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion generated/.htaccess
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Order deny,allow
Order allow,deny
Deny from all
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class ConfigOptionsListConstants
const CONFIG_PATH_DB = 'db';
const CONFIG_PATH_RESOURCE = 'resource';
const CONFIG_PATH_CACHE_TYPES = 'cache_types';
const CONFIG_PATH_DOCUMENT_ROOT_IS_PUB = 'directories/document_root_is_pub';

/**#@-*/

/**#@+
Expand Down
2 changes: 1 addition & 1 deletion vendor/.htaccess
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Order allow,deny
Deny from all
Deny from all

0 comments on commit bf5e9c1

Please sign in to comment.