wordpress-develop/tests/phpunit/tests/user/author.php
Drew Jaynes 0ea2acb720 Tests: Permalink Structures Phase II: DRY up logic for setting permalink structures in test methods.
Renames `reset_permalinks()` to `set_permalink_structure()` (mimicking `$wp_rewrite->set_permalink_structure()`) and allows it to accept an optional permalink structure. In this way, we can double dip using it to both set and reset the permalink structure from anywhere.

Removes alot of duplicated code from tests.

See #33968.


git-svn-id: https://develop.svn.wordpress.org/trunk@34810 602fd350-edb4-49c9-b593-d223f7449a82
2015-10-03 20:54:11 +00:00

151 lines
4.4 KiB
PHP

<?php
/**
* Test functions in wp-includes/author-template.php
*
* @group author
* @group user
*/
class Tests_User_Author_Template extends WP_UnitTestCase {
protected $author_id = 0;
protected $post_id = 0;
private $permalink_structure;
function setUp() {
parent::setUp();
$this->author_id = $this->factory->user->create( array(
'role' => 'author',
'user_login' => 'test_author',
'description' => 'test_author',
) );
$user = new WP_User( $this->author_id );
$post = array(
'post_author' => $this->author_id,
'post_status' => 'publish',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_type' => 'post'
);
// insert a post and make sure the ID is ok
$this->post_id = $this->factory->post->create( $post );
setup_postdata( get_post( $this->post_id ) );
}
function tearDown() {
wp_reset_postdata();
parent::tearDown();
}
function test_get_the_author() {
$author_name = get_the_author();
$user = new WP_User( $this->author_id );
$this->assertEquals( $user->display_name, $author_name );
$this->assertEquals( 'test_author', $author_name );
}
function test_get_the_author_meta() {
$this->assertEquals( 'test_author', get_the_author_meta( 'login' ) );
$this->assertEquals( 'test_author', get_the_author_meta( 'user_login' ) );
$this->assertEquals( 'test_author', get_the_author_meta( 'display_name' ) );
$this->assertEquals( 'test_author', get_the_author_meta( 'description' ) );
$this->assertEquals( 'test_author', get_the_author_meta( 'user_description' ) );
add_user_meta( $this->author_id, 'user_description', 'user description' );
$this->assertEquals( 'user description', get_user_meta( $this->author_id, 'user_description', true ) );
// user_description in meta is ignored. The content of description is returned instead.
// See #20285
$this->assertEquals( 'test_author', get_the_author_meta( 'user_description' ) );
$this->assertEquals( 'test_author', get_the_author_meta( 'description' ) );
update_user_meta( $this->author_id, 'user_description', '' );
$this->assertEquals( '', get_user_meta( $this->author_id, 'user_description', true ) );
$this->assertEquals( 'test_author', get_the_author_meta( 'user_description' ) );
$this->assertEquals( 'test_author', get_the_author_meta( 'description' ) );
$this->assertEquals( '', get_the_author_meta( 'does_not_exist' ) );
}
function test_get_the_author_meta_no_authordata() {
unset( $GLOBALS['authordata'] );
$this->assertEquals( '', get_the_author_meta( 'id' ) );
$this->assertEquals( '', get_the_author_meta( 'user_login' ) );
$this->assertEquals( '', 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->assertEquals( 0, get_the_author_posts() );
$GLOBALS['post'] = $this->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 = $this->factory->post->create_many( 2, array(
'post_author' => $this->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 = $this->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 = $this->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->assertContains( $url, $link );
$this->assertContains( 'Posts by Foo', $link );
$this->assertContains( '>Foo</a>', $link );
unset( $GLOBALS['authordata'] );
}
}