From 88d5ae975642c6963a9d86d833243f0f5cd497f9 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 24 Jul 2020 14:25:58 -0700 Subject: [PATCH] Add tests for ObsoleteBlockAttributeRemover --- .../src/ObsoleteBlockAttributeRemoverTest.php | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 tests/php/src/ObsoleteBlockAttributeRemoverTest.php diff --git a/tests/php/src/ObsoleteBlockAttributeRemoverTest.php b/tests/php/src/ObsoleteBlockAttributeRemoverTest.php new file mode 100644 index 00000000000..1bb87d9bb2f --- /dev/null +++ b/tests/php/src/ObsoleteBlockAttributeRemoverTest.php @@ -0,0 +1,149 @@ + 'data-amp-carousel', + 'ampLayout' => 'data-amp-layout', + 'ampLightbox' => 'data-amp-lightbox', + 'ampNoLoading' => 'data-amp-noloading', + ]; + + public function setUp() { + parent::setUp(); + $this->instance = new ObsoleteBlockAttributeRemover(); + } + + /** @covers ObsoleteBlockAttributeRemover::__construct() */ + public function test_it_can_be_initialized() { + $this->assertInstanceOf( Service::class, $this->instance ); + $this->assertInstanceOf( Registerable::class, $this->instance ); + $this->assertInstanceOf( Delayed::class, $this->instance ); + } + + /** @covers ObsoleteBlockAttributeRemover::get_registration_action() */ + public function test_get_registration_action() { + $this->assertEquals( 'rest_api_init', ObsoleteBlockAttributeRemover::get_registration_action() ); + } + + /** @covers ObsoleteBlockAttributeRemover::register() */ + public function test_register() { + $editor_post_types = get_post_types_by_support( 'editor' ); + $this->assertNotEmpty( $editor_post_types ); + $this->instance->register(); + foreach ( $editor_post_types as $editor_post_type ) { + $this->assertEquals( 10, has_filter( "rest_prepare_{$editor_post_type}", [ $this->instance, 'filter_rest_prepare_post' ] ) ); + } + } + + /** @return array */ + public function get_block_data() { + return [ + 'gallery_carousel_lightbox' => [ + ' + + + + ', + 2, + ], + 'image_block_multi_attrs' => [ + ' + +
Unicorn Wallpaper
Noloading and lightbox and fixed layout
+ + ', + 3, + ], + 'image_block' => [ + ' + +
Fixed layout
+ + ', + 1, // Expect props. + ], + 'amp_fit_text' => [ + ' + +

Here is amp-fit-text!

+ + ', + 0, // Expect props. + ], + ]; + } + + /** + * @covers ObsoleteBlockAttributeRemover::test_filter_rest_prepare_post() + * + * @dataProvider get_block_data + * @param string $block_content + * @param int $expected_prop_count + */ + public function test_filter_rest_prepare_post_raw( $block_content, $expected_prop_count ) { + $block_content = str_replace( "\t", '', trim( $block_content ) ); + + $parsed_blocks = parse_blocks( $block_content ); + $this->assertCount( 1, $parsed_blocks ); + $parsed_block = array_shift( $parsed_blocks ); + + $response = new WP_REST_Response( + [ + 'content' => [ + 'raw' => $block_content, + 'rendered' => preg_replace( '//s', '', $block_content ), + ], + ] + ); + + $filtered_response = $this->instance->filter_rest_prepare_post( clone $response ); + if ( $expected_prop_count > 0 ) { + $this->assertNotEquals( $response->data['content']['raw'], $filtered_response->data['content']['raw'] ); + } else { + $this->assertEquals( $response->data['content']['raw'], $filtered_response->data['content']['raw'] ); + } + $this->assertEquals( $response->data['content']['rendered'], $filtered_response->data['content']['rendered'] ); + + $present_count = 0; + foreach ( self::PROP_ATTRIBUTE_MAPPING as $prop => $attribute ) { + if ( isset( $parsed_block['attrs'][ $prop ] ) ) { + $this->assertStringContains( "$attribute=", $response->data['content']['raw'] ); + $this->assertStringNotContains( "$attribute=", $filtered_response->data['content']['raw'] ); + $present_count++; + } + } + + $this->assertEquals( $expected_prop_count, $present_count ); + } + + /** @covers ObsoleteBlockAttributeRemover::test_filter_rest_prepare_post() */ + public function test_filter_rest_prepare_post_rendered_only() { + $response = new WP_REST_Response( + [ + 'content' => [ + 'rendered' => '

Here is amp-fit-text!

', + ], + ] + ); + + $filtered_response = $this->instance->filter_rest_prepare_post( clone $response ); + $this->assertEquals( $response->data['content']['rendered'], $filtered_response->data['content']['rendered'] ); + } +}