wordpress-develop/tests/phpunit/tests/term/taxQuery.php
Sergey Biryukov ddb409edca Build/Test Tools: Implement use of the void solution.
> PHPUnit 8.0.0 introduced a `void` return type declaration to the "fixture" methods – `setUpBeforeClass()`, `setUp()`, `tearDown()` and `tearDownAfterClass()`. As the `void` return type was not introduced until PHP 7.1, this makes it more difficult to create cross-version compatible tests when using fixtures, due to signature mismatches.
>
> The `Yoast\PHPUnitPolyfills\TestCases\TestCase` overcomes the signature mismatch by having two versions. The correct one will be loaded depending on the PHPUnit version being used.
>
> When using this TestCase, if an individual test, or another TestCase which extends this TestCase, needs to overload any of the "fixture" methods, it should do so by using a snake_case variant of the original fixture method name, i.e. `set_up_before_class()`, `set_up()`, `assert_pre_conditions()`, `assert_post_conditions()`, `tear_down()`, and `tear_down_after_class()`.
>
> The snake_case methods will automatically be called by PHPUnit.
>
> > IMPORTANT: The snake_case methods should not call the PHPUnit parent, i.e. do not use `parent::setUp()` from within an overloaded `set_up()` method. If necessary, DO call `parent::set_up()`.

Reference: https://github.com/Yoast/PHPUnit-Polyfills#testcases

This commit renames all declared fixture methods, and calls to parent versions of those fixture methods, from camelCase to snake_case.

Follow-up to [51559-51567].

Props jrf, hellofromTonya, johnbillion, netweb, dd32, pputzer, SergeyBiryukov.
See #46149.

git-svn-id: https://develop.svn.wordpress.org/trunk@51568 602fd350-edb4-49c9-b593-d223f7449a82
2021-08-07 10:29:41 +00:00

499 lines
10 KiB
PHP

<?php
/**
* @group taxonomy
*/
class Tests_Term_Tax_Query extends WP_UnitTestCase {
protected $q;
public function set_up() {
parent::set_up();
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->assertCount( 1, $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' );
}
}