Skip to content

Commit 37930d2

Browse files
committed
Merge branch 'ACP2E-3532' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-Tier4-VK-2025-01-09
2 parents 13e276c + c1c1485 commit 37930d2

File tree

2 files changed

+84
-106
lines changed

2 files changed

+84
-106
lines changed

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

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
6-
76
namespace Magento\Sitemap\Model;
87

98
use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot;
@@ -12,14 +11,13 @@
1211
use Magento\Framework\DataObject;
1312
use Magento\Framework\Exception\LocalizedException;
1413
use Magento\Framework\Filesystem;
14+
use Magento\Framework\Filesystem\Directory\WriteInterface;
1515
use Magento\Framework\UrlInterface;
1616
use Magento\Robots\Model\Config\Value;
1717
use Magento\Sitemap\Model\ItemProvider\ItemProviderInterface;
1818
use Magento\Sitemap\Model\ResourceModel\Sitemap as SitemapResource;
1919

2020
/**
21-
* Sitemap model.
22-
*
2321
* @method string getSitemapType()
2422
* @method \Magento\Sitemap\Model\Sitemap setSitemapType(string $value)
2523
* @method string getSitemapFilename()
@@ -199,13 +197,11 @@ class Sitemap extends \Magento\Framework\Model\AbstractModel implements \Magento
199197
private $filesystem;
200198

201199
/**
202-
* @var DocumentRoot
200+
* @var WriteInterface
203201
*/
204-
private $documentRoot;
202+
private $tmpDirectory;
205203

