Make WordPress Core


Ignore:
Timestamp:
04/09/2025 01:29:39 PM (19 months 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/wpSiteQuery.php

    r57750 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * Test site query functionality in multisite.
     5 *
     6 * @group ms-required
     7 * @group ms-site
     8 * @group multisite
     9 */
     10class Tests_Multisite_wpSiteQuery extends WP_UnitTestCase {
     11        protected static $network_ids;
     12        protected static $site_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                );
     29
     30                foreach ( self::$network_ids as &$id ) {
     31                        $id = $factory->network->create( $id );
     32                }
     33                unset( $id );
     34
     35                self::$site_ids = array(
     36                        'wordpress.org/'          => array(
     37                                'domain'     => 'wordpress.org',
     38                                'path'       => '/',
     39                                'network_id' => self::$network_ids['wordpress.org/'],
     40                        ),
     41                        'wordpress.org/foo/'      => array(
     42                                'domain'     => 'wordpress.org',
     43                                'path'       => '/foo/',
     44                                'network_id' => self::$network_ids['wordpress.org/'],
     45                        ),
     46                        'wordpress.org/foo/bar/'  => array(
     47                                'domain'     => 'wordpress.org',
     48                                'path'       => '/foo/bar/',
     49                                'network_id' => self::$network_ids['wordpress.org/'],
     50                        ),
     51                        'make.wordpress.org/'     => array(
     52                                'domain'     => 'make.wordpress.org',
     53                                'path'       => '/',
     54                                'network_id' => self::$network_ids['make.wordpress.org/'],
     55                        ),
     56                        'make.wordpress.org/foo/' => array(
     57                                'domain'     => 'make.wordpress.org',
     58                                'path'       => '/foo/',
     59                                'network_id' => self::$network_ids['make.wordpress.org/'],
     60                        ),
     61                        'www.w.org/'              => array(
     62                                'domain' => 'www.w.org',
     63                                'path'   => '/',
     64                        ),
     65                        'www.w.org/foo/'          => array(
     66                                'domain' => 'www.w.org',
     67                                'path'   => '/foo/',
     68                        ),
     69                        'www.w.org/foo/bar/'      => array(
     70                                'domain' => 'www.w.org',
     71                                'path'   => '/foo/bar/',
     72                        ),
     73                        'www.w.org/make/'         => array(
     74                                'domain'  => 'www.w.org',
     75                                'path'    => '/make/',
     76                                'public'  => 1,
     77                                'lang_id' => 1,
     78                        ),
     79                );
     80
     81                foreach ( self::$site_ids as &$id ) {
     82                        $id = $factory->blog->create( $id );
     83                }
     84                unset( $id );
     85        }
     86
     87        public static function wpTearDownAfterClass() {
     88                global $wpdb;
     89
     90                foreach ( self::$site_ids as $id ) {
     91                        wp_delete_site( $id );
     92                }
     93
     94                foreach ( self::$network_ids as $id ) {
     95                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
     96                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
     97                }
     98
     99                wp_update_network_site_counts();
     100        }
     101
     102        public function test_wp_site_query_by_ID() {
     103                $q     = new WP_Site_Query();
     104                $found = $q->query(
     105                        array(
     106                                'fields' => 'ids',
     107                                'ID'     => self::$site_ids['www.w.org/'],
     108                        )
     109                );
     110
     111                $this->assertSameSets( array( self::$site_ids['www.w.org/'] ), $found );
     112        }
     113
     114        public function test_wp_site_query_by_number() {
     115                $q     = new WP_Site_Query();
     116                $found = $q->query(
     117                        array(
     118                                'fields' => 'ids',
     119                                'number' => 3,
     120                        )
     121                );
     122
     123                $this->assertCount( 3, $found );
     124        }
     125
     126        public function test_wp_site_query_by_site__in_with_single_id() {
     127                $expected = array( self::$site_ids['wordpress.org/foo/'] );
     128
     129                $q     = new WP_Site_Query();
     130                $found = $q->query(
     131                        array(
     132                                'fields'   => 'ids',
     133                                'site__in' => $expected,
     134                        )
     135                );
     136
     137                $this->assertSameSets( $expected, $found );
     138        }
     139
     140        public function test_wp_site_query_by_site__in_with_multiple_ids() {
     141                $expected = array( self::$site_ids['wordpress.org/'], self::$site_ids['wordpress.org/foo/'] );
     142
     143                $q     = new WP_Site_Query();
     144                $found = $q->query(
     145                        array(
     146                                'fields'   => 'ids',
     147                                'site__in' => $expected,
     148                        )
     149                );
     150
     151                $this->assertSameSets( $expected, $found );
     152        }
    4153
    5154        /**
    6          * Test site query functionality in multisite.
    7          *
    8          * @group ms-site
    9          * @group multisite
     155         * Test the `count` query var
    10156         */
    11         class Tests_Multisite_wpSiteQuery extends WP_UnitTestCase {
    12                 protected static $network_ids;
    13                 protected static $site_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                         );
    30 
    31                         foreach ( self::$network_ids as &$id ) {
    32                                 $id = $factory->network->create( $id );
    33                         }
    34                         unset( $id );
    35 
    36                         self::$site_ids = array(
    37                                 'wordpress.org/'          => array(
    38                                         'domain'     => 'wordpress.org',
    39                                         'path'       => '/',
    40                                         'network_id' => self::$network_ids['wordpress.org/'],
    41                                 ),
    42                                 'wordpress.org/foo/'      => array(
    43                                         'domain'     => 'wordpress.org',
    44                                         'path'       => '/foo/',
    45                                         'network_id' => self::$network_ids['wordpress.org/'],
    46                                 ),
    47                                 'wordpress.org/foo/bar/'  => array(
    48                                         'domain'     => 'wordpress.org',
    49                                         'path'       => '/foo/bar/',
    50                                         'network_id' => self::$network_ids['wordpress.org/'],
    51                                 ),
    52                                 'make.wordpress.org/'     => array(
    53                                         'domain'     => 'make.wordpress.org',
    54                                         'path'       => '/',
    55                                         'network_id' => self::$network_ids['make.wordpress.org/'],
    56                                 ),
    57                                 'make.wordpress.org/foo/' => array(
    58                                         'domain'     => 'make.wordpress.org',
    59                                         'path'       => '/foo/',
    60                                         'network_id' => self::$network_ids['make.wordpress.org/'],
    61                                 ),
    62                                 'www.w.org/'              => array(
    63                                         'domain' => 'www.w.org',
    64                                         'path'   => '/',
    65                                 ),
    66                                 'www.w.org/foo/'          => array(
    67                                         'domain' => 'www.w.org',
    68                                         'path'   => '/foo/',
    69                                 ),
    70                                 'www.w.org/foo/bar/'      => array(
    71                                         'domain' => 'www.w.org',
    72                                         'path'   => '/foo/bar/',
    73                                 ),
    74                                 'www.w.org/make/'         => array(
    75                                         'domain'  => 'www.w.org',
    76                                         'path'    => '/make/',
    77                                         'public'  => 1,
    78                                         'lang_id' => 1,
    79                                 ),
    80                         );
    81 
    82                         foreach ( self::$site_ids as &$id ) {
    83                                 $id = $factory->blog->create( $id );
    84                         }
    85                         unset( $id );
     157        public function test_wp_site_query_by_site__in_and_count_with_multiple_ids() {
     158                $expected = array( self::$site_ids['wordpress.org/'], self::$site_ids['wordpress.org/foo/'] );
     159
     160                $q     = new WP_Site_Query();
     161                $found = $q->query(
     162                        array(
     163                                'fields'   => 'ids',
     164                                'count'    => true,
     165                                'site__in' => $expected,
     166                        )
     167                );
     168
     169                $this->assertSame( 2, $found );
     170        }
     171
     172        public function test_wp_site_query_by_site__not_in_with_single_id() {
     173                $excluded = array( self::$site_ids['wordpress.org/foo/'] );
     174                $expected = array_diff( self::$site_ids, $excluded );
     175
     176                // Exclude main site since we don't have control over it here.
     177                $excluded[] = 1;
     178
     179                $q     = new WP_Site_Query();
     180                $found = $q->query(
     181                        array(
     182                                'fields'       => 'ids',
     183                                'site__not_in' => $excluded,
     184                        )
     185                );
     186
     187                $this->assertSameSets( $expected, $found );
     188        }
     189
     190        public function test_wp_site_query_by_site__not_in_with_multiple_ids() {
     191                $excluded = array( self::$site_ids['wordpress.org/'], self::$site_ids['wordpress.org/foo/'] );
     192                $expected = array_diff( self::$site_ids, $excluded );
     193
     194                // Exclude main site since we don't have control over it here.
     195                $excluded[] = 1;
     196
     197                $q     = new WP_Site_Query();
     198                $found = $q->query(
     199                        array(
     200                                'fields'       => 'ids',
     201                                'site__not_in' => $excluded,
     202                        )
     203                );
     204
     205                $this->assertSameSets( $expected, $found );
     206        }
     207
     208        public function test_wp_site_query_by_network_id_with_order() {
     209                $q     = new WP_Site_Query();
     210                $found = $q->query(
     211                        array(
     212                                'fields'     => 'ids',
     213                                'network_id' => self::$network_ids['wordpress.org/'],
     214                                'number'     => 3,
     215                                'order'      => 'ASC',
     216                        )
     217                );
     218
     219                $expected = array(
     220                        self::$site_ids['wordpress.org/'],
     221                        self::$site_ids['wordpress.org/foo/'],
     222                        self::$site_ids['wordpress.org/foo/bar/'],
     223                );
     224
     225                $this->assertSame( $expected, $found );
     226
     227                $found = $q->query(
     228                        array(
     229                                'fields'     => 'ids',
     230                                'network_id' => self::$network_ids['wordpress.org/'],
     231                                'number'     => 3,
     232                                'order'      => 'DESC',
     233                        )
     234                );
     235
     236                $this->assertSame( array_reverse( $expected ), $found );
     237        }
     238
     239        public function test_wp_site_query_by_network_id_with_existing_sites() {
     240                $q     = new WP_Site_Query();
     241                $found = $q->query(
     242                        array(
     243                                'fields'     => 'ids',
     244                                'network_id' => self::$network_ids['make.wordpress.org/'],
     245                        )
     246                );
     247
     248                $expected = array(
     249                        self::$site_ids['make.wordpress.org/'],
     250                        self::$site_ids['make.wordpress.org/foo/'],
     251                );
     252
     253                $this->assertSameSets( $expected, $found );
     254        }
     255
     256        public function test_wp_site_query_by_network_id_with_no_existing_sites() {
     257                $q     = new WP_Site_Query();
     258                $found = $q->query(
     259                        array(
     260                                'fields'     => 'ids',
     261                                'network_id' => self::$network_ids['www.wordpress.net/'],
     262                        )
     263                );
     264
     265                $this->assertEmpty( $found );
     266        }
     267
     268        public function test_wp_site_query_by_domain() {
     269                $q     = new WP_Site_Query();
     270                $found = $q->query(
     271                        array(
     272                                'fields' => 'ids',
     273                                'domain' => 'www.w.org',
     274                        )
     275                );
     276
     277                $expected = array(
     278                        self::$site_ids['www.w.org/'],
     279                        self::$site_ids['www.w.org/foo/'],
     280                        self::$site_ids['www.w.org/foo/bar/'],
     281                        self::$site_ids['www.w.org/make/'],
     282                );
     283
     284                $this->assertSameSets( $expected, $found );
     285        }
     286
     287        public function test_wp_site_query_by_domain_and_offset() {
     288                $q     = new WP_Site_Query();
     289                $found = $q->query(
     290                        array(
     291                                'fields' => 'ids',
     292                                'domain' => 'www.w.org',
     293                                'offset' => 1,
     294                        )
     295                );
     296
     297                $expected = array(
     298                        self::$site_ids['www.w.org/foo/'],
     299                        self::$site_ids['www.w.org/foo/bar/'],
     300                        self::$site_ids['www.w.org/make/'],
     301                );
     302
     303                $this->assertSameSets( $expected, $found );
     304        }
     305
     306        public function test_wp_site_query_by_domain_and_number_and_offset() {
     307                $q     = new WP_Site_Query();
     308                $found = $q->query(
     309                        array(
     310                                'fields' => 'ids',
     311                                'domain' => 'www.w.org',
     312                                'number' => 2,
     313                                'offset' => 1,
     314                        )
     315                );
     316
     317                $expected = array(
     318                        self::$site_ids['www.w.org/foo/'],
     319                        self::$site_ids['www.w.org/foo/bar/'],
     320                );
     321
     322                $this->assertSameSets( $expected, $found );
     323        }
     324
     325        public function test_wp_site_query_by_domain__in_with_single_domain() {
     326                $q     = new WP_Site_Query();
     327                $found = $q->query(
     328                        array(
     329                                'fields'     => 'ids',
     330                                'domain__in' => array( 'make.wordpress.org' ),
     331                        )
     332                );
     333
     334                $expected = array(
     335                        self::$site_ids['make.wordpress.org/'],
     336                        self::$site_ids['make.wordpress.org/foo/'],
     337                );
     338
     339                $this->assertSameSets( $expected, $found );
     340        }
     341
     342        public function test_wp_site_query_by_domain__in_with_multiple_domains() {
     343                $q     = new WP_Site_Query();
     344                $found = $q->query(
     345                        array(
     346                                'fields'     => 'ids',
     347                                'domain__in' => array( 'wordpress.org', 'make.wordpress.org' ),
     348                        )
     349                );
     350
     351                $expected = array(
     352                        self::$site_ids['wordpress.org/'],
     353                        self::$site_ids['wordpress.org/foo/'],
     354                        self::$site_ids['wordpress.org/foo/bar/'],
     355                        self::$site_ids['make.wordpress.org/'],
     356                        self::$site_ids['make.wordpress.org/foo/'],
     357                );
     358
     359                $this->assertSameSets( $expected, $found );
     360        }
     361
     362        public function test_wp_site_query_by_domain__not_in_with_single_domain() {
     363                $q     = new WP_Site_Query();
     364                $found = $q->query(
     365                        array(
     366                                'fields'         => 'ids',
     367                                'domain__not_in' => array( 'www.w.org' ),
     368                        )
     369                );
     370
     371                $expected = array(
     372                        get_current_blog_id(), // Account for the initial site added by the test suite.
     373                        self::$site_ids['wordpress.org/'],
     374                        self::$site_ids['wordpress.org/foo/'],
     375                        self::$site_ids['wordpress.org/foo/bar/'],
     376                        self::$site_ids['make.wordpress.org/'],
     377                        self::$site_ids['make.wordpress.org/foo/'],
     378                );
     379
     380                $this->assertSameSets( $expected, $found );
     381        }
     382
     383        public function test_wp_site_query_by_domain__not_in_with_multiple_domains() {
     384                $q     = new WP_Site_Query();
     385                $found = $q->query(
     386                        array(
     387                                'fields'         => 'ids',
     388                                'domain__not_in' => array( 'wordpress.org', 'www.w.org' ),
     389                        )
     390                );
     391
     392                $expected = array(
     393                        get_current_blog_id(), // Account for the initial site added by the test suite.
     394                        self::$site_ids['make.wordpress.org/'],
     395                        self::$site_ids['make.wordpress.org/foo/'],
     396                );
     397
     398                $this->assertSameSets( $expected, $found );
     399        }
     400
     401        public function test_wp_site_query_by_path_with_expected_results() {
     402                $q     = new WP_Site_Query();
     403                $found = $q->query(
     404                        array(
     405                                'fields' => 'ids',
     406                                'path'   => '/foo/bar/',
     407                        )
     408                );
     409
     410                $expected = array(
     411                        self::$site_ids['wordpress.org/foo/bar/'],
     412                        self::$site_ids['www.w.org/foo/bar/'],
     413                );
     414
     415                $this->assertSameSets( $expected, $found );
     416        }
     417
     418        public function test_wp_site_query_by_path_with_no_expected_results() {
     419                $q     = new WP_Site_Query();
     420                $found = $q->query(
     421                        array(
     422                                'fields' => 'ids',
     423                                'path'   => '/foo/bar/foo/',
     424                        )
     425                );
     426
     427                $this->assertEmpty( $found );
     428        }
     429
     430        // archived, mature, spam, deleted, public.
     431
     432        public function test_wp_site_query_by_archived() {
     433                $q     = new WP_Site_Query();
     434                $found = $q->query(
     435                        array(
     436                                'fields'       => 'ids',
     437                                // Exclude main site since we don't have control over it here.
     438                                'site__not_in' => array( 1 ),
     439                                'archived'     => '0',
     440                        )
     441                );
     442
     443                $this->assertSameSets( array_values( self::$site_ids ), $found );
     444        }
     445
     446        public function test_wp_site_query_by_mature() {
     447                $q     = new WP_Site_Query();
     448                $found = $q->query(
     449                        array(
     450                                'fields'       => 'ids',
     451                                // Exclude main site since we don't have control over it here.
     452                                'site__not_in' => array( 1 ),
     453                                'mature'       => '0',
     454                        )
     455                );
     456
     457                $this->assertSameSets( array_values( self::$site_ids ), $found );
     458        }
     459
     460        public function test_wp_site_query_by_spam() {
     461                $q     = new WP_Site_Query();
     462                $found = $q->query(
     463                        array(
     464                                'fields'       => 'ids',
     465                                // Exclude main site since we don't have control over it here.
     466                                'site__not_in' => array( 1 ),
     467                                'spam'         => '0',
     468                        )
     469                );
     470
     471                $this->assertSameSets( array_values( self::$site_ids ), $found );
     472        }
     473
     474        public function test_wp_site_query_by_deleted() {
     475                $q     = new WP_Site_Query();
     476                $found = $q->query(
     477                        array(
     478                                'fields'       => 'ids',
     479                                // Exclude main site since we don't have control over it here.
     480                                'site__not_in' => array( 1 ),
     481                                'deleted'      => '0',
     482                        )
     483                );
     484
     485                $this->assertSameSets( array_values( self::$site_ids ), $found );
     486        }
     487
     488        public function test_wp_site_query_by_deleted_with_no_results() {
     489                $q     = new WP_Site_Query();
     490                $found = $q->query(
     491                        array(
     492                                'fields'  => 'ids',
     493                                'deleted' => '1',
     494                        )
     495                );
     496
     497                $this->assertEmpty( $found );
     498        }
     499
     500        public function test_wp_site_query_by_public() {
     501                $q     = new WP_Site_Query();
     502                $found = $q->query(
     503                        array(
     504                                'fields'       => 'ids',
     505                                // Exclude main site since we don't have control over it here.
     506                                'site__not_in' => array( 1 ),
     507                                'public'       => '1',
     508                        )
     509                );
     510
     511                $this->assertSameSets( array_values( self::$site_ids ), $found );
     512        }
     513
     514        public function test_wp_site_query_by_lang_id_with_zero() {
     515                $q     = new WP_Site_Query();
     516                $found = $q->query(
     517                        array(
     518                                'fields'       => 'ids',
     519                                // Exclude main site since we don't have control over it here.
     520                                'site__not_in' => array( 1 ),
     521                                'lang_id'      => 0,
     522                        )
     523                );
     524
     525                $this->assertSameSets( array_diff( array_values( self::$site_ids ), array( self::$site_ids['www.w.org/make/'] ) ), $found );
     526        }
     527
     528        public function test_wp_site_query_by_lang_id() {
     529                $q     = new WP_Site_Query();
     530                $found = $q->query(
     531                        array(
     532                                'fields'  => 'ids',
     533                                'lang_id' => 1,
     534                        )
     535                );
     536
     537                $expected = array(
     538                        self::$site_ids['www.w.org/make/'],
     539                );
     540
     541                $this->assertSameSets( $expected, $found );
     542        }
     543
     544        public function test_wp_site_query_by_lang_id_with_no_results() {
     545                $q     = new WP_Site_Query();
     546                $found = $q->query(
     547                        array(
     548                                'fields'  => 'ids',
     549                                'lang_id' => 2,
     550                        )
     551                );
     552
     553                $this->assertEmpty( $found );
     554        }
     555
     556        public function test_wp_site_query_by_lang__in() {
     557                $q     = new WP_Site_Query();
     558                $found = $q->query(
     559                        array(
     560                                'fields'   => 'ids',
     561                                'lang__in' => array( 1 ),
     562                        )
     563                );
     564
     565                $expected = array(
     566                        self::$site_ids['www.w.org/make/'],
     567                );
     568
     569                $this->assertSameSets( $expected, $found );
     570        }
     571
     572        public function test_wp_site_query_by_lang__in_with_multiple_ids() {
     573                $q     = new WP_Site_Query();
     574                $found = $q->query(
     575                        array(
     576                                'fields'       => 'ids',
     577                                // Exclude main site since we don't have control over it here.
     578                                'site__not_in' => array( 1 ),
     579                                'lang__in'     => array( 0, 1 ),
     580                        )
     581                );
     582
     583                $this->assertSameSets( array_values( self::$site_ids ), $found );
     584        }
     585
     586        public function test_wp_site_query_by_lang__not_in() {
     587                $q     = new WP_Site_Query();
     588                $found = $q->query(
     589                        array(
     590                                'fields'       => 'ids',
     591                                'lang__not_in' => array( 0 ),
     592                        )
     593                );
     594
     595                $expected = array(
     596                        self::$site_ids['www.w.org/make/'],
     597                );
     598
     599                $this->assertSameSets( $expected, $found );
     600        }
     601
     602        public function test_wp_site_query_by_lang__not_in_with_multiple_ids() {
     603                $q     = new WP_Site_Query();
     604                $found = $q->query(
     605                        array(
     606                                'fields'       => 'ids',
     607                                'lang__not_in' => array( 0, 1 ),
     608                        )
     609                );
     610
     611                $this->assertEmpty( $found );
     612        }
     613
     614        public function test_wp_site_query_by_search_with_text_in_domain() {
     615                $q     = new WP_Site_Query();
     616                $found = $q->query(
     617                        array(
     618                                'fields' => 'ids',
     619                                'search' => 'ke.wordp',
     620                        )
     621                );
     622
     623                $expected = array(
     624                        self::$site_ids['make.wordpress.org/'],
     625                        self::$site_ids['make.wordpress.org/foo/'],
     626                );
     627
     628                $this->assertSameSets( $expected, $found );
     629        }
     630
     631        public function test_wp_site_query_by_search_with_text_in_path() {
     632                $q     = new WP_Site_Query();
     633                $found = $q->query(
     634                        array(
     635                                'fields' => 'ids',
     636                                'search' => 'foo',
     637                        )
     638                );
     639
     640                $expected = array(
     641                        self::$site_ids['wordpress.org/foo/'],
     642                        self::$site_ids['wordpress.org/foo/bar/'],
     643                        self::$site_ids['make.wordpress.org/foo/'],
     644                        self::$site_ids['www.w.org/foo/'],
     645                        self::$site_ids['www.w.org/foo/bar/'],
     646                );
     647
     648                $this->assertSameSets( $expected, $found );
     649        }
     650
     651        public function test_wp_site_query_by_search_with_text_in_path_and_domain() {
     652                $q     = new WP_Site_Query();
     653                $found = $q->query(
     654                        array(
     655                                'fields' => 'ids',
     656                                'search' => 'make',
     657                        )
     658                );
     659
     660                $expected = array(
     661                        self::$site_ids['make.wordpress.org/'],
     662                        self::$site_ids['make.wordpress.org/foo/'],
     663                        self::$site_ids['www.w.org/make/'],
     664                );
     665
     666                $this->assertSameSets( $expected, $found );
     667        }
     668
     669        public function test_wp_site_query_by_search_with_text_in_path_and_domain_order_by_domain_desc() {
     670                $q     = new WP_Site_Query();
     671                $found = $q->query(
     672                        array(
     673                                'fields'  => 'ids',
     674                                'search'  => 'make',
     675                                'order'   => 'DESC',
     676                                'orderby' => 'domain',
     677                        )
     678                );
     679
     680                $expected = array(
     681                        self::$site_ids['www.w.org/make/'],
     682                        self::$site_ids['make.wordpress.org/'],
     683                        self::$site_ids['make.wordpress.org/foo/'],
     684                );
     685
     686                $this->assertSame( $expected, $found );
     687        }
     688
     689        public function test_wp_site_query_by_search_with_text_in_path_exclude_domain_from_search() {
     690                $q     = new WP_Site_Query();
     691                $found = $q->query(
     692                        array(
     693                                'fields'         => 'ids',
     694                                'search'         => 'make',
     695                                'search_columns' => array( 'path' ),
     696                        )
     697                );
     698
     699                $expected = array(
     700                        self::$site_ids['www.w.org/make/'],
     701                );
     702
     703                $this->assertSame( $expected, $found );
     704        }
     705
     706        public function test_wp_site_query_by_search_with_text_in_domain_exclude_path_from_search() {
     707                $q     = new WP_Site_Query();
     708                $found = $q->query(
     709                        array(
     710                                'fields'         => 'ids',
     711                                'search'         => 'make',
     712                                'search_columns' => array( 'domain' ),
     713                        )
     714                );
     715
     716                $expected = array(
     717                        self::$site_ids['make.wordpress.org/'],
     718                        self::$site_ids['make.wordpress.org/foo/'],
     719                );
     720
     721                $this->assertSame( $expected, $found );
     722        }
     723
     724        public function test_wp_site_query_by_search_with_wildcard_in_text() {
     725                $q     = new WP_Site_Query();
     726                $found = $q->query(
     727                        array(
     728                                'fields' => 'ids',
     729                                'search' => 'm*ke',
     730                        )
     731                );
     732
     733                $expected = array(
     734                        self::$site_ids['www.w.org/make/'],
     735                        self::$site_ids['make.wordpress.org/'],
     736                        self::$site_ids['make.wordpress.org/foo/'],
     737                );
     738
     739                $this->assertSameSets( $expected, $found );
     740        }
     741
     742        public function test_wp_site_query_by_search_with_wildcard_in_text_exclude_path_from_search() {
     743                $q     = new WP_Site_Query();
     744                $found = $q->query(
     745                        array(
     746                                'fields'         => 'ids',
     747                                'search'         => 'm*ke',
     748                                'search_columns' => array( 'domain' ),
     749                        )
     750                );
     751
     752                $expected = array(
     753                        self::$site_ids['make.wordpress.org/'],
     754                        self::$site_ids['make.wordpress.org/foo/'],
     755                );
     756
     757                $this->assertSameSets( $expected, $found );
     758        }
     759
     760        public function test_wp_site_query_by_search_with_wildcard_in_text_exclude_domain_from_search() {
     761                $q     = new WP_Site_Query();
     762                $found = $q->query(
     763                        array(
     764                                'fields'         => 'ids',
     765                                'search'         => 'm*ke',
     766                                'search_columns' => array( 'path' ),
     767                        )
     768                );
     769
     770                $expected = array(
     771                        self::$site_ids['www.w.org/make/'],
     772                );
     773
     774                $this->assertSameSets( $expected, $found );
     775        }
     776
     777        /**
     778         * @ticket 41197
     779         */
     780        public function test_wp_site_query_cache_with_different_fields_no_count() {
     781                $q                 = new WP_Site_Query();
     782                $query_1           = $q->query(
     783                        array(
     784                                'fields'     => 'all',
     785                                'network_id' => self::$network_ids['wordpress.org/'],
     786                                'number'     => 3,
     787                                'order'      => 'ASC',
     788                        )
     789                );
     790                $number_of_queries = get_num_queries();
     791
     792                $query_2 = $q->query(
     793                        array(
     794                                'fields'     => 'ids',
     795                                'network_id' => self::$network_ids['wordpress.org/'],
     796                                'number'     => 3,
     797                                'order'      => 'ASC',
     798                        )
     799                );
     800
     801                $this->assertSame( $number_of_queries, get_num_queries() );
     802        }
     803
     804        /**
     805         * @ticket 41197
     806         */
     807        public function test_wp_site_query_cache_with_different_fields_active_count() {
     808                $q = new WP_Site_Query();
     809
     810                $query_1           = $q->query(
     811                        array(
     812                                'fields'     => 'all',
     813                                'network_id' => self::$network_ids['wordpress.org/'],
     814                                'number'     => 3,
     815                                'order'      => 'ASC',
     816                                'count'      => true,
     817                        )
     818                );
     819                $number_of_queries = get_num_queries();
     820
     821                $query_2 = $q->query(
     822                        array(
     823                                'fields'     => 'ids',
     824                                'network_id' => self::$network_ids['wordpress.org/'],
     825                                'number'     => 3,
     826                                'order'      => 'ASC',
     827                                'count'      => true,
     828                        )
     829                );
     830                $this->assertSame( $number_of_queries, get_num_queries() );
     831        }
     832
     833        /**
     834         * @ticket 41197
     835         */
     836        public function test_wp_site_query_cache_with_same_fields_different_count() {
     837                $q = new WP_Site_Query();
     838
     839                $query_1 = $q->query(
     840                        array(
     841                                'fields'     => 'ids',
     842                                'network_id' => self::$network_ids['wordpress.org/'],
     843                                'number'     => 3,
     844                                'order'      => 'ASC',
     845                        )
     846                );
     847
     848                $number_of_queries = get_num_queries();
     849
     850                $query_2 = $q->query(
     851                        array(
     852                                'fields'     => 'ids',
     853                                'network_id' => self::$network_ids['wordpress.org/'],
     854                                'number'     => 3,
     855                                'order'      => 'ASC',
     856                                'count'      => true,
     857                        )
     858                );
     859                $this->assertSame( $number_of_queries + 1, get_num_queries() );
     860        }
     861
     862        /**
     863         * @ticket 55462
     864         */
     865        public function test_wp_site_query_cache_with_same_fields_same_cache_fields() {
     866                $q = new WP_Site_Query();
     867
     868                $query_1 = $q->query(
     869                        array(
     870                                'fields'                 => 'ids',
     871                                'network_id'             => self::$network_ids['wordpress.org/'],
     872                                'number'                 => 3,
     873                                'order'                  => 'ASC',
     874                                'update_site_cache'      => true,
     875                                'update_site_meta_cache' => true,
     876                        )
     877                );
     878
     879                $number_of_queries = get_num_queries();
     880
     881                $query_2 = $q->query(
     882                        array(
     883                                'fields'                 => 'ids',
     884                                'network_id'             => self::$network_ids['wordpress.org/'],
     885                                'number'                 => 3,
     886                                'order'                  => 'ASC',
     887                                'update_site_cache'      => true,
     888                                'update_site_meta_cache' => true,
     889                        )
     890                );
     891                $this->assertSame( $number_of_queries, get_num_queries() );
     892        }
     893
     894        /**
     895         * @ticket 55462
     896         */
     897        public function test_wp_site_query_cache_with_same_fields_different_cache_fields() {
     898                $q = new WP_Site_Query();
     899
     900                $query_1 = $q->query(
     901                        array(
     902                                'fields'                 => 'ids',
     903                                'network_id'             => self::$network_ids['wordpress.org/'],
     904                                'number'                 => 3,
     905                                'order'                  => 'ASC',
     906                                'update_site_cache'      => true,
     907                                'update_site_meta_cache' => true,
     908                        )
     909                );
     910
     911                $number_of_queries = get_num_queries();
     912
     913                $query_2 = $q->query(
     914                        array(
     915                                'fields'                 => 'ids',
     916                                'network_id'             => self::$network_ids['wordpress.org/'],
     917                                'number'                 => 3,
     918                                'order'                  => 'ASC',
     919                                'update_site_cache'      => false,
     920                                'update_site_meta_cache' => false,
     921                        )
     922                );
     923                $this->assertSame( $number_of_queries, get_num_queries() );
     924        }
     925
     926        /**
     927         * @ticket 40229
     928         * @dataProvider data_wp_site_query_meta_query
     929         */
     930        public function test_wp_site_query_meta_query( $query, $expected, $strict ) {
     931                if ( ! is_site_meta_supported() ) {
     932                        $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    86933                }
    87934
    88                 public static function wpTearDownAfterClass() {
    89                         global $wpdb;
    90 
    91                         foreach ( self::$site_ids as $id ) {
    92                                 wp_delete_site( $id );
    93                         }
    94 
    95                         foreach ( self::$network_ids as $id ) {
    96                                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
    97                                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
    98                         }
    99 
    100                         wp_update_network_site_counts();
     935                add_site_meta( self::$site_ids['wordpress.org/'], 'foo', 'foo' );
     936                add_site_meta( self::$site_ids['wordpress.org/foo/'], 'foo', 'bar' );
     937                add_site_meta( self::$site_ids['wordpress.org/foo/bar/'], 'foo', 'baz' );
     938                add_site_meta( self::$site_ids['make.wordpress.org/'], 'bar', 'baz' );
     939                add_site_meta( self::$site_ids['wordpress.org/'], 'numberfoo', 1 );
     940                add_site_meta( self::$site_ids['wordpress.org/foo/'], 'numberfoo', 2 );
     941
     942                $query['fields'] = 'ids';
     943
     944                $q     = new WP_Site_Query();
     945                $found = $q->query( $query );
     946
     947                foreach ( $expected as $index => $domain_path ) {
     948                        $expected[ $index ] = self::$site_ids[ $domain_path ];
    101949                }
    102950
    103                 public function test_wp_site_query_by_ID() {
    104                         $q     = new WP_Site_Query();
    105                         $found = $q->query(
    106                                 array(
    107                                         'fields' => 'ids',
    108                                         'ID'     => self::$site_ids['www.w.org/'],
    109                                 )
    110                         );
    111 
    112                         $this->assertSameSets( array( self::$site_ids['www.w.org/'] ), $found );
    113                 }
    114 
    115                 public function test_wp_site_query_by_number() {
    116                         $q     = new WP_Site_Query();
    117                         $found = $q->query(
    118                                 array(
    119                                         'fields' => 'ids',
    120                                         'number' => 3,
    121                                 )
    122                         );
    123 
    124                         $this->assertCount( 3, $found );
    125                 }
    126 
    127                 public function test_wp_site_query_by_site__in_with_single_id() {
    128                         $expected = array( self::$site_ids['wordpress.org/foo/'] );
    129 
    130                         $q     = new WP_Site_Query();
    131                         $found = $q->query(
    132                                 array(
    133                                         'fields'   => 'ids',
    134                                         'site__in' => $expected,
    135                                 )
    136                         );
    137 
     951                if ( $strict ) {
     952                        $this->assertSame( $expected, $found );
     953                } else {
    138954                        $this->assertSameSets( $expected, $found );
    139955                }
    140 
    141                 public function test_wp_site_query_by_site__in_with_multiple_ids() {
    142                         $expected = array( self::$site_ids['wordpress.org/'], self::$site_ids['wordpress.org/foo/'] );
    143 
    144                         $q     = new WP_Site_Query();
    145                         $found = $q->query(
    146                                 array(
    147                                         'fields'   => 'ids',
    148                                         'site__in' => $expected,
    149                                 )
    150                         );
    151 
    152                         $this->assertSameSets( $expected, $found );
    153                 }
    154 
    155                 /**
    156                  * Test the `count` query var
    157                  */
    158                 public function test_wp_site_query_by_site__in_and_count_with_multiple_ids() {
    159                         $expected = array( self::$site_ids['wordpress.org/'], self::$site_ids['wordpress.org/foo/'] );
    160 
    161                         $q     = new WP_Site_Query();
    162                         $found = $q->query(
    163                                 array(
    164                                         'fields'   => 'ids',
    165                                         'count'    => true,
    166                                         'site__in' => $expected,
    167                                 )
    168                         );
    169 
    170                         $this->assertSame( 2, $found );
    171                 }
    172 
    173                 public function test_wp_site_query_by_site__not_in_with_single_id() {
    174                         $excluded = array( self::$site_ids['wordpress.org/foo/'] );
    175                         $expected = array_diff( self::$site_ids, $excluded );
    176 
    177                         // Exclude main site since we don't have control over it here.
    178                         $excluded[] = 1;
    179 
    180                         $q     = new WP_Site_Query();
    181                         $found = $q->query(
    182                                 array(
    183                                         'fields'       => 'ids',
    184                                         'site__not_in' => $excluded,
    185                                 )
    186                         );
    187 
    188                         $this->assertSameSets( $expected, $found );
    189                 }
    190 
    191                 public function test_wp_site_query_by_site__not_in_with_multiple_ids() {
    192                         $excluded = array( self::$site_ids['wordpress.org/'], self::$site_ids['wordpress.org/foo/'] );
    193                         $expected = array_diff( self::$site_ids, $excluded );
    194 
    195                         // Exclude main site since we don't have control over it here.
    196                         $excluded[] = 1;
    197 
    198                         $q     = new WP_Site_Query();
    199                         $found = $q->query(
    200                                 array(
    201                                         'fields'       => 'ids',
    202                                         'site__not_in' => $excluded,
    203                                 )
    204                         );
    205 
    206                         $this->assertSameSets( $expected, $found );
    207                 }
    208 
    209                 public function test_wp_site_query_by_network_id_with_order() {
    210                         $q     = new WP_Site_Query();
    211                         $found = $q->query(
    212                                 array(
    213                                         'fields'     => 'ids',
    214                                         'network_id' => self::$network_ids['wordpress.org/'],
    215                                         'number'     => 3,
    216                                         'order'      => 'ASC',
    217                                 )
    218                         );
    219 
    220                         $expected = array(
    221                                 self::$site_ids['wordpress.org/'],
    222                                 self::$site_ids['wordpress.org/foo/'],
    223                                 self::$site_ids['wordpress.org/foo/bar/'],
    224                         );
    225 
    226                         $this->assertSame( $expected, $found );
    227 
    228                         $found = $q->query(
    229                                 array(
    230                                         'fields'     => 'ids',
    231                                         'network_id' => self::$network_ids['wordpress.org/'],
    232                                         'number'     => 3,
    233                                         'order'      => 'DESC',
    234                                 )
    235                         );
    236 
    237                         $this->assertSame( array_reverse( $expected ), $found );
    238                 }
    239 
    240                 public function test_wp_site_query_by_network_id_with_existing_sites() {
    241                         $q     = new WP_Site_Query();
    242                         $found = $q->query(
    243                                 array(
    244                                         'fields'     => 'ids',
    245                                         'network_id' => self::$network_ids['make.wordpress.org/'],
    246                                 )
    247                         );
    248 
    249                         $expected = array(
    250                                 self::$site_ids['make.wordpress.org/'],
    251                                 self::$site_ids['make.wordpress.org/foo/'],
    252                         );
    253 
    254                         $this->assertSameSets( $expected, $found );
    255                 }
    256 
    257                 public function test_wp_site_query_by_network_id_with_no_existing_sites() {
    258                         $q     = new WP_Site_Query();
    259                         $found = $q->query(
    260                                 array(
    261                                         'fields'     => 'ids',
    262                                         'network_id' => self::$network_ids['www.wordpress.net/'],
    263                                 )
    264                         );
    265 
    266                         $this->assertEmpty( $found );
    267                 }
    268 
    269                 public function test_wp_site_query_by_domain() {
    270                         $q     = new WP_Site_Query();
    271                         $found = $q->query(
    272                                 array(
    273                                         'fields' => 'ids',
    274                                         'domain' => 'www.w.org',
    275                                 )
    276                         );
    277 
    278                         $expected = array(
    279                                 self::$site_ids['www.w.org/'],
    280                                 self::$site_ids['www.w.org/foo/'],
    281                                 self::$site_ids['www.w.org/foo/bar/'],
    282                                 self::$site_ids['www.w.org/make/'],
    283                         );
    284 
    285                         $this->assertSameSets( $expected, $found );
    286                 }
    287 
    288                 public function test_wp_site_query_by_domain_and_offset() {
    289                         $q     = new WP_Site_Query();
    290                         $found = $q->query(
    291                                 array(
    292                                         'fields' => 'ids',
    293                                         'domain' => 'www.w.org',
    294                                         'offset' => 1,
    295                                 )
    296                         );
    297 
    298                         $expected = array(
    299                                 self::$site_ids['www.w.org/foo/'],
    300                                 self::$site_ids['www.w.org/foo/bar/'],
    301                                 self::$site_ids['www.w.org/make/'],
    302                         );
    303 
    304                         $this->assertSameSets( $expected, $found );
    305                 }
    306 
    307                 public function test_wp_site_query_by_domain_and_number_and_offset() {
    308                         $q     = new WP_Site_Query();
    309                         $found = $q->query(
    310                                 array(
    311                                         'fields' => 'ids',
    312                                         'domain' => 'www.w.org',
    313                                         'number' => 2,
    314                                         'offset' => 1,
    315                                 )
    316                         );
    317 
    318                         $expected = array(
    319                                 self::$site_ids['www.w.org/foo/'],
    320                                 self::$site_ids['www.w.org/foo/bar/'],
    321                         );
    322 
    323                         $this->assertSameSets( $expected, $found );
    324                 }
    325 
    326                 public function test_wp_site_query_by_domain__in_with_single_domain() {
    327                         $q     = new WP_Site_Query();
    328                         $found = $q->query(
    329                                 array(
    330                                         'fields'     => 'ids',
    331                                         'domain__in' => array( 'make.wordpress.org' ),
    332                                 )
    333                         );
    334 
    335                         $expected = array(
    336                                 self::$site_ids['make.wordpress.org/'],
    337                                 self::$site_ids['make.wordpress.org/foo/'],
    338                         );
    339 
    340                         $this->assertSameSets( $expected, $found );
    341                 }
    342 
    343                 public function test_wp_site_query_by_domain__in_with_multiple_domains() {
    344                         $q     = new WP_Site_Query();
    345                         $found = $q->query(
    346                                 array(
    347                                         'fields'     => 'ids',
    348                                         'domain__in' => array( 'wordpress.org', 'make.wordpress.org' ),
    349                                 )
    350                         );
    351 
    352                         $expected = array(
    353                                 self::$site_ids['wordpress.org/'],
    354                                 self::$site_ids['wordpress.org/foo/'],
    355                                 self::$site_ids['wordpress.org/foo/bar/'],
    356                                 self::$site_ids['make.wordpress.org/'],
    357                                 self::$site_ids['make.wordpress.org/foo/'],
    358                         );
    359 
    360                         $this->assertSameSets( $expected, $found );
    361                 }
    362 
    363                 public function test_wp_site_query_by_domain__not_in_with_single_domain() {
    364                         $q     = new WP_Site_Query();
    365                         $found = $q->query(
    366                                 array(
    367                                         'fields'         => 'ids',
    368                                         'domain__not_in' => array( 'www.w.org' ),
    369                                 )
    370                         );
    371 
    372                         $expected = array(
    373                                 get_current_blog_id(), // Account for the initial site added by the test suite.
    374                                 self::$site_ids['wordpress.org/'],
    375                                 self::$site_ids['wordpress.org/foo/'],
    376                                 self::$site_ids['wordpress.org/foo/bar/'],
    377                                 self::$site_ids['make.wordpress.org/'],
    378                                 self::$site_ids['make.wordpress.org/foo/'],
    379                         );
    380 
    381                         $this->assertSameSets( $expected, $found );
    382                 }
    383 
    384                 public function test_wp_site_query_by_domain__not_in_with_multiple_domains() {
    385                         $q     = new WP_Site_Query();
    386                         $found = $q->query(
    387                                 array(
    388                                         'fields'         => 'ids',
    389                                         'domain__not_in' => array( 'wordpress.org', 'www.w.org' ),
    390                                 )
    391                         );
    392 
    393                         $expected = array(
    394                                 get_current_blog_id(), // Account for the initial site added by the test suite.
    395                                 self::$site_ids['make.wordpress.org/'],
    396                                 self::$site_ids['make.wordpress.org/foo/'],
    397                         );
    398 
    399                         $this->assertSameSets( $expected, $found );
    400                 }
    401 
    402                 public function test_wp_site_query_by_path_with_expected_results() {
    403                         $q     = new WP_Site_Query();
    404                         $found = $q->query(
    405                                 array(
    406                                         'fields' => 'ids',
    407                                         'path'   => '/foo/bar/',
    408                                 )
    409                         );
    410 
    411                         $expected = array(
    412                                 self::$site_ids['wordpress.org/foo/bar/'],
    413                                 self::$site_ids['www.w.org/foo/bar/'],
    414                         );
    415 
    416                         $this->assertSameSets( $expected, $found );
    417                 }
    418 
    419                 public function test_wp_site_query_by_path_with_no_expected_results() {
    420                         $q     = new WP_Site_Query();
    421                         $found = $q->query(
    422                                 array(
    423                                         'fields' => 'ids',
    424                                         'path'   => '/foo/bar/foo/',
    425                                 )
    426                         );
    427 
    428                         $this->assertEmpty( $found );
    429                 }
    430 
    431                 // archived, mature, spam, deleted, public.
    432 
    433                 public function test_wp_site_query_by_archived() {
    434                         $q     = new WP_Site_Query();
    435                         $found = $q->query(
    436                                 array(
    437                                         'fields'       => 'ids',
    438                                         // Exclude main site since we don't have control over it here.
    439                                         'site__not_in' => array( 1 ),
    440                                         'archived'     => '0',
    441                                 )
    442                         );
    443 
    444                         $this->assertSameSets( array_values( self::$site_ids ), $found );
    445                 }
    446 
    447                 public function test_wp_site_query_by_mature() {
    448                         $q     = new WP_Site_Query();
    449                         $found = $q->query(
    450                                 array(
    451                                         'fields'       => 'ids',
    452                                         // Exclude main site since we don't have control over it here.
    453                                         'site__not_in' => array( 1 ),
    454                                         'mature'       => '0',
    455                                 )
    456                         );
    457 
    458                         $this->assertSameSets( array_values( self::$site_ids ), $found );
    459                 }
    460 
    461                 public function test_wp_site_query_by_spam() {
    462                         $q     = new WP_Site_Query();
    463                         $found = $q->query(
    464                                 array(
    465                                         'fields'       => 'ids',
    466                                         // Exclude main site since we don't have control over it here.
    467                                         'site__not_in' => array( 1 ),
    468                                         'spam'         => '0',
    469                                 )
    470                         );
    471 
    472                         $this->assertSameSets( array_values( self::$site_ids ), $found );
    473                 }
    474 
    475                 public function test_wp_site_query_by_deleted() {
    476                         $q     = new WP_Site_Query();
    477                         $found = $q->query(
    478                                 array(
    479                                         'fields'       => 'ids',
    480                                         // Exclude main site since we don't have control over it here.
    481                                         'site__not_in' => array( 1 ),
    482                                         'deleted'      => '0',
    483                                 )
    484                         );
    485 
    486                         $this->assertSameSets( array_values( self::$site_ids ), $found );
    487                 }
    488 
    489                 public function test_wp_site_query_by_deleted_with_no_results() {
    490                         $q     = new WP_Site_Query();
    491                         $found = $q->query(
    492                                 array(
    493                                         'fields'  => 'ids',
    494                                         'deleted' => '1',
    495                                 )
    496                         );
    497 
    498                         $this->assertEmpty( $found );
    499                 }
    500 
    501                 public function test_wp_site_query_by_public() {
    502                         $q     = new WP_Site_Query();
    503                         $found = $q->query(
    504                                 array(
    505                                         'fields'       => 'ids',
    506                                         // Exclude main site since we don't have control over it here.
    507                                         'site__not_in' => array( 1 ),
    508                                         'public'       => '1',
    509                                 )
    510                         );
    511 
    512                         $this->assertSameSets( array_values( self::$site_ids ), $found );
    513                 }
    514 
    515                 public function test_wp_site_query_by_lang_id_with_zero() {
    516                         $q     = new WP_Site_Query();
    517                         $found = $q->query(
    518                                 array(
    519                                         'fields'       => 'ids',
    520                                         // Exclude main site since we don't have control over it here.
    521                                         'site__not_in' => array( 1 ),
    522                                         'lang_id'      => 0,
    523                                 )
    524                         );
    525 
    526                         $this->assertSameSets( array_diff( array_values( self::$site_ids ), array( self::$site_ids['www.w.org/make/'] ) ), $found );
    527                 }
    528 
    529                 public function test_wp_site_query_by_lang_id() {
    530                         $q     = new WP_Site_Query();
    531                         $found = $q->query(
    532                                 array(
    533                                         'fields'  => 'ids',
    534                                         'lang_id' => 1,
    535                                 )
    536                         );
    537 
    538                         $expected = array(
    539                                 self::$site_ids['www.w.org/make/'],
    540                         );
    541 
    542                         $this->assertSameSets( $expected, $found );
    543                 }
    544 
    545                 public function test_wp_site_query_by_lang_id_with_no_results() {
    546                         $q     = new WP_Site_Query();
    547                         $found = $q->query(
    548                                 array(
    549                                         'fields'  => 'ids',
    550                                         'lang_id' => 2,
    551                                 )
    552                         );
    553 
    554                         $this->assertEmpty( $found );
    555                 }
    556 
    557                 public function test_wp_site_query_by_lang__in() {
    558                         $q     = new WP_Site_Query();
    559                         $found = $q->query(
    560                                 array(
    561                                         'fields'   => 'ids',
    562                                         'lang__in' => array( 1 ),
    563                                 )
    564                         );
    565 
    566                         $expected = array(
    567                                 self::$site_ids['www.w.org/make/'],
    568                         );
    569 
    570                         $this->assertSameSets( $expected, $found );
    571                 }
    572 
    573                 public function test_wp_site_query_by_lang__in_with_multiple_ids() {
    574                         $q     = new WP_Site_Query();
    575                         $found = $q->query(
    576                                 array(
    577                                         'fields'       => 'ids',
    578                                         // Exclude main site since we don't have control over it here.
    579                                         'site__not_in' => array( 1 ),
    580                                         'lang__in'     => array( 0, 1 ),
    581                                 )
    582                         );
    583 
    584                         $this->assertSameSets( array_values( self::$site_ids ), $found );
    585                 }
    586 
    587                 public function test_wp_site_query_by_lang__not_in() {
    588                         $q     = new WP_Site_Query();
    589                         $found = $q->query(
    590                                 array(
    591                                         'fields'       => 'ids',
    592                                         'lang__not_in' => array( 0 ),
    593                                 )
    594                         );
    595 
    596                         $expected = array(
    597                                 self::$site_ids['www.w.org/make/'],
    598                         );
    599 
    600                         $this->assertSameSets( $expected, $found );
    601                 }
    602 
    603                 public function test_wp_site_query_by_lang__not_in_with_multiple_ids() {
    604                         $q     = new WP_Site_Query();
    605                         $found = $q->query(
    606                                 array(
    607                                         'fields'       => 'ids',
    608                                         'lang__not_in' => array( 0, 1 ),
    609                                 )
    610                         );
    611 
    612                         $this->assertEmpty( $found );
    613                 }
    614 
    615                 public function test_wp_site_query_by_search_with_text_in_domain() {
    616                         $q     = new WP_Site_Query();
    617                         $found = $q->query(
    618                                 array(
    619                                         'fields' => 'ids',
    620                                         'search' => 'ke.wordp',
    621                                 )
    622                         );
    623 
    624                         $expected = array(
    625                                 self::$site_ids['make.wordpress.org/'],
    626                                 self::$site_ids['make.wordpress.org/foo/'],
    627                         );
    628 
    629                         $this->assertSameSets( $expected, $found );
    630                 }
    631 
    632                 public function test_wp_site_query_by_search_with_text_in_path() {
    633                         $q     = new WP_Site_Query();
    634                         $found = $q->query(
    635                                 array(
    636                                         'fields' => 'ids',
    637                                         'search' => 'foo',
    638                                 )
    639                         );
    640 
    641                         $expected = array(
    642                                 self::$site_ids['wordpress.org/foo/'],
    643                                 self::$site_ids['wordpress.org/foo/bar/'],
    644                                 self::$site_ids['make.wordpress.org/foo/'],
    645                                 self::$site_ids['www.w.org/foo/'],
    646                                 self::$site_ids['www.w.org/foo/bar/'],
    647                         );
    648 
    649                         $this->assertSameSets( $expected, $found );
    650                 }
    651 
    652                 public function test_wp_site_query_by_search_with_text_in_path_and_domain() {
    653                         $q     = new WP_Site_Query();
    654                         $found = $q->query(
    655                                 array(
    656                                         'fields' => 'ids',
    657                                         'search' => 'make',
    658                                 )
    659                         );
    660 
    661                         $expected = array(
    662                                 self::$site_ids['make.wordpress.org/'],
    663                                 self::$site_ids['make.wordpress.org/foo/'],
    664                                 self::$site_ids['www.w.org/make/'],
    665                         );
    666 
    667                         $this->assertSameSets( $expected, $found );
    668                 }
    669 
    670                 public function test_wp_site_query_by_search_with_text_in_path_and_domain_order_by_domain_desc() {
    671                         $q     = new WP_Site_Query();
    672                         $found = $q->query(
    673                                 array(
    674                                         'fields'  => 'ids',
    675                                         'search'  => 'make',
    676                                         'order'   => 'DESC',
    677                                         'orderby' => 'domain',
    678                                 )
    679                         );
    680 
    681                         $expected = array(
    682                                 self::$site_ids['www.w.org/make/'],
    683                                 self::$site_ids['make.wordpress.org/'],
    684                                 self::$site_ids['make.wordpress.org/foo/'],
    685                         );
    686 
    687                         $this->assertSame( $expected, $found );
    688                 }
    689 
    690                 public function test_wp_site_query_by_search_with_text_in_path_exclude_domain_from_search() {
    691                         $q     = new WP_Site_Query();
    692                         $found = $q->query(
    693                                 array(
    694                                         'fields'         => 'ids',
    695                                         'search'         => 'make',
    696                                         'search_columns' => array( 'path' ),
    697                                 )
    698                         );
    699 
    700                         $expected = array(
    701                                 self::$site_ids['www.w.org/make/'],
    702                         );
    703 
    704                         $this->assertSame( $expected, $found );
    705                 }
    706 
    707                 public function test_wp_site_query_by_search_with_text_in_domain_exclude_path_from_search() {
    708                         $q     = new WP_Site_Query();
    709                         $found = $q->query(
    710                                 array(
    711                                         'fields'         => 'ids',
    712                                         'search'         => 'make',
    713                                         'search_columns' => array( 'domain' ),
    714                                 )
    715                         );
    716 
    717                         $expected = array(
    718                                 self::$site_ids['make.wordpress.org/'],
    719                                 self::$site_ids['make.wordpress.org/foo/'],
    720                         );
    721 
    722                         $this->assertSame( $expected, $found );
    723                 }
    724 
    725                 public function test_wp_site_query_by_search_with_wildcard_in_text() {
    726                         $q     = new WP_Site_Query();
    727                         $found = $q->query(
    728                                 array(
    729                                         'fields' => 'ids',
    730                                         'search' => 'm*ke',
    731                                 )
    732                         );
    733 
    734                         $expected = array(
    735                                 self::$site_ids['www.w.org/make/'],
    736                                 self::$site_ids['make.wordpress.org/'],
    737                                 self::$site_ids['make.wordpress.org/foo/'],
    738                         );
    739 
    740                         $this->assertSameSets( $expected, $found );
    741                 }
    742 
    743                 public function test_wp_site_query_by_search_with_wildcard_in_text_exclude_path_from_search() {
    744                         $q     = new WP_Site_Query();
    745                         $found = $q->query(
    746                                 array(
    747                                         'fields'         => 'ids',
    748                                         'search'         => 'm*ke',
    749                                         'search_columns' => array( 'domain' ),
    750                                 )
    751                         );
    752 
    753                         $expected = array(
    754                                 self::$site_ids['make.wordpress.org/'],
    755                                 self::$site_ids['make.wordpress.org/foo/'],
    756                         );
    757 
    758                         $this->assertSameSets( $expected, $found );
    759                 }
    760 
    761                 public function test_wp_site_query_by_search_with_wildcard_in_text_exclude_domain_from_search() {
    762                         $q     = new WP_Site_Query();
    763                         $found = $q->query(
    764                                 array(
    765                                         'fields'         => 'ids',
    766                                         'search'         => 'm*ke',
    767                                         'search_columns' => array( 'path' ),
    768                                 )
    769                         );
    770 
    771                         $expected = array(
    772                                 self::$site_ids['www.w.org/make/'],
    773                         );
    774 
    775                         $this->assertSameSets( $expected, $found );
    776                 }
    777 
    778                 /**
    779                  * @ticket 41197
    780                  */
    781                 public function test_wp_site_query_cache_with_different_fields_no_count() {
    782                         $q                 = new WP_Site_Query();
    783                         $query_1           = $q->query(
    784                                 array(
    785                                         'fields'     => 'all',
    786                                         'network_id' => self::$network_ids['wordpress.org/'],
    787                                         'number'     => 3,
    788                                         'order'      => 'ASC',
    789                                 )
    790                         );
    791                         $number_of_queries = get_num_queries();
    792 
    793                         $query_2 = $q->query(
    794                                 array(
    795                                         'fields'     => 'ids',
    796                                         'network_id' => self::$network_ids['wordpress.org/'],
    797                                         'number'     => 3,
    798                                         'order'      => 'ASC',
    799                                 )
    800                         );
    801 
    802                         $this->assertSame( $number_of_queries, get_num_queries() );
    803                 }
    804 
    805                 /**
    806                  * @ticket 41197
    807                  */
    808                 public function test_wp_site_query_cache_with_different_fields_active_count() {
    809                         $q = new WP_Site_Query();
    810 
    811                         $query_1           = $q->query(
    812                                 array(
    813                                         'fields'     => 'all',
    814                                         'network_id' => self::$network_ids['wordpress.org/'],
    815                                         'number'     => 3,
    816                                         'order'      => 'ASC',
    817                                         'count'      => true,
    818                                 )
    819                         );
    820                         $number_of_queries = get_num_queries();
    821 
    822                         $query_2 = $q->query(
    823                                 array(
    824                                         'fields'     => 'ids',
    825                                         'network_id' => self::$network_ids['wordpress.org/'],
    826                                         'number'     => 3,
    827                                         'order'      => 'ASC',
    828                                         'count'      => true,
    829                                 )
    830                         );
    831                         $this->assertSame( $number_of_queries, get_num_queries() );
    832                 }
    833 
    834                 /**
    835                  * @ticket 41197
    836                  */
    837                 public function test_wp_site_query_cache_with_same_fields_different_count() {
    838                         $q = new WP_Site_Query();
    839 
    840                         $query_1 = $q->query(
    841                                 array(
    842                                         'fields'     => 'ids',
    843                                         'network_id' => self::$network_ids['wordpress.org/'],
    844                                         'number'     => 3,
    845                                         'order'      => 'ASC',
    846                                 )
    847                         );
    848 
    849                         $number_of_queries = get_num_queries();
    850 
    851                         $query_2 = $q->query(
    852                                 array(
    853                                         'fields'     => 'ids',
    854                                         'network_id' => self::$network_ids['wordpress.org/'],
    855                                         'number'     => 3,
    856                                         'order'      => 'ASC',
    857                                         'count'      => true,
    858                                 )
    859                         );
    860                         $this->assertSame( $number_of_queries + 1, get_num_queries() );
    861                 }
    862 
    863                 /**
    864                  * @ticket 55462
    865                  */
    866                 public function test_wp_site_query_cache_with_same_fields_same_cache_fields() {
    867                         $q = new WP_Site_Query();
    868 
    869                         $query_1 = $q->query(
    870                                 array(
    871                                         'fields'                 => 'ids',
    872                                         'network_id'             => self::$network_ids['wordpress.org/'],
    873                                         'number'                 => 3,
    874                                         'order'                  => 'ASC',
    875                                         'update_site_cache'      => true,
    876                                         'update_site_meta_cache' => true,
    877                                 )
    878                         );
    879 
    880                         $number_of_queries = get_num_queries();
    881 
    882                         $query_2 = $q->query(
    883                                 array(
    884                                         'fields'                 => 'ids',
    885                                         'network_id'             => self::$network_ids['wordpress.org/'],
    886                                         'number'                 => 3,
    887                                         'order'                  => 'ASC',
    888                                         'update_site_cache'      => true,
    889                                         'update_site_meta_cache' => true,
    890                                 )
    891                         );
    892                         $this->assertSame( $number_of_queries, get_num_queries() );
    893                 }
    894 
    895                 /**
    896                  * @ticket 55462
    897                  */
    898                 public function test_wp_site_query_cache_with_same_fields_different_cache_fields() {
    899                         $q = new WP_Site_Query();
    900 
    901                         $query_1 = $q->query(
    902                                 array(
    903                                         'fields'                 => 'ids',
    904                                         'network_id'             => self::$network_ids['wordpress.org/'],
    905                                         'number'                 => 3,
    906                                         'order'                  => 'ASC',
    907                                         'update_site_cache'      => true,
    908                                         'update_site_meta_cache' => true,
    909                                 )
    910                         );
    911 
    912                         $number_of_queries = get_num_queries();
    913 
    914                         $query_2 = $q->query(
    915                                 array(
    916                                         'fields'                 => 'ids',
    917                                         'network_id'             => self::$network_ids['wordpress.org/'],
    918                                         'number'                 => 3,
    919                                         'order'                  => 'ASC',
    920                                         'update_site_cache'      => false,
    921                                         'update_site_meta_cache' => false,
    922                                 )
    923                         );
    924                         $this->assertSame( $number_of_queries, get_num_queries() );
    925                 }
    926 
    927                 /**
    928                  * @ticket 40229
    929                  * @dataProvider data_wp_site_query_meta_query
    930                  */
    931                 public function test_wp_site_query_meta_query( $query, $expected, $strict ) {
    932                         if ( ! is_site_meta_supported() ) {
    933                                 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
    934                         }
    935 
    936                         add_site_meta( self::$site_ids['wordpress.org/'], 'foo', 'foo' );
    937                         add_site_meta( self::$site_ids['wordpress.org/foo/'], 'foo', 'bar' );
    938                         add_site_meta( self::$site_ids['wordpress.org/foo/bar/'], 'foo', 'baz' );
    939                         add_site_meta( self::$site_ids['make.wordpress.org/'], 'bar', 'baz' );
    940                         add_site_meta( self::$site_ids['wordpress.org/'], 'numberfoo', 1 );
    941                         add_site_meta( self::$site_ids['wordpress.org/foo/'], 'numberfoo', 2 );
    942 
    943                         $query['fields'] = 'ids';
    944 
    945                         $q     = new WP_Site_Query();
    946                         $found = $q->query( $query );
    947 
    948                         foreach ( $expected as $index => $domain_path ) {
    949                                 $expected[ $index ] = self::$site_ids[ $domain_path ];
    950                         }
    951 
    952                         if ( $strict ) {
    953                                 $this->assertSame( $expected, $found );
    954                         } else {
    955                                 $this->assertSameSets( $expected, $found );
    956                         }
    957                 }
    958 
    959                 public function data_wp_site_query_meta_query() {
    960                         return array(
    961                                 array(
    962                                         array(
    963                                                 'meta_key' => 'foo',
    964                                         ),
    965                                         array(
    966                                                 'wordpress.org/',
    967                                                 'wordpress.org/foo/',
    968                                                 'wordpress.org/foo/bar/',
    969                                         ),
    970                                         false,
    971                                 ),
    972                                 array(
    973                                         array(
    974                                                 'meta_key'   => 'foo',
    975                                                 'meta_value' => 'bar',
    976                                         ),
    977                                         array(
    978                                                 'wordpress.org/foo/',
    979                                         ),
    980                                         false,
    981                                 ),
    982                                 array(
    983                                         array(
    984                                                 'meta_key'     => 'foo',
    985                                                 'meta_value'   => array( 'bar', 'baz' ),
    986                                                 'meta_compare' => 'IN',
    987                                         ),
    988                                         array(
    989                                                 'wordpress.org/foo/',
    990                                                 'wordpress.org/foo/bar/',
    991                                         ),
    992                                         false,
    993                                 ),
    994                                 array(
    995                                         array(
    996                                                 'meta_query' => array(
    997                                                         array(
    998                                                                 'key'   => 'foo',
    999                                                                 'value' => 'bar',
    1000                                                         ),
    1001                                                         array(
    1002                                                                 'key'   => 'numberfoo',
    1003                                                                 'value' => 2,
    1004                                                                 'type'  => 'NUMERIC',
    1005                                                         ),
     956        }
     957
     958        public function data_wp_site_query_meta_query() {
     959                return array(
     960                        array(
     961                                array(
     962                                        'meta_key' => 'foo',
     963                                ),
     964                                array(
     965                                        'wordpress.org/',
     966                                        'wordpress.org/foo/',
     967                                        'wordpress.org/foo/bar/',
     968                                ),
     969                                false,
     970                        ),
     971                        array(
     972                                array(
     973                                        'meta_key'   => 'foo',
     974                                        'meta_value' => 'bar',
     975                                ),
     976                                array(
     977                                        'wordpress.org/foo/',
     978                                ),
     979                                false,
     980                        ),
     981                        array(
     982                                array(
     983                                        'meta_key'     => 'foo',
     984                                        'meta_value'   => array( 'bar', 'baz' ),
     985                                        'meta_compare' => 'IN',
     986                                ),
     987                                array(
     988                                        'wordpress.org/foo/',
     989                                        'wordpress.org/foo/bar/',
     990                                ),
     991                                false,
     992                        ),
     993                        array(
     994                                array(
     995                                        'meta_query' => array(
     996                                                array(
     997                                                        'key'   => 'foo',
     998                                                        'value' => 'bar',
     999                                                ),
     1000                                                array(
     1001                                                        'key'   => 'numberfoo',
     1002                                                        'value' => 2,
     1003                                                        'type'  => 'NUMERIC',
    10061004                                                ),
    10071005                                        ),
    1008                                         array(
    1009                                                 'wordpress.org/foo/',
     1006                                ),
     1007                                array(
     1008                                        'wordpress.org/foo/',
     1009                                ),
     1010                                false,
     1011                        ),
     1012                        array(
     1013                                array(
     1014                                        'meta_key' => 'foo',
     1015                                        'orderby'  => 'meta_value',
     1016                                        'order'    => 'ASC',
     1017                                ),
     1018                                array(
     1019                                        'wordpress.org/foo/',
     1020                                        'wordpress.org/foo/bar/',
     1021                                        'wordpress.org/',
     1022                                ),
     1023                                true,
     1024                        ),
     1025                        array(
     1026                                array(
     1027                                        'meta_key' => 'foo',
     1028                                        'orderby'  => 'foo',
     1029                                        'order'    => 'ASC',
     1030                                ),
     1031                                array(
     1032                                        'wordpress.org/foo/',
     1033                                        'wordpress.org/foo/bar/',
     1034                                        'wordpress.org/',
     1035                                ),
     1036                                true,
     1037                        ),
     1038                        array(
     1039                                array(
     1040                                        'meta_key' => 'numberfoo',
     1041                                        'orderby'  => 'meta_value_num',
     1042                                        'order'    => 'DESC',
     1043                                ),
     1044                                array(
     1045                                        'wordpress.org/foo/',
     1046                                        'wordpress.org/',
     1047                                ),
     1048                                true,
     1049                        ),
     1050                        array(
     1051                                array(
     1052                                        'meta_query' => array(
     1053                                                array(
     1054                                                        'key'     => 'foo',
     1055                                                        'value'   => array( 'foo', 'bar' ),
     1056                                                        'compare' => 'IN',
     1057                                                ),
     1058                                                array(
     1059                                                        'key' => 'numberfoo',
     1060                                                ),
    10101061                                        ),
    1011                                         false,
    1012                                 ),
    1013                                 array(
    1014                                         array(
    1015                                                 'meta_key' => 'foo',
    1016                                                 'orderby'  => 'meta_value',
    1017                                                 'order'    => 'ASC',
     1062                                        'orderby'    => array( 'meta_value' => 'ASC' ),
     1063                                ),
     1064                                array(
     1065                                        'wordpress.org/foo/',
     1066                                        'wordpress.org/',
     1067                                ),
     1068                                true,
     1069                        ),
     1070                        array(
     1071                                array(
     1072                                        'meta_query' => array(
     1073                                                array(
     1074                                                        'key'     => 'foo',
     1075                                                        'value'   => array( 'foo', 'bar' ),
     1076                                                        'compare' => 'IN',
     1077                                                ),
     1078                                                array(
     1079                                                        'key' => 'numberfoo',
     1080                                                ),
    10181081                                        ),
    1019                                         array(
    1020                                                 'wordpress.org/foo/',
    1021                                                 'wordpress.org/foo/bar/',
    1022                                                 'wordpress.org/',
     1082                                        'orderby'    => array( 'foo' => 'ASC' ),
     1083                                ),
     1084                                array(
     1085                                        'wordpress.org/foo/',
     1086                                        'wordpress.org/',
     1087                                ),
     1088                                true,
     1089                        ),
     1090                        array(
     1091                                array(
     1092                                        'meta_query' => array(
     1093                                                array(
     1094                                                        'key'     => 'foo',
     1095                                                        'value'   => array( 'foo', 'bar' ),
     1096                                                        'compare' => 'IN',
     1097                                                ),
     1098                                                'my_subquery' => array(
     1099                                                        'key' => 'numberfoo',
     1100                                                ),
    10231101                                        ),
    1024                                         true,
    1025                                 ),
    1026                                 array(
    1027                                         array(
    1028                                                 'meta_key' => 'foo',
    1029                                                 'orderby'  => 'foo',
    1030                                                 'order'    => 'ASC',
    1031                                         ),
    1032                                         array(
    1033                                                 'wordpress.org/foo/',
    1034                                                 'wordpress.org/foo/bar/',
    1035                                                 'wordpress.org/',
    1036                                         ),
    1037                                         true,
    1038                                 ),
    1039                                 array(
    1040                                         array(
    1041                                                 'meta_key' => 'numberfoo',
    1042                                                 'orderby'  => 'meta_value_num',
    1043                                                 'order'    => 'DESC',
    1044                                         ),
    1045                                         array(
    1046                                                 'wordpress.org/foo/',
    1047                                                 'wordpress.org/',
    1048                                         ),
    1049                                         true,
    1050                                 ),
    1051                                 array(
    1052                                         array(
    1053                                                 'meta_query' => array(
    1054                                                         array(
    1055                                                                 'key'     => 'foo',
    1056                                                                 'value'   => array( 'foo', 'bar' ),
    1057                                                                 'compare' => 'IN',
    1058                                                         ),
    1059                                                         array(
    1060                                                                 'key' => 'numberfoo',
    1061                                                         ),
    1062                                                 ),
    1063                                                 'orderby'    => array( 'meta_value' => 'ASC' ),
    1064                                         ),
    1065                                         array(
    1066                                                 'wordpress.org/foo/',
    1067                                                 'wordpress.org/',
    1068                                         ),
    1069                                         true,
    1070                                 ),
    1071                                 array(
    1072                                         array(
    1073                                                 'meta_query' => array(
    1074                                                         array(
    1075                                                                 'key'     => 'foo',
    1076                                                                 'value'   => array( 'foo', 'bar' ),
    1077                                                                 'compare' => 'IN',
    1078                                                         ),
    1079                                                         array(
    1080                                                                 'key' => 'numberfoo',
    1081                                                         ),
    1082                                                 ),
    1083                                                 'orderby'    => array( 'foo' => 'ASC' ),
    1084                                         ),
    1085                                         array(
    1086                                                 'wordpress.org/foo/',
    1087                                                 'wordpress.org/',
    1088                                         ),
    1089                                         true,
    1090                                 ),
    1091                                 array(
    1092                                         array(
    1093                                                 'meta_query' => array(
    1094                                                         array(
    1095                                                                 'key'     => 'foo',
    1096                                                                 'value'   => array( 'foo', 'bar' ),
    1097                                                                 'compare' => 'IN',
    1098                                                         ),
    1099                                                         'my_subquery' => array(
    1100                                                                 'key' => 'numberfoo',
    1101                                                         ),
    1102                                                 ),
    1103                                                 'orderby'    => array( 'my_subquery' => 'DESC' ),
    1104                                         ),
    1105                                         array(
    1106                                                 'wordpress.org/foo/',
    1107                                                 'wordpress.org/',
    1108                                         ),
    1109                                         true,
    1110                                 ),
    1111                         );
    1112                 }
    1113 
    1114                 /**
    1115                  * @ticket 45749
    1116                  * @ticket 47599
    1117                  */
    1118                 public function test_sites_pre_query_filter_should_bypass_database_query() {
    1119                         add_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query' ), 10, 2 );
    1120 
    1121                         $num_queries = get_num_queries();
    1122 
    1123                         $q       = new WP_Site_Query();
    1124                         $results = $q->query( array() );
    1125 
    1126                         remove_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query' ), 10, 2 );
    1127 
    1128                         // Make sure no queries were executed.
    1129                         $this->assertSame( $num_queries, get_num_queries() );
    1130 
    1131                         // We manually inserted a non-existing site and overrode the results with it.
    1132                         $this->assertSame( array( 555 ), $results );
    1133 
    1134                         // Make sure manually setting found_sites doesn't get overwritten.
    1135                         $this->assertSame( 1, $q->found_sites );
    1136                 }
    1137 
    1138                 public static function filter_sites_pre_query( $sites, $query ) {
    1139                         $query->found_sites = 1;
    1140 
    1141                         return array( 555 );
    1142                 }
    1143 
    1144                 /**
    1145                  * @ticket 51333
    1146                  */
    1147                 public function test_sites_pre_query_filter_should_set_sites_property() {
    1148                         add_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query_and_set_sites' ), 10, 2 );
    1149 
    1150                         $q       = new WP_Site_Query();
    1151                         $results = $q->query( array() );
    1152 
    1153                         remove_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query_and_set_sites' ), 10 );
    1154 
    1155                         // Make sure the sites property is the same as the results.
    1156                         $this->assertSame( $results, $q->sites );
    1157 
    1158                         // Make sure the site domain is `wordpress.org`.
    1159                         $this->assertSame( 'wordpress.org', $q->sites[0]->domain );
    1160                 }
    1161 
    1162                 public static function filter_sites_pre_query_and_set_sites( $sites, $query ) {
    1163                         return array( get_site( self::$site_ids['wordpress.org/'] ) );
    1164                 }
    1165 
    1166                 /**
    1167                  * @ticket 56841
    1168                  */
    1169                 public function test_wp_site_query_does_not_have_leading_whitespace() {
    1170                         $q = new WP_Site_Query();
    1171 
    1172                         $q->query(
    1173                                 array(
    1174                                         'fields'                 => 'ids',
    1175                                         'network_id'             => self::$network_ids['wordpress.org/'],
    1176                                         'number'                 => 3,
    1177                                         'order'                  => 'ASC',
    1178                                         'update_site_cache'      => true,
    1179                                         'update_site_meta_cache' => true,
    1180                                 )
    1181                         );
    1182 
    1183                         $this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' );
    1184                 }
    1185         }
    1186 
    1187 endif;
     1102                                        'orderby'    => array( 'my_subquery' => 'DESC' ),
     1103                                ),
     1104                                array(
     1105                                        'wordpress.org/foo/',
     1106                                        'wordpress.org/',
     1107                                ),
     1108                                true,
     1109                        ),
     1110                );
     1111        }
     1112
     1113        /**
     1114         * @ticket 45749
     1115         * @ticket 47599
     1116         */
     1117        public function test_sites_pre_query_filter_should_bypass_database_query() {
     1118                add_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query' ), 10, 2 );
     1119
     1120                $num_queries = get_num_queries();
     1121
     1122                $q       = new WP_Site_Query();
     1123                $results = $q->query( array() );
     1124
     1125                remove_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query' ), 10, 2 );
     1126
     1127                // Make sure no queries were executed.
     1128                $this->assertSame( $num_queries, get_num_queries() );
     1129
     1130                // We manually inserted a non-existing site and overrode the results with it.
     1131                $this->assertSame( array( 555 ), $results );
     1132
     1133                // Make sure manually setting found_sites doesn't get overwritten.
     1134                $this->assertSame( 1, $q->found_sites );
     1135        }
     1136
     1137        public static function filter_sites_pre_query( $sites, $query ) {
     1138                $query->found_sites = 1;
     1139
     1140                return array( 555 );
     1141        }
     1142
     1143        /**
     1144         * @ticket 51333
     1145         */
     1146        public function test_sites_pre_query_filter_should_set_sites_property() {
     1147                add_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query_and_set_sites' ), 10, 2 );
     1148
     1149                $q       = new WP_Site_Query();
     1150                $results = $q->query( array() );
     1151
     1152                remove_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query_and_set_sites' ), 10 );
     1153
     1154                // Make sure the sites property is the same as the results.
     1155                $this->assertSame( $results, $q->sites );
     1156
     1157                // Make sure the site domain is `wordpress.org`.
     1158                $this->assertSame( 'wordpress.org', $q->sites[0]->domain );
     1159        }
     1160
     1161        public static function filter_sites_pre_query_and_set_sites( $sites, $query ) {
     1162                return array( get_site( self::$site_ids['wordpress.org/'] ) );
     1163        }
     1164
     1165        /**
     1166         * @ticket 56841
     1167         */
     1168        public function test_wp_site_query_does_not_have_leading_whitespace() {
     1169                $q = new WP_Site_Query();
     1170
     1171                $q->query(
     1172                        array(
     1173                                'fields'                 => 'ids',
     1174                                'network_id'             => self::$network_ids['wordpress.org/'],
     1175                                'number'                 => 3,
     1176                                'order'                  => 'ASC',
     1177                                'update_site_cache'      => true,
     1178                                'update_site_meta_cache' => true,
     1179                        )
     1180                );
     1181
     1182                $this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' );
     1183        }
     1184}
Note: See TracChangeset for help on using the changeset viewer.