mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-12 12:50:28 +00:00
Unit Tests: one $factory to rule them all, and it shall be static.
Using more than one instance of `WP_UnitTest_Factory` causes all kinds of craziness, due to out-of-sync internal generator sequences. Since we want to use `setUpBeforeClass`, we were creating ad hoc instances. To avoid that, we were injecting one `static` instance via Dependency Injection in `wpSetUpBeforeClass`. All tests should really use the `static` instance, so we will remove the instance prop `$factory`. Replace `$this->factory` with `self::$factory` over 2000 times. Rewrite all of the tests that were hard-coding dynamic values. #YOLOFriday git-svn-id: https://develop.svn.wordpress.org/trunk@35225 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -251,10 +251,10 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
|
||||
* @see WP_Customize_Manager::set_return_url()
|
||||
*/
|
||||
function test_return_url() {
|
||||
wp_set_current_user( $this->factory->user->create( array( 'role' => 'author' ) ) );
|
||||
wp_set_current_user( self::$factory->user->create( array( 'role' => 'author' ) ) );
|
||||
$this->assertEquals( get_admin_url(), $this->manager->get_return_url() );
|
||||
|
||||
wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
wp_set_current_user( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
$this->assertTrue( current_user_can( 'edit_theme_options' ) );
|
||||
$this->assertEquals( admin_url( 'themes.php' ), $this->manager->get_return_url() );
|
||||
|
||||
@@ -301,7 +301,7 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
|
||||
* @see WP_Customize_Manager::customize_pane_settings()
|
||||
*/
|
||||
function test_customize_pane_settings() {
|
||||
wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
wp_set_current_user( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
$this->manager->register_controls();
|
||||
$this->manager->prepare_controls();
|
||||
$autofocus = array( 'control' => 'blogname' );
|
||||
|
||||
@@ -21,7 +21,7 @@ class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase {
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
|
||||
wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
wp_set_current_user( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
|
||||
global $wp_customize;
|
||||
$this->wp_customize = new WP_Customize_Manager();
|
||||
@@ -149,7 +149,7 @@ class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase {
|
||||
function test_value_type_post_type() {
|
||||
do_action( 'customize_register', $this->wp_customize );
|
||||
|
||||
$post_id = $this->factory->post->create( array( 'post_title' => 'Hello World' ) );
|
||||
$post_id = self::$factory->post->create( array( 'post_title' => 'Hello World' ) );
|
||||
|
||||
$menu_id = wp_create_nav_menu( 'Menu' );
|
||||
$item_title = 'Greetings';
|
||||
@@ -192,7 +192,7 @@ class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase {
|
||||
function test_value_type_taxonomy() {
|
||||
do_action( 'customize_register', $this->wp_customize );
|
||||
|
||||
$tax_id = $this->factory->category->create( array( 'name' => 'Salutations' ) );
|
||||
$tax_id = self::$factory->category->create( array( 'name' => 'Salutations' ) );
|
||||
|
||||
$menu_id = wp_create_nav_menu( 'Menu' );
|
||||
$item_title = 'Greetings';
|
||||
@@ -270,7 +270,7 @@ class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase {
|
||||
$value = $menu->value();
|
||||
$this->assertEquals( $post_value, $value );
|
||||
|
||||
$post_id = $this->factory->post->create( array( 'post_title' => 'Hello World' ) );
|
||||
$post_id = self::$factory->post->create( array( 'post_title' => 'Hello World' ) );
|
||||
$item_id = wp_update_nav_menu_item( $menu_id, 0, array(
|
||||
'menu-item-type' => 'post_type',
|
||||
'menu-item-object' => 'post',
|
||||
@@ -296,8 +296,8 @@ class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase {
|
||||
function test_preview_updated() {
|
||||
do_action( 'customize_register', $this->wp_customize );
|
||||
|
||||
$first_post_id = $this->factory->post->create( array( 'post_title' => 'Hello World' ) );
|
||||
$second_post_id = $this->factory->post->create( array( 'post_title' => 'Hola Muno' ) );
|
||||
$first_post_id = self::$factory->post->create( array( 'post_title' => 'Hello World' ) );
|
||||
$second_post_id = self::$factory->post->create( array( 'post_title' => 'Hola Muno' ) );
|
||||
|
||||
$primary_menu_id = wp_create_nav_menu( 'Primary' );
|
||||
$secondary_menu_id = wp_create_nav_menu( 'Secondary' );
|
||||
@@ -348,7 +348,7 @@ class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase {
|
||||
do_action( 'customize_register', $this->wp_customize );
|
||||
|
||||
$menu_id = wp_create_nav_menu( 'Primary' );
|
||||
$post_id = $this->factory->post->create( array( 'post_title' => 'Hello World' ) );
|
||||
$post_id = self::$factory->post->create( array( 'post_title' => 'Hello World' ) );
|
||||
$item_ids = array();
|
||||
for ( $i = 0; $i < 5; $i += 1 ) {
|
||||
$item_id = wp_update_nav_menu_item( $menu_id, 0, array(
|
||||
@@ -403,7 +403,7 @@ class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase {
|
||||
do_action( 'customize_register', $this->wp_customize );
|
||||
|
||||
$menu_id = wp_create_nav_menu( 'Primary' );
|
||||
$post_id = $this->factory->post->create( array( 'post_title' => 'Hello World' ) );
|
||||
$post_id = self::$factory->post->create( array( 'post_title' => 'Hello World' ) );
|
||||
$item_ids = array();
|
||||
for ( $i = 0; $i < 5; $i += 1 ) {
|
||||
$item_id = wp_update_nav_menu_item( $menu_id, 0, array(
|
||||
@@ -488,8 +488,8 @@ class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase {
|
||||
function test_save_updated() {
|
||||
do_action( 'customize_register', $this->wp_customize );
|
||||
|
||||
$first_post_id = $this->factory->post->create( array( 'post_title' => 'Hello World' ) );
|
||||
$second_post_id = $this->factory->post->create( array( 'post_title' => 'Hola Muno' ) );
|
||||
$first_post_id = self::$factory->post->create( array( 'post_title' => 'Hello World' ) );
|
||||
$second_post_id = self::$factory->post->create( array( 'post_title' => 'Hola Muno' ) );
|
||||
|
||||
$primary_menu_id = wp_create_nav_menu( 'Primary' );
|
||||
$secondary_menu_id = wp_create_nav_menu( 'Secondary' );
|
||||
@@ -554,7 +554,7 @@ class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase {
|
||||
do_action( 'customize_register', $this->wp_customize );
|
||||
|
||||
$menu_id = wp_create_nav_menu( 'Primary' );
|
||||
$post_id = $this->factory->post->create( array( 'post_title' => 'Hello World' ) );
|
||||
$post_id = self::$factory->post->create( array( 'post_title' => 'Hello World' ) );
|
||||
$item_ids = array();
|
||||
for ( $i = 0; $i < 5; $i += 1 ) {
|
||||
$item_id = wp_update_nav_menu_item( $menu_id, 0, array(
|
||||
@@ -623,7 +623,7 @@ class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase {
|
||||
do_action( 'customize_register', $this->wp_customize );
|
||||
|
||||
$menu_id = wp_create_nav_menu( 'Primary' );
|
||||
$post_id = $this->factory->post->create( array( 'post_title' => 'Hello World' ) );
|
||||
$post_id = self::$factory->post->create( array( 'post_title' => 'Hello World' ) );
|
||||
$item_ids = array();
|
||||
for ( $i = 0; $i < 5; $i += 1 ) {
|
||||
$item_id = wp_update_nav_menu_item( $menu_id, 0, array(
|
||||
|
||||
@@ -22,7 +22,7 @@ class Test_WP_Customize_Nav_Menu_Setting extends WP_UnitTestCase {
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
|
||||
wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
wp_set_current_user( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
|
||||
global $wp_customize;
|
||||
$this->wp_customize = new WP_Customize_Manager();
|
||||
|
||||
@@ -22,7 +22,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
|
||||
wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
wp_set_current_user( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
global $wp_customize;
|
||||
$this->wp_customize = new WP_Customize_Manager();
|
||||
$wp_customize = $this->wp_customize;
|
||||
@@ -124,7 +124,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
|
||||
);
|
||||
|
||||
// Create pages.
|
||||
$this->factory->post->create_many( 12, array( 'post_type' => 'page' ) );
|
||||
self::$factory->post->create_many( 12, array( 'post_type' => 'page' ) );
|
||||
|
||||
// Home is included in menu items when page is zero.
|
||||
$items = $menus->load_available_items_query( 'post_type', 'page', 0 );
|
||||
@@ -145,10 +145,10 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
|
||||
$menus = new WP_Customize_Nav_Menus( $this->wp_customize );
|
||||
|
||||
// Create page.
|
||||
$post_id = $this->factory->post->create( array( 'post_title' => 'Post Title' ) );
|
||||
$post_id = self::$factory->post->create( array( 'post_title' => 'Post Title' ) );
|
||||
|
||||
// Create pages.
|
||||
$this->factory->post->create_many( 10 );
|
||||
self::$factory->post->create_many( 10 );
|
||||
|
||||
// Expected menu item array.
|
||||
$expected = array(
|
||||
@@ -175,7 +175,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
|
||||
$menus = new WP_Customize_Nav_Menus( $this->wp_customize );
|
||||
|
||||
// Create page.
|
||||
$page_id = $this->factory->post->create( array( 'post_title' => 'Page Title', 'post_type' => 'page' ) );
|
||||
$page_id = self::$factory->post->create( array( 'post_title' => 'Page Title', 'post_type' => 'page' ) );
|
||||
|
||||
// Expected menu item array.
|
||||
$expected = array(
|
||||
@@ -201,7 +201,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
|
||||
$menus = new WP_Customize_Nav_Menus( $this->wp_customize );
|
||||
|
||||
// Create post.
|
||||
$post_id = $this->factory->post->create( array( 'post_title' => 'Post Title' ) );
|
||||
$post_id = self::$factory->post->create( array( 'post_title' => 'Post Title' ) );
|
||||
|
||||
// Expected menu item array.
|
||||
$expected = array(
|
||||
@@ -227,7 +227,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
|
||||
$menus = new WP_Customize_Nav_Menus( $this->wp_customize );
|
||||
|
||||
// Create term.
|
||||
$term_id = $this->factory->category->create( array( 'name' => 'Term Title' ) );
|
||||
$term_id = self::$factory->category->create( array( 'name' => 'Term Title' ) );
|
||||
|
||||
// Expected menu item array.
|
||||
$expected = array(
|
||||
@@ -279,13 +279,13 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
|
||||
|
||||
// Create posts
|
||||
$post_ids = array();
|
||||
$post_ids[] = $this->factory->post->create( array( 'post_title' => 'Search & Test' ) );
|
||||
$post_ids[] = $this->factory->post->create( array( 'post_title' => 'Some Other Title' ) );
|
||||
$post_ids[] = self::$factory->post->create( array( 'post_title' => 'Search & Test' ) );
|
||||
$post_ids[] = self::$factory->post->create( array( 'post_title' => 'Some Other Title' ) );
|
||||
|
||||
// Create terms
|
||||
$term_ids = array();
|
||||
$term_ids[] = $this->factory->category->create( array( 'name' => 'Dogs Are Cool' ) );
|
||||
$term_ids[] = $this->factory->category->create( array( 'name' => 'Cats Drool' ) );
|
||||
$term_ids[] = self::$factory->category->create( array( 'name' => 'Dogs Are Cool' ) );
|
||||
$term_ids[] = self::$factory->category->create( array( 'name' => 'Cats Drool' ) );
|
||||
|
||||
// Test empty results
|
||||
$expected = array();
|
||||
@@ -386,7 +386,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
|
||||
function test_customize_register() {
|
||||
do_action( 'customize_register', $this->wp_customize );
|
||||
$menu_id = wp_create_nav_menu( 'Primary' );
|
||||
$post_id = $this->factory->post->create( array( 'post_title' => 'Hello World' ) );
|
||||
$post_id = self::$factory->post->create( array( 'post_title' => 'Hello World' ) );
|
||||
$item_id = wp_update_nav_menu_item( $menu_id, 0, array(
|
||||
'menu-item-type' => 'post_type',
|
||||
'menu-item-object' => 'post',
|
||||
|
||||
@@ -128,7 +128,7 @@ class Tests_WP_Customize_Panel extends WP_UnitTestCase {
|
||||
* @see WP_Customize_Panel::check_capabilities()
|
||||
*/
|
||||
function test_check_capabilities() {
|
||||
$user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
|
||||
$user_id = self::$factory->user->create( array( 'role' => 'administrator' ) );
|
||||
wp_set_current_user( $user_id );
|
||||
|
||||
$panel = new WP_Customize_Panel( $this->manager, 'foo' );
|
||||
@@ -154,7 +154,7 @@ class Tests_WP_Customize_Panel extends WP_UnitTestCase {
|
||||
* @see WP_Customize_Panel::maybe_render()
|
||||
*/
|
||||
function test_maybe_render() {
|
||||
wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
wp_set_current_user( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
$panel = new WP_Customize_Panel( $this->manager, 'bar' );
|
||||
$customize_render_panel_count = did_action( 'customize_render_panel' );
|
||||
add_action( 'customize_render_panel', array( $this, 'action_customize_render_panel_test' ) );
|
||||
@@ -179,7 +179,7 @@ class Tests_WP_Customize_Panel extends WP_UnitTestCase {
|
||||
* @see WP_Customize_Panel::print_template()
|
||||
*/
|
||||
function test_print_templates_standard() {
|
||||
wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
wp_set_current_user( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
|
||||
$panel = new WP_Customize_Panel( $this->manager, 'baz' );
|
||||
ob_start();
|
||||
@@ -197,7 +197,7 @@ class Tests_WP_Customize_Panel extends WP_UnitTestCase {
|
||||
* @see WP_Customize_Panel::print_template()
|
||||
*/
|
||||
function test_print_templates_custom() {
|
||||
wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
wp_set_current_user( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
|
||||
$panel = new Custom_Panel_Test( $this->manager, 'baz' );
|
||||
ob_start();
|
||||
|
||||
@@ -135,7 +135,7 @@ class Tests_WP_Customize_Section extends WP_UnitTestCase {
|
||||
* @see WP_Customize_Section::check_capabilities()
|
||||
*/
|
||||
function test_check_capabilities() {
|
||||
$user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
|
||||
$user_id = self::$factory->user->create( array( 'role' => 'administrator' ) );
|
||||
wp_set_current_user( $user_id );
|
||||
|
||||
$section = new WP_Customize_Section( $this->manager, 'foo' );
|
||||
@@ -161,7 +161,7 @@ class Tests_WP_Customize_Section extends WP_UnitTestCase {
|
||||
* @see WP_Customize_Section::maybe_render()
|
||||
*/
|
||||
function test_maybe_render() {
|
||||
wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
wp_set_current_user( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
$section = new WP_Customize_Section( $this->manager, 'bar' );
|
||||
$customize_render_section_count = did_action( 'customize_render_section' );
|
||||
add_action( 'customize_render_section', array( $this, 'action_customize_render_section_test' ) );
|
||||
@@ -186,7 +186,7 @@ class Tests_WP_Customize_Section extends WP_UnitTestCase {
|
||||
* @see WP_Customize_Section::print_template()
|
||||
*/
|
||||
function test_print_templates_standard() {
|
||||
wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
wp_set_current_user( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
|
||||
$section = new WP_Customize_Section( $this->manager, 'baz' );
|
||||
ob_start();
|
||||
@@ -201,7 +201,7 @@ class Tests_WP_Customize_Section extends WP_UnitTestCase {
|
||||
* @see WP_Customize_Section::print_template()
|
||||
*/
|
||||
function test_print_templates_custom() {
|
||||
wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
wp_set_current_user( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
|
||||
$section = new Custom_Section_Test( $this->manager, 'baz' );
|
||||
ob_start();
|
||||
|
||||
@@ -394,7 +394,7 @@ class Tests_WP_Customize_Setting extends WP_UnitTestCase {
|
||||
$this->assertTrue( 0 === did_action( 'customize_save_foo' ) );
|
||||
|
||||
// Satisfy all requirements for save to happen.
|
||||
wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
wp_set_current_user( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
|
||||
$this->assertTrue( false !== $setting->save() );
|
||||
$this->assertTrue( 1 === did_action( 'customize_update_custom' ) );
|
||||
$this->assertTrue( 1 === did_action( 'customize_save_foo' ) );
|
||||
@@ -465,7 +465,7 @@ class Tests_WP_Customize_Setting extends WP_UnitTestCase {
|
||||
$setting->preview();
|
||||
$this->assertTrue( $setting->is_current_blog_previewed() );
|
||||
|
||||
$blog_id = $this->factory->blog->create();
|
||||
$blog_id = self::$factory->blog->create();
|
||||
switch_to_blog( $blog_id );
|
||||
$this->assertFalse( $setting->is_current_blog_previewed() );
|
||||
$this->assertNotEquals( $post_value, $setting->value() );
|
||||
|
||||
@@ -30,7 +30,7 @@ class Tests_WP_Customize_Widgets extends WP_UnitTestCase {
|
||||
|
||||
remove_action( 'after_setup_theme', 'twentyfifteen_setup' ); // @todo We should not be including a theme anyway
|
||||
|
||||
$user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
|
||||
$user_id = self::$factory->user->create( array( 'role' => 'administrator' ) );
|
||||
wp_set_current_user( $user_id );
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user