mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
> 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
187 lines
5.8 KiB
PHP
187 lines
5.8 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @group taxonomy
|
|
*/
|
|
class Tests_Term_getTermField extends WP_UnitTestCase {
|
|
|
|
public $taxonomy = 'wptests_tax';
|
|
|
|
function set_up() {
|
|
parent::set_up();
|
|
|
|
register_taxonomy( $this->taxonomy, 'post' );
|
|
}
|
|
|
|
/**
|
|
* @ticket 34245
|
|
*/
|
|
public function test_get_term_field_should_not_return_error_for_empty_taxonomy() {
|
|
$term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
|
|
|
|
$found = get_term_field( 'taxonomy', $term->term_id, '' );
|
|
$this->assertNotWPError( $found );
|
|
$this->assertSame( $this->taxonomy, $found );
|
|
}
|
|
|
|
/**
|
|
* @ticket 34245
|
|
*/
|
|
public function test_get_term_field_supplying_a_taxonomy() {
|
|
$term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
|
|
|
|
$found = get_term_field( 'taxonomy', $term->term_id, $term->taxonomy );
|
|
$this->assertSame( $this->taxonomy, $found );
|
|
}
|
|
|
|
/**
|
|
* @ticket 34245
|
|
*/
|
|
public function test_get_term_field_supplying_no_taxonomy() {
|
|
$term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
|
|
|
|
$found = get_term_field( 'taxonomy', $term->term_id );
|
|
$this->assertSame( $this->taxonomy, $found );
|
|
}
|
|
|
|
/**
|
|
* @ticket 34245
|
|
*/
|
|
public function test_get_term_field_should_accept_a_WP_Term_id_or_object() {
|
|
$term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
|
|
|
|
$this->assertInstanceOf( 'WP_Term', $term );
|
|
$this->assertSame( $term->term_id, get_term_field( 'term_id', $term ) );
|
|
$this->assertSame( $term->term_id, get_term_field( 'term_id', $term->data ) );
|
|
$this->assertSame( $term->term_id, get_term_field( 'term_id', $term->term_id ) );
|
|
}
|
|
|
|
/**
|
|
* @ticket 34245
|
|
*/
|
|
public function test_get_term_field_invalid_taxonomy_should_return_WP_Error() {
|
|
$term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
|
|
|
|
$found = get_term_field( 'taxonomy', $term, 'foo-taxonomy' );
|
|
$this->assertWPError( $found );
|
|
$this->assertSame( 'invalid_taxonomy', $found->get_error_code() );
|
|
}
|
|
|
|
/**
|
|
* @ticket 34245
|
|
*/
|
|
public function test_get_term_field_invalid_term_should_return_WP_Error() {
|
|
$found = get_term_field( 'taxonomy', 0, $this->taxonomy );
|
|
|
|
$this->assertWPError( $found );
|
|
$this->assertSame( 'invalid_term', $found->get_error_code() );
|
|
|
|
$_found = get_term_field( 'taxonomy', 0 );
|
|
|
|
$this->assertWPError( $_found );
|
|
$this->assertSame( 'invalid_term', $_found->get_error_code() );
|
|
}
|
|
|
|
public function test_get_term_field_term_id() {
|
|
$term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
|
|
|
|
$this->assertSame( $term->term_id, get_term_field( 'term_id', $term ) );
|
|
$this->assertSame( $term->term_id, get_term_field( 'term_id', $term->data ) );
|
|
$this->assertSame( $term->term_id, get_term_field( 'term_id', $term->term_id ) );
|
|
}
|
|
|
|
public function test_get_term_field_name() {
|
|
$name = rand_str( 15 );
|
|
|
|
$term = self::factory()->term->create_and_get(
|
|
array(
|
|
'name' => $name,
|
|
'taxonomy' => $this->taxonomy,
|
|
)
|
|
);
|
|
|
|
$this->assertSame( $name, get_term_field( 'name', $term ) );
|
|
$this->assertSame( $name, get_term_field( 'name', $term->data ) );
|
|
$this->assertSame( $name, get_term_field( 'name', $term->term_id ) );
|
|
}
|
|
|
|
public function test_get_term_field_slug_when_slug_is_set() {
|
|
$slug = rand_str( 15 );
|
|
|
|
$term = self::factory()->term->create_and_get(
|
|
array(
|
|
'taxonomy' => $this->taxonomy,
|
|
'slug' => $slug,
|
|
)
|
|
);
|
|
|
|
$this->assertSame( $slug, get_term_field( 'slug', $term ) );
|
|
$this->assertSame( $slug, get_term_field( 'slug', $term->data ) );
|
|
$this->assertSame( $slug, get_term_field( 'slug', $term->term_id ) );
|
|
}
|
|
|
|
public function test_get_term_field_slug_when_slug_falls_back_from_name() {
|
|
$name = rand_str( 15 );
|
|
|
|
$term = self::factory()->term->create_and_get(
|
|
array(
|
|
'taxonomy' => $this->taxonomy,
|
|
'name' => $name,
|
|
)
|
|
);
|
|
|
|
$this->assertSame( $name, get_term_field( 'slug', $term ) );
|
|
$this->assertSame( $name, get_term_field( 'slug', $term->data ) );
|
|
$this->assertSame( $name, get_term_field( 'slug', $term->term_id ) );
|
|
}
|
|
|
|
public function test_get_term_field_slug_when_slug_and_name_are_not_set() {
|
|
$term = self::factory()->term->create_and_get(
|
|
array(
|
|
'taxonomy' => $this->taxonomy,
|
|
)
|
|
);
|
|
|
|
$this->assertSame( $term->slug, get_term_field( 'slug', $term ) );
|
|
$this->assertSame( $term->slug, get_term_field( 'slug', $term->data ) );
|
|
$this->assertSame( $term->slug, get_term_field( 'slug', $term->term_id ) );
|
|
}
|
|
|
|
public function test_get_term_field_taxonomy() {
|
|
$term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
|
|
|
|
$this->assertSame( $this->taxonomy, get_term_field( 'taxonomy', $term ) );
|
|
$this->assertSame( $this->taxonomy, get_term_field( 'taxonomy', $term->data ) );
|
|
$this->assertSame( $this->taxonomy, get_term_field( 'taxonomy', $term->term_id ) );
|
|
}
|
|
|
|
public function test_get_term_field_description() {
|
|
$desc = wpautop( rand_str() );
|
|
|
|
$term = self::factory()->term->create_and_get(
|
|
array(
|
|
'taxonomy' => $this->taxonomy,
|
|
'description' => $desc,
|
|
)
|
|
);
|
|
|
|
$this->assertSame( $desc, get_term_field( 'description', $term ) );
|
|
$this->assertSame( $desc, get_term_field( 'description', $term->data ) );
|
|
$this->assertSame( $desc, get_term_field( 'description', $term->term_id ) );
|
|
}
|
|
|
|
public function test_get_term_field_parent() {
|
|
$parent = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
|
|
$term = self::factory()->term->create_and_get(
|
|
array(
|
|
'taxonomy' => $this->taxonomy,
|
|
'parent' => $parent->term_id,
|
|
)
|
|
);
|
|
|
|
$this->assertSame( $parent->term_id, get_term_field( 'parent', $term ) );
|
|
$this->assertSame( $parent->term_id, get_term_field( 'parent', $term->data ) );
|
|
$this->assertSame( $parent->term_id, get_term_field( 'parent', $term->term_id ) );
|
|
}
|
|
}
|