Make WordPress Core

Changeset 54540


Ignore:
Timestamp:
10/17/2022 12:36:06 PM (4 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:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/6.0

  • branches/6.0/src/wp-includes/class-wp-date-query.php

    r52652 r54540  
    150150                }
    151151
    152                 if ( isset( $date_query['relation'] ) && 'OR' === strtoupper( $date_query['relation'] ) ) {
    153                         $this->relation = 'OR';
     152                if ( isset( $date_query['relation'] ) ) {
     153                        $this->relation = $this->sanitize_relation( $date_query['relation'] );
    154154                } else {
    155155                        $this->relation = 'AND';
     
    219219                        $this->validate_date_values( $queries );
    220220                }
     221
     222                // Sanitize the relation parameter.
     223                $queries['relation'] = $this->sanitize_relation( $queries['relation'] );
    221224
    222225                foreach ( $queries as $key => $q ) {
     
    10411044                return $wpdb->prepare( "DATE_FORMAT( $column, %s ) $compare %f", $format, $time );
    10421045        }
     1046
     1047        /**
     1048         * Sanitizes a 'relation' operator.
     1049         *
     1050         * @since 6.0.3
     1051         *
     1052         * @param string $relation Raw relation key from the query argument.
     1053         * @return string Sanitized relation ('AND' or 'OR').
     1054         */
     1055        public function sanitize_relation( $relation ) {
     1056                if ( 'OR' === strtoupper( $relation ) ) {
     1057                        return 'OR';
     1058                } else {
     1059                        return 'AND';
     1060                }
     1061        }
    10431062}
  • 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}
  • branches/6.0/tests/phpunit/tests/term/taxQuery.php

    r51568 r54540  
    336336        /**
    337337         * @ticket 18105
     338         * @covers WP_Tax_Query::get_sql
    338339         */
    339340        public function test_get_sql_relation_and_operator_in() {
     
    382383                $this->assertSame( 3, substr_count( $sql['join'], 'JOIN' ) );
    383384
     385                // Checking number of occurrences of AND while skipping the one at the beginning.
     386                $this->assertSame( 2, substr_count( substr( $sql['where'], 5 ), 'AND' ), 'SQL query does not contain expected number conditions joined by operator AND.' );
     387
     388                $this->assertStringNotContainsString( 'OR', $sql['where'], 'SQL query contains conditions joined by operator OR.' );
     389
    384390                _unregister_taxonomy( 'wptests_tax' );
    385391        }
     
    387393        /**
    388394         * @ticket 18105
     395         * @covers WP_Tax_Query::get_sql
    389396         */
    390397        public function test_get_sql_nested_relation_or_operator_in() {
     
    435442
    436443                $this->assertSame( 2, substr_count( $sql['join'], 'JOIN' ) );
     444                $this->assertSame( 2, substr_count( $sql['where'], 'OR' ), 'SQL query does not contain expected number conditions joined by operator OR.' );
     445                $this->assertStringNotContainsString( 'AND', substr( $sql['where'], 5 ), 'SQL query contains conditions joined by operator AND.' );
    437446
    438447                _unregister_taxonomy( 'wptests_tax' );
     
    496505                _unregister_taxonomy( 'wptests_tax' );
    497506        }
     507
     508        /**
     509         * @ticket 18105
     510         * @covers WP_Tax_Query::get_sql
     511         */
     512        public function test_get_sql_relation_unsupported() {
     513                register_taxonomy( 'wptests_tax', 'post' );
     514
     515                $t1 = self::factory()->term->create(
     516                        array(
     517                                'taxonomy' => 'wptests_tax',
     518                        )
     519                );
     520                $t2 = self::factory()->term->create(
     521                        array(
     522                                'taxonomy' => 'wptests_tax',
     523                        )
     524                );
     525                $t3 = self::factory()->term->create(
     526                        array(
     527                                'taxonomy' => 'wptests_tax',
     528                        )
     529                );
     530
     531                $tq = new WP_Tax_Query(
     532                        array(
     533                                'relation' => 'UNSUPPORTED',
     534                                array(
     535                                        'taxonomy' => 'wptests_tax',
     536                                        'field'    => 'term_id',
     537                                        'terms'    => $t1,
     538                                ),
     539                                array(
     540                                        'taxonomy' => 'wptests_tax',
     541                                        'field'    => 'term_id',
     542                                        'terms'    => $t2,
     543                                ),
     544                                array(
     545                                        'taxonomy' => 'wptests_tax',
     546                                        'field'    => 'term_id',
     547                                        'terms'    => $t3,
     548                                ),
     549                        )
     550                );
     551
     552                global $wpdb;
     553                $sql = $tq->get_sql( $wpdb->posts, 'ID' );
     554
     555                // Checking number of occurrences of AND while skipping the one at the beginning.
     556                $this->assertSame( 2, substr_count( substr( $sql['where'], 5 ), 'AND' ), 'SQL query does not contain expected number conditions joined by operator AND.' );
     557
     558                $this->assertStringNotContainsString( 'UNSUPPORTED', $sql['where'], 'SQL query contains unsupported relation operator.' );
     559                $this->assertStringNotContainsString( 'OR', $sql['where'], 'SQL query contains conditions joined by operator OR.' );
     560
     561                _unregister_taxonomy( 'wptests_tax' );
     562        }
    498563}
Note: See TracChangeset for help on using the changeset viewer.