|
| 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