wordpress-develop/tests/phpunit/tests/user/author.php
Sergey Biryukov 164b22cf6a Tests: First pass at using assertSame() instead of assertEquals() in most of the unit tests.
This ensures that not only the return values match the expected results, but also that their type is the same.

Going forward, stricter type checking by using `assertSame()` should generally be preferred to `assertEquals()` where appropriate, to make the tests more reliable.

Props johnbillion, jrf, SergeyBiryukov.
See #38266.

git-svn-id: https://develop.svn.wordpress.org/trunk@48937 602fd350-edb4-49c9-b593-d223f7449a82
2020-09-02 00:35:36 +00:00

162 lines
4.5 KiB
PHP

<?php
/**
* Test functions in wp-includes/author-template.php
*
* @group author
* @group user
*/
class Tests_User_Author_Template extends WP_UnitTestCase {
protected static $author_id = 0;
protected static $post_id = 0;
private $permalink_structure;
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$author_id = $factory->user->create(
array(
'role' => 'author',
'user_login' => 'test_author',
'description' => 'test_author',
)
);
self::$post_id = $factory->post->create(
array(
'post_author' => self::$author_id,
'post_status' => 'publish',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_type' => 'post',
)
);
}
function setUp() {
parent::setUp();
setup_postdata( get_post( self::$post_id ) );
}
function tearDown() {
wp_reset_postdata();
parent::tearDown();
}
function test_get_the_author() {
$author_name = get_the_author();
$user = new WP_User( self::$author_id );
$this->assertSame( $user->display_name, $author_name );
$this->assertSame( 'test_author', $author_name );
}
function test_get_the_author_meta() {
$this->assertSame( 'test_author', get_the_author_meta( 'login' ) );
$this->assertSame( 'test_author', get_the_author_meta( 'user_login' ) );
$this->assertSame( 'test_author', get_the_author_meta( 'display_name' ) );
$this->assertSame( 'test_author', trim( get_the_author_meta( 'description' ) ) );
$this->assertSame( 'test_author', get_the_author_meta( 'user_description' ) );
add_user_meta( self::$author_id, 'user_description', 'user description' );
$this->assertSame( 'user description', get_user_meta( self::$author_id, 'user_description', true ) );
// user_description in meta is ignored. The content of description is returned instead.
// See #20285.
$this->assertSame( 'test_author', get_the_author_meta( 'user_description' ) );
$this->assertSame( 'test_author', trim( get_the_author_meta( 'description' ) ) );
update_user_meta( self::$author_id, 'user_description', '' );
$this->assertSame( '', get_user_meta( self::$author_id, 'user_description', true ) );
$this->assertSame( 'test_author', get_the_author_meta( 'user_description' ) );
$this->assertSame( 'test_author', trim( get_the_author_meta( 'description' ) ) );
$this->assertSame( '', get_the_author_meta( 'does_not_exist' ) );
}
function test_get_the_author_meta_no_authordata() {
unset( $GLOBALS['authordata'] );
$this->assertSame( '', get_the_author_meta( 'id' ) );
$this->assertSame( '', get_the_author_meta( 'user_login' ) );
$this->assertSame( '', get_the_author_meta( 'does_not_exist' ) );
}
function test_get_the_author_posts() {
// Test with no global post, result should be 0 because no author is found.
$this->assertSame( 0, get_the_author_posts() );
$GLOBALS['post'] = self::$post_id;
$this->assertEquals( 1, get_the_author_posts() );
}
/**
* @ticket 30904
*/
function test_get_the_author_posts_with_custom_post_type() {
register_post_type( 'wptests_pt' );
$cpt_ids = self::factory()->post->create_many(
2,
array(
'post_author' => self::$author_id,
'post_type' => 'wptests_pt',
)
);
$GLOBALS['post'] = $cpt_ids[0];
$this->assertEquals( 2, get_the_author_posts() );
_unregister_post_type( 'wptests_pt' );
}
/**
* @ticket 30355
*/
public function test_get_the_author_posts_link_no_permalinks() {
$author = self::factory()->user->create_and_get(
array(
'display_name' => 'Foo',
'user_nicename' => 'bar',
)
);
$GLOBALS['authordata'] = $author->data;
$link = get_the_author_posts_link();
$url = sprintf( 'http://%1$s/?author=%2$s', WP_TESTS_DOMAIN, $author->ID );
$this->assertContains( $url, $link );
$this->assertContains( 'Posts by Foo', $link );
$this->assertContains( '>Foo</a>', $link );
unset( $GLOBALS['authordata'] );
}
/**
* @ticket 30355
*/
public function test_get_the_author_posts_link_with_permalinks() {
$this->set_permalink_structure( '/%postname%/' );
$author = self::factory()->user->create_and_get(
array(
'display_name' => 'Foo',
'user_nicename' => 'bar',
)
);
$GLOBALS['authordata'] = $author;
$link = get_the_author_posts_link();
$url = sprintf( 'http://%1$s/author/%2$s/', WP_TESTS_DOMAIN, $author->user_nicename );
$this->set_permalink_structure( '' );
$this->assertContains( $url, $link );
$this->assertContains( 'Posts by Foo', $link );
$this->assertContains( '>Foo</a>', $link );
unset( $GLOBALS['authordata'] );
}
}