wordpress-develop/tests/phpunit/tests
Jonny Harris 1f51e1f4f6 REST API: Fix issue with Template and Template Part Revision/Autosave REST API controllers.
The Template and Template Part REST API controllers have unique characteristics compared to other post type REST API controllers. They do not rely on integer IDs to reference objects; instead, they use a combination of the theme name and slug of the template, like 'twentytwentyfour//home.' Consequently, when the post types template and template part were introduced in [52062], it led to the registration of REST API endpoints for autosaves and revisions with invalid URL structures.

In this commit, we introduce new functionality to enable custom autosave and revisions endpoints to be registered at the post type level. Similar to the 'rest_controller_class' parameter, developers can now define 'revisions_rest_controller' and 'autosave_rest_controller.' This empowers developers to create custom controllers for these functionalities. Additionally, we introduce a 'late_route_registration' parameter, which proves helpful when dealing with custom URL patterns and regex pattern matching issues.
This commit registers new classes for template and template part autosave and revisions controllers, differentiating them from standard controllers in the following ways:
* The response shape now matches that of the template controller.
* Permission checks align with the template controller.
* A custom URL pattern is introduced to support slug-based identification of templates.

Furthermore, we've updated the utility function '_build_block_template_result_from_post' to support passing revision post objects. This enhancement ensures compatibility with the custom revisions controller.

Props spacedmonkey, revgeorge, andraganescu, hellofromTonya, antonvlasenko, kadamwhite, ironprogrammer, costdev, mukesh27, timothyblynjacobs, adamsilverstein. 
Fixes 56922.

