Wordpress/tests/phpunit/includes/testcase-xml.php
Pascal Birchler b58973554d Sitemaps: Add XML sitemaps functionality to WordPress.
While web crawlers are able to discover pages from links within the site and from other sites, XML sitemaps supplement this approach by allowing crawlers to quickly and comprehensively identify all URLs included in the sitemap and learn other signals about those URLs using the associated metadata.

See https://make.wordpress.org/core/2020/06/10/merge-announcement-extensible-core-sitemaps/ for more details.

This feature exposes the sitemap index via `/wp-sitemap.xml` and exposes a variety of new filters and hooks for developers to modify the behavior. Users can disable sitemaps completely by turning off search engine visibility in WordPress admin.

This change also introduces a new `esc_xml()` function to escape strings for output in XML, as well as XML support to `wp_kses_normalize_entities()`.

Props Adrian McShane, afragen, adamsilverstein, casiepa, flixos90, garrett-eclipse, joemcgill, kburgoine, kraftbj, milana_cap, pacifika, pbiron, pfefferle, Ruxandra Gradina, swissspidy, szepeviktor, tangrufus, tweetythierry.
Fixes #50117.
See #3670. See #19998.


git-svn-id: https://develop.svn.wordpress.org/trunk@48072 602fd350-edb4-49c9-b593-d223f7449a82
2020-06-17 15:22:49 +00:00

93 lines
4.2 KiB
PHP

<?php
abstract class WP_Test_XML_TestCase extends WP_UnitTestCase {
/**
* Load XML from a string.
*
* @param string $xml
* @param int $options Bitwise OR of the {@link https://www.php.net/manual/en/libxml.constants.php libxml option constants}.
* Default is 0.
* @return DOMDocument The DOMDocument object loaded from the XML.
*/
public function loadXML( $xml, $options = 0 ) {
// Suppress PHP warnings generated by DOMDocument::loadXML(), which would cause
// PHPUnit to incorrectly report an error instead of a just a failure.
$internal = libxml_use_internal_errors( true );
libxml_clear_errors();
$xml_dom = new DOMDocument();
$xml_dom->loadXML( $xml, $options );
$libxml_last_error = libxml_get_last_error();
$this->assertFalse(
isset( $libxml_last_error->message ),
isset( $libxml_last_error->message ) ? sprintf( 'Non-well-formed XML: %s.', $libxml_last_error->message ) : ''
);
// Restore default error handler.
libxml_use_internal_errors( $internal );
libxml_clear_errors();
return $xml_dom;
}
/**
* Normalize an XML document to make comparing two documents easier.
*
* @param string $xml
* @param int $options Bitwise OR of the {@link https://www.php.net/manual/en/libxml.constants.php libxml option constants}.
* Default is 0.
* @return string The normalized form of `$xml`.
*/
public function normalizeXML( $xml, $options = 0 ) {
if ( ! class_exists( 'XSLTProcessor' ) ) {
$this->markTestSkipped( 'This test requires the XSL extension.' );
}
static $xslt_proc;
if ( ! $xslt_proc ) {
$xslt_proc = new XSLTProcessor();
$xslt_proc->importStyleSheet( simplexml_load_file( __DIR__ . '/normalize-xml.xsl' ) );
}
return $xslt_proc->transformToXML( $this->loadXML( $xml, $options ) );
}
/**
* Reports an error identified by `$message` if the namespace normalized form of the XML document in `$actualXml`
* is equal to the namespace normalized form of the XML document in `$expectedXml`.
*
* This is similar to {@link https://phpunit.de/manual/6.5/en/appendixes.assertions.html#appendixes.assertions.assertXmlStringEqualsXmlString assertXmlStringEqualsXmlString()}
* except that differences in namespace prefixes are normalized away, such that given
* `$actualXml = "<root xmlns='urn:wordpress.org'><child/></root>";` and
* `$expectedXml = "<ns0:root xmlns:ns0='urn:wordpress.org'><ns0:child></ns0:root>";`
* then `$this->assertXMLEquals( $expectedXml, $actualXml )` will succeed.
*
* @param string $expectedXml
* @param string $actualXml
* @param string $message Optional. Message to display when the assertion fails.
*/
public function assertXMLEquals( $expectedXml, $actualXml, $message = '' ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
$this->assertEquals( $this->normalizeXML( $expectedXml ), $this->normalizeXML( $actualXml ), $message ); //phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
}
/**
* Reports an error identified by `$message` if the namespace normalized form of the XML document in `$actualXml`
* is not equal to the namespace normalized form of the XML document in `$expectedXml`.
*
* This is similar to {@link https://phpunit.de/manual/6.5/en/appendixes.assertions.html#appendixes.assertions.assertXmlStringEqualsXmlString assertXmlStringNotEqualsXmlString()}
* except that differences in namespace prefixes are normalized away, such that given
* `$actualXml = "<root xmlns='urn:wordpress.org'><child></root>";` and
* `$expectedXml = "<ns0:root xmlns:ns0='urn:wordpress.org'><ns0:child/></ns0:root>";`
* then `$this->assertXMLNotEquals( $expectedXml, $actualXml )` will fail.
*
* @param string $expectedXml
* @param string $actualXml
* @param string $message Optional. Message to display when the assertion fails.
*/
public function assertXMLNotEquals( $expectedXml, $actualXml, $message = '' ) { //phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
$this->assertNotEquals( $this->normalizeXML( $expectedXml ), $this->normalizeXML( $actualXml ), $message ); //phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
}
}