Move PHPUnit tests into a tests/phpunit directory.

wp-tests-config.php can/should reside in the root of a develop checkout. `phpunit` should be run from the root.

see #25088.


git-svn-id: https://develop.svn.wordpress.org/trunk@25165 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Nacin
2013-08-29 18:39:34 +00:00
parent d34baebc1d
commit 8045afd81b
318 changed files with 36 additions and 39 deletions
@@ -0,0 +1,38 @@
<?php
/**
* @group file
* @group admin
*/
class Tests_Admin_includesFile extends WP_UnitTestCase {
function setUp() {
parent::setUp();
}
/**
* @ticket 20449
*/
function test_get_home_path() {
$home = get_option( 'home' );
$siteurl = get_option( 'siteurl' );
$sfn = $_SERVER['SCRIPT_FILENAME'];
$this->assertEquals( str_replace( '\\', '/', ABSPATH ), get_home_path() );
update_option( 'home', 'http://localhost' );
update_option( 'siteurl', 'http://localhost/wp' );
$_SERVER['SCRIPT_FILENAME'] = 'D:\root\vhosts\site\httpdocs\wp\wp-admin\options-permalink.php';
$this->assertEquals( 'D:/root/vhosts/site/httpdocs/', get_home_path() );
$_SERVER['SCRIPT_FILENAME'] = '/Users/foo/public_html/trunk/wp/wp-admin/options-permalink.php';
$this->assertEquals( '/Users/foo/public_html/trunk/', get_home_path() );
$_SERVER['SCRIPT_FILENAME'] = 'S:/home/wordpress/trunk/wp/wp-admin/options-permalink.php';
$this->assertEquals( 'S:/home/wordpress/trunk/', get_home_path() );
update_option( 'home', $home );
update_option( 'siteurl', $siteurl );
$_SERVER['SCRIPT_FILENAME'] = $sfn;
}
}
@@ -0,0 +1,25 @@
<?php
/**
* @group admin
*/
class Tests_Admin_includesMisc extends WP_UnitTestCase {
function test_shorten_url() {
$tests = array(
'wordpress\.org/about/philosophy'
=> 'wordpress\.org/about/philosophy', // no longer strips slashes
'wordpress.org/about/philosophy'
=> 'wordpress.org/about/philosophy',
'http://wordpress.org/about/philosophy/'
=> 'wordpress.org/about/philosophy', // remove http, trailing slash
'http://www.wordpress.org/about/philosophy/'
=> 'wordpress.org/about/philosophy', // remove http, www
'http://wordpress.org/about/philosophy/#box'
=> 'wordpress.org/about/philosophy/#box', // don't shorten 35 characters
'http://wordpress.org/about/philosophy/#decisions'
=> 'wordpress.org/about/philosophy/#&hellip;', // shorten to 32 if > 35 after cleaning
);
foreach ( $tests as $k => $v )
$this->assertEquals( $v, url_shorten( $k ) );
}
}
@@ -0,0 +1,59 @@
<?php
/**
* @group plugins
* @group admin
*/
class Tests_Admin_includesPlugin extends WP_UnitTestCase {
function test_get_plugin_data() {
$data = get_plugin_data( DIR_TESTDATA . '/plugins/hello.php' );
$default_headers = array(
'Name' => 'Hello Dolly',
'Title' => '<a href="http://wordpress.org/#" title="Visit plugin homepage">Hello Dolly</a>',
'PluginURI' => 'http://wordpress.org/#',
'Description' => 'This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from Hello, Dolly in the upper right of your admin screen on every page. <cite>By <a href="http://ma.tt/" title="Visit author homepage">Matt Mullenweg</a>.</cite>',
'Author' => '<a href="http://ma.tt/" title="Visit author homepage">Matt Mullenweg</a>',
'AuthorURI' => 'http://ma.tt/',
'Version' => '1.5.1',
'TextDomain' => 'hello-dolly',
'DomainPath' => ''
);
$this->assertTrue( is_array($data) );
foreach ($default_headers as $name => $value) {
$this->assertTrue(isset($data[$name]));
$this->assertEquals($value, $data[$name]);
}
}
function test_menu_page_url() {
$current_user = get_current_user_id();
wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
update_option( 'siteurl', 'http://example.com' );
// add some pages
add_options_page( 'Test Settings', 'Test Settings', 'manage_options', 'testsettings', 'mt_settings_page' );
add_management_page( 'Test Tools', 'Test Tools', 'manage_options', 'testtools', 'mt_tools_page' );
add_menu_page( 'Test Toplevel', 'Test Toplevel', 'manage_options', 'mt-top-level-handle', 'mt_toplevel_page' );
add_submenu_page( 'mt-top-level-handle', 'Test Sublevel', 'Test Sublevel', 'manage_options', 'sub-page', 'mt_sublevel_page' );
add_submenu_page( 'mt-top-level-handle', 'Test Sublevel 2', 'Test Sublevel 2', 'manage_options', 'sub-page2', 'mt_sublevel_page2' );
add_theme_page( 'With Spaces', 'With Spaces', 'manage_options', 'With Spaces', 'mt_tools_page' );
add_pages_page( 'Appending Query Arg', 'Test Pages', 'edit_pages', 'testpages', 'mt_pages_page' );
$expected['testsettings'] = 'http://example.com/wp-admin/options-general.php?page=testsettings';
$expected['testtools'] = 'http://example.com/wp-admin/tools.php?page=testtools';
$expected['mt-top-level-handle'] = 'http://example.com/wp-admin/admin.php?page=mt-top-level-handle';
$expected['sub-page'] = 'http://example.com/wp-admin/admin.php?page=sub-page';
$expected['sub-page2'] = 'http://example.com/wp-admin/admin.php?page=sub-page2';
$expected['not_registered'] = '';
$expected['With Spaces'] = 'http://example.com/wp-admin/themes.php?page=WithSpaces';
$expected['testpages'] = 'http://example.com/wp-admin/edit.php?post_type=page&#038;page=testpages';
foreach ($expected as $name => $value) {
$this->assertEquals( $value, menu_page_url( $name, false ) );
}
wp_set_current_user( $current_user );
}
}
+117
View File
@@ -0,0 +1,117 @@
<?php
/**
* @group admin
*/
class Tests_Admin_includesPost extends WP_UnitTestCase {
function test__wp_translate_postdata_cap_checks_contributor() {
$contributor_id = $this->factory->user->create( array( 'role' => 'contributor' ) );
$editor_id = $this->factory->user->create( array( 'role' => 'editor' ) );
wp_set_current_user( $contributor_id );
// Create New Draft Post
$_post_data = array();
$_post_data['post_author'] = $contributor_id;
$_post_data['post_type'] = 'post';
$_post_data['saveasdraft'] = true;
$_results = _wp_translate_postdata( false, $_post_data );
$this->assertNotInstanceOf( 'WP_Error', $_results );
$this->assertEquals( $_post_data['post_author'], $_results['post_author'] );
$this->assertEquals( 'draft', $_results['post_status'] );
// Submit Post for Approval
$_post_data = array();
$_post_data['post_author'] = $contributor_id;
$_post_data['post_type'] = 'post';
$_post_data['publish'] = true;
$_results = _wp_translate_postdata( false, $_post_data );
$this->assertNotInstanceOf( 'WP_Error', $_results );
$this->assertEquals( $_post_data['post_author'], $_results['post_author'] );
$this->assertEquals( 'pending', $_results['post_status'] );
// Create New Draft Post for another user
$_post_data = array();
$_post_data['post_author'] = $editor_id;
$_post_data['post_type'] = 'post';
$_post_data['saveasdraft'] = true;
$_results = _wp_translate_postdata( false, $_post_data );
$this->assertInstanceOf( 'WP_Error', $_results );
$this->assertEquals( 'edit_others_posts', $_results->get_error_code() );
$this->assertEquals( 'You are not allowed to create posts as this user.', $_results->get_error_message() );
// Edit Draft Post for another user
$_post_data = array();
$_post_data['post_ID'] = $this->factory->post->create( array( 'post_author' => $editor_id ) );
$_post_data['post_author'] = $editor_id;
$_post_data['post_type'] = 'post';
$_post_data['post_status'] = 'draft';
$_post_data['saveasdraft'] = true;
$_results = _wp_translate_postdata( true, $_post_data );
$this->assertInstanceOf( 'WP_Error', $_results );
$this->assertEquals( 'edit_others_posts', $_results->get_error_code() );
$this->assertEquals( 'You are not allowed to edit posts as this user.', $_results->get_error_message() );
wp_set_current_user( 0 );
}
function test__wp_translate_postdata_cap_checks_editor() {
$contributor_id = $this->factory->user->create( array( 'role' => 'contributor' ) );
$editor_id = $this->factory->user->create( array( 'role' => 'editor' ) );
wp_set_current_user( $editor_id );
// Create New Draft Post
$_post_data = array();
$_post_data['post_author'] = $editor_id;
$_post_data['post_type'] = 'post';
$_post_data['saveasdraft'] = true;
$_results = _wp_translate_postdata( false, $_post_data );
$this->assertNotInstanceOf( 'WP_Error', $_results );
$this->assertEquals( $_post_data['post_author'], $_results['post_author'] );
$this->assertEquals( 'draft', $_results['post_status'] );
// Publish Post
$_post_data = array();
$_post_data['post_author'] = $editor_id;
$_post_data['post_type'] = 'post';
$_post_data['publish'] = true;
$_results = _wp_translate_postdata( false, $_post_data );
$this->assertNotInstanceOf( 'WP_Error', $_results );
$this->assertEquals( $_post_data['post_author'], $_results['post_author'] );
$this->assertEquals( 'publish', $_results['post_status'] );
// Create New Draft Post for another user
$_post_data = array();
$_post_data['post_author'] = $contributor_id;
$_post_data['post_type'] = 'post';
$_post_data['saveasdraft'] = true;
$_results = _wp_translate_postdata( false, $_post_data );
$this->assertNotInstanceOf( 'WP_Error', $_results );
$this->assertEquals( $_post_data['post_author'], $_results['post_author'] );
$this->assertEquals( 'draft', $_results['post_status'] );
// Edit Draft Post for another user
$_post_data = array();
$_post_data['post_ID'] = $this->factory->post->create( array( 'post_author' => $contributor_id ) );
$_post_data['post_author'] = $contributor_id;
$_post_data['post_type'] = 'post';
$_post_data['post_status'] = 'draft';
$_post_data['saveasdraft'] = true;
$_results = _wp_translate_postdata( true, $_post_data );
$this->assertNotInstanceOf( 'WP_Error', $_results );
$this->assertEquals( $_post_data['post_author'], $_results['post_author'] );
$this->assertEquals( 'draft', $_results['post_status'] );
wp_set_current_user( 0 );
}
}
@@ -0,0 +1,207 @@
<?php
/**
* @group admin
*/
class Tests_Admin_includesScreen extends WP_UnitTestCase {
var $core_screens = array(
'index.php' => array( 'base' => 'dashboard', 'id' => 'dashboard' ),
'edit.php' => array( 'base' => 'edit', 'id' => 'edit-post', 'post_type' => 'post' ),
'post-new.php'=> array( 'action' => 'add', 'base' => 'post', 'id' => 'post', 'post_type' => 'post' ),
'edit-tags.php' => array( 'base' => 'edit-tags', 'id' => 'edit-post_tag', 'post_type' => 'post', 'taxonomy' => 'post_tag' ),
'edit-tags.php?taxonomy=post_tag' => array( 'base' => 'edit-tags', 'id' => 'edit-post_tag', 'post_type' => 'post', 'taxonomy' => 'post_tag' ),
'edit-tags.php?taxonomy=category' => array( 'base' => 'edit-tags', 'id' => 'edit-category', 'post_type' => 'post', 'taxonomy' => 'category' ),
'upload.php' => array( 'base' => 'upload', 'id' => 'upload' ),
'media-new.php' => array( 'action' => 'add', 'base' => 'media', 'id' => 'media' ),
'edit.php?post_type=page' => array( 'base' => 'edit', 'id' => 'edit-page', 'post_type' => 'page' ),
'link-manager.php' => array( 'base' => 'link-manager', 'id' => 'link-manager' ),
'link-add.php' => array( 'action' => 'add', 'base' => 'link', 'id' => 'link' ),
'edit-tags.php?taxonomy=link_category' => array( 'base' => 'edit-tags', 'id' => 'edit-link_category', 'taxonomy' => 'link_category', 'post_type' => '' ),
'edit-comments.php' => array( 'base' => 'edit-comments', 'id' => 'edit-comments' ),
'themes.php' => array( 'base' => 'themes', 'id' => 'themes' ),
'widgets.php' => array( 'base' => 'widgets', 'id' => 'widgets' ),
'nav-menus.php' => array( 'base' => 'nav-menus', 'id' => 'nav-menus' ),
'plugins.php' => array( 'base' => 'plugins', 'id' => 'plugins' ),
'users.php' => array( 'base' => 'users', 'id' => 'users' ),
'user-new.php' => array( 'action' => 'add', 'base' => 'user', 'id' => 'user' ),
'profile.php' => array( 'base' => 'profile', 'id' => 'profile' ),
'tools.php' => array( 'base' => 'tools', 'id' => 'tools' ),
'import.php' => array( 'base' => 'import', 'id' => 'import' ),
'export.php' => array( 'base' => 'export', 'id' => 'export' ),
'options-general.php' => array( 'base' => 'options-general', 'id' => 'options-general' ),
'options-writing.php' => array( 'base' => 'options-writing', 'id' => 'options-writing' ),
);
function setUp() {
set_current_screen( 'front' );
}
function tearDown() {
parent::tearDown();
unset( $GLOBALS['wp_taxonomies']['old-or-new'] );
set_current_screen( 'front' );
}
function test_set_current_screen_with_hook_suffix() {
global $current_screen;
foreach ( $this->core_screens as $hook_name => $screen ) {
$_GET = $_POST = $_REQUEST = array();
$GLOBALS['taxnow'] = $GLOBALS['typenow'] = '';
$screen = (object) $screen;
$hook = parse_url( $hook_name );
if ( ! empty( $hook['query'] ) ) {
$args = wp_parse_args( $hook['query'] );
if ( isset( $args['taxonomy'] ) )
$GLOBALS['taxnow'] = $_GET['taxonomy'] = $_POST['taxonomy'] = $_REQUEST['taxonomy'] = $args['taxonomy'];
if ( isset( $args['post_type'] ) )
$GLOBALS['typenow'] = $_GET['post_type'] = $_POST['post_type'] = $_REQUEST['post_type'] = $args['post_type'];
else if ( isset( $screen->post_type ) )
$GLOBALS['typenow'] = $_GET['post_type'] = $_POST['post_type'] = $_REQUEST['post_type'] = $screen->post_type;
}
$GLOBALS['hook_suffix'] = $hook['path'];
set_current_screen();
$this->assertEquals( $screen->id, $current_screen->id, $hook_name );
$this->assertEquals( $screen->base, $current_screen->base, $hook_name );
if ( isset( $screen->action ) )
$this->assertEquals( $screen->action, $current_screen->action, $hook_name );
if ( isset( $screen->post_type ) )
$this->assertEquals( $screen->post_type, $current_screen->post_type, $hook_name );
else
$this->assertEmpty( $current_screen->post_type, $hook_name );
if ( isset( $screen->taxonomy ) )
$this->assertEquals( $screen->taxonomy, $current_screen->taxonomy, $hook_name );
$this->assertTrue( $current_screen->in_admin() );
$this->assertTrue( $current_screen->in_admin( 'site' ) );
$this->assertFalse( $current_screen->in_admin( 'network' ) );
$this->assertFalse( $current_screen->in_admin( 'user' ) );
$this->assertFalse( $current_screen->in_admin( 'garbage' ) );
// With convert_to_screen(), the same ID should return the exact $current_screen.
$this->assertSame( $current_screen, convert_to_screen( $screen->id ), $hook_name );
// With convert_to_screen(), the hook_suffix should return the exact $current_screen.
// But, convert_to_screen() cannot figure out ?taxonomy and ?post_type.
if ( empty( $hook['query'] ) )
$this->assertSame( $current_screen, convert_to_screen( $GLOBALS['hook_suffix'] ), $hook_name );
}
}
function test_post_type_as_hookname() {
$screen = convert_to_screen( 'page' );
$this->assertEquals( $screen->post_type, 'page' );
$this->assertEquals( $screen->base, 'post' );
$this->assertEquals( $screen->id, 'page' );
}
function test_post_type_with_special_suffix_as_hookname() {
register_post_type( 'value-add' );
$screen = convert_to_screen( 'value-add' ); // the -add part is key.
$this->assertEquals( $screen->post_type, 'value-add' );
$this->assertEquals( $screen->base, 'post' );
$this->assertEquals( $screen->id, 'value-add' );
$screen = convert_to_screen( 'edit-value-add' ); // the -add part is key.
$this->assertEquals( $screen->post_type, 'value-add' );
$this->assertEquals( $screen->base, 'edit' );
$this->assertEquals( $screen->id, 'edit-value-add' );
}
function test_taxonomy_with_special_suffix_as_hookname() {
register_taxonomy( 'old-or-new', 'post' );
$screen = convert_to_screen( 'edit-old-or-new' ); // the -new part is key.
$this->assertEquals( $screen->taxonomy, 'old-or-new' );
$this->assertEquals( $screen->base, 'edit-tags' );
$this->assertEquals( $screen->id, 'edit-old-or-new' );
}
function test_post_type_with_edit_prefix() {
register_post_type( 'edit-some-thing' );
$screen = convert_to_screen( 'edit-some-thing' );
$this->assertEquals( $screen->post_type, 'edit-some-thing' );
$this->assertEquals( $screen->base, 'post' );
$this->assertEquals( $screen->id, 'edit-some-thing' );
$screen = convert_to_screen( 'edit-edit-some-thing' );
$this->assertEquals( $screen->post_type, 'edit-some-thing' );
$this->assertEquals( $screen->base, 'edit' );
$this->assertEquals( $screen->id, 'edit-edit-some-thing' );
}
function test_post_type_edit_collisions() {
register_post_type( 'comments' );
register_post_type( 'tags' );
// Sorry, core wins here.
$screen = convert_to_screen( 'edit-comments' );
$this->assertEquals( $screen->base, 'edit-comments' );
// The post type wins here. convert_to_screen( $post_type ) is only relevant for meta boxes anyway.
$screen = convert_to_screen( 'comments' );
$this->assertEquals( $screen->base, 'post' );
// Core wins.
$screen = convert_to_screen( 'edit-tags' );
$this->assertEquals( $screen->base, 'edit-tags' );
$screen = convert_to_screen( 'tags' );
$this->assertEquals( $screen->base, 'post' );
}
function test_help_tabs() {
$tab = rand_str();
$tab_args = array(
'id' => $tab,
'title' => 'Help!',
'content' => 'Some content',
'callback' => false,
);
$screen = get_current_screen();
$screen->add_help_tab( $tab_args );
$this->assertEquals( $screen->get_help_tab( $tab ), $tab_args );
$tabs = $screen->get_help_tabs();
$this->assertArrayHasKey( $tab, $tabs );
$screen->remove_help_tab( $tab );
$this->assertNull( $screen->get_help_tab( $tab ) );
$screen->remove_help_tabs();
$this->assertEquals( $screen->get_help_tabs(), array() );
}
function test_in_admin() {
$screen = get_current_screen();
set_current_screen( 'edit.php' );
$this->assertTrue( get_current_screen()->in_admin() );
$this->assertTrue( get_current_screen()->in_admin( 'site' ) );
$this->assertFalse( get_current_screen()->in_admin( 'network' ) );
$this->assertFalse( get_current_screen()->in_admin( 'user' ) );
set_current_screen( 'dashboard-network' );
$this->assertTrue( get_current_screen()->in_admin() );
$this->assertFalse( get_current_screen()->in_admin( 'site' ) );
$this->assertTrue( get_current_screen()->in_admin( 'network' ) );
$this->assertFalse( get_current_screen()->in_admin( 'user' ) );
set_current_screen( 'dashboard-user' );
$this->assertTrue( get_current_screen()->in_admin() );
$this->assertFalse( get_current_screen()->in_admin( 'site' ) );
$this->assertFalse( get_current_screen()->in_admin( 'network' ) );
$this->assertTrue( get_current_screen()->in_admin( 'user' ) );
set_current_screen( 'front' );
$this->assertFalse( get_current_screen()->in_admin() );
$this->assertFalse( get_current_screen()->in_admin( 'site' ) );
$this->assertFalse( get_current_screen()->in_admin( 'network' ) );
$this->assertFalse( get_current_screen()->in_admin( 'user' ) );
$GLOBALS['current_screen'] = $screen;
}
}
@@ -0,0 +1,48 @@
<?php
/**
* @group admin
*/
class Tests_Admin_includesTemplate extends WP_UnitTestCase {
function test_equal() {
$this->assertEquals(' selected=\'selected\'', selected('foo','foo',false));
$this->assertEquals(' checked=\'checked\'', checked('foo','foo',false));
$this->assertEquals(' selected=\'selected\'', selected('1',1,false));
$this->assertEquals(' checked=\'checked\'', checked('1',1,false));
$this->assertEquals(' selected=\'selected\'', selected('1',true,false));
$this->assertEquals(' checked=\'checked\'', checked('1',true,false));
$this->assertEquals(' selected=\'selected\'', selected(1,1,false));
$this->assertEquals(' checked=\'checked\'', checked(1,1,false));
$this->assertEquals(' selected=\'selected\'', selected(1,true,false));
$this->assertEquals(' checked=\'checked\'', checked(1,true,false));
$this->assertEquals(' selected=\'selected\'', selected(true,true,false));
$this->assertEquals(' checked=\'checked\'', checked(true,true,false));
$this->assertEquals(' selected=\'selected\'', selected('0',0,false));
$this->assertEquals(' checked=\'checked\'', checked('0',0,false));
$this->assertEquals(' selected=\'selected\'', selected(0,0,false));
$this->assertEquals(' checked=\'checked\'', checked(0,0,false));
$this->assertEquals(' selected=\'selected\'', selected('',false,false));
$this->assertEquals(' checked=\'checked\'', checked('',false,false));
$this->assertEquals(' selected=\'selected\'', selected(false,false,false));
$this->assertEquals(' checked=\'checked\'', checked(false,false,false));
}
function test_notequal() {
$this->assertEquals('', selected('0','',false));
$this->assertEquals('', checked('0','',false));
$this->assertEquals('', selected(0,'',false));
$this->assertEquals('', checked(0,'',false));
$this->assertEquals('', selected(0,false,false));
$this->assertEquals('', checked(0,false,false));
}
}
@@ -0,0 +1,54 @@
<?php
/**
* @group themes
*/
class Tests_Admin_includesTheme extends WP_UnitTestCase {
function setUp() {
parent::setUp();
$this->theme_root = DIR_TESTDATA . '/themedir1';
$this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
$GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root );
add_filter('theme_root', array(&$this, '_theme_root'));
add_filter( 'stylesheet_root', array(&$this, '_theme_root') );
add_filter( 'template_root', array(&$this, '_theme_root') );
// clear caches
wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );
}
function tearDown() {
$GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
remove_filter('theme_root', array(&$this, '_theme_root'));
remove_filter( 'stylesheet_root', array(&$this, '_theme_root') );
remove_filter( 'template_root', array(&$this, '_theme_root') );
wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );
parent::tearDown();
}
// replace the normal theme root dir with our premade test dir
function _theme_root($dir) {
return $this->theme_root;
}
/**
* @ticket 10959
* @ticket 11216
*/
function test_page_templates() {
$theme = get_theme('Page Template Theme');
$this->assertFalse( empty($theme) );
switch_theme($theme['Template'], $theme['Stylesheet']);
$templates = get_page_templates();
$this->assertEquals(3, count($templates));
$this->assertEquals("template-top-level.php", $templates['Top Level']);
$this->assertEquals("subdir/template-sub-dir.php", $templates['Sub Dir']);
$this->assertEquals("template-header.php", $templates['This Template Header Is On One Line']);
}
}