git-svn-id: https://develop.svn.wordpress.org/trunk@56819 602fd350-edb4-49c9-b593-d223f7449a82
2023-10-10 14:03:03 +00:00
..
admin Coding Standards: Fix a few newly introduced WPCS issues. 2023-09-25 15:34:34 +00:00
ajax Quick/Bulk Edit: Fix inability to quick edit draft post date. 2023-10-08 20:08:08 +00:00
attachment Coding Standards: Remove superfluous blank lines at the end of various classes. 2023-09-08 09:30:38 +00:00
block-supports Editor: Add additional unit tests for elements block support. 2023-10-09 16:38:26 +00:00
block-templates Editor: Remove need for template prefix in get_template_hierarchy. 2023-02-02 19:36:29 +00:00
blocks Patterns: Inject theme attribute into Template Part blocks. 2023-10-10 13:19:42 +00:00
bookmark Tests: Use the function get_num_queries across all unit tests. 2023-05-11 10:05:51 +00:00
canonical Media: Disable attachment pages for new installations. 2023-09-22 00:02:28 +00:00
category Coding Standards: Include one space after function keyword for closures. 2023-09-12 15:21:02 +00:00
comment Comments: Improve WP_Comment_Query count query performance by setting 'order by' to 'none'. 2023-09-29 17:11:21 +00:00
compat Coding Standards: Remove superfluous blank lines at the end of various functions. 2023-09-08 10:01:14 +00:00
cron Cron API: Modify _get_cron_array() to always return an array. 2022-07-29 03:32:58 +00:00
customize Script Loader: Harden removal of script tag wrappers. 2023-09-29 19:45:53 +00:00
date Tests: Reduce usage of assertEquals 2023-09-29 15:22:12 +00:00
db Coding Standards: Remove redundant ignore annotations. 2023-09-28 00:02:47 +00:00
dependencies Script Loader: Harden removal of script tag wrappers. 2023-09-29 19:45:53 +00:00
diff Code Modernization: Use wp_trigger_error() in WP_Text_Diff_Renderer_Table magic methods. 2023-09-07 20:46:53 +00:00
editor Coding Standards: Remove superfluous blank lines at the end of various functions. 2023-09-08 10:01:14 +00:00
error-protection Tests: Rename classes in phpunit/tests/error-protection/ per the naming conventions. 2021-08-21 15:32:53 +00:00
feed Tests: Reduce usage of assertEquals 2023-09-29 15:22:12 +00:00
filesystem Coding Standards: Fix a few newly introduced WPCS issues. 2023-09-26 00:07:56 +00:00
fonts/font-face Fonts: Get font-family name from 'fontFamily' field. 2023-09-25 21:27:51 +00:00
formatting Coding Standards: Upgrade WPCS to version 3.0.0. 2023-09-26 00:24:43 +00:00
functions Login and Registration: Improve HTML for errors and notices. 2023-09-21 18:22:10 +00:00
general Themes: Deprecate usage of TEMPLATEPATH and STYLESHEETPATH constants. 2023-09-20 17:25:26 +00:00
hooks Plugins: Store result of call to array_keys, to save repeated calls in WP_Hook class. 2023-09-18 12:39:18 +00:00
html-api HTML API: Rename WP_HTML_Processor::createFragment() to follow WPCS. 2023-10-05 22:40:48 +00:00
http Coding Standards: Include one space after function keyword for closures. 2023-09-12 15:21:02 +00:00
image Coding Standards: Include one space after function keyword for closures. 2023-09-12 15:21:02 +00:00
import Coding Standards: Remove redundant ignore annotations. 2023-09-28 00:02:47 +00:00
includes Code Modernization: Explicitly declare all properties created in set_up() methods of various test classes. 2022-08-27 12:30:08 +00:00
kses Editor: Add PHPUnit tests for 5.9.0 new functions. 2022-10-04 14:20:18 +00:00
l10n Coding Standards: Include one space after function keyword for closures. 2023-09-12 15:21:02 +00:00
link Code Modernization: Fix "passing null to non-nullable" deprecation from next_posts(). 2023-09-28 21:24:35 +00:00
load General: Rename wp_in_development_mode() to wp_is_development_mode(). 2023-07-17 14:48:48 +00:00
media Media: Rely on wp_get_loading_optimization_attributes() to add decoding="async" to images. 2023-09-25 22:37:00 +00:00
menu Coding Standards: Include one space after function keyword for closures. 2023-09-12 15:21:02 +00:00
meta Revisions: framework for storing post meta revisions. 2023-09-26 15:30:34 +00:00
multisite Coding Standards: Use pre-increment/decrement for stand-alone statements. 2023-09-09 09:26:01 +00:00
oembed Script Loader: Replace hardcoded output of style tags with calls to wp_add_inline_style. 2023-09-25 17:04:41 +00:00
option Options, Meta APIs: Check setting group exists before search in unregister_setting(). 2023-10-10 12:50:19 +00:00
pluggable Coding Standards: Use pre-increment/decrement for stand-alone statements. 2023-09-09 09:26:01 +00:00
pomo Code Modernization: Use dirname() with the $levels parameter. 2023-09-11 04:51:09 +00:00
post REST API: Fix issue with Template and Template Part Revision/Autosave REST API controllers. 2023-10-10 14:03:03 +00:00
privacy Coding Standards: Remove superfluous blank lines at the end of various classes. 2023-09-08 09:30:38 +00:00
query Query: Ensure that the page parameter is scalar in WP_Query::get_posts(). 2023-10-10 11:20:28 +00:00
rest-api REST API: Fix issue with Template and Template Part Revision/Autosave REST API controllers. 2023-10-10 14:03:03 +00:00
rewrite Coding Standards: Remove superfluous blank lines at the end of various classes. 2023-09-08 09:30:38 +00:00
sitemaps Sitemaps: do not list users who only authored pages. 2023-09-26 12:40:36 +00:00
style-engine Editor: add background image support. 2023-09-19 01:27:43 +00:00
taxonomy Build/Test Tools: Implement use of the void solution. 2021-08-07 10:29:41 +00:00
term Tests: Reduce usage of assertEquals 2023-09-29 15:22:12 +00:00
theme Editor: Improve performance of _register_theme_block_patterns function. 2023-10-03 15:16:55 +00:00
url Tests: Second pass at merging file-level and class-level DocBlocks in various unit test files. 2023-03-03 14:42:42 +00:00
user Revisions: framework for storing post meta revisions. 2023-09-26 15:30:34 +00:00
widgets Tests: Reduce usage of assertEquals 2023-09-29 15:22:12 +00:00
wp Tests: Move the tests for WP class methods to the wp directory. 2022-10-28 14:08:20 +00:00
xmlrpc XML-RPC: Add alt attribute value to media item API. 2023-09-20 19:29:30 +00:00
actions.php Coding Standards: Include one space after function keyword for closures. 2023-09-12 15:21:02 +00:00
adminbar.php Tests: Add tests to ensure the contribute Toolbar node is added when appropriate. 2023-07-13 12:39:42 +00:00
auth.php REST API: Correct parsing of password from Authorization header when processing Application Password credentials. 2023-10-09 14:47:57 +00:00
avatar.php Coding Standards: Remove superfluous blank lines at the end of various classes. 2023-09-08 09:30:38 +00:00
basic.php Tests: Separate the tests in basic.php for clarity. 2022-07-07 23:55:13 +00:00
block-template-utils.php Templates: Introduce _remove_theme_attribute_from_template_part_block. 2023-09-26 16:59:11 +00:00
block-template.php Themes: Improve performance of get_block_theme_folders function 2023-09-19 16:15:52 +00:00
cache.php Coding Standards: Always use parentheses when instantiating an object. 2022-11-29 15:49:49 +00:00
canonical.php Media: Disable attachment pages for new installations. 2023-09-22 00:02:28 +00:00
comment.php Coding Standards: Remove superfluous blank lines at the end of various functions. 2023-09-08 10:01:14 +00:00
cron.php Coding Standards: Include one space after function keyword for closures. 2023-09-12 15:21:02 +00:00
db.php Database: Remove support for the mysql extension. 2023-08-26 13:01:05 +00:00
dependencies.php Coding Standards: Remove superfluous blank lines at the end of various functions. 2023-09-08 10:01:14 +00:00
file.php Coding Standards: Include one space after function keyword for closures. 2023-09-12 15:21:02 +00:00
filters.php Coding Standards: Remove superfluous blank lines at the end of various functions. 2023-09-08 10:01:14 +00:00
functions.php Coding Standards: Include one space after function keyword for closures. 2023-09-12 15:21:02 +00:00
https-detection.php Security: remove the cron event that checked for https support. 2023-09-22 19:06:45 +00:00
https-migration.php Coding Standards: Include one space after function keyword for closures. 2023-09-12 15:21:02 +00:00
kses.php KSES: Add writing-mode to the list of safe CSS properties. 2023-09-19 12:28:25 +00:00
l10n.php Tests: Ensure prerequisites are met for draft length tests in Tests_L10n. 2022-10-01 15:47:13 +00:00
link.php Coding Standards: Add visibility to methods in tests/phpunit/tests/. 2021-11-04 15:22:47 +00:00
locale.php I18N: Introduce word_count_type property to WP_Locale. 2023-02-07 17:26:14 +00:00
media.php Code Modernization: Rename parameters that use reserved keywords in phpunit/tests/media.php. 2023-09-26 00:23:25 +00:00
meta.php Tests: Reduce usage of assertEquals 2023-09-29 15:22:12 +00:00
post.php Build/Test Tools: Update PHPUnit Polyfills to version 1.1.0. 2023-08-21 15:53:04 +00:00
query.php Coding Standards: Include one space after function keyword for closures. 2023-09-12 15:21:02 +00:00
readme.php Tests: Relocate the tests for recommended PHP, MySQL, and MariaDB versions. 2022-10-11 14:25:47 +00:00
rest-api.php REST API: Avoid unnecessarily preparing item links REST API index. 2023-09-13 14:32:33 +00:00
rewrite.php Code Modernization: Check the return type of parse_url() in url_to_postid(). 2022-10-01 03:23:41 +00:00
robots.php Coding Standards: Include one space after function keyword for closures. 2023-09-12 15:21:02 +00:00
shortcode.php Coding Standards: Remove superfluous blank lines at the end of various functions. 2023-09-08 10:01:14 +00:00
taxonomy.php Taxonomy: add taxonomy for user pattern categories. 2023-09-21 04:23:12 +00:00
template.php Themes: Deprecate usage of TEMPLATEPATH and STYLESHEETPATH constants. 2023-09-20 17:25:26 +00:00
term.php Taxonomy: Stop double sanitization in get_term function. 2023-09-21 16:34:59 +00:00
theme-previews.php Editor: Add function prefix to avoid conflicts. 2023-10-02 22:40:36 +00:00
theme.php Bundled Themes: Twenty Twenty-Four is now the default theme. 2023-09-26 17:31:28 +00:00
upload.php Coding Standards: Remove superfluous blank lines at the end of various classes. 2023-09-08 09:30:38 +00:00
url.php Coding Standards: Use pre-increment/decrement for stand-alone statements. 2023-09-09 09:26:01 +00:00
user.php Tests: Reduce usage of assertEquals 2023-09-29 15:22:12 +00:00
utils.php Coding Standards: Remove superfluous blank lines at the end of various functions. 2023-09-08 10:01:14 +00:00
vars.php Coding Standards: Fix a few newly introduced WPCS issues. 2023-09-25 15:34:34 +00:00
walker.php Coding Standards: Remove superfluous blank lines at the end of various functions. 2023-09-08 10:01:14 +00:00
widgets.php Coding Standards: Include one space after function keyword for closures. 2023-09-12 15:21:02 +00:00

