-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pattern CPT: Add validation for the pattern metadata (#41)
* Add schema to the post meta, for validation * Add validation to check the pattern title existence * Check for a real `prepared_post` before using it * Add test cases for title validation
- Loading branch information
Showing
3 changed files
with
160 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
...html/wp-content/plugins/pattern-directory/tests/phpunit/pattern-title-validation-test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
/** | ||
* Test Block Pattern validation. | ||
*/ | ||
|
||
use const WordPressdotorg\Pattern_Directory\Pattern_Post_Type\POST_TYPE; | ||
|
||
/** | ||
* Test pattern validation. | ||
*/ | ||
class Pattern_Title_Validation_Test extends WP_UnitTestCase { | ||
protected static $pattern_id; | ||
protected static $user; | ||
|
||
/** | ||
* Setup fixtures that are shared across all tests. | ||
*/ | ||
public static function wpSetUpBeforeClass( $factory ) { | ||
self::$pattern_id = $factory->post->create( | ||
array( 'post_type' => POST_TYPE ) | ||
); | ||
self::$user = $factory->user->create( | ||
array( | ||
'role' => 'administrator', | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Helper function to handle REST requests to save the pattern. | ||
*/ | ||
protected function save_block( $args = array() ) { | ||
$request = new WP_REST_Request( 'POST', '/wp/v2/wporg-pattern/' . self::$pattern_id ); | ||
$request->set_header( 'content-type', 'application/json' ); | ||
$request_args = wp_parse_args( $args, array( | ||
'status' => 'publish', | ||
'content' => "<!-- wp:paragraph -->\n<p>This is a block.</p>\n<!-- /wp:paragraph -->", | ||
) ); | ||
$request->set_body( json_encode( $request_args ) ); | ||
return rest_do_request( $request ); | ||
} | ||
|
||
/** | ||
* Test valid pattern title: Add a new title. | ||
*/ | ||
public function test_valid_create_title() { | ||
wp_set_current_user( self::$user ); | ||
$response = $this->save_block( array( 'title' => 'Default Paragraph' ) ); | ||
$this->assertFalse( $response->is_error() ); | ||
} | ||
|
||
/** | ||
* Test valid pattern title: empty title for a draft pattern. | ||
*/ | ||
public function test_valid_empty_title_draft() { | ||
wp_set_current_user( self::$user ); | ||
$response = $this->save_block( array( | ||
'title' => '', | ||
'status' => 'draft', | ||
) ); | ||
$this->assertFalse( $response->is_error() ); | ||
} | ||
|
||
/** | ||
* Test valid pattern title: the existing pattern already has a title. | ||
*/ | ||
public function test_valid_title_already_set() { | ||
wp_set_current_user( self::$user ); | ||
wp_update_post( array( | ||
'ID' => self::$pattern_id, | ||
'post_title' => 'Test Title', | ||
) ); | ||
$response = $this->save_block(); | ||
$this->assertFalse( $response->is_error() ); | ||
} | ||
|
||
/** | ||
* Test invalid pattern title: Published pattern, setting empty title. | ||
*/ | ||
public function test_invalid_empty_new_title() { | ||
wp_set_current_user( self::$user ); | ||
$response = $this->save_block( array( | ||
'status' => 'publish', | ||
'title' => '', | ||
) ); | ||
$this->assertTrue( $response->is_error() ); | ||
$data = $response->get_data(); | ||
$this->assertSame( 'rest_pattern_empty_title', $data['code'] ); | ||
} | ||
|
||
/** | ||
* Test invalid pattern title: Published pattern, has empty title with no new title. | ||
*/ | ||
public function test_invalid_empty_existing_title() { | ||
wp_set_current_user( self::$user ); | ||
wp_update_post( array( | ||
'ID' => self::$pattern_id, | ||
'post_title' => '', | ||
) ); | ||
$response = $this->save_block(); | ||
$this->assertTrue( $response->is_error() ); | ||
$data = $response->get_data(); | ||
$this->assertSame( 'rest_pattern_empty_title', $data['code'] ); | ||
} | ||
} | ||
|