Skip to content

Commit bf5e9c1

Browse files
author
Oleksii Korshenko
committed
MAGETWO-67054: Issue #2802, #1146: Fixing sitemap generation folder #9094
- Merge Pull Request #9094 from rodrigowebjump/magento2:2802-fix-sitemap
2 parents 23f7456 + 3cc0e5a commit bf5e9c1

File tree

10 files changed

+168
-14
lines changed

10 files changed

+168
-14
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
/.settings
66
atlassian*
77
/nbproject
8+
/robots.txt
9+
/pub/robots.txt
810
/sitemap
911
/sitemap.xml
12+
/pub/sitemap
13+
/pub/sitemap.xml
1014
/.idea
1115
/.gitattributes
1216
/app/config_sandbox

app/code/Magento/Config/Model/Config/Backend/Admin/Robots.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
*/
1010
namespace Magento\Config\Model\Config\Backend\Admin;
1111

12-
use Magento\Framework\App\Filesystem\DirectoryList;
12+
use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot;
13+
use Magento\Framework\App\ObjectManager;
1314

1415
class Robots extends \Magento\Framework\App\Config\Value
1516
{
@@ -32,6 +33,7 @@ class Robots extends \Magento\Framework\App\Config\Value
3233
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
3334
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
3435
* @param array $data
36+
* @param DocumentRoot $documentRoot
3537
*/
3638
public function __construct(
3739
\Magento\Framework\Model\Context $context,
@@ -41,10 +43,13 @@ public function __construct(
4143
\Magento\Framework\Filesystem $filesystem,
4244
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
4345
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
44-
array $data = []
46+
array $data = [],
47+
\Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot $documentRoot = null
4548
) {
4649
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
47-
$this->_directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
50+
51+
$documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(DocumentRoot::class);
52+
$this->_directory = $filesystem->getDirectoryWrite($documentRoot->getPath());
4853
$this->_file = 'robots.txt';
4954
}
5055

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Config\Model\Config\Reader\Source\Deployed;
8+
9+
use Magento\Framework\Config\ConfigOptionsListConstants;
10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\App\DeploymentConfig;
12+
13+
/**
14+
* Class DocumentRoot
15+
* @package Magento\Config\Model\Config\Reader\Source\Deployed
16+
*/
17+
class DocumentRoot
18+
{
19+
/**
20+
* @var DeploymentConfig
21+
*/
22+
private $config;
23+
24+
/**
25+
* DocumentRoot constructor.
26+
* @param DeploymentConfig $config
27+
*/
28+
public function __construct(DeploymentConfig $config)
29+
{
30+
$this->config = $config;
31+
}
32+
33+
/**
34+
* A shortcut to load the document root path from the DirectoryList based on the
35+
* deployment configuration.
36+
*
37+
* @return string
38+
*/
39+
public function getPath()
40+
{
41+
return $this->isPub() ? DirectoryList::PUB : DirectoryList::ROOT;
42+
}
43+
44+
/**
45+
* Returns whether the deployment configuration specifies that the document root is
46+
* in the pub/ folder. This affects ares such as sitemaps and robots.txt (and will
47+
* likely be extended to control other areas).
48+
*
49+
* @return bool
50+
*/
51+
public function isPub()
52+
{
53+
return (bool)$this->config->get(ConfigOptionsListConstants::CONFIG_PATH_DOCUMENT_ROOT_IS_PUB);
54+
}
55+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Config\Test\Unit\Model\Config\Reader\Source\Deployed;
7+
8+
use Magento\Config\Model\Config\Reader;
9+
use Magento\Config\Model\Config\Reader\Source\Deployed\SettingChecker;
10+
use Magento\Framework\App\Config;
11+
use Magento\Framework\App\DeploymentConfig;
12+
use Magento\Config\Model\Placeholder\PlaceholderInterface;
13+
use Magento\Config\Model\Placeholder\PlaceholderFactory;
14+
use Magento\Framework\App\Filesystem\DirectoryList;
15+
use Magento\Framework\Config\ConfigOptionsListConstants;
16+
17+
/**
18+
* Test class for checking settings that defined in config file
19+
*/
20+
class DocumentRootTest extends \PHPUnit_Framework_TestCase
21+
{
22+
/**
23+
* @var Config|\PHPUnit_Framework_MockObject_MockObject
24+
*/
25+
private $configMock;
26+
27+
/**
28+
* @var Reader\Source\Deployed\DocumentRoot
29+
*/
30+
private $documentRoot;
31+
32+
public function setUp()
33+
{
34+
$this->configMock = $this->getMockBuilder(DeploymentConfig::class)
35+
->disableOriginalConstructor()
36+
->getMock();
37+
38+
$this->documentRoot = new Reader\Source\Deployed\DocumentRoot($this->configMock);
39+
}
40+
41+
/**
42+
* Ensures that the path returned matches the pub/ path.
43+
*/
44+
public function testGetPath()
45+
{
46+
$this->configMockSetForDocumentRootIsPub();
47+
48+
$this->assertSame(DirectoryList::PUB, $this->documentRoot->getPath());
49+
}
50+
51+
/**
52+
* Ensures that the deployment configuration returns the mocked value for
53+
* the pub/ folder.
54+
*/
55+
public function testIsPub()
56+
{
57+
$this->configMockSetForDocumentRootIsPub();
58+
59+
$this->assertSame(true, $this->documentRoot->isPub());
60+
}
61+
62+
private function configMockSetForDocumentRootIsPub()
63+
{
64+
$this->configMock->expects($this->any())
65+
->method('get')
66+
->willReturnMap([
67+
[
68+
ConfigOptionsListConstants::CONFIG_PATH_DOCUMENT_ROOT_IS_PUB,
69+
null,
70+
true
71+
],
72+
]);
73+
}
74+
}

app/code/Magento/Sitemap/Block/Adminhtml/Grid/Renderer/Link.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
namespace Magento\Sitemap\Block\Adminhtml\Grid\Renderer;
1212

1313
use Magento\Framework\App\Filesystem\DirectoryList;
14+
use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot;
15+
use Magento\Framework\App\ObjectManager;
1416

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

29+
/**
30+
* @var DocumentRoot
31+
*/
32+
protected $documentRoot;
33+
2734
/**
2835
* @param \Magento\Backend\Block\Context $context
2936
* @param \Magento\Sitemap\Model\SitemapFactory $sitemapFactory
3037
* @param \Magento\Framework\Filesystem $filesystem
3138
* @param array $data
39+
* @param DocumentRoot $documentRoot
3240
*/
3341
public function __construct(
3442
\Magento\Backend\Block\Context $context,
3543
\Magento\Sitemap\Model\SitemapFactory $sitemapFactory,
3644
\Magento\Framework\Filesystem $filesystem,
37-
array $data = []
45+
array $data = [],
46+
DocumentRoot $documentRoot = null
3847
) {
3948
$this->_sitemapFactory = $sitemapFactory;
4049
$this->_filesystem = $filesystem;
50+
$this->documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(DocumentRoot::class);
51+
4152
parent::__construct($context, $data);
4253
}
4354

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

5667
$fileName = preg_replace('/^\//', '', $row->getSitemapPath() . $row->getSitemapFilename());
57-
$directory = $this->_filesystem->getDirectoryRead(DirectoryList::ROOT);
68+
$documentRootPath = $this->documentRoot->getPath();
69+
$directory = $this->_filesystem->getDirectoryRead($documentRootPath);
5870
if ($directory->isFile($fileName)) {
5971
return sprintf('<a href="%1$s">%1$s</a>', $url);
6072
}

app/code/Magento/Sitemap/Model/Sitemap.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
// @codingStandardsIgnoreFile
88

99
namespace Magento\Sitemap\Model;
10-
11-
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot;
11+
use Magento\Framework\App\ObjectManager;
1212

1313
/**
1414
* Sitemap model
@@ -179,11 +179,13 @@ public function __construct(
179179
\Magento\Framework\Stdlib\DateTime $dateTime,
180180
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
181181
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
182+
\Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot $documentRoot = null,
182183
array $data = []
183184
) {
184185
$this->_escaper = $escaper;
185186
$this->_sitemapData = $sitemapData;
186-
$this->_directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
187+
$documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(DocumentRoot::class);
188+
$this->_directory = $filesystem->getDirectoryWrite($documentRoot->getPath());
187189
$this->_categoryFactory = $categoryFactory;
188190
$this->_productFactory = $productFactory;
189191
$this->_cmsFactory = $cmsFactory;

dev/tests/integration/testsuite/Magento/Config/Model/Config/Backend/Admin/RobotsTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
namespace Magento\Config\Model\Config\Backend\Admin;
77

8-
use Magento\Framework\App\Filesystem\DirectoryList;
8+
use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot;
99

1010
/**
1111
* @magentoAppArea adminhtml
@@ -33,11 +33,11 @@ protected function setUp()
3333
$this->model = $objectManager->create(\Magento\Config\Model\Config\Backend\Admin\Robots::class);
3434
$this->model->setPath('design/search_engine_robots/custom_instructions');
3535
$this->model->afterLoad();
36+
37+
$documentRootPath = $objectManager->get(DocumentRoot::class)->getPath();
3638
$this->rootDirectory = $objectManager->get(
3739
\Magento\Framework\Filesystem::class
38-
)->getDirectoryRead(
39-
DirectoryList::ROOT
40-
);
40+
)->getDirectoryRead($documentRootPath);
4141
}
4242

4343
/**

generated/.htaccess

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Order deny,allow
1+
Order allow,deny
22
Deny from all

lib/internal/Magento/Framework/Config/ConfigOptionsListConstants.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class ConfigOptionsListConstants
3030
const CONFIG_PATH_DB = 'db';
3131
const CONFIG_PATH_RESOURCE = 'resource';
3232
const CONFIG_PATH_CACHE_TYPES = 'cache_types';
33+
const CONFIG_PATH_DOCUMENT_ROOT_IS_PUB = 'directories/document_root_is_pub';
34+
3335
/**#@-*/
3436

3537
/**#@+

vendor/.htaccess

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Order allow,deny
2-
Deny from all
2+
Deny from all

0 commit comments

Comments
 (0)