mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 12:20:22 +00:00
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
This commit is contained in:
@@ -153,12 +153,15 @@ require __DIR__ . '/testcase-rest-post-type-controller.php';
|
||||
require __DIR__ . '/testcase-xmlrpc.php';
|
||||
require __DIR__ . '/testcase-ajax.php';
|
||||
require __DIR__ . '/testcase-canonical.php';
|
||||
require __DIR__ . '/testcase-xml.php';
|
||||
require __DIR__ . '/exceptions.php';
|
||||
require __DIR__ . '/utils.php';
|
||||
require __DIR__ . '/spy-rest-server.php';
|
||||
require __DIR__ . '/class-wp-rest-test-search-handler.php';
|
||||
require __DIR__ . '/class-wp-rest-test-configurable-controller.php';
|
||||
require __DIR__ . '/class-wp-fake-block-type.php';
|
||||
require __DIR__ . '/class-wp-sitemaps-test-provider.php';
|
||||
require __DIR__ . '/class-wp-sitemaps-empty-test-provider.php';
|
||||
|
||||
/**
|
||||
* A class to handle additional command line arguments passed to the script.
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WP_Sitemaps_Empty_Test_Provider.
|
||||
*
|
||||
* Provides test data for additional registered providers.
|
||||
*/
|
||||
class WP_Sitemaps_Empty_Test_Provider extends WP_Sitemaps_Provider {
|
||||
/**
|
||||
* WP_Sitemaps_Empty_Test_Provider constructor.
|
||||
*
|
||||
* @param string $object_type Optional. Object type name to use. Default 'test'.
|
||||
*/
|
||||
public function __construct( $object_type = 'test' ) {
|
||||
$this->object_type = $object_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a URL list for a sitemap.
|
||||
*
|
||||
* @param int $page_num Page of results.
|
||||
* @param string $object_subtype Optional. Object subtype name. Default empty.
|
||||
* @return array List of URLs for a sitemap.
|
||||
*/
|
||||
public function get_url_list( $page_num, $object_subtype = '' ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Query for determining the number of pages.
|
||||
*
|
||||
* @param string $object_subtype Optional. Object subtype. Default empty.
|
||||
* @return int Total number of pages.
|
||||
*/
|
||||
public function get_max_num_pages( $object_subtype = '' ) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WP_Sitemaps_Test_Provider.
|
||||
*
|
||||
* Provides test data for additional registered providers.
|
||||
*/
|
||||
class WP_Sitemaps_Test_Provider extends WP_Sitemaps_Provider {
|
||||
/**
|
||||
* WP_Sitemaps_Posts constructor.
|
||||
*
|
||||
* @param string $object_type Optional. Object type name to use. Default 'test'.
|
||||
*/
|
||||
public function __construct( $object_type = 'test' ) {
|
||||
$this->object_type = $object_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the public post types, which excludes nav_items and similar types.
|
||||
* Attachments are also excluded. This includes custom post types with public = true
|
||||
*
|
||||
* @return array Map of object subtype objects (WP_Post_Type) keyed by their name.
|
||||
*/
|
||||
public function get_object_subtypes() {
|
||||
return array(
|
||||
'type-1' => (object) array( 'name' => 'type-1' ),
|
||||
'type-2' => (object) array( 'name' => 'type-2' ),
|
||||
'type-3' => (object) array( 'name' => 'type-3' ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a URL list for a sitemap.
|
||||
*
|
||||
* @param int $page_num Page of results.
|
||||
* @param string $object_subtype Optional. Object subtype name. Default empty.
|
||||
* @return array List of URLs for a sitemap.
|
||||
*/
|
||||
public function get_url_list( $page_num, $object_subtype = '' ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Query for determining the number of pages.
|
||||
*
|
||||
* @param string $object_subtype Optional. Object subtype. Default empty.
|
||||
* @return int Total number of pages.
|
||||
*/
|
||||
public function get_max_num_pages( $object_subtype = '' ) {
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?xml version='1.0' encoding='UTF-8' ?>
|
||||
<!--
|
||||
Normalize an XML document to make it easier to compare whether 2 documents will
|
||||
be seen as "equal" to an XML processor.
|
||||
|
||||
The normalization is similiar, in spirit, to {@link https://www.w3.org/TR/xml-c14n11/ Canonical XML},
|
||||
but without some aspects of C14N that make the kinds of assertions we need difficult.
|
||||
|
||||
For example, the following XML documents will be interpreted the same by an XML processor,
|
||||
even though a string comparison of them would show differences:
|
||||
|
||||
<root xmlns='urn:example'>
|
||||
<ns0:child xmlns:ns0='urn:another-example'>this is a test</ns0:child>
|
||||
</root>
|
||||
|
||||
<ns0:root xmlns:ns0='urn:example'>
|
||||
<child xmlns='urn:another-example'>this is a test</child>
|
||||
</ns0:root>
|
||||
-->
|
||||
<xsl:transform
|
||||
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
|
||||
version='1.0'
|
||||
>
|
||||
|
||||
<!--
|
||||
Output UTF-8 XML, no indendation and all CDATA sections replaced with their character content.
|
||||
-->
|
||||
<xsl:output
|
||||
method='xml'
|
||||
indent='no'
|
||||
cdata-section-elements=''
|
||||
encoding='UTF-8' />
|
||||
|
||||
<!--
|
||||
Strip insignificant white space.
|
||||
-->
|
||||
<xsl:strip-space elements='*' />
|
||||
|
||||
<!--
|
||||
Noramlize elements by not relying on the prefix used in the input document
|
||||
and ordering attributes first by namespace-uri and then by local-name.
|
||||
-->
|
||||
<xsl:template match='*' priority='10'>
|
||||
<xsl:element name='{local-name()}' namespace='{namespace-uri()}'>
|
||||
<xsl:apply-templates select='@*'>
|
||||
<xsl:sort select='namespace-uri()' />
|
||||
<xsl:sort select='local-name()' />
|
||||
</xsl:apply-templates>
|
||||
|
||||
<xsl:apply-templates select='node()' />
|
||||
</xsl:element>
|
||||
</xsl:template>
|
||||
|
||||
<!--
|
||||
Noramlize attributes by not relying on the prefix used in the input document.
|
||||
-->
|
||||
<xsl:template match='@*'>
|
||||
<xsl:attribute name='{local-name()}' namespace='{namespace-uri()}'>
|
||||
<xsl:value-of select='.' />
|
||||
</xsl:attribute>
|
||||
</xsl:template>
|
||||
|
||||
<!--
|
||||
Strip comments.
|
||||
-->
|
||||
<xsl:template match='comment()' priority='10' />
|
||||
|
||||
<!--
|
||||
Pass all other nodes through unchanged.
|
||||
-->
|
||||
<xsl:template match='node()'>
|
||||
<xsl:copy>
|
||||
<xsl:apply-templates select='node()' />
|
||||
</xsl:copy>
|
||||
</xsl:template>
|
||||
</xsl:transform>
|
||||
@@ -0,0 +1,92 @@
|
||||
<?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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group canonical
|
||||
* @group rewrite
|
||||
* @group query
|
||||
* @group sitemaps
|
||||
*/
|
||||
class Tests_Canonical_Sitemaps extends WP_Canonical_UnitTestCase {
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$wp_sitemaps = new WP_Sitemaps();
|
||||
$wp_sitemaps->init();
|
||||
}
|
||||
|
||||
public function test_remove_trailing_slashes_for_sitemap_index_requests() {
|
||||
$this->set_permalink_structure( '/%postname%/' );
|
||||
$this->assertCanonical( '/wp-sitemap.xml', '/wp-sitemap.xml' );
|
||||
$this->assertCanonical( '/wp-sitemap.xml/', '/wp-sitemap.xml' );
|
||||
}
|
||||
|
||||
public function test_remove_trailing_slashes_for_sitemap_index_stylesheet_requests() {
|
||||
$this->set_permalink_structure( '/%postname%/' );
|
||||
$this->assertCanonical( '/wp-sitemap-index.xsl', '/wp-sitemap-index.xsl' );
|
||||
$this->assertCanonical( '/wp-sitemap-index.xsl/', '/wp-sitemap-index.xsl' );
|
||||
}
|
||||
|
||||
public function test_remove_trailing_slashes_for_sitemap_requests() {
|
||||
$this->set_permalink_structure( '/%postname%/' );
|
||||
$this->assertCanonical( '/wp-sitemap-posts-post-1.xml', '/wp-sitemap-posts-post-1.xml' );
|
||||
$this->assertCanonical( '/wp-sitemap-posts-post-1.xml/', '/wp-sitemap-posts-post-1.xml' );
|
||||
$this->assertCanonical( '/wp-sitemap-users-1.xml', '/wp-sitemap-users-1.xml' );
|
||||
$this->assertCanonical( '/wp-sitemap-users-1.xml/', '/wp-sitemap-users-1.xml' );
|
||||
}
|
||||
|
||||
public function test_remove_trailing_slashes_for_sitemap_stylesheet_requests() {
|
||||
$this->set_permalink_structure( '/%postname%/' );
|
||||
$this->assertCanonical( '/wp-sitemap.xsl', '/wp-sitemap.xsl' );
|
||||
$this->assertCanonical( '/wp-sitemap.xsl/', '/wp-sitemap.xsl' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group formatting
|
||||
*/
|
||||
class Tests_Formatting_EscXml extends WP_UnitTestCase {
|
||||
/**
|
||||
* Test basic escaping
|
||||
*
|
||||
* @dataProvider _test_esc_xml_basics_dataprovider
|
||||
*
|
||||
* @param string $source The source string to be escaped.
|
||||
* @param string $expected The expected escaped value of `$source`.
|
||||
*/
|
||||
public function test_esc_xml_basics( $source, $expected ) {
|
||||
$actual = esc_xml( $source );
|
||||
$this->assertEquals( $expected, $actual );
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for `test_esc_xml_basics()`.
|
||||
*
|
||||
* @return array {
|
||||
* @type string $source The source string to be escaped.
|
||||
* @type string $expected The expected escaped value of `$source`.
|
||||
* }
|
||||
*/
|
||||
public function _test_esc_xml_basics_dataprovider() {
|
||||
return array(
|
||||
// Simple string.
|
||||
array(
|
||||
'The quick brown fox.',
|
||||
'The quick brown fox.',
|
||||
),
|
||||
// URL with &.
|
||||
array(
|
||||
'http://localhost/trunk/wp-login.php?action=logout&_wpnonce=cd57d75985',
|
||||
'http://localhost/trunk/wp-login.php?action=logout&_wpnonce=cd57d75985',
|
||||
),
|
||||
// SQL query w/ single quotes.
|
||||
array(
|
||||
"SELECT meta_key, meta_value FROM wp_trunk_sitemeta WHERE meta_key IN ('site_name', 'siteurl', 'active_sitewide_plugins', '_site_transient_timeout_theme_roots', '_site_transient_theme_roots', 'site_admins', 'can_compress_scripts', 'global_terms_enabled') AND site_id = 1",
|
||||
'SELECT meta_key, meta_value FROM wp_trunk_sitemeta WHERE meta_key IN ('site_name', 'siteurl', 'active_sitewide_plugins', '_site_transient_timeout_theme_roots', '_site_transient_theme_roots', 'site_admins', 'can_compress_scripts', 'global_terms_enabled') AND site_id = 1',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function test_escapes_ampersands() {
|
||||
$source = 'penn & teller & at&t';
|
||||
$expected = 'penn & teller & at&t';
|
||||
$actual = esc_xml( $source );
|
||||
$this->assertEquals( $expected, $actual );
|
||||
}
|
||||
|
||||
public function test_escapes_greater_and_less_than() {
|
||||
$source = 'this > that < that <randomhtml />';
|
||||
$expected = 'this > that < that <randomhtml />';
|
||||
$actual = esc_xml( $source );
|
||||
$this->assertEquals( $expected, $actual );
|
||||
}
|
||||
|
||||
public function test_escapes_html_named_entities() {
|
||||
$source = 'this & is a … followed by › and more and a &nonexistent; entity';
|
||||
$expected = 'this & is a … followed by › and more and a &nonexistent; entity';
|
||||
$actual = esc_xml( $source );
|
||||
$this->assertEquals( $expected, $actual );
|
||||
}
|
||||
|
||||
public function test_ignores_existing_entities() {
|
||||
$source = '& £ " &';
|
||||
// note that _wp_specialchars() strips leading 0's from numeric character references.
|
||||
$expected = '& £ " &';
|
||||
$actual = esc_xml( $source );
|
||||
$this->assertEquals( $expected, $actual );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that CDATA Sections are not escaped.
|
||||
*
|
||||
* @dataProvider _test_ignores_cdata_sections_dataprovider
|
||||
*
|
||||
* @param string $source The source string to be escaped.
|
||||
* @param string $expected The expected escaped value of `$source`.
|
||||
*/
|
||||
public function test_ignores_cdata_sections( $source, $expected ) {
|
||||
$actual = esc_xml( $source );
|
||||
$this->assertEquals( $expected, $actual );
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for `test_ignores_cdata_sections()`.
|
||||
*
|
||||
* @return array {
|
||||
* @type string $source The source string to be escaped.
|
||||
* @type string $expected The expected escaped value of `$source`.
|
||||
* }
|
||||
*/
|
||||
public function _test_ignores_cdata_sections_dataprovider() {
|
||||
return array(
|
||||
// basic CDATA Section containing chars that would otherwise be escaped if not in a CDATA Section
|
||||
// not to mention the CDATA Section markup itself :-)
|
||||
// $source contains embedded newlines to test that the regex that ignores CDATA Sections
|
||||
// correctly handles that case.
|
||||
array(
|
||||
"This is\na<![CDATA[test of\nthe <emergency>]]>\nbroadcast system",
|
||||
"This is\na<![CDATA[test of\nthe <emergency>]]>\nbroadcast system",
|
||||
),
|
||||
// string with chars that should be escaped as well as a CDATA Section that should be not be.
|
||||
array(
|
||||
'This is … a <![CDATA[test of the <emergency>]]> broadcast <system />',
|
||||
'This is … a <![CDATA[test of the <emergency>]]> broadcast <system />',
|
||||
),
|
||||
// Same as above, but with the CDATA Section at the start of the string.
|
||||
array(
|
||||
'<![CDATA[test of the <emergency>]]> This is … a broadcast <system />',
|
||||
'<![CDATA[test of the <emergency>]]> This is … a broadcast <system />',
|
||||
),
|
||||
// Same as above, but with the CDATA Section at the end of the string.
|
||||
array(
|
||||
'This is … a broadcast <system /><![CDATA[test of the <emergency>]]>',
|
||||
'This is … a broadcast <system /><![CDATA[test of the <emergency>]]>',
|
||||
),
|
||||
// Multiple CDATA Sections.
|
||||
array(
|
||||
'This is … a <![CDATA[test of the <emergency>]]> &broadcast; <![CDATA[<system />]]>',
|
||||
'This is … a <![CDATA[test of the <emergency>]]> &broadcast; <![CDATA[<system />]]>',
|
||||
),
|
||||
// Ensure that ']]>' that does not mark the end of a CDATA Section is escaped.
|
||||
array(
|
||||
'<![CDATA[<&]]>]]>',
|
||||
'<![CDATA[<&]]>]]>',
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group sitemaps
|
||||
*/
|
||||
class Test_Sitemaps_Functions extends WP_UnitTestCase {
|
||||
/**
|
||||
* Test getting the correct number of URLs for a sitemap.
|
||||
*/
|
||||
public function test_wp_sitemaps_get_max_urls() {
|
||||
// Apply a filter to test filterable values.
|
||||
add_filter( 'wp_sitemaps_max_urls', array( $this, '_filter_max_url_value' ), 10, 2 );
|
||||
|
||||
$expected_posts = wp_sitemaps_get_max_urls( 'post' );
|
||||
$expected_taxonomies = wp_sitemaps_get_max_urls( 'term' );
|
||||
$expected_users = wp_sitemaps_get_max_urls( 'user' );
|
||||
|
||||
$this->assertEquals( $expected_posts, 300, 'Can not confirm max URL number for posts.' );
|
||||
$this->assertEquals( $expected_taxonomies, 50, 'Can not confirm max URL number for taxonomies.' );
|
||||
$this->assertEquals( $expected_users, 1, 'Can not confirm max URL number for users.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function for testing the `sitemaps_max_urls` filter.
|
||||
*
|
||||
* @param int $max_urls The maximum number of URLs included in a sitemap. Default 2000.
|
||||
* @param string $type Optional. The type of sitemap to be filtered. Default empty.
|
||||
* @return int The maximum number of URLs.
|
||||
*/
|
||||
public function _filter_max_url_value( $max_urls, $type ) {
|
||||
switch ( $type ) {
|
||||
case 'post':
|
||||
return 300;
|
||||
case 'term':
|
||||
return 50;
|
||||
case 'user':
|
||||
return 1;
|
||||
default:
|
||||
return $max_urls;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wp_get_sitemaps default functionality
|
||||
*/
|
||||
public function test_wp_get_sitemaps() {
|
||||
$sitemaps = wp_get_sitemaps();
|
||||
|
||||
$expected = array(
|
||||
'posts' => 'WP_Sitemaps_Posts',
|
||||
'taxonomies' => 'WP_Sitemaps_Taxonomies',
|
||||
'users' => 'WP_Sitemaps_Users',
|
||||
);
|
||||
|
||||
$this->assertEquals( array_keys( $expected ), array_keys( $sitemaps ), 'Unable to confirm default sitemap types are registered.' );
|
||||
|
||||
foreach ( $expected as $name => $provider ) {
|
||||
$this->assertTrue( is_a( $sitemaps[ $name ], $provider ), "Default $name sitemap is not a $provider object." );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group sitemaps
|
||||
*/
|
||||
class Test_WP_Sitemaps_Index extends WP_UnitTestCase {
|
||||
public function test_get_sitemap_list() {
|
||||
$registry = new WP_Sitemaps_Registry();
|
||||
|
||||
/*
|
||||
* The test provider has 3 subtypes.
|
||||
* Each subtype has 4 pages with results.
|
||||
* There are 2 providers registered.
|
||||
* Hence, 3*4*2=24.
|
||||
*/
|
||||
$registry->add_sitemap( 'foo', new WP_Sitemaps_Test_Provider( 'foo' ) );
|
||||
$registry->add_sitemap( 'bar', new WP_Sitemaps_Test_Provider( 'bar' ) );
|
||||
|
||||
$sitemap_index = new WP_Sitemaps_Index( $registry );
|
||||
$this->assertCount( 24, $sitemap_index->get_sitemap_list() );
|
||||
}
|
||||
|
||||
public function test_get_sitemap_list_no_entries() {
|
||||
$registry = new WP_Sitemaps_Registry();
|
||||
|
||||
$registry->add_sitemap( 'foo', new WP_Sitemaps_Empty_Test_Provider( 'foo' ) );
|
||||
|
||||
$sitemap_index = new WP_Sitemaps_Index( $registry );
|
||||
$this->assertCount( 0, $sitemap_index->get_sitemap_list() );
|
||||
}
|
||||
|
||||
public function test_get_index_url() {
|
||||
$sitemap_index = new WP_Sitemaps_Index( new WP_Sitemaps_Registry() );
|
||||
$index_url = $sitemap_index->get_index_url();
|
||||
|
||||
$this->assertStringEndsWith( '/?sitemap=index', $index_url );
|
||||
}
|
||||
|
||||
public function test_get_index_url_pretty_permalinks() {
|
||||
// Set permalinks for testing.
|
||||
$this->set_permalink_structure( '/%year%/%postname%/' );
|
||||
|
||||
$sitemap_index = new WP_Sitemaps_Index( new WP_Sitemaps_Registry() );
|
||||
$index_url = $sitemap_index->get_index_url();
|
||||
|
||||
// Clean up permalinks.
|
||||
$this->set_permalink_structure();
|
||||
|
||||
$this->assertStringEndsWith( '/wp-sitemap.xml', $index_url );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group sitemaps
|
||||
*/
|
||||
class Test_WP_Sitemaps_Posts extends WP_UnitTestCase {
|
||||
/**
|
||||
* Test ability to filter object subtypes.
|
||||
*/
|
||||
public function test_filter_sitemaps_post_types() {
|
||||
$posts_provider = new WP_Sitemaps_Posts();
|
||||
|
||||
// Return an empty array to show that the list of subtypes is filterable.
|
||||
add_filter( 'wp_sitemaps_post_types', '__return_empty_array' );
|
||||
$subtypes = $posts_provider->get_object_subtypes();
|
||||
|
||||
$this->assertEquals( array(), $subtypes, 'Could not filter posts subtypes.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test `wp_sitemaps_posts_show_on_front_entry` filter.
|
||||
*/
|
||||
public function test_posts_show_on_front_entry() {
|
||||
$posts_provider = new WP_Sitemaps_Posts();
|
||||
update_option( 'show_on_front', 'page' );
|
||||
|
||||
add_filter( 'wp_sitemaps_posts_show_on_front_entry', array( $this, '_show_on_front_entry' ) );
|
||||
|
||||
$url_list = $posts_provider->get_url_list( 1, 'page' );
|
||||
|
||||
$this->assertEquals( array(), $url_list );
|
||||
|
||||
update_option( 'show_on_front', 'posts' );
|
||||
|
||||
$url_list = $posts_provider->get_url_list( 1, 'page' );
|
||||
$sitemap_entry = array_shift( $url_list );
|
||||
|
||||
$this->assertTrue( isset( $sitemap_entry['lastmod'] ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for 'wp_sitemaps_posts_show_on_front_entry' filter.
|
||||
*/
|
||||
public function _show_on_front_entry( $sitemap_entry ) {
|
||||
$sitemap_entry['lastmod'] = wp_date( DATE_W3C, time() );
|
||||
|
||||
return $sitemap_entry;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group sitemaps
|
||||
*/
|
||||
class Test_WP_Sitemaps_Registry extends WP_UnitTestCase {
|
||||
public function test_add_sitemap() {
|
||||
$provider = new WP_Sitemaps_Test_Provider();
|
||||
$registry = new WP_Sitemaps_Registry();
|
||||
|
||||
$actual = $registry->add_sitemap( 'foo', $provider );
|
||||
$sitemaps = $registry->get_sitemaps();
|
||||
|
||||
$this->assertTrue( $actual );
|
||||
$this->assertCount( 1, $sitemaps );
|
||||
$this->assertSame( $sitemaps['foo'], $provider, 'Can not confirm sitemap registration is working.' );
|
||||
}
|
||||
|
||||
public function test_add_sitemap_prevent_duplicates() {
|
||||
$provider1 = new WP_Sitemaps_Test_Provider();
|
||||
$provider2 = new WP_Sitemaps_Test_Provider();
|
||||
$registry = new WP_Sitemaps_Registry();
|
||||
|
||||
$actual1 = $registry->add_sitemap( 'foo', $provider1 );
|
||||
$actual2 = $registry->add_sitemap( 'foo', $provider2 );
|
||||
$sitemaps = $registry->get_sitemaps();
|
||||
|
||||
$this->assertTrue( $actual1 );
|
||||
$this->assertFalse( $actual2 );
|
||||
$this->assertCount( 1, $sitemaps );
|
||||
$this->assertSame( $sitemaps['foo'], $provider1, 'Can not confirm sitemap registration is working.' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,283 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group sitemaps
|
||||
*/
|
||||
class Test_WP_Sitemaps_Renderer extends WP_Test_XML_TestCase {
|
||||
public function test_get_sitemap_stylesheet_url() {
|
||||
$sitemap_renderer = new WP_Sitemaps_Renderer();
|
||||
$stylesheet_url = $sitemap_renderer->get_sitemap_stylesheet_url();
|
||||
|
||||
$this->assertStringEndsWith( '/?sitemap-stylesheet=sitemap', $stylesheet_url );
|
||||
}
|
||||
|
||||
public function test_get_sitemap_stylesheet_url_pretty_permalinks() {
|
||||
// Set permalinks for testing.
|
||||
$this->set_permalink_structure( '/%year%/%postname%/' );
|
||||
|
||||
$sitemap_renderer = new WP_Sitemaps_Renderer();
|
||||
$stylesheet_url = $sitemap_renderer->get_sitemap_stylesheet_url();
|
||||
|
||||
// Clean up permalinks.
|
||||
$this->set_permalink_structure();
|
||||
|
||||
$this->assertStringEndsWith( '/wp-sitemap.xsl', $stylesheet_url );
|
||||
}
|
||||
|
||||
public function test_get_sitemap_index_stylesheet_url() {
|
||||
$sitemap_renderer = new WP_Sitemaps_Renderer();
|
||||
$stylesheet_url = $sitemap_renderer->get_sitemap_index_stylesheet_url();
|
||||
|
||||
$this->assertStringEndsWith( '/?sitemap-stylesheet=index', $stylesheet_url );
|
||||
}
|
||||
|
||||
public function test_get_sitemap_index_stylesheet_url_pretty_permalinks() {
|
||||
// Set permalinks for testing.
|
||||
$this->set_permalink_structure( '/%year%/%postname%/' );
|
||||
|
||||
$sitemap_renderer = new WP_Sitemaps_Renderer();
|
||||
$stylesheet_url = $sitemap_renderer->get_sitemap_index_stylesheet_url();
|
||||
|
||||
// Clean up permalinks.
|
||||
$this->set_permalink_structure();
|
||||
|
||||
$this->assertStringEndsWith( '/wp-sitemap-index.xsl', $stylesheet_url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test XML output for the sitemap index renderer.
|
||||
*/
|
||||
public function test_get_sitemap_index_xml() {
|
||||
$entries = array(
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-post-1.xml',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-page-1.xml',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-taxonomies-category-1.xml',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-taxonomies-post_tag-1.xml',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-users-1.xml',
|
||||
),
|
||||
);
|
||||
|
||||
$renderer = new WP_Sitemaps_Renderer();
|
||||
|
||||
$actual = $renderer->get_sitemap_index_xml( $entries );
|
||||
$expected = '<?xml version="1.0" encoding="UTF-8"?>' .
|
||||
'<?xml-stylesheet type="text/xsl" href="http://' . WP_TESTS_DOMAIN . '/?sitemap-stylesheet=index" ?>' .
|
||||
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' .
|
||||
'<sitemap><loc>http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-post-1.xml</loc></sitemap>' .
|
||||
'<sitemap><loc>http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-page-1.xml</loc></sitemap>' .
|
||||
'<sitemap><loc>http://' . WP_TESTS_DOMAIN . '/wp-sitemap-taxonomies-category-1.xml</loc></sitemap>' .
|
||||
'<sitemap><loc>http://' . WP_TESTS_DOMAIN . '/wp-sitemap-taxonomies-post_tag-1.xml</loc></sitemap>' .
|
||||
'<sitemap><loc>http://' . WP_TESTS_DOMAIN . '/wp-sitemap-users-1.xml</loc></sitemap>' .
|
||||
'</sitemapindex>';
|
||||
|
||||
$this->assertXMLEquals( $expected, $actual, 'Sitemap index markup incorrect.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test XML output for the sitemap index renderer with lastmod attributes.
|
||||
*/
|
||||
public function test_get_sitemap_index_xml_with_lastmod() {
|
||||
$entries = array(
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-post-1.xml',
|
||||
'lastmod' => '2005-01-01',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-page-1.xml',
|
||||
'lastmod' => '2005-01-01',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-taxonomies-category-1.xml',
|
||||
'lastmod' => '2005-01-01',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-taxonomies-post_tag-1.xml',
|
||||
'lastmod' => '2005-01-01',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-users-1.xml',
|
||||
'lastmod' => '2005-01-01',
|
||||
),
|
||||
);
|
||||
|
||||
$renderer = new WP_Sitemaps_Renderer();
|
||||
|
||||
$actual = $renderer->get_sitemap_index_xml( $entries );
|
||||
$expected = '<?xml version="1.0" encoding="UTF-8"?>' .
|
||||
'<?xml-stylesheet type="text/xsl" href="http://' . WP_TESTS_DOMAIN . '/?sitemap-stylesheet=index" ?>' .
|
||||
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' .
|
||||
'<sitemap><loc>http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-post-1.xml</loc><lastmod>2005-01-01</lastmod></sitemap>' .
|
||||
'<sitemap><loc>http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-page-1.xml</loc><lastmod>2005-01-01</lastmod></sitemap>' .
|
||||
'<sitemap><loc>http://' . WP_TESTS_DOMAIN . '/wp-sitemap-taxonomies-category-1.xml</loc><lastmod>2005-01-01</lastmod></sitemap>' .
|
||||
'<sitemap><loc>http://' . WP_TESTS_DOMAIN . '/wp-sitemap-taxonomies-post_tag-1.xml</loc><lastmod>2005-01-01</lastmod></sitemap>' .
|
||||
'<sitemap><loc>http://' . WP_TESTS_DOMAIN . '/wp-sitemap-users-1.xml</loc><lastmod>2005-01-01</lastmod></sitemap>' .
|
||||
'</sitemapindex>';
|
||||
|
||||
$this->assertXMLEquals( $expected, $actual, 'Sitemap index markup incorrect.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that all children of Q{http://www.sitemaps.org/schemas/sitemap/0.9}sitemap in the
|
||||
* rendered index XML are defined in the Sitemaps spec (i.e., loc, lastmod).
|
||||
*
|
||||
* Note that when a means of adding elements in extension namespaces is settled on,
|
||||
* this test will need to be updated accordingly.
|
||||
*
|
||||
* @expectedIncorrectUsage WP_Sitemaps_Renderer::get_sitemap_index_xml
|
||||
*/
|
||||
public function test_get_sitemap_index_xml_extra_elements() {
|
||||
$url_list = array(
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-post-1.xml',
|
||||
'unknown' => 'this is a test',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-page-1.xml',
|
||||
'unknown' => 'that was a test',
|
||||
),
|
||||
);
|
||||
|
||||
$renderer = new WP_Sitemaps_Renderer();
|
||||
|
||||
$xml_dom = $this->loadXML( $renderer->get_sitemap_index_xml( $url_list ) );
|
||||
$xpath = new DOMXPath( $xml_dom );
|
||||
$xpath->registerNamespace( 'sitemap', 'http://www.sitemaps.org/schemas/sitemap/0.9' );
|
||||
|
||||
$this->assertEquals(
|
||||
0,
|
||||
$xpath->evaluate( "count( /sitemap:sitemapindex/sitemap:sitemap/*[ namespace-uri() != 'http://www.sitemaps.org/schemas/sitemap/0.9' or not( local-name() = 'loc' or local-name() = 'lastmod' ) ] )" ),
|
||||
'Invalid child of "sitemap:sitemap" in rendered index XML.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test XML output for the sitemap index renderer when stylesheet is disabled.
|
||||
*/
|
||||
public function test_get_sitemap_index_xml_without_stylesheet() {
|
||||
$entries = array(
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-post-1.xml',
|
||||
),
|
||||
);
|
||||
|
||||
add_filter( 'wp_sitemaps_stylesheet_index_url', '__return_false' );
|
||||
|
||||
$renderer = new WP_Sitemaps_Renderer();
|
||||
|
||||
$xml_dom = $this->loadXML( $renderer->get_sitemap_index_xml( $entries ) );
|
||||
$xpath = new DOMXPath( $xml_dom );
|
||||
|
||||
$this->assertSame(
|
||||
0,
|
||||
$xpath->query( '//processing-instruction( "xml-stylesheet" )' )->length,
|
||||
'Sitemap index incorrectly contains the xml-stylesheet processing instruction.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test XML output for the sitemap page renderer.
|
||||
*/
|
||||
public function test_get_sitemap_xml() {
|
||||
$url_list = array(
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/2019/10/post-1',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/2019/10/post-2',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/2019/10/post-3',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/2019/10/post-4',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/2019/10/post-5',
|
||||
),
|
||||
);
|
||||
|
||||
$renderer = new WP_Sitemaps_Renderer();
|
||||
|
||||
$actual = $renderer->get_sitemap_xml( $url_list );
|
||||
$expected = '<?xml version="1.0" encoding="UTF-8"?>' .
|
||||
'<?xml-stylesheet type="text/xsl" href="http://' . WP_TESTS_DOMAIN . '/?sitemap-stylesheet=sitemap" ?>' .
|
||||
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' .
|
||||
'<url><loc>http://' . WP_TESTS_DOMAIN . '/2019/10/post-1</loc></url>' .
|
||||
'<url><loc>http://' . WP_TESTS_DOMAIN . '/2019/10/post-2</loc></url>' .
|
||||
'<url><loc>http://' . WP_TESTS_DOMAIN . '/2019/10/post-3</loc></url>' .
|
||||
'<url><loc>http://' . WP_TESTS_DOMAIN . '/2019/10/post-4</loc></url>' .
|
||||
'<url><loc>http://' . WP_TESTS_DOMAIN . '/2019/10/post-5</loc></url>' .
|
||||
'</urlset>';
|
||||
|
||||
$this->assertXMLEquals( $expected, $actual, 'Sitemap page markup incorrect.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test XML output for the sitemap page renderer when stylesheet is disabled.
|
||||
*/
|
||||
public function test_get_sitemap_xml_without_stylesheet() {
|
||||
$url_list = array(
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/2019/10/post-1',
|
||||
),
|
||||
);
|
||||
|
||||
add_filter( 'wp_sitemaps_stylesheet_url', '__return_false' );
|
||||
|
||||
$renderer = new WP_Sitemaps_Renderer();
|
||||
|
||||
$xml_dom = $this->loadXML( $renderer->get_sitemap_xml( $url_list ) );
|
||||
$xpath = new DOMXPath( $xml_dom );
|
||||
|
||||
$this->assertSame(
|
||||
0,
|
||||
$xpath->query( '//processing-instruction( "xml-stylesheet" )' )->length,
|
||||
'Sitemap incorrectly contains the xml-stylesheet processing instruction.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that all children of Q{http://www.sitemaps.org/schemas/sitemap/0.9}url in the
|
||||
* rendered sitemap XML are defined in the Sitemaps spec (i.e., loc, lastmod, changefreq, priority).
|
||||
*
|
||||
* Note that when a means of adding elements in extension namespaces is settled on,
|
||||
* this test will need to be updated accordingly.
|
||||
*
|
||||
* @expectedIncorrectUsage WP_Sitemaps_Renderer::get_sitemap_xml
|
||||
*/
|
||||
public function test_get_sitemap_xml_extra_elements() {
|
||||
$url_list = array(
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/2019/10/post-1',
|
||||
'string' => 'value',
|
||||
'number' => 200,
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/2019/10/post-2',
|
||||
'string' => 'another value',
|
||||
'number' => 300,
|
||||
),
|
||||
);
|
||||
|
||||
$renderer = new WP_Sitemaps_Renderer();
|
||||
|
||||
$xml_dom = $this->loadXML( $renderer->get_sitemap_xml( $url_list ) );
|
||||
$xpath = new DOMXPath( $xml_dom );
|
||||
$xpath->registerNamespace( 'sitemap', 'http://www.sitemaps.org/schemas/sitemap/0.9' );
|
||||
|
||||
$this->assertEquals(
|
||||
0,
|
||||
$xpath->evaluate( "count( /sitemap:urlset/sitemap:url/*[ namespace-uri() != 'http://www.sitemaps.org/schemas/sitemap/0.9' or not( local-name() = 'loc' or local-name() = 'lastmod' or local-name() = 'changefreq' or local-name() = 'priority' ) ] )" ),
|
||||
'Invalid child of "sitemap:url" in rendered XML.'
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group sitemaps
|
||||
*/
|
||||
class Test_WP_Sitemaps_Stylesheet extends WP_UnitTestCase {
|
||||
/**
|
||||
* Test that stylesheet content can be filtered.
|
||||
*/
|
||||
public function test_filter_sitemaps_stylesheet_content() {
|
||||
$stylesheet = new WP_Sitemaps_Stylesheet();
|
||||
|
||||
add_filter( 'wp_sitemaps_stylesheet_content', '__return_empty_string' );
|
||||
$content = $stylesheet->get_sitemap_stylesheet();
|
||||
|
||||
$this->assertSame( '', $content, 'Could not filter stylesheet content' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that sitemap index stylesheet content can be filtered.
|
||||
*/
|
||||
public function test_filter_sitemaps_stylesheet_index_content() {
|
||||
$stylesheet = new WP_Sitemaps_Stylesheet();
|
||||
|
||||
add_filter( 'wp_sitemaps_stylesheet_index_content', '__return_empty_string' );
|
||||
$content = $stylesheet->get_sitemap_index_stylesheet();
|
||||
|
||||
$this->assertSame( '', $content, 'Could not filter sitemap index stylesheet content' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that sitemap stylesheet CSS can be filtered.
|
||||
*/
|
||||
public function test_filter_sitemaps_stylesheet_css() {
|
||||
$stylesheet = new WP_Sitemaps_Stylesheet();
|
||||
|
||||
add_filter( 'wp_sitemaps_stylesheet_css', '__return_empty_string' );
|
||||
$css = $stylesheet->get_stylesheet_css();
|
||||
|
||||
$this->assertSame( '', $css, 'Could not filter sitemap stylesheet CSS' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group sitemaps
|
||||
*/
|
||||
class Test_WP_Sitemaps_Taxonomies extends WP_UnitTestCase {
|
||||
/**
|
||||
* List of post_tag IDs.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $post_tags;
|
||||
|
||||
/**
|
||||
* List of category IDs.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $cats;
|
||||
|
||||
/**
|
||||
* Editor ID for use in some tests.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public static $editor_id;
|
||||
|
||||
/**
|
||||
* Set up fixtures.
|
||||
*
|
||||
* @param WP_UnitTest_Factory $factory A WP_UnitTest_Factory object.
|
||||
*/
|
||||
public static function wpSetUpBeforeClass( $factory ) {
|
||||
self::$cats = $factory->term->create_many( 10, array( 'taxonomy' => 'category' ) );
|
||||
self::$post_tags = $factory->term->create_many( 10 );
|
||||
self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a URL list for default taxonomies via
|
||||
* WP_Sitemaps_Taxonomies::get_url_list().
|
||||
*/
|
||||
public function test_get_url_list_taxonomies() {
|
||||
// Add the default category to the list of categories we're testing.
|
||||
$categories = array_merge( array( 1 ), self::$cats );
|
||||
|
||||
// Create a test post to calculate update times.
|
||||
$post = self::factory()->post->create_and_get(
|
||||
array(
|
||||
'tags_input' => self::$post_tags,
|
||||
'post_category' => $categories,
|
||||
)
|
||||
);
|
||||
|
||||
$tax_provider = new WP_Sitemaps_Taxonomies();
|
||||
|
||||
$cat_list = $tax_provider->get_url_list( 1, 'category' );
|
||||
|
||||
$expected_cats = array_map(
|
||||
static function ( $id ) use ( $post ) {
|
||||
return array(
|
||||
'loc' => get_term_link( $id, 'category' ),
|
||||
);
|
||||
},
|
||||
$categories
|
||||
);
|
||||
|
||||
$this->assertSame( $expected_cats, $cat_list, 'Category URL list does not match.' );
|
||||
|
||||
$tag_list = $tax_provider->get_url_list( 1, 'post_tag' );
|
||||
|
||||
$expected_tags = array_map(
|
||||
static function ( $id ) use ( $post ) {
|
||||
return array(
|
||||
'loc' => get_term_link( $id, 'post_tag' ),
|
||||
);
|
||||
},
|
||||
self::$post_tags
|
||||
);
|
||||
|
||||
$this->assertSame( $expected_tags, $tag_list, 'Post Tags URL list does not match.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a URL list for a custom taxonomy via
|
||||
* WP_Sitemaps_Taxonomies::get_url_list().
|
||||
*/
|
||||
public function test_get_url_list_custom_taxonomy() {
|
||||
wp_set_current_user( self::$editor_id );
|
||||
|
||||
// Create a custom taxonomy for this test.
|
||||
$taxonomy = 'test_taxonomy';
|
||||
register_taxonomy( $taxonomy, 'post' );
|
||||
|
||||
// Create test terms in the custom taxonomy.
|
||||
$terms = self::factory()->term->create_many( 10, array( 'taxonomy' => $taxonomy ) );
|
||||
|
||||
// Create a test post applied to all test terms.
|
||||
$post = self::factory()->post->create_and_get( array( 'tax_input' => array( $taxonomy => $terms ) ) );
|
||||
|
||||
$expected = array_map(
|
||||
static function ( $id ) use ( $taxonomy, $post ) {
|
||||
return array(
|
||||
'loc' => get_term_link( $id, $taxonomy ),
|
||||
);
|
||||
},
|
||||
$terms
|
||||
);
|
||||
|
||||
$tax_provider = new WP_Sitemaps_Taxonomies();
|
||||
|
||||
$post_list = $tax_provider->get_url_list( 1, $taxonomy );
|
||||
|
||||
// Clean up.
|
||||
unregister_taxonomy_for_object_type( $taxonomy, 'post' );
|
||||
|
||||
$this->assertEquals( $expected, $post_list, 'Custom taxonomy term links are not visible.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a URL list for a private custom taxonomy via
|
||||
* WP_Sitemaps_Taxonomies::get_url_list().
|
||||
*/
|
||||
public function test_get_url_list_custom_taxonomy_private() {
|
||||
// Create a custom taxonomy for this test.
|
||||
$taxonomy = 'private_taxonomy';
|
||||
register_taxonomy( $taxonomy, 'post', array( 'public' => false ) );
|
||||
|
||||
// Create test terms in the custom taxonomy.
|
||||
$terms = self::factory()->term->create_many( 10, array( 'taxonomy' => $taxonomy ) );
|
||||
|
||||
// Create a test post applied to all test terms.
|
||||
self::factory()->post->create( array( 'tax_input' => array( $taxonomy => $terms ) ) );
|
||||
|
||||
$tax_provider = new WP_Sitemaps_Taxonomies();
|
||||
|
||||
$post_list = $tax_provider->get_url_list( 1, $taxonomy );
|
||||
|
||||
// Clean up.
|
||||
unregister_taxonomy_for_object_type( $taxonomy, 'post' );
|
||||
|
||||
$this->assertEmpty( $post_list, 'Private taxonomy term links are visible.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test sitemap index entries with public and private taxonomies.
|
||||
*/
|
||||
public function test_get_sitemap_entries_custom_taxonomies() {
|
||||
wp_set_current_user( self::$editor_id );
|
||||
|
||||
// Create a custom public and private taxonomies for this test.
|
||||
register_taxonomy( 'public_taxonomy', 'post' );
|
||||
register_taxonomy( 'private_taxonomy', 'post', array( 'public' => false ) );
|
||||
|
||||
// Create test terms in the custom taxonomy.
|
||||
$public_term = self::factory()->term->create( array( 'taxonomy' => 'public_taxonomy' ) );
|
||||
$private_term = self::factory()->term->create( array( 'taxonomy' => 'private_taxonomy' ) );
|
||||
|
||||
// Create a test post applied to all test terms.
|
||||
self::factory()->post->create_and_get(
|
||||
array(
|
||||
'tax_input' => array(
|
||||
'public_taxonomy' => array( $public_term ),
|
||||
'private_taxonomy' => array( $private_term ),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$tax_provider = new WP_Sitemaps_Taxonomies();
|
||||
$entries = wp_list_pluck( $tax_provider->get_sitemap_entries(), 'loc' );
|
||||
|
||||
// Clean up.
|
||||
unregister_taxonomy_for_object_type( 'public_taxonomy', 'post' );
|
||||
unregister_taxonomy_for_object_type( 'private_taxonomy', 'post' );
|
||||
|
||||
$this->assertContains( 'http://' . WP_TESTS_DOMAIN . '/?sitemap=taxonomies&sitemap-subtype=public_taxonomy&paged=1', $entries, 'Public Taxonomies are not in the index.' );
|
||||
$this->assertNotContains( 'http://' . WP_TESTS_DOMAIN . '/?sitemap=taxonomies&sitemap-subtype=private_taxonomy&paged=1', $entries, 'Private Taxonomies are visible in the index.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test ability to filter object subtypes.
|
||||
*/
|
||||
public function test_filter_sitemaps_taxonomies() {
|
||||
$taxonomies_provider = new WP_Sitemaps_Taxonomies();
|
||||
|
||||
// Return an empty array to show that the list of subtypes is filterable.
|
||||
add_filter( 'wp_sitemaps_taxonomies', '__return_empty_array' );
|
||||
$subtypes = $taxonomies_provider->get_object_subtypes();
|
||||
|
||||
$this->assertEquals( array(), $subtypes, 'Could not filter taxonomies subtypes.' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group sitemaps
|
||||
*/
|
||||
class Test_WP_Sitemaps_Users extends WP_UnitTestCase {
|
||||
/**
|
||||
* List of user IDs.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $users;
|
||||
|
||||
/**
|
||||
* Editor ID for use in some tests.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public static $editor_id;
|
||||
|
||||
/**
|
||||
* Set up fixtures.
|
||||
*
|
||||
* @param WP_UnitTest_Factory $factory A WP_UnitTest_Factory object.
|
||||
*/
|
||||
public static function wpSetUpBeforeClass( $factory ) {
|
||||
self::$users = $factory->user->create_many( 10, array( 'role' => 'editor' ) );
|
||||
self::$editor_id = self::$users[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a URL list for a users sitemap page via
|
||||
* WP_Sitemaps_Users::get_url_list().
|
||||
*/
|
||||
public function test_get_url_list_users() {
|
||||
// Set up the user to an editor to assign posts to other users.
|
||||
wp_set_current_user( self::$editor_id );
|
||||
|
||||
// Create a set of posts for each user and generate the expected URL list data.
|
||||
$expected = array_map(
|
||||
static function ( $user_id ) {
|
||||
$post = self::factory()->post->create_and_get( array( 'post_author' => $user_id ) );
|
||||
|
||||
return array(
|
||||
'loc' => get_author_posts_url( $user_id ),
|
||||
);
|
||||
},
|
||||
self::$users
|
||||
);
|
||||
|
||||
$user_provider = new WP_Sitemaps_Users();
|
||||
|
||||
$url_list = $user_provider->get_url_list( 1 );
|
||||
|
||||
$this->assertEqualSets( $expected, $url_list );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,396 @@
|
||||
<?php
|
||||
/**
|
||||
* Sitemaps: Sitemaps_Tests class
|
||||
*
|
||||
* Main test class.
|
||||
*
|
||||
* @package Sitemaps
|
||||
* @copyright 2019 The Core Sitemaps Contributors
|
||||
* @license GNU General Public License, version 2
|
||||
* @link https://github.com/GoogleChromeLabs/wp-sitemaps
|
||||
*/
|
||||
|
||||
/**
|
||||
* Core sitemaps test cases.
|
||||
*
|
||||
* @group sitemaps
|
||||
*/
|
||||
class Test_Sitemaps extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* List of user IDs.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $users;
|
||||
|
||||
/**
|
||||
* List of post_tag IDs.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $post_tags;
|
||||
|
||||
/**
|
||||
* List of category IDs.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $cats;
|
||||
|
||||
/**
|
||||
* List of post type post IDs.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $posts;
|
||||
|
||||
/**
|
||||
* List of post type page IDs.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $pages;
|
||||
|
||||
/**
|
||||
* Editor ID for use in some tests.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public static $editor_id;
|
||||
|
||||
/**
|
||||
* Test sitemap provider.
|
||||
*
|
||||
* @var WP_Sitemaps_Test_Provider
|
||||
*/
|
||||
public static $test_provider;
|
||||
|
||||
/**
|
||||
* Set up fixtures.
|
||||
*
|
||||
* @param WP_UnitTest_Factory $factory A WP_UnitTest_Factory object.
|
||||
*/
|
||||
public static function wpSetUpBeforeClass( $factory ) {
|
||||
self::$users = $factory->user->create_many( 10 );
|
||||
self::$post_tags = $factory->term->create_many( 10 );
|
||||
self::$cats = $factory->term->create_many( 10, array( 'taxonomy' => 'category' ) );
|
||||
self::$pages = $factory->post->create_many( 10, array( 'post_type' => 'page' ) );
|
||||
|
||||
// Create a set of posts pre-assigned to tags and authors.
|
||||
self::$posts = $factory->post->create_many(
|
||||
10,
|
||||
array(
|
||||
'tags_input' => self::$post_tags,
|
||||
'post_author' => reset( self::$users ),
|
||||
)
|
||||
);
|
||||
|
||||
// Create a user with an editor role to complete some tests.
|
||||
self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
|
||||
|
||||
self::$test_provider = new WP_Sitemaps_Test_Provider();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to get all sitemap entries data.
|
||||
*
|
||||
* @return array A list of sitemap entires.
|
||||
*/
|
||||
public function _get_sitemap_entries() {
|
||||
$entries = array();
|
||||
|
||||
$providers = wp_get_sitemaps();
|
||||
|
||||
foreach ( $providers as $provider ) {
|
||||
// Using `array_push` is more efficient than `array_merge` in the loop.
|
||||
array_push( $entries, ...$provider->get_sitemap_entries() );
|
||||
}
|
||||
|
||||
return $entries;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test default sitemap entries.
|
||||
*/
|
||||
public function test_get_sitemap_entries() {
|
||||
$entries = $this->_get_sitemap_entries();
|
||||
|
||||
$expected = array(
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/?sitemap=posts&sitemap-subtype=post&paged=1',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/?sitemap=posts&sitemap-subtype=page&paged=1',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/?sitemap=taxonomies&sitemap-subtype=category&paged=1',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/?sitemap=taxonomies&sitemap-subtype=post_tag&paged=1',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/?sitemap=users&paged=1',
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertSame( $expected, $entries );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test default sitemap entries with permalinks on.
|
||||
*/
|
||||
public function test_get_sitemap_entries_post_with_permalinks() {
|
||||
$this->set_permalink_structure( '/%year%/%postname%/' );
|
||||
|
||||
$entries = $this->_get_sitemap_entries();
|
||||
|
||||
$expected = array(
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-post-1.xml',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-page-1.xml',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-taxonomies-category-1.xml',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-taxonomies-post_tag-1.xml',
|
||||
),
|
||||
array(
|
||||
'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-users-1.xml',
|
||||
),
|
||||
);
|
||||
|
||||
// Clean up permalinks.
|
||||
$this->set_permalink_structure();
|
||||
|
||||
$this->assertSame( $expected, $entries );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test sitemap index entries with public and private custom post types.
|
||||
*/
|
||||
public function test_get_sitemap_entries_custom_post_types() {
|
||||
// Register and create a public post type post.
|
||||
register_post_type( 'public_cpt', array( 'public' => true ) );
|
||||
self::factory()->post->create( array( 'post_type' => 'public_cpt' ) );
|
||||
|
||||
// Register and create a private post type post.
|
||||
register_post_type( 'private_cpt', array( 'public' => false ) );
|
||||
self::factory()->post->create( array( 'post_type' => 'private_cpt' ) );
|
||||
|
||||
$entries = wp_list_pluck( $this->_get_sitemap_entries(), 'loc' );
|
||||
|
||||
// Clean up.
|
||||
unregister_post_type( 'public_cpt' );
|
||||
unregister_post_type( 'private_cpt' );
|
||||
|
||||
$this->assertContains( 'http://' . WP_TESTS_DOMAIN . '/?sitemap=posts&sitemap-subtype=public_cpt&paged=1', $entries, 'Public CPTs are not in the index.' );
|
||||
$this->assertNotContains( 'http://' . WP_TESTS_DOMAIN . '/?sitemap=posts&sitemap-subtype=private_cpt&paged=1', $entries, 'Private CPTs are visible in the index.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a URL list for post type post.
|
||||
*/
|
||||
public function test_get_url_list_post() {
|
||||
$providers = wp_get_sitemaps();
|
||||
|
||||
$post_list = $providers['posts']->get_url_list( 1, 'post' );
|
||||
|
||||
$expected = $this->_get_expected_url_list( 'post', self::$posts );
|
||||
|
||||
$this->assertEquals( $expected, $post_list );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a URL list for post type page.
|
||||
*/
|
||||
public function test_get_url_list_page() {
|
||||
// Short circuit the show on front option.
|
||||
add_filter( 'pre_option_show_on_front', '__return_true' );
|
||||
|
||||
$providers = wp_get_sitemaps();
|
||||
|
||||
$post_list = $providers['posts']->get_url_list( 1, 'page' );
|
||||
|
||||
$expected = $this->_get_expected_url_list( 'page', self::$pages );
|
||||
|
||||
$this->assertEquals( $expected, $post_list );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a URL list for post type page with included home page.
|
||||
*/
|
||||
public function test_get_url_list_page_with_home() {
|
||||
$providers = wp_get_sitemaps();
|
||||
|
||||
$post_list = $providers['posts']->get_url_list( 1, 'page' );
|
||||
|
||||
$expected = $this->_get_expected_url_list( 'page', self::$pages );
|
||||
|
||||
// Add the homepage to the front of the URL list.
|
||||
array_unshift(
|
||||
$expected,
|
||||
array(
|
||||
'loc' => home_url(),
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertEquals( $expected, $post_list );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a URL list for post with private post.
|
||||
*/
|
||||
public function test_get_url_list_private_post() {
|
||||
wp_set_current_user( self::$editor_id );
|
||||
|
||||
$providers = wp_get_sitemaps();
|
||||
|
||||
$post_list_before = $providers['posts']->get_url_list( 1, 'post' );
|
||||
|
||||
$private_post_id = self::factory()->post->create( array( 'post_status' => 'private' ) );
|
||||
|
||||
$post_list_after = $providers['posts']->get_url_list( 1, 'post' );
|
||||
|
||||
$private_post = array(
|
||||
'loc' => get_permalink( $private_post_id ),
|
||||
);
|
||||
|
||||
$this->assertNotContains( $private_post, $post_list_after );
|
||||
$this->assertEqualSets( $post_list_before, $post_list_after );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a URL list for a custom post type.
|
||||
*/
|
||||
public function test_get_url_list_cpt() {
|
||||
$post_type = 'custom_type';
|
||||
|
||||
// Registered post types are private unless explicitly set to public.
|
||||
register_post_type( $post_type, array( 'public' => true ) );
|
||||
|
||||
$ids = self::factory()->post->create_many( 10, array( 'post_type' => $post_type ) );
|
||||
|
||||
$providers = wp_get_sitemaps();
|
||||
|
||||
$post_list = $providers['posts']->get_url_list( 1, $post_type );
|
||||
|
||||
$expected = $this->_get_expected_url_list( $post_type, $ids );
|
||||
|
||||
// Clean up.
|
||||
unregister_post_type( $post_type );
|
||||
|
||||
$this->assertEquals( $expected, $post_list, 'Custom post type posts are not visible.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a URL list for a private custom post type.
|
||||
*/
|
||||
public function test_get_url_list_cpt_private() {
|
||||
$post_type = 'private_type';
|
||||
|
||||
// Create a private post type for testing against data leaking.
|
||||
register_post_type( $post_type, array( 'public' => false ) );
|
||||
|
||||
self::factory()->post->create_many( 10, array( 'post_type' => $post_type ) );
|
||||
|
||||
$providers = wp_get_sitemaps();
|
||||
|
||||
$post_list = $providers['posts']->get_url_list( 1, $post_type );
|
||||
|
||||
// Clean up.
|
||||
unregister_post_type( $post_type );
|
||||
|
||||
$this->assertEmpty( $post_list, 'Private post types may be returned by the post provider.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for building an expected url list.
|
||||
*
|
||||
* @param string $type An object sub type, e.g., post type.
|
||||
* @param array $ids Array of object IDs.
|
||||
* @return array A formed URL list.
|
||||
*/
|
||||
public function _get_expected_url_list( $type, $ids ) {
|
||||
$posts = get_posts(
|
||||
array(
|
||||
'include' => $ids,
|
||||
'orderby' => 'ID',
|
||||
'order' => 'ASC',
|
||||
'post_type' => $type,
|
||||
)
|
||||
);
|
||||
|
||||
return array_map(
|
||||
static function ( $post ) {
|
||||
return array(
|
||||
'loc' => get_permalink( $post ),
|
||||
);
|
||||
},
|
||||
$posts
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test functionality that adds a new sitemap provider to the registry.
|
||||
*/
|
||||
public function test_register_sitemap_provider() {
|
||||
wp_register_sitemap( 'test_sitemap', self::$test_provider );
|
||||
|
||||
$sitemaps = wp_get_sitemaps();
|
||||
|
||||
$this->assertEquals( $sitemaps['test_sitemap'], self::$test_provider, 'Can not confirm sitemap registration is working.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test robots.txt output.
|
||||
*/
|
||||
public function test_robots_text() {
|
||||
// Get the text added to the default robots text output.
|
||||
$robots_text = apply_filters( 'robots_txt', '', true );
|
||||
$sitemap_string = 'Sitemap: http://' . WP_TESTS_DOMAIN . '/?sitemap=index';
|
||||
|
||||
$this->assertContains( $sitemap_string, $robots_text, 'Sitemap URL not included in robots text.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test robots.txt output for a private site.
|
||||
*/
|
||||
public function test_robots_text_private_site() {
|
||||
$robots_text = apply_filters( 'robots_txt', '', false );
|
||||
$sitemap_string = 'Sitemap: http://' . WP_TESTS_DOMAIN . '/?sitemap=index';
|
||||
|
||||
$this->assertNotContains( $sitemap_string, $robots_text );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test robots.txt output with permalinks set.
|
||||
*/
|
||||
public function test_robots_text_with_permalinks() {
|
||||
// Set permalinks for testing.
|
||||
$this->set_permalink_structure( '/%year%/%postname%/' );
|
||||
|
||||
// Get the text added to the default robots text output.
|
||||
$robots_text = apply_filters( 'robots_txt', '', true );
|
||||
$sitemap_string = 'Sitemap: http://' . WP_TESTS_DOMAIN . '/wp-sitemap.xml';
|
||||
|
||||
// Clean up permalinks.
|
||||
$this->set_permalink_structure();
|
||||
|
||||
$this->assertContains( $sitemap_string, $robots_text, 'Sitemap URL not included in robots text.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test robots.txt output with line feed prefix.
|
||||
*/
|
||||
public function test_robots_text_prefixed_with_line_feed() {
|
||||
// Get the text added to the default robots text output.
|
||||
$robots_text = apply_filters( 'robots_txt', '', true );
|
||||
$sitemap_string = "\nSitemap: ";
|
||||
|
||||
$this->assertContains( $sitemap_string, $robots_text, 'Sitemap URL not prefixed with "\n".' );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user