Skip to content

Commit 3f3a788

Browse files
committed
Merge remote-tracking branch 'origin/MC-21860' into 2.3-develop-pr38
2 parents 67b6531 + 071b47b commit 3f3a788

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Framework\App\ObjectManager;
1111
use Magento\Framework\DataObject;
1212
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Filesystem;
1314
use Magento\Framework\UrlInterface;
1415
use Magento\Robots\Model\Config\Value;
1516
use Magento\Sitemap\Model\ItemProvider\ItemProviderInterface;
@@ -191,6 +192,16 @@ class Sitemap extends \Magento\Framework\Model\AbstractModel implements \Magento
191192
*/
192193
private $lastModMinTsVal;
193194

195+
/**
196+
* @var Filesystem
197+
*/
198+
private $filesystem;
199+
200+
/**
201+
* @var DocumentRoot
202+
*/
203+
private $documentRoot;
204+
194205
/**
195206
* Initialize dependencies.
196207
*
@@ -238,8 +249,9 @@ public function __construct(
238249
) {
239250
$this->_escaper = $escaper;
240251
$this->_sitemapData = $sitemapData;
241-
$documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(DocumentRoot::class);
242-
$this->_directory = $filesystem->getDirectoryWrite($documentRoot->getPath());
252+
$this->documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(DocumentRoot::class);
253+
$this->filesystem = $filesystem;
254+
$this->_directory = $filesystem->getDirectoryWrite($this->documentRoot->getPath());
243255
$this->_categoryFactory = $categoryFactory;
244256
$this->_productFactory = $productFactory;
245257
$this->_cmsFactory = $cmsFactory;
@@ -727,8 +739,8 @@ protected function _getFormattedLastmodDate($date)
727739
*/
728740
protected function _getDocumentRoot()
729741
{
730-
// phpcs:ignore Magento2.Functions.DiscouragedFunction
731-
return realpath($this->_request->getServer('DOCUMENT_ROOT'));
742+
return $this->filesystem->getDirectoryRead($this->documentRoot->getPath())
743+
->getAbsolutePath();
732744
}
733745

734746
/**
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Sitemap\Model;
10+
11+
use Magento\Framework\App\Filesystem\DirectoryList;
12+
use Magento\Framework\Filesystem;
13+
use Magento\Framework\ObjectManagerInterface;
14+
use Magento\Sitemap\Model\Sitemap;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use Magento\TestFramework\Request;
17+
use Zend\Stdlib\Parameters;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* Test for Sitemap
22+
*/
23+
class SitemapTest extends TestCase
24+
{
25+
/**
26+
* @var Sitemap
27+
*/
28+
private $model;
29+
30+
/**
31+
* @var ObjectManagerInterface
32+
*/
33+
private $objectManager;
34+
35+
/**
36+
* @var Filesystem
37+
*/
38+
private $filesystem;
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
protected function setUp()
44+
{
45+
$this->objectManager = Bootstrap::getObjectManager();
46+
$this->model = $this->objectManager->get(Sitemap::class);
47+
$this->filesystem = $this->objectManager->get(Filesystem::class);
48+
}
49+
50+
/**
51+
* Test get sitemap URL from parent root directory path
52+
*
53+
* @return void
54+
*/
55+
public function testGetSitemapUrlFromParentRootDirectoryPath(): void
56+
{
57+
$baseUrl = 'http://localhost/';
58+
59+
$rootDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT)
60+
->getAbsolutePath();
61+
$requestPath = dirname($rootDir);
62+
63+
/** @var Request $request */
64+
$request = $this->objectManager->get(Request::class);
65+
//imitation run script from parent root directory
66+
$request->setServer(new Parameters(['DOCUMENT_ROOT' => $requestPath]));
67+
68+
$sitemapUrl = $this->model->getSitemapUrl('/', 'sitemap.xml');
69+
70+
$this->assertEquals($baseUrl.'sitemap.xml', $sitemapUrl);
71+
}
72+
}

0 commit comments

Comments
 (0)