From 77128eb048fd63f4527e68a761c00f790b984799 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sun, 11 Jan 2015 01:25:19 +0000 Subject: [PATCH] Default `$parent` in `category_exists()` should default to null rather than 0. [29863] made the corresponding change in `term_exists()`. Failure to change the default value in `category_exists()` meant that an unspecified value for `$parent` would limit results to top-level categories. Includes unit tests and corrected function documentation. Props hissy. Fixes #30975 for trunk. git-svn-id: https://develop.svn.wordpress.org/trunk@31140 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/taxonomy.php | 11 +-- tests/phpunit/tests/term/categoryExists.php | 77 +++++++++++++++++++++ 2 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 tests/phpunit/tests/term/categoryExists.php 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 ); + } +}