Make WordPress Core


Ignore:
Timestamp:
04/18/2016 01:37:47 AM (9 years ago)
Author:
jeremyfelt
Message:

Tests: Share test fixtures in multisite bootstrap tests

  • Remove unnecessary setUp and tearDown methods.
  • Create networks and sites in wpSetupBeforeClass to share throughout.
  • Destroy networks and sites in wpTearDownAfterClass to unpollute.

See #36566, #34941.

File:
1 edited

Legend:

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

    r35242 r37234  
    1010 */
    1111class Tests_Multisite_Bootstrap extends WP_UnitTestCase {
    12     protected $suppress = false;
    13 
    14     function setUp() {
    15         global $wpdb;
    16         parent::setUp();
    17         $this->suppress = $wpdb->suppress_errors();
    18     }
    19 
    20     function tearDown() {
    21         global $wpdb;
    22         $wpdb->suppress_errors( $this->suppress );
    23         parent::tearDown();
    24     }
    25 
    26 
    27     /**
    28      * @ticket 27003
    29      */
    30     function test_get_network_by_path() {
    31         global $wpdb;
    32 
    33         $ids = array(
     12    protected static $network_ids;
     13    protected static $site_ids;
     14
     15    public static function wpSetUpBeforeClass( $factory ) {
     16        self::$network_ids = array(
    3417            'wordpress.org/'         => array( 'domain' => 'wordpress.org', 'path' => '/' ),
     18            'make.wordpress.org/'    => array( 'domain' => 'make.wordpress.org', 'path' => '/' ),
    3519            'wordpress.org/one/'     => array( 'domain' => 'wordpress.org', 'path' => '/one/' ),
    3620            'wordpress.net/'         => array( 'domain' => 'wordpress.net', 'path' => '/' ),
     
    4024        );
    4125
    42         foreach ( $ids as &$id ) {
    43             $id = self::factory()->network->create( $id );
     26        foreach ( self::$network_ids as &$id ) {
     27            $id = $factory->network->create( $id );
    4428        }
    4529        unset( $id );
    4630
    47         $this->assertEquals( $ids['www.wordpress.net/'],
    48             get_network_by_path( 'www.wordpress.net', '/notapath/' )->id );
    49 
    50         $this->assertEquals( $ids['www.wordpress.net/two/'],
    51             get_network_by_path( 'www.wordpress.net', '/two/' )->id );
    52 
    53         // This should find /one/ despite the www.
    54         $this->assertEquals( $ids['wordpress.org/one/'],
    55             get_network_by_path( 'www.wordpress.org', '/one/' )->id );
    56 
    57         // This should not find /one/ because the domains don't match.
    58         $this->assertEquals( $ids['wordpress.org/'],
    59             get_network_by_path( 'site1.wordpress.org', '/one/' )->id );
    60 
    61         $this->assertEquals( $ids['wordpress.net/three/'],
    62             get_network_by_path( 'wordpress.net', '/three/' )->id );
    63 
    64         $this->assertEquals( $ids['wordpress.net/'],
    65             get_network_by_path( 'wordpress.net', '/notapath/' )->id );
    66 
    67         $this->assertEquals( $ids['wordpress.net/'],
    68             get_network_by_path( 'site1.wordpress.net', '/notapath/' )->id );
    69 
    70         $this->assertEquals( $ids['wordpress.net/'],
    71             get_network_by_path( 'site1.wordpress.net', '/three/' )->id );
    72     }
    73 
    74     /**
    75      * @ticket 27003
    76      * @ticket 27927
    77      */
    78     function test_get_site_by_path() {
    79         $ids = array(
    80             'wordpress.org/'              => array( 'domain' => 'wordpress.org',      'path' => '/' ),
    81             'wordpress.org/foo/'          => array( 'domain' => 'wordpress.org',      'path' => '/foo/' ),
    82             'wordpress.org/foo/bar/'      => array( 'domain' => 'wordpress.org',      'path' => '/foo/bar/' ),
    83             'make.wordpress.org/'         => array( 'domain' => 'make.wordpress.org', 'path' => '/' ),
    84             'make.wordpress.org/foo/'     => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/' ),
     31        self::$site_ids = array(
     32            'wordpress.org/'              => array( 'domain' => 'wordpress.org',      'path' => '/',         'site_id' => self::$network_ids['wordpress.org/'] ),
     33            'wordpress.org/foo/'          => array( 'domain' => 'wordpress.org',      'path' => '/foo/',     'site_id' => self::$network_ids['wordpress.org/'] ),
     34            'wordpress.org/foo/bar/'      => array( 'domain' => 'wordpress.org',      'path' => '/foo/bar/', 'site_id' => self::$network_ids['wordpress.org/'] ),
     35            'make.wordpress.org/'         => array( 'domain' => 'make.wordpress.org', 'path' => '/',         'site_id' => self::$network_ids['make.wordpress.org/'] ),
     36            'make.wordpress.org/foo/'     => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/',     'site_id' => self::$network_ids['make.wordpress.org/'] ),
    8537            'www.w.org/'                  => array( 'domain' => 'www.w.org',          'path' => '/' ),
    8638            'www.w.org/foo/'              => array( 'domain' => 'www.w.org',          'path' => '/foo/' ),
     
    8840        );
    8941
    90         foreach ( $ids as &$id ) {
    91             $id = self::factory()->blog->create( $id );
     42        foreach ( self::$site_ids as &$id ) {
     43            $id = $factory->blog->create( $id );
    9244        }
    9345        unset( $id );
    94 
    95         $this->assertEquals( $ids['wordpress.org/'],
     46    }
     47
     48    public static function wpTearDownAfterClass() {
     49        global $wpdb;
     50
     51        foreach( self::$site_ids as $id ) {
     52            wpmu_delete_blog( $id, true );
     53        }
     54
     55        foreach( self::$network_ids as $id ) {
     56            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
     57            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
     58        }
     59
     60        wp_update_network_site_counts();
     61    }
     62
     63    /**
     64     * @ticket 27003
     65     */
     66    function test_get_network_by_path() {
     67        $this->assertEquals( self::$network_ids['www.wordpress.net/'],
     68            get_network_by_path( 'www.wordpress.net', '/notapath/' )->id );
     69
     70        $this->assertEquals( self::$network_ids['www.wordpress.net/two/'],
     71            get_network_by_path( 'www.wordpress.net', '/two/' )->id );
     72
     73        // This should find /one/ despite the www.
     74        $this->assertEquals( self::$network_ids['wordpress.org/one/'],
     75            get_network_by_path( 'www.wordpress.org', '/one/' )->id );
     76
     77        // This should not find /one/ because the domains don't match.
     78        $this->assertEquals( self::$network_ids['wordpress.org/'],
     79            get_network_by_path( 'site1.wordpress.org', '/one/' )->id );
     80
     81        $this->assertEquals( self::$network_ids['wordpress.net/three/'],
     82            get_network_by_path( 'wordpress.net', '/three/' )->id );
     83
     84        $this->assertEquals( self::$network_ids['wordpress.net/'],
     85            get_network_by_path( 'wordpress.net', '/notapath/' )->id );
     86
     87        $this->assertEquals( self::$network_ids['wordpress.net/'],
     88            get_network_by_path( 'site1.wordpress.net', '/notapath/' )->id );
     89
     90        $this->assertEquals( self::$network_ids['wordpress.net/'],
     91            get_network_by_path( 'site1.wordpress.net', '/three/' )->id );
     92    }
     93
     94    /**
     95     * @ticket 27003
     96     * @ticket 27927
     97     */
     98    function test_get_site_by_path() {
     99        $this->assertEquals( self::$site_ids['wordpress.org/'],
    96100            get_site_by_path( 'wordpress.org', '/notapath/' )->blog_id );
    97101
    98         $this->assertEquals( $ids['wordpress.org/'],
     102        $this->assertEquals( self::$site_ids['wordpress.org/'],
    99103            get_site_by_path( 'www.wordpress.org', '/notapath/' )->blog_id );
    100104
    101         $this->assertEquals( $ids['wordpress.org/foo/bar/'],
     105        $this->assertEquals( self::$site_ids['wordpress.org/foo/bar/'],
    102106            get_site_by_path( 'wordpress.org', '/foo/bar/baz/' )->blog_id );
    103107
    104         $this->assertEquals( $ids['wordpress.org/foo/bar/'],
     108        $this->assertEquals( self::$site_ids['wordpress.org/foo/bar/'],
    105109            get_site_by_path( 'www.wordpress.org', '/foo/bar/baz/' )->blog_id );
    106110
    107         $this->assertEquals( $ids['wordpress.org/foo/bar/'],
     111        $this->assertEquals( self::$site_ids['wordpress.org/foo/bar/'],
    108112            get_site_by_path( 'wordpress.org', '/foo/bar/baz/', 3 )->blog_id );
    109113
    110         $this->assertEquals( $ids['wordpress.org/foo/bar/'],
     114        $this->assertEquals( self::$site_ids['wordpress.org/foo/bar/'],
    111115            get_site_by_path( 'www.wordpress.org', '/foo/bar/baz/', 3 )->blog_id );
    112116
    113         $this->assertEquals( $ids['wordpress.org/foo/bar/'],
     117        $this->assertEquals( self::$site_ids['wordpress.org/foo/bar/'],
    114118            get_site_by_path( 'wordpress.org', '/foo/bar/baz/', 2 )->blog_id );
    115119
    116         $this->assertEquals( $ids['wordpress.org/foo/bar/'],
     120        $this->assertEquals( self::$site_ids['wordpress.org/foo/bar/'],
    117121            get_site_by_path( 'www.wordpress.org', '/foo/bar/baz/', 2 )->blog_id );
    118122
    119         $this->assertEquals( $ids['wordpress.org/foo/'],
     123        $this->assertEquals( self::$site_ids['wordpress.org/foo/'],
    120124            get_site_by_path( 'wordpress.org', '/foo/bar/baz/', 1 )->blog_id );
    121125
    122         $this->assertEquals( $ids['wordpress.org/foo/'],
     126        $this->assertEquals( self::$site_ids['wordpress.org/foo/'],
    123127            get_site_by_path( 'www.wordpress.org', '/foo/bar/baz/', 1 )->blog_id );
    124128
    125         $this->assertEquals( $ids['wordpress.org/'],
     129        $this->assertEquals( self::$site_ids['wordpress.org/'],
    126130            get_site_by_path( 'wordpress.org', '/', 0 )->blog_id );
    127131
    128         $this->assertEquals( $ids['wordpress.org/'],
     132        $this->assertEquals( self::$site_ids['wordpress.org/'],
    129133            get_site_by_path( 'www.wordpress.org', '/', 0 )->blog_id );
    130134
    131         $this->assertEquals( $ids['make.wordpress.org/foo/'],
     135        $this->assertEquals( self::$site_ids['make.wordpress.org/foo/'],
    132136            get_site_by_path( 'make.wordpress.org', '/foo/bar/baz/qux/', 4 )->blog_id );
    133137
    134         $this->assertEquals( $ids['make.wordpress.org/foo/'],
     138        $this->assertEquals( self::$site_ids['make.wordpress.org/foo/'],
    135139            get_site_by_path( 'www.make.wordpress.org', '/foo/bar/baz/qux/', 4 )->blog_id );
    136140
    137         $this->assertEquals( $ids['www.w.org/'],
     141        $this->assertEquals( self::$site_ids['www.w.org/'],
    138142            get_site_by_path( 'www.w.org', '/', 0 )->blog_id );
    139143
    140         $this->assertEquals( $ids['www.w.org/'],
     144        $this->assertEquals( self::$site_ids['www.w.org/'],
    141145            get_site_by_path( 'www.w.org', '/notapath/' )->blog_id );
    142146
    143         $this->assertEquals( $ids['www.w.org/foo/bar/'],
     147        $this->assertEquals( self::$site_ids['www.w.org/foo/bar/'],
    144148            get_site_by_path( 'www.w.org', '/foo/bar/baz/' )->blog_id );
    145149
    146         $this->assertEquals( $ids['www.w.org/foo/'],
     150        $this->assertEquals( self::$site_ids['www.w.org/foo/'],
    147151            get_site_by_path( 'www.w.org', '/foo/bar/baz/', 1 )->blog_id );
    148152
     
    164168        global $current_blog;
    165169
    166         $network_ids = array(
    167             'wordpress.org/'         => array( 'domain' => 'wordpress.org', 'path' => '/' ),
    168             'make.wordpress.org/'    => array( 'domain' => 'make.wordpress.org', 'path' => '/' ),
    169         );
    170 
    171         foreach ( $network_ids as &$id ) {
    172             $id = self::factory()->network->create( $id );
    173         }
    174         unset( $id );
    175 
    176         $ids = array(
    177             'wordpress.org/'              => array( 'domain' => 'wordpress.org',      'path' => '/',         'site_id' => $network_ids['wordpress.org/'] ),
    178             'wordpress.org/foo/'          => array( 'domain' => 'wordpress.org',      'path' => '/foo/',     'site_id' => $network_ids['wordpress.org/'] ),
    179             'wordpress.org/foo/bar/'      => array( 'domain' => 'wordpress.org',      'path' => '/foo/bar/', 'site_id' => $network_ids['wordpress.org/'] ),
    180             'make.wordpress.org/'         => array( 'domain' => 'make.wordpress.org', 'path' => '/',         'site_id' => $network_ids['make.wordpress.org/'] ),
    181             'make.wordpress.org/foo/'     => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/',     'site_id' => $network_ids['make.wordpress.org/'] ),
    182         );
    183 
    184         foreach ( $ids as &$id ) {
    185             $id = self::factory()->blog->create( $id );
    186         }
    187         unset( $id );
    188 
    189170        $this->_setup_host_request( 'wordpress.org', '/' );
    190         $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
    191         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     171        $this->assertEquals( self::$site_ids['wordpress.org/'], $current_blog->blog_id );
     172        $this->assertEquals( self::$network_ids['wordpress.org/'], $current_blog->site_id );
    192173
    193174        $this->_setup_host_request( 'wordpress.org', '/2014/04/23/hello-world/' );
    194         $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
    195         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     175        $this->assertEquals( self::$site_ids['wordpress.org/'], $current_blog->blog_id );
     176        $this->assertEquals( self::$network_ids['wordpress.org/'], $current_blog->site_id );
    196177
    197178        $this->_setup_host_request( 'wordpress.org', '/sample-page/' );
    198         $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
    199         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     179        $this->assertEquals( self::$site_ids['wordpress.org/'], $current_blog->blog_id );
     180        $this->assertEquals( self::$network_ids['wordpress.org/'], $current_blog->site_id );
    200181
    201182        $this->_setup_host_request( 'wordpress.org', '/?p=1' );
    202         $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
    203         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     183        $this->assertEquals( self::$site_ids['wordpress.org/'], $current_blog->blog_id );
     184        $this->assertEquals( self::$network_ids['wordpress.org/'], $current_blog->site_id );
    204185
    205186        $this->_setup_host_request( 'wordpress.org', '/wp-admin/' );
    206         $this->assertEquals( $ids['wordpress.org/'], $current_blog->blog_id );
    207         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     187        $this->assertEquals( self::$site_ids['wordpress.org/'], $current_blog->blog_id );
     188        $this->assertEquals( self::$network_ids['wordpress.org/'], $current_blog->site_id );
    208189
    209190        $this->_setup_host_request( 'wordpress.org', '/foo/' );
    210         $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
    211         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     191        $this->assertEquals( self::$site_ids['wordpress.org/foo/'], $current_blog->blog_id );
     192        $this->assertEquals( self::$network_ids['wordpress.org/'], $current_blog->site_id );
    212193
    213194        $this->_setup_host_request( 'wordpress.org', '/FOO/' );
    214         $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
    215         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     195        $this->assertEquals( self::$site_ids['wordpress.org/foo/'], $current_blog->blog_id );
     196        $this->assertEquals( self::$network_ids['wordpress.org/'], $current_blog->site_id );
    216197
    217198        $this->_setup_host_request( 'wordpress.org', '/foo/2014/04/23/hello-world/' );
    218         $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
    219         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     199        $this->assertEquals( self::$site_ids['wordpress.org/foo/'], $current_blog->blog_id );
     200        $this->assertEquals( self::$network_ids['wordpress.org/'], $current_blog->site_id );
    220201
    221202        $this->_setup_host_request( 'wordpress.org', '/foo/sample-page/' );
    222         $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
    223         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     203        $this->assertEquals( self::$site_ids['wordpress.org/foo/'], $current_blog->blog_id );
     204        $this->assertEquals( self::$network_ids['wordpress.org/'], $current_blog->site_id );
    224205
    225206        $this->_setup_host_request( 'wordpress.org', '/foo/?p=1' );
    226         $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
    227         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     207        $this->assertEquals( self::$site_ids['wordpress.org/foo/'], $current_blog->blog_id );
     208        $this->assertEquals( self::$network_ids['wordpress.org/'], $current_blog->site_id );
    228209
    229210        $this->_setup_host_request( 'wordpress.org', '/foo/wp-admin/' );
    230         $this->assertEquals( $ids['wordpress.org/foo/'], $current_blog->blog_id );
    231         $this->assertEquals( $network_ids['wordpress.org/'], $current_blog->site_id );
     211        $this->assertEquals( self::$site_ids['wordpress.org/foo/'], $current_blog->blog_id );
     212        $this->assertEquals( self::$network_ids['wordpress.org/'], $current_blog->site_id );
    232213
    233214        // @todo not currently passing.
     
    237218
    238219        $this->_setup_host_request( 'make.wordpress.org', '/' );
    239         $this->assertEquals( $ids['make.wordpress.org/'], $current_blog->blog_id );
    240         $this->assertEquals( $network_ids['make.wordpress.org/'], $current_blog->site_id );
     220        $this->assertEquals( self::$site_ids['make.wordpress.org/'], $current_blog->blog_id );
     221        $this->assertEquals( self::$network_ids['make.wordpress.org/'], $current_blog->site_id );
    241222
    242223        $this->_setup_host_request( 'make.wordpress.org', '/foo/' );
    243         $this->assertEquals( $ids['make.wordpress.org/foo/'], $current_blog->blog_id );
    244         $this->assertEquals( $network_ids['make.wordpress.org/'], $current_blog->site_id );
     224        $this->assertEquals( self::$site_ids['make.wordpress.org/foo/'], $current_blog->blog_id );
     225        $this->assertEquals( self::$network_ids['make.wordpress.org/'], $current_blog->site_id );
    245226
    246227        // Request the original tests domain and path to unpollute the stack.
Note: See TracChangeset for help on using the changeset viewer.