17 use Magento\Sitemap\Model\ResourceModel\Catalog\CategoryFactory;
19 use Magento\Sitemap\Model\ResourceModel\Catalog\ProductFactory;
21 use Magento\Sitemap\Model\ResourceModel\Cms\PageFactory;
37 private $helperMockSitemap;
42 private $resourceMock;
47 private $sitemapCategoryMock;
52 private $sitemapProductMock;
57 private $sitemapCmsPageMock;
62 private $filesystemMock;
67 private $directoryMock;
77 private $storeManagerMock;
82 private $itemProviderMock;
87 private $configReaderMock;
94 $this->sitemapCategoryMock = $this->getMockBuilder(Category::class)
95 ->disableOriginalConstructor()
97 $this->sitemapProductMock = $this->getMockBuilder(Product::class)
98 ->disableOriginalConstructor()
100 $this->sitemapCmsPageMock = $this->getMockBuilder(Page::class)
101 ->disableOriginalConstructor()
104 $this->helperMockSitemap = $this->getMockBuilder(Data::class)
105 ->disableOriginalConstructor()
118 $this->resourceMock = $this->getMockBuilder(SitemapResource::class)
119 ->setMethods($resourceMethods)
120 ->disableOriginalConstructor()
123 $this->resourceMock->expects($this->any())
124 ->method(
'addCommitCallback')
127 $this->fileMock = $this->createMock(Write::class);
129 $this->directoryMock = $this->createMock(DirectoryWrite::class);
131 $this->directoryMock->expects($this->any())
133 ->willReturn($this->fileMock);
135 $this->filesystemMock = $this->getMockBuilder(Filesystem::class)
136 ->setMethods([
'getDirectoryWrite'])
137 ->disableOriginalConstructor()
140 $this->filesystemMock->expects($this->any())
141 ->method(
'getDirectoryWrite')
142 ->willReturn($this->directoryMock);
144 $this->configReaderMock = $this->getMockForAbstractClass(SitemapConfigReaderInterface::class);
145 $this->itemProviderMock = $this->getMockForAbstractClass(ItemProviderInterface::class);
156 $model = $this->getModelMock();
157 $model->setSitemapPath(
'../');
169 $this->directoryMock->expects($this->once())
173 $model = $this->getModelMock();
185 $this->directoryMock->expects($this->once())
189 $this->directoryMock->expects($this->once())
190 ->method(
'isWritable')
193 $model = $this->getModelMock();
208 $this->directoryMock->expects($this->once())
212 $this->directoryMock->expects($this->once())
213 ->method(
'isWritable')
216 $model = $this->getModelMock();
217 $model->setSitemapFilename(
'*sitemap?.xml');
233 $expectedSingleFile = [
'/sitemap-1-1.xml' =>
__DIR__ .
'/_files/sitemap-single.xml'];
235 $expectedMultiFile = [
236 '/sitemap-1-1.xml' =>
__DIR__ .
'/_files/sitemap-1-1.xml',
237 '/sitemap-1-2.xml' =>
__DIR__ .
'/_files/sitemap-1-2.xml',
238 '/sitemap-1-3.xml' =>
__DIR__ .
'/_files/sitemap-1-3.xml',
239 '/sitemap-1-4.xml' =>
__DIR__ .
'/_files/sitemap-1-4.xml',
240 '/sitemap.xml' =>
__DIR__ .
'/_files/sitemap-index.xml',
244 [50000, 10485760, $expectedSingleFile, 6],
245 [1, 10485760, $expectedMultiFile, 18],
246 [50000, 264, $expectedMultiFile, 18],
259 public function testGenerateXml($maxLines, $maxFileSize, $expectedFile, $expectedWrites)
272 $this->assertCount(count($expectedFile), $actualData,
'Number of generated files is incorrect');
273 foreach ($expectedFile as $expectedFileName => $expectedFilePath) {
274 $this->assertArrayHasKey(
277 sprintf(
'File %s was not generated', $expectedFileName)
279 $this->assertXmlStringEqualsXmlFile($expectedFilePath, $actualData[$expectedFileName]);
291 $expectedSingleFile = [
'/sitemap-1-1.xml' =>
__DIR__ .
'/_files/sitemap-single.xml'];
293 $expectedMultiFile = [
294 '/sitemap-1-1.xml' =>
__DIR__ .
'/_files/sitemap-1-1.xml',
295 '/sitemap-1-2.xml' =>
__DIR__ .
'/_files/sitemap-1-2.xml',
296 '/sitemap-1-3.xml' =>
__DIR__ .
'/_files/sitemap-1-3.xml',
297 '/sitemap-1-4.xml' =>
__DIR__ .
'/_files/sitemap-1-4.xml',
298 '/sitemap.xml' =>
__DIR__ .
'/_files/sitemap-index.xml',
309 'robotsFinish' =>
'Sitemap: http://store.com/sitemap.xml',
319 'robotsStart' =>
"User-agent: *",
320 'robotsFinish' =>
"User-agent: *" . PHP_EOL .
'Sitemap: http://store.com/sitemap.xml',
330 'robotsStart' =>
"User-agent: *\r\n",
331 'robotsFinish' =>
"User-agent: *\r\n\r\nSitemap: http://store.com/sitemap.xml",
341 'robotsStart' =>
"User-agent: *\n",
342 'robotsFinish' =>
"User-agent: *\n\nSitemap: http://store.com/sitemap.xml",
351 [
'robotsStart' =>
'',
'robotsFinish' =>
'',
'pushToRobots' => 0]
403 $streamWriteCallback =
function ($str) use (&$actualData, &$currentFile) {
404 if (!array_key_exists($currentFile, $actualData)) {
405 $actualData[$currentFile] =
'';
407 $actualData[$currentFile] .= $str;
411 $this->fileMock->expects(
412 $this->exactly($expectedWrites)
416 $this->returnCallback($streamWriteCallback)
419 $checkFileCallback =
function ($file) use (&$currentFile) {
420 $currentFile = $file;
422 $this->directoryMock->expects($this->exactly(count($expectedFile)))->method(
'openFile')
423 ->willReturnCallback($checkFileCallback);
426 $this->fileMock->expects($this->exactly(count($expectedFile)))
429 if (count($expectedFile) == 1) {
430 $this->directoryMock->expects($this->once())
431 ->method(
'renameFile')
432 ->willReturnCallback(
function ($from, $to) {
433 \PHPUnit\Framework\Assert::assertEquals(
'/sitemap-1-1.xml', $from);
434 \PHPUnit\Framework\Assert::assertEquals(
'/sitemap.xml', $to);
440 if (isset($robotsInfo[
'robotsStart'])) {
441 $robotsStart = $robotsInfo[
'robotsStart'];
443 $robotsFinish =
'Sitemap: http://store.com/sitemap.xml';
444 if (isset($robotsInfo[
'robotsFinish'])) {
445 $robotsFinish = $robotsInfo[
'robotsFinish'];
447 $this->directoryMock->expects($this->any())
449 ->willReturn($robotsStart);
451 $this->directoryMock->expects($this->any())
452 ->method(
'writeFile')
454 $this->equalTo(
'robots.txt'),
455 $this->equalTo($robotsFinish)
460 if (isset($robotsInfo[
'pushToRobots'])) {
461 $pushToRobots = (int)$robotsInfo[
'pushToRobots'];
463 $this->configReaderMock->expects($this->any())
464 ->method(
'getMaximumLinesNumber')
465 ->willReturn($maxLines);
467 $this->configReaderMock->expects($this->any())
468 ->method(
'getMaximumFileSize')
469 ->willReturn($maxFileSize);
471 $this->configReaderMock->expects($this->any())
472 ->method(
'getEnableSubmissionRobots')
473 ->willReturn($pushToRobots);
475 $model = $this->getModelMock(
true);
477 $storeMock = $this->getMockBuilder(Store::class)
478 ->setMethods([
'isFrontUrlSecure',
'getBaseUrl'])
479 ->disableOriginalConstructor()
482 $storeMock->expects($this->atLeastOnce())
483 ->method(
'isFrontUrlSecure')
486 $storeMock->expects($this->atLeastOnce())
487 ->method(
'getBaseUrl')
488 ->with($this->isType(
'string'),
false)
489 ->willReturn(
'http://store.com/');
491 $this->storeManagerMock->expects($this->atLeastOnce())
494 ->willReturn($storeMock);
505 protected function getModelMock($mockBeforeSave =
false)
513 '_getCurrentDateTime',
514 '_getCategoryItemsCollection',
515 '_getProductItemsCollection',
516 '_getPageItemsCollection',
519 if ($mockBeforeSave) {
523 $storeBaseMediaUrl =
'http://store.com/pub/media/catalog/product/cache/c9e0b0ef589f3508e5ba515cde53c5ff/';
525 $this->itemProviderMock->expects($this->any())
528 new SitemapItem(
'category.html',
'1.0',
'daily',
'2012-12-21 00:00:00'),
529 new SitemapItem(
'/category/sub-category.html',
'1.0',
'daily',
'2012-12-21 00:00:00'),
530 new SitemapItem(
'product.html',
'0.5',
'monthly',
'0000-00-00 00:00:00'),
535 '2012-12-21 00:00:00',
540 'url' => $storeBaseMediaUrl .
'i/m/image1.png',
541 'caption' =>
'caption & > title < "' 545 [
'url' => $storeBaseMediaUrl .
'i/m/image_no_caption.png',
'caption' =>
null]
548 'thumbnail' => $storeBaseMediaUrl .
't/h/thumbnail.jpg',
549 'title' =>
'Product & > title < "',
555 $model = $this->getMockBuilder(Sitemap::class)
557 ->setConstructorArgs($this->getModelConstructorArgs())
560 $model->expects($this->any())
561 ->method(
'_getResource')
562 ->willReturn($this->resourceMock);
564 $model->expects($this->any())
565 ->method(
'_getCurrentDateTime')
566 ->willReturn(
'2012-12-21T00:00:00-08:00');
568 $model->expects($this->any())
569 ->method(
'_getDocumentRoot')
570 ->willReturn(
'/project');
572 $model->setSitemapFilename(
'sitemap.xml');
574 $model->setSitemapPath(
'/');
582 private function getModelConstructorArgs()
584 $categoryFactory = $this->getMockBuilder(CategoryFactory::class)
585 ->disableOriginalConstructor()
589 ->disableOriginalConstructor()
592 $cmsFactory = $this->getMockBuilder(PageFactory::class)
593 ->disableOriginalConstructor()
596 $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
597 ->setMethods([
'getStore'])
598 ->getMockForAbstractClass();
604 'categoryFactory' => $categoryFactory,
606 'cmsFactory' => $cmsFactory,
607 'storeManager' => $this->storeManagerMock,
608 'sitemapData' => $this->helperMockSitemap,
609 'filesystem' => $this->filesystemMock,
610 'itemProvider' => $this->itemProviderMock,
611 'configReader' => $this->configReaderMock,
614 $constructArguments[
'resource'] =
null;
615 return $constructArguments;
629 public function testGetSitemapUrl($storeBaseUrl, $documentRoot,
$baseDir, $sitemapPath, $sitemapFileName,
$result)
632 $model = $this->getMockBuilder(Sitemap::class)
641 ->setConstructorArgs($this->getModelConstructorArgs())
644 $model->expects($this->any())
645 ->method(
'_getStoreBaseUrl')
646 ->willReturn($storeBaseUrl);
648 $model->expects($this->any())
649 ->method(
'_getDocumentRoot')
650 ->willReturn($documentRoot);
652 $model->expects($this->any())
653 ->method(
'_getBaseDir')
656 $this->assertEquals(
$result,
$model->getSitemapUrl($sitemapPath, $sitemapFileName));
674 'http://store.com/sitemap.xml',
677 'http://store.com/store2',
682 'http://store.com/sitemaps/store2/sitemap.xml' 685 'http://store.com/builds/regression/ee/',
687 '/opt/builds/regression/ee',
690 'http://store.com/builds/regression/ee/sitemap.xml' 693 'http://store.com/store2',
695 'c:\\http\\mage2\\store2',
698 'http://store.com/store2/sitemaps/store2/sitemap.xml' 701 'http://store2.store.com',
706 'http://store2.store.com/sitemaps/store2/sitemap.xml' 714 'http://store.com/sitemap.xml' 717 'http://store.com/store2',
719 '/var/www/store/store2/',
722 'http://store.com/store2/sitemaps/store2/sitemap.xml'
static robotsDataProvider()
prepareSitemapModelMock(&$actualData, $maxLines, $maxFileSize, $expectedFile, $expectedWrites, $robotsInfo)
testGenerateXml($maxLines, $maxFileSize, $expectedFile, $expectedWrites)
testFilenameInvalidChars()
defined('TESTS_BP')||define('TESTS_BP' __DIR__
static siteUrlDataProvider()
static sitemapDataProvider()
testAddSitemapToRobotsTxt($maxLines, $maxFileSize, $expectedFile, $expectedWrites, $robotsInfo)