Skip to content

Commit b9bb8a8

Browse files
committed
MC-34617: [Magento Cloud] - Sitemap Error - Encoding Characters
1 parent f824b9e commit b9bb8a8

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,22 +571,36 @@ protected function _getSitemapRow($url, $lastmod = null, $changefreq = null, $pr
571571
foreach ($images->getCollection() as $image) {
572572
$row .= '<image:image>';
573573
$row .= '<image:loc>' . $this->_escaper->escapeUrl($image->getUrl()) . '</image:loc>';
574-
$row .= '<image:title>' . $this->_escaper->escapeHtml($images->getTitle()) . '</image:title>';
574+
$row .= '<image:title>' . $this->escapeXmlText($images->getTitle()) . '</image:title>';
575575
if ($image->getCaption()) {
576-
$row .= '<image:caption>' . $this->_escaper->escapeHtml($image->getCaption()) . '</image:caption>';
576+
$row .= '<image:caption>' . $this->escapeXmlText($image->getCaption()) . '</image:caption>';
577577
}
578578
$row .= '</image:image>';
579579
}
580580
// Add PageMap image for Google web search
581581
$row .= '<PageMap xmlns="http://www.google.com/schemas/sitemap-pagemap/1.0"><DataObject type="thumbnail">';
582-
$row .= '<Attribute name="name" value="' . $this->_escaper->escapeHtml($images->getTitle()) . '"/>';
582+
$row .= '<Attribute name="name" value="' . $this->_escaper->escapeHtmlAttr($images->getTitle()) . '"/>';
583583
$row .= '<Attribute name="src" value="' . $this->_escaper->escapeUrl($images->getThumbnail()) . '"/>';
584584
$row .= '</DataObject></PageMap>';
585585
}
586586

587587
return '<url>' . $row . '</url>';
588588
}
589589

590+
/**
591+
* Escape string for XML context.
592+
*
593+
* @param string $text
594+
* @return string
595+
*/
596+
private function escapeXmlText(string $text): string
597+
{
598+
$doc = new \DOMDocument('1.0', 'UTF-8');
599+
$fragment = $doc->createDocumentFragment();
600+
$fragment->appendChild($doc->createTextNode($text));
601+
return $doc->saveXML($fragment);
602+
}
603+
590604
/**
591605
* Get sitemap index row
592606
*

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Magento\Framework\Filesystem\Directory\Write as DirectoryWrite;
1616
use Magento\Framework\Filesystem\File\Write;
1717
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
18+
use Magento\Framework\Translate\InlineInterface;
19+
use Magento\Framework\ZendEscaper;
1820
use Magento\Sitemap\Helper\Data;
1921
use Magento\Sitemap\Model\ItemProvider\ConfigReaderInterface;
2022
use Magento\Sitemap\Model\ItemProvider\ItemProviderInterface;
@@ -551,7 +553,7 @@ protected function getModelMock($mockBeforeSave = false)
551553
new DataObject(
552554
[
553555
'url' => $storeBaseMediaUrl . 'i/m/image1.png',
554-
'caption' => 'caption & > title < "'
556+
'caption' => 'Copyright © caption &trade; & > title < "'
555557
]
556558
),
557559
new DataObject(
@@ -610,7 +612,8 @@ private function getModelConstructorArgs()
610612

611613
$objectManager = new ObjectManager($this);
612614
$escaper = $objectManager->getObject(Escaper::class);
613-
615+
$this->setPrivatePropertyValue($escaper, 'escaper', $objectManager->getObject(ZendEscaper::class));
616+
$this->setPrivatePropertyValue($escaper, 'translateInline', $this->createMock(InlineInterface::class));
614617
$constructArguments = $objectManager->getConstructArguments(
615618
Sitemap::class,
616619
[
@@ -796,4 +799,21 @@ public function getDocumentRootFromBaseDirUrlDataProvider(): array
796799
],
797800
];
798801
}
802+
803+
/**
804+
* @param mixed $object
805+
* @param string $attributeName
806+
* @param string $value
807+
*/
808+
private function setPrivatePropertyValue($object, $attributeName, $value): void
809+
{
810+
$attribute = new \ReflectionProperty($object, $attributeName);
811+
if ($attribute->isPublic()) {
812+
$object->$attributeName = $value;
813+
} else {
814+
$attribute->setAccessible(true);
815+
$attribute->setValue($object, $value);
816+
$attribute->setAccessible(false);
817+
}
818+
}
799819
}

app/code/Magento/Sitemap/Test/Unit/Model/_files/sitemap-1-4.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<image:image>
1616
<image:loc>http://store.com/pub/media/catalog/product/cache/c9e0b0ef589f3508e5ba515cde53c5ff/i/m/image1.png</image:loc>
1717
<image:title>Product &amp; &gt; title &lt; &quot;</image:title>
18-
<image:caption>caption &amp; &gt; title &lt; &quot;</image:caption>
18+
<image:caption>Copyright &#xa9; caption &amp;trade; &amp; &gt; title &lt; &quot;</image:caption>
1919
</image:image>
2020
<image:image>
2121
<image:loc>http://store.com/pub/media/catalog/product/cache/c9e0b0ef589f3508e5ba515cde53c5ff/i/m/image_no_caption.png</image:loc>

app/code/Magento/Sitemap/Test/Unit/Model/_files/sitemap-single.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<image:image>
3434
<image:loc>http://store.com/pub/media/catalog/product/cache/c9e0b0ef589f3508e5ba515cde53c5ff/i/m/image1.png</image:loc>
3535
<image:title>Product &amp; &gt; title &lt; &quot;</image:title>
36-
<image:caption>caption &amp; &gt; title &lt; &quot;</image:caption>
36+
<image:caption>Copyright &#xa9; caption &amp;trade; &amp; &gt; title &lt; &quot;</image:caption>
3737
</image:image>
3838
<image:image>
3939
<image:loc>http://store.com/pub/media/catalog/product/cache/c9e0b0ef589f3508e5ba515cde53c5ff/i/m/image_no_caption.png</image:loc>

0 commit comments

Comments
 (0)