Skip to content

Commit 4e3e5bd

Browse files
MC-21860: Partial sitemaps have wrong urls in sitemap index
1 parent 121fadd commit 4e3e5bd

File tree

2 files changed

+85
-4
lines changed

2 files changed

+85
-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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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\Config\Value;
12+
use Magento\Framework\App\Filesystem\DirectoryList;
13+
use Magento\Framework\Filesystem;
14+
use Magento\Framework\ObjectManagerInterface;
15+
use Magento\Sitemap\Model\Sitemap;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use Magento\TestFramework\Request;
18+
use Zend\Stdlib\Parameters;
19+
use PHPUnit\Framework\TestCase;
20+
21+
class SitemapTest extends TestCase
22+
{
23+
/**
24+
* @var Sitemap
25+
*/
26+
private $model;
27+
28+
/**
29+
* @var ObjectManagerInterface
30+
*/
31+
private $objectManager;
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
protected function setUp()
37+
{
38+
$this->objectManager = Bootstrap::getObjectManager();
39+
$this->model = $this->objectManager->get(Sitemap::class);
40+
}
41+
42+
/**
43+
* Test get sitemap URL from parent root directory path
44+
*
45+
* @return void
46+
*/
47+
public function testGetSitemapUrlFromParentRootDirectoryPath(): void
48+
{
49+
/** @var Value $configValue */
50+
$configValue = $this->objectManager->get(Value::class);
51+
$configValue->load('web/unsecure/base_url', 'path');
52+
$baseUrl = $configValue->getValue() ?: 'http://localhost/';
53+
54+
/** @var Filesystem $filesystem */
55+
$filesystem = $this->objectManager->create(Filesystem::class);
56+
$rootDir = $filesystem->getDirectoryRead(DirectoryList::ROOT)
57+
->getAbsolutePath();
58+
$requestPath = dirname($rootDir);
59+
60+
/** @var Request $request */
61+
$request = $this->objectManager->get(Request::class);
62+
//imitation run script from parent root directory
63+
$request->setServer(new Parameters(['DOCUMENT_ROOT' => $requestPath]));
64+
65+
$sitemapUrl = $this->model->getSitemapUrl('/', 'sitemap.xml');
66+
67+
$this->assertEquals($baseUrl.'sitemap.xml', $sitemapUrl);
68+
}
69+
}

0 commit comments

Comments
 (0)