|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the_author_posts(). |
| 5 | + * |
| 6 | + * @group author |
| 7 | + * @group user |
| 8 | + * |
| 9 | + * @covers ::the_author_posts |
| 10 | + */ |
| 11 | +class Tests_User_TheAuthorPosts extends WP_UnitTestCase { |
| 12 | + |
| 13 | + /** |
| 14 | + * Author user ID. |
| 15 | + * |
| 16 | + * @var int |
| 17 | + */ |
| 18 | + private static $author_id = 0; |
| 19 | + |
| 20 | + /** |
| 21 | + * Post ID authored by self::$author_id. |
| 22 | + * |
| 23 | + * @var int |
| 24 | + */ |
| 25 | + private static $post_id = 0; |
| 26 | + |
| 27 | + /** |
| 28 | + * Creates shared author and post fixtures. |
| 29 | + * |
| 30 | + * @param WP_UnitTest_Factory $factory Test factory. |
| 31 | + */ |
| 32 | + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| 33 | + self::$author_id = $factory->user->create( |
| 34 | + array( |
| 35 | + 'role' => 'author', |
| 36 | + 'user_login' => 'test_author', |
| 37 | + 'display_name' => 'Test Author', |
| 38 | + ) |
| 39 | + ); |
| 40 | + |
| 41 | + self::$post_id = $factory->post->create( |
| 42 | + array( |
| 43 | + 'post_author' => self::$author_id, |
| 44 | + 'post_status' => 'publish', |
| 45 | + 'post_title' => 'First Author Post', |
| 46 | + ) |
| 47 | + ); |
| 48 | + |
| 49 | + $factory->post->create( |
| 50 | + array( |
| 51 | + 'post_author' => self::$author_id, |
| 52 | + 'post_status' => 'publish', |
| 53 | + 'post_title' => 'Second Author Post', |
| 54 | + ) |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Sets up the global post data before each test. |
| 60 | + */ |
| 61 | + public function set_up() { |
| 62 | + parent::set_up(); |
| 63 | + |
| 64 | + $GLOBALS['post'] = get_post( self::$post_id ); |
| 65 | + setup_postdata( $GLOBALS['post'] ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Cleans up global state after each test. |
| 70 | + */ |
| 71 | + public function tear_down() { |
| 72 | + unset( $GLOBALS['post'], $GLOBALS['authordata'] ); |
| 73 | + |
| 74 | + parent::tear_down(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Tests that the_author_posts() echoes the current post author's post count. |
| 79 | + * |
| 80 | + * @ticket 65819 |
| 81 | + */ |
| 82 | + public function test_echoes_author_post_count() { |
| 83 | + $output = get_echo( 'the_author_posts' ); |
| 84 | + |
| 85 | + $this->assertSame( '2', $output ); |
| 86 | + $this->assertSame( (string) get_the_author_posts(), $output ); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Tests that the_author_posts() echoes 0 when no global post is available. |
| 91 | + * |
| 92 | + * @ticket 65819 |
| 93 | + */ |
| 94 | + public function test_echoes_zero_when_no_global_post_available() { |
| 95 | + unset( $GLOBALS['post'] ); |
| 96 | + |
| 97 | + $this->assertSame( '0', get_echo( 'the_author_posts' ) ); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Tests that the_author_posts() echoes the post count for custom post types. |
| 102 | + * |
| 103 | + * @ticket 65819 |
| 104 | + */ |
| 105 | + public function test_echoes_author_post_count_for_custom_post_type() { |
| 106 | + register_post_type( 'wptests_cpt' ); |
| 107 | + |
| 108 | + $cpt_post_id = self::factory()->post->create( |
| 109 | + array( |
| 110 | + 'post_author' => self::$author_id, |
| 111 | + 'post_status' => 'publish', |
| 112 | + 'post_type' => 'wptests_cpt', |
| 113 | + ) |
| 114 | + ); |
| 115 | + |
| 116 | + self::factory()->post->create_many( |
| 117 | + 2, |
| 118 | + array( |
| 119 | + 'post_author' => self::$author_id, |
| 120 | + 'post_status' => 'publish', |
| 121 | + 'post_type' => 'wptests_cpt', |
| 122 | + ) |
| 123 | + ); |
| 124 | + |
| 125 | + $GLOBALS['post'] = get_post( $cpt_post_id ); |
| 126 | + setup_postdata( $GLOBALS['post'] ); |
| 127 | + |
| 128 | + $output = get_echo( 'the_author_posts' ); |
| 129 | + |
| 130 | + $this->assertSame( '3', $output ); |
| 131 | + $this->assertSame( (string) get_the_author_posts(), $output ); |
| 132 | + } |
| 133 | +} |
0 commit comments