XmlSitemapWriterTest.php in XML sitemap 8
File
tests/src/Kernel/XmlSitemapWriterTest.php
View source
<?php
namespace Drupal\Tests\xmlsitemap\Kernel;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\xmlsitemap\Entity\XmlSitemap;
use Drupal\xmlsitemap\XmlSitemapWriter;
class XmlSitemapWriterTest extends KernelTestBase {
public function setUp() {
parent::setUp();
$this->sitemap = XmlSitemap::create();
}
public function testInvalidPage() {
$this
->expectExceptionObject(new \InvalidArgumentException("Invalid XML sitemap page invalid."));
new XmlSitemapWriter($this->sitemap, 'invalid');
}
public function testWriteElement() {
$writer = new XmlSitemapWriter($this->sitemap, '1');
$writer
->openMemory();
$writer
->writeElement('url', [
'item1' => 'value1',
[
'key' => 'item2',
'value' => '<value2>',
],
[
'key' => 'item3',
'value' => [
'subkey' => 'subvalue',
],
'attributes' => [
'attr1key' => 'attr1value',
'attr2key' => '<attr2value>',
],
],
]);
$output = $writer
->outputMemory();
$expected = '<url><item1>value1</item1><item2><value2></item2><item3 attr1key="attr1value" attr2key="<attr2value>"><subkey>subvalue</subkey></item3></url>' . PHP_EOL;
$this
->assertEquals($expected, $output);
$writer
->writeElement('url', [
'loc' => 'https://www.example.com/test',
'image:image' => [
'image:loc' => Url::fromUri('https://www.example.com/test.jpg'),
'image:title' => new TranslatableMarkup('The image title'),
'image:caption' => "'The image & its \"caption.\"'",
],
]);
$output = $writer
->outputMemory();
$expected = '<url><loc>https://www.example.com/test</loc><image:image><image:loc>https://www.example.com/test.jpg</image:loc><image:title>The image title</image:title><image:caption>'The image & its "caption."'</image:caption></image:image></url>' . PHP_EOL;
$this
->assertSame($expected, $output);
}
}