Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ public function get_item_schema() {
),
'origin' => array(
'description' => __( 'Source of a customized template' ),
'type' => 'string',
'type' => array( 'string', 'null' ),
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
Expand Down Expand Up @@ -1150,8 +1150,16 @@ public function get_item_schema() {
),
'modified' => array(
'description' => __( "The date the template was last modified, in the site's timezone." ),
'type' => 'string',
'format' => 'date-time',
'oneOf' => array(
array(
'type' => 'string',
'format' => 'date-time',
),
array(
'type' => 'boolean',
'enum' => array( false ),
),
),
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
Expand Down
87 changes: 87 additions & 0 deletions tests/phpunit/tests/rest-api/wpRestTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,93 @@ public function test_get_item_schema() {
$this->assertArrayHasKey( 'plugin', $properties );
}

/**
* Test that the origin property schema allows null values.
*
* @ticket 64168
* @covers WP_REST_Templates_Controller::get_item_schema
*/
public function test_origin_property_schema_allows_null() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/templates' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];

$this->assertArrayHasKey( 'origin', $properties );
$this->assertArrayHasKey( 'type', $properties['origin'] );
$this->assertIsArray( $properties['origin']['type'] );
$this->assertCount( 2, $properties['origin']['type'] );
$this->assertContains( 'string', $properties['origin']['type'] );
$this->assertContains( 'null', $properties['origin']['type'] );
}

/**
* Test that the modified property schema allows boolean (false) values.
*
* @ticket 64168
* @covers WP_REST_Templates_Controller::get_item_schema
*/
public function test_modified_property_schema_allows_boolean() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/templates' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];

$this->assertArrayHasKey( 'modified', $properties );
$this->assertArrayHasKey( 'oneOf', $properties['modified'] );
$this->assertIsArray( $properties['modified']['oneOf'] );
$this->assertCount( 2, $properties['modified']['oneOf'] );

$string_schema = null;
$boolean_schema = null;
foreach ( $properties['modified']['oneOf'] as $schema ) {
if ( isset( $schema['type'] ) && 'string' === $schema['type'] ) {
$string_schema = $schema;
}
if ( isset( $schema['type'] ) && 'boolean' === $schema['type'] ) {
$boolean_schema = $schema;
}
}

$this->assertNotNull( $string_schema, 'String schema not found in oneOf' );
$this->assertArrayHasKey( 'format', $string_schema );
$this->assertSame( 'date-time', $string_schema['format'] );

$this->assertNotNull( $boolean_schema, 'Boolean schema not found in oneOf' );
$this->assertArrayHasKey( 'enum', $boolean_schema );
$this->assertSame( array( false ), $boolean_schema['enum'] );
}

/**
* Test that template parts from theme files have null origin and false modified.
*
* @ticket 64168
* @covers WP_REST_Templates_Controller::prepare_item_for_response
*/
public function test_template_part_from_theme_has_null_origin_and_false_modified() {
wp_set_current_user( self::$admin_id );
switch_theme( 'block-theme' );
$request = new WP_REST_Request( 'GET', '/wp/v2/template-parts' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$theme_template_part = null;
foreach ( $data as $template_part ) {
if ( isset( $template_part['source'] ) && 'theme' === $template_part['source'] ) {
$theme_template_part = $template_part;
break;
}
}

$this->assertNotNull( $theme_template_part, 'No theme template part found in response' );

$this->assertArrayHasKey( 'origin', $theme_template_part );
$this->assertNull( $theme_template_part['origin'] );

$this->assertArrayHasKey( 'modified', $theme_template_part );
$this->assertFalse( $theme_template_part['modified'] );
}

protected function find_and_normalize_template_by_id( $templates, $id ) {
foreach ( $templates as $template ) {
if ( $template['id'] === $id ) {
Expand Down
Loading