diff --git a/tests/phpunit/tests/multisite/site.php b/tests/phpunit/tests/multisite/site.php index 86ad14ad52..4192625e9f 100644 --- a/tests/phpunit/tests/multisite/site.php +++ b/tests/phpunit/tests/multisite/site.php @@ -412,14 +412,6 @@ if ( is_multisite() ) : $this->assertEqualsWithDelta( $current_time, strtotime( $blog->last_updated ), 2, 'The dates should be equal' ); } - /** - * Provide a counter to determine that hooks are firing when intended. - */ - public function action_counter_cb() { - global $test_action_counter; - $test_action_counter++; - } - /** * Test cached data for a site that does not exist and then again after it exists. * @@ -465,27 +457,24 @@ if ( is_multisite() ) : } public function test_update_blog_status_make_ham_blog_action() { - global $test_action_counter; - $test_action_counter = 0; + $test_action_counter = new MockAction(); $blog_id = self::factory()->blog->create(); update_blog_details( $blog_id, array( 'spam' => 1 ) ); - add_action( 'make_ham_blog', array( $this, 'action_counter_cb' ), 10 ); + add_action( 'make_ham_blog', array( $test_action_counter, 'action' ) ); update_blog_status( $blog_id, 'spam', 0 ); $blog = get_site( $blog_id ); $this->assertSame( '0', $blog->spam ); - $this->assertSame( 1, $test_action_counter ); + $this->assertSame( 1, $test_action_counter->get_call_count() ); // The action should not fire if the status of 'spam' stays the same. update_blog_status( $blog_id, 'spam', 0 ); $blog = get_site( $blog_id ); $this->assertSame( '0', $blog->spam ); - $this->assertSame( 1, $test_action_counter ); - - remove_action( 'make_ham_blog', array( $this, 'action_counter_cb' ), 10 ); + $this->assertSame( 1, $test_action_counter->get_call_count() ); } public function test_content_from_spam_blog_is_not_available() { @@ -519,189 +508,165 @@ if ( is_multisite() ) : } public function test_update_blog_status_make_spam_blog_action() { - global $test_action_counter; - $test_action_counter = 0; + $test_action_counter = new MockAction(); $blog_id = self::factory()->blog->create(); - add_action( 'make_spam_blog', array( $this, 'action_counter_cb' ), 10 ); + add_action( 'make_spam_blog', array( $test_action_counter, 'action' ) ); update_blog_status( $blog_id, 'spam', 1 ); $blog = get_site( $blog_id ); $this->assertSame( '1', $blog->spam ); - $this->assertSame( 1, $test_action_counter ); + $this->assertSame( 1, $test_action_counter->get_call_count() ); // The action should not fire if the status of 'spam' stays the same. update_blog_status( $blog_id, 'spam', 1 ); $blog = get_site( $blog_id ); $this->assertSame( '1', $blog->spam ); - $this->assertSame( 1, $test_action_counter ); - - remove_action( 'make_spam_blog', array( $this, 'action_counter_cb' ), 10 ); + $this->assertSame( 1, $test_action_counter->get_call_count() ); } public function test_update_blog_status_archive_blog_action() { - global $test_action_counter; - $test_action_counter = 0; + $test_action_counter = new MockAction(); $blog_id = self::factory()->blog->create(); - add_action( 'archive_blog', array( $this, 'action_counter_cb' ), 10 ); + add_action( 'archive_blog', array( $test_action_counter, 'action' ) ); update_blog_status( $blog_id, 'archived', 1 ); $blog = get_site( $blog_id ); $this->assertSame( '1', $blog->archived ); - $this->assertSame( 1, $test_action_counter ); + $this->assertSame( 1, $test_action_counter->get_call_count() ); // The action should not fire if the status of 'archived' stays the same. update_blog_status( $blog_id, 'archived', 1 ); $blog = get_site( $blog_id ); $this->assertSame( '1', $blog->archived ); - $this->assertSame( 1, $test_action_counter ); - - remove_action( 'archive_blog', array( $this, 'action_counter_cb' ), 10 ); + $this->assertSame( 1, $test_action_counter->get_call_count() ); } public function test_update_blog_status_unarchive_blog_action() { - global $test_action_counter; - $test_action_counter = 0; + $test_action_counter = new MockAction(); $blog_id = self::factory()->blog->create(); update_blog_details( $blog_id, array( 'archived' => 1 ) ); - add_action( 'unarchive_blog', array( $this, 'action_counter_cb' ), 10 ); + add_action( 'unarchive_blog', array( $test_action_counter, 'action' ) ); update_blog_status( $blog_id, 'archived', 0 ); $blog = get_site( $blog_id ); $this->assertSame( '0', $blog->archived ); - $this->assertSame( 1, $test_action_counter ); + $this->assertSame( 1, $test_action_counter->get_call_count() ); // The action should not fire if the status of 'archived' stays the same. update_blog_status( $blog_id, 'archived', 0 ); $blog = get_site( $blog_id ); $this->assertSame( '0', $blog->archived ); - $this->assertSame( 1, $test_action_counter ); - - remove_action( 'unarchive_blog', array( $this, 'action_counter_cb' ), 10 ); + $this->assertSame( 1, $test_action_counter->get_call_count() ); } public function test_update_blog_status_make_delete_blog_action() { - global $test_action_counter; - $test_action_counter = 0; + $test_action_counter = new MockAction(); $blog_id = self::factory()->blog->create(); - add_action( 'make_delete_blog', array( $this, 'action_counter_cb' ), 10 ); + add_action( 'make_delete_blog', array( $test_action_counter, 'action' ) ); update_blog_status( $blog_id, 'deleted', 1 ); $blog = get_site( $blog_id ); $this->assertSame( '1', $blog->deleted ); - $this->assertSame( 1, $test_action_counter ); + $this->assertSame( 1, $test_action_counter->get_call_count() ); // The action should not fire if the status of 'deleted' stays the same. update_blog_status( $blog_id, 'deleted', 1 ); $blog = get_site( $blog_id ); $this->assertSame( '1', $blog->deleted ); - $this->assertSame( 1, $test_action_counter ); - - remove_action( 'make_delete_blog', array( $this, 'action_counter_cb' ), 10 ); + $this->assertSame( 1, $test_action_counter->get_call_count() ); } public function test_update_blog_status_make_undelete_blog_action() { - global $test_action_counter; - $test_action_counter = 0; + $test_action_counter = new MockAction(); $blog_id = self::factory()->blog->create(); update_blog_details( $blog_id, array( 'deleted' => 1 ) ); - add_action( 'make_undelete_blog', array( $this, 'action_counter_cb' ), 10 ); + add_action( 'make_undelete_blog', array( $test_action_counter, 'action' ) ); update_blog_status( $blog_id, 'deleted', 0 ); $blog = get_site( $blog_id ); $this->assertSame( '0', $blog->deleted ); - $this->assertSame( 1, $test_action_counter ); + $this->assertSame( 1, $test_action_counter->get_call_count() ); // The action should not fire if the status of 'deleted' stays the same. update_blog_status( $blog_id, 'deleted', 0 ); $blog = get_site( $blog_id ); $this->assertSame( '0', $blog->deleted ); - $this->assertSame( 1, $test_action_counter ); - - remove_action( 'make_undelete_blog', array( $this, 'action_counter_cb' ), 10 ); + $this->assertSame( 1, $test_action_counter->get_call_count() ); } public function test_update_blog_status_mature_blog_action() { - global $test_action_counter; - $test_action_counter = 0; + $test_action_counter = new MockAction(); $blog_id = self::factory()->blog->create(); - add_action( 'mature_blog', array( $this, 'action_counter_cb' ), 10 ); + add_action( 'mature_blog', array( $test_action_counter, 'action' ) ); update_blog_status( $blog_id, 'mature', 1 ); $blog = get_site( $blog_id ); $this->assertSame( '1', $blog->mature ); - $this->assertSame( 1, $test_action_counter ); + $this->assertSame( 1, $test_action_counter->get_call_count() ); // The action should not fire if the status of 'mature' stays the same. update_blog_status( $blog_id, 'mature', 1 ); $blog = get_site( $blog_id ); $this->assertSame( '1', $blog->mature ); - $this->assertSame( 1, $test_action_counter ); - - remove_action( 'mature_blog', array( $this, 'action_counter_cb' ), 10 ); + $this->assertSame( 1, $test_action_counter->get_call_count() ); } public function test_update_blog_status_unmature_blog_action() { - global $test_action_counter; - $test_action_counter = 0; + $test_action_counter = new MockAction(); $blog_id = self::factory()->blog->create(); update_blog_details( $blog_id, array( 'mature' => 1 ) ); - add_action( 'unmature_blog', array( $this, 'action_counter_cb' ), 10 ); + add_action( 'unmature_blog', array( $test_action_counter, 'action' ) ); update_blog_status( $blog_id, 'mature', 0 ); $blog = get_site( $blog_id ); $this->assertSame( '0', $blog->mature ); - $this->assertSame( 1, $test_action_counter ); + $this->assertSame( 1, $test_action_counter->get_call_count() ); // The action should not fire if the status of 'mature' stays the same. update_blog_status( $blog_id, 'mature', 0 ); $blog = get_site( $blog_id ); $this->assertSame( '0', $blog->mature ); - $this->assertSame( 1, $test_action_counter ); - - remove_action( 'unmature_blog', array( $this, 'action_counter_cb' ), 10 ); + $this->assertSame( 1, $test_action_counter->get_call_count() ); } public function test_update_blog_status_update_blog_public_action() { - global $test_action_counter; - $test_action_counter = 0; + $test_action_counter = new MockAction(); $blog_id = self::factory()->blog->create(); - add_action( 'update_blog_public', array( $this, 'action_counter_cb' ), 10 ); + add_action( 'update_blog_public', array( $test_action_counter, 'action' ) ); update_blog_status( $blog_id, 'public', 0 ); $blog = get_site( $blog_id ); $this->assertSame( '0', $blog->public ); - $this->assertSame( 1, $test_action_counter ); + $this->assertSame( 1, $test_action_counter->get_call_count() ); // The action should not fire if the status of 'mature' stays the same. update_blog_status( $blog_id, 'public', 0 ); $blog = get_site( $blog_id ); $this->assertSame( '0', $blog->public ); - $this->assertSame( 1, $test_action_counter ); - - remove_action( 'update_blog_public', array( $this, 'action_counter_cb' ), 10 ); + $this->assertSame( 1, $test_action_counter->get_call_count() ); } /** diff --git a/tests/phpunit/tests/multisite/updateBlogDetails.php b/tests/phpunit/tests/multisite/updateBlogDetails.php index 80595ed99b..1c7d19a2a5 100644 --- a/tests/phpunit/tests/multisite/updateBlogDetails.php +++ b/tests/phpunit/tests/multisite/updateBlogDetails.php @@ -56,8 +56,7 @@ if ( is_multisite() ) : * @dataProvider data_flag_hooks */ public function test_update_blog_details_flag_action( $flag, $flag_value, $hook ) { - global $test_action_counter; - $test_action_counter = 0; + $test_action_counter = new MockAction(); $blog_id = self::factory()->blog->create(); @@ -66,7 +65,7 @@ if ( is_multisite() ) : update_blog_details( $blog_id, array( $flag => '1' ) ); } - add_action( $hook, array( $this, 'action_counter_cb' ), 10 ); + add_action( $hook, array( $test_action_counter, 'action' ) ); update_blog_details( $blog_id, array( $flag => $flag_value ) ); $blog = get_site( $blog_id ); @@ -74,15 +73,13 @@ if ( is_multisite() ) : $this->assertSame( $flag_value, $blog->{$flag} ); // The hook attached to this flag should have fired once during update_blog_details(). - $this->assertSame( 1, $test_action_counter ); + $this->assertSame( 1, $test_action_counter->get_call_count() ); // Update the site to the exact same flag value for this flag. update_blog_details( $blog_id, array( $flag => $flag_value ) ); // The hook attached to this flag should not have fired again. - $this->assertSame( 1, $test_action_counter ); - - remove_action( $hook, array( $this, 'action_counter_cb' ), 10 ); + $this->assertSame( 1, $test_action_counter->get_call_count() ); } public function data_flag_hooks() { @@ -98,14 +95,6 @@ if ( is_multisite() ) : ); } - /** - * Provide a counter to determine that hooks are firing when intended. - */ - public function action_counter_cb() { - global $test_action_counter; - $test_action_counter++; - } - /** * When the path for a site is updated with update_blog_details(), the final path * should have a leading and trailing slash.