<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[59605] trunk/tests/phpunit/tests/rest-api: REST API: Improve autosave and revision endpoints for templates and template parts.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/59605">59605</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/59605","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>swissspidy</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2025-01-15 12:44:43 +0000 (Wed, 15 Jan 2025)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>REST API: Improve autosave and revision endpoints for templates and template parts.
Fixes those endpoints for file-based templates and template parts, as templates based on theme files can't be revisioned or autosaved.
Props antonvlasenko, swissspidy, spacedmonkey, kadamwhite.
Fixes <a href="https://core.trac.wordpress.org/ticket/61970">#61970</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswpresttemplaterevisionscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php</a></li>
<li><a href="#trunktestsphpunittestsrestapiwpRestTemplateAutosavesControllerphp">trunk/tests/phpunit/tests/rest-api/wpRestTemplateAutosavesController.php</a></li>
<li><a href="#trunktestsphpunittestsrestapiwpRestTemplateRevisionsControllerphp">trunk/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php</a></li>
</ul>
<h3>Property Changed</h3>
<ul>
<li><a href="#trunk">trunk/</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<span class="cx" style="display: block; padding: 0 10px">Index: trunk
</span><span class="cx" style="display: block; padding: 0 10px">===================================================================
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">--- trunk 2025-01-14 15:35:18 UTC (rev 59604)
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+++ trunk 2025-01-15 12:44:43 UTC (rev 59605)
</ins><a id="trunk"></a>
<div class="propset"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Property changes: trunk</h4>
<pre class="diff"><span>
</span></pre></div>
<a id="svnmergeinfo"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: svn:mergeinfo</h4></div>
<span class="cx" style="display: block; padding: 0 10px"> /branches/5.5:49373-49379,49381
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/5.8:51889
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/6.7:59437
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-/trunk:58570
</del><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/trunk:51179,58570
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunksrcwpincludesrestapiendpointsclasswpresttemplaterevisionscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php 2025-01-14 15:35:18 UTC (rev 59604)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php 2025-01-15 12:44:43 UTC (rev 59605)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -170,10 +170,20 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error(
</span><span class="cx" style="display: block; padding: 0 10px"> 'rest_post_invalid_parent',
</span><span class="cx" style="display: block; padding: 0 10px"> __( 'Invalid template parent ID.' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- array( 'status' => 404 )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ array( 'status' => WP_Http::NOT_FOUND )
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post_id = isset( $template->wp_id ) ? (int) $template->wp_id : 0;
+
+ if ( $parent_post_id <= 0 ) {
+ return new WP_Error(
+ 'rest_invalid_template',
+ __( 'Templates based on theme files can\'t have revisions.' ),
+ array( 'status' => WP_Http::BAD_REQUEST )
+ );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return get_post( $template->wp_id );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapiwpRestTemplateAutosavesControllerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/wpRestTemplateAutosavesController.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/wpRestTemplateAutosavesController.php 2025-01-14 15:35:18 UTC (rev 59604)
+++ trunk/tests/phpunit/tests/rest-api/wpRestTemplateAutosavesController.php 2025-01-15 12:44:43 UTC (rev 59605)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -22,9 +22,19 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * @var string
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- const PARENT_POST_TYPE = 'wp_template';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ const TEMPLATE_PART_NAME = 'my_template_part';
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @var string
+ */
+ const TEMPLATE_POST_TYPE = 'wp_template';
+
+ /**
+ * @var string
+ */
+ const TEMPLATE_PART_POST_TYPE = 'wp_template_part';
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Admin user ID.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.4.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -52,8 +62,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> private static $template_post;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Create fake data before our tests run.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Template part post.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.7.0
+ *
+ * @var WP_Post
+ */
+ private static $template_part_post;
+
+ /**
+ * Create fake data before the tests run.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -73,9 +92,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Set up template post.
</span><span class="cx" style="display: block; padding: 0 10px"> self::$template_post = $factory->post->create_and_get(
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'post_type' => self::PARENT_POST_TYPE,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'post_type' => self::TEMPLATE_POST_TYPE,
</ins><span class="cx" style="display: block; padding: 0 10px"> 'post_name' => self::TEMPLATE_NAME,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'post_title' => 'My Template',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'post_title' => 'My template',
</ins><span class="cx" style="display: block; padding: 0 10px"> 'post_content' => 'Content',
</span><span class="cx" style="display: block; padding: 0 10px"> 'post_excerpt' => 'Description of my template',
</span><span class="cx" style="display: block; padding: 0 10px"> 'tax_input' => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -86,6 +105,27 @@
</span><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> wp_set_post_terms( self::$template_post->ID, self::TEST_THEME, 'wp_theme' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // Set up template part post.
+ self::$template_part_post = $factory->post->create_and_get(
+ array(
+ 'post_type' => self::TEMPLATE_PART_POST_TYPE,
+ 'post_name' => self::TEMPLATE_PART_NAME,
+ 'post_title' => 'My template part',
+ 'post_content' => 'Content',
+ 'post_excerpt' => 'Description of my template part',
+ 'tax_input' => array(
+ 'wp_theme' => array(
+ self::TEST_THEME,
+ ),
+ 'wp_template_part_area' => array(
+ WP_TEMPLATE_PART_AREA_HEADER,
+ ),
+ ),
+ )
+ );
+ wp_set_post_terms( self::$template_part_post->ID, self::TEST_THEME, 'wp_theme' );
+ wp_set_post_terms( self::$template_part_post->ID, WP_TEMPLATE_PART_AREA_HEADER, 'wp_template_part_area' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -117,12 +157,26 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @coversNothing
+ * @ticket 56922
+ */
+ public function test_context_param() {
+ // A proper data provider cannot be used because this method's signature must match the parent method.
+ // Therefore, actual tests are performed in the test_context_param_with_data_provider method.
+ $this->assertTrue( true );
+ }
+
+ /**
+ * @dataProvider data_context_param_with_data_provider
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_REST_Template_Autosaves_Controller::get_context_param
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_context_param() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_context_param_with_data_provider( $rest_base, $template_id ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> // Collection.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/autosaves' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $data = $response->get_data();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -144,7 +198,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Single.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/autosaves/1' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves/1' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $data = $response->get_data();
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertCount(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -165,25 +219,55 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider for test_context_param_with_data_provider.
+ *
+ * @return array
+ */
+ public function data_context_param_with_data_provider() {
+ return array(
+ 'templates' => array( 'templates', self::TEST_THEME . '//' . self::TEMPLATE_NAME ),
+ 'template parts' => array( 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
+ );
+ }
+
+ /**
+ * @coversNothing
+ * @ticket 56922
+ */
+ public function test_get_items() {
+ // A proper data provider cannot be used because this method's signature must match the parent method.
+ // Therefore, actual tests are performed in the test_get_items_with_data_provider method.
+ $this->assertTrue( true );
+ }
+
+ /**
+ * @dataProvider data_get_items_with_data_provider
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_REST_Template_Autosaves_Controller::get_items
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $parent_post_property_name A class property name that contains the parent post object.
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_get_items() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_get_items_with_data_provider( $parent_post_property_name, $rest_base, $template_id ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( self::$admin_id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Cannot access this property in the data provider because it is not initialized at the time of execution.
+ $parent_post = self::$$parent_post_property_name;
</ins><span class="cx" style="display: block; padding: 0 10px"> $autosave_post_id = wp_create_post_autosave(
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'post_content' => 'Autosave content.',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'post_ID' => self::$template_post->ID,
- 'post_type' => self::PARENT_POST_TYPE,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'post_ID' => $parent_post->ID,
+ 'post_type' => $parent_post->post_type,
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $request = new WP_REST_Request(
</span><span class="cx" style="display: block; padding: 0 10px"> 'GET',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/autosaves'
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves'
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $autosaves = $response->get_data();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( WP_Http::OK, $response->get_status(), 'Response is expected to have a status code of 200.' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertCount(
</span><span class="cx" style="display: block; padding: 0 10px"> 1,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -197,7 +281,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'Failed asserting that the ID of the autosave matches the expected autosave post ID.'
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- self::$template_post->ID,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post->ID,
</ins><span class="cx" style="display: block; padding: 0 10px"> $autosaves[0]['parent'],
</span><span class="cx" style="display: block; padding: 0 10px"> 'Failed asserting that the parent ID of the autosave matches the template post ID.'
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -209,22 +293,121 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider for test_get_items_with_data_provider.
+ *
+ * @return array
+ */
+ public function data_get_items_with_data_provider() {
+ return array(
+ 'templates' => array( 'template_post', 'templates', self::TEST_THEME . '//' . self::TEMPLATE_NAME ),
+ 'template parts' => array( 'template_part_post', 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
+ );
+ }
+
+ /**
+ * @dataProvider data_get_items_for_templates_based_on_theme_files_should_return_bad_response_status
+ * @ticket 61970
+ *
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
+ */
+ public function test_get_items_for_templates_based_on_theme_files_should_return_bad_response_status( $rest_base, $template_id ) {
+ wp_set_current_user( self::$admin_id );
+ switch_theme( 'block-theme' );
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves' );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse(
+ 'rest_invalid_template',
+ $response,
+ WP_Http::BAD_REQUEST,
+ sprintf( 'Response is expected to have a status code of %d.', WP_Http::BAD_REQUEST )
+ );
+ }
+
+ /**
+ * Data provider for test_get_items_for_templates_based_on_theme_files_should_return_bad_response_status.
+ *
+ * @return array
+ */
+ public function data_get_items_for_templates_based_on_theme_files_should_return_bad_response_status() {
+ return array(
+ 'templates' => array( 'templates', self::TEST_THEME . '//page-home' ),
+ 'template parts' => array( 'template-parts', self::TEST_THEME . '//small-header' ),
+ );
+ }
+
+ /**
+ * @dataProvider data_get_item_for_templates_based_on_theme_files_should_return_bad_response_status
+ * @ticket 56922
+ *
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
+ */
+ public function test_get_item_for_templates_based_on_theme_files_should_return_bad_response_status( $rest_base, $template_id ) {
+ wp_set_current_user( self::$admin_id );
+ switch_theme( 'block-theme' );
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves/1' );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse(
+ 'rest_invalid_template',
+ $response,
+ WP_Http::BAD_REQUEST,
+ sprintf( 'Response is expected to have a status code of %d.', WP_Http::BAD_REQUEST )
+ );
+ }
+
+ /**
+ * Data provider for test_get_item_for_templates_based_on_theme_files_should_return_bad_response_status.
+ *
+ * @return array
+ */
+ public function data_get_item_for_templates_based_on_theme_files_should_return_bad_response_status() {
+ return array(
+ 'templates' => array( 'templates', self::TEST_THEME . '//page-home' ),
+ 'template parts' => array( 'template-parts', self::TEST_THEME . '//small-header' ),
+ );
+ }
+
+ /**
+ * @coversNothing
+ * @ticket 56922
+ */
+ public function test_get_item() {
+ // A proper data provider cannot be used because this method's signature must match the parent method.
+ // Therefore, actual tests are performed in the test_get_item_with_data_provider method.
+ $this->assertTrue( true );
+ }
+
+ /**
+ * @dataProvider data_get_item_with_data_provider
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_REST_Template_Autosaves_Controller::get_item
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $parent_post_property_name A class property name that contains the parent post object.
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_get_item() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_get_item_with_data_provider( $parent_post_property_name, $rest_base, $template_id ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( self::$admin_id );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post = self::$$parent_post_property_name;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $autosave_post_id = wp_create_post_autosave(
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'post_content' => 'Autosave content.',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'post_ID' => self::$template_post->ID,
- 'post_type' => self::PARENT_POST_TYPE,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'post_ID' => $parent_post->ID,
+ 'post_type' => $parent_post->post_type,
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $request = new WP_REST_Request( 'GET', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/autosaves/' . $autosave_post_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $request = new WP_REST_Request( 'GET', '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves/' . $autosave_post_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ $this->assertSame( WP_Http::OK, $response->get_status(), 'Response is expected to have a status code of 200.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $autosave = $response->get_data();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertIsArray( $autosave, 'Failed asserting that the autosave is an array.' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -234,32 +417,59 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "Failed asserting that the autosave id is the same as $autosave_post_id."
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- self::$template_post->ID,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post->ID,
</ins><span class="cx" style="display: block; padding: 0 10px"> $autosave['parent'],
</span><span class="cx" style="display: block; padding: 0 10px"> sprintf(
</span><span class="cx" style="display: block; padding: 0 10px"> 'Failed asserting that the parent id of the autosave is the same as %s.',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- self::$template_post->ID
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post->ID
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider for test_get_item_with_data_provider.
+ *
+ * @return array
+ */
+ public function data_get_item_with_data_provider() {
+ return array(
+ 'templates' => array( 'template_post', 'templates', self::TEST_THEME . '//' . self::TEMPLATE_NAME ),
+ 'template parts' => array( 'template_part_post', 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
+ );
+ }
+
+ /**
+ * @coversNothing
+ * @ticket 56922
+ */
+ public function test_prepare_item() {
+ // A proper data provider cannot be used because this method's signature must match the parent method.
+ // Therefore, actual tests are performed in the test_prepare_item_with_data_provider method.
+ $this->assertTrue( true );
+ }
+
+ /**
+ * @dataProvider data_prepare_item_with_data_provider
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_REST_Template_Autosaves_Controller::prepare_item_for_response
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $parent_post_property_name A class property name that contains the parent post object.
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_prepare_item() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_prepare_item_with_data_provider( $parent_post_property_name, $rest_base, $template_id ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( self::$admin_id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post = self::$$parent_post_property_name;
</ins><span class="cx" style="display: block; padding: 0 10px"> $autosave_post_id = wp_create_post_autosave(
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'post_content' => 'Autosave content.',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'post_ID' => self::$template_post->ID,
- 'post_type' => self::PARENT_POST_TYPE,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'post_ID' => $parent_post->ID,
+ 'post_type' => $parent_post->post_type,
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $autosave_db_post = get_post( $autosave_post_id );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $template_id = self::TEST_THEME . '//' . self::TEMPLATE_NAME;
- $request = new WP_REST_Request( 'GET', '/wp/v2/templates/' . $template_id . '/autosaves/' . $autosave_db_post->ID );
- $controller = new WP_REST_Template_Autosaves_Controller( self::PARENT_POST_TYPE );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $request = new WP_REST_Request( 'GET', '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves/' . $autosave_db_post->ID );
+ $controller = new WP_REST_Template_Autosaves_Controller( $parent_post->post_type );
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = $controller->prepare_item_for_response( $autosave_db_post, $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertInstanceOf(
</span><span class="cx" style="display: block; padding: 0 10px"> WP_REST_Response::class,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -275,11 +485,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "Failed asserting that the autosave id is the same as $autosave_db_post->ID."
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- self::$template_post->ID,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post->ID,
</ins><span class="cx" style="display: block; padding: 0 10px"> $autosave['parent'],
</span><span class="cx" style="display: block; padding: 0 10px"> sprintf(
</span><span class="cx" style="display: block; padding: 0 10px"> 'Failed asserting that the parent id of the autosave is the same as %s.',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- self::$template_post->ID
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post->ID
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -300,17 +510,45 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider for test_prepare_item_with_data_provider.
+ *
+ * @return array
+ */
+ public function data_prepare_item_with_data_provider() {
+ return array(
+ 'templates' => array( 'template_post', 'templates', self::TEST_THEME . '//' . self::TEMPLATE_NAME ),
+ 'template parts' => array( 'template_part_post', 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
+ );
+ }
+
+ /**
+ * @coversNothing
+ * @ticket 56922
+ */
+ public function test_get_item_schema() {
+ // A proper data provider cannot be used because this method's signature must match the parent method.
+ // Therefore, actual tests are performed in the test_prepare_item_with_data_provider method.
+ $this->assertTrue( true );
+ }
+
+ /**
+ * @dataProvider data_get_item_schema_with_data_provider
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_REST_Template_Autosaves_Controller::get_item_schema
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
+ * @param int $properties_count Number of properties to check for in the schema.
+ * @param array $additional_properties Additional properties to check for in the schema.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_get_item_schema() {
- $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/autosaves' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_get_item_schema_with_data_provider( $rest_base, $template_id, $properties_count, $additional_properties = array() ) {
+ $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $data = $response->get_data();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $properties = $data['schema']['properties'];
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertCount( 19, $properties );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertCount( $properties_count, $properties );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'id', $properties, 'ID key should exist in properties.' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'slug', $properties, 'Slug key should exist in properties.' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'theme', $properties, 'Theme key should exist in properties.' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -324,22 +562,58 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'has_theme_file', $properties, 'has_theme_file key should exist in properties.' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'author', $properties, 'author key should exist in properties.' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'modified', $properties, 'modified key should exist in properties.' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertArrayHasKey( 'is_custom', $properties, 'is_custom key should exist in properties.' );
</del><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'parent', $properties, 'Parent key should exist in properties.' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'author_text', $properties, 'author_text key should exist in properties.' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'original_source', $properties, 'original_source key should exist in properties.' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertArrayHasKey( 'plugin', $properties, 'plugin key should exist in properties.' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $additional_properties as $additional_property ) {
+ $this->assertArrayHasKey( $additional_property, $properties, $additional_property . ' key should exist in properties.' );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider for test_get_item_schema_with_data_provider.
+ *
+ * @return array
+ */
+ public function data_get_item_schema_with_data_provider() {
+ return array(
+ 'templates' => array(
+ 'templates',
+ self::TEST_THEME . '//' . self::TEMPLATE_NAME,
+ 19,
+ array( 'is_custom', 'plugin' ),
+ ),
+ 'template parts' => array(
+ 'template-parts',
+ self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME,
+ 18,
+ array( 'area' ),
+ ),
+ );
+ }
+
+ /**
+ * @coversNothing
+ * @ticket 56922
+ */
+ public function test_create_item() {
+ // A proper data provider cannot be used because this method's signature must match the parent method.
+ // Therefore, actual tests are performed in the test_create_item_with_data_provider method.
+ $this->assertTrue( true );
+ }
+
+ /**
+ * @dataProvider data_create_item_with_data_provider
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_REST_Template_Autosaves_Controller::create_item
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_create_item() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_create_item_with_data_provider( $rest_base, $template_id ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( self::$admin_id );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $template_id = self::TEST_THEME . '/' . self::TEMPLATE_NAME;
- $request = new WP_REST_Request( 'POST', '/wp/v2/templates/' . $template_id . '/autosaves' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $request = new WP_REST_Request( 'POST', '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $request->add_header( 'Content-Type', 'application/x-www-form-urlencoded' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $request_parameters = array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -353,42 +627,84 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $request->set_body_params( $request_parameters );
</span><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertNotWPError( $response, 'The response from this request should not return a WP_Error object' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertNotWPError( $response, 'The response from this request should not return a WP_Error object.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = rest_ensure_response( $response );
</span><span class="cx" style="display: block; padding: 0 10px"> $data = $response->get_data();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertArrayHasKey( 'content', $data, 'Response should contain a key called content' );
- $this->assertSame( $request_parameters['content'], $data['content']['raw'], 'Response data should match for field content' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertArrayHasKey( 'content', $data, 'Response should contain a key called content.' );
+ $this->assertSame( $request_parameters['content'], $data['content']['raw'], 'Response data should match for field content.' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertArrayHasKey( 'title', $data, 'Response should contain a key called title' );
- $this->assertSame( $request_parameters['title'], $data['title']['raw'], 'Response data should match for field title' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertArrayHasKey( 'title', $data, 'Response should contain a key called title.' );
+ $this->assertSame( $request_parameters['title'], $data['title']['raw'], 'Response data should match for field title.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @covers WP_REST_Template_Autosaves_Controller::delete_item
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider for test_create_item_with_data_provider.
+ *
+ * @return array
+ */
+ public function data_create_item_with_data_provider() {
+ return array(
+ 'templates' => array( 'templates', self::TEST_THEME . '//' . self::TEMPLATE_NAME ),
+ 'template part' => array( 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
+ );
+ }
+
+ /**
+ * @dataProvider data_create_item_incorrect_permission
+ * @covers WP_REST_Template_Autosaves_Controller::create_item_permissions_check
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_create_item_incorrect_permission() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_create_item_incorrect_permission( $rest_base, $template_id ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( self::$contributor_id );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $template_id = self::TEST_THEME . '/' . self::TEMPLATE_NAME;
- $request = new WP_REST_Request( 'POST', '/wp/v2/templates/' . $template_id . '/autosaves' );
- $response = rest_get_server()->dispatch( $request );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $request = new WP_REST_Request( 'POST', '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves' );
+ $response = rest_get_server()->dispatch( $request );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertErrorResponse( 'rest_cannot_manage_templates', $response, WP_Http::FORBIDDEN );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @covers WP_REST_Template_Autosaves_Controller::delete_item
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider for test_create_item_incorrect_permission.
+ *
+ * @return array
+ */
+ public function data_create_item_incorrect_permission() {
+ return array(
+ 'template' => array( 'templates', self::TEST_THEME . '//' . self::TEMPLATE_NAME ),
+ 'template part' => array( 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
+ );
+ }
+
+ /**
+ * @dataProvider data_create_item_no_permission
+ * @covers WP_REST_Template_Autosaves_Controller::create_item_permissions_check
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_create_item_no_permission() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_create_item_no_permission( $rest_base, $template_id ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( 0 );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $template_id = self::TEST_THEME . '/' . self::TEMPLATE_NAME;
- $request = new WP_REST_Request( 'POST', '/wp/v2/templates/' . $template_id . '/autosaves' );
- $response = rest_get_server()->dispatch( $request );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $request = new WP_REST_Request( 'POST', '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves' );
+ $response = rest_get_server()->dispatch( $request );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertErrorResponse( 'rest_cannot_manage_templates', $response, WP_Http::UNAUTHORIZED );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider for test_create_item_no_permission.
+ *
+ * @return array
+ */
+ public function data_create_item_no_permission() {
+ return array(
+ 'template' => array( 'templates', self::TEST_THEME . '//' . self::TEMPLATE_NAME ),
+ 'template part' => array( 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
+ );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * @coversNothing
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapiwpRestTemplateRevisionsControllerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php 2025-01-14 15:35:18 UTC (rev 59604)
+++ trunk/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php 2025-01-15 12:44:43 UTC (rev 59605)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -27,9 +27,24 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * @var string
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- const PARENT_POST_TYPE = 'wp_template';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ const TEMPLATE_PART_NAME = 'my_template_part';
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @var string
+ */
+ const TEMPLATE_PART_NAME_2 = 'my_template_part_2';
+
+ /**
+ * @var string
+ */
+ const TEMPLATE_POST_TYPE = 'wp_template';
+
+ /**
+ * @var string
+ */
+ const TEMPLATE_PART_POST_TYPE = 'wp_template_part';
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Admin user ID.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.4.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -66,11 +81,34 @@
</span><span class="cx" style="display: block; padding: 0 10px"> private static $template_post_2;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Template part post.
+ *
+ * @since 6.7.0
+ *
+ * @var WP_Post
+ */
+ private static $template_part_post;
+
+ /**
+ * Template part post.
+ *
+ * @since 6.7.0
+ *
+ * @var WP_Post
+ */
+ private static $template_part_post_2;
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * @var array
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- private static $revisions = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private static $template_revisions = array();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @var array
+ */
+ private static $template_part_revisions = array();
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Create fake data before our tests run.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -92,7 +130,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Set up template post.
</span><span class="cx" style="display: block; padding: 0 10px"> self::$template_post = $factory->post->create_and_get(
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'post_type' => self::PARENT_POST_TYPE,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'post_type' => self::TEMPLATE_POST_TYPE,
</ins><span class="cx" style="display: block; padding: 0 10px"> 'post_name' => self::TEMPLATE_NAME,
</span><span class="cx" style="display: block; padding: 0 10px"> 'post_title' => 'My Template',
</span><span class="cx" style="display: block; padding: 0 10px"> 'post_content' => 'Content',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -106,54 +144,84 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> wp_set_post_terms( self::$template_post->ID, self::TEST_THEME, 'wp_theme' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Update post to create a new revisions.
- self::$revisions[] = _wp_put_post_revision(
- array(
- 'ID' => self::$template_post->ID,
- 'post_content' => 'Content revision #2',
- )
- );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Update post to create new revisions.
+ foreach ( range( 2, 5 ) as $revision_index ) {
+ self::$template_revisions[] = _wp_put_post_revision(
+ array(
+ 'ID' => self::$template_post->ID,
+ 'post_content' => 'Content revision #' . $revision_index,
+ )
+ );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Update post to create a new revisions.
- self::$revisions[] = _wp_put_post_revision(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Create a new template post to test the get_item method.
+ self::$template_post_2 = $factory->post->create_and_get(
</ins><span class="cx" style="display: block; padding: 0 10px"> array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'ID' => self::$template_post->ID,
- 'post_content' => 'Content revision #3',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'post_type' => self::TEMPLATE_POST_TYPE,
+ 'post_name' => self::TEMPLATE_NAME_2,
+ 'post_title' => 'My Template 2',
+ 'post_content' => 'Content 2',
+ 'post_excerpt' => 'Description of my template 2',
+ 'tax_input' => array(
+ 'wp_theme' => array(
+ self::TEST_THEME,
+ ),
+ ),
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_set_post_terms( self::$template_post_2->ID, self::TEST_THEME, 'wp_theme' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Update post to create a new revisions.
- self::$revisions[] = _wp_put_post_revision(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Set up template part post.
+ self::$template_part_post = $factory->post->create_and_get(
</ins><span class="cx" style="display: block; padding: 0 10px"> array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'ID' => self::$template_post->ID,
- 'post_content' => 'Content revision #4',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'post_type' => self::TEMPLATE_PART_POST_TYPE,
+ 'post_name' => self::TEMPLATE_PART_NAME,
+ 'post_title' => 'My template part',
+ 'post_content' => 'Content',
+ 'post_excerpt' => 'Description of my template part',
+ 'tax_input' => array(
+ 'wp_theme' => array(
+ self::TEST_THEME,
+ ),
+ 'wp_template_part_area' => array(
+ WP_TEMPLATE_PART_AREA_HEADER,
+ ),
+ ),
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_set_post_terms( self::$template_part_post->ID, self::TEST_THEME, 'wp_theme' );
+ wp_set_post_terms( self::$template_part_post->ID, WP_TEMPLATE_PART_AREA_HEADER, 'wp_template_part_area' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Update post to create a new revisions.
- self::$revisions[] = _wp_put_post_revision(
- array(
- 'ID' => self::$template_post->ID,
- 'post_content' => 'Content revision #5',
- )
- );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Update post to create new revisions.
+ foreach ( range( 2, 5 ) as $revision_index ) {
+ self::$template_part_revisions[] = _wp_put_post_revision(
+ array(
+ 'ID' => self::$template_part_post->ID,
+ 'post_content' => 'Content revision #' . $revision_index,
+ )
+ );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Create a new template post to test the get_item method.
- self::$template_post_2 = $factory->post->create_and_get(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Set up template part post.
+ self::$template_part_post_2 = $factory->post->create_and_get(
</ins><span class="cx" style="display: block; padding: 0 10px"> array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'post_type' => self::PARENT_POST_TYPE,
- 'post_name' => self::TEMPLATE_NAME_2,
- 'post_title' => 'My Template 2',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'post_type' => self::TEMPLATE_PART_POST_TYPE,
+ 'post_name' => self::TEMPLATE_PART_NAME_2,
+ 'post_title' => 'My template part 2',
</ins><span class="cx" style="display: block; padding: 0 10px"> 'post_content' => 'Content 2',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'post_excerpt' => 'Description of my template 2',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'post_excerpt' => 'Description of my template part 2',
</ins><span class="cx" style="display: block; padding: 0 10px"> 'tax_input' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'wp_theme' => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'wp_theme' => array(
</ins><span class="cx" style="display: block; padding: 0 10px"> self::TEST_THEME,
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'wp_template_part_area' => array(
+ WP_TEMPLATE_PART_AREA_HEADER,
+ ),
</ins><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_set_post_terms( self::$template_post_2->ID, self::TEST_THEME, 'wp_theme' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_set_post_terms( self::$template_part_post_2->ID, self::TEST_THEME, 'wp_theme' );
+ wp_set_post_terms( self::$template_part_post_2->ID, WP_TEMPLATE_PART_AREA_HEADER, 'wp_template_part_area' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -161,9 +229,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public static function wpTearDownAfterClass() {
</span><span class="cx" style="display: block; padding: 0 10px"> // Also deletes revisions.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach ( self::$revisions as $revision ) {
- wp_delete_post( $revision, true );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( self::$template_revisions as $template_revision ) {
+ wp_delete_post( $template_revision, true );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ foreach ( self::$template_part_revisions as $template_part_revision ) {
+ wp_delete_post( $template_part_revision, true );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -195,12 +267,26 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @coversNothing
+ * @ticket 56922
+ */
+ public function test_context_param() {
+ // A proper data provider cannot be used because this method's signature must match the parent method.
+ // Therefore, actual tests are performed in the test_context_param_with_data_provider method.
+ $this->assertTrue( true );
+ }
+
+ /**
+ * @dataProvider data_context_param_with_data_provider
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_REST_Template_Revisions_Controller::get_context_param
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_context_param() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_context_param_with_data_provider( $rest_base, $template_id ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> // Collection.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/' . $rest_base . '/' . $template_id . '/revisions' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $data = $response->get_data();
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -215,7 +301,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Single.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions/1' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/' . $rest_base . '/' . $template_id . '/revisions/1' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $data = $response->get_data();
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertCount(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -236,17 +322,47 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider for test_context_param.
+ *
+ * @return array
+ */
+ public function data_context_param_with_data_provider() {
+ return array(
+ 'templates' => array( 'templates', self::TEST_THEME . '//' . self::TEMPLATE_NAME ),
+ 'template parts' => array( 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
+ );
+ }
+
+ /**
+ * @coversNothing
+ * @ticket 56922
+ */
+ public function test_get_items() {
+ // A proper data provider cannot be used because this method's signature must match the parent method.
+ // Therefore, actual tests are performed in the test_get_items_with_data_provider method.
+ $this->assertTrue( true );
+ }
+
+ /**
+ * @dataProvider data_get_items_with_data_provider
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_REST_Template_Revisions_Controller::get_items
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $parent_post_property_name A class property name that contains the parent post object.
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_get_items() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_get_items_with_data_provider( $parent_post_property_name, $rest_base, $template_id ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( self::$admin_id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post = self::$$parent_post_property_name;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $request = new WP_REST_Request(
</span><span class="cx" style="display: block; padding: 0 10px"> 'GET',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions'
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ '/wp/v2/' . $rest_base . '/' . $template_id . '/revisions'
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $revisions = $response->get_data();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( WP_Http::OK, $response->get_status(), 'Response is expected to have a status code of 200.' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertCount(
</span><span class="cx" style="display: block; padding: 0 10px"> 4,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -255,7 +371,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- self::$template_post->ID,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post->ID,
</ins><span class="cx" style="display: block; padding: 0 10px"> $revisions[0]['parent'],
</span><span class="cx" style="display: block; padding: 0 10px"> 'Failed asserting that the parent ID of the revision matches the template post ID.'
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -266,7 +382,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- self::$template_post->ID,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post->ID,
</ins><span class="cx" style="display: block; padding: 0 10px"> $revisions[1]['parent'],
</span><span class="cx" style="display: block; padding: 0 10px"> 'Failed asserting that the parent ID of the revision matches the template post ID.'
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -277,7 +393,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- self::$template_post->ID,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post->ID,
</ins><span class="cx" style="display: block; padding: 0 10px"> $revisions[2]['parent'],
</span><span class="cx" style="display: block; padding: 0 10px"> 'Failed asserting that the parent ID of the revision matches the template post ID.'
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -288,7 +404,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- self::$template_post->ID,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post->ID,
</ins><span class="cx" style="display: block; padding: 0 10px"> $revisions[3]['parent'],
</span><span class="cx" style="display: block; padding: 0 10px"> 'Failed asserting that the parent ID of the revision matches the template post ID.'
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -299,40 +415,168 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Data provider for test_get_items_with_data_provider.
+ *
+ * @return array
+ */
+ public function data_get_items_with_data_provider() {
+ return array(
+ 'templates' => array( 'template_post', 'templates', self::TEST_THEME . '//' . self::TEMPLATE_NAME ),
+ 'template parts' => array( 'template_part_post', 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
+ );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @dataProvider data_get_items_endpoint_should_return_unauthorized_https_status_code_for_unauthorized_request
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_REST_Template_Revisions_Controller::get_items_permissions_check
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_get_items_endpoint_should_return_unauthorized_https_status_code_for_unauthorized_request() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_get_items_endpoint_should_return_unauthorized_https_status_code_for_unauthorized_request( $rest_base, $template_id ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( 0 );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $request = new WP_REST_Request( 'GET', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $request = new WP_REST_Request( 'GET', '/wp/v2/' . $rest_base . '/' . $template_id . '/revisions' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertErrorResponse( 'rest_cannot_read', $response, WP_Http::UNAUTHORIZED );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider for test_get_items_endpoint_should_return_unauthorized_https_status_code_for_unauthorized_request.
+ *
+ * @return array
+ */
+ public function data_get_items_endpoint_should_return_unauthorized_https_status_code_for_unauthorized_request() {
+ return array(
+ 'templates' => array( 'templates', self::TEST_THEME . '//' . self::TEMPLATE_NAME ),
+ 'template parts' => array( 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
+ );
+ }
+
+ /**
+ * @dataProvider data_get_items_endpoint_should_return_forbidden_https_status_code_for_users_with_insufficient_permissions
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_REST_Template_Revisions_Controller::get_items_permissions_check
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_get_items_endpoint_should_return_forbidden_https_status_code_for_users_with_insufficient_permissions() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_get_items_endpoint_should_return_forbidden_https_status_code_for_users_with_insufficient_permissions( $rest_base, string $template_id ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( self::$contributor_id );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $request = new WP_REST_Request( 'GET', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $request = new WP_REST_Request( 'GET', '/wp/v2/' . $rest_base . '/' . $template_id . '/revisions' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertErrorResponse( 'rest_cannot_read', $response, WP_Http::FORBIDDEN );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider for test_get_items_endpoint_should_return_unauthorized_https_status_code_for_unauthorized_request.
+ *
+ * @return array
+ */
+ public function data_get_items_endpoint_should_return_forbidden_https_status_code_for_users_with_insufficient_permissions() {
+ return array(
+ 'templates' => array( 'templates', self::TEST_THEME . '//' . self::TEMPLATE_NAME ),
+ 'template parts' => array( 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
+ );
+ }
+
+ /**
+ * @dataProvider data_get_items_for_templates_based_on_theme_files_should_return_bad_response_status
+ * @ticket 61970
+ *
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
+ */
+ public function test_get_items_for_templates_based_on_theme_files_should_return_bad_response_status( $rest_base, $template_id ) {
+ wp_set_current_user( self::$admin_id );
+ switch_theme( 'block-theme' );
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/' . $rest_base . '/' . $template_id . '/revisions' );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse(
+ 'rest_invalid_template',
+ $response,
+ WP_Http::BAD_REQUEST,
+ sprintf( 'Response is expected to have a status code of %d.', WP_Http::BAD_REQUEST )
+ );
+ }
+
+ /**
+ * Data provider for test_get_items_for_templates_based_on_theme_files_should_return_bad_response_status.
+ *
+ * @return array
+ */
+ public function data_get_items_for_templates_based_on_theme_files_should_return_bad_response_status() {
+ return array(
+ 'templates' => array( 'templates', self::TEST_THEME . '//page-home' ),
+ 'template parts' => array( 'template-parts', self::TEST_THEME . '//small-header' ),
+ );
+ }
+
+ /**
+ * @dataProvider data_get_item_for_templates_based_on_theme_files_should_return_bad_response_status
+ * @ticket 56922
+ *
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
+ */
+ public function test_get_item_for_templates_based_on_theme_files_should_return_bad_response_status( $rest_base, $template_id ) {
+ wp_set_current_user( self::$admin_id );
+ switch_theme( 'block-theme' );
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/' . $rest_base . '/' . $template_id . '/revisions/1' );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse(
+ 'rest_invalid_template',
+ $response,
+ WP_Http::BAD_REQUEST,
+ sprintf( 'Response is expected to have a status code of %d.', WP_Http::BAD_REQUEST )
+ );
+ }
+
+ /**
+ * Data provider for test_get_item_for_templates_based_on_theme_files_should_return_bad_response_status.
+ *
+ * @return array
+ */
+ public function data_get_item_for_templates_based_on_theme_files_should_return_bad_response_status() {
+ return array(
+ 'templates' => array( 'templates', self::TEST_THEME . '//page-home' ),
+ 'template parts' => array( 'template-parts', self::TEST_THEME . '//small-header' ),
+ );
+ }
+
+ /**
+ * @coversNothing
+ * @ticket 56922
+ */
+ public function test_get_item() {
+ // A proper data provider cannot be used because this method's signature must match the parent method.
+ // Therefore, actual tests are performed in the test_get_item_with_data_provider method.
+ $this->assertTrue( true );
+ }
+
+ /**
+ * @dataProvider data_get_item_with_data_provider
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_REST_Template_Revisions_Controller::get_item
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $parent_post_property_name A class property name that contains the parent post object.
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_get_item() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_get_item_with_data_provider( $parent_post_property_name, $rest_base, $template_id ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( self::$admin_id );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $revisions = wp_get_post_revisions( self::$template_post, array( 'fields' => 'ids' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post = self::$$parent_post_property_name;
+
+ $revisions = wp_get_post_revisions( $parent_post, array( 'fields' => 'ids' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> $revision_id = array_shift( $revisions );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $request = new WP_REST_Request( 'GET', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions/' . $revision_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $request = new WP_REST_Request( 'GET', '/wp/v2/' . $rest_base . '/' . $template_id . '/revisions/' . $revision_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $revision = $response->get_data();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -343,7 +587,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "Failed asserting that the revision id is the same as $revision_id"
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- self::$template_post->ID,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post->ID,
</ins><span class="cx" style="display: block; padding: 0 10px"> $revision['parent'],
</span><span class="cx" style="display: block; padding: 0 10px"> sprintf(
</span><span class="cx" style="display: block; padding: 0 10px"> 'Failed asserting that the parent id of the revision is the same as %s.',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -353,47 +597,125 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider for test_get_item_with_data_provider.
+ *
+ * @return array
+ */
+ public function data_get_item_with_data_provider() {
+ return array(
+ 'templates' => array( 'template_post', 'templates', self::TEST_THEME . '//' . self::TEMPLATE_NAME ),
+ 'template parts' => array( 'template_part_post', 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
+ );
+ }
+
+ /**
+ * @dataProvider data_get_item_not_found
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_REST_Template_Revisions_Controller::get_item
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $parent_post_property_name A class property name that contains the parent post object.
+ * @param string $rest_base Base part of the REST API endpoint to test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_get_item_not_found() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_get_item_not_found( $parent_post_property_name, $rest_base ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( self::$admin_id );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $revisions = wp_get_post_revisions( self::$template_post, array( 'fields' => 'ids' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post = self::$$parent_post_property_name;
+
+ $revisions = wp_get_post_revisions( $parent_post, array( 'fields' => 'ids' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> $revision_id = array_shift( $revisions );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $request = new WP_REST_Request( 'GET', '/wp/v2/templates/invalid//parent/revisions/' . $revision_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $request = new WP_REST_Request( 'GET', '/wp/v2/' . $rest_base . '/invalid//parent/revisions/' . $revision_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertErrorResponse( 'rest_post_invalid_parent', $response, WP_Http::NOT_FOUND );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider for test_get_item_not_found.
+ *
+ * @return array
+ */
+ public function data_get_item_not_found() {
+ return array(
+ 'templates' => array( 'template_post', 'templates' ),
+ 'template parts' => array( 'template_part_post', 'template-parts' ),
+ );
+ }
+
+ /**
+ * @dataProvider data_get_item_invalid_parent_id
+ * @covers WP_REST_Template_Revisions_Controller::get_item
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 59875
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $parent_post_property_name A class property name that contains the parent post object.
+ * @param string $actual_parent_post_property_name A class property name that contains the parent post object.
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_get_item_invalid_parent_id() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_get_item_invalid_parent_id( $parent_post_property_name, $actual_parent_post_property_name, $rest_base, $template_id ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( self::$admin_id );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $revisions = wp_get_post_revisions( self::$template_post, array( 'fields' => 'ids' ) );
- $revision_id = array_shift( $revisions );
</del><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $request = new WP_REST_Request( 'GET', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME_2 . '/revisions/' . $revision_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post = self::$$parent_post_property_name;
+ $actual_parent_post = self::$$actual_parent_post_property_name;
+ $revisions = wp_get_post_revisions( $parent_post, array( 'fields' => 'ids' ) );
+ $revision_id = array_shift( $revisions );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $request = new WP_REST_Request( 'GET', '/wp/v2/' . $rest_base . '/' . $template_id . '/revisions/' . $revision_id );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertErrorResponse( 'rest_revision_parent_id_mismatch', $response, 404 );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $expected_message = 'The revision does not belong to the specified parent with id of "' . self::$template_post_2->ID . '"';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $expected_message = 'The revision does not belong to the specified parent with id of "' . $actual_parent_post->ID . '"';
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( $expected_message, $response->as_error()->get_error_messages()[0], 'The message must contain the correct parent ID.' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider for test_get_item_invalid_parent_id.
+ *
+ * @return array
+ */
+ public function data_get_item_invalid_parent_id() {
+ return array(
+ 'templates' => array(
+ 'template_post',
+ 'template_post_2',
+ 'templates',
+ self::TEST_THEME . '//' . self::TEMPLATE_NAME_2,
+ ),
+ 'template parts' => array(
+ 'template_part_post',
+ 'template_part_post_2',
+ 'template-parts',
+ self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME_2,
+ ),
+ );
+ }
+
+ /**
+ * @coversNothing
+ * @ticket 56922
+ */
+ public function test_prepare_item() {
+ // A proper data provider cannot be used because this method's signature must match the parent method.
+ // Therefore, actual tests are performed in the test_prepare_item_with_data_provider method.
+ $this->assertTrue( true );
+ }
+
+ /**
+ * @dataProvider data_prepare_item_with_data_provider
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_REST_Template_Revisions_Controller::prepare_item_for_response
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $parent_post_property_name A class property name that contains the parent post object.
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_prepare_item() {
- $revisions = wp_get_post_revisions( self::$template_post, array( 'fields' => 'ids' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_prepare_item_with_data_provider( $parent_post_property_name, $rest_base, $template_id ) {
+ $parent_post = self::$$parent_post_property_name;
+ $revisions = wp_get_post_revisions( $parent_post, array( 'fields' => 'ids' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> $revision_id = array_shift( $revisions );
</span><span class="cx" style="display: block; padding: 0 10px"> $post = get_post( $revision_id );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $request = new WP_REST_Request( 'GET', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions/' . $revision_id );
- $controller = new WP_REST_Template_Revisions_Controller( self::PARENT_POST_TYPE );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $request = new WP_REST_Request( 'GET', '/wp/v2/' . $rest_base . '/' . $template_id . '/revisions/' . $revision_id );
+ $controller = new WP_REST_Template_Revisions_Controller( $parent_post->post_type );
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = $controller->prepare_item_for_response( $post, $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertInstanceOf(
</span><span class="cx" style="display: block; padding: 0 10px"> WP_REST_Response::class,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -409,11 +731,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "Failed asserting that the revision id is the same as $revision_id."
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- self::$template_post->ID,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post->ID,
</ins><span class="cx" style="display: block; padding: 0 10px"> $revision['parent'],
</span><span class="cx" style="display: block; padding: 0 10px"> sprintf(
</span><span class="cx" style="display: block; padding: 0 10px"> 'Failed asserting that the parent id of the revision is the same as %s.',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- self::$template_post->ID
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post->ID
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -421,35 +743,63 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertIsArray( $links, 'Failed asserting that the links are an array.' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertStringEndsWith(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- self::TEST_THEME . '//' . self::TEMPLATE_NAME . '/revisions/' . $revision_id,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $template_id . '/revisions/' . $revision_id,
</ins><span class="cx" style="display: block; padding: 0 10px"> $links['self'][0]['href'],
</span><span class="cx" style="display: block; padding: 0 10px"> sprintf(
</span><span class="cx" style="display: block; padding: 0 10px"> 'Failed asserting that the self link ends with %s.',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- self::TEST_THEME . '//' . self::TEMPLATE_NAME . '/revisions/' . $revision_id
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $template_id . '/revisions/' . $revision_id
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertStringEndsWith(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- self::TEST_THEME . '//' . self::TEMPLATE_NAME,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $template_id,
</ins><span class="cx" style="display: block; padding: 0 10px"> $links['parent'][0]['href'],
</span><span class="cx" style="display: block; padding: 0 10px"> sprintf(
</span><span class="cx" style="display: block; padding: 0 10px"> 'Failed asserting that the parent link ends with %s.',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- self::TEST_THEME . '//' . self::TEMPLATE_NAME
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $template_id
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider for test_prepare_item_with_data_provider.
+ *
+ * @return array
+ */
+ public function data_prepare_item_with_data_provider() {
+ return array(
+ 'templates' => array( 'template_post', 'templates', self::TEST_THEME . '//' . self::TEMPLATE_NAME ),
+ 'template parts' => array( 'template_part_post', 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
+ );
+ }
+
+ /**
+ * @coversNothing
+ * @ticket 56922
+ */
+ public function test_get_item_schema() {
+ // A proper data provider cannot be used because this method's signature must match the parent method.
+ // Therefore, actual tests are performed in the test_prepare_item_with_data_provider method.
+ $this->assertTrue( true );
+ }
+
+ /**
+ * @dataProvider data_get_item_schema_with_data_provider
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_REST_Template_Revisions_Controller::get_item_schema
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
+ * @param int $properties_count Number of properties to check for in the schema.
+ * @param array $additional_properties Additional properties to check for in the schema.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_get_item_schema() {
- $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_get_item_schema_with_data_provider( $rest_base, $template_id, $properties_count, $additional_properties = array() ) {
+ $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/' . $rest_base . '/' . $template_id . '/revisions' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $data = $response->get_data();
</span><span class="cx" style="display: block; padding: 0 10px"> $properties = $data['schema']['properties'];
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertCount( 19, $properties );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertCount( $properties_count, $properties );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'id', $properties, 'ID key should exist in properties.' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'slug', $properties, 'Slug key should exist in properties.' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'theme', $properties, 'Theme key should exist in properties.' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -463,14 +813,38 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'has_theme_file', $properties, 'has_theme_file key should exist in properties.' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'author', $properties, 'author key should exist in properties.' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'modified', $properties, 'modified key should exist in properties.' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertArrayHasKey( 'is_custom', $properties, 'is_custom key should exist in properties.' );
</del><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'parent', $properties, 'Parent key should exist in properties.' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'author_text', $properties, 'author_text key should exist in properties.' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'original_source', $properties, 'original_source key should exist in properties.' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertArrayHasKey( 'plugin', $properties, 'plugin key should exist in properties.' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ foreach ( $additional_properties as $additional_property ) {
+ $this->assertArrayHasKey( $additional_property, $properties, $additional_property . ' key should exist in properties.' );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider for data_get_item_schema_with_data_provider.
+ *
+ * @return array
+ */
+ public function data_get_item_schema_with_data_provider() {
+ return array(
+ 'templates' => array(
+ 'templates',
+ self::TEST_THEME . '//' . self::TEMPLATE_NAME,
+ 19,
+ array( 'is_custom', 'plugin' ),
+ ),
+ 'template parts' => array(
+ 'template-parts',
+ self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME,
+ 18,
+ array( 'area' ),
+ ),
+ );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * @coversNothing
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -497,16 +871,35 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @coversNothing
+ * @ticket 56922
+ */
+ public function test_delete_item() {
+ // A proper data provider cannot be used because this method's signature must match the parent method.
+ // Therefore, actual tests are performed in the test_delete_item_with_data_provider method.
+ $this->assertTrue( true );
+ }
+
+ /**
+ * @dataProvider data_delete_item_with_data_provider
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_REST_Templates_Controller::delete_item
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $parent_post_property_name A class property name that contains the parent post object.
+ * @param string $revisions_property_name A class property name that contains the revisions array.
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_delete_item() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_delete_item_with_data_provider( $parent_post_property_name, $revisions_property_name, $rest_base, $template_id ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( self::$admin_id );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $revision_id = _wp_put_post_revision( self::$template_post );
- self::$revisions[] = $revision_id;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post = self::$$parent_post_property_name;
+ $revisions = self::$$revisions_property_name;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $request = new WP_REST_Request( 'DELETE', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions/' . $revision_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $revision_id = _wp_put_post_revision( $parent_post );
+ $revisions[] = $revision_id;
+
+ $request = new WP_REST_Request( 'DELETE', '/wp/v2/' . $rest_base . '/' . $template_id . '/revisions/' . $revision_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> $request->set_param( 'force', true );
</span><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -515,15 +908,46 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider for test_delete_item_with_data_provider.
+ *
+ * @return array
+ */
+ public function data_delete_item_with_data_provider() {
+ return array(
+ 'templates' => array(
+ 'template_post',
+ 'template_revisions',
+ 'templates',
+ self::TEST_THEME . '//' . self::TEMPLATE_NAME,
+ ),
+ 'template parts' => array(
+ 'template_part_post',
+ 'template_part_revisions',
+ 'template-parts',
+ self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME,
+ ),
+ );
+ }
+
+ /**
+ * @dataProvider data_delete_item_incorrect_permission
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_REST_Templates_Controller::delete_item
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $parent_post_property_name A class property name that contains the parent post object.
+ * @param string $revisions_property_name A class property name that contains the revisions array.
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_delete_item_incorrect_permission() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_delete_item_incorrect_permission( $parent_post_property_name, $revisions_property_name, $rest_base, $template_id ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( self::$contributor_id );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $revision_id = _wp_put_post_revision( self::$template_post );
- self::$revisions[] = $revision_id;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post = self::$$parent_post_property_name;
+ $revisions = self::$$revisions_property_name;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $request = new WP_REST_Request( 'DELETE', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions/' . $revision_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $revision_id = _wp_put_post_revision( $parent_post );
+ $revisions[] = $revision_id;
+
+ $request = new WP_REST_Request( 'DELETE', '/wp/v2/' . $rest_base . '/' . $template_id . '/revisions/' . $revision_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> $request->set_param( 'force', true );
</span><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertErrorResponse( 'rest_cannot_delete', $response, WP_Http::FORBIDDEN );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -530,15 +954,47 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider for test_delete_item_with_data_provider.
+ *
+ * @return array
+ */
+ public function data_delete_item_incorrect_permission() {
+ return array(
+ 'templates' => array(
+ 'template_post',
+ 'template_revisions',
+ 'templates',
+ self::TEST_THEME . '//' . self::TEMPLATE_NAME,
+ ),
+ 'template parts' => array(
+ 'template_part_post',
+ 'template_part_revisions',
+ 'template-parts',
+ self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME,
+ ),
+ );
+ }
+
+ /**
+ * @dataProvider data_delete_item_no_permission
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_REST_Templates_Controller::delete_item
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $parent_post_property_name A class property name that contains the parent post object.
+ * @param string $revisions_property_name A class property name that contains the revisions array.
+ * @param string $rest_base Base part of the REST API endpoint to test.
+ * @param string $template_id Template ID to use in the test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_delete_item_no_permission() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_delete_item_no_permission( $parent_post_property_name, $revisions_property_name, $rest_base, $template_id ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( 0 );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $revision_id = _wp_put_post_revision( self::$template_post );
- self::$revisions[] = $revision_id;
</del><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $request = new WP_REST_Request( 'DELETE', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions/' . $revision_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post = self::$$parent_post_property_name;
+ $revisions = self::$$revisions_property_name;
+
+ $revision_id = _wp_put_post_revision( $parent_post );
+ $revisions[] = $revision_id;
+
+ $request = new WP_REST_Request( 'DELETE', '/wp/v2/' . $rest_base . '/' . $template_id . '/revisions/' . $revision_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> $request->set_param( 'force', true );
</span><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertErrorResponse( 'rest_cannot_delete', $response, WP_Http::UNAUTHORIZED );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -545,18 +1001,68 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider for test_delete_item_no_permission.
+ *
+ * @return array
+ */
+ public function data_delete_item_no_permission() {
+ return array(
+ 'templates' => array(
+ 'template_post',
+ 'template_revisions',
+ 'templates',
+ self::TEST_THEME . '//' . self::TEMPLATE_NAME,
+ ),
+ 'template parts' => array(
+ 'template_part_post',
+ 'template_part_revisions',
+ 'template-parts',
+ self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME,
+ ),
+ );
+ }
+
+ /**
+ * @dataProvider data_delete_item_not_found
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers WP_REST_Template_Revisions_Controller::get_item
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 56922
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $parent_post_property_name A class property name that contains the parent post object.
+ * @param string $revisions_property_name A class property name that contains the revisions array.
+ * @param string $rest_base Base part of the REST API endpoint to test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_delete_item_not_found() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_delete_item_not_found( $parent_post_property_name, $revisions_property_name, $rest_base ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( self::$admin_id );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $revision_id = _wp_put_post_revision( self::$template_post );
- self::$revisions[] = $revision_id;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parent_post = self::$$parent_post_property_name;
+ $revisions = self::$$revisions_property_name;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $request = new WP_REST_Request( 'DELETE', '/wp/v2/templates/invalid//parent/revisions/' . $revision_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $revision_id = _wp_put_post_revision( $parent_post );
+ $revisions[] = $revision_id;
+
+ $request = new WP_REST_Request( 'DELETE', '/wp/v2/' . $rest_base . '/invalid//parent/revisions/' . $revision_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> $request->set_param( 'force', true );
</span><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertErrorResponse( 'rest_post_invalid_parent', $response, WP_Http::NOT_FOUND );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * Data provider for test_delete_item_not_found.
+ *
+ * @return array
+ */
+ public function data_delete_item_not_found() {
+ return array(
+ 'templates' => array(
+ 'template_post',
+ 'template_revisions',
+ 'templates',
+ ),
+ 'template parts' => array(
+ 'template_part_post',
+ 'template_part_revisions',
+ 'template-parts',
+ ),
+ );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>