Make WordPress Core


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