| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * @group taxonomy |
| | 5 | */ |
| | 6 | class Tests_Term_getTermField extends WP_UnitTestCase { |
| | 7 | |
| | 8 | public $taxonomy = 'wptests_tax'; |
| | 9 | |
| | 10 | function setUp() { |
| | 11 | parent::setUp(); |
| | 12 | |
| | 13 | register_taxonomy( $this->taxonomy, 'post' ); |
| | 14 | } |
| | 15 | |
| | 16 | /** |
| | 17 | * @ticket 34245 |
| | 18 | */ |
| | 19 | public function test_get_term_field_should_not_return_error_for_empty_taxonomy() { |
| | 20 | $term = $this->factory->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) ); |
| | 21 | |
| | 22 | $found = get_term_field( 'taxonomy', $term->term_id, '' ); |
| | 23 | $this->assertNotWPError( $found ); |
| | 24 | $this->assertSame( $this->taxonomy, $found ); |
| | 25 | } |
| | 26 | |
| | 27 | /** |
| | 28 | * @ticket 34245 |
| | 29 | */ |
| | 30 | public function test_get_term_field_supplying_a_taxonomy() { |
| | 31 | $term = $this->factory->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) ); |
| | 32 | |
| | 33 | $found = get_term_field( 'taxonomy', $term->term_id, $term->taxonomy ); |
| | 34 | $this->assertSame( $this->taxonomy, $found ); |
| | 35 | } |
| | 36 | |
| | 37 | /** |
| | 38 | * @ticket 34245 |
| | 39 | */ |
| | 40 | public function test_get_term_field_supplying_no_taxonomy() { |
| | 41 | $term = $this->factory->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) ); |
| | 42 | |
| | 43 | $found = get_term_field( 'taxonomy', $term->term_id ); |
| | 44 | $this->assertSame( $this->taxonomy, $found ); |
| | 45 | } |
| | 46 | |
| | 47 | /** |
| | 48 | * @ticket 34245 |
| | 49 | */ |
| | 50 | public function test_get_term_field_should_accept_a_WP_Term_object_or_a_term_id() { |
| | 51 | $term = $this->factory->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) ); |
| | 52 | |
| | 53 | $this->assertInstanceOf( 'WP_Term', $term ); |
| | 54 | $this->assertSame( $term->term_id, get_term_field( 'term_id', $term ) ); |
| | 55 | $this->assertSame( $term->term_id, get_term_field( 'term_id', $term->term_id ) ); |
| | 56 | } |
| | 57 | |
| | 58 | /** |
| | 59 | * @ticket 34245 |
| | 60 | */ |
| | 61 | public function test_get_term_field_invalid_taxonomy_should_return_WP_Error() { |
| | 62 | $term = $this->factory->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) ); |
| | 63 | |
| | 64 | $found = get_term_field( 'taxonomy', $term, 'foo-taxonomy' ); |
| | 65 | $this->assertWPError( $found ); |
| | 66 | $this->assertSame( 'invalid_taxonomy', $found->get_error_code() ); |
| | 67 | } |
| | 68 | |
| | 69 | /** |
| | 70 | * @ticket 34245 |
| | 71 | */ |
| | 72 | public function test_get_term_field_invalid_term_should_return_WP_Error() { |
| | 73 | $found = get_term_field( 'taxonomy', 0, $this->taxonomy ); |
| | 74 | |
| | 75 | $this->assertWPError( $found ); |
| | 76 | $this->assertSame( 'invalid_term', $found->get_error_code() ); |
| | 77 | |
| | 78 | $_found = get_term_field( 'taxonomy', 0 ); |
| | 79 | |
| | 80 | $this->assertWPError( $_found ); |
| | 81 | $this->assertSame( 'invalid_term', $_found->get_error_code() ); |
| | 82 | } |
| | 83 | } |