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..3dda0ad3 --- /dev/null +++ b/public_html/wp-content/plugins/pattern-directory/tests/phpunit/endpoint-wporg-pattern-test.php @@ -0,0 +1,74 @@ +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, 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 ); + $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 ); + + // Pattern content should always exist. + $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'] ), 'Category slugs is not a sequential array.' ); + $this->assertTrue( array_is_list( $pattern['meta']['wpop_block_types'] ), 'Block types is not a sequential array.' ); + } +}