From 0be257af75d047312adb7e76e7d537970f0c1b63 Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Fri, 4 Oct 2024 16:18:31 -0400 Subject: [PATCH 1/3] Pattern directory: Add pattern endpoint test matching schema --- .../phpunit/endpoint-wporg-pattern-test.php | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 public_html/wp-content/plugins/pattern-directory/tests/phpunit/endpoint-wporg-pattern-test.php diff --git a/public_html/wp-content/plugins/pattern-directory/tests/phpunit/endpoint-wporg-pattern-test.php b/public_html/wp-content/plugins/pattern-directory/tests/phpunit/endpoint-wporg-pattern-test.php new file mode 100644 index 00000000..7ba04442 --- /dev/null +++ b/public_html/wp-content/plugins/pattern-directory/tests/phpunit/endpoint-wporg-pattern-test.php @@ -0,0 +1,62 @@ +term->create( + [ + 'taxonomy' => 'wporg-pattern-category', + 'name' => $term_name, + ] + ); + } + self::$pattern_id = $factory->post->create( + array( + 'post_title' => 'Services call to action with image on left', + 'post_type' => POST_TYPE, + 'post_content' => '

Guiding your business through the project

Experience the fusion of imagination and expertise with Études—the catalyst for architectural transformations that enrich the world around us.

Our services

', + 'meta_input' => [ + 'wpop_contains_block_types' => 'core/heading,core/paragraph', + 'wpop_viewport_width' => 1400, + 'wpop_description' => 'An image, title, paragraph and a CTA button to describe services.', + ], + ) + ); + $factory->term->add_post_terms( self::$pattern_id, $term_ids, 'wporg-pattern-category' ); + } + + /** + * Verify the pattern response matches the schema. + */ + public function test_pattern_directory_api() { + $request = new WP_REST_Request( 'GET', '/wp/v2/wporg-pattern/' . self::$pattern_id ); + $response = rest_do_request( $request ); + $this->assertFalse( $response->is_error() ); + $pattern = $response->get_data(); + + // New request to get schema, so that `rest_api_init` is called (to register the custom endpoint fields). + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/wporg-pattern/' . self::$pattern_id ); + $response = rest_do_request( $request ); + $schema = $response->get_data(); + $schema = $schema['schema']; + + $result = rest_validate_value_from_schema( $pattern, $schema ); + $this->assertTrue( $result ); + } +} From 66e31afadffc1567d53d9734568834038476893d Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Mon, 7 Oct 2024 18:20:49 -0400 Subject: [PATCH 2/3] Add extra tests to type-check values --- .../tests/phpunit/endpoint-wporg-pattern-test.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/public_html/wp-content/plugins/pattern-directory/tests/phpunit/endpoint-wporg-pattern-test.php b/public_html/wp-content/plugins/pattern-directory/tests/phpunit/endpoint-wporg-pattern-test.php index 7ba04442..a3bdf3f9 100644 --- a/public_html/wp-content/plugins/pattern-directory/tests/phpunit/endpoint-wporg-pattern-test.php +++ b/public_html/wp-content/plugins/pattern-directory/tests/phpunit/endpoint-wporg-pattern-test.php @@ -42,7 +42,12 @@ public static function wpSetUpBeforeClass( $factory ) { } /** - * Verify the pattern response matches the schema. + * Verify the pattern response matches the schema, plus strict type checking + * for the array values. + * + * `rest_validate_value_from_schema` will check most values, but it also + * "normalizes" array values to associative arrays, which does not happen + * in practice, so we need to manually test those values. */ public function test_pattern_directory_api() { $request = new WP_REST_Request( 'GET', '/wp/v2/wporg-pattern/' . self::$pattern_id ); @@ -58,5 +63,12 @@ public function test_pattern_directory_api() { $result = rest_validate_value_from_schema( $pattern, $schema ); $this->assertTrue( $result ); + + // Pattern content should always exist. + $this->assertNotEmpty( $pattern['pattern_content'] ); + + // Check that these arrays are sequential, not associative arrays. + $this->assertTrue( array_is_list( $pattern['category_slugs'] ) ); + $this->assertTrue( array_is_list( $pattern['meta']['wpop_block_types'] ) ); } } From 61c8e979417cf030b7c94390c1901f6d474b971e Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Tue, 8 Oct 2024 12:29:54 -0400 Subject: [PATCH 3/3] Add messages to type check assertions --- .../tests/phpunit/endpoint-wporg-pattern-test.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/public_html/wp-content/plugins/pattern-directory/tests/phpunit/endpoint-wporg-pattern-test.php b/public_html/wp-content/plugins/pattern-directory/tests/phpunit/endpoint-wporg-pattern-test.php index a3bdf3f9..3dda0ad3 100644 --- a/public_html/wp-content/plugins/pattern-directory/tests/phpunit/endpoint-wporg-pattern-test.php +++ b/public_html/wp-content/plugins/pattern-directory/tests/phpunit/endpoint-wporg-pattern-test.php @@ -65,10 +65,10 @@ public function test_pattern_directory_api() { $this->assertTrue( $result ); // Pattern content should always exist. - $this->assertNotEmpty( $pattern['pattern_content'] ); + $this->assertNotEmpty( $pattern['pattern_content'], 'Pattern content is empty.' ); // Check that these arrays are sequential, not associative arrays. - $this->assertTrue( array_is_list( $pattern['category_slugs'] ) ); - $this->assertTrue( array_is_list( $pattern['meta']['wpop_block_types'] ) ); + $this->assertTrue( array_is_list( $pattern['category_slugs'] ), 'Category slugs is not a sequential array.' ); + $this->assertTrue( array_is_list( $pattern['meta']['wpop_block_types'] ), 'Block types is not a sequential array.' ); } }