Make WordPress Core


Ignore:
Timestamp:
04/09/2025 01:29:39 PM (5 weeks ago)
Author:
SergeyBiryukov
Message:

Tests: Use the ms-required group where appropriate.

This replaces the if ( is_multisite() ) conditional wrapping entire test classes with the ms-required group for more consistency across the test suite.

Follow-up to [40520].

See #63167.

File:
1 edited

Legend:

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

    r57750 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
    4 
    5     /**
    6      * Test network query functionality in multisite.
    7      *
    8      * @group ms-network
    9      * @group ms-network-query
    10      * @group multisite
    11      */
    12     class Tests_Multisite_wpNetworkQuery extends WP_UnitTestCase {
    13         protected static $network_ids;
    14 
    15         public static function wpSetUpBeforeClass( WP_UnitTest_Factory $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                 'www.wordpress.net/'  => array(
    26                     'domain' => 'www.wordpress.net',
    27                     'path'   => '/',
    28                 ),
    29                 'www.w.org/foo/'      => array(
    30                     'domain' => 'www.w.org',
    31                     'path'   => '/foo/',
    32                 ),
    33             );
    34 
    35             foreach ( self::$network_ids as &$id ) {
    36                 $id = $factory->network->create( $id );
    37             }
    38             unset( $id );
     3/**
     4 * Test network query functionality in multisite.
     5 *
     6 * @group ms-network
     7 * @group ms-network-query
     8 * @group ms-required
     9 * @group multisite
     10 */
     11class Tests_Multisite_wpNetworkQuery extends WP_UnitTestCase {
     12    protected static $network_ids;
     13
     14    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     15        self::$network_ids = array(
     16            'wordpress.org/'      => array(
     17                'domain' => 'wordpress.org',
     18                'path'   => '/',
     19            ),
     20            'make.wordpress.org/' => array(
     21                'domain' => 'make.wordpress.org',
     22                'path'   => '/',
     23            ),
     24            'www.wordpress.net/'  => array(
     25                'domain' => 'www.wordpress.net',
     26                'path'   => '/',
     27            ),
     28            'www.w.org/foo/'      => array(
     29                'domain' => 'www.w.org',
     30                'path'   => '/foo/',
     31            ),
     32        );
     33
     34        foreach ( self::$network_ids as &$id ) {
     35            $id = $factory->network->create( $id );
    3936        }
    40 
    41         public static function wpTearDownAfterClass() {
    42             global $wpdb;
    43 
    44             foreach ( self::$network_ids as $id ) {
    45                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
    46                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
    47             }
     37        unset( $id );
     38    }
     39
     40    public static function wpTearDownAfterClass() {
     41        global $wpdb;
     42
     43        foreach ( self::$network_ids as $id ) {
     44            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
     45            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
    4846        }
    49 
    50         public function test_wp_network_query_by_number() {
    51             $q     = new WP_Network_Query();
    52             $found = $q->query(
    53                 array(
    54                     'fields' => 'ids',
    55                     'number' => 3,
    56                 )
    57             );
    58 
    59             $this->assertCount( 3, $found );
    60         }
    61 
    62         public function test_wp_network_query_by_network__in_with_order() {
    63             $expected = array( self::$network_ids['wordpress.org/'], self::$network_ids['make.wordpress.org/'] );
    64 
    65             $q     = new WP_Network_Query();
    66             $found = $q->query(
    67                 array(
    68                     'fields'      => 'ids',
    69                     'network__in' => $expected,
    70                     'order'       => 'ASC',
    71                 )
    72             );
    73 
    74             $this->assertSame( $expected, $found );
    75 
    76             $found = $q->query(
    77                 array(
    78                     'fields'      => 'ids',
    79                     'network__in' => $expected,
    80                     'order'       => 'DESC',
    81                 )
    82             );
    83 
    84             $this->assertSame( array_reverse( $expected ), $found );
    85         }
    86 
    87         public function test_wp_network_query_by_network__in_with_single_id() {
    88             $expected = array( self::$network_ids['wordpress.org/'] );
    89 
    90             $q     = new WP_Network_Query();
    91             $found = $q->query(
    92                 array(
    93                     'fields'      => 'ids',
    94                     'network__in' => $expected,
    95                 )
    96             );
    97 
    98             $this->assertSameSets( $expected, $found );
    99         }
    100 
    101         public function test_wp_network_query_by_network__in_with_multiple_ids() {
    102             $expected = array( self::$network_ids['wordpress.org/'], self::$network_ids['www.wordpress.net/'] );
    103 
    104             $q     = new WP_Network_Query();
    105             $found = $q->query(
    106                 array(
    107                     'fields'      => 'ids',
    108                     'network__in' => $expected,
    109                 )
    110             );
    111 
    112             $this->assertSameSets( $expected, $found );
    113         }
    114 
    115         public function test_wp_network_query_by_network__in_and_count_with_multiple_ids() {
    116             $expected = array( self::$network_ids['wordpress.org/'], self::$network_ids['make.wordpress.org/'] );
    117 
    118             $q     = new WP_Network_Query();
    119             $found = $q->query(
    120                 array(
    121                     'fields'      => 'ids',
    122                     'count'       => true,
    123                     'network__in' => $expected,
    124                 )
    125             );
    126 
    127             $this->assertSame( 2, $found );
    128         }
    129 
    130         public function test_wp_network_query_by_network__not_in_with_single_id() {
    131             $excluded = array( self::$network_ids['wordpress.org/'] );
    132             $expected = array_diff( self::$network_ids, $excluded );
    133 
    134             // Exclude main network since we don't have control over it here.
    135             $excluded[] = 1;
    136 
    137             $q     = new WP_Network_Query();
    138             $found = $q->query(
    139                 array(
    140                     'fields'          => 'ids',
    141                     'network__not_in' => $excluded,
    142                 )
    143             );
    144 
    145             $this->assertSameSets( $expected, $found );
    146         }
    147 
    148         public function test_wp_network_query_by_network__not_in_with_multiple_ids() {
    149             $excluded = array( self::$network_ids['wordpress.org/'], self::$network_ids['www.w.org/foo/'] );
    150             $expected = array_diff( self::$network_ids, $excluded );
    151 
    152             // Exclude main network since we don't have control over it here.
    153             $excluded[] = 1;
    154 
    155             $q     = new WP_Network_Query();
    156             $found = $q->query(
    157                 array(
    158                     'fields'          => 'ids',
    159                     'network__not_in' => $excluded,
    160                 )
    161             );
    162 
    163             $this->assertSameSets( $expected, $found );
    164         }
    165 
    166         public function test_wp_network_query_by_domain() {
    167             $q     = new WP_Network_Query();
    168             $found = $q->query(
    169                 array(
    170                     'fields' => 'ids',
    171                     'domain' => 'www.w.org',
    172                 )
    173             );
    174 
    175             $expected = array(
    176                 self::$network_ids['www.w.org/foo/'],
    177             );
    178 
    179             $this->assertSameSets( $expected, $found );
    180         }
    181 
    182         public function test_wp_network_query_by_domain__in_with_single_domain() {
    183             $q     = new WP_Network_Query();
    184             $found = $q->query(
    185                 array(
    186                     'fields'     => 'ids',
    187                     'domain__in' => array( 'make.wordpress.org' ),
    188                 )
    189             );
    190 
    191             $expected = array(
    192                 self::$network_ids['make.wordpress.org/'],
    193             );
    194 
    195             $this->assertSameSets( $expected, $found );
    196         }
    197 
    198         public function test_wp_network_query_by_domain__in_with_multiple_domains() {
    199             $q     = new WP_Network_Query();
    200             $found = $q->query(
    201                 array(
    202                     'fields'     => 'ids',
    203                     'domain__in' => array( 'wordpress.org', 'make.wordpress.org' ),
    204                 )
    205             );
    206 
    207             $expected = array(
    208                 self::$network_ids['wordpress.org/'],
    209                 self::$network_ids['make.wordpress.org/'],
    210             );
    211 
    212             $this->assertSameSets( $expected, $found );
    213         }
    214 
    215         public function test_wp_network_query_by_domain__in_with_multiple_domains_and_number() {
    216             $q     = new WP_Network_Query();
    217             $found = $q->query(
    218                 array(
    219                     'fields'     => 'ids',
    220                     'number'     => 1,
    221                     'domain__in' => array( 'wordpress.org', 'make.wordpress.org' ),
    222                 )
    223             );
    224 
    225             $expected = array(
    226                 self::$network_ids['wordpress.org/'],
    227             );
    228 
    229             $this->assertSameSets( $expected, $found );
    230         }
    231 
    232         public function test_wp_network_query_by_domain__in_with_multiple_domains_and_number_and_offset() {
    233             $q     = new WP_Network_Query();
    234             $found = $q->query(
    235                 array(
    236                     'fields'     => 'ids',
    237                     'number'     => 1,
    238                     'offset'     => 1,
    239                     'domain__in' => array( 'wordpress.org', 'make.wordpress.org' ),
    240                 )
    241             );
    242 
    243             $expected = array(
    244                 self::$network_ids['make.wordpress.org/'],
    245             );
    246 
    247             $this->assertSameSets( $expected, $found );
    248         }
    249 
    250         public function test_wp_network_query_by_domain__not_in_with_single_domain() {
    251             $q     = new WP_Network_Query();
    252             $found = $q->query(
    253                 array(
    254                     'fields'         => 'ids',
    255                     'domain__not_in' => array( 'www.w.org' ),
    256                 )
    257             );
    258 
    259             $expected = array(
    260                 get_current_site()->id, // Account for the initial network added by the test suite.
    261                 self::$network_ids['wordpress.org/'],
    262                 self::$network_ids['make.wordpress.org/'],
    263                 self::$network_ids['www.wordpress.net/'],
    264             );
    265 
    266             $this->assertSameSets( $expected, $found );
    267         }
    268 
    269         public function test_wp_network_query_by_domain__not_in_with_multiple_domains() {
    270             $q     = new WP_Network_Query();
    271             $found = $q->query(
    272                 array(
    273                     'fields'         => 'ids',
    274                     'domain__not_in' => array( 'wordpress.org', 'www.w.org' ),
    275                 )
    276             );
    277 
    278             $expected = array(
    279                 get_current_site()->id, // Account for the initial network added by the test suite.
    280                 self::$network_ids['make.wordpress.org/'],
    281                 self::$network_ids['www.wordpress.net/'],
    282             );
    283 
    284             $this->assertSameSets( $expected, $found );
    285         }
    286 
    287         public function test_wp_network_query_by_domain__not_in_with_multiple_domains_and_number() {
    288             $q     = new WP_Network_Query();
    289             $found = $q->query(
    290                 array(
    291                     'fields'         => 'ids',
    292                     'number'         => 2,
    293                     'domain__not_in' => array( 'wordpress.org', 'www.w.org' ),
    294                 )
    295             );
    296 
    297             $expected = array(
    298                 get_current_site()->id, // Account for the initial network added by the test suite.
    299                 self::$network_ids['make.wordpress.org/'],
    300             );
    301 
    302             $this->assertSameSets( $expected, $found );
    303         }
    304 
    305         public function test_wp_network_query_by_domain__not_in_with_multiple_domains_and_number_and_offset() {
    306             $q     = new WP_Network_Query();
    307             $found = $q->query(
    308                 array(
    309                     'fields'         => 'ids',
    310                     'number'         => 2,
    311                     'offset'         => 1,
    312                     'domain__not_in' => array( 'wordpress.org', 'www.w.org' ),
    313                 )
    314             );
    315 
    316             $expected = array(
    317                 self::$network_ids['make.wordpress.org/'],
    318                 self::$network_ids['www.wordpress.net/'],
    319             );
    320 
    321             $this->assertSameSets( $expected, $found );
    322         }
    323 
    324         public function test_wp_network_query_by_path_with_expected_results() {
    325             $q     = new WP_Network_Query();
    326             $found = $q->query(
    327                 array(
    328                     'fields'          => 'ids',
    329                     'path'            => '/',
    330                     'network__not_in' => get_current_site()->id, // Exclude the initial network added by the test suite.
    331                 )
    332             );
    333 
    334             $expected = array(
    335                 self::$network_ids['wordpress.org/'],
    336                 self::$network_ids['make.wordpress.org/'],
    337                 self::$network_ids['www.wordpress.net/'],
    338             );
    339 
    340             $this->assertSameSets( $expected, $found );
    341         }
    342 
    343         public function test_wp_network_query_by_path_and_number_and_offset_with_expected_results() {
    344             $q     = new WP_Network_Query();
    345             $found = $q->query(
    346                 array(
    347                     'fields'          => 'ids',
    348                     'number'          => 1,
    349                     'offset'          => 2,
    350                     'path'            => '/',
    351                     'network__not_in' => get_current_site()->id, // Exclude the initial network added by the test suite.
    352                 )
    353             );
    354 
    355             $expected = array(
    356                 self::$network_ids['www.wordpress.net/'],
    357             );
    358 
    359             $this->assertSameSets( $expected, $found );
    360         }
    361 
    362         public function test_wp_network_query_by_path_with_no_expected_results() {
    363             $q     = new WP_Network_Query();
    364             $found = $q->query(
    365                 array(
    366                     'fields' => 'ids',
    367                     'path'   => '/bar/',
    368                 )
    369             );
    370 
    371             $this->assertEmpty( $found );
    372         }
    373 
    374         public function test_wp_network_query_by_search_with_text_in_domain() {
    375             $q     = new WP_Network_Query();
    376             $found = $q->query(
    377                 array(
    378                     'fields' => 'ids',
    379                     'search' => 'ww.word',
    380                 )
    381             );
    382 
    383             $expected = array(
    384                 self::$network_ids['www.wordpress.net/'],
    385             );
    386 
    387             $this->assertSameSets( $expected, $found );
    388         }
    389 
    390         public function test_wp_network_query_by_search_with_text_in_path() {
    391             $q     = new WP_Network_Query();
    392             $found = $q->query(
    393                 array(
    394                     'fields' => 'ids',
    395                     'search' => 'foo',
    396                 )
    397             );
    398 
    399             $expected = array(
    400                 self::$network_ids['www.w.org/foo/'],
    401             );
    402 
    403             $this->assertSameSets( $expected, $found );
    404         }
    405 
    406         public function test_wp_network_query_by_path_order_by_domain_desc() {
    407             $q     = new WP_Network_Query();
    408             $found = $q->query(
    409                 array(
    410                     'fields'          => 'ids',
    411                     'path'            => '/',
    412                     'network__not_in' => get_current_site()->id, // Exclude the initial network added by the test suite.
    413                     'order'           => 'DESC',
    414                     'orderby'         => 'domain',
    415                 )
    416             );
    417 
    418             $expected = array(
    419                 self::$network_ids['www.wordpress.net/'],
    420                 self::$network_ids['wordpress.org/'],
    421                 self::$network_ids['make.wordpress.org/'],
    422             );
    423 
    424             $this->assertSame( $expected, $found );
    425         }
    426 
    427         /**
    428          * @ticket 41347
    429          */
    430         public function test_wp_network_query_cache_with_different_fields_no_count() {
    431             $q                 = new WP_Network_Query();
    432             $query_1           = $q->query(
    433                 array(
    434                     'fields' => 'all',
    435                     'number' => 3,
    436                     'order'  => 'ASC',
    437                 )
    438             );
    439             $number_of_queries = get_num_queries();
    440 
    441             $query_2 = $q->query(
    442                 array(
    443                     'fields' => 'ids',
    444                     'number' => 3,
    445                     'order'  => 'ASC',
    446                 )
    447             );
    448 
    449             $this->assertSame( $number_of_queries, get_num_queries() );
    450         }
    451 
    452         /**
    453          * @ticket 41347
    454          */
    455         public function test_wp_network_query_cache_with_different_fields_active_count() {
    456             $q = new WP_Network_Query();
    457 
    458             $query_1           = $q->query(
    459                 array(
    460                     'fields' => 'all',
    461                     'number' => 3,
    462                     'order'  => 'ASC',
    463                     'count'  => true,
    464                 )
    465             );
    466             $number_of_queries = get_num_queries();
    467 
    468             $query_2 = $q->query(
    469                 array(
    470                     'fields' => 'ids',
    471                     'number' => 3,
    472                     'order'  => 'ASC',
    473                     'count'  => true,
    474                 )
    475             );
    476             $this->assertSame( $number_of_queries, get_num_queries() );
    477         }
    478 
    479         /**
    480          * @ticket 41347
    481          */
    482         public function test_wp_network_query_cache_with_same_fields_different_count() {
    483             $q = new WP_Network_Query();
    484 
    485             $query_1 = $q->query(
    486                 array(
    487                     'fields' => 'ids',
    488                     'number' => 3,
    489                     'order'  => 'ASC',
    490                 )
    491             );
    492 
    493             $number_of_queries = get_num_queries();
    494 
    495             $query_2 = $q->query(
    496                 array(
    497                     'fields' => 'ids',
    498                     'number' => 3,
    499                     'order'  => 'ASC',
    500                     'count'  => true,
    501                 )
    502             );
    503             $this->assertSame( $number_of_queries + 1, get_num_queries() );
    504         }
    505 
    506         /**
    507          * @ticket 55461
    508          */
    509         public function test_wp_network_query_cache_with_same_fields_same_cache_field() {
    510             $q                 = new WP_Network_Query();
    511             $query_1           = $q->query(
    512                 array(
    513                     'fields'               => 'all',
    514                     'number'               => 3,
    515                     'order'                => 'ASC',
    516                     'update_network_cache' => true,
    517                 )
    518             );
    519             $number_of_queries = get_num_queries();
    520 
    521             $query_2 = $q->query(
    522                 array(
    523                     'fields'               => 'all',
    524                     'number'               => 3,
    525                     'order'                => 'ASC',
    526                     'update_network_cache' => true,
    527                 )
    528             );
    529 
    530             $this->assertSame( $number_of_queries, get_num_queries() );
    531         }
    532 
    533         /**
    534          * @ticket 55461
    535          */
    536         public function test_wp_network_query_cache_with_same_fields_different_cache_field() {
    537             $q                 = new WP_Network_Query();
    538             $query_1           = $q->query(
    539                 array(
    540                     'fields'               => 'all',
    541                     'number'               => 3,
    542                     'order'                => 'ASC',
    543                     'update_network_cache' => true,
    544                 )
    545             );
    546             $number_of_queries = get_num_queries();
    547 
    548             $query_2 = $q->query(
    549                 array(
    550                     'fields'               => 'all',
    551                     'number'               => 3,
    552                     'order'                => 'ASC',
    553                     'update_network_cache' => false,
    554                 )
    555             );
    556 
    557             $this->assertSame( $number_of_queries, get_num_queries() );
    558         }
    559 
    560         /**
    561          * @ticket 45749
    562          * @ticket 47599
    563          */
    564         public function test_networks_pre_query_filter_should_bypass_database_query() {
    565             add_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query' ), 10, 2 );
    566 
    567             $num_queries = get_num_queries();
    568 
    569             $q       = new WP_Network_Query();
    570             $results = $q->query( array() );
    571 
    572             remove_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query' ), 10, 2 );
    573 
    574             // Make sure no queries were executed.
    575             $this->assertSame( $num_queries, get_num_queries() );
    576 
    577             // We manually inserted a non-existing site and overrode the results with it.
    578             $this->assertSame( array( 555 ), $results );
    579 
    580             // Make sure manually setting found_networks doesn't get overwritten.
    581             $this->assertSame( 1, $q->found_networks );
    582         }
    583 
    584         public static function filter_networks_pre_query( $networks, $query ) {
    585             $query->found_networks = 1;
    586 
    587             return array( 555 );
    588         }
    589 
    590         /**
    591          * @ticket 51333
    592          */
    593         public function test_networks_pre_query_filter_should_set_networks_property() {
    594             add_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query_and_set_networks' ), 10, 2 );
    595 
    596             $q       = new WP_Network_Query();
    597             $results = $q->query( array() );
    598 
    599             remove_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query_and_set_networks' ), 10 );
    600 
    601             // Make sure the networks property is the same as the results.
    602             $this->assertSame( $results, $q->networks );
    603 
    604             // Make sure the network domain is `wordpress.org`.
    605             $this->assertSame( 'wordpress.org', $q->networks[0]->domain );
    606         }
    607 
    608         public static function filter_networks_pre_query_and_set_networks( $networks, $query ) {
    609             return array( get_network( self::$network_ids['wordpress.org/'] ) );
    610         }
    611 
    612         /**
    613          * @ticket 56841
    614          */
    615         public function test_wp_network_query_does_not_have_leading_whitespace() {
    616             $q = new WP_Network_Query();
    617             $q->query(
    618                 array(
    619                     'fields'               => 'all',
    620                     'number'               => 3,
    621                     'order'                => 'ASC',
    622                     'update_network_cache' => true,
    623                 )
    624             );
    625 
    626             $this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' );
    627         }
    628     }
    629 
    630 endif;
     47    }
     48
     49    public function test_wp_network_query_by_number() {
     50        $q     = new WP_Network_Query();
     51        $found = $q->query(
     52            array(
     53                'fields' => 'ids',
     54                'number' => 3,
     55            )
     56        );
     57
     58        $this->assertCount( 3, $found );
     59    }
     60
     61    public function test_wp_network_query_by_network__in_with_order() {
     62        $expected = array( self::$network_ids['wordpress.org/'], self::$network_ids['make.wordpress.org/'] );
     63
     64        $q     = new WP_Network_Query();
     65        $found = $q->query(
     66            array(
     67                'fields'      => 'ids',
     68                'network__in' => $expected,
     69                'order'       => 'ASC',
     70            )
     71        );
     72
     73        $this->assertSame( $expected, $found );
     74
     75        $found = $q->query(
     76            array(
     77                'fields'      => 'ids',
     78                'network__in' => $expected,
     79                'order'       => 'DESC',
     80            )
     81        );
     82
     83        $this->assertSame( array_reverse( $expected ), $found );
     84    }
     85
     86    public function test_wp_network_query_by_network__in_with_single_id() {
     87        $expected = array( self::$network_ids['wordpress.org/'] );
     88
     89        $q     = new WP_Network_Query();
     90        $found = $q->query(
     91            array(
     92                'fields'      => 'ids',
     93                'network__in' => $expected,
     94            )
     95        );
     96
     97        $this->assertSameSets( $expected, $found );
     98    }
     99
     100    public function test_wp_network_query_by_network__in_with_multiple_ids() {
     101        $expected = array( self::$network_ids['wordpress.org/'], self::$network_ids['www.wordpress.net/'] );
     102
     103        $q     = new WP_Network_Query();
     104        $found = $q->query(
     105            array(
     106                'fields'      => 'ids',
     107                'network__in' => $expected,
     108            )
     109        );
     110
     111        $this->assertSameSets( $expected, $found );
     112    }
     113
     114    public function test_wp_network_query_by_network__in_and_count_with_multiple_ids() {
     115        $expected = array( self::$network_ids['wordpress.org/'], self::$network_ids['make.wordpress.org/'] );
     116
     117        $q     = new WP_Network_Query();
     118        $found = $q->query(
     119            array(
     120                'fields'      => 'ids',
     121                'count'       => true,
     122                'network__in' => $expected,
     123            )
     124        );
     125
     126        $this->assertSame( 2, $found );
     127    }
     128
     129    public function test_wp_network_query_by_network__not_in_with_single_id() {
     130        $excluded = array( self::$network_ids['wordpress.org/'] );
     131        $expected = array_diff( self::$network_ids, $excluded );
     132
     133        // Exclude main network since we don't have control over it here.
     134        $excluded[] = 1;
     135
     136        $q     = new WP_Network_Query();
     137        $found = $q->query(
     138            array(
     139                'fields'          => 'ids',
     140                'network__not_in' => $excluded,
     141            )
     142        );
     143
     144        $this->assertSameSets( $expected, $found );
     145    }
     146
     147    public function test_wp_network_query_by_network__not_in_with_multiple_ids() {
     148        $excluded = array( self::$network_ids['wordpress.org/'], self::$network_ids['www.w.org/foo/'] );
     149        $expected = array_diff( self::$network_ids, $excluded );
     150
     151        // Exclude main network since we don't have control over it here.
     152        $excluded[] = 1;
     153
     154        $q     = new WP_Network_Query();
     155        $found = $q->query(
     156            array(
     157                'fields'          => 'ids',
     158                'network__not_in' => $excluded,
     159            )
     160        );
     161
     162        $this->assertSameSets( $expected, $found );
     163    }
     164
     165    public function test_wp_network_query_by_domain() {
     166        $q     = new WP_Network_Query();
     167        $found = $q->query(
     168            array(
     169                'fields' => 'ids',
     170                'domain' => 'www.w.org',
     171            )
     172        );
     173
     174        $expected = array(
     175            self::$network_ids['www.w.org/foo/'],
     176        );
     177
     178        $this->assertSameSets( $expected, $found );
     179    }
     180
     181    public function test_wp_network_query_by_domain__in_with_single_domain() {
     182        $q     = new WP_Network_Query();
     183        $found = $q->query(
     184            array(
     185                'fields'     => 'ids',
     186                'domain__in' => array( 'make.wordpress.org' ),
     187            )
     188        );
     189
     190        $expected = array(
     191            self::$network_ids['make.wordpress.org/'],
     192        );
     193
     194        $this->assertSameSets( $expected, $found );
     195    }
     196
     197    public function test_wp_network_query_by_domain__in_with_multiple_domains() {
     198        $q     = new WP_Network_Query();
     199        $found = $q->query(
     200            array(
     201                'fields'     => 'ids',
     202                'domain__in' => array( 'wordpress.org', 'make.wordpress.org' ),
     203            )
     204        );
     205
     206        $expected = array(
     207            self::$network_ids['wordpress.org/'],
     208            self::$network_ids['make.wordpress.org/'],
     209        );
     210
     211        $this->assertSameSets( $expected, $found );
     212    }
     213
     214    public function test_wp_network_query_by_domain__in_with_multiple_domains_and_number() {
     215        $q     = new WP_Network_Query();
     216        $found = $q->query(
     217            array(
     218                'fields'     => 'ids',
     219                'number'     => 1,
     220                'domain__in' => array( 'wordpress.org', 'make.wordpress.org' ),
     221            )
     222        );
     223
     224        $expected = array(
     225            self::$network_ids['wordpress.org/'],
     226        );
     227
     228        $this->assertSameSets( $expected, $found );
     229    }
     230
     231    public function test_wp_network_query_by_domain__in_with_multiple_domains_and_number_and_offset() {
     232        $q     = new WP_Network_Query();
     233        $found = $q->query(
     234            array(
     235                'fields'     => 'ids',
     236                'number'     => 1,
     237                'offset'     => 1,
     238                'domain__in' => array( 'wordpress.org', 'make.wordpress.org' ),
     239            )
     240        );
     241
     242        $expected = array(
     243            self::$network_ids['make.wordpress.org/'],
     244        );
     245
     246        $this->assertSameSets( $expected, $found );
     247    }
     248
     249    public function test_wp_network_query_by_domain__not_in_with_single_domain() {
     250        $q     = new WP_Network_Query();
     251        $found = $q->query(
     252            array(
     253                'fields'         => 'ids',
     254                'domain__not_in' => array( 'www.w.org' ),
     255            )
     256        );
     257
     258        $expected = array(
     259            get_current_site()->id, // Account for the initial network added by the test suite.
     260            self::$network_ids['wordpress.org/'],
     261            self::$network_ids['make.wordpress.org/'],
     262            self::$network_ids['www.wordpress.net/'],
     263        );
     264
     265        $this->assertSameSets( $expected, $found );
     266    }
     267
     268    public function test_wp_network_query_by_domain__not_in_with_multiple_domains() {
     269        $q     = new WP_Network_Query();
     270        $found = $q->query(
     271            array(
     272                'fields'         => 'ids',
     273                'domain__not_in' => array( 'wordpress.org', 'www.w.org' ),
     274            )
     275        );
     276
     277        $expected = array(
     278            get_current_site()->id, // Account for the initial network added by the test suite.
     279            self::$network_ids['make.wordpress.org/'],
     280            self::$network_ids['www.wordpress.net/'],
     281        );
     282
     283        $this->assertSameSets( $expected, $found );
     284    }
     285
     286    public function test_wp_network_query_by_domain__not_in_with_multiple_domains_and_number() {
     287        $q     = new WP_Network_Query();
     288        $found = $q->query(
     289            array(
     290                'fields'         => 'ids',
     291                'number'         => 2,
     292                'domain__not_in' => array( 'wordpress.org', 'www.w.org' ),
     293            )
     294        );
     295
     296        $expected = array(
     297            get_current_site()->id, // Account for the initial network added by the test suite.
     298            self::$network_ids['make.wordpress.org/'],
     299        );
     300
     301        $this->assertSameSets( $expected, $found );
     302    }
     303
     304    public function test_wp_network_query_by_domain__not_in_with_multiple_domains_and_number_and_offset() {
     305        $q     = new WP_Network_Query();
     306        $found = $q->query(
     307            array(
     308                'fields'         => 'ids',
     309                'number'         => 2,
     310                'offset'         => 1,
     311                'domain__not_in' => array( 'wordpress.org', 'www.w.org' ),
     312            )
     313        );
     314
     315        $expected = array(
     316            self::$network_ids['make.wordpress.org/'],
     317            self::$network_ids['www.wordpress.net/'],
     318        );
     319
     320        $this->assertSameSets( $expected, $found );
     321    }
     322
     323    public function test_wp_network_query_by_path_with_expected_results() {
     324        $q     = new WP_Network_Query();
     325        $found = $q->query(
     326            array(
     327                'fields'          => 'ids',
     328                'path'            => '/',
     329                'network__not_in' => get_current_site()->id, // Exclude the initial network added by the test suite.
     330            )
     331        );
     332
     333        $expected = array(
     334            self::$network_ids['wordpress.org/'],
     335            self::$network_ids['make.wordpress.org/'],
     336            self::$network_ids['www.wordpress.net/'],
     337        );
     338
     339        $this->assertSameSets( $expected, $found );
     340    }
     341
     342    public function test_wp_network_query_by_path_and_number_and_offset_with_expected_results() {
     343        $q     = new WP_Network_Query();
     344        $found = $q->query(
     345            array(
     346                'fields'          => 'ids',
     347                'number'          => 1,
     348                'offset'          => 2,
     349                'path'            => '/',
     350                'network__not_in' => get_current_site()->id, // Exclude the initial network added by the test suite.
     351            )
     352        );
     353
     354        $expected = array(
     355            self::$network_ids['www.wordpress.net/'],
     356        );
     357
     358        $this->assertSameSets( $expected, $found );
     359    }
     360
     361    public function test_wp_network_query_by_path_with_no_expected_results() {
     362        $q     = new WP_Network_Query();
     363        $found = $q->query(
     364            array(
     365                'fields' => 'ids',
     366                'path'   => '/bar/',
     367            )
     368        );
     369
     370        $this->assertEmpty( $found );
     371    }
     372
     373    public function test_wp_network_query_by_search_with_text_in_domain() {
     374        $q     = new WP_Network_Query();
     375        $found = $q->query(
     376            array(
     377                'fields' => 'ids',
     378                'search' => 'ww.word',
     379            )
     380        );
     381
     382        $expected = array(
     383            self::$network_ids['www.wordpress.net/'],
     384        );
     385
     386        $this->assertSameSets( $expected, $found );
     387    }
     388
     389    public function test_wp_network_query_by_search_with_text_in_path() {
     390        $q     = new WP_Network_Query();
     391        $found = $q->query(
     392            array(
     393                'fields' => 'ids',
     394                'search' => 'foo',
     395            )
     396        );
     397
     398        $expected = array(
     399            self::$network_ids['www.w.org/foo/'],
     400        );
     401
     402        $this->assertSameSets( $expected, $found );
     403    }
     404
     405    public function test_wp_network_query_by_path_order_by_domain_desc() {
     406        $q     = new WP_Network_Query();
     407        $found = $q->query(
     408            array(
     409                'fields'          => 'ids',
     410                'path'            => '/',
     411                'network__not_in' => get_current_site()->id, // Exclude the initial network added by the test suite.
     412                'order'           => 'DESC',
     413                'orderby'         => 'domain',
     414            )
     415        );
     416
     417        $expected = array(
     418            self::$network_ids['www.wordpress.net/'],
     419            self::$network_ids['wordpress.org/'],
     420            self::$network_ids['make.wordpress.org/'],
     421        );
     422
     423        $this->assertSame( $expected, $found );
     424    }
     425
     426    /**
     427     * @ticket 41347
     428     */
     429    public function test_wp_network_query_cache_with_different_fields_no_count() {
     430        $q                 = new WP_Network_Query();
     431        $query_1           = $q->query(
     432            array(
     433                'fields' => 'all',
     434                'number' => 3,
     435                'order'  => 'ASC',
     436            )
     437        );
     438        $number_of_queries = get_num_queries();
     439
     440        $query_2 = $q->query(
     441            array(
     442                'fields' => 'ids',
     443                'number' => 3,
     444                'order'  => 'ASC',
     445            )
     446        );
     447
     448        $this->assertSame( $number_of_queries, get_num_queries() );
     449    }
     450
     451    /**
     452     * @ticket 41347
     453     */
     454    public function test_wp_network_query_cache_with_different_fields_active_count() {
     455        $q = new WP_Network_Query();
     456
     457        $query_1           = $q->query(
     458            array(
     459                'fields' => 'all',
     460                'number' => 3,
     461                'order'  => 'ASC',
     462                'count'  => true,
     463            )
     464        );
     465        $number_of_queries = get_num_queries();
     466
     467        $query_2 = $q->query(
     468            array(
     469                'fields' => 'ids',
     470                'number' => 3,
     471                'order'  => 'ASC',
     472                'count'  => true,
     473            )
     474        );
     475        $this->assertSame( $number_of_queries, get_num_queries() );
     476    }
     477
     478    /**
     479     * @ticket 41347
     480     */
     481    public function test_wp_network_query_cache_with_same_fields_different_count() {
     482        $q = new WP_Network_Query();
     483
     484        $query_1 = $q->query(
     485            array(
     486                'fields' => 'ids',
     487                'number' => 3,
     488                'order'  => 'ASC',
     489            )
     490        );
     491
     492        $number_of_queries = get_num_queries();
     493
     494        $query_2 = $q->query(
     495            array(
     496                'fields' => 'ids',
     497                'number' => 3,
     498                'order'  => 'ASC',
     499                'count'  => true,
     500            )
     501        );
     502        $this->assertSame( $number_of_queries + 1, get_num_queries() );
     503    }
     504
     505    /**
     506     * @ticket 55461
     507     */
     508    public function test_wp_network_query_cache_with_same_fields_same_cache_field() {
     509        $q                 = new WP_Network_Query();
     510        $query_1           = $q->query(
     511            array(
     512                'fields'               => 'all',
     513                'number'               => 3,
     514                'order'                => 'ASC',
     515                'update_network_cache' => true,
     516            )
     517        );
     518        $number_of_queries = get_num_queries();
     519
     520        $query_2 = $q->query(
     521            array(
     522                'fields'               => 'all',
     523                'number'               => 3,
     524                'order'                => 'ASC',
     525                'update_network_cache' => true,
     526            )
     527        );
     528
     529        $this->assertSame( $number_of_queries, get_num_queries() );
     530    }
     531
     532    /**
     533     * @ticket 55461
     534     */
     535    public function test_wp_network_query_cache_with_same_fields_different_cache_field() {
     536        $q                 = new WP_Network_Query();
     537        $query_1           = $q->query(
     538            array(
     539                'fields'               => 'all',
     540                'number'               => 3,
     541                'order'                => 'ASC',
     542                'update_network_cache' => true,
     543            )
     544        );
     545        $number_of_queries = get_num_queries();
     546
     547        $query_2 = $q->query(
     548            array(
     549                'fields'               => 'all',
     550                'number'               => 3,
     551                'order'                => 'ASC',
     552                'update_network_cache' => false,
     553            )
     554        );
     555
     556        $this->assertSame( $number_of_queries, get_num_queries() );
     557    }
     558
     559    /**
     560     * @ticket 45749
     561     * @ticket 47599
     562     */
     563    public function test_networks_pre_query_filter_should_bypass_database_query() {
     564        add_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query' ), 10, 2 );
     565
     566        $num_queries = get_num_queries();
     567
     568        $q       = new WP_Network_Query();
     569        $results = $q->query( array() );
     570
     571        remove_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query' ), 10, 2 );
     572
     573        // Make sure no queries were executed.
     574        $this->assertSame( $num_queries, get_num_queries() );
     575
     576        // We manually inserted a non-existing site and overrode the results with it.
     577        $this->assertSame( array( 555 ), $results );
     578
     579        // Make sure manually setting found_networks doesn't get overwritten.
     580        $this->assertSame( 1, $q->found_networks );
     581    }
     582
     583    public static function filter_networks_pre_query( $networks, $query ) {
     584        $query->found_networks = 1;
     585
     586        return array( 555 );
     587    }
     588
     589    /**
     590     * @ticket 51333
     591     */
     592    public function test_networks_pre_query_filter_should_set_networks_property() {
     593        add_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query_and_set_networks' ), 10, 2 );
     594
     595        $q       = new WP_Network_Query();
     596        $results = $q->query( array() );
     597
     598        remove_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query_and_set_networks' ), 10 );
     599
     600        // Make sure the networks property is the same as the results.
     601        $this->assertSame( $results, $q->networks );
     602
     603        // Make sure the network domain is `wordpress.org`.
     604        $this->assertSame( 'wordpress.org', $q->networks[0]->domain );
     605    }
     606
     607    public static function filter_networks_pre_query_and_set_networks( $networks, $query ) {
     608        return array( get_network( self::$network_ids['wordpress.org/'] ) );
     609    }
     610
     611    /**
     612     * @ticket 56841
     613     */
     614    public function test_wp_network_query_does_not_have_leading_whitespace() {
     615        $q = new WP_Network_Query();
     616        $q->query(
     617            array(
     618                'fields'               => 'all',
     619                'number'               => 3,
     620                'order'                => 'ASC',
     621                'update_network_cache' => true,
     622            )
     623        );
     624
     625        $this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' );
     626    }
     627}
Note: See TracChangeset for help on using the changeset viewer.