123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- <?php
- /**
- * @group taxonomy
- */
- class Tests_Term_Tax_Query extends WP_UnitTestCase {
- protected $q;
- public function setUp() {
- parent::setUp();
- unset( $this->q );
- $this->q = new WP_Query();
- }
- public function test_construct_with_relation_default() {
- $tq = new WP_Tax_Query( array() );
- $this->assertSame( 'AND', $tq->relation );
- }
- public function test_construct_with_relation_or_lowercase() {
- $tq = new WP_Tax_Query(
- array(
- 'relation' => 'or',
- )
- );
- $this->assertSame( 'OR', $tq->relation );
- }
- public function test_construct_with_relation_or_uppercase() {
- $tq = new WP_Tax_Query(
- array(
- 'relation' => 'OR',
- )
- );
- $this->assertSame( 'OR', $tq->relation );
- }
- public function test_construct_with_relation_other() {
- $tq = new WP_Tax_Query(
- array(
- 'relation' => 'foo',
- )
- );
- $this->assertSame( 'AND', $tq->relation );
- }
- public function test_construct_fill_missing_query_params() {
- $tq = new WP_Tax_Query(
- array(
- array(),
- )
- );
- $expected = array(
- 'taxonomy' => '',
- 'terms' => array(),
- 'include_children' => true,
- 'field' => 'term_id',
- 'operator' => 'IN',
- );
- $this->assertEquals( $expected, $tq->queries[0] );
- }
- public function test_construct_fill_missing_query_params_merge_with_passed_values() {
- $tq = new WP_Tax_Query(
- array(
- array(
- 'taxonomy' => 'foo',
- 'include_children' => false,
- 'foo' => 'bar',
- ),
- )
- );
- $expected = array(
- 'taxonomy' => 'foo',
- 'terms' => array(),
- 'include_children' => false,
- 'field' => 'term_id',
- 'operator' => 'IN',
- 'foo' => 'bar',
- );
- $this->assertEquals( $expected, $tq->queries[0] );
- }
- public function test_construct_cast_terms_to_array() {
- $tq = new WP_Tax_Query(
- array(
- array(
- 'terms' => 'foo',
- ),
- )
- );
- $this->assertSame( array( 'foo' ), $tq->queries[0]['terms'] );
- }
- /**
- * @ticket 30117
- */
- public function test_construct_empty_strings_array_members_should_be_discarded() {
- $q = new WP_Tax_Query(
- array(
- '',
- array(
- 'taxonomy' => 'post_tag',
- 'terms' => 'foo',
- ),
- )
- );
- $this->assertSame( 1, count( $q->queries ) );
- }
- public function test_transform_query_terms_empty() {
- $tq = new WP_Tax_Query(
- array(
- array(),
- )
- );
- $query = $tq->queries[0];
- $tq->transform_query( $tq->queries[0], 'term_id' );
- $this->assertSame( $query, $tq->queries[0] );
- }
- public function test_transform_query_field_same_as_resulting_field() {
- $tq = new WP_Tax_Query(
- array(
- array(
- 'field' => 'term_id',
- ),
- )
- );
- $query = $tq->queries[0];
- $tq->transform_query( $tq->queries[0], 'term_id' );
- $this->assertSame( $query, $tq->queries[0] );
- }
- public function test_transform_query_resulting_field_sanitized() {
- $t1 = self::factory()->category->create( array( 'slug' => 'foo' ) );
- $t2 = self::factory()->category->create( array( 'slug' => 'bar' ) );
- $p = self::factory()->post->create();
- wp_set_post_categories( $p, $t1 );
- $tq1 = new WP_Tax_Query(
- array(
- array(
- 'terms' => array( 'foo' ),
- 'field' => 'slug',
- ),
- )
- );
- $tq1->transform_query( $tq1->queries[0], 'term_taxonomy_id' );
- $tq2 = new WP_Tax_Query(
- array(
- array(
- 'terms' => array( 'foo' ),
- 'field' => 'slug',
- ),
- )
- );
- $tq2->transform_query( $tq2->queries[0], 'TERM_ ta%xonomy_id' );
- $this->assertSame( $tq1->queries[0], $tq2->queries[0] );
- }
- public function test_transform_query_field_slug() {
- $t1 = self::factory()->category->create( array( 'slug' => 'foo' ) );
- $p = self::factory()->post->create();
- $tt_ids = wp_set_post_categories( $p, $t1 );
- $tq = new WP_Tax_Query(
- array(
- array(
- 'taxonomy' => 'category',
- 'terms' => array( 'foo' ),
- 'field' => 'slug',
- ),
- )
- );
- $tq->transform_query( $tq->queries[0], 'term_taxonomy_id' );
- $this->assertEqualSets( $tt_ids, $tq->queries[0]['terms'] );
- $this->assertSame( 'term_taxonomy_id', $tq->queries[0]['field'] );
- }
- public function test_transform_query_field_name() {
- $t1 = self::factory()->category->create(
- array(
- 'slug' => 'foo',
- 'name' => 'Foo',
- )
- );
- $p = self::factory()->post->create();
- $tt_ids = wp_set_post_categories( $p, $t1 );
- $tq = new WP_Tax_Query(
- array(
- array(
- 'taxonomy' => 'category',
- 'terms' => array( 'Foo' ),
- 'field' => 'name',
- ),
- )
- );
- $tq->transform_query( $tq->queries[0], 'term_taxonomy_id' );
- $this->assertEqualSets( $tt_ids, $tq->queries[0]['terms'] );
- $this->assertSame( 'term_taxonomy_id', $tq->queries[0]['field'] );
- }
- public function test_transform_query_field_term_taxonomy_id() {
- $t1 = self::factory()->category->create(
- array(
- 'slug' => 'foo',
- 'name' => 'Foo',
- )
- );
- $p = self::factory()->post->create();
- $tt_ids = wp_set_post_categories( $p, $t1 );
- $tq = new WP_Tax_Query(
- array(
- array(
- 'taxonomy' => 'category',
- 'terms' => $tt_ids,
- 'field' => 'term_taxonomy_id',
- ),
- )
- );
- $tq->transform_query( $tq->queries[0], 'term_id' );
- $this->assertSame( array( $t1 ), $tq->queries[0]['terms'] );
- $this->assertSame( 'term_id', $tq->queries[0]['field'] );
- }
- public function test_transform_query_field_term_taxonomy_default() {
- $t1 = self::factory()->category->create(
- array(
- 'slug' => 'foo',
- 'name' => 'Foo',
- )
- );
- $p = self::factory()->post->create();
- $tt_ids = wp_set_post_categories( $p, $t1 );
- $tq = new WP_Tax_Query(
- array(
- array(
- 'taxonomy' => 'category',
- 'terms' => array( $t1 ),
- 'field' => 'foo', // Anything defaults to term_id.
- ),
- )
- );
- $tq->transform_query( $tq->queries[0], 'term_taxonomy_id' );
- $this->assertEquals( $tt_ids, $tq->queries[0]['terms'] );
- $this->assertSame( 'term_taxonomy_id', $tq->queries[0]['field'] );
- }
- public function test_transform_query_nonexistent_terms() {
- $tq = new WP_Tax_Query(
- array(
- array(
- 'terms' => array( 'foo' ),
- 'field' => 'slug',
- 'operator' => 'AND',
- ),
- )
- );
- $tq->transform_query( $tq->queries[0], 'term_taxonomy_id' );
- $this->assertWPError( $tq->queries[0] );
- }
- /**
- * @ticket 18105
- */
- public function test_get_sql_relation_or_operator_in() {
- register_taxonomy( 'wptests_tax', 'post' );
- $t1 = self::factory()->term->create(
- array(
- 'taxonomy' => 'wptests_tax',
- )
- );
- $t2 = self::factory()->term->create(
- array(
- 'taxonomy' => 'wptests_tax',
- )
- );
- $t3 = self::factory()->term->create(
- array(
- 'taxonomy' => 'wptests_tax',
- )
- );
- $tq = new WP_Tax_Query(
- array(
- 'relation' => 'OR',
- array(
- 'taxonomy' => 'wptests_tax',
- 'field' => 'term_id',
- 'terms' => $t1,
- ),
- array(
- 'taxonomy' => 'wptests_tax',
- 'field' => 'term_id',
- 'terms' => $t2,
- ),
- array(
- 'taxonomy' => 'wptests_tax',
- 'field' => 'term_id',
- 'terms' => $t3,
- ),
- )
- );
- global $wpdb;
- $sql = $tq->get_sql( $wpdb->posts, 'ID' );
- // Only one JOIN is required with OR + IN.
- $this->assertSame( 1, substr_count( $sql['join'], 'JOIN' ) );
- _unregister_taxonomy( 'wptests_tax' );
- }
- /**
- * @ticket 18105
- */
- public function test_get_sql_relation_and_operator_in() {
- register_taxonomy( 'wptests_tax', 'post' );
- $t1 = self::factory()->term->create(
- array(
- 'taxonomy' => 'wptests_tax',
- )
- );
- $t2 = self::factory()->term->create(
- array(
- 'taxonomy' => 'wptests_tax',
- )
- );
- $t3 = self::factory()->term->create(
- array(
- 'taxonomy' => 'wptests_tax',
- )
- );
- $tq = new WP_Tax_Query(
- array(
- 'relation' => 'AND',
- array(
- 'taxonomy' => 'wptests_tax',
- 'field' => 'term_id',
- 'terms' => $t1,
- ),
- array(
- 'taxonomy' => 'wptests_tax',
- 'field' => 'term_id',
- 'terms' => $t2,
- ),
- array(
- 'taxonomy' => 'wptests_tax',
- 'field' => 'term_id',
- 'terms' => $t3,
- ),
- )
- );
- global $wpdb;
- $sql = $tq->get_sql( $wpdb->posts, 'ID' );
- $this->assertSame( 3, substr_count( $sql['join'], 'JOIN' ) );
- _unregister_taxonomy( 'wptests_tax' );
- }
- /**
- * @ticket 18105
- */
- public function test_get_sql_nested_relation_or_operator_in() {
- register_taxonomy( 'wptests_tax', 'post' );
- $t1 = self::factory()->term->create(
- array(
- 'taxonomy' => 'wptests_tax',
- )
- );
- $t2 = self::factory()->term->create(
- array(
- 'taxonomy' => 'wptests_tax',
- )
- );
- $t3 = self::factory()->term->create(
- array(
- 'taxonomy' => 'wptests_tax',
- )
- );
- $tq = new WP_Tax_Query(
- array(
- 'relation' => 'OR',
- array(
- 'taxonomy' => 'wptests_tax',
- 'field' => 'term_id',
- 'terms' => $t1,
- ),
- array(
- 'relation' => 'OR',
- array(
- 'taxonomy' => 'wptests_tax',
- 'field' => 'term_id',
- 'terms' => $t2,
- ),
- array(
- 'taxonomy' => 'wptests_tax',
- 'field' => 'term_id',
- 'terms' => $t3,
- ),
- ),
- )
- );
- global $wpdb;
- $sql = $tq->get_sql( $wpdb->posts, 'ID' );
- $this->assertSame( 2, substr_count( $sql['join'], 'JOIN' ) );
- _unregister_taxonomy( 'wptests_tax' );
- }
- /**
- * @ticket 29738
- */
- public function test_get_sql_operator_not_in_empty_terms() {
- register_taxonomy( 'wptests_tax', 'post' );
- $tq = new WP_Tax_Query(
- array(
- 'relation' => 'OR',
- array(
- 'taxonomy' => 'wptests_tax',
- 'field' => 'term_id',
- 'operator' => 'NOT IN',
- 'terms' => array(),
- ),
- )
- );
- global $wpdb;
- $expected = array(
- 'join' => '',
- 'where' => '',
- );
- $this->assertSame( $expected, $tq->get_sql( $wpdb->posts, 'ID' ) );
- _unregister_taxonomy( 'wptests_tax' );
- }
- /**
- * @ticket 29738
- */
- public function test_get_sql_operator_and_empty_terms() {
- register_taxonomy( 'wptests_tax', 'post' );
- $tq = new WP_Tax_Query(
- array(
- 'relation' => 'OR',
- array(
- 'taxonomy' => 'wptests_tax',
- 'field' => 'term_id',
- 'operator' => 'AND',
- 'terms' => array(),
- ),
- )
- );
- global $wpdb;
- $expected = array(
- 'join' => '',
- 'where' => '',
- );
- $this->assertSame( $expected, $tq->get_sql( $wpdb->posts, 'ID' ) );
- _unregister_taxonomy( 'wptests_tax' );
- }
- }
|