206204
/**
207-
* Initialize dependencies.
208-
*
209205
* @param \Magento\Framework\Model\Context $context
210206
* @param \Magento\Framework\Registry $registry
211207
* @param \Magento\Framework\Escaper $escaper
@@ -252,6 +248,7 @@ public function __construct(
252248
$this->_sitemapData = $sitemapData;
253249
$this->filesystem = $filesystem;
254250
$this->_directory = $filesystem->getDirectoryWrite(DirectoryList::PUB);
251+
$this->tmpDirectory = $filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
255252
$this->_categoryFactory = $categoryFactory;
256253
$this->_productFactory = $productFactory;
257254
$this->_cmsFactory = $cmsFactory;
@@ -482,11 +479,9 @@ public function generateXml()
482479

483480
if ($this->_sitemapIncrement == 1) {
484481
// In case when only one increment file was created use it as default sitemap
485-
$sitemapPath = $this->getSitemapPath() !== null ? rtrim($this->getSitemapPath(), '/') : '';
486-
$path = $sitemapPath . '/' . $this->_getCurrentSitemapFilename($this->_sitemapIncrement);
487-
$destination = $sitemapPath . '/' . $this->getSitemapFilename();
488-
489-
$this->_directory->renameFile($path, $destination);
482+
$path = $this->getFilePath($this->_getCurrentSitemapFilename($this->_sitemapIncrement));
483+
$destination = $this->getFilePath($this->getSitemapFilename());
484+
$this->tmpDirectory->renameFile($path, $destination, $this->_directory);
490485
} else {
491486
// Otherwise create index file with list of generated sitemaps
492487
$this->_createSitemapIndex();
@@ -507,10 +502,15 @@ protected function _createSitemapIndex()
507502
{
508503
$this->_createSitemap($this->getSitemapFilename(), self::TYPE_INDEX);
509504
for ($i = 1; $i <= $this->_sitemapIncrement; $i++) {
510-
$xml = $this->_getSitemapIndexRow($this->_getCurrentSitemapFilename($i), $this->_getCurrentDateTime());
505+
$fileName = $this->_getCurrentSitemapFilename($i);
506+
$path = $this->getFilePath($fileName);
507+
$this->tmpDirectory->renameFile($path, $path, $this->_directory);
508+
$xml = $this->_getSitemapIndexRow($fileName, $this->_getCurrentDateTime());
511509
$this->_writeSitemapRow($xml);
512510
}
513511
$this->_finalizeSitemap(self::TYPE_INDEX);
512+
$path = $this->getFilePath($this->getSitemapFilename());
513+
$this->tmpDirectory->renameFile($path, $path, $this->_directory);
514514
}
515515

516516
/**
@@ -638,9 +638,8 @@ protected function _createSitemap($fileName = null, $type = self::TYPE_URL)
638638
$this->_sitemapIncrement++;
639639
$fileName = $this->_getCurrentSitemapFilename($this->_sitemapIncrement);
640640
}
641-
642-
$path = ($this->getSitemapPath() !== null ? rtrim($this->getSitemapPath(), '/') : '') . '/' . $fileName;
643-
$this->_stream = $this->_directory->openFile($path);
641+
$path = $this->getFilePath($fileName);
642+
$this->_stream = $this->tmpDirectory->openFile($path);
644643

645644
$fileHeader = sprintf($this->_tags[$type][self::OPEN_TAG_KEY], $type);
646645
$this->_stream->write($fileHeader);
@@ -688,6 +687,20 @@ protected function _getCurrentSitemapFilename($index)
688687
. '-' . $this->getStoreId() . '-' . $index . '.xml';
689688
}
690689

690+
/**
691+
* Get path to sitemap file
692+
*
693+
* @param string $fileName
694+
* @return string
695+
*/
696+
private function getFilePath(string $fileName): string
697+
{
698+
$path = $this->getSitemapPath() !== null ? rtrim($this->getSitemapPath(), '/') : '';
699+
$path .= '/' . $fileName;
700+
701+
return $path;
702+
}
703+
691704
/**
692705
* Get base dir
693706
*
@@ -815,6 +828,7 @@ public function getSitemapUrl($sitemapPath, $sitemapFileName)
815828
* @return bool
816829
* @deprecated 100.1.5 Because the robots.txt file is not generated anymore,
817830
* this method is not needed and will be removed in major release.
831+
* @see no alternatives
818832
*/
819833
protected function _isEnabledSubmissionRobots()
820834
{
@@ -829,6 +843,7 @@ protected function _isEnabledSubmissionRobots()
829843
* @return void
830844
* @deprecated 100.1.5 Because the robots.txt file is not generated anymore,
831845
* this method is not needed and will be removed in major release.
846+
* @see no alternatives
832847
*/
833848
protected function _addSitemapToRobotsTxt($sitemapFileName)
834849
{

app/code/Magento/Sitemap/Test/Unit/Model/SitemapTest.php

Lines changed: 51 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2012 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\Sitemap\Test\Unit\Model;
99

10+
use Magento\Framework\App\Filesystem\DirectoryList;
1011
use Magento\Framework\App\Request\Http;
1112
use Magento\Framework\DataObject;
1213
use Magento\Framework\Escaper;
@@ -20,19 +21,15 @@
2021
use Magento\Sitemap\Helper\Data;
2122
use Magento\Sitemap\Model\ItemProvider\ConfigReaderInterface;
2223
use Magento\Sitemap\Model\ItemProvider\ItemProviderInterface;
23-
use Magento\Sitemap\Model\ResourceModel\Catalog\Category;
2424
use Magento\Sitemap\Model\ResourceModel\Catalog\CategoryFactory;
25-
use Magento\Sitemap\Model\ResourceModel\Catalog\Product;
2625
use Magento\Sitemap\Model\ResourceModel\Catalog\ProductFactory;
27-
use Magento\Sitemap\Model\ResourceModel\Cms\Page;
2826
use Magento\Sitemap\Model\ResourceModel\Cms\PageFactory;
2927
use Magento\Sitemap\Model\ResourceModel\Sitemap as SitemapResource;
3028
use Magento\Sitemap\Model\Sitemap;
3129
use Magento\Sitemap\Model\SitemapConfigReaderInterface;
3230
use Magento\Sitemap\Model\SitemapItem;
3331
use Magento\Store\Model\Store;
3432
use Magento\Store\Model\StoreManagerInterface;
35-
use PHPUnit\Framework\Assert;
3633
use PHPUnit\Framework\MockObject\MockObject;
3734
use PHPUnit\Framework\TestCase;
3835

@@ -42,44 +39,39 @@
4239
class SitemapTest extends TestCase
4340
{
4441
/**
45-
* @var Data
46-
*/
47-
private $helperMockSitemap;
48-
49-
/**
50-
* @var SitemapResource
42+
* @var SitemapResource|MockObject
5143
*/
5244
private $resourceMock;
5345

5446
/**
55-
* @var Category
47+
* @var Filesystem|MockObject
5648
*/
57-
private $sitemapCategoryMock;
49+
private $filesystemMock;
5850

5951
/**
60-
* @var Product
52+
* @var DirectoryWrite|MockObject
6153
*/
62-
private $sitemapProductMock;
54+
private $directoryMock;
6355

6456
/**
65-
* @var Page
57+
* @var Write|MockObject
6658
*/
67-
private $sitemapCmsPageMock;
59+
private $fileMock;
6860

6961
/**
70-
* @var Filesystem
62+
* @var DirectoryWrite|MockObject
7163
*/
72-
private $filesystemMock;
64+
private $tmpDirectoryMock;
7365

7466
/**
75-
* @var DirectoryWrite
67+
* @var Write|MockObject
7668
*/
77-
private $directoryMock;
69+
private $tmpFileMock;
7870

7971
/**
80-
* @var Write
72+
* @var DirectoryWrite|MockObject
8173
*/
82-
private $fileMock;
74+
private $mediaDirectoryMock;
8375

8476
/**
8577
* @var StoreManagerInterface|MockObject
@@ -96,10 +88,6 @@ class SitemapTest extends TestCase
9688
*/
9789
private $configReaderMock;
9890

99-
/**
100-
* @var Http|MockObject
101-
*/
102-
private $request;
10391
/**
10492
* @var Store|MockObject
10593
*/
@@ -110,20 +98,6 @@ class SitemapTest extends TestCase
11098
*/
11199
protected function setUp(): void
112100
{
113-
$this->sitemapCategoryMock = $this->getMockBuilder(Category::class)
114-
->disableOriginalConstructor()
115-
->getMock();
116-
$this->sitemapProductMock = $this->getMockBuilder(Product::class)
117-
->disableOriginalConstructor()
118-
->getMock();
119-
$this->sitemapCmsPageMock = $this->getMockBuilder(Page::class)
120-
->disableOriginalConstructor()
121-
->getMock();
122-
123-
$this->helperMockSitemap = $this->getMockBuilder(Data::class)
124-
->disableOriginalConstructor()
125-
->getMock();
126-
127101
$resourceMethods = [
128102
'_construct',
129103
'beginTransaction',
@@ -133,35 +107,37 @@ protected function setUp(): void
133107
'commit',
134108
'__wakeup',
135109
];
136-
137110
$this->resourceMock = $this->getMockBuilder(SitemapResource::class)
138111
->onlyMethods($resourceMethods)
139112
->disableOriginalConstructor()
140113
->getMock();
141-
142114
$this->resourceMock->method('addCommitCallback')
143115
->willReturnSelf();
144116

145117
$this->fileMock = $this->createMock(Write::class);
146-
147118
$this->directoryMock = $this->createMock(DirectoryWrite::class);
148-
149119
$this->directoryMock->method('openFile')
150120
->willReturn($this->fileMock);
151-
152-
$this->filesystemMock = $this->getMockBuilder(Filesystem::class)
153-
->onlyMethods(['getDirectoryWrite'])
154-
->disableOriginalConstructor()
155-
->getMock();
156-
121+
$this->tmpFileMock = $this->createMock(Write::class);
122+
$this->tmpDirectoryMock = $this->createMock(DirectoryWrite::class);
123+
$this->tmpDirectoryMock->method('openFile')
124+
->willReturn($this->tmpFileMock);
125+
$this->mediaDirectoryMock = $this->createMock(DirectoryWrite::class);
126+
$this->filesystemMock = $this->createMock(Filesystem::class);
157127
$this->filesystemMock->method('getDirectoryWrite')
158-
->willReturn($this->directoryMock);
128+
->willReturnMap(
129+
[
130+
[DirectoryList::PUB, $this->directoryMock],
131+
[DirectoryList::SYS_TMP, $this->tmpDirectoryMock],
132+
[DirectoryList::MEDIA, $this->mediaDirectoryMock],
133+
]
134+
);
135+
136+
$this->configReaderMock = $this->createMock(SitemapConfigReaderInterface::class);
137+
$this->itemProviderMock = $this->createMock(ItemProviderInterface::class);
159138

160-
$this->configReaderMock = $this->getMockForAbstractClass(SitemapConfigReaderInterface::class);
161-
$this->itemProviderMock = $this->getMockForAbstractClass(ItemProviderInterface::class);
162-
$this->request = $this->createMock(Http::class);
163139
$this->store = $this->createPartialMock(Store::class, ['isFrontUrlSecure', 'getBaseUrl']);
164-
$this->storeManagerMock = $this->getMockForAbstractClass(StoreManagerInterface::class);
140+
$this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
165141
$this->storeManagerMock->method('getStore')
166142
->willReturn($this->store);
167143
}
@@ -423,35 +399,28 @@ protected function prepareSitemapModelMock(
423399
}
424400
$actualData[$currentFile] .= $str;
425401
};
426-
427402
// Check that all expected lines were written
428-
$this->fileMock->expects(
429-
$this->exactly($expectedWrites)
430-
)->method(
431-
'write'
432-
)->willReturnCallback(
433-
$streamWriteCallback
434-
);
403+
$this->tmpFileMock->expects($this->exactly($expectedWrites))
404+
->method('write')
405+
->willReturnCallback($streamWriteCallback);
435406

436407
$checkFileCallback = function ($file) use (&$currentFile) {
437408
$currentFile = $file;
438-
};// Check that all expected file descriptors were created
439-
$this->directoryMock->expects($this->exactly(count($expectedFile)))->method('openFile')
409+
};
410+
// Check that all expected file descriptors were created
411+
$this->tmpDirectoryMock->expects($this->exactly(count($expectedFile)))
412+
->method('openFile')
440413
->willReturnCallback($checkFileCallback);
441414

442415
// Check that all file descriptors were closed
443-
$this->fileMock->expects($this->exactly(count($expectedFile)))
416+
$this->tmpFileMock->expects($this->exactly(count($expectedFile)))
444417
->method('close');
445418

446419
if (count($expectedFile) == 1) {
447-
$this->directoryMock->expects($this->once())
420+
$this->tmpDirectoryMock->expects($this->once())
448421
->method('renameFile')
449-
->willReturnCallback(
450-
function ($from, $to) {
451-
Assert::assertEquals('/sitemap-1-1.xml', $from);
452-
Assert::assertEquals('/sitemap.xml', $to);
453-
}
454-
);
422+
->with('/sitemap-1-1.xml', '/sitemap.xml', $this->directoryMock)
423+
->willReturn(true);
455424
}
456425

457426
// Check robots txt
@@ -591,17 +560,11 @@ protected function getModelMock($mockBeforeSave = false)
591560
*/
592561
private function getModelConstructorArgs()
593562
{
594-
$categoryFactory = $this->getMockBuilder(CategoryFactory::class)
595-
->disableOriginalConstructor()
596-
->getMock();
597-
598-
$productFactory = $this->getMockBuilder(ProductFactory::class)
599-
->disableOriginalConstructor()
600-
->getMock();
601-
602-
$cmsFactory = $this->getMockBuilder(PageFactory::class)
603-
->disableOriginalConstructor()
604-
->getMock();
563+
$categoryFactory = $this->createMock(CategoryFactory::class);
564+
$productFactory = $this->createMock(ProductFactory::class);
565+
$cmsFactory = $this->createMock(PageFactory::class);
566+
$helperMockSitemap = $this->createMock(Data::class);
567+
$request = $this->createMock(Http::class);
605568

606569
$objectManager = new ObjectManager($this);
607570
$escaper = $objectManager->getObject(Escaper::class);
@@ -614,12 +577,12 @@ private function getModelConstructorArgs()
614577
'productFactory' => $productFactory,
615578
'cmsFactory' => $cmsFactory,
616579
'storeManager' => $this->storeManagerMock,
617-
'sitemapData' => $this->helperMockSitemap,
580+
'sitemapData' => $helperMockSitemap,
618581
'filesystem' => $this->filesystemMock,
619582
'itemProvider' => $this->itemProviderMock,
620583
'configReader' => $this->configReaderMock,
621584
'escaper' => $escaper,
622-
'request' => $this->request,
585+
'request' => $request,
623586
]
624587
);
625588
$constructArguments['resource'] = null;

0 commit comments

Comments
 (0)