Make WordPress Core


Ignore:
Timestamp:
10/17/2022 12:36:06 PM (2 years ago)
Author:
audrasjb
Message:

Query: Validate relation in WP_Date_Query.

Props dd32, johnjamesjacoby, martinkrcho, ehtis, paulkevan, peterwilsoncc.
Merges [54530] to the 6.0 branch.

Location:
branches/6.0
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.0

  • branches/6.0/tests/phpunit/tests/date/query.php

    r54512 r54540  
    11361136        $this->assertSame( array( $p1, $p2 ), $q->posts );
    11371137    }
     1138
     1139    /**
     1140     * @covers WP_Date_Query::get_sql
     1141     */
     1142    public function test_relation_in_query_and() {
     1143        $date_query = array(
     1144            'relation' => 'AND',
     1145            array(
     1146                'before'    => array(
     1147                    'year'  => 2021,
     1148                    'month' => 9,
     1149                    'day'   => 20,
     1150                ),
     1151                'after'     => array(
     1152                    'year'  => 2019,
     1153                    'month' => 2,
     1154                    'day'   => 25,
     1155                ),
     1156                'inclusive' => true,
     1157            ),
     1158            array(
     1159                'before'    => array(
     1160                    'year'  => 2016,
     1161                    'month' => 9,
     1162                    'day'   => 11,
     1163                ),
     1164                'after'     => array(
     1165                    'year'  => 2014,
     1166                    'month' => 5,
     1167                    'day'   => 12,
     1168                ),
     1169                'inclusive' => false,
     1170            ),
     1171        );
     1172
     1173        $q = new WP_Date_Query( $date_query );
     1174
     1175        $sql = $q->get_sql();
     1176
     1177        $parts = mb_split( '\)\s+AND\s+\(', $sql );
     1178        $this->assertIsArray( $parts, 'SQL query cannot be split into multiple parts using operator AND.' );
     1179        $this->assertEquals( 2, count( $parts ), 'SQL query does not contain correct number of AND operators.' );
     1180
     1181        $this->assertStringNotContainsString( 'OR', $sql, 'SQL query contains conditions joined by operator OR.' );
     1182    }
     1183
     1184    /**
     1185     * @covers WP_Date_Query::get_sql
     1186     */
     1187    public function test_relation_in_query_or() {
     1188        $date_query = array(
     1189            'relation' => 'OR',
     1190            array(
     1191                'before'    => array(
     1192                    'year'  => 2021,
     1193                    'month' => 9,
     1194                    'day'   => 20,
     1195                ),
     1196                'after'     => array(
     1197                    'year'  => 2019,
     1198                    'month' => 2,
     1199                    'day'   => 25,
     1200                ),
     1201                'inclusive' => true,
     1202            ),
     1203            array(
     1204                'before'    => array(
     1205                    'year'  => 2016,
     1206                    'month' => 9,
     1207                    'day'   => 11,
     1208                ),
     1209                'after'     => array(
     1210                    'year'  => 2014,
     1211                    'month' => 5,
     1212                    'day'   => 12,
     1213                ),
     1214                'inclusive' => false,
     1215            ),
     1216        );
     1217
     1218        $q = new WP_Date_Query( $date_query );
     1219
     1220        $sql = $q->get_sql();
     1221
     1222        $this->assertStringContainsString( 'OR', $sql, 'SQL query does not contain conditions joined by operator OR.' );
     1223
     1224        $parts = mb_split( '\)\s+OR\s+\(', $sql );
     1225        $this->assertIsArray( $parts, 'SQL query cannot be split into multiple parts using operator OR.' );
     1226        $this->assertEquals( 2, count( $parts ), 'SQL query does not contain correct number of OR operators.' );
     1227
     1228        // Checking number of occurrences of AND while skipping the one at the beginning.
     1229        $this->assertSame( 2, substr_count( substr( $sql, 5 ), 'AND' ), 'SQL query does not contain expected number conditions joined by operator AND.' );
     1230    }
     1231
     1232    /**
     1233     * @covers WP_Date_Query::get_sql
     1234     */
     1235    public function test_relation_in_query_unsupported() {
     1236        $date_query = array(
     1237            'relation' => 'UNSUPPORTED',
     1238            array(
     1239                'before'    => array(
     1240                    'year'  => 2021,
     1241                    'month' => 9,
     1242                    'day'   => 20,
     1243                ),
     1244                'after'     => array(
     1245                    'year'  => 2019,
     1246                    'month' => 2,
     1247                    'day'   => 25,
     1248                ),
     1249                'inclusive' => true,
     1250            ),
     1251            array(
     1252                'before'    => array(
     1253                    'year'  => 2016,
     1254                    'month' => 9,
     1255                    'day'   => 11,
     1256                ),
     1257                'after'     => array(
     1258                    'year'  => 2014,
     1259                    'month' => 5,
     1260                    'day'   => 12,
     1261                ),
     1262                'inclusive' => false,
     1263            ),
     1264        );
     1265
     1266        $q = new WP_Date_Query( $date_query );
     1267
     1268        $sql = $q->get_sql();
     1269
     1270        $parts = mb_split( '\)\s+AND\s+\(', $sql );
     1271        $this->assertIsArray( $parts, 'SQL query cannot be split into multiple parts using operator AND.' );
     1272        $this->assertEquals( 2, count( $parts ), 'SQL query does not contain correct number of AND operators.' );
     1273
     1274        $this->assertStringNotContainsString( 'OR', $sql, 'SQL query contains conditions joined by operator OR.' );
     1275    }
    11381276}
Note: See TracChangeset for help on using the changeset viewer.