Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed May 25, 2021
1 parent 600db7c commit 2fc65d0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -363,20 +363,21 @@ public function delete_item( $request ) {
protected function prepare_item_for_database( $request ) {
$template = $request['id'] ? get_block_template( $request['id'], $this->post_type ) : null;
$changes = new stdClass();
$changes->post_name = $template->slug;
if ( null === $template ) {
$changes->post_type = $this->post_type;
$changes->post_status = 'publish';
$changes->tax_input = array(
'wp_theme' => isset( $request['theme'] ) ? $request['content'] : wp_get_theme()->get_stylesheet(),
'wp_theme' => isset( $request['theme'] ) ? $request['theme'] : wp_get_theme()->get_stylesheet(),
);
} elseif ( 'custom' !== $template->source ) {
$changes->post_name = $template->slug;
$changes->post_type = $this->post_type;
$changes->post_status = 'publish';
$changes->tax_input = array(
'wp_theme' => $template->theme,
);
} else {
$changes->post_name = $template->slug;
$changes->ID = $template->wp_id;
$changes->post_status = 'publish';
}
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/block-template.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Block_Template_Test class
*
*
* @package WordPress
*/

Expand Down
31 changes: 16 additions & 15 deletions tests/phpunit/tests/rest-api/rest-templates-controller.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?php
/**
* Unit tests covering the templates endpoint..
*
* @package WordPress
* @subpackage REST API
*/

class WP_REST_Template_Controller_Test extends WP_Test_REST_Controller_Testcase {
/**
Expand All @@ -25,7 +31,7 @@ public static function wpSetupBeforeClass( $factory ) {
'post_name' => 'my_template',
'post_title' => 'My Template',
'post_content' => 'Content',
'post_excerpt' => 'Description of my template',
'post_excerpt' => 'Description of my template.',
'tax_input' => array(
'wp_theme' => array(
get_stylesheet(),
Expand Down Expand Up @@ -76,8 +82,8 @@ function find_and_normalize_template_by_id( $templates, $id ) {

$this->assertEquals(
array(
'id' => 'tt1-blocks//my_template',
'theme' => 'tt1-blocks',
'id' => 'default//my_template',
'theme' => 'default',
'slug' => 'my_template',
'title' => array(
'raw' => 'My Template',
Expand All @@ -90,22 +96,22 @@ function find_and_normalize_template_by_id( $templates, $id ) {
'wp_id' => self::$post->ID,
'has_theme_file' => false,
),
find_and_normalize_template_by_id( $data, 'tt1-blocks//my_template' )
find_and_normalize_template_by_id( $data, 'default//my_template' )
);
}

public function test_get_item() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/templates/tt1-blocks//my_template' );
$request = new WP_REST_Request( 'GET', '/wp/v2/templates/default//my_template' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
unset( $data['content'] );
unset( $data['_links'] );

$this->assertEquals(
array(
'id' => 'tt1-blocks//my_template',
'theme' => 'tt1-blocks',
'id' => 'default//my_template',
'theme' => 'default',
'slug' => 'my_template',
'title' => array(
'raw' => 'My Template',
Expand Down Expand Up @@ -140,8 +146,8 @@ public function test_create_item() {

$this->assertEquals(
array(
'id' => 'tt1-blocks//my_custom_template',
'theme' => 'tt1-blocks',
'id' => 'default//my_custom_template',
'theme' => 'default',
'slug' => 'my_custom_template',
'title' => array(
'raw' => 'My Template',
Expand All @@ -162,7 +168,7 @@ public function test_create_item() {

public function test_update_item() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'PUT', '/wp/v2/templates/tt1-blocks//index' );
$request = new WP_REST_Request( 'PUT', '/wp/v2/templates/default//my_template' );
$request->set_body_params(
array(
'title' => 'My new Index Title',
Expand All @@ -179,11 +185,6 @@ public function test_delete_item() {
$request = new WP_REST_Request( 'DELETE', '/wp/v2/templates/justrandom//template' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_template_not_found', $response, 404 );

wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'DELETE', '/wp/v2/templates/tt1-blocks//single' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_template', $response, 400 );
}

public function test_prepare_item() {
Expand Down

0 comments on commit 2fc65d0

Please sign in to comment.