diff --git a/src/wp-admin/includes/taxonomy.php b/src/wp-admin/includes/taxonomy.php index 124c0239a3..52cff7f23c 100644 --- a/src/wp-admin/includes/taxonomy.php +++ b/src/wp-admin/includes/taxonomy.php @@ -11,14 +11,17 @@ // /** - * {@internal Missing Short Description}} + * Check whether a category exists. * * @since 2.0.0 * - * @param int|string $cat_name - * @return int + * @see term_exists() + * + * @param int|string $cat_name Category name. + * @param int $parent Optional. ID of parent term. + * @return mixed */ -function category_exists($cat_name, $parent = 0) { +function category_exists( $cat_name, $parent = null ) { $id = term_exists($cat_name, 'category', $parent); if ( is_array($id) ) $id = $id['term_id']; diff --git a/tests/phpunit/tests/term/categoryExists.php b/tests/phpunit/tests/term/categoryExists.php new file mode 100644 index 0000000000..5f973d4437 --- /dev/null +++ b/tests/phpunit/tests/term/categoryExists.php @@ -0,0 +1,77 @@ +factory->category->create(); + $c2 = $this->factory->category->create( array( + 'name' => 'Foo', + 'parent' => $c1, + ) ); + $c3 = $this->factory->category->create( array( + 'name' => 'Foo', + ) ); + + $found = category_exists( 'Foo', 0 ); + + $this->assertEquals( $found, $c3 ); + } + + /** + * @ticket 30975 + */ + public function test_category_exists_should_select_oldest_matching_category_when_no_parent_is_specified_1() { + // Foo child of c1 is created first. + $c1 = $this->factory->category->create(); + $c2 = $this->factory->category->create( array( + 'name' => 'Foo', + 'parent' => $c1, + ) ); + $c3 = $this->factory->category->create( array( + 'name' => 'Foo', + ) ); + + $found = category_exists( 'Foo' ); + + $this->assertEquals( $found, $c2 ); + } + + /** + * @ticket 30975 + */ + public function test_category_exists_should_select_oldest_matching_category_when_no_parent_is_specified_2() { + // Top-level Foo is created first. + $c1 = $this->factory->category->create(); + $c2 = $this->factory->category->create( array( + 'name' => 'Foo', + ) ); + $c3 = $this->factory->category->create( array( + 'name' => 'Foo', + 'parent' => $c1, + ) ); + + $found = category_exists( 'Foo' ); + + $this->assertEquals( $found, $c2 ); + } + + /** + * @ticket 30975 + */ + public function test_category_exists_should_respect_nonempty_parent() { + $c1 = $this->factory->category->create(); + $c2 = $this->factory->category->create( array( + 'name' => 'Foo', + 'parent' => $c1, + ) ); + $c3 = $this->factory->category->create( array( + 'name' => 'Foo', + ) ); + + $found = category_exists( 'Foo', $c1 ); + + $this->assertEquals( $found, $c2 ); + } +}