Make WordPress Core

Changeset 54756


Ignore:
Timestamp:
11/06/2022 03:54:34 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Tests: Remove a custom callback for checking action call count in multisite tests.

Use MockAction::get_call_count() instead, for consistency with the rest of the test suite.

Follow-up to [24/tests], [99/tests], [1078/tests], [1089/tests], [29916], [30784], [30785], [33253].

See #56793.

Location:
trunk/tests/phpunit/tests/multisite
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/multisite/site.php

    r54402 r54756  
    414414
    415415                /**
    416                  * Provide a counter to determine that hooks are firing when intended.
    417                  */
    418                 public function action_counter_cb() {
    419                         global $test_action_counter;
    420                         $test_action_counter++;
    421                 }
    422 
    423                 /**
    424416                 * Test cached data for a site that does not exist and then again after it exists.
    425417                 *
    … …  
    466458
    467459                public function test_update_blog_status_make_ham_blog_action() {
    468                         global $test_action_counter;
    469                         $test_action_counter = 0;
     460                        $test_action_counter = new MockAction();
    470461
    471462                        $blog_id = self::factory()->blog->create();
    472463                        update_blog_details( $blog_id, array( 'spam' => 1 ) );
    473464
    474                         add_action( 'make_ham_blog', array( $this, 'action_counter_cb' ), 10 );
     465                        add_action( 'make_ham_blog', array( $test_action_counter, 'action' ) );
    475466                        update_blog_status( $blog_id, 'spam', 0 );
    476467                        $blog = get_site( $blog_id );
    477468
    478469                        $this->assertSame( '0', $blog->spam );
    479                         $this->assertSame( 1, $test_action_counter );
     470                        $this->assertSame( 1, $test_action_counter->get_call_count() );
    480471
    481472                        // The action should not fire if the status of 'spam' stays the same.
    … …  
    484475
    485476                        $this->assertSame( '0', $blog->spam );
    486                         $this->assertSame( 1, $test_action_counter );
    487 
    488                         remove_action( 'make_ham_blog', array( $this, 'action_counter_cb' ), 10 );
     477                        $this->assertSame( 1, $test_action_counter->get_call_count() );
    489478                }
    490479
    … …  
    520509
    521510                public function test_update_blog_status_make_spam_blog_action() {
    522                         global $test_action_counter;
    523                         $test_action_counter = 0;
     511                        $test_action_counter = new MockAction();
    524512
    525513                        $blog_id = self::factory()->blog->create();
    526514
    527                         add_action( 'make_spam_blog', array( $this, 'action_counter_cb' ), 10 );
     515                        add_action( 'make_spam_blog', array( $test_action_counter, 'action' ) );
    528516                        update_blog_status( $blog_id, 'spam', 1 );
    529517                        $blog = get_site( $blog_id );
    530518
    531519                        $this->assertSame( '1', $blog->spam );
    532                         $this->assertSame( 1, $test_action_counter );
     520                        $this->assertSame( 1, $test_action_counter->get_call_count() );
    533521
    534522                        // The action should not fire if the status of 'spam' stays the same.
    … …  
    537525
    538526                        $this->assertSame( '1', $blog->spam );
    539                         $this->assertSame( 1, $test_action_counter );
    540 
    541                         remove_action( 'make_spam_blog', array( $this, 'action_counter_cb' ), 10 );
     527                        $this->assertSame( 1, $test_action_counter->get_call_count() );
    542528                }
    543529
    544530                public function test_update_blog_status_archive_blog_action() {
    545                         global $test_action_counter;
    546                         $test_action_counter = 0;
     531                        $test_action_counter = new MockAction();
    547532
    548533                        $blog_id = self::factory()->blog->create();
    549534
    550                         add_action( 'archive_blog', array( $this, 'action_counter_cb' ), 10 );
     535                        add_action( 'archive_blog', array( $test_action_counter, 'action' ) );
    551536                        update_blog_status( $blog_id, 'archived', 1 );
    552537                        $blog = get_site( $blog_id );
    553538
    554539                        $this->assertSame( '1', $blog->archived );
    555                         $this->assertSame( 1, $test_action_counter );
     540                        $this->assertSame( 1, $test_action_counter->get_call_count() );
    556541
    557542                        // The action should not fire if the status of 'archived' stays the same.
    … …  
    560545
    561546                        $this->assertSame( '1', $blog->archived );
    562                         $this->assertSame( 1, $test_action_counter );
    563 
    564                         remove_action( 'archive_blog', array( $this, 'action_counter_cb' ), 10 );
     547                        $this->assertSame( 1, $test_action_counter->get_call_count() );
    565548                }
    566549
    567550                public function test_update_blog_status_unarchive_blog_action() {
    568                         global $test_action_counter;
    569                         $test_action_counter = 0;
     551                        $test_action_counter = new MockAction();
    570552
    571553                        $blog_id = self::factory()->blog->create();
    572554                        update_blog_details( $blog_id, array( 'archived' => 1 ) );
    573555
    574                         add_action( 'unarchive_blog', array( $this, 'action_counter_cb' ), 10 );
     556                        add_action( 'unarchive_blog', array( $test_action_counter, 'action' ) );
    575557                        update_blog_status( $blog_id, 'archived', 0 );
    576558                        $blog = get_site( $blog_id );
    577559
    578560                        $this->assertSame( '0', $blog->archived );
    579                         $this->assertSame( 1, $test_action_counter );
     561                        $this->assertSame( 1, $test_action_counter->get_call_count() );
    580562
    581563                        // The action should not fire if the status of 'archived' stays the same.
    … …  
    583565                        $blog = get_site( $blog_id );
    584566                        $this->assertSame( '0', $blog->archived );
    585                         $this->assertSame( 1, $test_action_counter );
    586 
    587                         remove_action( 'unarchive_blog', array( $this, 'action_counter_cb' ), 10 );
     567                        $this->assertSame( 1, $test_action_counter->get_call_count() );
    588568                }
    589569
    590570                public function test_update_blog_status_make_delete_blog_action() {
    591                         global $test_action_counter;
    592                         $test_action_counter = 0;
     571                        $test_action_counter = new MockAction();
    593572
    594573                        $blog_id = self::factory()->blog->create();
    595574
    596                         add_action( 'make_delete_blog', array( $this, 'action_counter_cb' ), 10 );
     575                        add_action( 'make_delete_blog', array( $test_action_counter, 'action' ) );
    597576                        update_blog_status( $blog_id, 'deleted', 1 );
    598577                        $blog = get_site( $blog_id );
    599578
    600579                        $this->assertSame( '1', $blog->deleted );
    601                         $this->assertSame( 1, $test_action_counter );
     580                        $this->assertSame( 1, $test_action_counter->get_call_count() );
    602581
    603582                        // The action should not fire if the status of 'deleted' stays the same.
    … …  
    606585
    607586                        $this->assertSame( '1', $blog->deleted );
    608                         $this->assertSame( 1, $test_action_counter );
    609 
    610                         remove_action( 'make_delete_blog', array( $this, 'action_counter_cb' ), 10 );
     587                        $this->assertSame( 1, $test_action_counter->get_call_count() );
    611588                }
    612589
    613590                public function test_update_blog_status_make_undelete_blog_action() {
    614                         global $test_action_counter;
    615                         $test_action_counter = 0;
     591                        $test_action_counter = new MockAction();
    616592
    617593                        $blog_id = self::factory()->blog->create();
    618594                        update_blog_details( $blog_id, array( 'deleted' => 1 ) );
    619595
    620                         add_action( 'make_undelete_blog', array( $this, 'action_counter_cb' ), 10 );
     596                        add_action( 'make_undelete_blog', array( $test_action_counter, 'action' ) );
    621597                        update_blog_status( $blog_id, 'deleted', 0 );
    622598                        $blog = get_site( $blog_id );
    623599
    624600                        $this->assertSame( '0', $blog->deleted );
    625                         $this->assertSame( 1, $test_action_counter );
     601                        $this->assertSame( 1, $test_action_counter->get_call_count() );
    626602
    627603                        // The action should not fire if the status of 'deleted' stays the same.
    … …  
    630606
    631607                        $this->assertSame( '0', $blog->deleted );
    632                         $this->assertSame( 1, $test_action_counter );
    633 
    634                         remove_action( 'make_undelete_blog', array( $this, 'action_counter_cb' ), 10 );
     608                        $this->assertSame( 1, $test_action_counter->get_call_count() );
    635609                }
    636610
    637611                public function test_update_blog_status_mature_blog_action() {
    638                         global $test_action_counter;
    639                         $test_action_counter = 0;
     612                        $test_action_counter = new MockAction();
    640613
    641614                        $blog_id = self::factory()->blog->create();
    642615
    643                         add_action( 'mature_blog', array( $this, 'action_counter_cb' ), 10 );
     616                        add_action( 'mature_blog', array( $test_action_counter, 'action' ) );
    644617                        update_blog_status( $blog_id, 'mature', 1 );
    645618                        $blog = get_site( $blog_id );
    646619
    647620                        $this->assertSame( '1', $blog->mature );
    648                         $this->assertSame( 1, $test_action_counter );
     621                        $this->assertSame( 1, $test_action_counter->get_call_count() );
    649622
    650623                        // The action should not fire if the status of 'mature' stays the same.
    … …  
    653626
    654627                        $this->assertSame( '1', $blog->mature );
    655                         $this->assertSame( 1, $test_action_counter );
    656 
    657                         remove_action( 'mature_blog', array( $this, 'action_counter_cb' ), 10 );
     628                        $this->assertSame( 1, $test_action_counter->get_call_count() );
    658629                }
    659630
    660631                public function test_update_blog_status_unmature_blog_action() {
    661                         global $test_action_counter;
    662                         $test_action_counter = 0;
     632                        $test_action_counter = new MockAction();
    663633
    664634                        $blog_id = self::factory()->blog->create();
    665635                        update_blog_details( $blog_id, array( 'mature' => 1 ) );
    666636
    667                         add_action( 'unmature_blog', array( $this, 'action_counter_cb' ), 10 );
     637                        add_action( 'unmature_blog', array( $test_action_counter, 'action' ) );
    668638                        update_blog_status( $blog_id, 'mature', 0 );
    669639
    670640                        $blog = get_site( $blog_id );
    671641                        $this->assertSame( '0', $blog->mature );
    672                         $this->assertSame( 1, $test_action_counter );
     642                        $this->assertSame( 1, $test_action_counter->get_call_count() );
    673643
    674644                        // The action should not fire if the status of 'mature' stays the same.
    … …  
    677647
    678648                        $this->assertSame( '0', $blog->mature );
    679                         $this->assertSame( 1, $test_action_counter );
    680 
    681                         remove_action( 'unmature_blog', array( $this, 'action_counter_cb' ), 10 );
     649                        $this->assertSame( 1, $test_action_counter->get_call_count() );
    682650                }
    683651
    684652                public function test_update_blog_status_update_blog_public_action() {
    685                         global $test_action_counter;
    686                         $test_action_counter = 0;
     653                        $test_action_counter = new MockAction();
    687654
    688655                        $blog_id = self::factory()->blog->create();
    689656
    690                         add_action( 'update_blog_public', array( $this, 'action_counter_cb' ), 10 );
     657                        add_action( 'update_blog_public', array( $test_action_counter, 'action' ) );
    691658                        update_blog_status( $blog_id, 'public', 0 );
    692659
    693660                        $blog = get_site( $blog_id );
    694661                        $this->assertSame( '0', $blog->public );
    695                         $this->assertSame( 1, $test_action_counter );
     662                        $this->assertSame( 1, $test_action_counter->get_call_count() );
    696663
    697664                        // The action should not fire if the status of 'mature' stays the same.
    … …  
    700667
    701668                        $this->assertSame( '0', $blog->public );
    702                         $this->assertSame( 1, $test_action_counter );
    703 
    704                         remove_action( 'update_blog_public', array( $this, 'action_counter_cb' ), 10 );
     669                        $this->assertSame( 1, $test_action_counter->get_call_count() );
    705670                }
    706671
  • trunk/tests/phpunit/tests/multisite/updateBlogDetails.php

    r52010 r54756  
    5757                 */
    5858                public function test_update_blog_details_flag_action( $flag, $flag_value, $hook ) {
    59                         global $test_action_counter;
    60                         $test_action_counter = 0;
     59                        $test_action_counter = new MockAction();
    6160
    6261                        $blog_id = self::factory()->blog->create();
    … …  
    6766                        }
    6867
    69                         add_action( $hook, array( $this, 'action_counter_cb' ), 10 );
     68                        add_action( $hook, array( $test_action_counter, 'action' ) );
    7069
    7170                        update_blog_details( $blog_id, array( $flag => $flag_value ) );
    … …  
    7574
    7675                        // The hook attached to this flag should have fired once during update_blog_details().
    77                         $this->assertSame( 1, $test_action_counter );
     76                        $this->assertSame( 1, $test_action_counter->get_call_count() );
    7877
    7978                        // Update the site to the exact same flag value for this flag.
    … …  
    8180
    8281                        // The hook attached to this flag should not have fired again.
    83                         $this->assertSame( 1, $test_action_counter );
    84 
    85                         remove_action( $hook, array( $this, 'action_counter_cb' ), 10 );
     82                        $this->assertSame( 1, $test_action_counter->get_call_count() );
    8683                }
    8784
    … …  
    9794                                array( 'mature', '0', 'unmature_blog' ),
    9895                        );
    99                 }
    100 
    101                 /**
    102                  * Provide a counter to determine that hooks are firing when intended.
    103                  */
    104                 public function action_counter_cb() {
    105                         global $test_action_counter;
    106                         $test_action_counter++;
    10796                }
    10897
Note: See TracChangeset for help on using the changeset viewer.