<?php
/**
 * Validate recommended versions for dependencies referenced in `readme.html`,
 * based on external site support pages.
 *
 * @group external-http
 */
class Tests_Readme extends WP_UnitTestCase {

	/**
	 * @coversNothing
	 */
	public function test_readme_php_version() {
		$this->markTestSkipped(
			'Temporarily disabled. Test should be re-enabled once WordPress is fully compatible with PHP 8.0+.'
		);

		// This test is designed to only run on trunk.
		$this->skipOnAutomatedBranches();

		$readme = file_get_contents( ABSPATH . 'readme.html' );

		preg_match( '#Recommendations.*PHP</a> version <strong>([0-9.]*)#s', $readme, $matches );

		$response_body = $this->get_response_body( 'https://www.php.net/supported-versions.php' );

		preg_match_all( '#<tr class="stable">\s*<td>\s*<a [^>]*>\s*([0-9.]*)#s', $response_body, $php_matches );

		$this->assertContains( $matches[1], $php_matches[1], "readme.html's Recommended PHP version is too old. Remember to update the WordPress.org Requirements page, too." );
	}

	/**
	 * @coversNothing
	 */
	public function test_readme_mysql_version() {
		// This test is designed to only run on trunk.
		$this->skipOnAutomatedBranches();

		$readme = file_get_contents( ABSPATH . 'readme.html' );

		preg_match( '#Recommendations.*MySQL</a> version <strong>([0-9.]*)#s', $readme, $matches );

		$response_body = $this->get_response_body( "https://dev.mysql.com/doc/relnotes/mysql/{$matches[1]}/en/" );

		// Retrieve the date of the first GA release for the recommended branch.
		preg_match( '#.*(\d{4}-\d{2}-\d{2}), General Availability#s', $response_body, $mysql_matches );

		/*
		 * Per https://www.mysql.com/support/, Oracle actively supports MySQL releases for 5 years from GA release.
		 *
		 * The currently recommended MySQL 5.7 branch moved from active support to extended support on 2020-10-21.
		 * As WordPress core is not fully compatible with MySQL 8.0 at this time, the "supported" period here
		 * is increased to 8 years to include extended support.
		 *
		 * TODO: Reduce this back to 5 years once MySQL 8.0 compatibility is achieved.
		 */
		$mysql_eol    = gmdate( 'Y-m-d', strtotime( $mysql_matches[1] . ' +8 years' ) );
		$current_date = gmdate( 'Y-m-d' );

		$this->assertLessThan( $mysql_eol, $current_date, "readme.html's Recommended MySQL version is too old. Remember to update the WordPress.org Requirements page, too." );
	}

	/**
	 * @coversNothing
	 */
	public function test_readme_mariadb_version() {
		// This test is designed to only run on trunk.
		$this->skipOnAutomatedBranches();

		$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/" );

		// 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 );

		// 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." );
	}

	/**
	 * Helper function to retrieve the response body or skip the test on HTTP timeout.
	 *
	 * @param string $url The URL to retrieve the response from.
	 * @return string The response body.
	 */
	public function get_response_body( $url ) {
		$response = wp_remote_get( $url );

		$this->skipTestOnTimeout( $response );

		$response_code = wp_remote_retrieve_response_code( $response );
		$response_body = wp_remote_retrieve_body( $response );

		if ( 200 !== $response_code ) {
			$parsed_url = parse_url( $url );

			$error_message = sprintf(
				'Could not contact %1$s to check versions. Response code: %2$s. Response body: %3$s',
				$parsed_url['host'],
				$response_code,
				$response_body
			);

			if ( 503 === $response_code ) {
				$this->markTestSkipped( $error_message );
			}

			$this->fail( $error_message );
		}

		return $response_body;
	}
}