Skip to content

Commit b3748e6

Browse files
core/content: align the content tests with the instance classes and core
Drive the Content and Show_In_Abilities tests through instances (held on the test case so the same instance detaches its hooks on tear down) instead of the old static calls. Bring ContentTest in line with core's wpRegisterCoreContentAbility: assert show_in_rest + destructive/idempotent annotations on registration, assert the input schema type and additionalProperties, add output-schema coverage, and add a test that a post type registered by another active plugin (flagged show_in_abilities) is exposed in the input enum and in query results.
1 parent 4372137 commit b3748e6

2 files changed

Lines changed: 123 additions & 11 deletions

File tree

tests/Integration/Includes/Abilities/Content/ContentTest.php

Lines changed: 106 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818
*/
1919
class ContentTest extends WP_UnitTestCase {
2020

21+
/**
22+
* The exposure component. Held so the same instance can detach its filters on tear down.
23+
*
24+
* @since x.x.x
25+
*
26+
* @var \WordPress\AI\Abilities\Show_In_Abilities
27+
*/
28+
private $show_in_abilities;
29+
2130
/**
2231
* Set up test case.
2332
*
@@ -27,9 +36,15 @@ public function setUp(): void {
2736
parent::setUp();
2837

2938
// Mark the curated core post types (post, page) as exposed to abilities.
30-
Show_In_Abilities::register();
39+
$this->show_in_abilities = new Show_In_Abilities();
40+
$this->show_in_abilities->register();
3141

3242
$this->ensure_content_category();
43+
44+
// The plugin also registers the `core/settings` ability (into the `site` category)
45+
// on the same abilities-init hook, so make sure that category exists too; otherwise
46+
// its registration emits an "incorrect usage" notice that fails this test.
47+
$this->ensure_site_category();
3348
}
3449

3550
/**
@@ -42,8 +57,8 @@ public function tearDown(): void {
4257
wp_unregister_ability( 'core/content' );
4358
}
4459

45-
remove_filter( 'register_setting_args', array( Show_In_Abilities::class, 'mark_setting' ), 10 );
46-
remove_filter( 'register_post_type_args', array( Show_In_Abilities::class, 'mark_post_type' ), 10 );
60+
remove_filter( 'register_setting_args', array( $this->show_in_abilities, 'mark_setting' ), 10 );
61+
remove_filter( 'register_post_type_args', array( $this->show_in_abilities, 'mark_post_type' ), 10 );
4762

4863
// Restore the curated post types to their unmarked state to avoid leaking into other tests.
4964
foreach ( array( 'post', 'page' ) as $post_type ) {
@@ -85,6 +100,32 @@ private function ensure_content_category(): void {
85100
}
86101
}
87102

103+
/**
104+
* Ensures the `site` ability category exists, used by the plugin's `core/settings`
105+
* ability which registers on the same hook as `core/content`.
106+
*
107+
* @since x.x.x
108+
*/
109+
private function ensure_site_category(): void {
110+
if ( wp_has_ability_category( 'site' ) ) {
111+
return;
112+
}
113+
114+
global $wp_current_filter;
115+
$wp_current_filter[] = 'wp_abilities_api_categories_init'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Faking the action context to register within it.
116+
try {
117+
wp_register_ability_category(
118+
'site',
119+
array(
120+
'label' => 'Site',
121+
'description' => 'Site.',
122+
)
123+
);
124+
} finally {
125+
array_pop( $wp_current_filter );
126+
}
127+
}
128+
88129
/**
89130
* Registers the plugin's core/content ability inside a faked init action.
90131
*
@@ -94,7 +135,7 @@ private function register_ability(): void {
94135
global $wp_current_filter;
95136
$wp_current_filter[] = 'wp_abilities_api_init'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Faking the action context to register within it.
96137
try {
97-
Content::register();
138+
( new Content() )->register();
98139
} finally {
99140
array_pop( $wp_current_filter );
100141
}
@@ -125,9 +166,12 @@ public function test_registers_core_content_ability(): void {
125166
$this->assertNotNull( $ability );
126167
$this->assertSame( 'core/content', $ability->get_name() );
127168
$this->assertSame( 'content', $ability->get_category() );
169+
$this->assertTrue( $ability->get_meta_item( 'show_in_rest', false ) );
128170

129171
$annotations = $ability->get_meta_item( 'annotations', array() );
130172
$this->assertTrue( $annotations['readonly'] );
173+
$this->assertFalse( $annotations['destructive'] );
174+
$this->assertTrue( $annotations['idempotent'] );
131175
}
132176

133177
/**
@@ -172,6 +216,8 @@ public function test_input_schema_requires_id_or_post_type(): void {
172216

173217
$schema = wp_get_ability( 'core/content' )->get_input_schema();
174218

219+
$this->assertSame( 'object', $schema['type'] );
220+
$this->assertFalse( $schema['additionalProperties'] );
175221
$this->assertSame(
176222
array(
177223
array( 'required' => array( 'id' ) ),
@@ -185,6 +231,62 @@ public function test_input_schema_requires_id_or_post_type(): void {
185231
$this->assertContains( 'page', $enum );
186232
}
187233

234+
/**
235+
* The output schema describes each post as an object with no required fields.
236+
*
237+
* @since x.x.x
238+
*/
239+
public function test_output_schema_has_no_required_post_fields(): void {
240+
$this->register_ability();
241+
242+
$schema = wp_get_ability( 'core/content' )->get_output_schema();
243+
$post_item = $schema['properties']['posts']['items'];
244+
245+
$this->assertSame( 'object', $post_item['type'] );
246+
$this->assertArrayNotHasKey( 'required', $post_item );
247+
$this->assertFalse( $post_item['additionalProperties'] );
248+
$this->assertArrayHasKey( 'raw_content', $post_item['properties'] );
249+
$this->assertArrayHasKey( 'total', $schema['properties'] );
250+
$this->assertArrayHasKey( 'total_pages', $schema['properties'] );
251+
}
252+
253+
/**
254+
* A post type registered by another active plugin and flagged `show_in_abilities`
255+
* is exposed by the ability, both in the input enum and in query results.
256+
*
257+
* @since x.x.x
258+
*/
259+
public function test_exposes_a_post_type_registered_by_another_plugin(): void {
260+
register_post_type(
261+
'wpai_content_cpt',
262+
array(
263+
'public' => true,
264+
'show_in_abilities' => true,
265+
'supports' => array( 'title', 'editor' ),
266+
)
267+
);
268+
269+
$this->login_as( 'administrator' );
270+
$this->register_ability();
271+
272+
$enum = wp_get_ability( 'core/content' )->get_input_schema()['properties']['post_type']['enum'];
273+
$this->assertContains( 'wpai_content_cpt', $enum );
274+
275+
$post_id = self::factory()->post->create(
276+
array(
277+
'post_type' => 'wpai_content_cpt',
278+
'post_status' => 'publish',
279+
)
280+
);
281+
282+
$result = wp_get_ability( 'core/content' )->execute( array( 'post_type' => 'wpai_content_cpt' ) );
283+
$ids = wp_list_pluck( $result['posts'], 'id' );
284+
285+
$this->assertContains( $post_id, $ids );
286+
287+
unregister_post_type( 'wpai_content_cpt' );
288+
}
289+
188290
/**
189291
* A published post can be fetched by ID.
190292
*

tests/Integration/Includes/Abilities/Show_In_AbilitiesTest.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ class Show_In_AbilitiesTest extends WP_UnitTestCase {
2626
*/
2727
private $registered_options = array();
2828

29+
/**
30+
* The component under test. Held so the same instance can detach its filters on tear down.
31+
*
32+
* @since x.x.x
33+
*
34+
* @var \WordPress\AI\Abilities\Show_In_Abilities
35+
*/
36+
private $show_in_abilities;
37+
2938
/**
3039
* Set up test case.
3140
*
@@ -34,7 +43,8 @@ class Show_In_AbilitiesTest extends WP_UnitTestCase {
3443
public function setUp(): void {
3544
parent::setUp();
3645

37-
Show_In_Abilities::register();
46+
$this->show_in_abilities = new Show_In_Abilities();
47+
$this->show_in_abilities->register();
3848
}
3949

4050
/**
@@ -43,8 +53,8 @@ public function setUp(): void {
4353
* @since x.x.x
4454
*/
4555
public function tearDown(): void {
46-
remove_filter( 'register_setting_args', array( Show_In_Abilities::class, 'mark_setting' ), 10 );
47-
remove_filter( 'register_post_type_args', array( Show_In_Abilities::class, 'mark_post_type' ), 10 );
56+
remove_filter( 'register_setting_args', array( $this->show_in_abilities, 'mark_setting' ), 10 );
57+
remove_filter( 'register_post_type_args', array( $this->show_in_abilities, 'mark_post_type' ), 10 );
4858

4959
foreach ( $this->registered_options as $option ) {
5060
unregister_setting( 'group', $option );
@@ -146,7 +156,7 @@ public function test_respects_existing_value(): void {
146156
* @since x.x.x
147157
*/
148158
public function test_marks_curated_registered_post_types(): void {
149-
// Show_In_Abilities::register() ran in setUp and patches existing post types.
159+
// $this->show_in_abilities->register() ran in setUp and patches existing post types.
150160
$this->assertNotEmpty( get_post_type_object( 'post' )->show_in_abilities );
151161
$this->assertNotEmpty( get_post_type_object( 'page' )->show_in_abilities );
152162
}
@@ -157,7 +167,7 @@ public function test_marks_curated_registered_post_types(): void {
157167
* @since x.x.x
158168
*/
159169
public function test_filter_marks_curated_post_type(): void {
160-
$args = Show_In_Abilities::mark_post_type( array(), 'page' );
170+
$args = $this->show_in_abilities->mark_post_type( array(), 'page' );
161171

162172
$this->assertTrue( $args['show_in_abilities'] );
163173
}
@@ -168,7 +178,7 @@ public function test_filter_marks_curated_post_type(): void {
168178
* @since x.x.x
169179
*/
170180
public function test_filter_skips_uncurated_post_type(): void {
171-
$args = Show_In_Abilities::mark_post_type( array(), 'wpai_not_curated_cpt' );
181+
$args = $this->show_in_abilities->mark_post_type( array(), 'wpai_not_curated_cpt' );
172182

173183
$this->assertTrue( empty( $args['show_in_abilities'] ) );
174184
}
@@ -179,7 +189,7 @@ public function test_filter_skips_uncurated_post_type(): void {
179189
* @since x.x.x
180190
*/
181191
public function test_filter_respects_existing_post_type_value(): void {
182-
$args = Show_In_Abilities::mark_post_type(
192+
$args = $this->show_in_abilities->mark_post_type(
183193
array( 'show_in_abilities' => array( 'custom' => true ) ),
184194
'post'
185195
);

0 commit comments

Comments
 (0)