From f03562b3640bcc3c3d3d17f00ae14af4431451a6 Mon Sep 17 00:00:00 2001 From: Drew Jaynes Date: Mon, 19 Oct 2015 03:28:49 +0000 Subject: [PATCH] Tests: Add some more test coverage for `get_term_field()`. See #33968. git-svn-id: https://develop.svn.wordpress.org/trunk@35270 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/term/getTermField.php | 93 ++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/term/getTermField.php b/tests/phpunit/tests/term/getTermField.php index a225acc10d..c6250e0a14 100644 --- a/tests/phpunit/tests/term/getTermField.php +++ b/tests/phpunit/tests/term/getTermField.php @@ -47,11 +47,12 @@ class Tests_Term_getTermField extends WP_UnitTestCase { /** * @ticket 34245 */ - public function test_get_term_field_should_accept_a_WP_Term_object_or_a_term_id() { + public function test_get_term_field_should_accept_a_WP_Term_object_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 ) ); } @@ -80,4 +81,94 @@ class Tests_Term_getTermField extends WP_UnitTestCase { $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 ) ); + } }