-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathTitle_GenerationTest.php
More file actions
552 lines (464 loc) · 17.9 KB
/
Title_GenerationTest.php
File metadata and controls
552 lines (464 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
<?php
/**
* Integration tests for the Title_Generation Ability class.
*
* @package WordPress\AI\Tests\Integration\Includes\Abilities
*/
namespace WordPress\AI\Tests\Integration\Includes\Abilities;
use WP_Error;
use WP_UnitTestCase;
use WordPress\AI\Abilities\Title_Generation\Title_Generation;
use WordPress\AI\Abstracts\Abstract_Experiment;
/**
* Test experiment for Title_Generation Ability tests.
*
* @since 0.1.0
*/
class Test_Title_Generation_Experiment extends Abstract_Experiment {
/**
* Loads experiment metadata.
*
* @since 0.1.0
*
* @return array{id: string, label: string, description: string} Experiment metadata.
*/
protected function load_experiment_metadata(): array {
return array(
'id' => 'title-generation',
'label' => 'Title Generation',
'description' => 'Generates title suggestions from content',
);
}
/**
* Initializes the experiment.
*
* @since 0.1.0
*/
public function init(): void {
// No-op for testing.
}
}
/**
* Title_Generation Ability test case.
*
* @since 0.1.0
*/
class Title_GenerationTest extends WP_UnitTestCase {
/**
* Title_Generation ability instance.
*
* @var \WordPress\AI\Abilities\Title_Generation\Title_Generation
*/
private $ability;
/**
* Test experiment instance.
*
* @var \WordPress\AI\Tests\Integration\Includes\Abilities\Test_Title_Generation_Experiment
*/
private $experiment;
/**
* Set up test case.
*
* @since 0.1.0
*/
public function setUp(): void {
parent::setUp();
$this->experiment = new Test_Title_Generation_Experiment();
$this->ability = new Title_Generation(
'ai/title-generation',
array(
'label' => $this->experiment->get_label(),
'description' => $this->experiment->get_description(),
)
);
}
/**
* Tear down test case.
*
* @since 0.1.0
*/
public function tearDown(): void {
wp_set_current_user( 0 );
parent::tearDown();
}
/**
* Test that category() returns the correct category.
*
* @since 0.1.0
*/
public function test_category_returns_correct_category() {
$reflection = new \ReflectionClass( $this->ability );
$method = $reflection->getMethod( 'category' );
$method->setAccessible( true );
$result = $method->invoke( $this->ability );
$this->assertEquals( 'ai-experiments', $result, 'Category should be ai-experiments' );
}
/**
* Test that input_schema() returns the expected schema structure.
*
* @since 0.1.0
*/
public function test_input_schema_returns_expected_structure() {
$reflection = new \ReflectionClass( $this->ability );
$method = $reflection->getMethod( 'input_schema' );
$method->setAccessible( true );
$schema = $method->invoke( $this->ability );
$this->assertIsArray( $schema, 'Input schema should be an array' );
$this->assertEquals( 'object', $schema['type'], 'Schema type should be object' );
$this->assertArrayHasKey( 'properties', $schema, 'Schema should have properties' );
$this->assertArrayHasKey( 'content', $schema['properties'], 'Schema should have content property' );
$this->assertArrayHasKey( 'post_id', $schema['properties'], 'Schema should have post_id property' );
$this->assertArrayHasKey( 'candidates', $schema['properties'], 'Schema should have candidates property' );
// Verify content property.
$this->assertEquals( 'string', $schema['properties']['content']['type'], 'Content should be string type' );
$this->assertEquals( 'sanitize_text_field', $schema['properties']['content']['sanitize_callback'], 'Content should use sanitize_text_field' );
// Verify post_id property.
$this->assertEquals( 'integer', $schema['properties']['post_id']['type'], 'Post ID should be integer type' );
$this->assertEquals( 'absint', $schema['properties']['post_id']['sanitize_callback'], 'Post ID should use absint' );
// Verify candidates property.
$this->assertEquals( 'integer', $schema['properties']['candidates']['type'], 'candidates should be integer type' );
$this->assertEquals( 1, $schema['properties']['candidates']['minimum'], 'candidates minimum should be 1' );
$this->assertEquals( 10, $schema['properties']['candidates']['maximum'], 'candidates maximum should be 10' );
}
/**
* Test that output_schema() returns the expected schema structure.
*
* @since 0.1.0
*/
public function test_output_schema_returns_expected_structure() {
$reflection = new \ReflectionClass( $this->ability );
$method = $reflection->getMethod( 'output_schema' );
$method->setAccessible( true );
$schema = $method->invoke( $this->ability );
$this->assertIsArray( $schema, 'Output schema should be an array' );
$this->assertEquals( 'object', $schema['type'], 'Schema type should be object' );
$this->assertArrayHasKey( 'properties', $schema, 'Schema should have properties' );
$this->assertArrayHasKey( 'titles', $schema['properties'], 'Schema should have titles property' );
$this->assertEquals( 'array', $schema['properties']['titles']['type'], 'Titles should be array type' );
$this->assertArrayHasKey( 'items', $schema['properties']['titles'], 'Titles should have items' );
$this->assertEquals( 'string', $schema['properties']['titles']['items']['type'], 'Title items should be string type' );
}
/**
* Test that get_system_instruction() returns the system instruction.
*
* @since 0.1.0
*/
public function test_get_system_instruction_returns_system_instruction() {
$system_instruction = $this->ability->get_system_instruction();
// System instruction may be empty if file doesn't exist, or contain content if it does.
// We just verify it returns a string.
$this->assertIsString( $system_instruction, 'System instruction should be a string' );
}
/**
* Test that execute_callback() handles content parameter correctly.
*
* @since 0.1.0
*/
public function test_execute_callback_with_content() {
$reflection = new \ReflectionClass( $this->ability );
$method = $reflection->getMethod( 'execute_callback' );
$method->setAccessible( true );
$input = array(
'content' => 'This is some test content.',
'candidates' => 3,
);
try {
$result = $method->invoke( $this->ability, $input );
} catch ( \Throwable $e ) {
$this->markTestSkipped( 'AI client not available in test environment: ' . $e->getMessage() );
return;
}
// Result may be array (success) or WP_Error (if AI client unavailable).
if ( is_wp_error( $result ) ) {
$this->markTestSkipped( 'AI client not available in test environment: ' . $result->get_error_message() );
return;
}
$this->assertIsArray( $result, 'Result should be an array' );
$this->assertArrayHasKey( 'titles', $result, 'Result should have titles key' );
$this->assertIsArray( $result['titles'], 'Titles should be an array' );
$this->assertCount( 3, $result['titles'], 'Should have 3 titles' );
}
/**
* Test that execute_callback() handles post_id parameter correctly.
*
* @since 0.1.0
*/
public function test_execute_callback_with_post_id() {
$reflection = new \ReflectionClass( $this->ability );
$method = $reflection->getMethod( 'execute_callback' );
$method->setAccessible( true );
// Create a test post.
$post_id = $this->factory->post->create(
array(
'post_content' => 'This is post content.',
'post_title' => 'Test Post',
)
);
$input = array(
'post_id' => $post_id,
'candidates' => 2,
);
try {
$result = $method->invoke( $this->ability, $input );
} catch ( \Throwable $e ) {
$this->markTestSkipped( 'AI client not available in test environment: ' . $e->getMessage() );
return;
}
// Result may be array (success) or WP_Error (if AI client unavailable).
if ( is_wp_error( $result ) ) {
$this->markTestSkipped( 'AI client not available in test environment: ' . $result->get_error_message() );
return;
}
$this->assertIsArray( $result, 'Result should be an array' );
$this->assertArrayHasKey( 'titles', $result, 'Result should have titles key' );
$this->assertIsArray( $result['titles'], 'Titles should be an array' );
$this->assertCount( 2, $result['titles'], 'Should have 2 titles' );
}
/**
* Test that execute_callback() returns error when post_id points to non-existent post.
*
* @since 0.1.0
*/
public function test_execute_callback_with_invalid_post_id() {
$reflection = new \ReflectionClass( $this->ability );
$method = $reflection->getMethod( 'execute_callback' );
$method->setAccessible( true );
$input = array(
'post_id' => 99999, // Non-existent post ID.
);
$result = $method->invoke( $this->ability, $input );
$this->assertInstanceOf( WP_Error::class, $result, 'Result should be WP_Error' );
$this->assertEquals( 'post_not_found', $result->get_error_code(), 'Error code should be post_not_found' );
}
/**
* Test that execute_callback() returns error when content is missing.
*
* @since 0.1.0
*/
public function test_execute_callback_without_content() {
$reflection = new \ReflectionClass( $this->ability );
$method = $reflection->getMethod( 'execute_callback' );
$method->setAccessible( true );
$input = array();
$result = $method->invoke( $this->ability, $input );
$this->assertInstanceOf( WP_Error::class, $result, 'Result should be WP_Error' );
$this->assertEquals( 'content_not_provided', $result->get_error_code(), 'Error code should be content_not_provided' );
}
/**
* Test that execute_callback() uses default values.
*
* @since 0.1.0
*/
public function test_execute_callback_uses_defaults() {
$reflection = new \ReflectionClass( $this->ability );
$method = $reflection->getMethod( 'execute_callback' );
$method->setAccessible( true );
$input = array(
'content' => 'Test content',
);
try {
$result = $method->invoke( $this->ability, $input );
} catch ( \Throwable $e ) {
$this->markTestSkipped( 'AI client not available in test environment: ' . $e->getMessage() );
return;
}
// Result may be array (success) or WP_Error (if AI client unavailable).
if ( is_wp_error( $result ) ) {
$this->markTestSkipped( 'AI client not available in test environment: ' . $result->get_error_message() );
return;
}
$this->assertIsArray( $result, 'Result should be an array' );
$this->assertArrayHasKey( 'titles', $result, 'Result should have titles key' );
$this->assertIsArray( $result['titles'], 'Titles should be an array' );
$this->assertCount( 3, $result['titles'], 'Should have 3 titles by default' );
}
/**
* Test that execute_callback() prioritizes post_id over content.
*
* @since 0.1.0
*/
public function test_execute_callback_post_id_overrides_content() {
$reflection = new \ReflectionClass( $this->ability );
$method = $reflection->getMethod( 'execute_callback' );
$method->setAccessible( true );
// Create a test post.
$post_id = $this->factory->post->create(
array(
'post_content' => 'Post content takes priority.',
'post_title' => 'Test Post',
)
);
$input = array(
'content' => 'This content should be ignored.',
'post_id' => $post_id,
);
try {
$result = $method->invoke( $this->ability, $input );
} catch ( \Throwable $e ) {
$this->markTestSkipped( 'AI client not available in test environment: ' . $e->getMessage() );
return;
}
// Result may be array (success) or WP_Error (if AI client unavailable).
if ( is_wp_error( $result ) ) {
$this->markTestSkipped( 'AI client not available in test environment: ' . $result->get_error_message() );
return;
}
$this->assertIsArray( $result, 'Result should be an array' );
$this->assertArrayHasKey( 'titles', $result, 'Result should have titles key' );
$this->assertIsArray( $result['titles'], 'Titles should be an array' );
// The feature's generate_titles uses the post content, verified by titles being generated.
$this->assertNotEmpty( $result['titles'], 'Should generate titles from post content' );
}
/**
* Test that permission_callback() returns true for user with edit_posts capability.
*
* @since 0.1.0
*/
public function test_permission_callback_with_edit_posts_capability() {
$reflection = new \ReflectionClass( $this->ability );
$method = $reflection->getMethod( 'permission_callback' );
$method->setAccessible( true );
// Create a user with edit_posts capability.
$user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
wp_set_current_user( $user_id );
$result = $method->invoke( $this->ability, array() );
$this->assertTrue( $result, 'Permission should be granted for user with edit_posts capability' );
}
/**
* Test that permission_callback() returns error for user without edit_posts capability.
*
* @since 0.1.0
*/
public function test_permission_callback_without_edit_posts_capability() {
$reflection = new \ReflectionClass( $this->ability );
$method = $reflection->getMethod( 'permission_callback' );
$method->setAccessible( true );
// Create a user without edit_posts capability.
$user_id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
wp_set_current_user( $user_id );
$result = $method->invoke( $this->ability, array() );
$this->assertInstanceOf( WP_Error::class, $result, 'Result should be WP_Error' );
$this->assertEquals( 'insufficient_capabilities', $result->get_error_code(), 'Error code should be insufficient_capabilities' );
}
/**
* Test that permission_callback() returns error for logged out user.
*
* @since 0.1.0
*/
public function test_permission_callback_for_logged_out_user() {
$reflection = new \ReflectionClass( $this->ability );
$method = $reflection->getMethod( 'permission_callback' );
$method->setAccessible( true );
// Ensure no user is logged in.
wp_set_current_user( 0 );
$result = $method->invoke( $this->ability, array() );
$this->assertInstanceOf( WP_Error::class, $result, 'Result should be WP_Error' );
$this->assertEquals( 'insufficient_capabilities', $result->get_error_code(), 'Error code should be insufficient_capabilities' );
}
/**
* Test that permission_callback() returns true for user with read_post capability.
*
* @since 0.1.0
*/
public function test_permission_callback_with_post_id_and_read_capability() {
$reflection = new \ReflectionClass( $this->ability );
$method = $reflection->getMethod( 'permission_callback' );
$method->setAccessible( true );
// Create a test post.
$post_id = $this->factory->post->create(
array(
'post_content' => 'Test content',
'post_status' => 'publish',
)
);
// Create a user with read capability.
$user_id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
wp_set_current_user( $user_id );
$result = $method->invoke( $this->ability, array( 'post_id' => $post_id ) );
$this->assertTrue( $result, 'Permission should be granted for user with read_post capability' );
}
/**
* Test that permission_callback() returns error for user without read_post capability.
*
* @since 0.1.0
*/
public function test_permission_callback_with_post_id_without_read_capability() {
$reflection = new \ReflectionClass( $this->ability );
$method = $reflection->getMethod( 'permission_callback' );
$method->setAccessible( true );
// Create a private test post.
$post_id = $this->factory->post->create(
array(
'post_content' => 'Test content',
'post_status' => 'private',
)
);
// Create a user without read capability for this post.
$user_id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
wp_set_current_user( $user_id );
$result = $method->invoke( $this->ability, array( 'post_id' => $post_id ) );
$this->assertInstanceOf( WP_Error::class, $result, 'Result should be WP_Error' );
$this->assertEquals( 'insufficient_capabilities', $result->get_error_code(), 'Error code should be insufficient_capabilities' );
}
/**
* Test that permission_callback() returns error for non-existent post.
*
* @since 0.1.0
*/
public function test_permission_callback_with_nonexistent_post_id() {
$reflection = new \ReflectionClass( $this->ability );
$method = $reflection->getMethod( 'permission_callback' );
$method->setAccessible( true );
$user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
wp_set_current_user( $user_id );
$result = $method->invoke( $this->ability, array( 'post_id' => 99999 ) );
$this->assertInstanceOf( WP_Error::class, $result, 'Result should be WP_Error' );
$this->assertEquals( 'post_not_found', $result->get_error_code(), 'Error code should be post_not_found' );
}
/**
* Test that permission_callback() returns false for post type without show_in_rest.
*
* @since 0.1.0
*/
public function test_permission_callback_with_post_type_without_show_in_rest() {
$reflection = new \ReflectionClass( $this->ability );
$method = $reflection->getMethod( 'permission_callback' );
$method->setAccessible( true );
// Register a custom post type without show_in_rest.
register_post_type(
'test_no_rest',
array(
'public' => true,
'show_in_rest' => false,
)
);
// Create a test post with this post type.
$post_id = $this->factory->post->create(
array(
'post_content' => 'Test content',
'post_type' => 'test_no_rest',
'post_status' => 'publish',
)
);
$user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
wp_set_current_user( $user_id );
$result = $method->invoke( $this->ability, array( 'post_id' => $post_id ) );
$this->assertFalse( $result, 'Permission should be denied for post type without show_in_rest' );
// Clean up.
unregister_post_type( 'test_no_rest' );
}
/**
* Test that meta() returns the expected meta structure.
*
* @since 0.1.0
*/
public function test_meta_returns_expected_structure() {
$reflection = new \ReflectionClass( $this->ability );
$method = $reflection->getMethod( 'meta' );
$method->setAccessible( true );
$meta = $method->invoke( $this->ability );
$this->assertIsArray( $meta, 'Meta should be an array' );
$this->assertArrayHasKey( 'show_in_rest', $meta, 'Meta should have show_in_rest' );
$this->assertTrue( $meta['show_in_rest'], 'show_in_rest should be true' );
}
}