public function XmlSitemapWriterTest::testWriteElement in XML sitemap 8
Same name and namespace in other branches
- 2.x tests/src/Kernel/XmlSitemapWriterTest.php \Drupal\Tests\xmlsitemap\Kernel\XmlSitemapWriterTest::testWriteElement()
Tests the writeElement() method.
@covers ::writeElement @covers ::formatXmlElements @covers ::toString
File
- tests/
src/ Kernel/ XmlSitemapWriterTest.php, line 44
Class
Namespace
Drupal\Tests\xmlsitemap\KernelCode
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);
}