Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
06/29/2015 09:42:43 PM (11 years ago)
Author:
boonebgorges
Message:

More targeted cleanup for unit tests that use bp_blogs_record_existing_blogs().

bp_blogs_record_existing_blogs() uses TRUNCATE, which closes the MySQL
transaction started by the WP automated test suite. As a result, fixtures
created before the TRUNCATE are not properly rolled back during tearDown().
Previously, the workaround for the problem had been heavy-handed: the wholesale
deletion of certain content during every test setup/teardown. The new technique
is to detect when a known autocommit has taken place - as it does in
bp_blogs_record_existing_blogs() - and to do a more targeted cleanup only in
these cases.

This allows us to dispense with various instances of blog fixture cleanup in
existing tests.

Fixes #6540.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/testcase.php

    r9913 r9980  
    1414        protected static $cached_SERVER_NAME = null;
    1515
     16        /**
     17         * A flag indicating whether an autocommit has been detected inside of a test.
     18         *
     19         * @since BuddyPress (2.4.0)
     20         *
     21         * @var bool
     22         */
     23        protected $autocommitted = false;
     24
    1625        public static function setUpBeforeClass() {
    1726                // Fake WP mail globals, to avoid errors
     
    2332                parent::setUp();
    2433
    25                 // There's a bug in the multisite tests that causes the
    26                 // transaction rollback to fail for the first user created,
    27                 // which busts every other attempt to create users. This is a
    28                 // hack workaround.
    29                 global $wpdb;
    30                 if ( is_multisite() ) {
    31                         $user_1 = get_user_by( 'login', 'user 1' );
    32                         if ( $user_1 ) {
    33                                 $wpdb->delete( $wpdb->users, array( 'ID' => $user_1->ID ) );
    34                                 clean_user_cache( $user_1 );
    35                         }
    36                 }
    37 
    38 
    3934                $this->factory = new BP_UnitTest_Factory;
    4035
    4136                // Fixes warnings in multisite functions
    4237                $_SERVER['REMOTE_ADDR'] = '';
     38                global $wpdb;
     39
     40                // Clean up after autocommits.
     41                add_action( 'bp_blogs_recorded_existing_blogs', array( $this, 'set_autocommit_flag' ) );
     42        }
     43
     44        public function tearDown() {
     45                global $wpdb;
     46
     47                remove_action( 'bp_blogs_recorded_existing_blogs', array( $this, 'set_autocommit_flag' ) );
     48
     49                parent::tearDown();
     50
     51                // If we detect that a COMMIT has been triggered during the test, clean up blog and user fixtures.
     52                if ( $this->autocommitted ) {
     53                        if ( is_multisite() ) {
     54                                foreach ( $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs WHERE blog_id != 1" ) as $blog_id ) {
     55                                        wpmu_delete_blog( $blog_id, true );
     56                                }
     57                        }
     58
     59                        foreach ( $wpdb->get_col( "SELECT ID FROM $wpdb->users WHERE ID != 1" ) as $user_id ) {
     60                                if ( is_multisite() ) {
     61                                        wpmu_delete_user( $user_id );
     62                                } else {
     63                                        wp_delete_user( $user_id );
     64                                }
     65                        }
     66                }
     67
     68                $this->commit_transaction();
    4369        }
    4470
     
    5480
    5581                parent::clean_up_global_scope();
    56         }
    57 
    58         function tearDown() {
    59                 parent::tearDown();
    60 
    61                 /**
    62                  * Sites created by the WP_UnitTest_Factory_For_Blog sometimes are not removed when the current
    63                  * transaction is rolled back. This requires further investigation to understand the root cause
    64                  * but for now, we'll empty out the blogs table manually.
    65                  */
    66                 global $wpdb;
    67 
    68                 if ( is_multisite() ) {
    69                         $blogs = wp_get_sites();
    70                         foreach ( $blogs as $blog ) {
    71                                 if ( (int) $blog['blog_id'] !== 1 ) {
    72                                         wpmu_delete_blog( $blog['blog_id'], true );
    73                                 }
    74                         }
    75                 }
    7682        }
    7783
     
    449455                @rmdir( $dir );
    450456        }
     457
     458        /**
     459         * Set a flag that an autocommit has taken place inside of a test method.
     460         *
     461         * @since BuddyPress (2.4.0)
     462         */
     463        public function set_autocommit_flag() {
     464                $this->autocommitted = true;
     465        }
    451466}
Note: See TracChangeset for help on using the changeset viewer.