summaryrefslogtreecommitdiffstatshomepage
path: root/tests/phpunit
diff options
context:
space:
mode:
Diffstat (limited to 'tests/phpunit')
-rw-r--r--tests/phpunit/data/images/png-tests/grayscale-test-image.pngbin0 -> 8843 bytes
-rw-r--r--tests/phpunit/data/images/png-tests/vivid-green-bird.pngbin0 -> 2625142 bytes
-rw-r--r--tests/phpunit/includes/abstract-testcase.php55
-rw-r--r--tests/phpunit/includes/build-visual-html-tree.php304
-rw-r--r--tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php5
-rw-r--r--tests/phpunit/includes/object-cache.php8
-rw-r--r--tests/phpunit/tests/admin/includesPlugin.php21
-rw-r--r--tests/phpunit/tests/admin/includesTemplate.php14
-rw-r--r--tests/phpunit/tests/admin/wpPluginsListTable.php2
-rw-r--r--tests/phpunit/tests/ajax/wpAjaxSendAttachmentToEditor.php38
-rw-r--r--tests/phpunit/tests/ajax/wpCustomizeNavMenus.php37
-rw-r--r--tests/phpunit/tests/auth.php16
-rw-r--r--tests/phpunit/tests/build-visual-html-tree.php144
-rw-r--r--tests/phpunit/tests/comment.php20
-rw-r--r--tests/phpunit/tests/comment/commentForm.php34
-rw-r--r--tests/phpunit/tests/comment/commentsTemplate.php15
-rw-r--r--tests/phpunit/tests/comment/metaCache.php34
-rw-r--r--tests/phpunit/tests/comment/wpHandleCommentSubmission.php39
-rw-r--r--tests/phpunit/tests/customize/control.php18
-rw-r--r--tests/phpunit/tests/customize/custom-css-setting.php32
-rw-r--r--tests/phpunit/tests/customize/manager.php24
-rw-r--r--tests/phpunit/tests/customize/nav-menu-item-setting.php18
-rw-r--r--tests/phpunit/tests/customize/nav-menu-setting.php18
-rw-r--r--tests/phpunit/tests/customize/nav-menus.php54
-rw-r--r--tests/phpunit/tests/customize/panel.php25
-rw-r--r--tests/phpunit/tests/customize/selective-refresh-ajax.php26
-rw-r--r--tests/phpunit/tests/customize/setting.php32
-rw-r--r--tests/phpunit/tests/customize/widgets.php57
-rw-r--r--tests/phpunit/tests/db/charset.php8
-rw-r--r--tests/phpunit/tests/dependencies/scripts.php435
-rw-r--r--tests/phpunit/tests/feed/atom.php2
-rw-r--r--tests/phpunit/tests/feed/rss2.php2
-rw-r--r--tests/phpunit/tests/file.php6
-rw-r--r--tests/phpunit/tests/formatting/escUrl.php6
-rw-r--r--tests/phpunit/tests/formatting/sanitizeFileName.php12
-rw-r--r--tests/phpunit/tests/formatting/sanitizePost.php3
-rw-r--r--tests/phpunit/tests/formatting/wpSpecialchars.php2
-rw-r--r--tests/phpunit/tests/formatting/wpTrimExcerpt.php22
-rw-r--r--tests/phpunit/tests/functions.php2
-rw-r--r--tests/phpunit/tests/general/template.php38
-rw-r--r--tests/phpunit/tests/image/editorImagick.php61
-rw-r--r--tests/phpunit/tests/kses.php2
-rw-r--r--tests/phpunit/tests/l10n.php17
-rw-r--r--tests/phpunit/tests/l10n/getUserLocale.php53
-rw-r--r--tests/phpunit/tests/media.php7
-rw-r--r--tests/phpunit/tests/meta.php117
-rw-r--r--tests/phpunit/tests/oembed/controller.php24
-rw-r--r--tests/phpunit/tests/oembed/wpOembed.php26
-rw-r--r--tests/phpunit/tests/post.php3
-rw-r--r--tests/phpunit/tests/post/getPages.php71
-rw-r--r--tests/phpunit/tests/post/getPostsByAuthorSql.php36
-rw-r--r--tests/phpunit/tests/post/revisions.php2
-rw-r--r--tests/phpunit/tests/post/updateTermCountOnTransitionPostStatus.php180
-rw-r--r--tests/phpunit/tests/post/wpPostType.php2
-rw-r--r--tests/phpunit/tests/query.php3
-rw-r--r--tests/phpunit/tests/query/conditionals.php40
-rw-r--r--tests/phpunit/tests/query/postStatus.php6
-rw-r--r--tests/phpunit/tests/query/setupPostdata.php12
-rw-r--r--tests/phpunit/tests/readme.php18
-rw-r--r--tests/phpunit/tests/rest-api/rest-comments-controller.php4
-rw-r--r--tests/phpunit/tests/rest-api/rest-global-styles-controller.php56
-rw-r--r--tests/phpunit/tests/rest-api/rest-posts-controller.php3
-rw-r--r--tests/phpunit/tests/rest-api/rest-schema-setup.php2
-rw-r--r--tests/phpunit/tests/rest-api/rest-server.php2
-rw-r--r--tests/phpunit/tests/rest-api/rest-settings-controller.php71
-rw-r--r--tests/phpunit/tests/rest-api/rest-users-controller.php6
-rw-r--r--tests/phpunit/tests/shortcode.php3
-rw-r--r--tests/phpunit/tests/taxonomy.php28
-rw-r--r--tests/phpunit/tests/term/cache.php2
-rw-r--r--tests/phpunit/tests/term/meta.php64
-rw-r--r--tests/phpunit/tests/term/wpGenerateTagCloud.php4
-rw-r--r--tests/phpunit/tests/term/wpGetObjectTerms.php5
-rw-r--r--tests/phpunit/tests/term/wpTaxonomy.php2
-rw-r--r--tests/phpunit/tests/theme.php11
-rw-r--r--tests/phpunit/tests/url.php17
-rw-r--r--tests/phpunit/tests/user.php28
-rw-r--r--tests/phpunit/tests/user/capabilities.php23
-rw-r--r--tests/phpunit/tests/user/getTheAuthorPosts.php4
-rw-r--r--tests/phpunit/tests/xmlrpc/wp/getComments.php2
-rw-r--r--tests/phpunit/tests/xmlrpc/wp/getPageList.php1
-rw-r--r--tests/phpunit/tests/xmlrpc/wp/getPages.php1
-rw-r--r--tests/phpunit/tests/xmlrpc/wp/getPosts.php2
-rw-r--r--tests/phpunit/tests/xmlrpc/wp/getTerms.php1
-rw-r--r--tests/phpunit/tests/xmlrpc/wp/getUsers.php1
84 files changed, 2035 insertions, 616 deletions
diff --git a/tests/phpunit/data/images/png-tests/grayscale-test-image.png b/tests/phpunit/data/images/png-tests/grayscale-test-image.png
new file mode 100644
index 0000000000..10f537a71b
--- /dev/null
+++ b/tests/phpunit/data/images/png-tests/grayscale-test-image.png
Binary files differ
diff --git a/tests/phpunit/data/images/png-tests/vivid-green-bird.png b/tests/phpunit/data/images/png-tests/vivid-green-bird.png
new file mode 100644
index 0000000000..7b55b3d5e7
--- /dev/null
+++ b/tests/phpunit/data/images/png-tests/vivid-green-bird.png
Binary files differ
diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php
index f665bdafb1..29ce8d8b53 100644
--- a/tests/phpunit/includes/abstract-testcase.php
+++ b/tests/phpunit/includes/abstract-testcase.php
@@ -1,5 +1,6 @@
<?php
+require_once __DIR__ . '/build-visual-html-tree.php';
require_once __DIR__ . '/factory.php';
require_once __DIR__ . '/trac.php';
@@ -13,7 +14,6 @@ require_once __DIR__ . '/trac.php';
* All WordPress unit tests should inherit from this class.
*/
abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
-
protected static $forced_tickets = array();
protected $expected_deprecated = array();
protected $caught_deprecated = array();
@@ -136,6 +136,21 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
$this->start_transaction();
$this->expectDeprecated();
add_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) );
+ add_filter( 'wp_hash_password_options', array( $this, 'wp_hash_password_options' ), 1, 2 );
+ }
+
+ /**
+ * Sets the bcrypt cost option for password hashing during tests.
+ *
+ * @param array $options The options for password hashing.
+ * @param string|int $algorithm The algorithm to use for hashing. This is a string in PHP 7.4+ and an integer in PHP 7.3 and earlier.
+ */
+ public function wp_hash_password_options( array $options, $algorithm ): array {
+ if ( PASSWORD_BCRYPT === $algorithm ) {
+ $options['cost'] = 5;
+ }
+
+ return $options;
}
/**
@@ -1181,6 +1196,44 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
}
/**
+ * Check HTML markup (including blocks) for semantic equivalence.
+ *
+ * Given two markup strings, assert that they translate to the same semantic HTML tree,
+ * normalizing tag names, attribute names, and attribute order. Furthermore, attributes
+ * and class names are sorted and deduplicated, and whitespace in style attributes
+ * is normalized. Finally, block delimiter comments are recognized and normalized,
+ * applying the same principles.
+ *
+ * @since 6.9.0
+ *
+ * @param string $expected The expected HTML.
+ * @param string $actual The actual HTML.
+ * @param string|null $fragment_context Optional. The fragment context, for example "<td>" expected HTML
+ * must occur within "<table><tr>" fragment context. Default "<body>".
+ * Only "<body>" or `null` are supported at this time.
+ * Set to `null` to parse a full HTML document.
+ * @param string|null $message Optional. The assertion error message.
+ */
+ public function assertEqualHTML( string $expected, string $actual, ?string $fragment_context = '<body>', $message = 'HTML markup was not equivalent.' ): void {
+ try {
+ $tree_expected = build_visual_html_tree( $expected, $fragment_context );
+ $tree_actual = build_visual_html_tree( $actual, $fragment_context );
+ } catch ( Exception $e ) {
+ // For PHP 8.4+, we can retry, using the built-in DOM\HTMLDocument parser.
+ if ( class_exists( 'DOM\HtmlDocument' ) ) {
+ $dom_expected = DOM\HtmlDocument::createFromString( $expected, LIBXML_NOERROR );
+ $tree_expected = build_visual_html_tree( $dom_expected->saveHtml(), $fragment_context );
+ $dom_actual = DOM\HtmlDocument::createFromString( $actual, LIBXML_NOERROR );
+ $tree_actual = build_visual_html_tree( $dom_actual->saveHtml(), $fragment_context );
+ } else {
+ throw $e;
+ }
+ }
+
+ $this->assertSame( $tree_expected, $tree_actual, $message );
+ }
+
+ /**
* Helper function to convert a single-level array containing text strings to a named data provider.
*
* The value of the data set will also be used as the name of the data set.
diff --git a/tests/phpunit/includes/build-visual-html-tree.php b/tests/phpunit/includes/build-visual-html-tree.php
new file mode 100644
index 0000000000..e0f6c3ac3d
--- /dev/null
+++ b/tests/phpunit/includes/build-visual-html-tree.php
@@ -0,0 +1,304 @@
+<?php
+
+/* phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped */
+
+/**
+ * Generates representation of the semantic HTML tree structure.
+ *
+ * This is inspired by the representation used by the HTML5lib tests. It's been extended here for
+ * blocks to render the semantic structure of blocks and their attributes.
+ * The order of attributes and class names is normalized both for HTML tags and blocks,
+ * as is the whitespace in HTML tags' style attribute.
+ *
+ * For example, consider the following block markup:
+ *
+ * <!-- wp:separator {"className":"is-style-default has-custom-classname","style":{"spacing":{"margin":{"top":"50px","bottom":"50px"}}},"backgroundColor":"accent-1"} -->
+ * <hr class="wp-block-separator is-style-default has-custom-classname" style="margin-top: 50px; margin-bottom: 50px" />
+ * <!-- /wp:separator -->
+ *
+ * This will be represented as:
+ *
+ * BLOCK["core/separator"]
+ * {
+ * "backgroundColor": "accent-1",
+ * "className": "has-custom-classname is-style-default",
+ * "style": {
+ * "spacing": {
+ * "margin": {
+ * "top": "50px",
+ * "bottom": "50px"
+ * }
+ * }
+ * }
+ * }
+ * <hr>
+ * class="has-custom-classname is-style-default wp-block-separator"
+ * style="margin-top:50px;margin-bottom:50px;"
+ *
+ *
+ * @see https://github.com/WordPress/wordpress-develop/blob/trunk/tests/phpunit/data/html5lib-tests/tree-construction/README.md
+ *
+ * @since 6.9.0
+ *
+ * @throws WP_HTML_Unsupported_Exception|Error If the markup could not be parsed.
+ *
+ * @param string $html Given test HTML.
+ * @param string|null $fragment_context Context element in which to parse HTML, such as BODY or SVG.
+ * @return string Tree structure of parsed HTML, if supported.
+ */
+function build_visual_html_tree( string $html, ?string $fragment_context ): string {
+ $processor = $fragment_context
+ ? WP_HTML_Processor::create_fragment( $html, $fragment_context )
+ : WP_HTML_Processor::create_full_parser( $html );
+ if ( null === $processor ) {
+ throw new Error( 'Could not create a parser.' );
+ }
+ $tree_indent = ' ';
+
+ $output = '';
+ $indent_level = 0;
+ $was_text = null;
+ $text_node = '';
+
+ $block_context = array();
+
+ while ( $processor->next_token() ) {
+ if ( null !== $processor->get_last_error() ) {
+ break;
+ }
+
+ $token_name = $processor->get_token_name();
+ $token_type = $processor->get_token_type();
+ $is_closer = $processor->is_tag_closer();
+
+ if ( $was_text && '#text' !== $token_name ) {
+ if ( '' !== $text_node ) {
+ $output .= "{$text_node}\"\n";
+ }
+ $was_text = false;
+ $text_node = '';
+ }
+
+ switch ( $token_type ) {
+ case '#doctype':
+ $doctype = $processor->get_doctype_info();
+ $output .= "<!DOCTYPE {$doctype->name}";
+ if ( null !== $doctype->public_identifier || null !== $doctype->system_identifier ) {
+ $output .= " \"{$doctype->public_identifier}\" \"{$doctype->system_identifier}\"";
+ }
+ $output .= ">\n";
+ break;
+
+ case '#tag':
+ $namespace = $processor->get_namespace();
+ $tag_name = 'html' === $namespace
+ ? strtolower( $processor->get_tag() )
+ : "{$namespace} {$processor->get_qualified_tag_name()}";
+
+ if ( $is_closer ) {
+ --$indent_level;
+
+ if ( 'html' === $namespace && 'TEMPLATE' === $token_name ) {
+ --$indent_level;
+ }
+
+ break;
+ }
+
+ $tag_indent = $indent_level;
+
+ if ( $processor->expects_closer() ) {
+ ++$indent_level;
+ }
+
+ $output .= str_repeat( $tree_indent, $tag_indent ) . "<{$tag_name}>\n";
+
+ $attribute_names = $processor->get_attribute_names_with_prefix( '' );
+ if ( $attribute_names ) {
+ $sorted_attributes = array();
+ foreach ( $attribute_names as $attribute_name ) {
+ $sorted_attributes[ $attribute_name ] = $processor->get_qualified_attribute_name( $attribute_name );
+ }
+
+ /*
+ * Sorts attributes to match html5lib sort order.
+ *
+ * - First comes normal HTML attributes.
+ * - Then come adjusted foreign attributes; these have spaces in their names.
+ * - Finally come non-adjusted foreign attributes; these have a colon in their names.
+ *
+ * Example:
+ *
+ * From: <math xlink:author definitionurl xlink:title xlink:show>
+ * Sorted: 'definitionURL', 'xlink show', 'xlink title', 'xlink:author'
+ */
+ uasort(
+ $sorted_attributes,
+ static function ( $a, $b ) {
+ $a_has_ns = str_contains( $a, ':' );
+ $b_has_ns = str_contains( $b, ':' );
+
+ // Attributes with `:` should follow all other attributes.
+ if ( $a_has_ns !== $b_has_ns ) {
+ return $a_has_ns ? 1 : -1;
+ }
+
+ $a_has_sp = str_contains( $a, ' ' );
+ $b_has_sp = str_contains( $b, ' ' );
+
+ // Attributes with a namespace ' ' should come after those without.
+ if ( $a_has_sp !== $b_has_sp ) {
+ return $a_has_sp ? 1 : -1;
+ }
+
+ return $a <=> $b;
+ }
+ );
+
+ foreach ( $sorted_attributes as $attribute_name => $display_name ) {
+ $val = $processor->get_attribute( $attribute_name );
+ /*
+ * Attributes with no value are `true` with the HTML API,
+ * we use the empty string value in the tree structure.
+ */
+ if ( true === $val ) {
+ $val = '';
+ } elseif ( 'class' === $attribute_name ) {
+ $class_names = iterator_to_array( $processor->class_list() );
+ sort( $class_names, SORT_STRING );
+ $val = implode( ' ', $class_names );
+ } elseif ( 'style' === $attribute_name ) {
+ $normalized_style = '';
+ foreach ( explode( ';', $val ) as $style ) {
+ if ( empty( trim( $style ) ) ) {
+ continue;
+ }
+ list( $style_key, $style_val ) = explode( ':', $style );
+
+ $style_key = trim( $style_key );
+ $style_val = trim( $style_val );
+
+ $normalized_style .= "{$style_key}:{$style_val};";
+ }
+ $val = $normalized_style;
+ }
+ $output .= str_repeat( $tree_indent, $tag_indent + 1 ) . "{$display_name}=\"{$val}\"\n";
+ }
+ }
+
+ // Self-contained tags contain their inner contents as modifiable text.
+ $modifiable_text = $processor->get_modifiable_text();
+ if ( '' !== $modifiable_text ) {
+ $output .= str_repeat( $tree_indent, $tag_indent + 1 ) . "\"{$modifiable_text}\"\n";
+ }
+
+ if ( 'html' === $namespace && 'TEMPLATE' === $token_name ) {
+ $output .= str_repeat( $tree_indent, $indent_level ) . "content\n";
+ ++$indent_level;
+ }
+
+ break;
+
+ case '#cdata-section':
+ case '#text':
+ $text_content = $processor->get_modifiable_text();
+ if ( '' === trim( $text_content, " \f\t\r\n" ) ) {
+ break;
+ }
+ $was_text = true;
+ if ( '' === $text_node ) {
+ $text_node .= str_repeat( $tree_indent, $indent_level ) . '"';
+ }
+ $text_node .= $text_content;
+ break;
+
+ case '#funky-comment':
+ // Comments must be "<" then "!-- " then the data then " -->".
+ $output .= str_repeat( $tree_indent, $indent_level ) . "<!-- {$processor->get_modifiable_text()} -->\n";
+ break;
+
+ case '#comment':
+ // Comments must be "<" then "!--" then the data then "-->".
+ $comment = "<!--{$processor->get_full_comment_text()}-->";
+
+ // Maybe the comment is a block delimiter.
+ $parser = new WP_Block_Parser();
+ $parser->document = $comment;
+ $parser->offset = 0;
+ list( $delimiter_type, $block_name, $block_attrs, $start_offset, $token_length ) = $parser->next_token();
+
+ switch ( $delimiter_type ) {
+ case 'block-opener':
+ case 'void-block':
+ $output .= str_repeat( $tree_indent, $indent_level ) . "BLOCK[\"{$block_name}\"]\n";
+
+ if ( 'block-opener' === $delimiter_type ) {
+ $block_context[] = $block_name;
+ ++$indent_level;
+ }
+
+ // If they're no attributes, we're done here.
+ if ( empty( $block_attrs ) ) {
+ break;
+ }
+
+ // Normalize attribute order.
+ ksort( $block_attrs, SORT_STRING );
+
+ if ( isset( $block_attrs['className'] ) ) {
+ // Normalize class name order (and de-duplicate), as we need to be tolerant of different orders.
+ // (Style attributes don't need this treatment, as they are parsed into a nested array.)
+ $block_class_processor = new WP_HTML_Tag_Processor( '<div>' );
+ $block_class_processor->next_token();
+ $block_class_processor->set_attribute( 'class', $block_attrs['className'] );
+ $class_names = iterator_to_array( $block_class_processor->class_list() );
+ sort( $class_names, SORT_STRING );
+ $block_attrs['className'] = implode( ' ', $class_names );
+ }
+
+ $block_attrs = json_encode( $block_attrs, JSON_PRETTY_PRINT );
+ // Fix indentation by "halving" it (2 spaces instead of 4).
+ // Additionally, we need to indent each line by the current indentation level.
+ $block_attrs = preg_replace( '/^( +)\1/m', str_repeat( $tree_indent, $indent_level ) . '$1', $block_attrs );
+ // Finally, indent the first line, and the last line (with the closing curly brace).
+ $output .= str_repeat( $tree_indent, $indent_level ) . substr( $block_attrs, 0, -1 ) . str_repeat( $tree_indent, $indent_level ) . "}\n";
+ break;
+ case 'block-closer':
+ // Is this a closer for the currently open block?
+ if ( ! empty( $block_context ) && end( $block_context ) === $block_name ) {
+ // If it's a closer, we don't add it to the output.
+ // Instead, we decrease indentation and remove the block from block context stack.
+ --$indent_level;
+ array_pop( $block_context );
+ }
+ break;
+ default: // Not a block delimiter.
+ $output .= str_repeat( $tree_indent, $indent_level ) . $comment . "\n";
+ break;
+ }
+ break;
+ default:
+ // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
+ $serialized_token_type = var_export( $processor->get_token_type(), true );
+ throw new Error( "Unhandled token type for tree construction: {$serialized_token_type}" );
+ }
+ }
+
+ if ( null !== $processor->get_unsupported_exception() ) {
+ throw $processor->get_unsupported_exception();
+ }
+
+ if ( null !== $processor->get_last_error() ) {
+ throw new Error( "Parser error: {$processor->get_last_error()}" );
+ }
+
+ if ( $processor->paused_at_incomplete_token() ) {
+ throw new Error( 'Paused at incomplete token.' );
+ }
+
+ if ( '' !== $text_node ) {
+ $output .= "{$text_node}\"\n";
+ }
+
+ return $output;
+}
diff --git a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php
index 262c6c4640..2c1872795f 100644
--- a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php
+++ b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php
@@ -50,12 +50,13 @@ class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
}
/**
- * Saves an attachment.
+ * Saves a file as an attachment.
*
* @since 4.4.0
* @since 6.2.0 Returns a WP_Error object on failure.
*
- * @param string $file The file name to create attachment object for.
+ * @param string $file Full path to the file to create an attachment object for.
+ * The name of the file will be used as the attachment name.
* @param int $parent_post_id ID of the post to attach the file to.
*
* @return int|WP_Error The attachment ID on success, WP_Error object on failure.
diff --git a/tests/phpunit/includes/object-cache.php b/tests/phpunit/includes/object-cache.php
index ef03546892..daffcabac5 100644
--- a/tests/phpunit/includes/object-cache.php
+++ b/tests/phpunit/includes/object-cache.php
@@ -160,8 +160,8 @@ function wp_cache_cas( $cas_token, $key, $value, $group = '', $expiration = 0 )
*
* @link https://www.php.net/manual/en/memcached.casbykey.php
*
- * @param string $server_key The key identifying the server to store the value on.
* @param float $cas_token Unique value associated with the existing item. Generated by memcached.
+ * @param string $server_key The key identifying the server to store the value on.
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
@@ -1238,8 +1238,8 @@ class WP_Object_Cache {
*
* @link https://www.php.net/manual/en/memcached.casbykey.php
*
- * @param string $server_key The key identifying the server to store the value on.
* @param float $cas_token Unique value associated with the existing item. Generated by memcached.
+ * @param string $server_key The key identifying the server to store the value on.
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
@@ -1929,12 +1929,12 @@ class WP_Object_Cache {
*
* @link https://www.php.net/manual/en/memcached.replace.php
*
- * @param string $server_key The key identifying the server to store the value on.
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
- * @param bool $by_key True to store in internal cache by key; false to not store by key.
* @param int $expiration The expiration time, defaults to 0.
+ * @param string $server_key The key identifying the server to store the value on.
+ * @param bool $by_key True to store in internal cache by key; false to not store by key.
* @return bool True on success, false on failure.
*/
public function replace( $key, $value, $group = 'default', $expiration = 0, $server_key = '', $by_key = false ) {
diff --git a/tests/phpunit/tests/admin/includesPlugin.php b/tests/phpunit/tests/admin/includesPlugin.php
index e95697810d..4d47fb7db0 100644
--- a/tests/phpunit/tests/admin/includesPlugin.php
+++ b/tests/phpunit/tests/admin/includesPlugin.php
@@ -4,7 +4,16 @@
* @group admin
*/
class Tests_Admin_IncludesPlugin extends WP_UnitTestCase {
+
+ /**
+ * Admin user ID.
+ *
+ * @var int $admin_id
+ */
+ public static $admin_id;
+
public static function wpSetUpBeforeClass( $factory ) {
+ self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
self::_back_up_mu_plugins();
}
@@ -37,7 +46,7 @@ class Tests_Admin_IncludesPlugin extends WP_UnitTestCase {
public function test_menu_page_url() {
$current_user = get_current_user_id();
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$admin_id );
update_option( 'siteurl', 'http://example.com' );
// Add some pages.
@@ -81,7 +90,7 @@ class Tests_Admin_IncludesPlugin extends WP_UnitTestCase {
global $submenu;
global $menu;
$current_user = get_current_user_id();
- $admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
+ $admin_user = self::$admin_id;
wp_set_current_user( $admin_user );
set_current_screen( 'dashboard' );
@@ -134,7 +143,7 @@ class Tests_Admin_IncludesPlugin extends WP_UnitTestCase {
$menu = array();
$current_user = get_current_user_id();
- $admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
+ $admin_user = self::$admin_id;
wp_set_current_user( $admin_user );
set_current_screen( 'dashboard' );
@@ -283,7 +292,7 @@ class Tests_Admin_IncludesPlugin extends WP_UnitTestCase {
$submenu = array();
$menu = array();
$current_user = get_current_user_id();
- $admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
+ $admin_user = self::$admin_id;
wp_set_current_user( $admin_user );
set_current_screen( 'dashboard' );
@@ -316,7 +325,7 @@ class Tests_Admin_IncludesPlugin extends WP_UnitTestCase {
$submenu = array();
$menu = array();
$current_user = get_current_user_id();
- $admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
+ $admin_user = self::$admin_id;
wp_set_current_user( $admin_user );
set_current_screen( 'dashboard' );
@@ -344,7 +353,7 @@ class Tests_Admin_IncludesPlugin extends WP_UnitTestCase {
$submenu = array();
$menu = array();
$current_user = get_current_user_id();
- $admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
+ $admin_user = self::$admin_id;
wp_set_current_user( $admin_user );
set_current_screen( 'dashboard' );
diff --git a/tests/phpunit/tests/admin/includesTemplate.php b/tests/phpunit/tests/admin/includesTemplate.php
index 66e3befd5f..909aff217a 100644
--- a/tests/phpunit/tests/admin/includesTemplate.php
+++ b/tests/phpunit/tests/admin/includesTemplate.php
@@ -3,6 +3,16 @@
* @group admin
*/
class Tests_Admin_IncludesTemplate extends WP_UnitTestCase {
+ /**
+ * Editor user ID.
+ *
+ * @var int $editor_id
+ */
+ public static $editor_id;
+
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
+ }
/**
* @ticket 51137
@@ -64,7 +74,7 @@ class Tests_Admin_IncludesTemplate extends WP_UnitTestCase {
wp_set_object_terms( $post->ID, $term['term_id'], 'wptests_tax_1' );
// Test that get_inline_data() has `post_category` div containing the assigned term.
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
+ wp_set_current_user( self::$editor_id );
get_inline_data( $post );
$this->expectOutputRegex( '/<div class="post_category" id="wptests_tax_1_' . $post->ID . '">' . $term['term_id'] . '<\/div>/' );
}
@@ -90,7 +100,7 @@ class Tests_Admin_IncludesTemplate extends WP_UnitTestCase {
wp_set_object_terms( $post->ID, $term['term_id'], 'wptests_tax_1' );
// Test that get_inline_data() has `tags_input` div containing the assigned term.
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
+ wp_set_current_user( self::$editor_id );
get_inline_data( $post );
$this->expectOutputRegex( '/<div class="tags_input" id="wptests_tax_1_' . $post->ID . '">Test<\/div>/' );
}
diff --git a/tests/phpunit/tests/admin/wpPluginsListTable.php b/tests/phpunit/tests/admin/wpPluginsListTable.php
index af44861324..0b10881c46 100644
--- a/tests/phpunit/tests/admin/wpPluginsListTable.php
+++ b/tests/phpunit/tests/admin/wpPluginsListTable.php
@@ -57,7 +57,7 @@ class Tests_Admin_wpPluginsListTable extends WP_UnitTestCase {
'role' => 'administrator',
'user_login' => 'test_wp_plugins_list_table',
'user_pass' => 'password',
- 'user_email' => 'testadmin@test.com',
+ 'user_email' => 'testadmin@example.com',
)
);
self::$original_s = $s;
diff --git a/tests/phpunit/tests/ajax/wpAjaxSendAttachmentToEditor.php b/tests/phpunit/tests/ajax/wpAjaxSendAttachmentToEditor.php
index 29066ff287..351485afd1 100644
--- a/tests/phpunit/tests/ajax/wpAjaxSendAttachmentToEditor.php
+++ b/tests/phpunit/tests/ajax/wpAjaxSendAttachmentToEditor.php
@@ -14,6 +14,28 @@ require_once ABSPATH . 'wp-admin/includes/ajax-actions.php';
class Tests_Ajax_wpAjaxSendAttachmentToEditor extends WP_Ajax_UnitTestCase {
/**
+ * Shared user ID for the tests.
+ *
+ * @var int
+ */
+ public static $user_id = 0;
+
+ /**
+ * Set up shared fixtures.
+ *
+ * @param WP_UnitTest_Factory $factory
+ */
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$user_id = $factory->user->create(
+ array(
+ 'role' => 'administrator',
+ 'user_login' => 'user_36578_administrator',
+ 'user_email' => 'user_36578_administrator@example.com',
+ )
+ );
+ }
+
+ /**
* @ticket 36578
*
* @covers ::get_image_send_to_editor
@@ -105,13 +127,7 @@ class Tests_Ajax_wpAjaxSendAttachmentToEditor extends WP_Ajax_UnitTestCase {
public function test_wp_ajax_set_attachment_thumbnail_success() {
// Become an administrator.
$post = $_POST;
- $user_id = self::factory()->user->create(
- array(
- 'role' => 'administrator',
- 'user_login' => 'user_36578_administrator',
- 'user_email' => 'user_36578_administrator@example.com',
- )
- );
+ $user_id = self::$user_id;
wp_set_current_user( $user_id );
$_POST = array_merge( $_POST, $post );
@@ -151,13 +167,7 @@ class Tests_Ajax_wpAjaxSendAttachmentToEditor extends WP_Ajax_UnitTestCase {
public function test_wp_ajax_set_attachment_thumbnail_missing_nonce() {
// Become an administrator.
$post = $_POST;
- $user_id = self::factory()->user->create(
- array(
- 'role' => 'administrator',
- 'user_login' => 'user_36578_administrator',
- 'user_email' => 'user_36578_administrator@example.com',
- )
- );
+ $user_id = self::$user_id;
wp_set_current_user( $user_id );
$_POST = array_merge( $_POST, $post );
diff --git a/tests/phpunit/tests/ajax/wpCustomizeNavMenus.php b/tests/phpunit/tests/ajax/wpCustomizeNavMenus.php
index 1dfe5f684d..08353f6e65 100644
--- a/tests/phpunit/tests/ajax/wpCustomizeNavMenus.php
+++ b/tests/phpunit/tests/ajax/wpCustomizeNavMenus.php
@@ -38,6 +38,26 @@ class Tests_Ajax_wpCustomizeNavMenus extends WP_Ajax_UnitTestCase {
*/
public static $terms;
+
+ /**
+ * Admin user ID.
+ *
+ * @var int
+ */
+ public static $admin_user_id = 0;
+
+ /**
+ * User IDs keyed by role.
+ *
+ * @var int[]
+ */
+ public static $user_ids = array();
+
+ /**
+ * Set up shared fixtures.
+ *
+ * @param WP_UnitTest_Factory $factory The factory.
+ */
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
// Make some post objects.
self::$posts = $factory->post->create_many( 5 );
@@ -45,6 +65,13 @@ class Tests_Ajax_wpCustomizeNavMenus extends WP_Ajax_UnitTestCase {
// Some terms too.
self::$terms = $factory->term->create_many( 5 );
+
+ // Create an admin user.
+ self::$admin_user_id = $factory->user->create( array( 'role' => 'administrator' ) );
+
+ foreach ( array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ) as $role ) {
+ self::$user_ids[ $role ] = $factory->user->create( array( 'role' => $role ) );
+ }
}
/**
@@ -53,7 +80,7 @@ class Tests_Ajax_wpCustomizeNavMenus extends WP_Ajax_UnitTestCase {
public function set_up() {
parent::set_up();
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$admin_user_id );
global $wp_customize;
$this->wp_customize = new WP_Customize_Manager();
$wp_customize = $this->wp_customize;
@@ -91,7 +118,7 @@ class Tests_Ajax_wpCustomizeNavMenus extends WP_Ajax_UnitTestCase {
$this->expectExceptionMessage( '-1' );
}
- wp_set_current_user( self::factory()->user->create( array( 'role' => $role ) ) );
+ wp_set_current_user( self::$user_ids[ $role ] );
$_POST = array(
'action' => 'load-available-menu-items-customizer',
@@ -485,7 +512,7 @@ class Tests_Ajax_wpCustomizeNavMenus extends WP_Ajax_UnitTestCase {
$this->expectExceptionMessage( '-1' );
}
- wp_set_current_user( self::factory()->user->create( array( 'role' => $role ) ) );
+ wp_set_current_user( self::$user_ids[ $role ] );
$_POST = array(
'action' => 'search-available-menu-items-customizer',
@@ -705,7 +732,7 @@ class Tests_Ajax_wpCustomizeNavMenus extends WP_Ajax_UnitTestCase {
$this->assertSame( 'bad_nonce', $response['data'] );
// Bad nonce.
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'subscriber' ) ) );
+ wp_set_current_user( self::$user_ids['subscriber'] );
$_POST = wp_slash(
array(
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
@@ -718,7 +745,7 @@ class Tests_Ajax_wpCustomizeNavMenus extends WP_Ajax_UnitTestCase {
$this->assertSame( 'customize_not_allowed', $response['data'] );
// Missing params.
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$user_ids['administrator'] );
$_POST = wp_slash(
array(
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
diff --git a/tests/phpunit/tests/auth.php b/tests/phpunit/tests/auth.php
index 3bdcb98b65..a490842ebd 100644
--- a/tests/phpunit/tests/auth.php
+++ b/tests/phpunit/tests/auth.php
@@ -1579,12 +1579,7 @@ class Tests_Auth extends WP_UnitTestCase {
* @covers ::wp_validate_application_password
*/
public function test_application_password_authentication() {
- $user_id = self::factory()->user->create(
- array(
- 'user_login' => 'http_auth_login',
- 'user_pass' => 'http_auth_pass', // Shouldn't be allowed for API login.
- )
- );
+ $user_id = self::$_user->ID;
// Create a new app-only password.
list( $user_app_password, $item ) = WP_Application_Passwords::create_new_application_password( $user_id, array( 'name' => 'phpunit' ) );
@@ -1594,8 +1589,8 @@ class Tests_Auth extends WP_UnitTestCase {
add_filter( 'wp_is_application_passwords_available', '__return_true' );
// Fake an HTTP Auth request with the regular account password first.
- $_SERVER['PHP_AUTH_USER'] = 'http_auth_login';
- $_SERVER['PHP_AUTH_PW'] = 'http_auth_pass';
+ $_SERVER['PHP_AUTH_USER'] = self::USER_LOGIN;
+ $_SERVER['PHP_AUTH_PW'] = self::USER_PASS;
$this->assertNull(
wp_validate_application_password( null ),
@@ -2094,9 +2089,6 @@ class Tests_Auth extends WP_UnitTestCase {
}
private static function get_default_bcrypt_cost(): int {
- $hash = password_hash( 'password', PASSWORD_BCRYPT );
- $info = password_get_info( $hash );
-
- return $info['options']['cost'];
+ return 5;
}
}
diff --git a/tests/phpunit/tests/build-visual-html-tree.php b/tests/phpunit/tests/build-visual-html-tree.php
new file mode 100644
index 0000000000..42e35c5b74
--- /dev/null
+++ b/tests/phpunit/tests/build-visual-html-tree.php
@@ -0,0 +1,144 @@
+<?php
+
+/**
+ * Tests for build_visual_html_tree().
+ *
+ * @package WordPress
+ *
+ * @group testsuite
+ */
+class Tests_Build_Equivalent_HTML_Semantic_Tree extends WP_UnitTestCase {
+ public function data_build_equivalent_html_semantic_tree() {
+ $block_markup = <<<END
+ <!-- wp:separator {"className":"is-style-default has-custom-classname","style":{"spacing":{"margin":{"top":"50px","bottom":"50px"}}},"backgroundColor":"accent-1"} -->
+ <hr class="wp-block-separator is-style-default has-custom-classname" style="margin-top: 50px; margin-bottom: 50px" />
+ <!-- /wp:separator -->
+END;
+
+ $tree_structure = <<<END
+BLOCK["core/separator"]
+ {
+ "backgroundColor": "accent-1",
+ "className": "has-custom-classname is-style-default",
+ "style": {
+ "spacing": {
+ "margin": {
+ "top": "50px",
+ "bottom": "50px"
+ }
+ }
+ }
+ }
+ <hr>
+ class="has-custom-classname is-style-default wp-block-separator"
+ style="margin-top:50px;margin-bottom:50px;"
+
+END;
+
+ return array(
+ 'Block delimiter' => array( $block_markup, $tree_structure ),
+ );
+ }
+
+ /**
+ * @ticket 63527
+ *
+ * @covers ::build_visual_html_tree
+ *
+ * @dataProvider data_build_equivalent_html_semantic_tree
+ */
+ public function test_build_equivalent_html_semantic_tree( $markup, $expected ) {
+ $actual = build_visual_html_tree( $markup, '<body>' );
+ $this->assertSame( $expected, $actual );
+ }
+
+ public function data_build_equivalent_html_semantic_tree_with_equivalent_html() {
+ return array(
+ 'Different attribute order' => array(
+ '<img src="wp.png" alt="The WordPress logo">',
+ '<img alt="The WordPress logo" src="wp.png">',
+ ),
+ 'Different class name order' => array(
+ '<hr class="wp-block-separator is-style-default">',
+ '<hr class="is-style-default wp-block-separator">',
+ ),
+ 'Differences in style attribute whitespace and trailing semicolon' => array(
+ '<hr style="margin-top: 50px; margin-bottom: 50px;">',
+ '<hr style="margin-top:50px;margin-bottom: 50px">',
+ ),
+ 'Different block attribute order' => array(
+ '<!-- wp:separator {"className":"is-style-default","backgroundColor":"accent-1"} -->',
+ '<!-- wp:separator {"backgroundColor":"accent-1","className":"is-style-default"} -->',
+ ),
+ 'Different block class name order' => array(
+ '<!-- wp:separator {"className":"is-style-default has-custom-classname"} -->',
+ '<!-- wp:separator {"className":"has-custom-classname is-style-default"} -->',
+ ),
+ 'Different whitespace in block class name' => array(
+ '<!-- wp:separator {"className":"wp-block-separator is-style-default"} -->',
+ '<!-- wp:separator {"className":"wp-block-separator is-style-default "} -->',
+ ),
+ 'Duplicated block class names' => array(
+ '<!-- wp:separator {"className":"wp-block-separator is-style-default"} -->',
+ '<!-- wp:separator {"className":"wp-block-separator is-style-default wp-block-separator"} -->',
+ ),
+ 'Different Capitalization of tag' => array(
+ '<IMG src="wp.png" alt="The WordPress logo">',
+ '<img src="wp.png" alt="The WordPress logo">',
+ ),
+ );
+ }
+
+ /**
+ * @ticket 63527
+ *
+ * @covers ::build_visual_html_tree
+ *
+ * @dataProvider data_build_equivalent_html_semantic_tree_with_equivalent_html
+ */
+ public function test_build_equivalent_html_semantic_tree_with_equivalent_html( $expected, $actual ) {
+ $tree_expected = build_visual_html_tree( $expected, '<body>' );
+ $tree_actual = build_visual_html_tree( $actual, '<body>' );
+
+ $this->assertSame( $tree_expected, $tree_actual );
+ }
+
+ public function data_build_equivalent_html_semantic_tree_with_non_equivalent_html() {
+ return array(
+ 'Different attributes' => array(
+ '<img src="wp.png" alt="The WordPress logo">',
+ '<img alt="The WordPress logo" src="wp.png" title="WordPress">',
+ ),
+ 'Different class names' => array(
+ '<hr class="wp-block-separator is-style-default">',
+ '<hr class="is-style-default wp-block-hairline">',
+ ),
+ 'Different styles' => array(
+ '<hr style="margin-top: 50px; margin-bottom: 50px;">',
+ '<hr style="margin-top: 50px; margin-bottom: 100px">',
+ ),
+ 'Different comments' => array(
+ '<!-- abc -->',
+ '<!-- xyz -->',
+ ),
+ 'Semantically relevant whitespace' => array(
+ '<div style="color: rgb(50 139 31)">Test</div>',
+ '<div style="color:rgb(5013931)">Test</div>',
+ ),
+ );
+ }
+
+ /**
+ * @ticket 63527
+ *
+ * @covers ::build_visual_html_tree
+ *
+ * @dataProvider data_build_equivalent_html_semantic_tree_with_non_equivalent_html
+ */
+ public function test_build_equivalent_html_semantic_tree_with_non_equivalent_html( $expected, $actual ) {
+ $tree_expected = build_visual_html_tree( $expected, '<body>' );
+ $tree_actual = build_visual_html_tree( $actual, '<body>' );
+
+ $this->assertNotSame( $tree_expected, $tree_actual );
+ }
+}
diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php
index 2af822f456..be36ba4059 100644
--- a/tests/phpunit/tests/comment.php
+++ b/tests/phpunit/tests/comment.php
@@ -21,7 +21,7 @@ class Tests_Comment extends WP_UnitTestCase {
'role' => 'author',
'user_login' => 'test_wp_user_get',
'user_pass' => 'password',
- 'user_email' => 'test@test.com',
+ 'user_email' => 'author@example.com',
)
);
@@ -95,7 +95,7 @@ class Tests_Comment extends WP_UnitTestCase {
'comment_post_ID' => self::$post_id,
'comment_author' => 'Author',
'comment_author_url' => 'http://example.localhost/',
- 'comment_author_email' => 'test@test.com',
+ 'comment_author_email' => 'author@example.com',
'user_id' => $admin_id_1,
'comment_content' => 'This is a comment',
)
@@ -108,7 +108,7 @@ class Tests_Comment extends WP_UnitTestCase {
'role' => 'administrator',
'user_login' => 'test_wp_admin_get',
'user_pass' => 'password',
- 'user_email' => 'testadmin@test.com',
+ 'user_email' => 'testadmin@example.com',
)
);
@@ -139,7 +139,7 @@ class Tests_Comment extends WP_UnitTestCase {
'comment_post_ID' => self::$post_id,
'comment_author' => 'Author',
'comment_author_url' => 'http://example.localhost/',
- 'comment_author_email' => 'test@test.com',
+ 'comment_author_email' => 'author@example.com',
'user_id' => self::$user_id,
'comment_content' => '<a href="http://example.localhost/something.html">click</a>',
)
@@ -152,7 +152,7 @@ class Tests_Comment extends WP_UnitTestCase {
'role' => 'administrator',
'user_login' => 'test_wp_admin_get',
'user_pass' => 'password',
- 'user_email' => 'testadmin@test.com',
+ 'user_email' => 'testadmin@example.com',
)
);
@@ -1445,10 +1445,10 @@ class Tests_Comment extends WP_UnitTestCase {
// Post authors possibly notified when a comment is approved on their post.
wp_set_comment_status( $comment, 'approve' );
- // Check to see if a notification email was sent to the post author `test@test.com`.
+ // Check to see if a notification email was sent to the post author `author@example.com`.
if ( isset( $GLOBALS['phpmailer']->mock_sent )
&& ! empty( $GLOBALS['phpmailer']->mock_sent )
- && 'test@test.com' === $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0]
+ && 'author@example.com' === $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0]
) {
$email_sent_when_comment_approved = true;
} else {
@@ -1467,10 +1467,10 @@ class Tests_Comment extends WP_UnitTestCase {
);
wp_new_comment( $data );
- // Check to see if a notification email was sent to the post author `test@test.com`.
+ // Check to see if a notification email was sent to the post author `author@example.com`.
if ( isset( $GLOBALS['phpmailer']->mock_sent ) &&
! empty( $GLOBALS['phpmailer']->mock_sent ) &&
- 'test@test.com' === $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] ) {
+ 'author@example.com' === $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] ) {
$email_sent_when_comment_added = true;
reset_phpmailer_instance();
} else {
@@ -1566,6 +1566,8 @@ class Tests_Comment extends WP_UnitTestCase {
$lengths = wp_get_comment_fields_max_lengths();
+ $this->assertNotEmpty( $lengths );
+
foreach ( $lengths as $field => $length ) {
$this->assertSame( $expected[ $field ], $length );
}
diff --git a/tests/phpunit/tests/comment/commentForm.php b/tests/phpunit/tests/comment/commentForm.php
index 771cbc1b57..e3dab07e24 100644
--- a/tests/phpunit/tests/comment/commentForm.php
+++ b/tests/phpunit/tests/comment/commentForm.php
@@ -193,4 +193,38 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase {
$post_hidden_field = "<input type='hidden' name='comment_post_ID' value='{$post_id}' id='comment_post_ID' />";
$this->assertStringContainsString( $post_hidden_field, $form );
}
+
+ /**
+ * Tests novalidate attribute on the comment form.
+ *
+ * @ticket 47595
+ */
+ public function test_comment_form_and_novalidate_attribute() {
+ $post_id = self::$post_id;
+
+ // By default, the novalidate is not emitted.
+ $form = get_echo( 'comment_form', array( array(), $post_id ) );
+ $p = new WP_HTML_Tag_Processor( $form );
+ $this->assertTrue( $p->next_tag( array( 'tag_name' => 'FORM' ) ), 'Expected FORM tag.' );
+ $this->assertNull( $p->get_attribute( 'novalidate' ), 'Expected FORM to not have novalidate attribute by default.' );
+
+ // Opt in to the novalidate attribute by passing an arg to comment_form().
+ $form = get_echo( 'comment_form', array( array( 'novalidate' => true ), $post_id ) );
+ $p = new WP_HTML_Tag_Processor( $form );
+ $this->assertTrue( $p->next_tag( array( 'tag_name' => 'FORM' ) ), 'Expected FORM tag.' );
+ $this->assertTrue( $p->get_attribute( 'novalidate' ), 'Expected FORM to have the novalidate attribute.' );
+
+ // Opt in to the novalidate attribute via the comment_form_defaults filter.
+ add_filter(
+ 'comment_form_defaults',
+ static function ( array $defaults ): array {
+ $defaults['novalidate'] = true;
+ return $defaults;
+ }
+ );
+ $form = get_echo( 'comment_form', array( array(), $post_id ) );
+ $p = new WP_HTML_Tag_Processor( $form );
+ $this->assertTrue( $p->next_tag( array( 'tag_name' => 'FORM' ) ), 'Expected FORM tag.' );
+ $this->assertTrue( $p->get_attribute( 'novalidate' ), 'Expected FORM to have novalidate attribute.' );
+ }
}
diff --git a/tests/phpunit/tests/comment/commentsTemplate.php b/tests/phpunit/tests/comment/commentsTemplate.php
index 7fa51f79f9..bc260545ad 100644
--- a/tests/phpunit/tests/comment/commentsTemplate.php
+++ b/tests/phpunit/tests/comment/commentsTemplate.php
@@ -566,6 +566,9 @@ class Tests_Comment_CommentsTemplate extends WP_UnitTestCase {
// Find the comment permalinks.
preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p1, $matches );
+ $this->assertNotEmpty( $matches );
+ $this->assertNotEmpty( $matches[1] );
+
// This is the main post page, so we don't expect any cpage param.
foreach ( $matches[1] as $m ) {
$this->assertStringNotContainsString( 'cpage', $m );
@@ -586,6 +589,9 @@ class Tests_Comment_CommentsTemplate extends WP_UnitTestCase {
// Find the comment permalinks.
preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p2, $matches );
+ $this->assertNotEmpty( $matches );
+ $this->assertNotEmpty( $matches[1] );
+
// They should all be on page 2.
foreach ( $matches[1] as $m ) {
$this->assertStringContainsString( 'cpage=2', $m );
@@ -659,6 +665,9 @@ class Tests_Comment_CommentsTemplate extends WP_UnitTestCase {
// Find the comment permalinks.
preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p0, $matches );
+ $this->assertNotEmpty( $matches );
+ $this->assertNotEmpty( $matches[1] );
+
foreach ( $matches[1] as $m ) {
$this->assertStringContainsString( 'cpage=3', $m );
}
@@ -678,6 +687,9 @@ class Tests_Comment_CommentsTemplate extends WP_UnitTestCase {
// Find the comment permalinks.
preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p2, $matches );
+ $this->assertNotEmpty( $matches );
+ $this->assertNotEmpty( $matches[1] );
+
// They should all be on page 2.
foreach ( $matches[1] as $m ) {
$this->assertStringContainsString( 'cpage=2', $m );
@@ -699,6 +711,9 @@ class Tests_Comment_CommentsTemplate extends WP_UnitTestCase {
// Find the comment permalinks.
preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p1, $matches );
+ $this->assertNotEmpty( $matches );
+ $this->assertNotEmpty( $matches[1] );
+
// They should all be on page 2.
foreach ( $matches[1] as $m ) {
$this->assertStringContainsString( 'cpage=1', $m );
diff --git a/tests/phpunit/tests/comment/metaCache.php b/tests/phpunit/tests/comment/metaCache.php
index 75695ccfa9..92337d9571 100644
--- a/tests/phpunit/tests/comment/metaCache.php
+++ b/tests/phpunit/tests/comment/metaCache.php
@@ -195,23 +195,23 @@ class Tests_Comment_MetaCache extends WP_UnitTestCase {
$this->go_to( get_permalink( $p ) );
- if ( have_posts() ) {
- while ( have_posts() ) {
- the_post();
-
- // Load comments with `comments_template()`.
- $cform = get_echo( 'comments_template' );
-
- // First request will hit the database.
- $num_queries = get_num_queries();
- get_comment_meta( $comment_ids[0], 'sauce' );
- $this->assertSame( 1, get_num_queries() - $num_queries );
-
- // Second and third requests should be in cache.
- get_comment_meta( $comment_ids[1], 'sauce' );
- get_comment_meta( $comment_ids[2], 'sauce' );
- $this->assertSame( 1, get_num_queries() - $num_queries );
- }
+ $this->assertTrue( have_posts() );
+
+ while ( have_posts() ) {
+ the_post();
+
+ // Load comments with `comments_template()`.
+ $cform = get_echo( 'comments_template' );
+
+ // First request will hit the database.
+ $num_queries = get_num_queries();
+ get_comment_meta( $comment_ids[0], 'sauce' );
+ $this->assertSame( 1, get_num_queries() - $num_queries );
+
+ // Second and third requests should be in cache.
+ get_comment_meta( $comment_ids[1], 'sauce' );
+ get_comment_meta( $comment_ids[2], 'sauce' );
+ $this->assertSame( 1, get_num_queries() - $num_queries );
}
}
diff --git a/tests/phpunit/tests/comment/wpHandleCommentSubmission.php b/tests/phpunit/tests/comment/wpHandleCommentSubmission.php
index 49d54e3da5..3f2ba84194 100644
--- a/tests/phpunit/tests/comment/wpHandleCommentSubmission.php
+++ b/tests/phpunit/tests/comment/wpHandleCommentSubmission.php
@@ -9,6 +9,7 @@ class Tests_Comment_wpHandleCommentSubmission extends WP_UnitTestCase {
protected static $post;
protected static $author_id;
+ protected static $author_id2;
protected static $editor_id;
protected $preprocess_comment_data = array();
@@ -22,6 +23,13 @@ class Tests_Comment_wpHandleCommentSubmission extends WP_UnitTestCase {
)
);
+ self::$author_id2 = $factory->user->create(
+ array(
+ 'role' => 'author',
+ 'user_url' => 'http://user.example.org',
+ )
+ );
+
self::$editor_id = $factory->user->create(
array(
'role' => 'editor',
@@ -223,11 +231,7 @@ class Tests_Comment_wpHandleCommentSubmission extends WP_UnitTestCase {
public function test_submitting_valid_comment_as_logged_in_user_succeeds() {
- $user = self::factory()->user->create_and_get(
- array(
- 'user_url' => 'http://user.example.org',
- )
- );
+ $user = get_user_by( 'id', self::$author_id2 );
wp_set_current_user( $user->ID );
@@ -314,11 +318,7 @@ class Tests_Comment_wpHandleCommentSubmission extends WP_UnitTestCase {
$error = 'comment_id_not_found';
- $user = self::factory()->user->create_and_get(
- array(
- 'role' => 'author',
- )
- );
+ $user = get_user_by( 'id', self::$author_id2 );
wp_set_current_user( $user->ID );
@@ -343,11 +343,7 @@ class Tests_Comment_wpHandleCommentSubmission extends WP_UnitTestCase {
$error = 'comment_id_not_found';
- $user = self::factory()->user->create_and_get(
- array(
- 'role' => 'author',
- )
- );
+ $user = get_user_by( 'id', self::$author_id2 );
wp_set_current_user( $user->ID );
@@ -834,12 +830,8 @@ class Tests_Comment_wpHandleCommentSubmission extends WP_UnitTestCase {
/**
* @ticket 36901
*/
- public function test_comments_flood_user_is_admin() {
- $user = self::factory()->user->create_and_get(
- array(
- 'role' => 'administrator',
- )
- );
+ public function test_comments_flood_user_can_moderate_comments() {
+ $user = get_user_by( 'id', self::$editor_id );
wp_set_current_user( $user->ID );
$data = array(
@@ -853,8 +845,9 @@ class Tests_Comment_wpHandleCommentSubmission extends WP_UnitTestCase {
$data['comment'] = 'Wow! I am quick!';
$second_comment = wp_handle_comment_submission( $data );
- $this->assertNotWPError( $second_comment );
- $this->assertSame( (string) self::$post->ID, $second_comment->comment_post_ID );
+ $this->assertTrue( current_user_can( 'moderate_comments' ), 'Test user should have the moderate_comments capability' );
+ $this->assertNotWPError( $second_comment, 'Second comment should not trigger comment flooding error.' );
+ $this->assertSame( (string) self::$post->ID, $second_comment->comment_post_ID, 'Second comment should be made against initial post.' );
}
/**
diff --git a/tests/phpunit/tests/customize/control.php b/tests/phpunit/tests/customize/control.php
index d472eacad9..ea3311e15c 100644
--- a/tests/phpunit/tests/customize/control.php
+++ b/tests/phpunit/tests/customize/control.php
@@ -18,11 +18,27 @@ class Test_WP_Customize_Control extends WP_UnitTestCase {
public $wp_customize;
/**
+ * Shared user ID for the tests.
+
+ * @var int
+ */
+ public static $user_id = 0;
+
+ /**
+ * Set up shared fixtures.
+ *
+ * @param WP_UnitTest_Factory $factory Factory.
+ */
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$user_id = $factory->user->create( array( 'role' => 'administrator' ) );
+ }
+
+ /**
* Set up.
*/
public function set_up() {
parent::set_up();
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$user_id );
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
$GLOBALS['wp_customize'] = new WP_Customize_Manager();
$this->wp_customize = $GLOBALS['wp_customize'];
diff --git a/tests/phpunit/tests/customize/custom-css-setting.php b/tests/phpunit/tests/customize/custom-css-setting.php
index 015b6308af..65cc3f717f 100644
--- a/tests/phpunit/tests/customize/custom-css-setting.php
+++ b/tests/phpunit/tests/customize/custom-css-setting.php
@@ -23,24 +23,40 @@ class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase {
public $setting;
/**
- * Set up the test case.
+ * The user ID to use for the tests.
*
- * @see WP_UnitTestCase_Base::set_up()
+ * @var int
*/
- public function set_up() {
- parent::set_up();
- require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
+ public static $user_id = 0;
- $user_id = self::factory()->user->create(
+ /**
+ * Set up the test case.
+ *
+ * @see WP_UnitTestCase::set_up()
+ */
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ // Create a user to use for the tests.
+ self::$user_id = $factory->user->create(
array(
'role' => 'administrator',
)
);
+
if ( is_multisite() ) {
- grant_super_admin( $user_id );
+ grant_super_admin( self::$user_id );
}
+ }
+
+ /**
+ * Set up the test case.
+ *
+ * @see WP_UnitTestCase_Base::set_up()
+ */
+ public function set_up() {
+ parent::set_up();
+ require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
- wp_set_current_user( $user_id );
+ wp_set_current_user( self::$user_id );
global $wp_customize;
$this->wp_customize = new WP_Customize_Manager();
diff --git a/tests/phpunit/tests/customize/manager.php b/tests/phpunit/tests/customize/manager.php
index 0f8ddb2d9b..21bdd85eeb 100644
--- a/tests/phpunit/tests/customize/manager.php
+++ b/tests/phpunit/tests/customize/manager.php
@@ -27,6 +27,13 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
protected static $admin_user_id;
/**
+ * Second admin user ID.
+ *
+ * @var int
+ */
+ protected static $other_admin_user_id;
+
+ /**
* Subscriber user ID.
*
* @var int
@@ -46,8 +53,9 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
* @param WP_UnitTest_Factory $factory Factory.
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
- self::$subscriber_user_id = $factory->user->create( array( 'role' => 'subscriber' ) );
- self::$admin_user_id = $factory->user->create( array( 'role' => 'administrator' ) );
+ self::$subscriber_user_id = $factory->user->create( array( 'role' => 'subscriber' ) );
+ self::$admin_user_id = $factory->user->create( array( 'role' => 'administrator' ) );
+ self::$other_admin_user_id = $factory->user->create( array( 'role' => 'administrator' ) );
}
/**
@@ -153,7 +161,7 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
*/
public function test_constructor_deferred_changeset_uuid() {
wp_set_current_user( self::$admin_user_id );
- $other_admin_user_id = self::factory()->user->create( array( 'role' => 'admin' ) );
+ $other_admin_user_id = self::$other_admin_user_id;
$data = array(
'blogname' => array(
@@ -1254,7 +1262,7 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
*/
public function test_save_changeset_post_without_kses_corrupting_json() {
global $wp_customize;
- $lesser_admin_user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
+ $lesser_admin_user_id = self::$other_admin_user_id;
$uuid = wp_generate_uuid4();
$wp_customize = new WP_Customize_Manager(
@@ -1499,7 +1507,7 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
add_theme_support( 'custom-background' );
wp_set_current_user( self::$admin_user_id );
- $other_admin_user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
+ $other_admin_user_id = self::$other_admin_user_id;
$uuid = wp_generate_uuid4();
$wp_customize = $this->create_test_manager( $uuid );
@@ -1714,7 +1722,7 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
add_theme_support( 'custom-background' );
wp_set_current_user( self::$admin_user_id );
- $other_admin_user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
+ $other_admin_user_id = self::$other_admin_user_id;
$uuid = wp_generate_uuid4();
$wp_customize = $this->create_test_manager( $uuid );
@@ -1877,7 +1885,7 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
$r = $wp_customize->save_changeset_post(
array(
'autosave' => true,
- 'user_id' => self::factory()->user->create( array( 'role' => 'administrator' ) ),
+ 'user_id' => self::$other_admin_user_id,
)
);
$this->assertSame( 'illegal_autosave_with_non_current_user', $r->get_error_code() );
@@ -3009,7 +3017,7 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
* @see WP_Customize_Manager::set_return_url()
*/
public function test_return_url() {
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'author' ) ) );
+ wp_set_current_user( self::$subscriber_user_id );
$this->assertSame( home_url( '/' ), $this->manager->get_return_url() );
wp_set_current_user( self::$admin_user_id );
diff --git a/tests/phpunit/tests/customize/nav-menu-item-setting.php b/tests/phpunit/tests/customize/nav-menu-item-setting.php
index 22d4bc7de7..eb5aa46e89 100644
--- a/tests/phpunit/tests/customize/nav-menu-item-setting.php
+++ b/tests/phpunit/tests/customize/nav-menu-item-setting.php
@@ -14,6 +14,22 @@ class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase {
public $wp_customize;
/**
+ * ID of the administrator user.
+ *
+ * @var int
+ */
+ public static $administrator_id;
+
+ /**
+ * Set up the shared fixture.
+ *
+ * @param WP_UnitTest_Factory $factory Factory instance.
+ */
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$administrator_id = $factory->user->create( array( 'role' => 'administrator' ) );
+ }
+
+ /**
* Set up a test case.
*
* @see WP_UnitTestCase_Base::set_up()
@@ -21,7 +37,7 @@ class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase {
public function set_up() {
parent::set_up();
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$administrator_id );
global $wp_customize;
$this->wp_customize = new WP_Customize_Manager();
diff --git a/tests/phpunit/tests/customize/nav-menu-setting.php b/tests/phpunit/tests/customize/nav-menu-setting.php
index 6d50c96ee0..1348c86395 100644
--- a/tests/phpunit/tests/customize/nav-menu-setting.php
+++ b/tests/phpunit/tests/customize/nav-menu-setting.php
@@ -15,6 +15,22 @@ class Test_WP_Customize_Nav_Menu_Setting extends WP_UnitTestCase {
public $wp_customize;
/**
+ * ID of the administrator user.
+ *
+ * @var int
+ */
+ public static $administrator_id;
+
+ /**
+ * Set up the shared fixture.
+ *
+ * @param WP_UnitTest_Factory $factory Factory instance.
+ */
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$administrator_id = $factory->user->create( array( 'role' => 'administrator' ) );
+ }
+
+ /**
* Set up a test case.
*
* @see WP_UnitTestCase_Base::set_up()
@@ -22,7 +38,7 @@ class Test_WP_Customize_Nav_Menu_Setting extends WP_UnitTestCase {
public function set_up() {
parent::set_up();
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$administrator_id );
global $wp_customize;
$this->wp_customize = new WP_Customize_Manager();
diff --git a/tests/phpunit/tests/customize/nav-menus.php b/tests/phpunit/tests/customize/nav-menus.php
index e731774693..e25418242a 100644
--- a/tests/phpunit/tests/customize/nav-menus.php
+++ b/tests/phpunit/tests/customize/nav-menus.php
@@ -15,6 +15,22 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
public $wp_customize;
/**
+ * ID of the administrator user.
+ *
+ * @var int
+ */
+ public static $administrator_id;
+
+ /**
+ * Set up the shared fixture.
+ *
+ * @param WP_UnitTest_Factory $factory Factory instance.
+ */
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$administrator_id = $factory->user->create( array( 'role' => 'administrator' ) );
+ }
+
+ /**
* Set up a test case.
*
* @see WP_UnitTestCase_Base::set_up()
@@ -22,7 +38,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
public function set_up() {
parent::set_up();
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$administrator_id );
global $wp_customize;
$this->wp_customize = new WP_Customize_Manager();
$wp_customize = $this->wp_customize;
@@ -766,25 +782,27 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
$this->assertStringContainsString( $expected, $template );
$post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'object' );
- if ( $post_types ) {
- foreach ( $post_types as $type ) {
- $this->assertStringContainsString( 'available-menu-items-post_type-' . esc_attr( $type->name ), $template );
- $this->assertMatchesRegularExpression( '#<h4 class="accordion-section-title".*>\s*<button type="button" class="accordion-trigger" aria-expanded="false" aria-controls=".*">\s*' . esc_html( $type->labels->name ) . '#', $template );
- $this->assertStringContainsString( 'data-type="post_type"', $template );
- $this->assertStringContainsString( 'data-object="' . esc_attr( $type->name ) . '"', $template );
- $this->assertStringContainsString( 'data-type_label="' . esc_attr( $type->labels->singular_name ) . '"', $template );
- }
+
+ $this->assertNotEmpty( $post_types );
+
+ foreach ( $post_types as $type ) {
+ $this->assertStringContainsString( 'available-menu-items-post_type-' . esc_attr( $type->name ), $template );
+ $this->assertMatchesRegularExpression( '#<h4 class="accordion-section-title".*>\s*<button type="button" class="accordion-trigger" aria-expanded="false" aria-controls=".*">\s*' . esc_html( $type->labels->name ) . '#', $template );
+ $this->assertStringContainsString( 'data-type="post_type"', $template );
+ $this->assertStringContainsString( 'data-object="' . esc_attr( $type->name ) . '"', $template );
+ $this->assertStringContainsString( 'data-type_label="' . esc_attr( $type->labels->singular_name ) . '"', $template );
}
$taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'object' );
- if ( $taxonomies ) {
- foreach ( $taxonomies as $tax ) {
- $this->assertStringContainsString( 'available-menu-items-taxonomy-' . esc_attr( $tax->name ), $template );
- $this->assertMatchesRegularExpression( '#<h4 class="accordion-section-title".*>\s*<button type="button" class="accordion-trigger" aria-expanded="false" aria-controls=".*">\s*' . esc_html( $tax->labels->name ) . '#', $template );
- $this->assertStringContainsString( 'data-type="taxonomy"', $template );
- $this->assertStringContainsString( 'data-object="' . esc_attr( $tax->name ) . '"', $template );
- $this->assertStringContainsString( 'data-type_label="' . esc_attr( $tax->labels->singular_name ) . '"', $template );
- }
+
+ $this->assertNotEmpty( $taxonomies );
+
+ foreach ( $taxonomies as $tax ) {
+ $this->assertStringContainsString( 'available-menu-items-taxonomy-' . esc_attr( $tax->name ), $template );
+ $this->assertMatchesRegularExpression( '#<h4 class="accordion-section-title".*>\s*<button type="button" class="accordion-trigger" aria-expanded="false" aria-controls=".*">\s*' . esc_html( $tax->labels->name ) . '#', $template );
+ $this->assertStringContainsString( 'data-type="taxonomy"', $template );
+ $this->assertStringContainsString( 'data-object="' . esc_attr( $tax->name ) . '"', $template );
+ $this->assertStringContainsString( 'data-type_label="' . esc_attr( $tax->labels->singular_name ) . '"', $template );
}
$this->assertStringContainsString( 'available-menu-items-custom_type', $template );
@@ -852,7 +870,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
$menus = new WP_Customize_Nav_Menus( $this->wp_customize );
$contributor_user_id = self::factory()->user->create( array( 'role' => 'contributor' ) );
$author_user_id = self::factory()->user->create( array( 'role' => 'author' ) );
- $administrator_user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
+ $administrator_user_id = self::$administrator_id;
$contributor_post_id = self::factory()->post->create(
array(
diff --git a/tests/phpunit/tests/customize/panel.php b/tests/phpunit/tests/customize/panel.php
index 40f198675a..bd191e65d7 100644
--- a/tests/phpunit/tests/customize/panel.php
+++ b/tests/phpunit/tests/customize/panel.php
@@ -8,6 +8,22 @@
class Tests_WP_Customize_Panel extends WP_UnitTestCase {
/**
+ * ID of the administrator user.
+ *
+ * @var int
+ */
+ public static $administrator_id;
+
+ /**
+ * Set up the shared fixture.
+ *
+ * @param WP_UnitTest_Factory $factory Factory instance.
+ */
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$administrator_id = $factory->user->create( array( 'role' => 'administrator' ) );
+ }
+
+ /**
* @var WP_Customize_Manager
*/
protected $manager;
@@ -131,8 +147,7 @@ class Tests_WP_Customize_Panel extends WP_UnitTestCase {
* @see WP_Customize_Panel::check_capabilities()
*/
public function test_check_capabilities() {
- $user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
- wp_set_current_user( $user_id );
+ wp_set_current_user( self::$administrator_id );
$panel = new WP_Customize_Panel( $this->manager, 'foo' );
$this->assertTrue( $panel->check_capabilities() );
@@ -157,7 +172,7 @@ class Tests_WP_Customize_Panel extends WP_UnitTestCase {
* @see WP_Customize_Panel::maybe_render()
*/
public function test_maybe_render() {
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$administrator_id );
$panel = new WP_Customize_Panel( $this->manager, 'bar' );
$customize_render_panel_count = did_action( 'customize_render_panel' );
add_action( 'customize_render_panel', array( $this, 'action_customize_render_panel_test' ) );
@@ -182,7 +197,7 @@ class Tests_WP_Customize_Panel extends WP_UnitTestCase {
* @see WP_Customize_Panel::print_template()
*/
public function test_print_templates_standard() {
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$administrator_id );
$panel = new WP_Customize_Panel( $this->manager, 'baz' );
ob_start();
@@ -200,7 +215,7 @@ class Tests_WP_Customize_Panel extends WP_UnitTestCase {
* @see WP_Customize_Panel::print_template()
*/
public function test_print_templates_custom() {
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$administrator_id );
$panel = new Custom_Panel_Test( $this->manager, 'baz' );
ob_start();
diff --git a/tests/phpunit/tests/customize/selective-refresh-ajax.php b/tests/phpunit/tests/customize/selective-refresh-ajax.php
index 39d3d45a70..8d9305962b 100644
--- a/tests/phpunit/tests/customize/selective-refresh-ajax.php
+++ b/tests/phpunit/tests/customize/selective-refresh-ajax.php
@@ -29,6 +29,22 @@ class Test_WP_Customize_Selective_Refresh_Ajax extends WP_UnitTestCase {
public $selective_refresh;
/**
+ * Shared user ID for the tests.
+ *
+ * @var int
+ */
+ public static $user_id = 0;
+
+ /**
+ * Set up shared fixtures.
+ *
+ * @param WP_UnitTest_Factory $factory Factory.
+ */
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$user_id = $factory->user->create( array( 'role' => 'administrator' ) );
+ }
+
+ /**
* Set up the test fixture.
*/
public function set_up() {
@@ -79,7 +95,7 @@ class Test_WP_Customize_Selective_Refresh_Ajax extends WP_UnitTestCase {
$this->assertSame( 'expected_customize_preview', $output['data'] );
// Check expected_customize_preview.
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$user_id );
$_REQUEST['nonce'] = wp_create_nonce( 'preview-customize_' . $this->wp_customize->theme()->get_stylesheet() );
ob_start();
try {
@@ -121,7 +137,7 @@ class Test_WP_Customize_Selective_Refresh_Ajax extends WP_UnitTestCase {
* Set the current user to be an admin, add the preview nonce, and set the query var.
*/
private function setup_valid_render_partials_request_environment() {
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$user_id );
$_REQUEST['nonce'] = wp_create_nonce( 'preview-customize_' . $this->wp_customize->theme()->get_stylesheet() );
$_POST[ WP_Customize_Selective_Refresh::RENDER_QUERY_VAR ] = '1';
$this->do_customize_boot_actions();
@@ -171,7 +187,7 @@ class Test_WP_Customize_Selective_Refresh_Ajax extends WP_UnitTestCase {
*/
public function test_handle_render_partials_request_for_non_rendering_partial() {
$this->setup_valid_render_partials_request_environment();
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$user_id );
$this->wp_customize->add_setting( 'home' );
$this->wp_customize->selective_refresh->add_partial( 'foo', array( 'settings' => array( 'home' ) ) );
$context_data = array();
@@ -208,7 +224,7 @@ class Test_WP_Customize_Selective_Refresh_Ajax extends WP_UnitTestCase {
*/
public function test_handle_rendering_disallowed_partial() {
$this->setup_valid_render_partials_request_environment();
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$user_id );
$this->wp_customize->add_setting(
'secret_message',
array(
@@ -244,7 +260,7 @@ class Test_WP_Customize_Selective_Refresh_Ajax extends WP_UnitTestCase {
*/
public function test_handle_rendering_partial_with_missing_settings() {
$this->setup_valid_render_partials_request_environment();
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$user_id );
$this->wp_customize->selective_refresh->add_partial( 'bar', array( 'settings' => 'bar' ) );
$context_data = array();
diff --git a/tests/phpunit/tests/customize/setting.php b/tests/phpunit/tests/customize/setting.php
index 62861a2726..8150a2f03d 100644
--- a/tests/phpunit/tests/customize/setting.php
+++ b/tests/phpunit/tests/customize/setting.php
@@ -17,6 +17,22 @@ class Tests_WP_Customize_Setting extends WP_UnitTestCase {
*/
public $undefined;
+ /**
+ * Shared admin user ID for the tests.
+ *
+ * @var int
+ */
+ public static $admin_id = 0;
+
+ /**
+ * Set up shared fixtures.
+ *
+ * @param WP_UnitTest_Factory $factory Factory.
+ */
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
+ }
+
public function set_up() {
parent::set_up();
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
@@ -137,7 +153,7 @@ class Tests_WP_Customize_Setting extends WP_UnitTestCase {
* @see WP_Customize_Setting::value()
*/
public function test_preview_standard_types_non_multidimensional() {
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$admin_id );
$_POST['customized'] = wp_slash( wp_json_encode( $this->post_data_overrides ) );
// Try non-multidimensional settings.
@@ -216,7 +232,7 @@ class Tests_WP_Customize_Setting extends WP_UnitTestCase {
* @see WP_Customize_Setting::value()
*/
public function test_preview_standard_types_multidimensional() {
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$admin_id );
$_POST['customized'] = wp_slash( wp_json_encode( $this->post_data_overrides ) );
foreach ( $this->standard_type_configs as $type => $type_options ) {
@@ -362,7 +378,7 @@ class Tests_WP_Customize_Setting extends WP_UnitTestCase {
* @see WP_Customize_Setting::preview()
*/
public function test_preview_custom_type() {
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$admin_id );
$type = 'custom_type';
$post_data_overrides = array(
"unset_{$type}_with_post_value" => "unset_{$type}_without_post_value\\o/",
@@ -513,7 +529,7 @@ class Tests_WP_Customize_Setting extends WP_UnitTestCase {
$this->assertTrue( 0 === did_action( 'customize_save_foo' ) );
// Satisfy all requirements for save to happen.
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$admin_id );
$this->assertNotFalse( $setting->save() );
$this->assertTrue( 1 === did_action( 'customize_update_custom' ) );
$this->assertTrue( 1 === did_action( 'customize_save_foo' ) );
@@ -551,7 +567,7 @@ class Tests_WP_Customize_Setting extends WP_UnitTestCase {
* @ticket 31428
*/
public function test_is_current_blog_previewed() {
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$admin_id );
$type = 'option';
$name = 'blogname';
$post_value = __FUNCTION__;
@@ -573,7 +589,7 @@ class Tests_WP_Customize_Setting extends WP_UnitTestCase {
* @group ms-required
*/
public function test_previewing_with_switch_to_blog() {
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$admin_id );
$type = 'option';
$name = 'blogdescription';
$post_value = __FUNCTION__;
@@ -596,7 +612,7 @@ class Tests_WP_Customize_Setting extends WP_UnitTestCase {
*/
public function test_option_autoloading() {
global $wpdb;
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$admin_id );
$name = 'autoloaded1';
$setting = new WP_Customize_Setting(
@@ -743,7 +759,7 @@ class Tests_WP_Customize_Setting extends WP_UnitTestCase {
* @ticket 37294
*/
public function test_multidimensional_value_when_previewed() {
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$admin_id );
WP_Customize_Setting::reset_aggregated_multidimensionals();
$initial_value = 456;
diff --git a/tests/phpunit/tests/customize/widgets.php b/tests/phpunit/tests/customize/widgets.php
index 77b1419440..5b64f85ce3 100644
--- a/tests/phpunit/tests/customize/widgets.php
+++ b/tests/phpunit/tests/customize/widgets.php
@@ -20,6 +20,22 @@ class Tests_WP_Customize_Widgets extends WP_UnitTestCase {
*/
protected $backup_registered_sidebars;
+ /**
+ * ID of the administrator user.
+ *
+ * @var int
+ */
+ public static $administrator_id;
+
+ /**
+ * Set up the shared fixture.
+ *
+ * @param WP_UnitTest_Factory $factory Factory instance.
+ */
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$administrator_id = $factory->user->create( array( 'role' => 'administrator' ) );
+ }
+
public function set_up() {
parent::set_up();
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
@@ -27,8 +43,7 @@ class Tests_WP_Customize_Widgets extends WP_UnitTestCase {
add_theme_support( 'customize-selective-refresh-widgets' );
add_action( 'widgets_init', array( $this, 'remove_widgets_block_editor' ) );
- $user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
- wp_set_current_user( $user_id );
+ wp_set_current_user( self::$administrator_id );
update_option(
'widget_search',
@@ -857,4 +872,42 @@ class Tests_WP_Customize_Widgets extends WP_UnitTestCase {
$this->manager->widgets->prepreview_added_widget_instance();
$this->manager->widgets->remove_prepreview_filters();
}
+
+ /**
+ * Test that output_widget_control_templates() works without sidebars.
+ * This test verifies that the fix for accessing panel title works correctly
+ * when no sidebars are registered or the widgets panel doesn't exist.
+ *
+ * @ticket 63151
+ *
+ * @covers WP_Customize_Widgets::output_widget_control_templates
+ */
+ public function test_output_widget_control_templates_without_sidebars() {
+ global $wp_registered_sidebars;
+
+ $original_sidebars = $wp_registered_sidebars;
+ $wp_registered_sidebars = array();
+ $manager = new WP_Customize_Manager();
+ $widgets = new WP_Customize_Widgets( $manager );
+
+ if ( $manager->get_panel( 'widgets' ) ) {
+ $manager->remove_panel( 'widgets' );
+ }
+
+ ob_start();
+
+ $widgets->output_widget_control_templates();
+
+ $output = ob_get_clean();
+ $wp_registered_sidebars = $original_sidebars;
+
+ $this->assertStringNotContainsString( 'Warning', $output, 'Failed asserting that the output does not contain "Warning".' );
+ $this->assertStringNotContainsString( 'Notice', $output, 'Failed asserting that the output does not contain "Notice".' );
+ $this->assertStringNotContainsString( 'Error', $output, 'Failed asserting that the output does not contain "Error".' );
+
+ // Check that the output contains expected widget controls HTML.
+ $this->assertStringContainsString( 'id="widgets-left"', $output, 'Failed asserting that the output contains "id=widgets-left".' );
+ $this->assertStringContainsString( 'id="available-widgets"', $output, 'Failed asserting that the output contains "id=available-widgets".' );
+ $this->assertStringNotContainsString( 'id="accordion-panel-widgets"', $output, 'Failed asserting that the output does not contain "id=accordion-panel-widgets".' );
+ }
}
diff --git a/tests/phpunit/tests/db/charset.php b/tests/phpunit/tests/db/charset.php
index 26074e74b8..8b3fdb2f10 100644
--- a/tests/phpunit/tests/db/charset.php
+++ b/tests/phpunit/tests/db/charset.php
@@ -829,6 +829,8 @@ class Tests_DB_Charset extends WP_UnitTestCase {
self::$_wpdb->query( $drop );
self::$_wpdb->query( $create );
+ $this->assertNotEmpty( $expected_charset );
+
foreach ( $expected_charset as $column => $charset ) {
if ( self::$utf8_is_utf8mb3 && 'utf8' === $charset ) {
$charset = 'utf8mb3';
@@ -854,6 +856,8 @@ class Tests_DB_Charset extends WP_UnitTestCase {
self::$_wpdb->query( $create );
+ $this->assertNotEmpty( $columns );
+
$columns = array_keys( $columns );
foreach ( $columns as $column => $charset ) {
$this->assertFalse( self::$_wpdb->get_col_charset( $table, $column ) );
@@ -877,6 +881,8 @@ class Tests_DB_Charset extends WP_UnitTestCase {
self::$_wpdb->query( $create );
+ $this->assertNotEmpty( $columns );
+
$columns = array_keys( $columns );
foreach ( $columns as $column => $charset ) {
$this->assertFalse( self::$_wpdb->get_col_charset( $table, $column ) );
@@ -1070,6 +1076,8 @@ class Tests_DB_Charset extends WP_UnitTestCase {
)
);
+ $this->assertNotEmpty( $always_true );
+
foreach ( $always_true as $true_query ) {
$return = self::$_wpdb->check_safe_collation( $true_query );
$this->assertTrue(
diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php
index 89bfb4ef92..d00c77b5a1 100644
--- a/tests/phpunit/tests/dependencies/scripts.php
+++ b/tests/phpunit/tests/dependencies/scripts.php
@@ -79,7 +79,7 @@ JS;
$expected .= "<script type='text/javascript' src='http://example.com?ver=1.2' id='empty-deps-version-js'></script>\n";
$expected .= "<script type='text/javascript' src='http://example.com' id='empty-deps-null-version-js'></script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
// No scripts left to print.
$this->assertSame( '', get_echo( 'wp_print_scripts' ) );
@@ -125,7 +125,7 @@ JS;
'id' => 'ms-isa-1-js-after',
)
);
- $this->assertEqualMarkup( $expected, $output, 'Inline scripts in the "after" position, that are attached to a deferred main script, are failing to print/execute.' );
+ $this->assertEqualHTML( $expected, $output, '<body>', 'Inline scripts in the "after" position, that are attached to a deferred main script, are failing to print/execute.' );
}
/**
@@ -154,7 +154,7 @@ JS;
)
);
- $this->assertEqualMarkup( $expected, $output, 'Inline scripts in the "after" position, that are attached to a blocking main script, are failing to print/execute.' );
+ $this->assertEqualHTML( $expected, $output, '<body>', 'Inline scripts in the "after" position, that are attached to a blocking main script, are failing to print/execute.' );
}
/**
@@ -187,9 +187,9 @@ JS;
'id' => 'ds-i1-1-js-before',
)
);
- $expected .= "<script type='text/javascript' src='http://example.org/ds-i1-1.js' id='ds-i1-1-js' $strategy data-wp-strategy='{$strategy}'></script>\n";
- $expected .= "<script type='text/javascript' src='http://example.org/ds-i1-2.js' id='ds-i1-2-js' $strategy data-wp-strategy='{$strategy}'></script>\n";
- $expected .= "<script type='text/javascript' src='http://example.org/ds-i1-3.js' id='ds-i1-3-js' $strategy data-wp-strategy='{$strategy}'></script>\n";
+ $expected .= "<script type='text/javascript' src='http://example.org/ds-i1-1.js' id='ds-i1-1-js' {$strategy}='{$strategy}' data-wp-strategy='{$strategy}'></script>\n";
+ $expected .= "<script type='text/javascript' src='http://example.org/ds-i1-2.js' id='ds-i1-2-js' {$strategy}='{$strategy}' data-wp-strategy='{$strategy}'></script>\n";
+ $expected .= "<script type='text/javascript' src='http://example.org/ds-i1-3.js' id='ds-i1-3-js' {$strategy}='{$strategy}' data-wp-strategy='{$strategy}'></script>\n";
$expected .= wp_get_inline_script_tag(
'console.log("before last");',
array(
@@ -197,9 +197,9 @@ JS;
'type' => 'text/javascript',
)
);
- $expected .= "<script type='text/javascript' src='http://example.org/ms-i1-1.js' id='ms-i1-1-js' {$strategy} data-wp-strategy='{$strategy}'></script>\n";
+ $expected .= "<script type='text/javascript' src='http://example.org/ms-i1-1.js' id='ms-i1-1-js' {$strategy}='{$strategy}' data-wp-strategy='{$strategy}'></script>\n";
- $this->assertEqualMarkup( $expected, $output, 'Inline scripts in the "before" position, that are attached to a deferred main script, are failing to print/execute.' );
+ $this->assertEqualHTML( $expected, $output, '<body>', 'Inline scripts in the "before" position, that are attached to a deferred main script, are failing to print/execute.' );
}
/**
@@ -216,8 +216,8 @@ JS;
// No dependents, No dependencies then async.
wp_enqueue_script( 'main-script-a1', '/main-script-a1.js', array(), null, array( 'strategy' => 'async' ) );
$output = get_echo( 'wp_print_scripts' );
- $expected = "<script type='text/javascript' src='/main-script-a1.js' id='main-script-a1-js' async data-wp-strategy='async'></script>\n";
- $this->assertEqualMarkup( $expected, $output, 'Scripts enqueued with an async loading strategy are failing to have the async attribute applied to the script handle when being printed.' );
+ $expected = "<script type='text/javascript' src='/main-script-a1.js' id='main-script-a1-js' async='async' data-wp-strategy='async'></script>\n";
+ $this->assertEqualHTML( $expected, $output, '<body>', 'Scripts enqueued with an async loading strategy are failing to have the async attribute applied to the script handle when being printed.' );
}
/**
@@ -238,9 +238,9 @@ JS;
wp_enqueue_script( 'dependency-script-a2', '/dependency-script-a2.js', array(), null );
wp_enqueue_script( 'main-script-a2', '/main-script-a2.js', array( 'dependency-script-a2' ), null, compact( 'strategy' ) );
$output = get_echo( 'wp_print_scripts' );
- $expected = "<script id='dependency-script-a2-js' src='/dependency-script-a2.js'></script>\n";
- $expected .= "<script type='text/javascript' src='/main-script-a2.js' id='main-script-a2-js' {$strategy} data-wp-strategy='{$strategy}'></script>";
- $this->assertEqualMarkup( $expected, $output, 'Dependents of a blocking dependency are free to have any strategy.' );
+ $expected = "<script id='dependency-script-a2-js' src='/dependency-script-a2.js' type='text/javascript'></script>\n";
+ $expected .= "<script type='text/javascript' src='/main-script-a2.js' id='main-script-a2-js' {$strategy}='{$strategy}' data-wp-strategy='{$strategy}'></script>";
+ $this->assertEqualHTML( $expected, $output, '<body>', 'Dependents of a blocking dependency are free to have any strategy.' );
}
/**
@@ -264,7 +264,7 @@ JS;
<script type='text/javascript' src='/main-script-a3.js' id='main-script-a3-js' data-wp-strategy='{$strategy}'></script>
<script id="dependent-script-a3-js" src="/dependent-script-a3.js" type="text/javascript"></script>
JS;
- $this->assertEqualMarkup( $expected, $output, 'Blocking dependents must force delayed dependencies to become blocking.' );
+ $this->assertEqualHTML( $expected, $output, '<body>', 'Blocking dependents must force delayed dependencies to become blocking.' );
}
/**
@@ -511,18 +511,26 @@ JS;
},
'expected_markup' => <<<HTML
<script id="blocking-not-async-without-dependency-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "blocking-not-async-without-dependency: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=blocking-not-async-without-dependency:%20script' id='blocking-not-async-without-dependency-js'></script>
<script id="blocking-not-async-without-dependency-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "blocking-not-async-without-dependency: after inline" )
+/* ]]> */
</script>
<script id="async-with-blocking-dependency-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "async-with-blocking-dependency: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=async-with-blocking-dependency:%20script' id='async-with-blocking-dependency-js' data-wp-strategy='async'></script>
<script id="async-with-blocking-dependency-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "async-with-blocking-dependency: after inline" )
+/* ]]> */
</script>
HTML
,
@@ -548,25 +556,37 @@ HTML
},
'expected_markup' => <<<HTML
<script id="async-no-dependency-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "async-no-dependency: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=async-no-dependency:%20script' id='async-no-dependency-js' data-wp-strategy='async'></script>
<script id="async-no-dependency-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "async-no-dependency: after inline" )
+/* ]]> */
</script>
<script id="async-one-async-dependency-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "async-one-async-dependency: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=async-one-async-dependency:%20script' id='async-one-async-dependency-js' data-wp-strategy='async'></script>
<script id="async-one-async-dependency-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "async-one-async-dependency: after inline" )
+/* ]]> */
</script>
<script id="async-two-async-dependencies-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "async-two-async-dependencies: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=async-two-async-dependencies:%20script' id='async-two-async-dependencies-js' data-wp-strategy='async'></script>
<script id="async-two-async-dependencies-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "async-two-async-dependencies: after inline" )
+/* ]]> */
</script>
HTML
,
@@ -584,18 +604,26 @@ HTML
},
'expected_markup' => <<<HTML
<script id="async-with-blocking-dependent-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "async-with-blocking-dependent: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=async-with-blocking-dependent:%20script' id='async-with-blocking-dependent-js' data-wp-strategy='async'></script>
<script id="async-with-blocking-dependent-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "async-with-blocking-dependent: after inline" )
+/* ]]> */
</script>
<script id="blocking-dependent-of-async-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "blocking-dependent-of-async: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=blocking-dependent-of-async:%20script' id='blocking-dependent-of-async-js'></script>
<script id="blocking-dependent-of-async-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "blocking-dependent-of-async: after inline" )
+/* ]]> */
</script>
HTML
,
@@ -613,18 +641,26 @@ HTML
},
'expected_markup' => <<<HTML
<script id="async-with-defer-dependent-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "async-with-defer-dependent: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=async-with-defer-dependent:%20script' id='async-with-defer-dependent-js' data-wp-strategy='async'></script>
<script id="async-with-defer-dependent-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "async-with-defer-dependent: after inline" )
+/* ]]> */
</script>
<script id="defer-dependent-of-async-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-dependent-of-async: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=defer-dependent-of-async:%20script' id='defer-dependent-of-async-js' data-wp-strategy='defer'></script>
<script id="defer-dependent-of-async-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-dependent-of-async: after inline" )
+/* ]]> */
</script>
HTML
,
@@ -645,17 +681,25 @@ HTML
},
'expected_markup' => <<<HTML
<script id="blocking-bundle-of-none-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "blocking-bundle-of-none: before inline" )
+/* ]]> */
</script>
<script id="blocking-bundle-of-none-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "blocking-bundle-of-none: after inline" )
+/* ]]> */
</script>
<script id="defer-dependent-of-blocking-bundle-of-none-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-dependent-of-blocking-bundle-of-none: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=defer-dependent-of-blocking-bundle-of-none:%20script' id='defer-dependent-of-blocking-bundle-of-none-js' data-wp-strategy='defer'></script>
<script id="defer-dependent-of-blocking-bundle-of-none-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-dependent-of-blocking-bundle-of-none: after inline" )
+/* ]]> */
</script>
HTML
,
@@ -679,25 +723,37 @@ HTML
},
'expected_markup' => <<<HTML
<script id="blocking-bundle-member-one-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "blocking-bundle-member-one: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=blocking-bundle-member-one:%20script' id='blocking-bundle-member-one-js'></script>
<script id="blocking-bundle-member-one-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "blocking-bundle-member-one: after inline" )
+/* ]]> */
</script>
<script id="blocking-bundle-member-two-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "blocking-bundle-member-two: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=blocking-bundle-member-two:%20script' id='blocking-bundle-member-two-js'></script>
<script id="blocking-bundle-member-two-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "blocking-bundle-member-two: after inline" )
+/* ]]> */
</script>
<script id="defer-dependent-of-blocking-bundle-of-two-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-dependent-of-blocking-bundle-of-two: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=defer-dependent-of-blocking-bundle-of-two:%20script' id='defer-dependent-of-blocking-bundle-of-two-js' data-wp-strategy='defer'></script>
<script id="defer-dependent-of-blocking-bundle-of-two-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-dependent-of-blocking-bundle-of-two: after inline" )
+/* ]]> */
</script>
HTML
,
@@ -720,17 +776,25 @@ HTML
},
'expected_markup' => <<<HTML
<script id="defer-bundle-of-none-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-bundle-of-none: before inline" )
+/* ]]> */
</script>
<script id="defer-bundle-of-none-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-bundle-of-none: after inline" )
+/* ]]> */
</script>
<script id="defer-dependent-of-defer-bundle-of-none-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-dependent-of-defer-bundle-of-none: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=defer-dependent-of-defer-bundle-of-none:%20script' id='defer-dependent-of-defer-bundle-of-none-js' data-wp-strategy='defer'></script>
<script id="defer-dependent-of-defer-bundle-of-none-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-dependent-of-defer-bundle-of-none: after inline" )
+/* ]]> */
</script>
HTML
,
@@ -751,25 +815,37 @@ HTML
},
'expected_markup' => <<<HTML
<script id="blocking-dependency-with-defer-following-dependency-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "blocking-dependency-with-defer-following-dependency: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=blocking-dependency-with-defer-following-dependency:%20script' id='blocking-dependency-with-defer-following-dependency-js'></script>
<script id="blocking-dependency-with-defer-following-dependency-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "blocking-dependency-with-defer-following-dependency: after inline" )
+/* ]]> */
</script>
<script id="defer-dependency-with-blocking-preceding-dependency-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-dependency-with-blocking-preceding-dependency: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=defer-dependency-with-blocking-preceding-dependency:%20script' id='defer-dependency-with-blocking-preceding-dependency-js' data-wp-strategy='defer'></script>
<script id="defer-dependency-with-blocking-preceding-dependency-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-dependency-with-blocking-preceding-dependency: after inline" )
+/* ]]> */
</script>
<script id="defer-dependent-of-blocking-and-defer-dependencies-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-dependent-of-blocking-and-defer-dependencies: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=defer-dependent-of-blocking-and-defer-dependencies:%20script' id='defer-dependent-of-blocking-and-defer-dependencies-js' data-wp-strategy='defer'></script>
<script id="defer-dependent-of-blocking-and-defer-dependencies-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-dependent-of-blocking-and-defer-dependencies: after inline" )
+/* ]]> */
</script>
HTML
,
@@ -790,25 +866,37 @@ HTML
},
'expected_markup' => <<<HTML
<script id="defer-dependency-with-blocking-following-dependency-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-dependency-with-blocking-following-dependency: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=defer-dependency-with-blocking-following-dependency:%20script' id='defer-dependency-with-blocking-following-dependency-js' data-wp-strategy='defer'></script>
<script id="defer-dependency-with-blocking-following-dependency-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-dependency-with-blocking-following-dependency: after inline" )
+/* ]]> */
</script>
<script id="blocking-dependency-with-defer-preceding-dependency-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "blocking-dependency-with-defer-preceding-dependency: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=blocking-dependency-with-defer-preceding-dependency:%20script' id='blocking-dependency-with-defer-preceding-dependency-js'></script>
<script id="blocking-dependency-with-defer-preceding-dependency-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "blocking-dependency-with-defer-preceding-dependency: after inline" )
+/* ]]> */
</script>
<script id="defer-dependent-of-defer-and-blocking-dependencies-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-dependent-of-defer-and-blocking-dependencies: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=defer-dependent-of-defer-and-blocking-dependencies:%20script' id='defer-dependent-of-defer-and-blocking-dependencies-js' data-wp-strategy='defer'></script>
<script id="defer-dependent-of-defer-and-blocking-dependencies-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-dependent-of-defer-and-blocking-dependencies: after inline" )
+/* ]]> */
</script>
HTML
,
@@ -826,18 +914,26 @@ HTML
},
'expected_markup' => <<<HTML
<script id="defer-with-async-dependent-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-with-async-dependent: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=defer-with-async-dependent:%20script' id='defer-with-async-dependent-js' data-wp-strategy='defer'></script>
<script id="defer-with-async-dependent-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-with-async-dependent: after inline" )
+/* ]]> */
</script>
<script id="async-dependent-of-defer-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "async-dependent-of-defer: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=async-dependent-of-defer:%20script' id='async-dependent-of-defer-js' data-wp-strategy='async'></script>
<script id="async-dependent-of-defer-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "async-dependent-of-defer: after inline" )
+/* ]]> */
</script>
HTML
,
@@ -851,9 +947,11 @@ HTML
},
'expected_markup' => <<<HTML
<script id="defer-with-before-inline-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-with-before-inline: before inline" )
+/* ]]> */
</script>
-<script type='text/javascript' src='https://example.com/external.js?script_event_log=defer-with-before-inline:%20script' id='defer-with-before-inline-js' defer data-wp-strategy='defer'></script>
+<script type='text/javascript' src='https://example.com/external.js?script_event_log=defer-with-before-inline:%20script' id='defer-with-before-inline-js' defer='defer' data-wp-strategy='defer'></script>
HTML
,
),
@@ -867,7 +965,9 @@ HTML
'expected_markup' => <<<HTML
<script type='text/javascript' src='https://example.com/external.js?script_event_log=defer-with-after-inline:%20script' id='defer-with-after-inline-js' data-wp-strategy='defer'></script>
<script id="defer-with-after-inline-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-with-after-inline: after inline" )
+/* ]]> */
</script>
HTML
,
@@ -883,9 +983,9 @@ HTML
wp_enqueue_script( 'theme-functions', 'https://example.com/theme-functions.js', array( 'jquery' ), null, array( 'strategy' => 'defer' ) );
},
'expected_markup' => <<<HTML
-<script type='text/javascript' src='http://$wp_tests_domain/wp-includes/js/jquery/jquery.js' id='jquery-core-js' defer data-wp-strategy='defer'></script>
-<script type='text/javascript' src='http://$wp_tests_domain/wp-includes/js/jquery/jquery-migrate.js' id='jquery-migrate-js' defer data-wp-strategy='defer'></script>
-<script type='text/javascript' src='https://example.com/theme-functions.js' id='theme-functions-js' defer data-wp-strategy='defer'></script>
+<script type='text/javascript' src='http://$wp_tests_domain/wp-includes/js/jquery/jquery.js' id='jquery-core-js' defer='defer' data-wp-strategy='defer'></script>
+<script type='text/javascript' src='http://$wp_tests_domain/wp-includes/js/jquery/jquery-migrate.js' id='jquery-migrate-js' defer='defer' data-wp-strategy='defer'></script>
+<script type='text/javascript' src='https://example.com/theme-functions.js' id='theme-functions-js' defer='defer' data-wp-strategy='defer'></script>
HTML
,
),
@@ -912,11 +1012,15 @@ HTML
<script type='text/javascript' src='https://example.com/external.js?script_event_log=inner-bundle-member-two:%20script' id='inner-bundle-member-two-js' data-wp-strategy='defer'></script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=outer-bundle-leaf-member:%20script' id='outer-bundle-leaf-member-js'></script>
<script id="defer-dependent-of-nested-aliases-js-before" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-dependent-of-nested-aliases: before inline" )
+/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/external.js?script_event_log=defer-dependent-of-nested-aliases:%20script' id='defer-dependent-of-nested-aliases-js' data-wp-strategy='defer'></script>
<script id="defer-dependent-of-nested-aliases-js-after" type="text/javascript">
+/* <![CDATA[ */
scriptEventLog.push( "defer-dependent-of-nested-aliases: after inline" )
+/* ]]> */
</script>
HTML
,
@@ -935,9 +1039,9 @@ HTML
$this->enqueue_test_script( 'defer-dependent-of-async-aliases', 'defer', array( $alias_handle ) );
},
'expected_markup' => <<<HTML
-<script type='text/javascript' src='https://example.com/external.js?script_event_log=async1:%20script' id='async1-js' defer data-wp-strategy='async'></script>
-<script type='text/javascript' src='https://example.com/external.js?script_event_log=async2:%20script' id='async2-js' defer data-wp-strategy='async'></script>
-<script type='text/javascript' src='https://example.com/external.js?script_event_log=defer-dependent-of-async-aliases:%20script' id='defer-dependent-of-async-aliases-js' defer data-wp-strategy='defer'></script>
+<script type='text/javascript' src='https://example.com/external.js?script_event_log=async1:%20script' id='async1-js' defer='defer' data-wp-strategy='async'></script>
+<script type='text/javascript' src='https://example.com/external.js?script_event_log=async2:%20script' id='async2-js' defer='defer' data-wp-strategy='async'></script>
+<script type='text/javascript' src='https://example.com/external.js?script_event_log=defer-dependent-of-async-aliases:%20script' id='defer-dependent-of-async-aliases-js' defer='defer' data-wp-strategy='defer'></script>
HTML
,
),
@@ -960,7 +1064,7 @@ HTML
public function test_various_strategy_dependency_chains( $set_up, $expected_markup ) {
$set_up();
$actual_markup = get_echo( 'wp_print_scripts' );
- $this->assertEqualMarkup( trim( $expected_markup ), trim( $actual_markup ), "Actual markup:\n{$actual_markup}" );
+ $this->assertEqualHTML( trim( $expected_markup ), trim( $actual_markup ), '<body>', "Actual markup:\n{$actual_markup}" );
}
/**
@@ -1036,12 +1140,12 @@ HTML
wp_enqueue_script( 'dependent-script-d4-2', '/dependent-script-d4-2.js', array( 'dependent-script-d4-1' ), null, array( 'strategy' => 'async' ) );
wp_enqueue_script( 'dependent-script-d4-3', '/dependent-script-d4-3.js', array( 'dependent-script-d4-2' ), null, array( 'strategy' => 'defer' ) );
$output = get_echo( 'wp_print_scripts' );
- $expected = "<script type='text/javascript' src='/main-script-d4.js' id='main-script-d4-js' defer data-wp-strategy='defer'></script>\n";
- $expected .= "<script type='text/javascript' src='/dependent-script-d4-1.js' id='dependent-script-d4-1-js' defer data-wp-strategy='defer'></script>\n";
- $expected .= "<script type='text/javascript' src='/dependent-script-d4-2.js' id='dependent-script-d4-2-js' defer data-wp-strategy='async'></script>\n";
- $expected .= "<script type='text/javascript' src='/dependent-script-d4-3.js' id='dependent-script-d4-3-js' defer data-wp-strategy='defer'></script>\n";
+ $expected = "<script type='text/javascript' src='/main-script-d4.js' id='main-script-d4-js' defer='defer' data-wp-strategy='defer'></script>\n";
+ $expected .= "<script type='text/javascript' src='/dependent-script-d4-1.js' id='dependent-script-d4-1-js' defer='defer' data-wp-strategy='defer'></script>\n";
+ $expected .= "<script type='text/javascript' src='/dependent-script-d4-2.js' id='dependent-script-d4-2-js' defer='defer' data-wp-strategy='async'></script>\n";
+ $expected .= "<script type='text/javascript' src='/dependent-script-d4-3.js' id='dependent-script-d4-3-js' defer='defer' data-wp-strategy='defer'></script>\n";
- $this->assertEqualMarkup( $expected, $output, 'Scripts registered as defer but that have dependents that are async are expected to have said dependents deferred.' );
+ $this->assertEqualHTML( $expected, $output, '<body>', 'Scripts registered as defer but that have dependents that are async are expected to have said dependents deferred.' );
}
/**
@@ -1113,7 +1217,7 @@ HTML
$expected_header .= "<script type='text/javascript' src='/enqueue-header-old.js' id='enqueue-header-old-js'></script>\n";
$expected_header .= "<script type='text/javascript' src='/enqueue-header-new.js' id='enqueue-header-new-js'></script>\n";
- $this->assertEqualMarkup( $expected_header, $actual_header, 'Scripts registered/enqueued using the older $in_footer parameter or the newer $args parameter should have the same outcome.' );
+ $this->assertEqualHTML( $expected_header, $actual_header, '<body>', 'Scripts registered/enqueued using the older $in_footer parameter or the newer $args parameter should have the same outcome.' );
$this->assertEmpty( $actual_footer, 'Expected footer to be empty since all scripts were for head.' );
}
@@ -1141,7 +1245,7 @@ HTML
$expected_footer .= "<script type='text/javascript' src='/enqueue-footer-new.js' id='enqueue-footer-new-js'></script>\n";
$this->assertEmpty( $actual_header, 'Expected header to be empty since all scripts targeted footer.' );
- $this->assertEqualMarkup( $expected_footer, $actual_footer, 'Scripts registered/enqueued using the older $in_footer parameter or the newer $args parameter should have the same outcome.' );
+ $this->assertEqualHTML( $expected_footer, $actual_footer, '<body>', 'Scripts registered/enqueued using the older $in_footer parameter or the newer $args parameter should have the same outcome.' );
}
/**
@@ -1260,7 +1364,7 @@ HTML
wp_register_script( 'invalid-strategy', '/defaults.js', array(), null, array( 'strategy' => 'random-strategy' ) );
wp_enqueue_script( 'invalid-strategy' );
- $this->assertEqualMarkup(
+ $this->assertEqualHTML(
"<script type='text/javascript' src='/defaults.js' id='invalid-strategy-js'></script>\n",
get_echo( 'wp_print_scripts' )
);
@@ -1285,7 +1389,7 @@ HTML
wp_script_add_data( 'invalid-strategy', 'strategy', 'random-strategy' );
wp_enqueue_script( 'invalid-strategy' );
- $this->assertEqualMarkup(
+ $this->assertEqualHTML(
"<script type='text/javascript' src='/defaults.js' id='invalid-strategy-js'></script>\n",
get_echo( 'wp_print_scripts' )
);
@@ -1306,7 +1410,7 @@ HTML
public function test_script_strategy_doing_it_wrong_via_enqueue() {
wp_enqueue_script( 'invalid-strategy', '/defaults.js', array(), null, array( 'strategy' => 'random-strategy' ) );
- $this->assertEqualMarkup(
+ $this->assertEqualHTML(
"<script type='text/javascript' src='/defaults.js' id='invalid-strategy-js'></script>\n",
get_echo( 'wp_print_scripts' )
);
@@ -1342,9 +1446,9 @@ HTML
$concatenate_scripts = $old_value;
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5Bchunk_0%5D=one-concat-dep,two-concat-dep,three-concat-dep&amp;ver={$wp_version}'></script>\n";
- $expected .= "<script type='text/javascript' src='/main-script.js' id='main-defer-script-js' defer data-wp-strategy='defer'></script>\n";
+ $expected .= "<script type='text/javascript' src='/main-script.js' id='main-defer-script-js' defer='defer' data-wp-strategy='defer'></script>\n";
- $this->assertEqualMarkup( $expected, $print_scripts, 'Scripts are being incorrectly concatenated when a main script is registered with a "defer" loading strategy. Deferred scripts should not be part of the script concat loading query.' );
+ $this->assertEqualHTML( $expected, $print_scripts, '<body>', 'Scripts are being incorrectly concatenated when a main script is registered with a "defer" loading strategy. Deferred scripts should not be part of the script concat loading query.' );
}
/**
@@ -1377,9 +1481,9 @@ HTML
$concatenate_scripts = $old_value;
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5Bchunk_0%5D=one-concat-dep-1,two-concat-dep-1,three-concat-dep-1&amp;ver={$wp_version}'></script>\n";
- $expected .= "<script type='text/javascript' src='/main-script.js' id='main-async-script-1-js' async data-wp-strategy='async'></script>\n";
+ $expected .= "<script type='text/javascript' src='/main-script.js' id='main-async-script-1-js' async='async' data-wp-strategy='async'></script>\n";
- $this->assertEqualMarkup( $expected, $print_scripts, 'Scripts are being incorrectly concatenated when a main script is registered with an "async" loading strategy. Async scripts should not be part of the script concat loading query.' );
+ $this->assertEqualHTML( $expected, $print_scripts, '<body>', 'Scripts are being incorrectly concatenated when a main script is registered with an "async" loading strategy. Async scripts should not be part of the script concat loading query.' );
}
/**
@@ -1416,9 +1520,9 @@ HTML
$concatenate_scripts = $old_value;
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5Bchunk_0%5D=one-concat-dep-2,two-concat-dep-2,three-concat-dep-2,four-concat-dep-2,five-concat-dep-2,six-concat-dep-2&amp;ver={$wp_version}'></script>\n";
- $expected .= "<script type='text/javascript' src='/main-script.js' id='deferred-script-2-js' defer data-wp-strategy='defer'></script>\n";
+ $expected .= "<script type='text/javascript' src='/main-script.js' id='deferred-script-2-js' defer='defer' data-wp-strategy='defer'></script>\n";
- $this->assertEqualMarkup( $expected, $print_scripts, 'Scripts are being incorrectly concatenated when a main script is registered as deferred after other blocking scripts are registered. Deferred scripts should not be part of the script concat loader query string. ' );
+ $this->assertEqualHTML( $expected, $print_scripts, '<body>', 'Scripts are being incorrectly concatenated when a main script is registered as deferred after other blocking scripts are registered. Deferred scripts should not be part of the script concat loader query string. ' );
}
/**
@@ -1427,6 +1531,8 @@ HTML
public function test_wp_enqueue_script_with_html5_support_does_not_contain_type_attribute() {
global $wp_version;
+ $this->add_html5_script_theme_support();
+
$GLOBALS['wp_scripts'] = new WP_Scripts();
$GLOBALS['wp_scripts']->default_version = get_bloginfo( 'version' );
@@ -1434,7 +1540,7 @@ HTML
$expected = "<script src='http://example.com?ver={$wp_version}' id='empty-deps-no-version-js'></script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -1473,7 +1579,7 @@ HTML
$expected .= "<script type='text/javascript' src='{$wp_scripts->base_url}ftp://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js?ver={$wp_version}' id='jquery-ftp-js'></script>\n";
// Go!
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
// No scripts left to print.
$this->assertSame( '', get_echo( 'wp_print_scripts' ) );
@@ -1516,7 +1622,7 @@ HTML
$expected .= "<script type='text/javascript' src='http://example.com' id='test-only-data-js'></script>\n";
// Go!
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
// No scripts left to print.
$this->assertSame( '', get_echo( 'wp_print_scripts' ) );
@@ -1531,10 +1637,10 @@ HTML
// Enqueue and add conditional comments.
wp_enqueue_script( 'test-only-conditional', 'example.com', array(), null );
wp_script_add_data( 'test-only-conditional', 'conditional', 'gt IE 7' );
- $expected = "<!--[if gt IE 7]>\n<script type='text/javascript' src='http://example.com' id='test-only-conditional-js'></script>\n<![endif]-->\n";
+ $expected = "<!--[if gt IE 7]>\n<script type=\"text/javascript\" src=\"http://example.com\" id=\"test-only-conditional-js\"></script>\n<![endif]-->\n";
// Go!
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
// No scripts left to print.
$this->assertSame( '', get_echo( 'wp_print_scripts' ) );
@@ -1555,7 +1661,7 @@ HTML
$expected = str_replace( "'", '"', $expected );
// Go!
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
// No scripts left to print.
$this->assertSame( '', get_echo( 'wp_print_scripts' ) );
@@ -1573,10 +1679,10 @@ HTML
$expected = "<script type='text/javascript' src='http://example.com' id='test-invalid-js'></script>\n";
// Go!
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
// No scripts left to print.
- $this->assertEqualMarkup( '', get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( '', get_echo( 'wp_print_scripts' ) );
}
/**
@@ -1602,7 +1708,7 @@ HTML
wp_enqueue_script( 'handle-three' );
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -1690,8 +1796,8 @@ HTML
$expected_header .= "<script type='text/javascript' src='/child-head.js' id='child-head-js'></script>\n";
$expected_footer = "<script type='text/javascript' src='/parent.js' id='parent-js'></script>\n";
- $this->assertEqualMarkup( $expected_header, $header, 'Expected same header markup.' );
- $this->assertEqualMarkup( $expected_footer, $footer, 'Expected same footer markup.' );
+ $this->assertEqualHTML( $expected_header, $header, '<body>', 'Expected same header markup.' );
+ $this->assertEqualHTML( $expected_footer, $footer, '<body>', 'Expected same footer markup.' );
}
/**
@@ -1711,8 +1817,8 @@ HTML
$expected_footer = "<script type='text/javascript' src='/child-footer.js' id='child-footer-js'></script>\n";
$expected_footer .= "<script type='text/javascript' src='/parent.js' id='parent-js'></script>\n";
- $this->assertEqualMarkup( $expected_header, $header, 'Expected same header markup.' );
- $this->assertEqualMarkup( $expected_footer, $footer, 'Expected same footer markup.' );
+ $this->assertEqualHTML( $expected_header, $header, '<body>', 'Expected same header markup.' );
+ $this->assertEqualHTML( $expected_footer, $footer, '<body>', 'Expected same footer markup.' );
}
/**
@@ -1742,8 +1848,8 @@ HTML
$expected_footer .= "<script type='text/javascript' src='/child2-footer.js' id='child2-footer-js'></script>\n";
$expected_footer .= "<script type='text/javascript' src='/parent-footer.js' id='parent-footer-js'></script>\n";
- $this->assertEqualMarkup( $expected_header, $header, 'Expected same header markup.' );
- $this->assertEqualMarkup( $expected_footer, $footer, 'Expected same footer markup.' );
+ $this->assertEqualHTML( $expected_header, $header, '<body>', 'Expected same header markup.' );
+ $this->assertEqualHTML( $expected_footer, $footer, '<body>', 'Expected same footer markup.' );
}
/**
@@ -1770,10 +1876,16 @@ HTML
wp_enqueue_script( 'test-example', 'example.com', array(), null );
wp_add_inline_script( 'test-example', 'console.log("before");', 'before' );
- $expected = "<script type='text/javascript' id='test-example-js-before'>\nconsole.log(\"before\");\n</script>\n";
+ $expected = <<<HTML
+<script type='text/javascript' id='test-example-js-before'>
+/* <![CDATA[ */
+console.log("before");
+/* ]]> */
+</script>
+HTML;
$expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -1784,9 +1896,15 @@ HTML
wp_add_inline_script( 'test-example', 'console.log("after");' );
$expected = "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
- $expected .= "<script type='text/javascript' id='test-example-js-after'>\nconsole.log(\"after\");\n</script>\n";
+ $expected .= <<<HTML
+<script type='text/javascript' id='test-example-js-after'>
+/* <![CDATA[ */
+console.log("after");
+/* ]]> */
+</script>
+HTML;
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -1797,11 +1915,11 @@ HTML
wp_add_inline_script( 'test-example', 'console.log("before");', 'before' );
wp_add_inline_script( 'test-example', 'console.log("after");' );
- $expected = "<script type='text/javascript' id='test-example-js-before'>\nconsole.log(\"before\");\n</script>\n";
+ $expected = "<script type='text/javascript' id='test-example-js-before'>\n/* <![CDATA[ */\nconsole.log(\"before\");\n/* ]]> */\n</script>\n";
$expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
- $expected .= "<script type='text/javascript' id='test-example-js-after'>\nconsole.log(\"after\");\n</script>\n";
+ $expected .= "<script type='text/javascript' id='test-example-js-after'>\n/* <![CDATA[ */\nconsole.log(\"after\");\n/* ]]> */\n</script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -1812,9 +1930,9 @@ HTML
wp_enqueue_script( 'test-example' );
wp_add_inline_script( 'test-example', 'console.log("before");', 'before' );
- $expected = "<script type='text/javascript' id='test-example-js-before'>\nconsole.log(\"before\");\n</script>\n";
+ $expected = "<script type='text/javascript' id='test-example-js-before'>\n/* <![CDATA[ */\nconsole.log(\"before\");\n/* ]]> */\n</script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -1825,9 +1943,9 @@ HTML
wp_enqueue_script( 'test-example' );
wp_add_inline_script( 'test-example', 'console.log("after");' );
- $expected = "<script type='text/javascript' id='test-example-js-after'>\nconsole.log(\"after\");\n</script>\n";
+ $expected = "<script type='text/javascript' id='test-example-js-after'>\n/* <![CDATA[ */\nconsole.log(\"after\");\n/* ]]> */\n</script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -1839,10 +1957,10 @@ HTML
wp_add_inline_script( 'test-example', 'console.log("before");', 'before' );
wp_add_inline_script( 'test-example', 'console.log("after");' );
- $expected = "<script type='text/javascript' id='test-example-js-before'>\nconsole.log(\"before\");\n</script>\n";
- $expected .= "<script type='text/javascript' id='test-example-js-after'>\nconsole.log(\"after\");\n</script>\n";
+ $expected = "<script type='text/javascript' id='test-example-js-before'>\n/* <![CDATA[ */\nconsole.log(\"before\");\n/* ]]> */\n</script>\n";
+ $expected .= "<script type='text/javascript' id='test-example-js-after'>\n/* <![CDATA[ */\nconsole.log(\"after\");\n/* ]]> */\n</script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -1855,11 +1973,11 @@ HTML
wp_add_inline_script( 'test-example', 'console.log("after");' );
wp_add_inline_script( 'test-example', 'console.log("after");' );
- $expected = "<script type='text/javascript' id='test-example-js-before'>\nconsole.log(\"before\");\nconsole.log(\"before\");\n</script>\n";
+ $expected = "<script type='text/javascript' id='test-example-js-before'>\n/* <![CDATA[ */\nconsole.log(\"before\");\nconsole.log(\"before\");\n/* ]]> */\n</script>\n";
$expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
- $expected .= "<script type='text/javascript' id='test-example-js-after'>\nconsole.log(\"after\");\nconsole.log(\"after\");\n</script>\n";
+ $expected .= "<script type='text/javascript' id='test-example-js-after'>\n/* <![CDATA[ */\nconsole.log(\"after\");\nconsole.log(\"after\");\n/* ]]> */\n</script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -1872,11 +1990,11 @@ HTML
wp_add_inline_script( 'test-example', 'console.log("after");' );
$expected = "<script type='text/javascript' id='test-example-js-extra'>\n/* <![CDATA[ */\nvar testExample = {\"foo\":\"bar\"};\n/* ]]> */\n</script>\n";
- $expected .= "<script type='text/javascript' id='test-example-js-before'>\nconsole.log(\"before\");\n</script>\n";
+ $expected .= "<script type='text/javascript' id='test-example-js-before'>\n/* <![CDATA[ */\nconsole.log(\"before\");\n/* ]]> */\n</script>\n";
$expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
- $expected .= "<script type='text/javascript' id='test-example-js-after'>\nconsole.log(\"after\");\n</script>\n";
+ $expected .= "<script type='text/javascript' id='test-example-js-after'>\n/* <![CDATA[ */\nconsole.log(\"after\");\n/* ]]> */\n</script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -1895,13 +2013,13 @@ HTML
wp_add_inline_script( 'one', 'console.log("before one");', 'before' );
wp_add_inline_script( 'two', 'console.log("before two");', 'before' );
- $expected = "<script type='text/javascript' id='one-js-before'>\nconsole.log(\"before one\");\n</script>\n";
+ $expected = "<script type='text/javascript' id='one-js-before'>\n/* <![CDATA[ */\nconsole.log(\"before one\");\n/* ]]> */\n</script>\n";
$expected .= "<script type='text/javascript' src='{$this->default_scripts_dir}one.js?ver={$wp_version}' id='one-js'></script>\n";
- $expected .= "<script type='text/javascript' id='two-js-before'>\nconsole.log(\"before two\");\n</script>\n";
+ $expected .= "<script type='text/javascript' id='two-js-before'>\n/* <![CDATA[ */\nconsole.log(\"before two\");\n/* ]]> */\n</script>\n";
$expected .= "<script type='text/javascript' src='{$this->default_scripts_dir}two.js?ver={$wp_version}' id='two-js'></script>\n";
$expected .= "<script type='text/javascript' src='{$this->default_scripts_dir}three.js?ver={$wp_version}' id='three-js'></script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -1919,12 +2037,12 @@ HTML
wp_add_inline_script( 'one', 'console.log("before one");', 'before' );
- $expected = "<script type='text/javascript' id='one-js-before'>\nconsole.log(\"before one\");\n</script>\n";
+ $expected = "<script type='text/javascript' id='one-js-before'>\n/* <![CDATA[ */\nconsole.log(\"before one\");\n/* ]]> */\n</script>\n";
$expected .= "<script type='text/javascript' src='{$this->default_scripts_dir}one.js?ver={$wp_version}' id='one-js'></script>\n";
$expected .= "<script type='text/javascript' src='{$this->default_scripts_dir}two.js?ver={$wp_version}' id='two-js'></script>\n";
$expected .= "<script type='text/javascript' src='{$this->default_scripts_dir}three.js?ver={$wp_version}' id='three-js'></script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -1946,12 +2064,12 @@ HTML
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5Bchunk_0%5D=one&amp;ver={$wp_version}'></script>\n";
$expected .= "<script type='text/javascript' src='{$this->default_scripts_dir}two.js?ver={$wp_version}' id='two-js'></script>\n";
- $expected .= "<script type='text/javascript' id='two-js-after'>\nconsole.log(\"after two\");\n</script>\n";
+ $expected .= "<script type='text/javascript' id='two-js-after'>\n/* <![CDATA[ */\nconsole.log(\"after two\");\n/* ]]> */\n</script>\n";
$expected .= "<script type='text/javascript' src='{$this->default_scripts_dir}three.js?ver={$wp_version}' id='three-js'></script>\n";
- $expected .= "<script type='text/javascript' id='three-js-after'>\nconsole.log(\"after three\");\n</script>\n";
+ $expected .= "<script type='text/javascript' id='three-js-after'>\n/* <![CDATA[ */\nconsole.log(\"after three\");\n/* ]]> */\n</script>\n";
$expected .= "<script type='text/javascript' src='{$this->default_scripts_dir}four.js?ver={$wp_version}' id='four-js'></script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -1969,9 +2087,9 @@ HTML
$expected_localized = str_replace( "'", '"', $expected_localized );
$expected = "<!--[if gte IE 9]>\n";
- $expected .= "<script type='text/javascript' id='test-example-js-before'>\nconsole.log(\"before\");\n</script>\n";
+ $expected .= "<script type='text/javascript' id='test-example-js-before'>\n/* <![CDATA[ */\nconsole.log(\"before\");\n/* ]]> */\n</script>\n";
$expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
- $expected .= "<script type='text/javascript' id='test-example-js-after'>\nconsole.log(\"after\");\n</script>\n";
+ $expected .= "<script type='text/javascript' id='test-example-js-after'>\n/* <![CDATA[ */\nconsole.log(\"after\");\n/* ]]> */\n</script>\n";
$expected .= "<![endif]-->\n";
$expected = str_replace( "'", '"', $expected );
@@ -1982,7 +2100,7 @@ HTML
wp_script_add_data( 'test-example', 'conditional', 'gte IE 9' );
$this->assertSame( $expected_localized, get_echo( 'wp_print_scripts' ) );
- $this->assertEqualMarkup( $expected, $wp_scripts->print_html );
+ $this->assertEqualHTML( $expected, $wp_scripts->print_html );
$this->assertTrue( $wp_scripts->do_concat );
}
@@ -1999,7 +2117,7 @@ HTML
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5Bchunk_0%5D=jquery-core,jquery-migrate&amp;ver={$wp_version}'></script>\n";
$expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
- $expected .= "<script type='text/javascript' id='test-example-js-after'>\nconsole.log(\"after\");\n</script>\n";
+ $expected .= "<script type='text/javascript' id='test-example-js-after'>\n/* <![CDATA[ */\nconsole.log(\"after\");\n/* ]]> */\n</script>\n";
wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null );
wp_add_inline_script( 'test-example', 'console.log("after");' );
@@ -2007,7 +2125,7 @@ HTML
wp_print_scripts();
$print_scripts = get_echo( '_print_scripts' );
- $this->assertEqualMarkup( $expected, $print_scripts );
+ $this->assertEqualHTML( $expected, $print_scripts );
}
/**
@@ -2023,8 +2141,8 @@ HTML
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5Bchunk_0%5D=jquery-core,jquery-migrate&amp;ver={$wp_version}'></script>\n";
$expected .= "<!--[if gte IE 9]>\n";
- $expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
- $expected .= "<script type='text/javascript' id='test-example-js-after'>\nconsole.log(\"after\");\n</script>\n";
+ $expected .= "<script type=\"text/javascript\" src=\"http://example.com\" id=\"test-example-js\"></script>\n";
+ $expected .= "<script type=\"text/javascript\" id=\"test-example-js-after\">\n/* <![CDATA[ */\nconsole.log(\"after\");\n/* ]]> */\n</script>\n";
$expected .= "<![endif]-->\n";
wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null );
@@ -2034,7 +2152,7 @@ HTML
wp_print_scripts();
$print_scripts = get_echo( '_print_scripts' );
- $this->assertEqualMarkup( $expected, $print_scripts );
+ $this->assertEqualHTML( $expected, $print_scripts );
}
/**
@@ -2050,7 +2168,7 @@ HTML
$wp_scripts->do_concat = true;
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5Bchunk_0%5D=jquery-core,jquery-migrate&amp;ver={$wp_version}'></script>\n";
- $expected .= "<script type='text/javascript' id='test-example-js-before'>\nconsole.log(\"before\");\n</script>\n";
+ $expected .= "<script type='text/javascript' id='test-example-js-before'>\n/* <![CDATA[ */\nconsole.log(\"before\");\n/* ]]> */\n</script>\n";
$expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null );
@@ -2059,7 +2177,7 @@ HTML
wp_print_scripts();
$print_scripts = get_echo( '_print_scripts' );
- $this->assertEqualMarkup( $expected, $print_scripts );
+ $this->assertEqualHTML( $expected, $print_scripts );
}
/**
@@ -2075,15 +2193,17 @@ HTML
$wp_scripts->do_concat = true;
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5Bchunk_0%5D=jquery-core,jquery-migrate,wp-dom-ready,wp-hooks&amp;ver={$wp_version}'></script>\n";
- $expected .= "<script type='text/javascript' id='test-example-js-before'>\nconsole.log(\"before\");\n</script>\n";
+ $expected .= "<script type='text/javascript' id='test-example-js-before'>\n/* <![CDATA[ */\nconsole.log(\"before\");\n/* ]]> */\n</script>\n";
$expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
$expected .= "<script type='text/javascript' src='/wp-includes/js/dist/i18n.min.js' id='wp-i18n-js'></script>\n";
$expected .= "<script type='text/javascript' id='wp-i18n-js-after'>\n";
+ $expected .= "/* <![CDATA[ */\n";
$expected .= "wp.i18n.setLocaleData( { 'text direction\u0004ltr': [ 'ltr' ] } );\n";
+ $expected .= "/* ]]> */\n";
$expected .= "</script>\n";
$expected .= "<script type='text/javascript' src='/wp-includes/js/dist/a11y.min.js' id='wp-a11y-js'></script>\n";
$expected .= "<script type='text/javascript' src='http://example2.com' id='test-example2-js'></script>\n";
- $expected .= "<script type='text/javascript' id='test-example2-js-after'>\nconsole.log(\"after\");\n</script>\n";
+ $expected .= "<script type='text/javascript' id='test-example2-js-after'>\n/* <![CDATA[ */\nconsole.log(\"after\");\n/* ]]> */\n</script>\n";
wp_enqueue_script( 'test-example', 'http://example.com', array( 'jquery' ), null );
wp_add_inline_script( 'test-example', 'console.log("before");', 'before' );
@@ -2109,7 +2229,7 @@ HTML
$print_scripts // Printed scripts.
);
- $this->assertEqualMarkup( $expected, $print_scripts );
+ $this->assertEqualHTML( $expected, $print_scripts );
}
/**
@@ -2126,7 +2246,9 @@ HTML
$expected_tail = "<script type='text/javascript' src='/customize-dependency.js' id='customize-dependency-js'></script>\n";
$expected_tail .= "<script type='text/javascript' id='customize-dependency-js-after'>\n";
+ $expected_tail .= "/* <![CDATA[ */\n";
$expected_tail .= "tryCustomizeDependency()\n";
+ $expected_tail .= "/* ]]> */\n";
$expected_tail .= "</script>\n";
$handle = 'customize-dependency';
@@ -2142,7 +2264,7 @@ HTML
$tail = substr( $print_scripts, strrpos( $print_scripts, '<script type="text/javascript" src="/customize-dependency.js" id="customize-dependency-js">' ) );
- $this->assertEqualMarkup( $expected_tail, $tail );
+ $this->assertEqualHTML( $expected_tail, $tail );
}
/**
@@ -2161,12 +2283,12 @@ HTML
wp_enqueue_script( 'four', '/wp-includes/js/script4.js' );
$expected = "<script type='text/javascript' src='/wp-includes/js/script.js?ver={$wp_version}' id='one-js'></script>\n";
- $expected .= "<script type='text/javascript' id='one-js-after'>\nconsole.log(\"after one\");\n</script>\n";
+ $expected .= "<script type='text/javascript' id='one-js-after'>\n/* <![CDATA[ */\nconsole.log(\"after one\");\n/* ]]> */\n</script>\n";
$expected .= "<script type='text/javascript' src='/wp-includes/js/script2.js?ver={$wp_version}' id='two-js'></script>\n";
$expected .= "<script type='text/javascript' src='/wp-includes/js/script3.js?ver={$wp_version}' id='three-js'></script>\n";
$expected .= "<script type='text/javascript' src='/wp-includes/js/script4.js?ver={$wp_version}' id='four-js'></script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -2185,11 +2307,11 @@ HTML
wp_enqueue_script( 'four', '/wp-includes/js/script4.js' );
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5Bchunk_0%5D=one,two&amp;ver={$wp_version}'></script>\n";
- $expected .= "<script type='text/javascript' id='three-js-before'>\nconsole.log(\"before three\");\n</script>\n";
+ $expected .= "<script type='text/javascript' id='three-js-before'>\n/* <![CDATA[ */\nconsole.log(\"before three\");\n/* ]]> */\n</script>\n";
$expected .= "<script type='text/javascript' src='/wp-includes/js/script3.js?ver={$wp_version}' id='three-js'></script>\n";
$expected .= "<script type='text/javascript' src='/wp-includes/js/script4.js?ver={$wp_version}' id='four-js'></script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -2206,7 +2328,7 @@ HTML
),
'delayed' => false,
'expected_data' => '/*before foo 1*/',
- 'expected_tag' => "<script id='foo-js-before' type='text/javascript'>\n/*before foo 1*/\n</script>\n",
+ 'expected_tag' => "<script id='foo-js-before' type='text/javascript'>\n/* <![CDATA[ */\n/*before foo 1*/\n/* ]]> */\n</script>\n",
),
'after-blocking' => array(
'position' => 'after',
@@ -2216,7 +2338,7 @@ HTML
),
'delayed' => false,
'expected_data' => "/*after foo 1*/\n/*after foo 2*/",
- 'expected_tag' => "<script id='foo-js-after' type='text/javascript'>\n/*after foo 1*/\n/*after foo 2*/\n</script>\n",
+ 'expected_tag' => "<script id='foo-js-after' type='text/javascript'>\n/* <![CDATA[ */\n/*after foo 1*/\n/*after foo 2*/\n/* ]]> */\n</script>\n",
),
'before-delayed' => array(
'position' => 'before',
@@ -2225,7 +2347,7 @@ HTML
),
'delayed' => true,
'expected_data' => '/*before foo 1*/',
- 'expected_tag' => "<script id='foo-js-before' type='text/javascript'>\n/*before foo 1*/\n</script>\n",
+ 'expected_tag' => "<script id='foo-js-before' type='text/javascript'>\n/* <![CDATA[ */\n/*before foo 1*/\n/* ]]> */\n</script>\n",
),
'after-delayed' => array(
'position' => 'after',
@@ -2235,7 +2357,7 @@ HTML
),
'delayed' => true,
'expected_data' => "/*after foo 1*/\n/*after foo 2*/",
- 'expected_tag' => "<script id='foo-js-after' type='text/javascript'>\n/*after foo 1*/\n/*after foo 2*/\n</script>\n",
+ 'expected_tag' => "<script id='foo-js-after' type='text/javascript'>\n/* <![CDATA[ */\n/*after foo 1*/\n/*after foo 2*/\n/* ]]> */\n</script>\n",
),
);
}
@@ -2287,13 +2409,13 @@ HTML
$this->assertSame( $expected_data, $wp_scripts->get_inline_script_data( $handle, $position ) );
$this->assertSame( $expected_data, $wp_scripts->print_inline_script( $handle, $position, false ) );
- $this->assertEqualMarkup(
+ $this->assertEqualHTML(
$expected_tag,
$wp_scripts->get_inline_script_tag( $handle, $position )
);
ob_start();
$output = $wp_scripts->print_inline_script( $handle, $position, true );
- $this->assertEqualMarkup( $expected_tag, ob_get_clean() );
+ $this->assertEqualHTML( $expected_tag, ob_get_clean() );
$this->assertEquals( $expected_data, $output );
}
@@ -2321,7 +2443,7 @@ HTML
);
$expected .= "<script type='text/javascript' src='/wp-includes/js/script.js' id='test-example-js'></script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -2348,7 +2470,7 @@ HTML
);
$expected .= "<script type='text/javascript' src='/wp-content/plugins/my-plugin/js/script.js' id='plugin-example-js'></script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -2375,7 +2497,7 @@ HTML
);
$expected .= "<script type='text/javascript' src='/wp-content/themes/my-theme/js/script.js' id='theme-example-js'></script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -2402,7 +2524,7 @@ HTML
);
$expected .= "<script type='text/javascript' src='/wp-admin/js/script.js' id='script-handle-js'></script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -2432,7 +2554,7 @@ HTML
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js' id='wp-i18n-js'></script>\n";
$expected .= "<script type='text/javascript' src='/wp-admin/js/script.js' id='test-example-js'></script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -2461,7 +2583,7 @@ HTML
);
$expected .= "<script type='text/javascript' src='/wp-includes/js/script.js' id='test-example-js'></script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -2491,7 +2613,7 @@ HTML
$expected .= "<script type='text/javascript' src='/wp-includes/js/script.js' id='test-dependency-js'></script>\n";
$expected .= "<script type='text/javascript' src='/wp-includes/js/script2.js' id='test-example-js'></script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -2880,7 +3002,7 @@ HTML
$expected = "<script type='text/javascript' id='test-example-js-extra'>\n/* <![CDATA[ */\nvar testExample = {$expected};\n/* ]]> */\n</script>\n";
$expected .= "<script type='text/javascript' src='http://example.com' id='test-example-js'></script>\n";
- $this->assertEqualMarkup( $expected, get_echo( 'wp_print_scripts' ) );
+ $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
}
/**
@@ -2945,7 +3067,7 @@ HTML
$expected .= "<script type='text/javascript' src='/plugins/wp-i18n.js' id='wp-i18n-js'></script>\n";
$expected .= "<script type='text/javascript' src='/default/common.js' id='common-js'></script>\n";
- $this->assertEqualMarkup( $expected, $print_scripts );
+ $this->assertEqualHTML( $expected, $print_scripts );
}
/**
@@ -2981,87 +3103,6 @@ HTML
}
/**
- * Parse an HTML markup fragment.
- *
- * @param string $markup Markup.
- * @return DOMDocument Document containing the normalized markup fragment.
- */
- protected function parse_markup_fragment( $markup ) {
- $dom = new DOMDocument();
- $dom->loadHTML(
- "<!DOCTYPE html><html><head><meta charset=utf8></head><body>{$markup}</body></html>"
- );
-
- /** @var DOMElement $body */
- $body = $dom->getElementsByTagName( 'body' )->item( 0 );
-
- // Trim whitespace nodes added before/after which can be added when parsing.
- foreach ( array( $body->firstChild, $body->lastChild ) as $node ) {
- if ( $node instanceof DOMText && '' === trim( $node->data ) ) {
- $body->removeChild( $node );
- }
- }
-
- // Normalize other whitespace nodes.
- $xpath = new DOMXPath( $dom );
- foreach ( $xpath->query( '//text()' ) as $node ) {
- /** @var DOMText $node */
- if ( preg_match( '/^\s+$/', $node->nodeValue ) ) {
- $node->nodeValue = ' ';
- }
- }
-
- return $dom;
- }
-
- /**
- * Assert markup is equal after normalizing script tags.
- *
- * @param string $expected Expected markup.
- * @param string $actual Actual markup.
- * @param string $message Message.
- */
- protected function assertEqualMarkup( $expected, $actual, $message = '' ) {
- $expected_dom = $this->parse_markup_fragment( $expected );
- $actual_dom = $this->parse_markup_fragment( $actual );
- foreach ( array( $expected_dom, $actual_dom ) as $dom ) {
- $xpath = new DOMXPath( $dom );
- /** @var DOMElement $script */
-
- // Normalize type attribute. When missing, it defaults to text/javascript.
- foreach ( $xpath->query( '//script[ not( @type ) ]' ) as $script ) {
- $script->setAttribute( 'type', 'text/javascript' );
- }
-
- // Normalize script contents to remove CDATA wrapper.
- foreach ( $xpath->query( '//script[ contains( text(), "<![CDATA[" ) ]' ) as $script ) {
- $script->textContent = str_replace(
- array(
- "/* <![CDATA[ */\n",
- "\n/* ]]> */",
- ),
- '',
- $script->textContent
- );
- }
-
- // Normalize XHTML-compatible boolean attributes to HTML5 ones.
- foreach ( array( 'async', 'defer' ) as $attribute ) {
- foreach ( iterator_to_array( $xpath->query( "//script[ @{$attribute} = '{$attribute}' ]" ) ) as $script ) {
- $script->removeAttribute( $attribute );
- $script->setAttributeNode( $dom->createAttribute( $attribute ) );
- }
- }
- }
-
- $this->assertEquals(
- $expected_dom->getElementsByTagName( 'body' )->item( 0 ),
- $actual_dom->getElementsByTagName( 'body' )->item( 0 ),
- $message
- );
- }
-
- /**
* Adds html5 script theme support.
*/
protected function add_html5_script_theme_support() {
@@ -3100,8 +3141,8 @@ HTML
wp_scripts()->do_footer_items();
$footer = ob_get_clean();
- $this->assertEqualMarkup( $expected_header, $header, 'Expected header script markup to match.' );
- $this->assertEqualMarkup( $expected_footer, $footer, 'Expected footer script markup to match.' );
+ $this->assertEqualHTML( $expected_header, $header, '<body>', 'Expected header script markup to match.' );
+ $this->assertEqualHTML( $expected_footer, $footer, '<body>', 'Expected footer script markup to match.' );
$this->assertEqualSets( $expected_in_footer, wp_scripts()->in_footer, 'Expected to have the same handles for in_footer.' );
$this->assertEquals( $expected_groups, wp_scripts()->groups, 'Expected groups to match.' );
}
diff --git a/tests/phpunit/tests/feed/atom.php b/tests/phpunit/tests/feed/atom.php
index dae234f51c..99d4e837af 100644
--- a/tests/phpunit/tests/feed/atom.php
+++ b/tests/phpunit/tests/feed/atom.php
@@ -279,6 +279,8 @@ class Tests_Feed_Atom extends WP_UnitTestCase {
$entries = xml_find( $xml, 'feed', 'entry' );
$entries = array_slice( $entries, 0, 1 );
+ $this->assertNotEmpty( $entries );
+
foreach ( $entries as $key => $entry ) {
$links = xml_find( $entries[ $key ]['child'], 'link' );
$i = 0;
diff --git a/tests/phpunit/tests/feed/rss2.php b/tests/phpunit/tests/feed/rss2.php
index 4eea99965c..c4f8807d7b 100644
--- a/tests/phpunit/tests/feed/rss2.php
+++ b/tests/phpunit/tests/feed/rss2.php
@@ -289,6 +289,8 @@ class Tests_Feed_RSS2 extends WP_UnitTestCase {
// Get all the rss -> channel -> item elements.
$items = xml_find( $xml, 'rss', 'channel', 'item' );
+ $this->assertNotEmpty( $items );
+
// Check each of the items against the known post data.
foreach ( $items as $key => $item ) {
// Get post for comparison.
diff --git a/tests/phpunit/tests/file.php b/tests/phpunit/tests/file.php
index c99f1baffd..9a29e4e676 100644
--- a/tests/phpunit/tests/file.php
+++ b/tests/phpunit/tests/file.php
@@ -40,6 +40,8 @@ class Tests_File extends WP_UnitTestCase {
'AuthorURI' => 'http://binarybonsai.com/',
);
+ $this->assertNotEmpty( $actual );
+
foreach ( $actual as $header => $value ) {
$this->assertSame( $expected[ $header ], $value, $header );
}
@@ -64,6 +66,8 @@ class Tests_File extends WP_UnitTestCase {
'Author' => 'A Very Old Mac',
);
+ $this->assertNotEmpty( $actual );
+
foreach ( $actual as $header => $value ) {
$this->assertSame( $expected[ $header ], $value, $header );
}
@@ -84,6 +88,8 @@ class Tests_File extends WP_UnitTestCase {
'TemplateName' => 'Something',
);
+ $this->assertNotEmpty( $actual );
+
foreach ( $actual as $header => $value ) {
$this->assertSame( $expected[ $header ], $value, $header );
}
diff --git a/tests/phpunit/tests/formatting/escUrl.php b/tests/phpunit/tests/formatting/escUrl.php
index 769ecacfdc..6fdd582617 100644
--- a/tests/phpunit/tests/formatting/escUrl.php
+++ b/tests/phpunit/tests/formatting/escUrl.php
@@ -124,7 +124,11 @@ class Tests_Formatting_EscUrl extends WP_UnitTestCase {
)
);
- foreach ( wp_allowed_protocols() as $scheme ) {
+ $protocols = wp_allowed_protocols();
+
+ $this->assertNotEmpty( $protocols );
+
+ foreach ( $protocols as $scheme ) {
$this->assertSame( "{$scheme}://example.com", esc_url( "{$scheme}://example.com" ), $scheme );
$this->assertSame(
"{$scheme}://example.com",
diff --git a/tests/phpunit/tests/formatting/sanitizeFileName.php b/tests/phpunit/tests/formatting/sanitizeFileName.php
index d0fac9e232..d0e366f121 100644
--- a/tests/phpunit/tests/formatting/sanitizeFileName.php
+++ b/tests/phpunit/tests/formatting/sanitizeFileName.php
@@ -35,13 +35,17 @@ class Tests_Formatting_SanitizeFileName extends WP_UnitTestCase {
* Test that spaces are correctly replaced with dashes.
*
* @ticket 16330
+ * @ticket 62995
*/
public function test_replaces_spaces() {
$urls = array(
- 'unencoded space.png' => 'unencoded-space.png',
- 'encoded-space.jpg' => 'encoded-space.jpg',
- 'plus+space.jpg' => 'plusspace.jpg',
- 'multi %20 +space.png' => 'multi-20-space.png',
+ 'unencoded space.png' => 'unencoded-space.png',
+ 'encoded-space.jpg' => 'encoded-space.jpg',
+ 'plus+space.jpg' => 'plusspace.jpg',
+ 'multi %20 +space.png' => 'multi-20-space.png',
+ "Screenshot 2025-02-19 at 2.17.33\u{202F}PM.png" => 'Screenshot-2025-02-19-at-2.17.33-PM.png',
+ "Filename with non-breaking\u{00A0}space.txt" => 'Filename-with-non-breaking-space.txt',
+ "Filename with thin\u{2009}space.txt" => 'Filename-with-thin-space.txt',
);
foreach ( $urls as $test => $expected ) {
diff --git a/tests/phpunit/tests/formatting/sanitizePost.php b/tests/phpunit/tests/formatting/sanitizePost.php
index 60ef8927aa..0366cf77c7 100644
--- a/tests/phpunit/tests/formatting/sanitizePost.php
+++ b/tests/phpunit/tests/formatting/sanitizePost.php
@@ -29,6 +29,9 @@ class Tests_Formatting_SanitizePost extends WP_UnitTestCase {
case 'string':
$this->assertIsString( $post->$field, "field $field" );
break;
+ default:
+ $this->fail( "Type $type is not handled by this test." );
+ break;
}
}
}
diff --git a/tests/phpunit/tests/formatting/wpSpecialchars.php b/tests/phpunit/tests/formatting/wpSpecialchars.php
index 890fb99c94..edbf29336c 100644
--- a/tests/phpunit/tests/formatting/wpSpecialchars.php
+++ b/tests/phpunit/tests/formatting/wpSpecialchars.php
@@ -17,6 +17,8 @@ class Tests_Formatting_wpSpecialchars extends WP_UnitTestCase {
public function test_allowed_entity_names() {
global $allowedentitynames;
+ $this->assertNotEmpty( $allowedentitynames );
+
// Allowed entities should be unchanged.
foreach ( $allowedentitynames as $ent ) {
if ( 'apos' === $ent ) {
diff --git a/tests/phpunit/tests/formatting/wpTrimExcerpt.php b/tests/phpunit/tests/formatting/wpTrimExcerpt.php
index 0f9c6e9cb7..688ba7731f 100644
--- a/tests/phpunit/tests/formatting/wpTrimExcerpt.php
+++ b/tests/phpunit/tests/formatting/wpTrimExcerpt.php
@@ -29,11 +29,12 @@ class Tests_Formatting_wpTrimExcerpt extends WP_UnitTestCase {
'post__in' => array( $post2 ),
)
);
- if ( $q->have_posts() ) {
- while ( $q->have_posts() ) {
- $q->the_post();
- $this->assertSame( 'Post 2 Page 1', wp_trim_excerpt() );
- }
+
+ $this->assertTrue( $q->have_posts() );
+
+ while ( $q->have_posts() ) {
+ $q->the_post();
+ $this->assertSame( 'Post 2 Page 1', wp_trim_excerpt() );
}
}
@@ -60,11 +61,12 @@ class Tests_Formatting_wpTrimExcerpt extends WP_UnitTestCase {
'post__in' => array( $post2 ),
)
);
- if ( $q->have_posts() ) {
- while ( $q->have_posts() ) {
- $q->the_post();
- $this->assertSame( 'Post 2 Page 1', wp_trim_excerpt() );
- }
+
+ $this->assertTrue( $q->have_posts() );
+
+ while ( $q->have_posts() ) {
+ $q->the_post();
+ $this->assertSame( 'Post 2 Page 1', wp_trim_excerpt() );
}
}
diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php
index d2775393a6..2c458d968c 100644
--- a/tests/phpunit/tests/functions.php
+++ b/tests/phpunit/tests/functions.php
@@ -1145,6 +1145,8 @@ class Tests_Functions extends WP_UnitTestCase {
public function test_wp_ext2type() {
$extensions = wp_get_ext_types();
+ $this->assertNotEmpty( $extensions );
+
foreach ( $extensions as $type => $extension_list ) {
foreach ( $extension_list as $extension ) {
$this->assertSame( $type, wp_ext2type( $extension ) );
diff --git a/tests/phpunit/tests/general/template.php b/tests/phpunit/tests/general/template.php
index 5aaf93be4e..d3b35a2c46 100644
--- a/tests/phpunit/tests/general/template.php
+++ b/tests/phpunit/tests/general/template.php
@@ -32,7 +32,29 @@ class Tests_General_Template extends WP_UnitTestCase {
*/
public static $home_page_id;
+ /**
+ * ID of the administrator user.
+ *
+ * @var int
+ */
+ public static $administrator_id;
+
+ /**
+ * ID of the author user.
+ *
+ * @var int
+ */
+ public static $author_id;
+
+ /**
+ * Set up the shared fixtures.
+ *
+ * @param WP_UnitTest_Factory $factory Factory instance.
+ */
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$administrator_id = $factory->user->create( array( 'role' => 'administrator' ) );
+ self::$author_id = $factory->user->create( array( 'role' => 'author' ) );
+
/*
* Declare theme support for custom logo.
*
@@ -212,7 +234,7 @@ class Tests_General_Template extends WP_UnitTestCase {
*/
public function test_customize_preview_wp_site_icon_empty() {
global $wp_customize;
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$administrator_id );
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
$wp_customize = new WP_Customize_Manager();
@@ -230,7 +252,7 @@ class Tests_General_Template extends WP_UnitTestCase {
*/
public function test_customize_preview_wp_site_icon_dirty() {
global $wp_customize;
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_set_current_user( self::$administrator_id );
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
$wp_customize = new WP_Customize_Manager();
@@ -763,16 +785,8 @@ class Tests_General_Template extends WP_UnitTestCase {
* @covers ::get_the_archive_title
*/
public function test_get_the_archive_title_is_correct_for_author_queries() {
- $user_with_posts = self::factory()->user->create_and_get(
- array(
- 'role' => 'author',
- )
- );
- $user_with_no_posts = self::factory()->user->create_and_get(
- array(
- 'role' => 'author',
- )
- );
+ $user_with_posts = get_user_by( 'id', self::$administrator_id );
+ $user_with_no_posts = get_user_by( 'id', self::$author_id );
self::factory()->post->create(
array(
diff --git a/tests/phpunit/tests/image/editorImagick.php b/tests/phpunit/tests/image/editorImagick.php
index 8b00f3ac12..1edbf579c3 100644
--- a/tests/phpunit/tests/image/editorImagick.php
+++ b/tests/phpunit/tests/image/editorImagick.php
@@ -770,6 +770,8 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
* @ticket 36477
*
* @dataProvider data_resizes_are_small_for_16bit_images
+ *
+ * @param string $file Path to the image file.
*/
public function test_resizes_are_small_for_16bit_images( $file ) {
@@ -783,7 +785,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
$imagick_image_editor->resize( $size['width'] * .5, $size['height'] * .5 );
- $saved = $imagick_image_editor->save( $temp_file );
+ $imagick_image_editor->save( $temp_file );
$new_filesize = filesize( $temp_file );
@@ -793,7 +795,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
}
/**
- * data_test_resizes_are_small_for_16bit
+ * Data provider for test_resizes_are_small_for_16bit_images.
*
* @return array[]
*/
@@ -816,4 +818,59 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
),
);
}
+
+ /**
+ * Tests that the 'png:IHDR.color-type-orig' property is preserved after resizing
+ * Used to identify indexed PNG images, see https://www.w3.org/TR/PNG-Chunks.html#C.IHDR.
+ *
+ * @ticket 63448
+ * @dataProvider data_png_color_type_after_resize
+ *
+ * @param string $file_path Path to the image file.
+ * @param int $expected_color_type The expected original color type.
+ */
+ public function test_png_color_type_is_preserved_after_resize( $file_path, $expected_color_type ) {
+
+ $temp_file = DIR_TESTDATA . '/images/test-temp.png';
+
+ $imagick_image_editor = new WP_Image_Editor_Imagick( $file_path );
+ $imagick_image_editor->load();
+
+ $size = $imagick_image_editor->get_size();
+ $imagick_image_editor->resize( $size['width'] * 0.5, $size['height'] * 0.5 );
+ $imagick_image_editor->save( $temp_file );
+
+ $imagick = new Imagick( $temp_file );
+ $actual_color_type = $imagick->getImageProperty( 'png:IHDR.color-type-orig' );
+
+ unlink( $temp_file );
+
+ $this->assertSame( (string) $expected_color_type, $actual_color_type, "The PNG original color type should be preserved after resize for {$file_path}." );
+ }
+
+ /**
+ * Data provider for test_png_color_type_is_preserved_after_resize.
+ *
+ * @return array[]
+ */
+ public static function data_png_color_type_after_resize() {
+ return array(
+ 'vivid-green-bird_color_type_6' => array(
+ DIR_TESTDATA . '/images/png-tests/vivid-green-bird.png',
+ 6, // RGBA.
+ ),
+ 'grayscale-test-image_color_type_4' => array(
+ DIR_TESTDATA . '/images/png-tests/grayscale-test-image.png',
+ 4, // Grayscale with Alpha.
+ ),
+ 'rabbit-time-paletted-or8_color_type_3' => array(
+ DIR_TESTDATA . '/images/png-tests/rabbit-time-paletted-or8.png',
+ 3, // Paletted.
+ ),
+ 'test8_color_type_3' => array(
+ DIR_TESTDATA . '/images/png-tests/test8.png',
+ 3, // Paletted.
+ ),
+ );
+ }
}
diff --git a/tests/phpunit/tests/kses.php b/tests/phpunit/tests/kses.php
index 16d447fd4c..256a3866ec 100644
--- a/tests/phpunit/tests/kses.php
+++ b/tests/phpunit/tests/kses.php
@@ -489,6 +489,8 @@ EOF;
$tags = wp_kses_allowed_html( 'post' );
+ $this->assertNotEmpty( $tags );
+
foreach ( $tags as $tag ) {
$this->assertTrue( $tag['class'] );
$this->assertTrue( $tag['dir'] );
diff --git a/tests/phpunit/tests/l10n.php b/tests/phpunit/tests/l10n.php
index 7926a804da..2f7992c340 100644
--- a/tests/phpunit/tests/l10n.php
+++ b/tests/phpunit/tests/l10n.php
@@ -16,6 +16,17 @@ class Tests_L10n extends WP_UnitTestCase {
private $long_text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
/**
+ * Editor user ID.
+ *
+ * @var int $editor_id
+ */
+ public static $editor_id;
+
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
+ }
+
+ /**
* @ticket 35961
*
* @covers ::_n_noop
@@ -464,7 +475,7 @@ class Tests_L10n extends WP_UnitTestCase {
* and return a string instead of null, which would otherwise cause a PHP 8.1
* "passing null to non-nullable" deprecation notice.
*/
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
+ wp_set_current_user( self::$editor_id );
$args = array(
'post_content' => $this->long_text,
@@ -503,7 +514,7 @@ class Tests_L10n extends WP_UnitTestCase {
* and return a string instead of null, which would otherwise cause a PHP 8.1
* "passing null to non-nullable" deprecation notice.
*/
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
+ wp_set_current_user( self::$editor_id );
$args = array(
'post_content' => $this->long_text,
@@ -542,7 +553,7 @@ class Tests_L10n extends WP_UnitTestCase {
* and return a string instead of null, which would otherwise cause a PHP 8.1
* "passing null to non-nullable" deprecation notice.
*/
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
+ wp_set_current_user( self::$editor_id );
$args = array(
'post_content' => str_repeat( 'あ', 200 ),
diff --git a/tests/phpunit/tests/l10n/getUserLocale.php b/tests/phpunit/tests/l10n/getUserLocale.php
index 76492b3b70..e4eaf7a260 100644
--- a/tests/phpunit/tests/l10n/getUserLocale.php
+++ b/tests/phpunit/tests/l10n/getUserLocale.php
@@ -9,34 +9,61 @@
class Tests_L10n_GetUserLocale extends WP_UnitTestCase {
protected $user_id;
- public function set_up() {
- parent::set_up();
+ /**
+ * ID of the administrator user with de_DE local.
+ *
+ * @var int
+ */
+ public static $administrator_de_de;
+
+ /**
+ * ID of the user with es_ES local.
+ *
+ * @var int
+ */
+ public static $user_es_es;
- $this->user_id = self::factory()->user->create(
+ /**
+ * Set up the shared fixtures.
+ *
+ * @param WP_UnitTest_Factory $factory Factory instance.
+ */
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$administrator_de_de = $factory->user->create(
array(
'role' => 'administrator',
'locale' => 'de_DE',
)
);
- wp_set_current_user( $this->user_id );
+ self::$user_es_es = self::factory()->user->create(
+ array(
+ 'locale' => 'es_ES',
+ )
+ );
+ }
+
+ public function set_up() {
+ parent::set_up();
+
+ wp_set_current_user( self::$administrator_de_de );
}
public function test_user_locale_property() {
set_current_screen( 'dashboard' );
$this->assertSame( 'de_DE', get_user_locale() );
- $this->assertSame( get_user_by( 'id', $this->user_id )->locale, get_user_locale() );
+ $this->assertSame( get_user_by( 'id', self::$administrator_de_de )->locale, get_user_locale() );
}
public function test_update_user_locale() {
set_current_screen( 'dashboard' );
- update_user_meta( $this->user_id, 'locale', 'fr_FR' );
+ update_user_meta( self::$administrator_de_de, 'locale', 'fr_FR' );
$this->assertSame( 'fr_FR', get_user_locale() );
}
public function test_returns_site_locale_if_empty() {
set_current_screen( 'dashboard' );
- update_user_meta( $this->user_id, 'locale', '' );
+ update_user_meta( self::$administrator_de_de, 'locale', '' );
$this->assertSame( get_locale(), get_user_locale() );
}
@@ -78,11 +105,7 @@ class Tests_L10n_GetUserLocale extends WP_UnitTestCase {
}
public function test_user_id_argument_with_id() {
- $user_id = self::factory()->user->create(
- array(
- 'locale' => 'es_ES',
- )
- );
+ $user_id = self::$user_es_es;
$user_locale1 = get_user_locale( $user_id );
@@ -95,11 +118,7 @@ class Tests_L10n_GetUserLocale extends WP_UnitTestCase {
}
public function test_user_id_argument_with_wp_user_object() {
- $user_id = self::factory()->user->create(
- array(
- 'locale' => 'es_ES',
- )
- );
+ $user_id = self::$user_es_es;
$user = get_user_by( 'id', $user_id );
diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php
index d8aac58d2a..a1e70c2ff1 100644
--- a/tests/phpunit/tests/media.php
+++ b/tests/phpunit/tests/media.php
@@ -999,7 +999,6 @@ VIDEO;
$content = apply_filters( 'the_content', $video );
$expected = '<div style="width: ' . $width . 'px;" class="wp-video">' .
- "<!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->\n" .
'<video class="wp-video-shortcode" id="video-' . $post_id . '-1" width="' . $width . '" height="' . $h . '" preload="metadata" controls="controls">' .
'<source type="video/mp4" src="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4?_=1" />' .
'<!-- WebM/VP8 for Firefox4, Opera, and Chrome --><source type="video/webm" src="myvideo.webm" />' .
@@ -1847,6 +1846,8 @@ EOF;
// Calculate a srcset array.
$sizes = explode( ', ', wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
+ $this->assertNotEmpty( $sizes );
+
// Test to confirm all sources in the array include the same edit hash.
foreach ( $sizes as $size ) {
$this->assertStringContainsString( $hash, $size );
@@ -3780,6 +3781,8 @@ EOF;
$query = $this->get_new_wp_query_for_published_post();
+ $this->assertTrue( have_posts() );
+
while ( have_posts() ) {
the_post();
@@ -3837,6 +3840,8 @@ EOF;
// Use the filter to alter the threshold for not lazy-loading to the first five elements.
$this->force_omit_loading_attr_threshold( 5 );
+ $this->assertTrue( have_posts() );
+
while ( have_posts() ) {
the_post();
diff --git a/tests/phpunit/tests/meta.php b/tests/phpunit/tests/meta.php
index 857414190a..6431a15861 100644
--- a/tests/phpunit/tests/meta.php
+++ b/tests/phpunit/tests/meta.php
@@ -9,16 +9,15 @@ class Tests_Meta extends WP_UnitTestCase {
/**
* @var \WP_User
*/
- private $author;
+ private static $author;
- private $meta_id;
- private $delete_meta_id;
+ private static $meta_id;
+ private static $delete_meta_id;
- public function set_up() {
- parent::set_up();
- $this->author = new WP_User( self::factory()->user->create( array( 'role' => 'author' ) ) );
- $this->meta_id = add_metadata( 'user', $this->author->ID, 'meta_key', 'meta_value' );
- $this->delete_meta_id = add_metadata( 'user', $this->author->ID, 'delete_meta_key', 'delete_meta_value' );
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$author = new WP_User( $factory->user->create( array( 'role' => 'author' ) ) );
+ self::$meta_id = add_metadata( 'user', self::$author->ID, 'meta_key', 'meta_value' );
+ self::$delete_meta_id = add_metadata( 'user', self::$author->ID, 'delete_meta_key', 'delete_meta_value' );
}
public function meta_sanitize_cb( $meta_value, $meta_key, $meta_type ) {
@@ -37,51 +36,51 @@ class Tests_Meta extends WP_UnitTestCase {
public function test_delete_metadata_by_mid() {
// Let's try and delete a non-existing ID, non existing meta.
$this->assertFalse( delete_metadata_by_mid( 'user', 0 ) );
- $this->assertFalse( delete_metadata_by_mid( 'non_existing_meta', $this->delete_meta_id ) );
+ $this->assertFalse( delete_metadata_by_mid( 'non_existing_meta', self::$delete_meta_id ) );
// Now let's delete the real meta data.
- $this->assertTrue( delete_metadata_by_mid( 'user', $this->delete_meta_id ) );
+ $this->assertTrue( delete_metadata_by_mid( 'user', self::$delete_meta_id ) );
// And make sure it's been deleted.
- $this->assertFalse( get_metadata_by_mid( 'user', $this->delete_meta_id ) );
+ $this->assertFalse( get_metadata_by_mid( 'user', self::$delete_meta_id ) );
// Make sure the caches are cleared.
- $this->assertFalse( (bool) get_user_meta( $this->author->ID, 'delete_meta_key' ) );
+ $this->assertFalse( (bool) get_user_meta( self::$author->ID, 'delete_meta_key' ) );
}
public function test_update_metadata_by_mid() {
// Setup.
- $meta = get_metadata_by_mid( 'user', $this->meta_id );
+ $meta = get_metadata_by_mid( 'user', self::$meta_id );
// Update the meta value.
- $this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, 'meta_new_value' ) );
- $meta = get_metadata_by_mid( 'user', $this->meta_id );
+ $this->assertTrue( update_metadata_by_mid( 'user', self::$meta_id, 'meta_new_value' ) );
+ $meta = get_metadata_by_mid( 'user', self::$meta_id );
$this->assertSame( 'meta_new_value', $meta->meta_value );
// Update the meta value.
- $this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, 'meta_new_value', 'meta_new_key' ) );
- $meta = get_metadata_by_mid( 'user', $this->meta_id );
+ $this->assertTrue( update_metadata_by_mid( 'user', self::$meta_id, 'meta_new_value', 'meta_new_key' ) );
+ $meta = get_metadata_by_mid( 'user', self::$meta_id );
$this->assertSame( 'meta_new_key', $meta->meta_key );
// Update the key and value.
- $this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, 'meta_value', 'meta_key' ) );
- $meta = get_metadata_by_mid( 'user', $this->meta_id );
+ $this->assertTrue( update_metadata_by_mid( 'user', self::$meta_id, 'meta_value', 'meta_key' ) );
+ $meta = get_metadata_by_mid( 'user', self::$meta_id );
$this->assertSame( 'meta_key', $meta->meta_key );
$this->assertSame( 'meta_value', $meta->meta_value );
// Update the value that has to be serialized.
- $this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, array( 'first', 'second' ) ) );
- $meta = get_metadata_by_mid( 'user', $this->meta_id );
+ $this->assertTrue( update_metadata_by_mid( 'user', self::$meta_id, array( 'first', 'second' ) ) );
+ $meta = get_metadata_by_mid( 'user', self::$meta_id );
$this->assertSame( array( 'first', 'second' ), $meta->meta_value );
// Let's try some invalid meta data.
$this->assertFalse( update_metadata_by_mid( 'user', 0, 'meta_value' ) );
- $this->assertFalse( update_metadata_by_mid( 'user', $this->meta_id, 'meta_value', array( 'invalid', 'key' ) ) );
+ $this->assertFalse( update_metadata_by_mid( 'user', self::$meta_id, 'meta_value', array( 'invalid', 'key' ) ) );
// Let's see if caches get cleared after updates.
- $meta = get_metadata_by_mid( 'user', $this->meta_id );
+ $meta = get_metadata_by_mid( 'user', self::$meta_id );
$first = get_user_meta( $meta->user_id, $meta->meta_key );
- $this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, 'other_meta_value' ) );
+ $this->assertTrue( update_metadata_by_mid( 'user', self::$meta_id, 'other_meta_value' ) );
$second = get_user_meta( $meta->user_id, $meta->meta_key );
$this->assertFalse( $first === $second );
}
@@ -107,14 +106,16 @@ class Tests_Meta extends WP_UnitTestCase {
$found = $this->updated_mids;
$this->updated_mids = array();
+ $this->assertNotEmpty( $found );
+
foreach ( $found as $action => $mids ) {
$this->assertCount( 2, $mids );
}
}
public function test_metadata_exists() {
- $this->assertFalse( metadata_exists( 'user', $this->author->ID, 'foobarbaz' ) );
- $this->assertTrue( metadata_exists( 'user', $this->author->ID, 'meta_key' ) );
+ $this->assertFalse( metadata_exists( 'user', self::$author->ID, 'foobarbaz' ) );
+ $this->assertTrue( metadata_exists( 'user', self::$author->ID, 'meta_key' ) );
$this->assertFalse( metadata_exists( 'user', 1234567890, 'foobarbaz' ) );
$this->assertFalse( metadata_exists( 'user', 1234567890, 'meta_key' ) );
}
@@ -125,7 +126,7 @@ class Tests_Meta extends WP_UnitTestCase {
public function test_metadata_exists_with_filter() {
// Let's see if it returns the correct value when adding a filter.
add_filter( 'get_user_metadata', '__return_zero' );
- $this->assertFalse( metadata_exists( 'user', $this->author->ID, 'meta_key' ) ); // Existing meta key.
+ $this->assertFalse( metadata_exists( 'user', self::$author->ID, 'meta_key' ) ); // Existing meta key.
$this->assertFalse( metadata_exists( 'user', 1234567890, 'meta_key' ) );
remove_filter( 'get_user_metadata', '__return_zero' );
}
@@ -148,7 +149,7 @@ class Tests_Meta extends WP_UnitTestCase {
$this->assertCount( 1, $u );
// User found is not locally defined author (it's the admin).
- $this->assertNotEquals( $this->author->user_login, $u[0]->user_login );
+ $this->assertNotEquals( self::$author->user_login, $u[0]->user_login );
// Test EXISTS and NOT EXISTS together, no users should be found.
$this->assertCount(
@@ -183,7 +184,7 @@ class Tests_Meta extends WP_UnitTestCase {
)
);
- delete_metadata( 'user', $this->author->ID, 'meta_key' );
+ delete_metadata( 'user', self::$author->ID, 'meta_key' );
$this->assertCount(
2,
@@ -206,28 +207,28 @@ class Tests_Meta extends WP_UnitTestCase {
$expected = 'Testsingleslash';
$value2 = 'Test\\\\doubleslash';
$expected2 = 'Test\\doubleslash';
- $this->assertFalse( metadata_exists( 'user', $this->author->ID, $key ) );
- $this->assertFalse( delete_metadata( 'user', $this->author->ID, $key ) );
- $this->assertSame( '', get_metadata( 'user', $this->author->ID, $key, true ) );
- $this->assertIsInt( add_metadata( 'user', $this->author->ID, $key, $value ) );
- $this->assertSame( $expected, get_metadata( 'user', $this->author->ID, $key, true ) );
- $this->assertTrue( delete_metadata( 'user', $this->author->ID, $key ) );
- $this->assertSame( '', get_metadata( 'user', $this->author->ID, $key, true ) );
- $this->assertIsInt( update_metadata( 'user', $this->author->ID, $key, $value ) );
- $this->assertSame( $expected, get_metadata( 'user', $this->author->ID, $key, true ) );
- $this->assertTrue( update_metadata( 'user', $this->author->ID, $key, 'blah' ) );
- $this->assertSame( 'blah', get_metadata( 'user', $this->author->ID, $key, true ) );
- $this->assertTrue( delete_metadata( 'user', $this->author->ID, $key ) );
- $this->assertSame( '', get_metadata( 'user', $this->author->ID, $key, true ) );
- $this->assertFalse( metadata_exists( 'user', $this->author->ID, $key ) );
+ $this->assertFalse( metadata_exists( 'user', self::$author->ID, $key ) );
+ $this->assertFalse( delete_metadata( 'user', self::$author->ID, $key ) );
+ $this->assertSame( '', get_metadata( 'user', self::$author->ID, $key, true ) );
+ $this->assertIsInt( add_metadata( 'user', self::$author->ID, $key, $value ) );
+ $this->assertSame( $expected, get_metadata( 'user', self::$author->ID, $key, true ) );
+ $this->assertTrue( delete_metadata( 'user', self::$author->ID, $key ) );
+ $this->assertSame( '', get_metadata( 'user', self::$author->ID, $key, true ) );
+ $this->assertIsInt( update_metadata( 'user', self::$author->ID, $key, $value ) );
+ $this->assertSame( $expected, get_metadata( 'user', self::$author->ID, $key, true ) );
+ $this->assertTrue( update_metadata( 'user', self::$author->ID, $key, 'blah' ) );
+ $this->assertSame( 'blah', get_metadata( 'user', self::$author->ID, $key, true ) );
+ $this->assertTrue( delete_metadata( 'user', self::$author->ID, $key ) );
+ $this->assertSame( '', get_metadata( 'user', self::$author->ID, $key, true ) );
+ $this->assertFalse( metadata_exists( 'user', self::$author->ID, $key ) );
// Test overslashing.
- $this->assertIsInt( add_metadata( 'user', $this->author->ID, $key, $value2 ) );
- $this->assertSame( $expected2, get_metadata( 'user', $this->author->ID, $key, true ) );
- $this->assertTrue( delete_metadata( 'user', $this->author->ID, $key ) );
- $this->assertSame( '', get_metadata( 'user', $this->author->ID, $key, true ) );
- $this->assertIsInt( update_metadata( 'user', $this->author->ID, $key, $value2 ) );
- $this->assertSame( $expected2, get_metadata( 'user', $this->author->ID, $key, true ) );
+ $this->assertIsInt( add_metadata( 'user', self::$author->ID, $key, $value2 ) );
+ $this->assertSame( $expected2, get_metadata( 'user', self::$author->ID, $key, true ) );
+ $this->assertTrue( delete_metadata( 'user', self::$author->ID, $key ) );
+ $this->assertSame( '', get_metadata( 'user', self::$author->ID, $key, true ) );
+ $this->assertIsInt( update_metadata( 'user', self::$author->ID, $key, $value2 ) );
+ $this->assertSame( $expected2, get_metadata( 'user', self::$author->ID, $key, true ) );
}
/**
@@ -344,7 +345,7 @@ class Tests_Meta extends WP_UnitTestCase {
* @ticket 37746
*/
public function test_negative_meta_id() {
- $negative_mid = $this->meta_id * -1;
+ $negative_mid = self::$meta_id * -1;
$this->assertLessThan( 0, $negative_mid );
$this->assertFalse( get_metadata_by_mid( 'user', $negative_mid ) );
@@ -356,7 +357,7 @@ class Tests_Meta extends WP_UnitTestCase {
* @ticket 37746
*/
public function test_floating_meta_id() {
- $floating_mid = $this->meta_id + 0.1337;
+ $floating_mid = self::$meta_id + 0.1337;
$this->assertTrue( floor( $floating_mid ) !== $floating_mid );
$this->assertFalse( get_metadata_by_mid( 'user', $floating_mid ) );
@@ -368,7 +369,7 @@ class Tests_Meta extends WP_UnitTestCase {
* @ticket 37746
*/
public function test_string_point_zero_meta_id() {
- $meta_id = add_metadata( 'user', $this->author->ID, 'meta_key', 'meta_value_2' );
+ $meta_id = add_metadata( 'user', self::$author->ID, 'meta_key', 'meta_value_2' );
$string_mid = "{$meta_id}.0";
@@ -385,8 +386,8 @@ class Tests_Meta extends WP_UnitTestCase {
public function test_get_metadata_with_empty_key_array_value() {
$data = array( 1, 2 );
$value = serialize( $data );
- add_metadata( 'user', $this->author->ID, 'foo', $data );
- $found = get_metadata( 'user', $this->author->ID );
+ add_metadata( 'user', self::$author->ID, 'foo', $data );
+ $found = get_metadata( 'user', self::$author->ID );
$this->assertSame( array( $value ), $found['foo'] );
}
@@ -398,8 +399,8 @@ class Tests_Meta extends WP_UnitTestCase {
$data = new stdClass();
$data->foo = 'bar';
$value = serialize( $data );
- add_metadata( 'user', $this->author->ID, 'foo', $data );
- $found = get_metadata( 'user', $this->author->ID );
+ add_metadata( 'user', self::$author->ID, 'foo', $data );
+ $found = get_metadata( 'user', self::$author->ID );
$this->assertSame( array( $value ), $found['foo'] );
}
@@ -413,8 +414,8 @@ class Tests_Meta extends WP_UnitTestCase {
array( 3, 4 ),
);
$value = serialize( $data );
- add_metadata( 'user', $this->author->ID, 'foo', $data );
- $found = get_metadata( 'user', $this->author->ID );
+ add_metadata( 'user', self::$author->ID, 'foo', $data );
+ $found = get_metadata( 'user', self::$author->ID );
$this->assertSame( array( $value ), $found['foo'] );
}
diff --git a/tests/phpunit/tests/oembed/controller.php b/tests/phpunit/tests/oembed/controller.php
index 9011b789bb..aa0275c4c8 100644
--- a/tests/phpunit/tests/oembed/controller.php
+++ b/tests/phpunit/tests/oembed/controller.php
@@ -304,11 +304,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
}
public function test_request_json() {
- $user = self::factory()->user->create_and_get(
- array(
- 'display_name' => 'John Doe',
- )
- );
+ $user = get_user_by( 'id', self::$subscriber );
$post = self::factory()->post->create_and_get(
array(
'post_author' => $user->ID,
@@ -391,11 +387,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
}
public function test_request_xml() {
- $user = self::factory()->user->create_and_get(
- array(
- 'display_name' => 'John Doe',
- )
- );
+ $user = get_user_by( 'id', self::$subscriber );
$post = self::factory()->post->create_and_get(
array(
'post_author' => $user->ID,
@@ -461,11 +453,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
}
public function test_rest_pre_serve_request() {
- $user = self::factory()->user->create_and_get(
- array(
- 'display_name' => 'John Doe',
- )
- );
+ $user = get_user_by( 'id', self::$subscriber );
$post = self::factory()->post->create_and_get(
array(
'post_author' => $user->ID,
@@ -677,11 +665,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
public function test_proxy_with_internal_url() {
wp_set_current_user( self::$editor );
- $user = self::factory()->user->create_and_get(
- array(
- 'display_name' => 'John Doe',
- )
- );
+ $user = get_user_by( 'id', self::$subscriber );
$post = self::factory()->post->create_and_get(
array(
'post_author' => $user->ID,
diff --git a/tests/phpunit/tests/oembed/wpOembed.php b/tests/phpunit/tests/oembed/wpOembed.php
index 051a7efe3d..76d733dbce 100644
--- a/tests/phpunit/tests/oembed/wpOembed.php
+++ b/tests/phpunit/tests/oembed/wpOembed.php
@@ -11,6 +11,22 @@ class Tests_WP_oEmbed extends WP_UnitTestCase {
public $pre_oembed_result_filtered = false;
+ /**
+ * ID of the user.
+ *
+ * @var int
+ */
+ public static $user_id;
+
+ /**
+ * Set up the shared fixture.
+ *
+ * @param WP_UnitTest_Factory $factory Factory instance.
+ */
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$user_id = $factory->user->create();
+ }
+
public function set_up() {
parent::set_up();
@@ -96,7 +112,7 @@ class Tests_WP_oEmbed extends WP_UnitTestCase {
* @group ms-required
*/
public function test_wp_filter_pre_oembed_result_multisite_sub_samesub() {
- $user_id = self::factory()->user->create();
+ $user_id = self::$user_id;
$blog_id = self::factory()->blog->create(
array(
@@ -125,7 +141,7 @@ class Tests_WP_oEmbed extends WP_UnitTestCase {
* @group ms-required
*/
public function test_wp_filter_pre_oembed_result_multisite_sub_othersub() {
- $user_id = self::factory()->user->create();
+ $user_id = self::$user_id;
$blog_id = self::factory()->blog->create(
array(
@@ -164,7 +180,7 @@ class Tests_WP_oEmbed extends WP_UnitTestCase {
public function test_wp_filter_pre_oembed_result_multisite_sub_main() {
$post_id = self::factory()->post->create();
$permalink = get_permalink( $post_id );
- $user_id = self::factory()->user->create();
+ $user_id = self::$user_id;
$blog_id = self::factory()->blog->create(
array(
'user_id' => $user_id,
@@ -189,7 +205,7 @@ class Tests_WP_oEmbed extends WP_UnitTestCase {
* @group ms-required
*/
public function test_wp_filter_pre_oembed_result_multisite_preserves_switched_state() {
- $user_id = self::factory()->user->create();
+ $user_id = self::$user_id;
$blog_id = self::factory()->blog->create( array( 'user_id' => $user_id ) );
switch_to_blog( $blog_id );
@@ -220,7 +236,7 @@ class Tests_WP_oEmbed extends WP_UnitTestCase {
public function test_wp_filter_pre_oembed_result_multisite_restores_state_if_no_post_is_found() {
$current_blog_id = get_current_blog_id();
- $user_id = self::factory()->user->create();
+ $user_id = self::$user_id;
$blog_id = self::factory()->blog->create(
array(
'user_id' => $user_id,
diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php
index 6e0207add2..36c337939e 100644
--- a/tests/phpunit/tests/post.php
+++ b/tests/phpunit/tests/post.php
@@ -269,6 +269,9 @@ class Tests_Post extends WP_UnitTestCase {
$terms = get_terms( $tax );
$term = reset( $terms );
+ $this->assertNotEmpty( $matches );
+ $this->assertNotEmpty( $matches[1] );
+
foreach ( $matches[1] as $url ) {
$this->assertStringContainsString( 'tag_ID=' . $term->term_id, $url );
$this->assertStringContainsString( 'post_type=new_post_type', $url );
diff --git a/tests/phpunit/tests/post/getPages.php b/tests/phpunit/tests/post/getPages.php
index 76735bae86..dad85ddc69 100644
--- a/tests/phpunit/tests/post/getPages.php
+++ b/tests/phpunit/tests/post/getPages.php
@@ -6,6 +6,42 @@
* @covers ::get_pages
*/
class Tests_Post_GetPages extends WP_UnitTestCase {
+
+ /**
+ * ID of the first author.
+ *
+ * @var int
+ */
+ public static $author_id_1;
+
+ /**
+ * ID of the second author.
+ *
+ * @var int
+ */
+ public static $author_id_2;
+
+ /**
+ * Set up the shared fixture.
+ *
+ * @param WP_UnitTest_Factory $factory Factory instance.
+ */
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$author_id_1 = $factory->user->create(
+ array(
+ 'user_login' => 'author1',
+ 'role' => 'author',
+ )
+ );
+
+ self::$author_id_2 = $factory->user->create(
+ array(
+ 'user_login' => 'author2',
+ 'role' => 'author',
+ )
+ );
+ }
+
/**
* @ticket 23167
*/
@@ -963,12 +999,7 @@ class Tests_Post_GetPages extends WP_UnitTestCase {
* @ticket 12821
*/
public function test_get_pages_author() {
- $author_1 = self::factory()->user->create(
- array(
- 'user_login' => 'author1',
- 'role' => 'author',
- )
- );
+ $author_1 = self::$author_id_1;
$posts = self::factory()->post->create_many(
2,
array(
@@ -989,12 +1020,7 @@ class Tests_Post_GetPages extends WP_UnitTestCase {
* @ticket 12821
*/
public function test_get_pages_multiple_authors() {
- $author_1 = self::factory()->user->create(
- array(
- 'user_login' => 'author1',
- 'role' => 'author',
- )
- );
+ $author_1 = self::$author_id_1;
$post_1 = self::factory()->post->create(
array(
'post_title' => 'Page 1',
@@ -1004,12 +1030,7 @@ class Tests_Post_GetPages extends WP_UnitTestCase {
)
);
- $author_2 = self::factory()->user->create(
- array(
- 'user_login' => 'author2',
- 'role' => 'author',
- )
- );
+ $author_2 = self::$author_id_2;
$post_2 = self::factory()->post->create(
array(
'post_title' => 'Page 2',
@@ -1031,12 +1052,7 @@ class Tests_Post_GetPages extends WP_UnitTestCase {
* @ticket 12821
*/
public function test_get_pages_multiple_authors_by_user_login() {
- $author_1 = self::factory()->user->create(
- array(
- 'user_login' => 'author1',
- 'role' => 'author',
- )
- );
+ $author_1 = self::$author_id_1;
$post_1 = self::factory()->post->create(
array(
'post_title' => 'Page 1',
@@ -1046,12 +1062,7 @@ class Tests_Post_GetPages extends WP_UnitTestCase {
)
);
- $author_2 = self::factory()->user->create(
- array(
- 'user_login' => 'author2',
- 'role' => 'author',
- )
- );
+ $author_2 = self::$author_id_2;
$post_2 = self::factory()->post->create(
array(
'post_title' => 'Page 2',
diff --git a/tests/phpunit/tests/post/getPostsByAuthorSql.php b/tests/phpunit/tests/post/getPostsByAuthorSql.php
index fc6f1e8526..e2657a3fdf 100644
--- a/tests/phpunit/tests/post/getPostsByAuthorSql.php
+++ b/tests/phpunit/tests/post/getPostsByAuthorSql.php
@@ -5,6 +5,30 @@
*/
class Tests_Post_GetPostsByAuthorSql extends WP_UnitTestCase {
+ /**
+ * ID of the first user.
+ *
+ * @var int
+ */
+ public static $user_id_1;
+
+ /**
+ * ID of the second user.
+ *
+ * @var int
+ */
+ public static $user_id_2;
+
+ /**
+ * Set up the shared fixture.
+ *
+ * @param WP_UnitTest_Factory $factory Factory instance.
+ */
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$user_id_1 = $factory->user->create();
+ self::$user_id_2 = $factory->user->create();
+ }
+
public function test_post_type_post() {
$maybe_string = get_posts_by_author_sql( 'post' );
$this->assertStringContainsString( "post_type = 'post'", $maybe_string );
@@ -59,7 +83,7 @@ class Tests_Post_GetPostsByAuthorSql extends WP_UnitTestCase {
public function test_public_only_true_should_not_allow_any_private_posts_for_loggedin_user() {
$current_user = get_current_user_id();
- $u = self::factory()->user->create();
+ $u = self::$user_id_1;
wp_set_current_user( $u );
$maybe_string = get_posts_by_author_sql( 'post', true, $u, true );
@@ -70,7 +94,7 @@ class Tests_Post_GetPostsByAuthorSql extends WP_UnitTestCase {
public function test_public_only_should_default_to_false() {
$current_user = get_current_user_id();
- $u = self::factory()->user->create();
+ $u = self::$user_id_1;
wp_set_current_user( $u );
$this->assertSame( get_posts_by_author_sql( 'post', true, $u, false ), get_posts_by_author_sql( 'post', true, $u ) );
@@ -80,7 +104,7 @@ class Tests_Post_GetPostsByAuthorSql extends WP_UnitTestCase {
public function test_public_only_false_should_allow_current_user_access_to_own_private_posts_when_current_user_matches_post_author() {
$current_user = get_current_user_id();
- $u = self::factory()->user->create();
+ $u = self::$user_id_1;
wp_set_current_user( $u );
$maybe_string = get_posts_by_author_sql( 'post', true, $u, false );
@@ -91,8 +115,8 @@ class Tests_Post_GetPostsByAuthorSql extends WP_UnitTestCase {
public function test_public_only_false_should_not_allow_access_to_private_posts_if_current_user_is_not_post_author() {
$current_user = get_current_user_id();
- $u1 = self::factory()->user->create();
- $u2 = self::factory()->user->create();
+ $u1 = self::$user_id_1;
+ $u2 = self::$user_id_2;
wp_set_current_user( $u1 );
$maybe_string = get_posts_by_author_sql( 'post', true, $u2, false );
@@ -103,7 +127,7 @@ class Tests_Post_GetPostsByAuthorSql extends WP_UnitTestCase {
public function test_public_only_false_should_allow_current_user_access_to_own_private_posts_when_post_author_is_not_provided() {
$current_user = get_current_user_id();
- $u = self::factory()->user->create();
+ $u = self::$user_id_1;
wp_set_current_user( $u );
$maybe_string = get_posts_by_author_sql( 'post', true, $u, false );
diff --git a/tests/phpunit/tests/post/revisions.php b/tests/phpunit/tests/post/revisions.php
index a4e45aefcf..699c10d29b 100644
--- a/tests/phpunit/tests/post/revisions.php
+++ b/tests/phpunit/tests/post/revisions.php
@@ -230,6 +230,8 @@ class Tests_Post_Revisions extends WP_UnitTestCase {
$this->assertCount( 1, $revisions );
$this->assertTrue( user_can( self::$editor_user_id, 'read_post', $post_id ) );
+ $this->assertNotEmpty( $revisions );
+
foreach ( $revisions as $revision ) {
$this->assertTrue( user_can( self::$editor_user_id, 'read_post', $revision->ID ) );
}
diff --git a/tests/phpunit/tests/post/updateTermCountOnTransitionPostStatus.php b/tests/phpunit/tests/post/updateTermCountOnTransitionPostStatus.php
new file mode 100644
index 0000000000..8467f8e92d
--- /dev/null
+++ b/tests/phpunit/tests/post/updateTermCountOnTransitionPostStatus.php
@@ -0,0 +1,180 @@
+<?php
+
+/**
+ * Tests for the _update_term_count_on_transition_post_status function.
+ *
+ * See Tests_Term_WpSetObjectTerms for tests that cover changing terms on a post when saving it.
+ *
+ * @group taxonomy
+ *
+ * @covers ::_update_term_count_on_transition_post_status
+ */
+class Tests_Taxonomy_UpdateTermCountOnTransitionPostStatus extends WP_UnitTestCase {
+
+ /**
+ * @var int Post ID.
+ */
+ protected static $post_id;
+
+ /**
+ * @var int Term ID.
+ */
+ protected static $term_id;
+
+ /**
+ * @var string Post type.
+ */
+ protected static $post_type = 'post';
+
+ /**
+ * @var string Taxonomy name.
+ */
+ protected static $taxonomy = 'category';
+
+ /**
+ * Create shared fixtures.
+ */
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$post_id = $factory->post->create(
+ array(
+ 'post_type' => self::$post_type,
+ 'post_status' => 'publish',
+ )
+ );
+
+ self::$term_id = $factory->term->create(
+ array(
+ 'taxonomy' => self::$taxonomy,
+ 'name' => 'Test Category',
+ )
+ );
+
+ wp_set_object_terms( self::$post_id, self::$term_id, self::$taxonomy );
+ }
+
+ /**
+ * Test that the term count is updated when a post is published.
+ *
+ * @ticket 42522
+ */
+ public function test_update_term_count_on_publish() {
+ $this->assertTermCount( 1, self::$term_id );
+
+ // Change post status to draft.
+ wp_update_post(
+ array(
+ 'ID' => self::$post_id,
+ 'post_status' => 'draft',
+ )
+ );
+
+ $this->assertTermCount( 0, self::$term_id );
+
+ // Change post status back to publish.
+ wp_update_post(
+ array(
+ 'ID' => self::$post_id,
+ 'post_status' => 'publish',
+ )
+ );
+
+ $this->assertTermCount( 1, self::$term_id );
+ }
+
+ /**
+ * Test that the term count is updated when a post is moved to trash.
+ *
+ * @ticket 42522
+ */
+ public function test_update_term_count_on_trash() {
+ $this->assertTermCount( 1, self::$term_id );
+
+ // Move post to trash.
+ wp_trash_post( self::$post_id );
+
+ $this->assertTermCount( 0, self::$term_id );
+ }
+
+ /**
+ * Test that the term count is updated when a post is restored from trash.
+ *
+ * @ticket 42522
+ */
+ public function test_update_term_count_on_restore() {
+ $this->assertTermCount( 1, self::$term_id );
+
+ // Move post to trash.
+ wp_trash_post( self::$post_id );
+
+ $this->assertTermCount( 0, self::$term_id, 'Post is in trash.' );
+
+ // Restore post from trash.
+ wp_untrash_post( self::$post_id );
+
+ $this->assertTermCount( 0, self::$term_id, 'Post is in draft after untrashing.' );
+
+ // re-publish post.
+ wp_publish_post( self::$post_id );
+
+ $this->assertTermCount( 1, self::$term_id, 'Post is in publish after publishing.' );
+ }
+
+ /**
+ * Test that the term count is updated when a post is deleted permanently.
+ *
+ * @ticket 42522
+ */
+ public function test_update_term_count_on_delete() {
+ $this->assertTermCount( 1, self::$term_id );
+
+ // Delete post permanently.
+ wp_delete_post( self::$post_id, true );
+
+ $this->assertTermCount( 0, self::$term_id );
+ }
+
+ /**
+ * Test that the term count is not recalculated when neither the terms nor the post status change.
+ *
+ * @ticket 42522
+ */
+ public function test_term_count_is_not_recalculated_when_status_does_not_change() {
+ $post_id = self::factory()->post->create(
+ array(
+ 'post_type' => self::$post_type,
+ 'post_status' => 'publish',
+ )
+ );
+
+ wp_set_object_terms(
+ $post_id,
+ self::$term_id,
+ self::$taxonomy
+ );
+ $edited_term_taxonomy_count = did_action( 'edited_term_taxonomy' );
+
+ // Change something about the post but not its status.
+ wp_update_post(
+ array(
+ 'ID' => $post_id,
+ 'post_content' => get_post( $post_id )->post_content . ' - updated',
+ )
+ );
+
+ $this->assertSame( 0, did_action( 'edited_term_taxonomy' ) - $edited_term_taxonomy_count, 'Term taxonomy count should not be recalculated when post status does not change.' );
+ $this->assertTermCount( 2, self::$term_id );
+ }
+
+ /**
+ * Assert that the term count is correct.
+ *
+ * @since 6.9.0
+ *
+ * @param int $expected_count Expected term count.
+ * @param int $term_id Term ID.
+ */
+ protected function assertTermCount( $expected_count, $term_id, $message = '' ) {
+ $term = get_term( $term_id );
+ $this->assertSame( $expected_count, $term->count, $message );
+ }
+}
diff --git a/tests/phpunit/tests/post/wpPostType.php b/tests/phpunit/tests/post/wpPostType.php
index 0eb9f1e203..23ec325a65 100644
--- a/tests/phpunit/tests/post/wpPostType.php
+++ b/tests/phpunit/tests/post/wpPostType.php
@@ -7,6 +7,8 @@ class Tests_Post_WP_Post_Type extends WP_UnitTestCase {
public function test_instances() {
global $wp_post_types;
+ $this->assertNotEmpty( $wp_post_types );
+
foreach ( $wp_post_types as $post_type ) {
$this->assertInstanceOf( 'WP_Post_Type', $post_type );
}
diff --git a/tests/phpunit/tests/query.php b/tests/phpunit/tests/query.php
index 37dd51a063..40d23816d1 100644
--- a/tests/phpunit/tests/query.php
+++ b/tests/phpunit/tests/query.php
@@ -17,6 +17,9 @@ class Tests_Query extends WP_UnitTestCase {
$nested_post_id = self::factory()->post->create();
$first_query = new WP_Query( array( 'post__in' => array( $post_id ) ) );
+
+ $this->assertTrue( $first_query->have_posts() );
+
while ( $first_query->have_posts() ) {
$first_query->the_post();
$second_query = new WP_Query( array( 'post__in' => array( $nested_post_id ) ) );
diff --git a/tests/phpunit/tests/query/conditionals.php b/tests/phpunit/tests/query/conditionals.php
index 05278ffd7a..4b47317889 100644
--- a/tests/phpunit/tests/query/conditionals.php
+++ b/tests/phpunit/tests/query/conditionals.php
@@ -14,6 +14,22 @@ class Tests_Query_Conditionals extends WP_UnitTestCase {
protected $page_ids;
protected $post_ids;
+ /**
+ * ID of the user-a.
+ *
+ * @var int
+ */
+ public static $user_a_id;
+
+ /**
+ * Set up the shared fixture.
+ *
+ * @param WP_UnitTest_Factory $factory Factory instance.
+ */
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$user_a_id = $factory->user->create( array( 'user_login' => 'user-a' ) );
+ }
+
public function set_up() {
parent::set_up();
@@ -588,7 +604,6 @@ class Tests_Query_Conditionals extends WP_UnitTestCase {
// 'author/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?author_name=$matches[1]&feed=$matches[2]',
// 'author/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?author_name=$matches[1]&feed=$matches[2]',
public function test_author_feed() {
- self::factory()->user->create( array( 'user_login' => 'user-a' ) );
// Check the long form.
$types = array( 'feed', 'rdf', 'rss', 'rss2', 'atom' );
foreach ( $types as $type ) {
@@ -607,7 +622,7 @@ class Tests_Query_Conditionals extends WP_UnitTestCase {
// 'author/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?author_name=$matches[1]&paged=$matches[2]',
public function test_author_paged() {
update_option( 'posts_per_page', 2 );
- $user_id = self::factory()->user->create( array( 'user_login' => 'user-a' ) );
+ $user_id = self::$user_a_id;
self::factory()->post->create_many( 3, array( 'post_author' => $user_id ) );
$this->go_to( '/author/user-a/page/2/' );
$this->assertQueryTrue( 'is_archive', 'is_author', 'is_paged' );
@@ -615,14 +630,13 @@ class Tests_Query_Conditionals extends WP_UnitTestCase {
// 'author/([^/]+)/?$' => 'index.php?author_name=$matches[1]',
public function test_author() {
- $user_id = self::factory()->user->create( array( 'user_login' => 'user-a' ) );
+ $user_id = self::$user_a_id;
self::factory()->post->create( array( 'post_author' => $user_id ) );
$this->go_to( '/author/user-a/' );
$this->assertQueryTrue( 'is_archive', 'is_author' );
}
public function test_author_with_no_posts() {
- $user_id = self::factory()->user->create( array( 'user_login' => 'user-a' ) );
$this->go_to( '/author/user-a/' );
$this->assertQueryTrue( 'is_archive', 'is_author' );
}
@@ -1122,7 +1136,7 @@ class Tests_Query_Conditionals extends WP_UnitTestCase {
* @ticket 24674
*/
public function test_is_author_with_nicename_that_begins_with_a_number_that_clashes_with_another_author_id() {
- $u1 = self::factory()->user->create();
+ $u1 = self::$user_a_id;
$u2_name = $u1 . '_user';
$u2 = self::factory()->user->create(
@@ -1279,7 +1293,7 @@ class Tests_Query_Conditionals extends WP_UnitTestCase {
global $wpdb;
// We need a non-post that shares an ID with a post assigned a template.
- $user_id = self::factory()->user->create();
+ $user_id = self::$user_a_id;
if ( ! get_post( $user_id ) ) {
$post_id = self::factory()->post->create( array( 'post_type' => 'post' ) );
$wpdb->update( $wpdb->posts, array( 'ID' => $user_id ), array( 'ID' => $post_id ), array( '%d' ) );
@@ -1352,12 +1366,7 @@ class Tests_Query_Conditionals extends WP_UnitTestCase {
* @ticket 35902
*/
public function test_is_author_should_not_match_numeric_id_to_nickname_beginning_with_id() {
- $u1 = self::factory()->user->create(
- array(
- 'nickname' => 'Foo',
- 'user_nicename' => 'foo',
- )
- );
+ $u1 = self::$user_a_id;
$u2 = self::factory()->user->create(
array(
'nickname' => "$u1 Foo",
@@ -1375,12 +1384,7 @@ class Tests_Query_Conditionals extends WP_UnitTestCase {
* @ticket 35902
*/
public function test_is_author_should_not_match_numeric_id_to_user_nicename_beginning_with_id() {
- $u1 = self::factory()->user->create(
- array(
- 'nickname' => 'Foo',
- 'user_nicename' => 'foo',
- )
- );
+ $u1 = self::$user_a_id;
$u2 = self::factory()->user->create(
array(
'nickname' => 'Foo',
diff --git a/tests/phpunit/tests/query/postStatus.php b/tests/phpunit/tests/query/postStatus.php
index 238551b524..95e7968951 100644
--- a/tests/phpunit/tests/query/postStatus.php
+++ b/tests/phpunit/tests/query/postStatus.php
@@ -240,7 +240,11 @@ class Tests_Query_PostStatus extends WP_UnitTestCase {
)
);
- foreach ( get_post_stati( array( 'public' => true ) ) as $status ) {
+ $stati = get_post_stati( array( 'public' => true ) );
+
+ $this->assertNotEmpty( $stati );
+
+ foreach ( $stati as $status ) {
$this->assertStringContainsString( "post_status = '$status'", $q->request );
}
}
diff --git a/tests/phpunit/tests/query/setupPostdata.php b/tests/phpunit/tests/query/setupPostdata.php
index 985ae06f7a..ef6b3479d8 100644
--- a/tests/phpunit/tests/query/setupPostdata.php
+++ b/tests/phpunit/tests/query/setupPostdata.php
@@ -121,6 +121,9 @@ class Tests_Query_SetupPostdata extends WP_UnitTestCase {
'posts_per_page' => 1,
)
);
+
+ $this->assertTrue( $q->have_posts() );
+
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
@@ -221,6 +224,9 @@ class Tests_Query_SetupPostdata extends WP_UnitTestCase {
'post__in' => array( $post2 ),
)
);
+
+ $this->assertTrue( $q->have_posts() );
+
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
@@ -281,6 +287,9 @@ class Tests_Query_SetupPostdata extends WP_UnitTestCase {
'posts_per_page' => 1,
)
);
+
+ $this->assertTrue( $q->have_posts() );
+
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
@@ -367,6 +376,9 @@ class Tests_Query_SetupPostdata extends WP_UnitTestCase {
'posts_per_page' => 1,
)
);
+
+ $this->assertTrue( $q->have_posts() );
+
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
diff --git a/tests/phpunit/tests/readme.php b/tests/phpunit/tests/readme.php
index 85ffffcdf6..b9c643e221 100644
--- a/tests/phpunit/tests/readme.php
+++ b/tests/phpunit/tests/readme.php
@@ -70,15 +70,21 @@ class Tests_Readme extends WP_UnitTestCase {
$readme = file_get_contents( ABSPATH . 'readme.html' );
preg_match( '#Recommendations.*MariaDB</a> version <strong>([0-9.]*)#s', $readme, $matches );
- $matches[1] = str_replace( '.', '', $matches[1] );
- $response_body = $this->get_response_body( "https://mariadb.com/kb/en/release-notes-mariadb-{$matches[1]}-series/" );
+ $response_body = $this->get_response_body( 'https://downloads.mariadb.org/rest-api/mariadb/' );
+ $releases = json_decode( $response_body, true );
- // Retrieve the date of the first stable release for the recommended branch.
- preg_match( '#.*Stable.*?(\d{2} [A-Za-z]{3} \d{4})#s', $response_body, $mariadb_matches );
+ foreach ( $releases['major_releases'] as $release ) {
+ if ( isset( $release['release_id'] ) && $release['release_id'] === $matches[1] ) {
+ $mariadb_eol = $release['release_eol_date'];
+ }
+ }
+
+ // If the release ID is not found the version is unsupported.
+ if ( ! isset( $mariadb_eol ) ) {
+ $this->fail( "{$matches[1]} is not included in MariaDB's list of supported versions. Remember to update the WordPress.org Requirements page, too." );
+ }
- // Per https://mariadb.org/about/#maintenance-policy, MariaDB releases are supported for 5 years.
- $mariadb_eol = gmdate( 'Y-m-d', strtotime( $mariadb_matches[1] . ' +5 years' ) );
$current_date = gmdate( 'Y-m-d' );
$this->assertLessThan( $mariadb_eol, $current_date, "readme.html's Recommended MariaDB version is too old. Remember to update the WordPress.org Requirements page, too." );
diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php
index 647d5df03d..0bfe4e778d 100644
--- a/tests/phpunit/tests/rest-api/rest-comments-controller.php
+++ b/tests/phpunit/tests/rest-api/rest-comments-controller.php
@@ -3442,9 +3442,7 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$this->assertSame( $comment->comment_author_IP, $data['author_ip'] );
$this->assertSame( $comment->comment_agent, $data['author_user_agent'] );
$this->assertSame( $comment->comment_content, $data['content']['raw'] );
- }
-
- if ( 'edit' !== $context ) {
+ } else {
$this->assertArrayNotHasKey( 'author_email', $data );
$this->assertArrayNotHasKey( 'author_ip', $data );
$this->assertArrayNotHasKey( 'author_user_agent', $data );
diff --git a/tests/phpunit/tests/rest-api/rest-global-styles-controller.php b/tests/phpunit/tests/rest-api/rest-global-styles-controller.php
index b55c7c3d60..59986e597c 100644
--- a/tests/phpunit/tests/rest-api/rest-global-styles-controller.php
+++ b/tests/phpunit/tests/rest-api/rest-global-styles-controller.php
@@ -133,13 +133,13 @@ class WP_REST_Global_Styles_Controller_Test extends WP_Test_REST_Controller_Test
public function test_register_routes() {
$routes = rest_get_server()->get_routes();
$this->assertArrayHasKey(
- '/wp/v2/global-styles/(?P<id>[\/\w-]+)',
+ '/wp/v2/global-styles/(?P<id>[\/\d+]+)',
$routes,
'Single global style based on the given ID route does not exist'
);
$this->assertCount(
2,
- $routes['/wp/v2/global-styles/(?P<id>[\/\w-]+)'],
+ $routes['/wp/v2/global-styles/(?P<id>[\/\d+]+)'],
'Single global style based on the given ID route does not have exactly two elements'
);
$this->assertArrayHasKey(
@@ -408,7 +408,7 @@ class WP_REST_Global_Styles_Controller_Test extends WP_Test_REST_Controller_Test
// Themes deep in subdirectories.
'2 subdirectories deep' => array(
'theme_dirname' => 'subdir/subsubdir/mytheme',
- 'expected' => 'rest_global_styles_not_found',
+ 'expected' => 'rest_no_route',
),
);
}
@@ -776,4 +776,54 @@ class WP_REST_Global_Styles_Controller_Test extends WP_Test_REST_Controller_Test
$this->assertArrayHasKey( 'https://api.w.org/action-edit-css', $links );
}
}
+
+ /**
+ * Test that the route accepts integer IDs.
+ *
+ * @ticket 61911
+ */
+ public function test_global_styles_route_accepts_integer_id() {
+ wp_set_current_user( self::$admin_id );
+ $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->assertEquals( 200, $response->get_status() );
+
+ $data = $response->get_data();
+ $this->assertIsInt( $data['id'] );
+ $this->assertSame( self::$global_styles_id, $data['id'] );
+ }
+
+ /**
+ * Test that the schema defines ID as an integer.
+ *
+ * @ticket 61911
+ */
+ public function test_global_styles_schema_id_type() {
+ $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/global-styles/' . self::$global_styles_id );
+ $response = rest_get_server()->dispatch( $request );
+
+ $data = $response->get_data();
+ $schema = $data['schema'];
+
+ $this->assertArrayHasKey( 'properties', $schema );
+ $this->assertArrayHasKey( 'id', $schema['properties'] );
+ $this->assertArrayHasKey( 'type', $schema['properties']['id'] );
+ $this->assertSame( 'integer', $schema['properties']['id']['type'] );
+ }
+
+ /**
+ * Test that the route argument schema defines ID as an integer.
+ *
+ * @ticket 61911
+ */
+ public function test_global_styles_route_args_schema() {
+ $routes = rest_get_server()->get_routes();
+ $route_data = $routes['/wp/v2/global-styles/(?P<id>[\/\d+]+)'];
+
+ $this->assertArrayHasKey( 'args', $route_data[0] );
+ $this->assertArrayHasKey( 'id', $route_data[0]['args'] );
+ $this->assertArrayHasKey( 'type', $route_data[0]['args']['id'] );
+ $this->assertSame( 'integer', $route_data[0]['args']['id']['type'] );
+ }
}
diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php
index 6462bb9241..bd0b9a3af4 100644
--- a/tests/phpunit/tests/rest-api/rest-posts-controller.php
+++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php
@@ -876,6 +876,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$this->assertSame( 200, $response->get_status() );
$all_data = $response->get_data();
+
+ $this->assertNotEmpty( $all_data );
+
foreach ( $all_data as $post ) {
$this->assertNotEquals( $draft_id, $post['id'] );
}
diff --git a/tests/phpunit/tests/rest-api/rest-schema-setup.php b/tests/phpunit/tests/rest-api/rest-schema-setup.php
index 03dcb0631f..7f8de5f0dd 100644
--- a/tests/phpunit/tests/rest-api/rest-schema-setup.php
+++ b/tests/phpunit/tests/rest-api/rest-schema-setup.php
@@ -133,7 +133,7 @@ class WP_Test_REST_Schema_Initialization extends WP_Test_REST_TestCase {
'/wp/v2/users/(?P<user_id>(?:[\\d]+|me))/application-passwords/(?P<uuid>[\\w\\-]+)',
'/wp/v2/comments',
'/wp/v2/comments/(?P<id>[\\d]+)',
- '/wp/v2/global-styles/(?P<id>[\/\w-]+)',
+ '/wp/v2/global-styles/(?P<id>[\/\d+]+)',
'/wp/v2/global-styles/(?P<parent>[\d]+)/revisions',
'/wp/v2/global-styles/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)',
'/wp/v2/global-styles/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)/variations',
diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php
index dc991eb672..9cd80005f5 100644
--- a/tests/phpunit/tests/rest-api/rest-server.php
+++ b/tests/phpunit/tests/rest-api/rest-server.php
@@ -1871,6 +1871,8 @@ class Tests_REST_Server extends WP_Test_REST_TestCase {
public function test_get_routes_respects_namespace_parameter() {
$routes = rest_get_server()->get_routes( 'oembed/1.0' );
+ $this->assertNotEmpty( $routes );
+
foreach ( $routes as $route => $handlers ) {
$this->assertStringStartsWith( '/oembed/1.0', $route );
}
diff --git a/tests/phpunit/tests/rest-api/rest-settings-controller.php b/tests/phpunit/tests/rest-api/rest-settings-controller.php
index e8f90b53f2..c6b42c365f 100644
--- a/tests/phpunit/tests/rest-api/rest-settings-controller.php
+++ b/tests/phpunit/tests/rest-api/rest-settings-controller.php
@@ -385,14 +385,24 @@ class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase
}
/**
- * @doesNotPerformAssertions
+ * Settings can't be created
*/
public function test_create_item() {
- // Controller does not implement create_item().
+ wp_set_current_user( self::$administrator );
+
+ $request = new WP_REST_Request( 'POST', '/wp/v2/settings' );
+ $request->set_param( 'new_setting', 'New value' );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->assertSame( 400, $response->get_status() );
}
+ /**
+ * @ticket 41604
+ */
public function test_update_item() {
wp_set_current_user( self::$administrator );
+
$request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
$request->set_param( 'title', 'The new title!' );
$response = rest_get_server()->dispatch( $request );
@@ -403,6 +413,63 @@ class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase
$this->assertSame( get_option( 'blogname' ), $data['title'] );
}
+ /**
+ * @ticket 41604
+ */
+ public function test_update_item_with_global_parameters_present() {
+ wp_set_current_user( self::$administrator );
+
+ $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
+ $request->set_param( 'title', 'The new title!' );
+ $request->set_url_params( array( '_locale' => 'user' ) );
+ $response = rest_get_server()->dispatch( $request );
+ $data = $response->get_data();
+
+ $this->assertSame( 200, $response->get_status() );
+ $this->assertSame( 'The new title!', $data['title'] );
+ $this->assertSame( get_option( 'blogname' ), $data['title'] );
+ }
+
+ /**
+ * @ticket 41604
+ */
+ public function test_update_item_with_empty_body() {
+ wp_set_current_user( self::$administrator );
+
+ $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
+ $response = rest_get_server()->dispatch( $request );
+ $data = $response->get_data();
+
+ $this->assertSame( 400, $response->get_status() );
+ }
+
+ /**
+ * @ticket 41604
+ */
+ public function test_update_nonexistent_item() {
+ wp_set_current_user( self::$administrator );
+
+ $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
+ $request->set_param( 'i_do_no_exist', 'New value' );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->assertSame( 400, $response->get_status() );
+ }
+
+ /**
+ * @ticket 41604
+ */
+ public function test_update_partially_valid_items() {
+ wp_set_current_user( self::$administrator );
+
+ $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
+ $request->set_param( 'title', 'The new title!' );
+ $request->set_param( 'i_do_no_exist', 'New value' );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->assertSame( 400, $response->get_status() );
+ }
+
public function update_setting_custom_callback( $result, $name, $value, $args ) {
if ( 'title' === $name && 'The new title!' === $value ) {
// Do not allow changing the title in this case.
diff --git a/tests/phpunit/tests/rest-api/rest-users-controller.php b/tests/phpunit/tests/rest-api/rest-users-controller.php
index 2f4a2f4982..db34f88752 100644
--- a/tests/phpunit/tests/rest-api/rest-users-controller.php
+++ b/tests/phpunit/tests/rest-api/rest-users-controller.php
@@ -308,6 +308,8 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase {
$rest_post_types = array_values( get_post_types( array( 'show_in_rest' => true ), 'names' ) );
+ $this->assertNotEmpty( $users );
+
foreach ( $users as $user ) {
$this->assertNotEmpty( count_user_posts( $user['id'], $rest_post_types ) );
@@ -3359,9 +3361,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase {
$this->assertSame( $user->user_login, $data['username'] );
$this->assertSame( $user->roles, $data['roles'] );
$this->assertSame( get_user_locale( $user ), $data['locale'] );
- }
-
- if ( 'edit' !== $context ) {
+ } else {
$this->assertArrayNotHasKey( 'roles', $data );
$this->assertArrayNotHasKey( 'capabilities', $data );
$this->assertArrayNotHasKey( 'registered_date', $data );
diff --git a/tests/phpunit/tests/shortcode.php b/tests/phpunit/tests/shortcode.php
index 2c5b04d02e..269da2b05e 100644
--- a/tests/phpunit/tests/shortcode.php
+++ b/tests/phpunit/tests/shortcode.php
@@ -693,10 +693,11 @@ EOF;
private function sub_registration( $input, $expected ) {
add_shortcode( $input, '' );
$actual = shortcode_exists( $input );
- $this->assertSame( $expected, $actual );
if ( $actual ) {
remove_shortcode( $input );
}
+
+ $this->assertSame( $expected, $actual );
}
public function data_registration_bad() {
diff --git a/tests/phpunit/tests/taxonomy.php b/tests/phpunit/tests/taxonomy.php
index 2a4ee3b560..13528c3015 100644
--- a/tests/phpunit/tests/taxonomy.php
+++ b/tests/phpunit/tests/taxonomy.php
@@ -4,6 +4,18 @@
* @group taxonomy
*/
class Tests_Taxonomy extends WP_UnitTestCase {
+
+ /**
+ * Editor user ID.
+ *
+ * @var int $editor_id
+ */
+ public static $editor_id;
+
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
+ }
+
public function test_get_post_taxonomies() {
$this->assertSame( array( 'category', 'post_tag', 'post_format' ), get_object_taxonomies( 'post' ) );
}
@@ -28,7 +40,11 @@ class Tests_Taxonomy extends WP_UnitTestCase {
}
public function test_get_post_taxonomy() {
- foreach ( get_object_taxonomies( 'post' ) as $taxonomy ) {
+ $taxonomies = get_object_taxonomies( 'post' );
+
+ $this->assertNotEmpty( $taxonomies );
+
+ foreach ( $taxonomies as $taxonomy ) {
$tax = get_taxonomy( $taxonomy );
// Should return an object with the correct taxonomy object type.
$this->assertIsObject( $tax );
@@ -110,7 +126,11 @@ class Tests_Taxonomy extends WP_UnitTestCase {
}
public function test_get_link_taxonomy() {
- foreach ( get_object_taxonomies( 'link' ) as $taxonomy ) {
+ $taxonomies = get_object_taxonomies( 'link' );
+
+ $this->assertNotEmpty( $taxonomies );
+
+ foreach ( $taxonomies as $taxonomy ) {
$tax = get_taxonomy( $taxonomy );
// Should return an object with the correct taxonomy object type.
$this->assertIsObject( $tax );
@@ -1002,7 +1022,7 @@ class Tests_Taxonomy extends WP_UnitTestCase {
)
);
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
+ wp_set_current_user( self::$editor_id );
$updated_post_id = edit_post(
array(
'post_ID' => $post->ID,
@@ -1028,7 +1048,7 @@ class Tests_Taxonomy extends WP_UnitTestCase {
*/
public function test_default_term_for_custom_taxonomy() {
- wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
+ wp_set_current_user( self::$editor_id );
$tax = 'custom-tax';
diff --git a/tests/phpunit/tests/term/cache.php b/tests/phpunit/tests/term/cache.php
index 99a73f7823..76eff31adf 100644
--- a/tests/phpunit/tests/term/cache.php
+++ b/tests/phpunit/tests/term/cache.php
@@ -222,6 +222,8 @@ class Tests_Term_Cache extends WP_UnitTestCase {
update_term_cache( $terms );
+ $this->assertNotEmpty( $terms );
+
foreach ( $terms as $term ) {
$this->assertSame( $p, $term->object_id );
}
diff --git a/tests/phpunit/tests/term/meta.php b/tests/phpunit/tests/term/meta.php
index 248d190917..a1eed9d335 100644
--- a/tests/phpunit/tests/term/meta.php
+++ b/tests/phpunit/tests/term/meta.php
@@ -132,26 +132,26 @@ class Tests_Term_Meta extends WP_UnitTestCase {
$this->go_to( get_permalink( $p ) );
remove_action( 'pre_get_posts', array( $this, 'set_cache_results' ) );
- if ( have_posts() ) {
- while ( have_posts() ) {
- the_post();
-
- // First request will hit the database.
- $num_queries = get_num_queries();
- $this->assertSame( 'bar', get_term_meta( $terms[0], 'foo', true ) );
- ++$num_queries;
- $this->assertSame( $num_queries, get_num_queries() );
-
- // Second and third requests should be in cache.
- $this->assertSame( 'bar', get_term_meta( $terms[1], 'foo', true ) );
- $this->assertSame( 'bar', get_term_meta( $terms[2], 'foo', true ) );
- $this->assertSame( $num_queries, get_num_queries() );
-
- // Querying a term not primed should result in a hit.
- ++$num_queries;
- $this->assertSame( 'bar', get_term_meta( $orphan_term, 'foo', true ) );
- $this->assertSame( $num_queries, get_num_queries() );
- }
+ $this->assertTrue( have_posts() );
+
+ while ( have_posts() ) {
+ the_post();
+
+ // First request will hit the database.
+ $num_queries = get_num_queries();
+ $this->assertSame( 'bar', get_term_meta( $terms[0], 'foo', true ) );
+ ++$num_queries;
+ $this->assertSame( $num_queries, get_num_queries() );
+
+ // Second and third requests should be in cache.
+ $this->assertSame( 'bar', get_term_meta( $terms[1], 'foo', true ) );
+ $this->assertSame( 'bar', get_term_meta( $terms[2], 'foo', true ) );
+ $this->assertSame( $num_queries, get_num_queries() );
+
+ // Querying a term not primed should result in a hit.
+ ++$num_queries;
+ $this->assertSame( 'bar', get_term_meta( $orphan_term, 'foo', true ) );
+ $this->assertSame( $num_queries, get_num_queries() );
}
}
@@ -201,20 +201,20 @@ class Tests_Term_Meta extends WP_UnitTestCase {
)
);
- if ( $q->have_posts() ) {
- while ( $q->have_posts() ) {
- $q->the_post();
+ $this->assertTrue( $q->have_posts() );
- // Requests will hit the database.
- $num_queries = get_num_queries();
- $this->assertSame( 'bar', get_term_meta( $terms[0], 'foo', true ) );
- ++$num_queries;
- $this->assertSame( $num_queries, get_num_queries() );
+ while ( $q->have_posts() ) {
+ $q->the_post();
- $this->assertSame( 'bar', get_term_meta( $terms[1], 'foo', true ) );
- ++$num_queries;
- $this->assertSame( $num_queries, get_num_queries() );
- }
+ // Requests will hit the database.
+ $num_queries = get_num_queries();
+ $this->assertSame( 'bar', get_term_meta( $terms[0], 'foo', true ) );
+ ++$num_queries;
+ $this->assertSame( $num_queries, get_num_queries() );
+
+ $this->assertSame( 'bar', get_term_meta( $terms[1], 'foo', true ) );
+ ++$num_queries;
+ $this->assertSame( $num_queries, get_num_queries() );
}
}
diff --git a/tests/phpunit/tests/term/wpGenerateTagCloud.php b/tests/phpunit/tests/term/wpGenerateTagCloud.php
index 62de4a0c26..4b7a27fb47 100644
--- a/tests/phpunit/tests/term/wpGenerateTagCloud.php
+++ b/tests/phpunit/tests/term/wpGenerateTagCloud.php
@@ -156,6 +156,8 @@ class Tests_WP_Generate_Tag_Cloud extends WP_UnitTestCase {
)
);
+ $this->assertNotEmpty( $tags );
+
foreach ( $tags as $tag ) {
$this->assertStringContainsString( '>' . $tag->name . '<', $found );
}
@@ -187,6 +189,8 @@ class Tests_WP_Generate_Tag_Cloud extends WP_UnitTestCase {
$this->assertMatchesRegularExpression( "|^<ul class='wp-tag-cloud' role='list'>|", $found );
$this->assertMatchesRegularExpression( "|</ul>\n|", $found );
+ $this->assertNotEmpty( $tags );
+
foreach ( $tags as $tag ) {
$this->assertStringContainsString( '>' . $tag->name . '<', $found );
}
diff --git a/tests/phpunit/tests/term/wpGetObjectTerms.php b/tests/phpunit/tests/term/wpGetObjectTerms.php
index 948ba1e1c3..128e4a19a8 100644
--- a/tests/phpunit/tests/term/wpGetObjectTerms.php
+++ b/tests/phpunit/tests/term/wpGetObjectTerms.php
@@ -110,6 +110,9 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
add_filter( 'wp_get_object_terms', array( $this, 'filter_get_object_terms' ) );
$terms = wp_get_object_terms( $post_id, $this->taxonomy );
remove_filter( 'wp_get_object_terms', array( $this, 'filter_get_object_terms' ) );
+
+ $this->assertNotEmpty( $terms );
+
foreach ( $terms as $term ) {
$this->assertIsObject( $term );
}
@@ -845,6 +848,8 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
)
);
+ $this->assertNotEmpty( $found );
+
foreach ( $found as $f ) {
$this->assertSame( $p, $f->object_id );
}
diff --git a/tests/phpunit/tests/term/wpTaxonomy.php b/tests/phpunit/tests/term/wpTaxonomy.php
index 4106e60f3b..845497ad06 100644
--- a/tests/phpunit/tests/term/wpTaxonomy.php
+++ b/tests/phpunit/tests/term/wpTaxonomy.php
@@ -7,6 +7,8 @@ class Tests_WP_Taxonomy extends WP_UnitTestCase {
public function test_instances() {
global $wp_taxonomies;
+ $this->assertNotEmpty( $wp_taxonomies );
+
foreach ( $wp_taxonomies as $taxonomy ) {
$this->assertInstanceOf( 'WP_Taxonomy', $taxonomy );
}
diff --git a/tests/phpunit/tests/theme.php b/tests/phpunit/tests/theme.php
index 40a8534c43..64a226d004 100644
--- a/tests/phpunit/tests/theme.php
+++ b/tests/phpunit/tests/theme.php
@@ -89,6 +89,9 @@ class Tests_Theme extends WP_UnitTestCase {
*/
public function test_get_theme() {
$themes = get_themes();
+
+ $this->assertNotEmpty( $themes );
+
foreach ( array_keys( $themes ) as $name ) {
$theme = get_theme( $name );
// WP_Theme implements ArrayAccess. Even ArrayObject returns false for is_array().
@@ -100,6 +103,9 @@ class Tests_Theme extends WP_UnitTestCase {
public function test_wp_get_theme() {
$themes = wp_get_themes();
+
+ $this->assertNotEmpty( $themes );
+
foreach ( $themes as $theme ) {
$this->assertInstanceOf( 'WP_Theme', $theme );
$this->assertFalse( $theme->errors() );
@@ -115,6 +121,9 @@ class Tests_Theme extends WP_UnitTestCase {
*/
public function test_get_themes_contents() {
$themes = get_themes();
+
+ $this->assertNotEmpty( $themes );
+
// Generic tests that should hold true for any theme.
foreach ( $themes as $k => $theme ) {
// Don't run these checks for custom themes.
@@ -361,6 +370,8 @@ class Tests_Theme extends WP_UnitTestCase {
public function test_switch_theme() {
$themes = get_themes();
+ $this->assertNotEmpty( $themes );
+
// Switch to each theme in sequence.
// Do it twice to make sure we switch to the first theme, even if it's our starting theme.
// Do it a third time to ensure switch_theme() works with one argument.
diff --git a/tests/phpunit/tests/url.php b/tests/phpunit/tests/url.php
index 4768e25b77..37ceb69cba 100644
--- a/tests/phpunit/tests/url.php
+++ b/tests/phpunit/tests/url.php
@@ -7,6 +7,17 @@
*/
class Tests_URL extends WP_UnitTestCase {
+ /**
+ * Author user ID.
+ *
+ * @var int $author_id
+ */
+ public static $author_id;
+
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$author_id = $factory->user->create( array( 'role' => 'author' ) );
+ }
+
public function set_up() {
parent::set_up();
$GLOBALS['pagenow'] = '';
@@ -386,7 +397,7 @@ class Tests_URL extends WP_UnitTestCase {
* @covers ::get_adjacent_post
*/
public function test_get_adjacent_post_should_return_private_posts_belonging_to_the_current_user() {
- $u = self::factory()->user->create( array( 'role' => 'author' ) );
+ $u = self::$author_id;
$old_uid = get_current_user_id();
wp_set_current_user( $u );
@@ -425,7 +436,7 @@ class Tests_URL extends WP_UnitTestCase {
* @covers ::get_adjacent_post
*/
public function test_get_adjacent_post_should_return_private_posts_belonging_to_other_users_if_the_current_user_can_read_private_posts() {
- $u1 = self::factory()->user->create( array( 'role' => 'author' ) );
+ $u1 = self::$author_id;
$u2 = self::factory()->user->create( array( 'role' => 'administrator' ) );
$old_uid = get_current_user_id();
wp_set_current_user( $u2 );
@@ -465,7 +476,7 @@ class Tests_URL extends WP_UnitTestCase {
* @covers ::get_adjacent_post
*/
public function test_get_adjacent_post_should_not_return_private_posts_belonging_to_other_users_if_the_current_user_cannot_read_private_posts() {
- $u1 = self::factory()->user->create( array( 'role' => 'author' ) );
+ $u1 = self::$author_id;
$u2 = self::factory()->user->create( array( 'role' => 'author' ) );
$old_uid = get_current_user_id();
wp_set_current_user( $u2 );
diff --git a/tests/phpunit/tests/user.php b/tests/phpunit/tests/user.php
index 9e1faa2dca..df9f74b7e8 100644
--- a/tests/phpunit/tests/user.php
+++ b/tests/phpunit/tests/user.php
@@ -49,7 +49,7 @@ class Tests_User extends WP_UnitTestCase {
self::$user_ids[] = self::$admin_id;
self::$editor_id = $factory->user->create(
array(
- 'user_email' => 'test@test.com',
+ 'user_email' => 'test@example.com',
'role' => 'editor',
)
);
@@ -820,7 +820,7 @@ class Tests_User extends WP_UnitTestCase {
*/
public function test_validate_username_string() {
$this->assertTrue( validate_username( 'johndoe' ) );
- $this->assertTrue( validate_username( 'test@test.com' ) );
+ $this->assertTrue( validate_username( 'test@example.com' ) );
}
/**
@@ -1035,7 +1035,7 @@ class Tests_User extends WP_UnitTestCase {
$u = wp_insert_user(
array(
'user_login' => 'test',
- 'user_email' => 'test@example.com',
+ 'user_email' => 'urltest@example.com',
'user_pass' => 'password',
'user_url' => $user_url,
)
@@ -1083,7 +1083,7 @@ class Tests_User extends WP_UnitTestCase {
* @ticket 35750
*/
public function test_wp_update_user_should_delete_userslugs_cache() {
- $u = self::factory()->user->create();
+ $u = self::$sub_id;
$user = get_userdata( $u );
wp_update_user(
@@ -1256,7 +1256,7 @@ class Tests_User extends WP_UnitTestCase {
// Alter the case of the email address (which stays the same).
$userdata = array(
'ID' => self::$editor_id,
- 'user_email' => 'test@TEST.com',
+ 'user_email' => 'test@EXAMPLE.com',
);
$update = wp_update_user( $userdata );
@@ -1270,7 +1270,7 @@ class Tests_User extends WP_UnitTestCase {
// Change the email address.
$userdata = array(
'ID' => self::$editor_id,
- 'user_email' => 'test2@test.com',
+ 'user_email' => 'test2@example.com',
);
$update = wp_update_user( $userdata );
@@ -1279,7 +1279,7 @@ class Tests_User extends WP_UnitTestCase {
// Verify that the email address has been updated.
$user = get_userdata( self::$editor_id );
- $this->assertSame( $user->user_email, 'test2@test.com' );
+ $this->assertSame( $user->user_email, 'test2@example.com' );
}
/**
@@ -1939,11 +1939,7 @@ class Tests_User extends WP_UnitTestCase {
$_GET = array();
$_REQUEST = array();
- $administrator = self::factory()->user->create(
- array(
- 'role' => 'administrator',
- )
- );
+ $administrator = self::$admin_id;
wp_set_current_user( $administrator );
@@ -1957,11 +1953,7 @@ class Tests_User extends WP_UnitTestCase {
$this->assertSame( array( 'administrator' ), get_userdata( $administrator )->roles );
// Promote an editor to an administrator.
- $editor = self::factory()->user->create(
- array(
- 'role' => 'editor',
- )
- );
+ $editor = self::$editor_id;
$_POST['role'] = 'administrator';
$_POST['email'] = 'administrator@administrator.test';
@@ -1978,7 +1970,7 @@ class Tests_User extends WP_UnitTestCase {
* @ticket 43547
*/
public function test_wp_user_personal_data_exporter_no_user() {
- $actual = wp_user_personal_data_exporter( 'not-a-user-email@test.com' );
+ $actual = wp_user_personal_data_exporter( 'not-a-user-email@example.com' );
$expected = array(
'data' => array(),
diff --git a/tests/phpunit/tests/user/capabilities.php b/tests/phpunit/tests/user/capabilities.php
index 65942dca27..783dd18a4e 100644
--- a/tests/phpunit/tests/user/capabilities.php
+++ b/tests/phpunit/tests/user/capabilities.php
@@ -993,6 +993,29 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
}
/**
+ * Test add_role with implied capabilities grant successfully grants capabilities.
+ *
+ * @ticket 43421
+ */
+ public function test_add_role_with_single_level_capabilities() {
+ $role_name = 'janitor';
+ add_role(
+ $role_name,
+ 'Janitor',
+ array(
+ 'foo',
+ )
+ );
+ $this->flush_roles();
+
+ // Assign a user to that role.
+ $id = self::factory()->user->create( array( 'role' => $role_name ) );
+ $user = new WP_User( $id );
+
+ $this->assertTrue( $user->has_cap( 'foo' ) );
+ }
+
+ /**
* Change the capabilities associated with a role and make sure the change
* is reflected in has_cap().
*/
diff --git a/tests/phpunit/tests/user/getTheAuthorPosts.php b/tests/phpunit/tests/user/getTheAuthorPosts.php
index 5cd87bc79b..3a0abac5de 100644
--- a/tests/phpunit/tests/user/getTheAuthorPosts.php
+++ b/tests/phpunit/tests/user/getTheAuthorPosts.php
@@ -42,7 +42,7 @@ class Tests_User_GetTheAuthorPosts extends WP_UnitTestCase {
// Test with no global post, result should be 0 because no author is found.
$this->assertSame( 0, get_the_author_posts() );
$GLOBALS['post'] = self::$post_id;
- $this->assertEquals( 1, get_the_author_posts() );
+ $this->assertSame( 1, get_the_author_posts() );
}
/**
@@ -60,7 +60,7 @@ class Tests_User_GetTheAuthorPosts extends WP_UnitTestCase {
);
$GLOBALS['post'] = $cpt_ids[0];
- $this->assertEquals( 2, get_the_author_posts() );
+ $this->assertSame( 2, get_the_author_posts() );
_unregister_post_type( 'wptests_pt' );
}
diff --git a/tests/phpunit/tests/xmlrpc/wp/getComments.php b/tests/phpunit/tests/xmlrpc/wp/getComments.php
index edeb6254f0..606f9e0e85 100644
--- a/tests/phpunit/tests/xmlrpc/wp/getComments.php
+++ b/tests/phpunit/tests/xmlrpc/wp/getComments.php
@@ -28,6 +28,7 @@ class Tests_XMLRPC_wp_getComments extends WP_XMLRPC_UnitTestCase {
$results = $this->myxmlrpcserver->wp_getComments( array( 1, 'editor', 'editor', array() ) );
$this->assertNotIXRError( $results );
+ $this->assertNotEmpty( $results );
foreach ( $results as $result ) {
$comment = get_comment( $result['comment_id'], ARRAY_A );
@@ -52,6 +53,7 @@ class Tests_XMLRPC_wp_getComments extends WP_XMLRPC_UnitTestCase {
)
);
$this->assertNotIXRError( $results );
+ $this->assertNotEmpty( $results );
foreach ( $results as $result ) {
$this->assertEquals( $this->post_id, $result['post_id'] );
diff --git a/tests/phpunit/tests/xmlrpc/wp/getPageList.php b/tests/phpunit/tests/xmlrpc/wp/getPageList.php
index 15d14b748f..abf936c799 100644
--- a/tests/phpunit/tests/xmlrpc/wp/getPageList.php
+++ b/tests/phpunit/tests/xmlrpc/wp/getPageList.php
@@ -41,6 +41,7 @@ class Tests_XMLRPC_wp_getPageList extends WP_XMLRPC_UnitTestCase {
$results = $this->myxmlrpcserver->wp_getPageList( array( 1, 'editor', 'editor' ) );
$this->assertNotIXRError( $results );
+ $this->assertNotEmpty( $results );
foreach ( $results as $result ) {
$page = get_post( $result->page_id );
diff --git a/tests/phpunit/tests/xmlrpc/wp/getPages.php b/tests/phpunit/tests/xmlrpc/wp/getPages.php
index eb99caf5c1..9e120c87cc 100644
--- a/tests/phpunit/tests/xmlrpc/wp/getPages.php
+++ b/tests/phpunit/tests/xmlrpc/wp/getPages.php
@@ -47,6 +47,7 @@ class Tests_XMLRPC_wp_getPages extends WP_XMLRPC_UnitTestCase {
public function test_capable_user() {
$results = $this->myxmlrpcserver->wp_getPages( array( 1, 'administrator', 'administrator' ) );
$this->assertNotIXRError( $results );
+ $this->assertNotEmpty( $results );
foreach ( $results as $result ) {
$page = get_post( $result['page_id'] );
diff --git a/tests/phpunit/tests/xmlrpc/wp/getPosts.php b/tests/phpunit/tests/xmlrpc/wp/getPosts.php
index 814f0c1018..1369aaaae2 100644
--- a/tests/phpunit/tests/xmlrpc/wp/getPosts.php
+++ b/tests/phpunit/tests/xmlrpc/wp/getPosts.php
@@ -100,6 +100,8 @@ class Tests_XMLRPC_wp_getPosts extends WP_XMLRPC_UnitTestCase {
);
$results2 = $this->myxmlrpcserver->wp_getPosts( array( 1, 'editor', 'editor', $filter2 ) );
$this->assertNotIXRError( $results2 );
+ $this->assertNotEmpty( $results2 );
+
$last_comment_count = 100;
foreach ( $results2 as $post ) {
$comment_count = (int) get_comments_number( $post['post_id'] );
diff --git a/tests/phpunit/tests/xmlrpc/wp/getTerms.php b/tests/phpunit/tests/xmlrpc/wp/getTerms.php
index 3487c320da..d538a4efd2 100644
--- a/tests/phpunit/tests/xmlrpc/wp/getTerms.php
+++ b/tests/phpunit/tests/xmlrpc/wp/getTerms.php
@@ -46,6 +46,7 @@ class Tests_XMLRPC_wp_getTerms extends WP_XMLRPC_UnitTestCase {
$results = $this->myxmlrpcserver->wp_getTerms( array( 1, 'editor', 'editor', 'category' ) );
$this->assertNotIXRError( $results );
+ $this->assertNotEmpty( $results );
foreach ( $results as $term ) {
$this->assertIsInt( $term['count'] );
diff --git a/tests/phpunit/tests/xmlrpc/wp/getUsers.php b/tests/phpunit/tests/xmlrpc/wp/getUsers.php
index 0cf340c0d6..2c8c4ec245 100644
--- a/tests/phpunit/tests/xmlrpc/wp/getUsers.php
+++ b/tests/phpunit/tests/xmlrpc/wp/getUsers.php
@@ -117,6 +117,7 @@ class Tests_XMLRPC_wp_getUsers extends WP_XMLRPC_UnitTestCase {
);
$results = $this->myxmlrpcserver->wp_getUsers( array( 1, 'administrator', 'administrator', $filter ) );
$this->assertNotIXRError( $results );
+ $this->assertNotEmpty( $results );
$last_email = '';
foreach ( $results as $user ) {