diff --git a/tests/phpunit/tests/blocks/renderPostAuthorName.php b/tests/phpunit/tests/blocks/renderPostAuthorName.php new file mode 100644 index 0000000000000..53ff2b3c94531 --- /dev/null +++ b/tests/phpunit/tests/blocks/renderPostAuthorName.php @@ -0,0 +1,295 @@ +user->create( + array( + 'role' => 'author', + 'display_name' => 'Jane Doe', + ) + ); + self::$post_id = $factory->post->create( array( 'post_author' => self::$author_id ) ); + } + + public function set_up() { + parent::set_up(); + + $this->original_block_to_render = WP_Block_Supports::$block_to_render; + WP_Block_Supports::$block_to_render = null; + } + + public function tear_down() { + WP_Block_Supports::$block_to_render = $this->original_block_to_render; + + parent::tear_down(); + } + + /** + * Renders the block. + * + * @param array $attributes Attributes of the block. + * @param array $context Context of the block, with the post by default. + * @return string The rendered block. + */ + private function render( $attributes = array(), $context = null ) { + $block = new WP_Block( + array( + 'blockName' => 'core/post-author-name', + 'attrs' => $attributes, + 'innerBlocks' => array(), + 'innerHTML' => '', + 'innerContent' => array(), + ) + ); + + $block->context = null === $context ? array( 'postId' => self::$post_id ) : $context; + + return render_block_core_post_author_name( $attributes, '', $block ); + } + + /** + * Tests the name of the author of the post of the context. + * + * @ticket 66540 + */ + public function test_should_render_the_name_of_the_author_of_the_post() { + $this->assertSame( '
Jane Doe
', $this->render() ); + } + + /** + * Tests that the author of the archive is used without a post. + * + * @ticket 66540 + */ + public function test_should_use_the_author_of_the_archive_without_a_post() { + $this->go_to( get_author_posts_url( self::$author_id ) ); + + $this->assertSame( '
Jane Doe
', $this->render( array(), array() ) ); + } + + /** + * Tests that nothing is rendered without an author. + * + * @ticket 66540 + */ + public function test_should_render_nothing_without_an_author() { + $this->go_to( home_url( '/' ) ); + + $this->assertSame( '', $this->render( array(), array() ), 'There should be no author without a post or an archive.' ); + $this->assertSame( '', $this->render( array(), array( 'postId' => 999999 ) ), 'There should be no author for a post that does not exist.' ); + } + + /** + * Tests that nothing is rendered for a post type that does not support authors. + * + * @ticket 66540 + */ + public function test_should_render_nothing_for_a_post_type_that_does_not_support_authors() { + register_post_type( 'wp_test_no_author', array( 'supports' => array( 'title' ) ) ); + + try { + $this->assertSame( + '', + $this->render( + array(), + array( + 'postId' => self::$post_id, + 'postType' => 'wp_test_no_author', + ) + ) + ); + $this->assertSame( + '
Jane Doe
', + $this->render( + array(), + array( + 'postId' => self::$post_id, + 'postType' => 'post', + ) + ), + 'A post type that supports authors should be rendered.' + ); + } finally { + unregister_post_type( 'wp_test_no_author' ); + } + } + + /** + * Tests the link to the posts of the author. + * + * @ticket 66540 + */ + public function test_should_link_the_name_to_the_posts_of_the_author() { + $this->assertSame( + '
Jane Doe
', + $this->render( + array( + 'isLink' => true, + 'linkTarget' => '_blank', + ) + ) + ); + } + + /** + * Tests that the target of the link is escaped. + * + * @ticket 66540 + */ + public function test_should_escape_the_target_of_the_link() { + $this->assertStringContainsString( + 'target=""><script>"', + $this->render( + array( + 'isLink' => true, + 'linkTarget' => '">