diff --git a/tests/phpunit/tests/blocks/renderPattern.php b/tests/phpunit/tests/blocks/renderPattern.php new file mode 100644 index 0000000000000..64873827d104f --- /dev/null +++ b/tests/phpunit/tests/blocks/renderPattern.php @@ -0,0 +1,197 @@ +registered as $slug ) { + unregister_block_pattern( $slug ); + } + + $this->registered = array(); + + parent::tear_down(); + } + + /** + * Registers a pattern for the test. + * + * @param string $slug The slug of the pattern. + * @param string $content The content of the pattern. + */ + private function register_pattern( $slug, $content ) { + register_block_pattern( + $slug, + array( + 'title' => 'Test pattern', + 'content' => $content, + ) + ); + + $this->registered[] = $slug; + } + + /** + * Tests the content of a pattern. + * + * @ticket 66579 + */ + public function test_should_render_the_blocks_of_the_pattern() { + $this->register_pattern( 'test/render-pattern', '
Pattern text
' ); + + $this->assertSame( 'Pattern text
', render_block_core_pattern( array( 'slug' => 'test/render-pattern' ) ) ); + } + + /** + * Tests the pattern through the rendering of the block. + * + * @ticket 66579 + */ + public function test_should_render_the_pattern_with_the_block() { + $this->register_pattern( 'test/render-pattern', 'Pattern text
' ); + + $this->assertSame( 'Pattern text
', do_blocks( '' ) ); + } + + /** + * Tests a pattern inside a pattern. + * + * @ticket 66579 + */ + public function test_should_render_a_pattern_inside_another_pattern() { + $this->register_pattern( 'test/inner', 'Inner
' ); + $this->register_pattern( 'test/outer', 'Outer
' ); + + $this->assertSame( 'Inner
Outer
', render_block_core_pattern( array( 'slug' => 'test/outer' ) ) ); + } + + /** + * Tests that a slug that is not valid gives an empty string. + * + * @ticket 66579 + * + * @dataProvider data_slugs_that_are_not_valid + * + * @param array $attributes The attributes of the block. + */ + public function test_should_return_an_empty_string_for_a_slug_that_is_not_valid( $attributes ) { + $this->register_pattern( '0', 'Zero
' ); + + $this->assertSame( '', render_block_core_pattern( $attributes ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_slugs_that_are_not_valid() { + return array( + 'no slug' => array( array() ), + 'null' => array( array( 'slug' => null ) ), + 'empty' => array( array( 'slug' => '' ) ), + 'zero string' => array( array( 'slug' => '0' ) ), + 'an array' => array( array( 'slug' => array( 'test/render-pattern' ) ) ), + 'a number' => array( array( 'slug' => 5 ) ), + ); + } + + /** + * Tests that a pattern that is not registered gives an empty string. + * + * @ticket 66579 + */ + public function test_should_return_an_empty_string_for_a_pattern_that_is_not_registered() { + $this->assertSame( '', render_block_core_pattern( array( 'slug' => 'test/not-registered' ) ) ); + } + + /** + * Tests that a pattern that includes itself is not rendered again. + * + * @ticket 66579 + */ + public function test_should_stop_a_pattern_that_includes_itself() { + $this->register_pattern( 'test/self', 'Self
' ); + + $halted = WP_DEBUG && WP_DEBUG_DISPLAY ? '[block rendering halted for pattern "test/self"]' : ''; + $expected = 'Self
' . $halted; + + $this->assertSame( $expected, render_block_core_pattern( array( 'slug' => 'test/self' ) ) ); + } + + /** + * Tests that a pattern can be rendered again after it was stopped. + * + * @ticket 66579 + */ + public function test_should_render_a_pattern_again_after_it_was_stopped() { + $this->register_pattern( 'test/self', 'Self
' ); + + $first = render_block_core_pattern( array( 'slug' => 'test/self' ) ); + $second = render_block_core_pattern( array( 'slug' => 'test/self' ) ); + + $this->assertSame( $first, $second ); + $this->assertStringStartsWith( 'Self
', $second ); + } + + /** + * Tests that a pattern is rendered twice when it is repeated in a row and does not include itself. + * + * @ticket 66579 + */ + public function test_should_render_the_same_pattern_twice_in_a_row() { + $this->register_pattern( 'test/twice', 'Twice
' ); + + $this->assertSame( + 'Twice
Twice
', + do_blocks( '' ) + ); + } + + /** + * Tests that the block is registered with the render callback. + * + * @ticket 66579 + * + * @covers ::register_block_core_pattern + */ + public function test_should_register_the_block_with_the_render_callback() { + $registry = WP_Block_Type_Registry::get_instance(); + $original_block = $registry->get_registered( 'core/pattern' ); + + if ( $original_block ) { + unregister_block_type( 'core/pattern' ); + } + + try { + register_block_core_pattern(); + $block_type = $registry->get_registered( 'core/pattern' ); + + $this->assertInstanceOf( WP_Block_Type::class, $block_type ); + $this->assertSame( 'render_block_core_pattern', $block_type->render_callback ); + } finally { + if ( $registry->is_registered( 'core/pattern' ) ) { + unregister_block_type( 'core/pattern' ); + } + + if ( $original_block ) { + $registry->register( $original_block ); + } + } + } +}