Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (8 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

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

    r40342 r42343  
    33if ( is_multisite() ) :
    44
    5 /**
    6  * Tests specific to the bootstrap process of Multisite.
    7  *
    8  * @group ms-bootstrap
    9  * @group multisite
    10  */
    11 class Tests_Multisite_Bootstrap extends WP_UnitTestCase {
    12     protected static $network_ids;
    13     protected static $site_ids;
    14 
    15     public static function wpSetUpBeforeClass( $factory ) {
    16         self::$network_ids = array(
    17             'wordpress.org/'         => array( 'domain' => 'wordpress.org', 'path' => '/' ),
    18             'make.wordpress.org/'    => array( 'domain' => 'make.wordpress.org', 'path' => '/' ),
    19             'wordpress.org/one/'     => array( 'domain' => 'wordpress.org', 'path' => '/one/' ),
    20             'wordpress.org/one/b/'   => array( 'domain' => 'wordpress.org', 'path' => '/one/b/' ),
    21             'wordpress.net/'         => array( 'domain' => 'wordpress.net', 'path' => '/' ),
    22             'www.wordpress.net/'     => array( 'domain' => 'www.wordpress.net', 'path' => '/' ),
    23             'www.wordpress.net/two/' => array( 'domain' => 'www.wordpress.net', 'path' => '/two/' ),
    24             'wordpress.net/three/'   => array( 'domain' => 'wordpress.net', 'path' => '/three/' ),
    25         );
    26 
    27         foreach ( self::$network_ids as &$id ) {
    28             $id = $factory->network->create( $id );
    29         }
    30         unset( $id );
    31 
    32         self::$site_ids = array(
    33             'wordpress.org/'              => array( 'domain' => 'wordpress.org',      'path' => '/',         'site_id' => self::$network_ids['wordpress.org/'] ),
    34             'wordpress.org/foo/'          => array( 'domain' => 'wordpress.org',      'path' => '/foo/',     'site_id' => self::$network_ids['wordpress.org/'] ),
    35             'wordpress.org/foo/bar/'      => array( 'domain' => 'wordpress.org',      'path' => '/foo/bar/', 'site_id' => self::$network_ids['wordpress.org/'] ),
    36             'make.wordpress.org/'         => array( 'domain' => 'make.wordpress.org', 'path' => '/',         'site_id' => self::$network_ids['make.wordpress.org/'] ),
    37             'make.wordpress.org/foo/'     => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/',     'site_id' => self::$network_ids['make.wordpress.org/'] ),
    38             'www.w.org/'                  => array( 'domain' => 'www.w.org',          'path' => '/' ),
    39             'www.w.org/foo/'              => array( 'domain' => 'www.w.org',          'path' => '/foo/' ),
    40             'www.w.org/foo/bar/'          => array( 'domain' => 'www.w.org',          'path' => '/foo/bar/' ),
    41         );
    42 
    43         foreach ( self::$site_ids as &$id ) {
    44             $id = $factory->blog->create( $id );
    45         }
    46         unset( $id );
     5    /**
     6     * Tests specific to the bootstrap process of Multisite.
     7     *
     8     * @group ms-bootstrap
     9     * @group multisite
     10     */
     11    class Tests_Multisite_Bootstrap extends WP_UnitTestCase {
     12        protected static $network_ids;
     13        protected static $site_ids;
     14
     15        public static function wpSetUpBeforeClass( $factory ) {
     16            self::$network_ids = array(
     17                'wordpress.org/'         => array(
     18                    'domain' => 'wordpress.org',
     19                    'path'   => '/',
     20                ),
     21                'make.wordpress.org/'    => array(
     22                    'domain' => 'make.wordpress.org',
     23                    'path'   => '/',
     24                ),
     25                'wordpress.org/one/'     => array(
     26                    'domain' => 'wordpress.org',
     27                    'path'   => '/one/',
     28                ),
     29                'wordpress.org/one/b/'   => array(
     30                    'domain' => 'wordpress.org',
     31                    'path'   => '/one/b/',
     32                ),
     33                'wordpress.net/'         => array(
     34                    'domain' => 'wordpress.net',
     35                    'path'   => '/',
     36                ),
     37                'www.wordpress.net/'     => array(
     38                    'domain' => 'www.wordpress.net',
     39                    'path'   => '/',
     40                ),
     41                'www.wordpress.net/two/' => array(
     42                    'domain' => 'www.wordpress.net',
     43                    'path'   => '/two/',
     44                ),
     45                'wordpress.net/three/'   => array(
     46                    'domain' => 'wordpress.net',
     47                    'path'   => '/three/',
     48                ),
     49            );
     50
     51            foreach ( self::$network_ids as &$id ) {
     52                $id = $factory->network->create( $id );
     53            }
     54            unset( $id );
     55
     56            self::$site_ids = array(
     57                'wordpress.org/'          => array(
     58                    'domain'  => 'wordpress.org',
     59                    'path'    => '/',
     60                    'site_id' => self::$network_ids['wordpress.org/'],
     61                ),
     62                'wordpress.org/foo/'      => array(
     63                    'domain'  => 'wordpress.org',
     64                    'path'    => '/foo/',
     65                    'site_id' => self::$network_ids['wordpress.org/'],
     66                ),
     67                'wordpress.org/foo/bar/'  => array(
     68                    'domain'  => 'wordpress.org',
     69                    'path'    => '/foo/bar/',
     70                    'site_id' => self::$network_ids['wordpress.org/'],
     71                ),
     72                'make.wordpress.org/'     => array(
     73                    'domain'  => 'make.wordpress.org',
     74                    'path'    => '/',
     75                    'site_id' => self::$network_ids['make.wordpress.org/'],
     76                ),
     77                'make.wordpress.org/foo/' => array(
     78                    'domain'  => 'make.wordpress.org',
     79                    'path'    => '/foo/',
     80                    'site_id' => self::$network_ids['make.wordpress.org/'],
     81                ),
     82                'www.w.org/'              => array(
     83                    'domain' => 'www.w.org',
     84                    'path'   => '/',
     85                ),
     86                'www.w.org/foo/'          => array(
     87                    'domain' => 'www.w.org',
     88                    'path'   => '/foo/',
     89                ),
     90                'www.w.org/foo/bar/'      => array(
     91                    'domain' => 'www.w.org',
     92                    'path'   => '/foo/bar/',
     93                ),
     94            );
     95
     96            foreach ( self::$site_ids as &$id ) {
     97                $id = $factory->blog->create( $id );
     98            }
     99            unset( $id );
     100        }
     101
     102        public static function wpTearDownAfterClass() {
     103            global $wpdb;
     104
     105            foreach ( self::$site_ids as $id ) {
     106                wpmu_delete_blog( $id, true );
     107            }
     108
     109            foreach ( self::$network_ids as $id ) {
     110                $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
     111                $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
     112            }
     113
     114            wp_update_network_site_counts();
     115        }
     116
     117        /**
     118         * @ticket 27003
     119         * @dataProvider data_get_network_by_path
     120         *
     121         * @param string $expected_key The array key associated with expected data for the test.
     122         * @param string $domain       The requested domain.
     123         * @param string $path         The requested path.
     124         * @param string $message      The message to pass for failed tests.
     125         */
     126        function test_get_network_by_path( $expected_key, $domain, $path, $message ) {
     127            $network = get_network_by_path( $domain, $path );
     128            $this->assertEquals( self::$network_ids[ $expected_key ], $network->id, $message );
     129        }
     130
     131        public function data_get_network_by_path() {
     132            return array(
     133                array( 'wordpress.org/', 'wordpress.org', '/', 'A standard domain and path request should work.' ),
     134                array( 'wordpress.net/', 'wordpress.net', '/notapath/', 'A missing path on a top level domain should find the correct network.' ),
     135                array( 'www.wordpress.net/', 'www.wordpress.net', '/notapath/', 'A missing path should find the correct network.' ),
     136                array( 'wordpress.org/one/', 'www.wordpress.org', '/one/', 'Should find the path despite the www.' ),
     137                array( 'wordpress.org/one/', 'wordpress.org', '/one/page/', 'A request with two path segments should find the correct network.' ),
     138                array( 'wordpress.org/one/b/', 'wordpress.org', '/one/b/', 'A request with two valid path segments should find the correct network.' ),
     139                array( 'wordpress.org/', 'site1.wordpress.org', '/one/', 'Should not find path because domains do not match.' ),
     140                array( 'wordpress.net/three/', 'wordpress.net', '/three/', 'A network can have a path.' ),
     141                array( 'www.wordpress.net/two/', 'www.wordpress.net', '/two/', 'A www network with a path can coexist with a non-www network.' ),
     142                array( 'wordpress.net/', 'site1.wordpress.net', '/notapath/', 'An invalid subdomain should find the top level network domain.' ),
     143                array( 'wordpress.net/', 'site1.wordpress.net', '/three/', 'An invalid subdomain and path should find the top level network domain.' ),
     144                array( 'wordpress.net/', 'x.y.wordpress.net', '/', 'An invalid two level subdomain should find the top level network domain.' ),
     145            );
     146        }
     147
     148        /**
     149         * @ticket 37217
     150         * @dataProvider data_get_network_by_path_with_zero_path_segments
     151         *
     152         * @param string $expected_key The array key associated with expected data for the test.
     153         * @param string $domain       The requested domain.
     154         * @param string $path         The requested path.
     155         * @param string $message      The message to pass for failed tests.
     156         */
     157        public function test_get_network_by_path_with_zero_path_segments( $expected_key, $domain, $path, $message ) {
     158            add_filter( 'network_by_path_segments_count', '__return_zero' );
     159
     160            $network = get_network_by_path( $domain, $path );
     161
     162            remove_filter( 'network_by_path_segments_count', '__return_zero' );
     163
     164            $this->assertEquals( self::$network_ids[ $expected_key ], $network->id, $message );
     165        }
     166
     167        public function data_get_network_by_path_with_zero_path_segments() {
     168            return array(
     169                array( 'wordpress.org/', 'wordpress.org', '/', 'A standard domain and path request should work.' ),
     170                array( 'wordpress.net/', 'wordpress.net', '/notapath/', 'A network matching a top level domain should be found regardless of path.' ),
     171                array( 'www.wordpress.net/', 'www.wordpress.net', '/notapath/', 'A network matching a domain should be found regardless of path.' ),
     172                array( 'wordpress.org/', 'www.wordpress.org', '/one/', 'Should find the network despite the www and regardless of path.' ),
     173                array( 'wordpress.org/', 'site1.wordpress.org', '/one/', 'Should find the network with the corresponding top level domain regardless of path.' ),
     174                array( 'www.wordpress.net/', 'www.wordpress.net', '/two/', 'A www network can coexist with a non-www network.' ),
     175                array( 'make.wordpress.org/', 'make.wordpress.org', '/notapath/', 'A subdomain network should be found regardless of path.' ),
     176                array( 'wordpress.net/', 'x.y.wordpress.net', '/', 'An invalid two level subdomain should find the top level network domain.' ),
     177            );
     178        }
     179
     180        /**
     181         * Even if a matching network is available, it should not match if the the filtered
     182         * value for network path segments is fewer than the number of paths passed.
     183         */
     184        public function test_get_network_by_path_with_forced_single_path_segment_returns_single_path_network() {
     185            add_filter( 'network_by_path_segments_count', array( $this, 'filter_network_path_segments' ) );
     186            $network = get_network_by_path( 'wordpress.org', '/one/b/' );
     187            remove_filter( 'network_by_path_segments_count', array( $this, 'filter_network_path_segments' ) );
     188
     189            $this->assertEquals( self::$network_ids['wordpress.org/one/'], $network->id );
     190        }
     191
     192        public function filter_network_path_segments() {
     193            return 1;
     194        }
     195
     196        /**
     197         * @ticket 27003
     198         * @ticket 27927
     199         * @dataProvider data_get_site_by_path
     200         *
     201         * @param string $expected_key The array key associated with expected data for the test.
     202         * @param string $domain       The requested domain.
     203         * @param string $path         The requested path.
     204         * @param int    $segments     Optional. Number of segments to use in `get_site_by_path()`.
     205         */
     206        public function test_get_site_by_path( $expected_key, $domain, $path, $segments = null ) {
     207            $site = get_site_by_path( $domain, $path, $segments );
     208
     209            if ( $expected_key ) {
     210                $this->assertEquals( self::$site_ids[ $expected_key ], $site->blog_id );
     211            } else {
     212                $this->assertFalse( $site );
     213            }
     214        }
     215
     216        public function data_get_site_by_path() {
     217            return array(
     218                array( 'wordpress.org/', 'wordpress.org', '/notapath/' ),
     219                array( 'wordpress.org/', 'www.wordpress.org', '/notapath/' ),
     220                array( 'wordpress.org/foo/bar/', 'wordpress.org', '/foo/bar/baz/' ),
     221                array( 'wordpress.org/foo/bar/', 'www.wordpress.org', '/foo/bar/baz/' ),
     222                array( 'wordpress.org/foo/bar/', 'wordpress.org', '/foo/bar/baz/', 3 ),
     223                array( 'wordpress.org/foo/bar/', 'www.wordpress.org', '/foo/bar/baz/', 3 ),
     224                array( 'wordpress.org/foo/bar/', 'wordpress.org', '/foo/bar/baz/', 2 ),
     225                array( 'wordpress.org/foo/bar/', 'www.wordpress.org', '/foo/bar/baz/', 2 ),
     226                array( 'wordpress.org/foo/', 'wordpress.org', '/foo/bar/baz/', 1 ),
     227                array( 'wordpress.org/foo/', 'www.wordpress.org', '/foo/bar/baz/', 1 ),
     228                array( 'wordpress.org/', 'wordpress.org', '/', 0 ),
     229                array( 'wordpress.org/', 'www.wordpress.org', '/', 0 ),
     230                array( 'make.wordpress.org/foo/', 'make.wordpress.org', '/foo/bar/baz/quz/', 4 ),
     231                array( 'make.wordpress.org/foo/', 'www.make.wordpress.org', '/foo/bar/baz/quz/', 4 ),
     232                array( 'www.w.org/', 'www.w.org', '/', 0 ),
     233                array( 'www.w.org/', 'www.w.org', '/notapath' ),
     234                array( 'www.w.org/foo/bar/', 'www.w.org', '/foo/bar/baz/' ),
     235                array( 'www.w.org/foo/', 'www.w.org', '/foo/bar/baz/', 1 ),
     236
     237                // A site installed with www will not be found by the root domain.
     238                array( false, 'w.org', '/' ),
     239                array( false, 'w.org', '/notapath/' ),
     240                array( false, 'w.org', '/foo/bar/baz/' ),
     241                array( false, 'w.org', '/foo/bar/baz/', 1 ),
     242
     243                // A site will not be found by its root domain when an invalid subdomain is requested.
     244                array( false, 'invalid.wordpress.org', '/' ),
     245                array( false, 'invalid.wordpress.org', '/foo/bar/' ),
     246            );
     247        }
     248
     249        /**
     250         * @ticket 27884
     251         * @dataProvider data_multisite_bootstrap
     252         *
     253         * @param string $site_key    The array key associated with the expected site for the test.
     254         * @param string $network_key The array key associated with the expected network for the test.
     255         * @param string $domain      The requested domain.
     256         * @param string $path        The requested path.
     257         */
     258        function test_multisite_bootstrap( $site_key, $network_key, $domain, $path ) {
     259            global $current_blog;
     260
     261            $expected = array(
     262                'network_id' => self::$network_ids[ $network_key ],
     263                'site_id'    => self::$site_ids[ $site_key ],
     264            );
     265
     266            ms_load_current_site_and_network( $domain, $path );
     267            $actual = array(
     268                'network_id' => $current_blog->site_id,
     269                'site_id'    => $current_blog->blog_id,
     270            );
     271            ms_load_current_site_and_network( WP_TESTS_DOMAIN, '/' );
     272
     273            $this->assertEqualSetsWithIndex( $expected, $actual );
     274        }
     275
     276        public function data_multisite_bootstrap() {
     277            return array(
     278                array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/' ),
     279                array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/2014/04/23/hello-world/' ),
     280                array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/sample-page/' ),
     281                array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/?p=1' ),
     282                array( 'wordpress.org/', 'wordpress.org/', 'wordpress.org', '/wp-admin/' ),
     283                array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/' ),
     284                array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/FOO/' ),
     285                array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/2014/04/23/hello-world/' ),
     286                array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/sample-page/' ),
     287                array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/?p=1' ),
     288                array( 'wordpress.org/foo/', 'wordpress.org/', 'wordpress.org', '/foo/wp-admin/' ),
     289                array( 'make.wordpress.org/', 'make.wordpress.org/', 'make.wordpress.org', '/' ),
     290                array( 'make.wordpress.org/foo/', 'make.wordpress.org/', 'make.wordpress.org', '/foo/' ),
     291            );
     292        }
     293
     294        /**
     295         * @ticket 27884
     296         */
     297        public function test_multisite_bootstrap_additional_path_segments() {
     298            global $current_blog;
     299
     300            $expected = array(
     301                'network_id' => self::$network_ids['wordpress.org/'],
     302                'site_id'    => self::$site_ids['wordpress.org/foo/bar/'],
     303            );
     304            add_filter( 'site_by_path_segments_count', array( $this, 'filter_path_segments_to_two' ) );
     305            ms_load_current_site_and_network( 'wordpress.org', '/foo/bar/' );
     306            $actual = array(
     307                'network_id' => $current_blog->site_id,
     308                'site_id'    => $current_blog->blog_id,
     309            );
     310            remove_filter( 'site_by_path_segments_count', array( $this, 'filter_path_segments_to_two' ) );
     311            ms_load_current_site_and_network( WP_TESTS_DOMAIN, '/' );
     312
     313            $this->assertEqualSetsWithIndex( $expected, $actual );
     314        }
     315
     316        /**
     317         * @ticket 37053
     318         */
     319        public function test_get_site_by_path_returns_wp_site() {
     320            add_filter( 'pre_get_site_by_path', array( $this, 'filter_pre_get_site_by_path' ), 10, 3 );
     321
     322            $site = get_site_by_path( 'example.com', '/foo/' );
     323
     324            remove_filter( 'pre_get_site_by_path', array( $this, 'filter_pre_get_site_by_path' ), 10 );
     325
     326            $this->assertInstanceOf( 'WP_Site', $site );
     327        }
     328
     329        public function filter_path_segments_to_two() {
     330            return 2;
     331        }
     332
     333        public function filter_pre_get_site_by_path( $site, $domain, $path ) {
     334            $site          = new stdClass();
     335            $site->blog_id = 100;
     336            $site->domain  = $domain;
     337            $site->path    = $path;
     338            $site->site_id = 1;
     339
     340            return $site;
     341        }
    47342    }
    48343
    49     public static function wpTearDownAfterClass() {
    50         global $wpdb;
    51 
    52         foreach( self::$site_ids as $id ) {
    53             wpmu_delete_blog( $id, true );
    54         }
    55 
    56         foreach( self::$network_ids as $id ) {
    57             $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
    58             $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
    59         }
    60 
    61         wp_update_network_site_counts();
    62     }
    63 
    64     /**
    65      * @ticket 27003
    66      * @dataProvider data_get_network_by_path
    67      *
    68      * @param string $expected_key The array key associated with expected data for the test.
    69      * @param string $domain       The requested domain.
    70      * @param string $path         The requested path.
    71      * @param string $message      The message to pass for failed tests.
    72      */
    73     function test_get_network_by_path( $expected_key, $domain, $path, $message ) {
    74         $network = get_network_by_path( $domain, $path );
    75         $this->assertEquals( self::$network_ids[ $expected_key ], $network->id, $message );
    76     }
    77 
    78     public function data_get_network_by_path() {
    79         return array(
    80             array( 'wordpress.org/',         'wordpress.org',       '/',          'A standard domain and path request should work.' ),
    81             array( 'wordpress.net/',         'wordpress.net',       '/notapath/', 'A missing path on a top level domain should find the correct network.' ),
    82             array( 'www.wordpress.net/',     'www.wordpress.net',   '/notapath/', 'A missing path should find the correct network.' ),
    83             array( 'wordpress.org/one/',     'www.wordpress.org',   '/one/',      'Should find the path despite the www.' ),
    84             array( 'wordpress.org/one/',     'wordpress.org',       '/one/page/', 'A request with two path segments should find the correct network.' ),
    85             array( 'wordpress.org/one/b/',   'wordpress.org',       '/one/b/',    'A request with two valid path segments should find the correct network.' ),
    86             array( 'wordpress.org/',         'site1.wordpress.org', '/one/',      'Should not find path because domains do not match.' ),
    87             array( 'wordpress.net/three/',   'wordpress.net',       '/three/',    'A network can have a path.' ),
    88             array( 'www.wordpress.net/two/', 'www.wordpress.net',   '/two/',      'A www network with a path can coexist with a non-www network.' ),
    89             array( 'wordpress.net/',         'site1.wordpress.net', '/notapath/', 'An invalid subdomain should find the top level network domain.' ),
    90             array( 'wordpress.net/',         'site1.wordpress.net', '/three/',    'An invalid subdomain and path should find the top level network domain.' ),
    91             array( 'wordpress.net/',         'x.y.wordpress.net',   '/',          'An invalid two level subdomain should find the top level network domain.' ),
    92         );
    93     }
    94 
    95     /**
    96      * @ticket 37217
    97      * @dataProvider data_get_network_by_path_with_zero_path_segments
    98      *
    99      * @param string $expected_key The array key associated with expected data for the test.
    100      * @param string $domain       The requested domain.
    101      * @param string $path         The requested path.
    102      * @param string $message      The message to pass for failed tests.
    103      */
    104     public function test_get_network_by_path_with_zero_path_segments( $expected_key, $domain, $path, $message ) {
    105         add_filter( 'network_by_path_segments_count', '__return_zero' );
    106 
    107         $network = get_network_by_path( $domain, $path );
    108 
    109         remove_filter( 'network_by_path_segments_count', '__return_zero' );
    110 
    111         $this->assertEquals( self::$network_ids[ $expected_key ], $network->id, $message );
    112     }
    113 
    114     public function data_get_network_by_path_with_zero_path_segments() {
    115         return array(
    116             array( 'wordpress.org/',         'wordpress.org',       '/',          'A standard domain and path request should work.' ),
    117             array( 'wordpress.net/',         'wordpress.net',       '/notapath/', 'A network matching a top level domain should be found regardless of path.' ),
    118             array( 'www.wordpress.net/',     'www.wordpress.net',   '/notapath/', 'A network matching a domain should be found regardless of path.' ),
    119             array( 'wordpress.org/',         'www.wordpress.org',   '/one/',      'Should find the network despite the www and regardless of path.' ),
    120             array( 'wordpress.org/',         'site1.wordpress.org', '/one/',      'Should find the network with the corresponding top level domain regardless of path.' ),
    121             array( 'www.wordpress.net/',     'www.wordpress.net',   '/two/',      'A www network can coexist with a non-www network.' ),
    122             array( 'make.wordpress.org/',    'make.wordpress.org',  '/notapath/', 'A subdomain network should be found regardless of path.' ),
    123             array( 'wordpress.net/',         'x.y.wordpress.net',   '/',          'An invalid two level subdomain should find the top level network domain.' ),
    124         );
    125     }
    126 
    127     /**
    128      * Even if a matching network is available, it should not match if the the filtered
    129      * value for network path segments is fewer than the number of paths passed.
    130      */
    131     public function test_get_network_by_path_with_forced_single_path_segment_returns_single_path_network() {
    132         add_filter( 'network_by_path_segments_count', array( $this, 'filter_network_path_segments' ) );
    133         $network = get_network_by_path( 'wordpress.org', '/one/b/' );
    134         remove_filter( 'network_by_path_segments_count', array( $this, 'filter_network_path_segments' ) );
    135 
    136         $this->assertEquals( self::$network_ids[ 'wordpress.org/one/' ], $network->id );
    137     }
    138 
    139     public function filter_network_path_segments() {
    140         return 1;
    141     }
    142 
    143     /**
    144      * @ticket 27003
    145      * @ticket 27927
    146      * @dataProvider data_get_site_by_path
    147      *
    148      * @param string $expected_key The array key associated with expected data for the test.
    149      * @param string $domain       The requested domain.
    150      * @param string $path         The requested path.
    151      * @param int    $segments     Optional. Number of segments to use in `get_site_by_path()`.
    152      */
    153     public function test_get_site_by_path( $expected_key, $domain, $path, $segments = null ) {
    154         $site = get_site_by_path( $domain, $path, $segments );
    155 
    156         if ( $expected_key ) {
    157             $this->assertEquals( self::$site_ids[ $expected_key ], $site->blog_id );
    158         } else {
    159             $this->assertFalse( $site );
    160         }
    161     }
    162 
    163     public function data_get_site_by_path() {
    164         return array(
    165             array( 'wordpress.org/',          'wordpress.org',          '/notapath/' ),
    166             array( 'wordpress.org/',          'www.wordpress.org',      '/notapath/' ),
    167             array( 'wordpress.org/foo/bar/',  'wordpress.org',          '/foo/bar/baz/' ),
    168             array( 'wordpress.org/foo/bar/',  'www.wordpress.org',      '/foo/bar/baz/' ),
    169             array( 'wordpress.org/foo/bar/',  'wordpress.org',          '/foo/bar/baz/',     3 ),
    170             array( 'wordpress.org/foo/bar/',  'www.wordpress.org',      '/foo/bar/baz/',     3 ),
    171             array( 'wordpress.org/foo/bar/',  'wordpress.org',          '/foo/bar/baz/',     2 ),
    172             array( 'wordpress.org/foo/bar/',  'www.wordpress.org',      '/foo/bar/baz/',     2 ),
    173             array( 'wordpress.org/foo/',      'wordpress.org',          '/foo/bar/baz/',     1 ),
    174             array( 'wordpress.org/foo/',      'www.wordpress.org',      '/foo/bar/baz/',     1 ),
    175             array( 'wordpress.org/',          'wordpress.org',          '/',                 0 ),
    176             array( 'wordpress.org/',          'www.wordpress.org',      '/',                 0 ),
    177             array( 'make.wordpress.org/foo/', 'make.wordpress.org',     '/foo/bar/baz/quz/', 4 ),
    178             array( 'make.wordpress.org/foo/', 'www.make.wordpress.org', '/foo/bar/baz/quz/', 4 ),
    179             array( 'www.w.org/',              'www.w.org',              '/',                 0 ),
    180             array( 'www.w.org/',              'www.w.org',              '/notapath' ),
    181             array( 'www.w.org/foo/bar/',      'www.w.org',              '/foo/bar/baz/' ),
    182             array( 'www.w.org/foo/',          'www.w.org',              '/foo/bar/baz/',     1 ),
    183 
    184             // A site installed with www will not be found by the root domain.
    185             array( false, 'w.org', '/' ),
    186             array( false, 'w.org', '/notapath/' ),
    187             array( false, 'w.org', '/foo/bar/baz/' ),
    188             array( false, 'w.org', '/foo/bar/baz/', 1 ),
    189 
    190             // A site will not be found by its root domain when an invalid subdomain is requested.
    191             array( false, 'invalid.wordpress.org', '/' ),
    192             array( false, 'invalid.wordpress.org', '/foo/bar/' ),
    193         );
    194     }
    195 
    196     /**
    197      * @ticket 27884
    198      * @dataProvider data_multisite_bootstrap
    199      *
    200      * @param string $site_key    The array key associated with the expected site for the test.
    201      * @param string $network_key The array key associated with the expected network for the test.
    202      * @param string $domain      The requested domain.
    203      * @param string $path        The requested path.
    204      */
    205     function test_multisite_bootstrap( $site_key, $network_key, $domain, $path ) {
    206         global $current_blog;
    207 
    208         $expected = array(
    209             'network_id' => self::$network_ids[ $network_key ],
    210             'site_id' => self::$site_ids[ $site_key ],
    211         );
    212 
    213         ms_load_current_site_and_network( $domain, $path );
    214         $actual = array(
    215             'network_id' => $current_blog->site_id,
    216             'site_id' => $current_blog->blog_id,
    217         );
    218         ms_load_current_site_and_network( WP_TESTS_DOMAIN, '/' );
    219 
    220         $this->assertEqualSetsWithIndex( $expected, $actual );
    221     }
    222 
    223     public function data_multisite_bootstrap() {
    224         return array(
    225             array( 'wordpress.org/',          'wordpress.org/',      'wordpress.org',      '/' ),
    226             array( 'wordpress.org/',          'wordpress.org/',      'wordpress.org',      '/2014/04/23/hello-world/' ),
    227             array( 'wordpress.org/',          'wordpress.org/',      'wordpress.org',      '/sample-page/' ),
    228             array( 'wordpress.org/',          'wordpress.org/',      'wordpress.org',      '/?p=1' ),
    229             array( 'wordpress.org/',          'wordpress.org/',      'wordpress.org',      '/wp-admin/' ),
    230             array( 'wordpress.org/foo/',      'wordpress.org/',      'wordpress.org',      '/foo/' ),
    231             array( 'wordpress.org/foo/',      'wordpress.org/',      'wordpress.org',      '/FOO/' ),
    232             array( 'wordpress.org/foo/',      'wordpress.org/',      'wordpress.org',      '/foo/2014/04/23/hello-world/' ),
    233             array( 'wordpress.org/foo/',      'wordpress.org/',      'wordpress.org',      '/foo/sample-page/' ),
    234             array( 'wordpress.org/foo/',      'wordpress.org/',      'wordpress.org',      '/foo/?p=1' ),
    235             array( 'wordpress.org/foo/',      'wordpress.org/',      'wordpress.org',      '/foo/wp-admin/' ),
    236             array( 'make.wordpress.org/',     'make.wordpress.org/', 'make.wordpress.org', '/' ),
    237             array( 'make.wordpress.org/foo/', 'make.wordpress.org/', 'make.wordpress.org', '/foo/' ),
    238         );
    239     }
    240 
    241     /**
    242      * @ticket 27884
    243      */
    244     public function test_multisite_bootstrap_additional_path_segments() {
    245         global $current_blog;
    246 
    247         $expected = array(
    248             'network_id' => self::$network_ids['wordpress.org/'],
    249             'site_id'    => self::$site_ids['wordpress.org/foo/bar/'],
    250         );
    251         add_filter( 'site_by_path_segments_count', array( $this, 'filter_path_segments_to_two' ) );
    252         ms_load_current_site_and_network( 'wordpress.org', '/foo/bar/' );
    253         $actual = array(
    254             'network_id' => $current_blog->site_id,
    255             'site_id' => $current_blog->blog_id,
    256         );
    257         remove_filter( 'site_by_path_segments_count', array( $this, 'filter_path_segments_to_two' ) );
    258         ms_load_current_site_and_network( WP_TESTS_DOMAIN, '/' );
    259 
    260         $this->assertEqualSetsWithIndex( $expected, $actual );
    261     }
    262 
    263     /**
    264      * @ticket 37053
    265      */
    266     public function test_get_site_by_path_returns_wp_site() {
    267         add_filter( 'pre_get_site_by_path', array( $this, 'filter_pre_get_site_by_path' ), 10, 3 );
    268 
    269         $site = get_site_by_path( 'example.com', '/foo/' );
    270 
    271         remove_filter( 'pre_get_site_by_path', array( $this, 'filter_pre_get_site_by_path' ), 10 );
    272 
    273         $this->assertInstanceOf( 'WP_Site', $site );
    274     }
    275 
    276     public function filter_path_segments_to_two() {
    277         return 2;
    278     }
    279 
    280     public function filter_pre_get_site_by_path( $site, $domain, $path ) {
    281         $site = new stdClass();
    282         $site->blog_id = 100;
    283         $site->domain = $domain;
    284         $site->path = $path;
    285         $site->site_id = 1;
    286 
    287         return $site;
    288     }
    289 }
    290 
    291344endif;
Note: See TracChangeset for help on using the changeset viewer.