Skip to content

Commit 6ae5eb0

Browse files
committed
Tests: Cover eleven admin AJAX handlers.
Dispatch registered AJAX actions and verify their responses, permission checks, and resulting database state. Consolidate the earlier handler test proposals and mock dashboard feeds to avoid network requests. Add `WP_Ajax_UnitTestCase::get_ajax_response()` to clear the response buffer before dispatch and fail when a handler does not terminate with `wp_die()`. Developed in: #15381 Props mindctrl, pbearne. See #65252. git-svn-id: https://develop.svn.wordpress.org/trunk@65210 602fd350-edb4-49c9-b593-d223f7449a82
1 parent f684568 commit 6ae5eb0

12 files changed

Lines changed: 1826 additions & 0 deletions

File tree

‎tests/phpunit/includes/testcase-ajax.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,29 @@ protected function _handleAjax( $action ) {
285285
$this->_last_response = $buffer;
286286
}
287287
}
288+
289+
/**
290+
* Dispatches an Ajax action and returns its response.
291+
*
292+
* Fails the test if the handler returns without calling wp_die().
293+
*
294+
* @since 7.2.0
295+
*
296+
* @param string $action The action to handle.
297+
* @return string The output if any was sent, otherwise the wp_die() message.
298+
*/
299+
protected function get_ajax_response( $action ) {
300+
// dieHandler() appends to the previous response, which would leak into this one.
301+
$this->_last_response = '';
302+
303+
try {
304+
$this->_handleAjax( $action );
305+
} catch ( WPAjaxDieStopException $e ) {
306+
return $e->getMessage();
307+
} catch ( WPAjaxDieContinueException $e ) {
308+
return $this->_last_response;
309+
}
310+
311+
$this->fail( "The '{$action}' Ajax handler did not terminate." );
312+
}
288313
}
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
<?php
2+
3+
/**
4+
* Admin Ajax functions to be tested.
5+
*/
6+
require_once ABSPATH . 'wp-admin/includes/ajax-actions.php';
7+
8+
/**
9+
* Testing _wp_ajax_add_hierarchical_term() functionality.
10+
*
11+
* The handler is registered for `wp_ajax_add-{$taxonomy}` by WP_Taxonomy::add_hooks()
12+
* for hierarchical taxonomies that have a UI.
13+
*
14+
* @package WordPress
15+
* @subpackage UnitTests
16+
*
17+
* @group ajax
18+
*
19+
* @covers ::_wp_ajax_add_hierarchical_term
20+
*/
21+
class Tests_Admin_Includes_AjaxActions_addHierarchicalTerm extends WP_Ajax_UnitTestCase {
22+
23+
/**
24+
* Administrator user ID.
25+
*
26+
* @var int
27+
*/
28+
protected static $admin_id;
29+
30+
/**
31+
* Subscriber user ID.
32+
*
33+
* @var int
34+
*/
35+
protected static $subscriber_id;
36+
37+
/**
38+
* Setup test fixtures.
39+
*
40+
* @param WP_UnitTest_Factory $factory
41+
*/
42+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ): void {
43+
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
44+
self::$subscriber_id = $factory->user->create( array( 'role' => 'subscriber' ) );
45+
}
46+
47+
/**
48+
* Populates $_POST for an add request.
49+
*
50+
* @param string $taxonomy Taxonomy name.
51+
* @param string $names Comma-separated term names.
52+
* @param array $extra Optional. Additional request fields. Default empty array.
53+
* @param string|null $nonce Optional. Nonce to send. Default is a valid nonce.
54+
*/
55+
private function set_request( string $taxonomy, string $names, array $extra = array(), ?string $nonce = null ): void {
56+
$_POST = array_merge(
57+
array(
58+
"new{$taxonomy}" => $names,
59+
"_ajax_nonce-add-{$taxonomy}" => $nonce ?? wp_create_nonce( "add-{$taxonomy}" ),
60+
),
61+
$extra
62+
);
63+
}
64+
65+
/**
66+
* Dispatches the request and parses the XML response.
67+
*
68+
* @param string $taxonomy Taxonomy name.
69+
* @return SimpleXMLElement The first response element.
70+
*/
71+
private function get_xml_response( string $taxonomy ): SimpleXMLElement {
72+
$xml = simplexml_load_string( $this->get_ajax_response( "add-{$taxonomy}" ), 'SimpleXMLElement', LIBXML_NOCDATA );
73+
$this->assertInstanceOf( SimpleXMLElement::class, $xml, 'The response should be valid XML.' );
74+
75+
return $xml->response;
76+
}
77+
78+
/**
79+
* @ticket 65252
80+
*/
81+
public function test_add_category_returns_checklist_and_parent_dropdown(): void {
82+
wp_set_current_user( self::$admin_id );
83+
$this->set_request( 'category', 'New Category' );
84+
85+
$response = $this->get_xml_response( 'category' );
86+
87+
$term = get_term_by( 'name', 'New Category', 'category' );
88+
$this->assertInstanceOf( WP_Term::class, $term );
89+
$this->assertSame( 0, $term->parent );
90+
$this->assertSame( (string) $term->term_id, (string) $response->category['id'] );
91+
$this->assertStringContainsString( "value=\"{$term->term_id}\" type=\"checkbox\" name=\"post_category[]\"", (string) $response->category->response_data );
92+
$this->assertStringContainsString( "name='newcategory_parent'", (string) $response->category->supplemental->newcat_parent );
93+
}
94+
95+
/**
96+
* When a parent is given, the response replaces the checklist of the top-level ancestor.
97+
*
98+
* @ticket 65252
99+
*/
100+
public function test_add_child_category_returns_top_level_ancestor_checklist(): void {
101+
wp_set_current_user( self::$admin_id );
102+
103+
$grandparent_id = self::factory()->category->create();
104+
$parent_id = self::factory()->category->create( array( 'parent' => $grandparent_id ) );
105+
$this->set_request( 'category', 'Child Category', array( 'newcategory_parent' => $parent_id ) );
106+
107+
$response = $this->get_xml_response( 'category' );
108+
109+
$term = get_term_by( 'name', 'Child Category', 'category' );
110+
$this->assertInstanceOf( WP_Term::class, $term );
111+
$this->assertSame( $parent_id, $term->parent );
112+
$this->assertSame( (string) $grandparent_id, (string) $response->category['id'] );
113+
114+
$data = (string) $response->category->response_data;
115+
$this->assertStringContainsString( "value=\"{$grandparent_id}\"", $data );
116+
$this->assertStringContainsString( "value=\"{$parent_id}\"", $data );
117+
$this->assertStringContainsString( "value=\"{$term->term_id}\"", $data );
118+
}
119+
120+
/**
121+
* @ticket 65252
122+
*/
123+
public function test_add_category_clamps_negative_parent_to_zero(): void {
124+
wp_set_current_user( self::$admin_id );
125+
$this->set_request( 'category', 'Negative Parent', array( 'newcategory_parent' => -5 ) );
126+
127+
$this->get_xml_response( 'category' );
128+
129+
$term = get_term_by( 'name', 'Negative Parent', 'category' );
130+
$this->assertInstanceOf( WP_Term::class, $term );
131+
$this->assertSame( 0, $term->parent );
132+
}
133+
134+
/**
135+
* @ticket 65252
136+
*/
137+
public function test_add_category_creates_multiple_terms_and_skips_blank_names(): void {
138+
wp_set_current_user( self::$admin_id );
139+
$this->set_request( 'category', 'Cat A, , Cat B' );
140+
141+
$response = $this->get_xml_response( 'category' );
142+
143+
$cat_a = get_term_by( 'name', 'Cat A', 'category' );
144+
$cat_b = get_term_by( 'name', 'Cat B', 'category' );
145+
$this->assertInstanceOf( WP_Term::class, $cat_a );
146+
$this->assertInstanceOf( WP_Term::class, $cat_b );
147+
$this->assertSame( (string) $cat_b->term_id, (string) $response->category['id'], 'Only the last term should be returned.' );
148+
}
149+
150+
/**
151+
* @ticket 65252
152+
*/
153+
public function test_add_category_skips_existing_term(): void {
154+
wp_set_current_user( self::$admin_id );
155+
156+
self::factory()->category->create( array( 'name' => 'Existing' ) );
157+
$this->set_request( 'category', 'Existing' );
158+
159+
$response = $this->get_xml_response( 'category' );
160+
161+
$this->assertCount( 0, $response->category );
162+
$this->assertCount(
163+
1,
164+
get_terms(
165+
array(
166+
'taxonomy' => 'category',
167+
'name' => 'Existing',
168+
'hide_empty' => false,
169+
)
170+
)
171+
);
172+
}
173+
174+
/**
175+
* Taxonomies other than category read the checked terms from tax_input.
176+
*
177+
* @ticket 65252
178+
*/
179+
public function test_add_term_to_custom_taxonomy_uses_tax_input(): void {
180+
register_taxonomy(
181+
'wptests_tax',
182+
'post',
183+
array(
184+
'hierarchical' => true,
185+
'show_ui' => true,
186+
)
187+
);
188+
wp_set_current_user( self::$admin_id );
189+
190+
$parent_id = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
191+
$sibling_id = self::factory()->term->create(
192+
array(
193+
'taxonomy' => 'wptests_tax',
194+
'parent' => $parent_id,
195+
)
196+
);
197+
$this->set_request(
198+
'wptests_tax',
199+
'Custom Child',
200+
array(
201+
'newwptests_tax_parent' => $parent_id,
202+
'tax_input' => array( 'wptests_tax' => array( $sibling_id ) ),
203+
)
204+
);
205+
206+
$response = $this->get_xml_response( 'wptests_tax' );
207+
208+
$term = get_term_by( 'name', 'Custom Child', 'wptests_tax' );
209+
$this->assertInstanceOf( WP_Term::class, $term );
210+
$this->assertSame( (string) $parent_id, (string) $response->wptests_tax['id'] );
211+
$this->assertMatchesRegularExpression(
212+
"/value=\"{$sibling_id}\" type=\"checkbox\" name=\"tax_input\[wptests_tax\]\[\]\" id=\"[^\"]+\" checked='checked'/",
213+
(string) $response->wptests_tax->response_data
214+
);
215+
}
216+
217+
/**
218+
* @ticket 65252
219+
*/
220+
public function test_add_category_rejects_invalid_nonce(): void {
221+
wp_set_current_user( self::$admin_id );
222+
$this->set_request( 'category', 'Invalid Nonce Category', array(), 'invalid-nonce' );
223+
224+
$this->assertSame( '-1', $this->get_ajax_response( 'add-category' ) );
225+
$this->assertFalse( get_term_by( 'name', 'Invalid Nonce Category', 'category' ) );
226+
}
227+
228+
/**
229+
* @ticket 65252
230+
*/
231+
public function test_add_category_rejects_user_without_capability(): void {
232+
wp_set_current_user( self::$subscriber_id );
233+
$this->set_request( 'category', 'No Permission Category' );
234+
235+
$this->assertSame( '-1', $this->get_ajax_response( 'add-category' ) );
236+
$this->assertFalse( get_term_by( 'name', 'No Permission Category', 'category' ) );
237+
}
238+
}

0 commit comments

Comments
 (0)