Skip to content

Commit 2cf0f3d

Browse files
committed
Docs: Update typing for wp_create_category().
* Ensure that `wp_create_category()` returns `int` as opposed to `numeric-string`. * Rename `$cat_name` to `$category_name` to avoid abbreviating variables. * Update docblock to remove erroneous `int` for `$category_name` param when only `string` is intended. * Add missing tests for `wp_create_category()`. Developed in #8861 Props justlevine, westonruter. See #64238, #64226. git-svn-id: https://develop.svn.wordpress.org/trunk@61298 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 693cd3c commit 2cf0f3d

File tree

2 files changed

+88
-7
lines changed

2 files changed

+88
-7
lines changed

src/wp-admin/includes/taxonomy.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,19 @@ function get_category_to_edit( $id ) {
4848
*
4949
* @since 2.0.0
5050
*
51-
* @param int|string $cat_name Category name.
52-
* @param int $category_parent Optional. ID of parent category.
53-
* @return int|WP_Error
51+
* @param string $category_name Category name.
52+
* @param int $category_parent Optional. ID of parent category.
53+
* @return int The ID of category term on success, or zero on failure.
5454
*/
55-
function wp_create_category( $cat_name, $category_parent = 0 ) {
56-
$id = category_exists( $cat_name, $category_parent );
55+
function wp_create_category( $category_name, $category_parent = 0 ) {
56+
$id = category_exists( $category_name, $category_parent );
5757
if ( $id ) {
58-
return $id;
58+
return (int) $id;
5959
}
6060

6161
return wp_insert_category(
6262
array(
63-
'cat_name' => $cat_name,
63+
'cat_name' => $category_name,
6464
'category_parent' => $category_parent,
6565
)
6666
);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/**
4+
* @group taxonomy
5+
* @covers ::wp_create_category
6+
*/
7+
class Tests_Term_WpCreateCategory extends WP_UnitTestCase {
8+
9+
/**
10+
* Tests that a new category is created.
11+
*/
12+
public function test_create_category_when_not_existing() {
13+
$category_name = 'Foo';
14+
$category_id = wp_create_category( $category_name );
15+
$this->assertGreaterThan( 0, $category_id, 'Expected category to be created.' );
16+
$term = get_term( $category_id );
17+
$this->assertSame( 'Foo', $term->name, 'Expected category name to match.' );
18+
}
19+
20+
/**
21+
* Tests error case.
22+
*/
23+
public function test_create_with_error() {
24+
$this->assertSame( 0, wp_create_category( '' ), 'Expected error.' );
25+
}
26+
27+
/**
28+
* Tests that an existing category is identified.
29+
*/
30+
public function test_create_category_when_already_exists() {
31+
$category_name = 'Foo';
32+
$category_id = self::factory()->category->create( array( 'name' => $category_name ) );
33+
$this->assertSame( $category_id, wp_create_category( $category_name ), 'Expected existing category to be identified.' );
34+
}
35+
36+
/**
37+
* Tests that the existing category is identified when a child of a parent.
38+
*/
39+
public function test_create_category_with_parent_when_already_exists() {
40+
$category_name = 'Foo';
41+
$parent_category_id = self::factory()->category->create( array( 'name' => 'Parent' ) );
42+
$category_id = self::factory()->category->create(
43+
array(
44+
'name' => $category_name,
45+
'parent' => $parent_category_id,
46+
)
47+
);
48+
$this->assertSame( $category_id, wp_create_category( $category_name, $parent_category_id ), 'Expected existing category to be identified.' );
49+
}
50+
51+
/**
52+
* Tests that new root category is created when another of the same name exists as a child.
53+
*/
54+
public function test_create_root_category_when_exists_as_child_of_parent() {
55+
$category_name = 'Foo';
56+
$parent_category_id = self::factory()->category->create( array( 'name' => 'Parent' ) );
57+
$category_id = self::factory()->category->create(
58+
array(
59+
'name' => $category_name,
60+
'parent' => $parent_category_id,
61+
)
62+
);
63+
64+
$new_category_id = wp_create_category( $category_name );
65+
$this->assertNotSame( $category_id, $new_category_id, 'Expected a new category to have been created.' );
66+
$this->assertGreaterThan( 0, $new_category_id, 'Expected category to have been created.' );
67+
}
68+
69+
/**
70+
* Tests that new child category is created when another of the same name exists at the root.
71+
*/
72+
public function test_create_child_category_when_exists_as_root() {
73+
$category_name = 'Foo';
74+
$parent_category_id = self::factory()->category->create( array( 'name' => 'Parent' ) );
75+
$category_id = self::factory()->category->create( array( 'name' => $category_name ) );
76+
77+
$new_category_id = wp_create_category( $category_name, $parent_category_id );
78+
$this->assertNotSame( $category_id, $new_category_id, 'Expected a new category to have been created.' );
79+
$this->assertGreaterThan( 0, $new_category_id, 'Expected category to have been created.' );
80+
}
81+
}

0 commit comments

Comments
 (0)