Skip to content

Commit 10b069b

Browse files
committed
Tests: Simplify category helper coverage.
Consolidate repeated category and tag cases and replace implementation-derived expectations with independent expected results. Developed in: #15025 Props huzaifaalmesbah, shailu25. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64985 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6fb9459 commit 10b069b

12 files changed

Lines changed: 55 additions & 100 deletions

‎tests/phpunit/tests/category/catIsAncestorOf.php‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ public function test_accepts_category_objects_and_ids( $parent_type, $child_type
164164
*/
165165
public static function data_category_parameter_types() {
166166
return array(
167-
'both category IDs' => array( 'id', 'id' ),
168167
'both WP_Term objects' => array( 'object', 'object' ),
169168
'parent ID and child WP_Term object' => array( 'id', 'object' ),
170169
'parent WP_Term object and child ID' => array( 'object', 'id' ),

‎tests/phpunit/tests/category/getCategory.php‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,6 @@ public static function data_empty_category_values() {
165165
);
166166
}
167167

168-
/**
169-
* Tests that a non-existent category ID returns null.
170-
*
171-
* @ticket 65819
172-
*/
173-
public function test_returns_null_for_non_existent_category() {
174-
$this->assertNull( get_category( 99999999 ) );
175-
}
176-
177168
/**
178169
* Tests that a term ID belonging to another taxonomy returns null.
179170
*

‎tests/phpunit/tests/category/getTag.php‎

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -136,46 +136,6 @@ static function ( $name ) {
136136
$this->assertSame( 'Test Tag (edited)', $tag->name );
137137
}
138138

139-
/**
140-
* Tests that empty tag values return an invalid_term WP_Error.
141-
*
142-
* @ticket 65819
143-
*
144-
* @dataProvider data_empty_tag_values
145-
*
146-
* @param mixed $empty_value Empty tag input.
147-
*/
148-
public function test_returns_wp_error_for_empty_tag( $empty_value ) {
149-
$result = get_tag( $empty_value );
150-
151-
$this->assertWPError( $result );
152-
$this->assertSame( 'invalid_term', $result->get_error_code() );
153-
}
154-
155-
/**
156-
* Data provider.
157-
*
158-
* @return array[]
159-
*/
160-
public static function data_empty_tag_values() {
161-
return array(
162-
'empty string' => array( '' ),
163-
'zero int' => array( 0 ),
164-
'zero string' => array( '0' ),
165-
'null' => array( null ),
166-
'false' => array( false ),
167-
);
168-
}
169-
170-
/**
171-
* Tests that a non-existent tag ID returns null.
172-
*
173-
* @ticket 65819
174-
*/
175-
public function test_returns_null_for_non_existent_tag() {
176-
$this->assertNull( get_tag( 99999999 ) );
177-
}
178-
179139
/**
180140
* Tests that a term ID belonging to another taxonomy returns null.
181141
*

‎tests/phpunit/tests/category/getTags.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function test_returns_post_tag_terms_by_default() {
7979
$tags = get_tags();
8080

8181
$this->assertIsArray( $tags );
82-
$this->assertCount( count( self::$tag_ids ), $tags );
82+
$this->assertSameSets( self::$tag_ids, wp_list_pluck( $tags, 'term_id' ) );
8383

8484
foreach ( $tags as $tag ) {
8585
$this->assertInstanceOf( WP_Term::class, $tag );

‎tests/phpunit/tests/category/getTheCategory.php‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,23 @@ public function test_returns_empty_array_when_no_categories_or_invalid_post() {
120120
public function test_applies_get_the_categories_filter() {
121121
$filter = new MockAction();
122122
add_filter( 'get_the_categories', array( $filter, 'filter' ), 10, 2 );
123+
add_filter(
124+
'get_the_categories',
125+
static function () {
126+
return array();
127+
},
128+
20
129+
);
123130

124131
$categories = get_the_category( self::$post_id );
125132

126133
$this->assertSame( 1, $filter->get_call_count() );
127-
$this->assertSame( array( $categories, self::$post_id ), $filter->get_args()[0] );
134+
$args = $filter->get_args()[0];
135+
$this->assertCount( 2, $args );
136+
$this->assertCount( 1, $args[0] );
137+
$this->assertSame( self::$category->term_id, $args[0][0]->term_id );
138+
$this->assertSame( self::$category->term_id, $args[0][0]->cat_ID );
139+
$this->assertSame( self::$post_id, $args[1] );
140+
$this->assertSame( array(), $categories );
128141
}
129142
}

‎tests/phpunit/tests/category/getTheTags.php‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,22 @@ public function test_returns_false_when_no_tags_or_invalid_post() {
101101
public function test_applies_get_the_tags_filter() {
102102
$filter = new MockAction();
103103
add_filter( 'get_the_tags', array( $filter, 'filter' ) );
104+
add_filter(
105+
'get_the_tags',
106+
static function () {
107+
return array();
108+
},
109+
20
110+
);
104111

105112
$tags = get_the_tags( self::$post->ID );
106113

107114
$this->assertSame( 1, $filter->get_call_count() );
108-
$this->assertSame( array( $tags ), $filter->get_args()[0] );
115+
$args = $filter->get_args()[0];
116+
$this->assertCount( 1, $args );
117+
$this->assertCount( 1, $args[0] );
118+
$this->assertSame( self::$tag->term_id, $args[0][0]->term_id );
119+
$this->assertSame( 'post_tag', $args[0][0]->taxonomy );
120+
$this->assertSame( array(), $tags );
109121
}
110122
}

‎tests/phpunit/tests/category/sanitizeCategory.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public function test_sanitizes_category_array() {
6464

6565
$this->assertIsArray( $result );
6666
$this->assertSame( 15, $result['term_id'] );
67-
$this->assertSame( esc_attr( '<b>Category</b>' ), $result['name'] );
68-
$this->assertSame( esc_html( '<p>Description</p>' ), $result['description'] );
67+
$this->assertSame( '&lt;b&gt;Category&lt;/b&gt;', $result['name'] );
68+
$this->assertSame( '&lt;p&gt;Description&lt;/p&gt;', $result['description'] );
6969
$this->assertSame( 0, $result['count'] );
7070
$this->assertSame( 3, $result['parent'] );
7171
$this->assertSame( 0, $result['term_group'] );

‎tests/phpunit/tests/category/tagDescription.php‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,6 @@ public function test_defaults_to_queried_object_on_tag_archive() {
100100
);
101101
}
102102

103-
/**
104-
* Tests that tag_description() returns an empty string when the tag does not exist.
105-
*
106-
* @ticket 65819
107-
*/
108-
public function test_returns_empty_string_for_non_existent_tag() {
109-
$this->assertSame( '', tag_description( 99999999 ) );
110-
}
111-
112103
/**
113104
* Tests that tag_description() returns an empty string when the tag has no description.
114105
*
@@ -118,17 +109,6 @@ public function test_returns_empty_string_for_tag_without_description() {
118109
$this->assertSame( '', tag_description( self::$empty_tag->term_id ) );
119110
}
120111

121-
/**
122-
* Tests that tag_description() returns an empty string when called without arguments outside a term archive.
123-
*
124-
* @ticket 65819
125-
*/
126-
public function test_returns_empty_string_when_called_without_arguments_outside_archive() {
127-
$this->go_to( home_url() );
128-
$this->assertFalse( is_tag() );
129-
$this->assertSame( '', tag_description() );
130-
}
131-
132112
/**
133113
* Tests that tag_description() applies the 'term_description' filter.
134114
*

‎tests/phpunit/tests/category/theCategory.php‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,14 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
6565
* @ticket 65819
6666
*/
6767
public function test_outputs_default_unordered_list_for_global_post() {
68+
$this->set_permalink_structure( '' );
6869
$GLOBALS['post'] = get_post( self::$post_id );
6970

70-
$this->expectOutputString( get_the_category_list() );
71+
$this->expectOutputString(
72+
'<ul class="post-categories">' .
73+
"\n\t" . '<li><a href="http://' . WP_TESTS_DOMAIN . '/?cat=' . self::$child_id . '" rel="category">Child Category</a></li>' .
74+
'</ul>'
75+
);
7176

7277
the_category();
7378
}
@@ -79,9 +84,12 @@ public function test_outputs_default_unordered_list_for_global_post() {
7984
* @ticket 65819
8085
*/
8186
public function test_outputs_category_list_with_separator_parents_and_post_id() {
82-
$expected = get_the_category_list( ' / ', 'multiple', self::$post_id );
87+
$this->set_permalink_structure( '' );
8388

84-
$this->expectOutputString( $expected );
89+
$this->expectOutputString(
90+
'<a href="http://' . WP_TESTS_DOMAIN . '/?cat=' . self::$parent_id . '">Parent Category</a> / ' .
91+
'<a href="http://' . WP_TESTS_DOMAIN . '/?cat=' . self::$child_id . '" rel="category">Child Category</a>'
92+
);
8593

8694
the_category( ' / ', 'multiple', self::$post_id );
8795
}

‎tests/phpunit/tests/category/walkCategoryDropdownTree.php‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,9 @@ public function test_uses_default_walker_category_dropdown() {
6161
'value_field' => 'term_id',
6262
);
6363

64-
$default_walker = new Walker_CategoryDropdown();
65-
$expected = $default_walker->walk( $elements, 0, $args );
64+
$expected = "\t" . '<option class="level-0" value="' . self::$parent->term_id . '">Parent Category</option>' . "\n" .
65+
"\t" . '<option class="level-1" value="' . self::$child->term_id . '">&nbsp;&nbsp;&nbsp;Child Category</option>' . "\n";
6666

67-
$this->assertStringContainsString( 'value="' . self::$child->term_id . '"', $expected );
6867
$this->assertSame( $expected, walk_category_dropdown_tree( $elements, 0, $args ) );
6968
}
7069

@@ -83,10 +82,9 @@ public function test_falls_back_to_default_walker_when_walker_arg_is_invalid() {
8382
'walker' => new stdClass(),
8483
);
8584

86-
$default_walker = new Walker_CategoryDropdown();
87-
$expected = $default_walker->walk( $elements, 0, $args );
85+
$expected = "\t" . '<option class="level-0" value="' . self::$parent->term_id . '">Parent Category</option>' . "\n" .
86+
"\t" . '<option class="level-1" value="' . self::$child->term_id . '">&nbsp;&nbsp;&nbsp;Child Category</option>' . "\n";
8887

89-
$this->assertStringContainsString( 'value="' . self::$child->term_id . '"', $expected );
9088
$this->assertSame( $expected, walk_category_dropdown_tree( $elements, 0, $args ) );
9189
}
9290

0 commit comments

Comments
 (0)