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
This commit is contained in:
Boone Gorges 2015-01-11 01:25:19 +00:00
parent f323dc9b48
commit 77128eb048
2 changed files with 84 additions and 4 deletions

View File

@ -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'];

View File

@ -0,0 +1,77 @@
<?php
class Tests_Term_CategoryExists extends WP_UnitTestCase {
/**
* @ticket 30975
*/
public function test_category_exists_should_return_only_top_level_categories_when_parent_is_0() {
$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', 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 );
}
}