Make WordPress Core


Ignore:
Timestamp:
08/13/2022 10:29:19 PM (2 years ago)
Author:
johnbillion
Message:

Query: Be better at forcing data types for query vars.

Several query vars only accept a scalar value and pass the value through functions that assume a scalar value. Adding extra guard conditions to the types of query vars doesn't affect their functionality but does remove PHP notices and warnings that can otherwise be generated when a non-scalar value such as an array is present in a query var.

Props juliobox, xknown, SergeyBiryukov, dave1010, nacin, tellyworth, dd32, audrasjb, johnregan3

Fixes #17737

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/query/parseQuery.php

    r48937 r53891  
    105105    }
    106106
     107    /**
     108     * Ensure an array of authors is rejected.
     109     *
     110     * @ticket 17737
     111     */
     112    public function test_parse_query_author_array() {
     113        $q = new WP_Query();
     114        $q->parse_query(
     115            array(
     116                'author' => array( 1, 2, 3 ),
     117            )
     118        );
     119
     120        $this->assertEmpty( $q->query_vars['author'] );
     121    }
     122
     123    /**
     124     * Ensure a non-scalar (non-numeric) author value is rejected.
     125     *
     126     * @ticket 17737
     127     */
     128    public function test_parse_query_author_string() {
     129        $q = new WP_Query();
     130        $q->parse_query(
     131            array(
     132                'author' => 'admin',
     133            )
     134        );
     135
     136        $this->assertEmpty( $q->query_vars['author'] );
     137    }
     138
     139    /**
     140     * Ensure nonscalar 'cat' array values are rejected.
     141     *
     142     * Note the returned 'cat' query_var value is a string.
     143     *
     144     * @ticket 17737
     145     */
     146    public function test_parse_query_cat_array_mixed() {
     147        $q = new WP_Query();
     148        $q->parse_query(
     149            array(
     150                'cat' => array( 1, 'uncategorized', '-1' ),
     151            )
     152        );
     153
     154        $this->assertSame( '1,-1', $q->query_vars['cat'] );
     155    }
     156
     157    /**
     158     * Ensure a nonscalar menu_order value is rejected.
     159     *
     160     * @ticket 17737
     161     */
     162    public function test_parse_query_menu_order_nonscalar() {
     163        $q = new WP_Query();
     164        $q->parse_query(
     165            array(
     166                'menu_order' => array( 1 ),
     167            )
     168        );
     169
     170        $this->assertEmpty( $q->query_vars['menu_order'] );
     171    }
     172
     173    /**
     174     * Ensure numeric 'subpost' gets assigned to 'attachment'.
     175     *
     176     * @ticket 17737
     177     */
     178    public function test_parse_query_subpost_scalar() {
     179        $q = new WP_Query();
     180        $q->parse_query(
     181            array(
     182                'subpost' => 1,
     183            )
     184        );
     185
     186        $this->assertSame( 1, $q->query_vars['attachment'] );
     187    }
     188
     189    /**
     190     * Ensure non-scalar 'subpost' does not get assigned to 'attachment'.
     191     *
     192     * @ticket 17737
     193     */
     194    public function test_parse_query_subpost_nonscalar() {
     195        $q = new WP_Query();
     196        $q->parse_query(
     197            array(
     198                'subpost' => array( 1 ),
     199            )
     200        );
     201
     202        $this->assertEmpty( $q->query_vars['attachment'] );
     203    }
     204
     205    /**
     206     * Ensure numeric 'attachment_id' value is assigned.
     207     *
     208     * @ticket 17737
     209     */
     210    public function test_parse_query_attachment_id() {
     211        $q = new WP_Query();
     212        $q->parse_query(
     213            array(
     214                'attachment_id' => 1,
     215            )
     216        );
     217
     218        $this->assertSame( 1, $q->query_vars['attachment_id'] );
     219    }
     220
     221    /**
     222     * Ensure non-scalar 'attachment_id' value is rejected.
     223     *
     224     * @ticket 17737
     225     */
     226    public function test_parse_query_attachment_id_nonscalar() {
     227        $q = new WP_Query();
     228        $q->parse_query(
     229            array(
     230                'attachment_id' => array( 1 ),
     231            )
     232        );
     233
     234        $this->assertEmpty( $q->query_vars['attachment_id'] );
     235    }
    107236}
Note: See TracChangeset for help on using the changeset viewer.