| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * @group comment |
| | 5 | * |
| | 6 | * @covers ::get_comment_author |
| | 7 | */ |
| | 8 | class Tests_Comment_GetCommentAuthor extends WP_UnitTestCase { |
| | 9 | protected static $comments = array(); |
| | 10 | |
| | 11 | public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| | 12 | unset( $GLOBALS['comment'] ); |
| | 13 | |
| | 14 | $comment_ids = $factory->comment->create_post_comments( 0, 1 ); |
| | 15 | $user_generated_comment_ids = $factory->comment->create_post_comments( 0, 1, array( |
| | 16 | 'user_id' => $factory->user->create(), |
| | 17 | 'comment_author' => '', |
| | 18 | ) ); |
| | 19 | self::$comments = array_map( |
| | 20 | 'get_comment', |
| | 21 | array_merge( |
| | 22 | $comment_ids, |
| | 23 | $user_generated_comment_ids |
| | 24 | ) |
| | 25 | ); |
| | 26 | } |
| | 27 | |
| | 28 | public function test_no_comment() { |
| | 29 | $author = get_comment_author(); |
| | 30 | $this->assertSame( 'Anonymous', $author ); |
| | 31 | } |
| | 32 | |
| | 33 | public function test_invalid_comment() { |
| | 34 | $comment = end( self::$comments ); |
| | 35 | $invalid_comment_id = $comment->comment_ID + 1; |
| | 36 | $author = get_comment_author( $invalid_comment_id ); |
| | 37 | $this->assertSame( 'Anonymous', $author ); |
| | 38 | } |
| | 39 | |
| | 40 | public function test_global_comment() { |
| | 41 | $comment = reset( self::$comments ); |
| | 42 | $GLOBALS['comment'] = $comment; |
| | 43 | $author = get_comment_author(); |
| | 44 | $this->assertSame( $comment->comment_author, $author ); |
| | 45 | unset( $GLOBALS['comment'] ); |
| | 46 | } |
| | 47 | |
| | 48 | public function test_comment_arg() { |
| | 49 | $comment = reset( self::$comments ); |
| | 50 | $author = get_comment_author( $comment ); |
| | 51 | $this->assertSame( $comment->comment_author, $author ); |
| | 52 | } |
| | 53 | |
| | 54 | public function test_comment_by_registered_user_with_empty_comment_author_name() { |
| | 55 | $comment = end( self::$comments ); |
| | 56 | $user = get_userdata( $comment->user_id ); |
| | 57 | $author = get_comment_author( $comment ); |
| | 58 | $this->assertSame( $user->display_name, $author ); |
| | 59 | } |
| | 60 | |
| | 61 | } |