-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathtest-fieldmanager-datasource-post.php
More file actions
587 lines (526 loc) · 18.2 KB
/
test-fieldmanager-datasource-post.php
File metadata and controls
587 lines (526 loc) · 18.2 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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
<?php
/**
* Tests the Fieldmanager Datasource Post
*
* @group datasource
* @group post
*/
class Test_Fieldmanager_Datasource_Post extends WP_UnitTestCase {
public function setUp() {
parent::setUp();
Fieldmanager_Field::$debug = true;
$this->author = $this->factory->user->create(
array(
'role' => 'author',
'user_login' => 'author',
)
);
$this->editor = $this->factory->user->create(
array(
'role' => 'editor',
'user_login' => 'editor',
)
);
wp_set_current_user( $this->editor );
$this->parent_post = $this->factory->post->create_and_get(
array(
'post_status' => 'draft',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_author' => $this->author,
)
);
$this->child_post_a = $this->factory->post->create_and_get(
array(
'post_status' => 'draft',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_author' => $this->author,
)
);
$this->child_post_b = $this->factory->post->create_and_get(
array(
'post_status' => 'draft',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_author' => $this->editor,
)
);
}
/**
* Helper which returns the post meta box HTML for a given field;
*
* @param object $field Some Fieldmanager_Field object.
* @param object $post A WP_Post object.
* @param array $test_data Data to save (and use when rendering)
* @return string Rendered HTML.
*/
private function _get_html_for( $field, $post, $test_data = null ) {
ob_start();
$context = $field->add_meta_box( 'test meta box', $post );
if ( $test_data ) {
$context->save_to_post_meta( $post->ID, $test_data );
}
$context->render_meta_box( $post );
return ob_get_clean();
}
/**
* Set up the request environment values and save the data.
*
* @param Fieldmanager_Field $field
* @param WP_Post $post
* @param mixed $values
*/
public function save_values( $field, $post, $values ) {
$_POST = array(
'post_ID' => $post->ID,
'action' => 'editpost',
'post_type' => $post->post_type,
"fieldmanager-{$field->name}-nonce" => wp_create_nonce( "fieldmanager-save-{$field->name}" ),
$field->name => $values,
);
$field->add_meta_box( $field->name, $post->post_type )->save_to_post_meta( $post->ID, $values );
}
/**
* Test behavior when using the post datasource.
*/
public function test_save_child_posts() {
// Author is permitted to use child_post_b because this configuration makes no changes to children.
wp_set_current_user( $this->author );
$children = new Fieldmanager_Autocomplete(
array(
'name' => 'test_children',
'limit' => 0,
'datasource' => new Fieldmanager_Datasource_Post(
array(
'query_args' => array(
'post_type' => 'post',
),
)
),
)
);
$this->save_values( $children, $this->parent_post, array( $this->child_post_a->ID, $this->child_post_b->ID ) );
$saved_value = get_post_meta( $this->parent_post->ID, 'test_children', true );
$this->assertEquals( 2, count( $saved_value ) );
$this->assertEquals( $this->child_post_a->ID, $saved_value[0] );
$this->assertEquals( $this->child_post_b->ID, $saved_value[1] );
wp_publish_post( $this->parent_post->ID );
$this->assertEquals( 'publish', get_post_status( $this->parent_post->ID ) );
$this->assertEquals( 'draft', get_post_status( $this->child_post_a->ID ) );
$this->assertEquals( 'draft', get_post_status( $this->child_post_b->ID ) );
// reload post from db
$this->child_post_a = get_post( $this->child_post_a->ID );
$this->child_post_b = get_post( $this->child_post_b->ID );
$this->assertEquals( null, $this->child_post_a->post_name );
$this->assertEquals( null, $this->child_post_b->post_name );
}
/**
* Test that when configured to do so, child posts will store a reciprocal post id meta.
*/
public function test_reciprocal_meta() {
$children = new Fieldmanager_Autocomplete(
array(
'name' => 'test_children',
'limit' => 0,
'datasource' => new Fieldmanager_Datasource_Post(
array(
'query_args' => array(
'post_type' => 'post',
),
'reciprocal' => 'parent_post',
)
),
)
);
$this->assertEquals( null, get_post_meta( $this->child_post_a->ID, 'parent_post', true ) );
$this->assertEquals( null, get_post_meta( $this->child_post_b->ID, 'parent_post', true ) );
$this->save_values( $children, $this->parent_post, array( $this->child_post_a->ID ) );
$this->assertEquals( $this->parent_post->ID, get_post_meta( $this->child_post_a->ID, 'parent_post', true ) );
$this->assertEquals( null, get_post_meta( $this->child_post_b->ID, 'parent_post', true ) );
$this->save_values( $children, $this->parent_post, array( $this->child_post_b->ID ) );
$this->assertEquals( null, get_post_meta( $this->child_post_a->ID, 'parent_post', true ) );
$this->assertEquals( $this->parent_post->ID, get_post_meta( $this->child_post_b->ID, 'parent_post', true ) );
$this->save_values( $children, $this->parent_post, array( $this->child_post_a->ID, $this->child_post_b->ID ) );
$this->assertEquals( $this->parent_post->ID, get_post_meta( $this->child_post_a->ID, 'parent_post', true ) );
$this->assertEquals( $this->parent_post->ID, get_post_meta( $this->child_post_b->ID, 'parent_post', true ) );
$this->save_values( $children, $this->parent_post, array() );
$this->assertEquals( null, get_post_meta( $this->child_post_a->ID, 'parent_post', true ) );
$this->assertEquals( null, get_post_meta( $this->child_post_b->ID, 'parent_post', true ) );
}
/**
* Test that when configured to do so, child posts will publish when the parent is published
*/
public function test_publish_children_with_parent() {
$children = new Fieldmanager_Autocomplete(
array(
'name' => 'test_children',
'limit' => 0,
'datasource' => new Fieldmanager_Datasource_Post(
array(
'query_args' => array(
'post_type' => 'post',
),
'publish_with_parent' => true,
)
),
)
);
$this->save_values( $children, $this->parent_post, array( $this->child_post_a->ID, $this->child_post_b->ID ) );
$this->assertEquals( 'draft', get_post_status( $this->parent_post->ID ) );
$this->assertEquals( 'draft', get_post_status( $this->child_post_a->ID ) );
$this->assertEquals( 'draft', get_post_status( $this->child_post_b->ID ) );
wp_publish_post( $this->parent_post->ID );
$this->assertEquals( 'publish', get_post_status( $this->parent_post->ID ) );
$this->assertEquals( 'publish', get_post_status( $this->child_post_a->ID ) );
$this->assertEquals( 'publish', get_post_status( $this->child_post_b->ID ) );
// reload post from db
$this->child_post_a = get_post( $this->child_post_a->ID );
$this->child_post_b = get_post( $this->child_post_b->ID );
$this->assertEquals( sanitize_title( $this->child_post_a->post_title ), $this->child_post_a->post_name );
$this->assertEquals( sanitize_title( $this->child_post_b->post_title ), $this->child_post_b->post_name );
}
/**
* Test multiple-select field with datasource
*/
public function test_save_multiple_select_with_datasource() {
$test_data = array( $this->child_post_a->ID, $this->child_post_b->ID );
$children = new Fieldmanager_Select(
array(
'name' => 'multi_select_test_1',
'multiple' => false,
'datasource' => new Fieldmanager_Datasource_Post(
array(
'query_args' => array(
'post_type' => 'post',
),
)
),
)
);
$this->save_values( $children, $this->parent_post, $test_data );
$saved_value = get_post_meta( $this->parent_post->ID, 'multi_select_test_1', true );
$this->assertNotEquals( $test_data, $saved_value );
$children = new Fieldmanager_Select(
array(
'name' => 'test_save_multiple_select_with_datasource2',
'multiple' => true,
'datasource' => new Fieldmanager_Datasource_Post(
array(
'query_args' => array(
'post_type' => 'post',
),
)
),
)
);
$this->save_values( $children, $this->parent_post, $test_data );
$saved_value = get_post_meta( $this->parent_post->ID, 'test_save_multiple_select_with_datasource2', true );
$this->assertEquals( $test_data, $saved_value );
$children = new Fieldmanager_Select(
array(
'name' => 'test_save_multiple_select_with_datasource3',
'attributes' => array( 'multiple' => 'multiple' ),
'datasource' => new Fieldmanager_Datasource_Post(
array(
'query_args' => array(
'post_type' => 'post',
),
)
),
)
);
$this->save_values( $children, $this->parent_post, $test_data );
$saved_value = get_post_meta( $this->parent_post->ID, 'test_save_multiple_select_with_datasource3', true );
$this->assertEquals( $test_data, $saved_value );
}
/**
* Test that a user lacking permission can not publish a child post.
*
* @expectedException WPDieException
*/
public function test_alter_child_invalid_publish() {
$test_data = array( $this->child_post_a->ID, $this->child_post_b->ID );
$children = new Fieldmanager_Autocomplete(
array(
'name' => 'test_children',
'limit' => 0,
'datasource' => new Fieldmanager_Datasource_Post(
array(
'query_args' => array(
'post_type' => 'post',
),
'publish_with_parent' => true,
)
),
)
);
wp_set_current_user( $this->author );
$this->save_values( $children, $this->parent_post, $test_data );
}
/**
* Test save_to_post_parent logic
*/
public function test_post_parent() {
$test_data = $this->parent_post->ID;
$fm = new Fieldmanager_Autocomplete(
array(
'name' => 'test_parent',
'datasource' => new Fieldmanager_Datasource_Post(
array(
'query_args' => array(
'post_type' => 'post',
),
'save_to_post_parent' => true,
)
),
)
);
$html = $this->_get_html_for( $fm, $this->child_post_a );
$this->assertContains( '<input class="fm-autocomplete-hidden fm-element" type="hidden" name="test_parent" value="" />', $html );
$html = $this->_get_html_for( $fm, $this->child_post_a, $test_data );
// Reload the post
$this->child_post_a = get_post( $this->child_post_a->ID );
$this->assertEquals( $test_data, $this->child_post_a->post_parent );
$this->assertEquals( $test_data, get_post_meta( $this->child_post_a->ID, 'test_parent', true ) );
$this->assertContains(
sprintf( '<input class="fm-autocomplete-hidden fm-element" type="hidden" name="test_parent" value="%d" />', $test_data ),
$html
);
}
/**
* Test save_to_post_parent logic
*/
public function test_post_parent_nested() {
$test_data = array( 'parent' => $this->parent_post->ID );
$fm = new Fieldmanager_Group(
array(
'name' => 'test_parent',
'children' => array(
'parent' => new Fieldmanager_Autocomplete(
'Post Parent',
array(
'datasource' => new Fieldmanager_Datasource_Post(
array(
'query_args' => array(
'post_type' => 'post',
),
'save_to_post_parent' => true,
'only_save_to_post_parent' => true,
)
),
)
),
),
)
);
$html = $this->_get_html_for( $fm, $this->child_post_a );
$this->assertContains( '<input class="fm-autocomplete-hidden fm-element" type="hidden" name="test_parent[parent]" value="" />', $html );
$html = $this->_get_html_for( $fm, $this->child_post_a, $test_data );
// Reload the post
$this->child_post_a = get_post( $this->child_post_a->ID );
$this->assertEquals( $this->parent_post->ID, $this->child_post_a->post_parent );
$this->assertEquals( '', get_post_meta( $this->child_post_a->ID, 'test_parent', true ) );
$this->assertContains(
sprintf( '<input class="fm-autocomplete-hidden fm-element" type="hidden" name="test_parent[parent]" value="%d" />', $this->parent_post->ID ),
$html
);
}
/**
* Test save_to_post_parent_only logic
*/
public function test_post_parent_only() {
$test_data = $this->parent_post->ID;
$fm = new Fieldmanager_Autocomplete(
array(
'name' => 'test_parent',
'datasource' => new Fieldmanager_Datasource_Post(
array(
'query_args' => array(
'post_type' => 'post',
),
'save_to_post_parent' => true,
'only_save_to_post_parent' => true,
)
),
)
);
$html = $this->_get_html_for( $fm, $this->child_post_a );
$this->assertContains( '<input class="fm-autocomplete-hidden fm-element" type="hidden" name="test_parent" value="" />', $html );
$html = $this->_get_html_for( $fm, $this->child_post_a, $test_data );
// Reload the post
$this->child_post_a = get_post( $this->child_post_a->ID );
$this->assertEquals( $test_data, $this->child_post_a->post_parent );
$this->assertEquals( '', get_post_meta( $this->child_post_a->ID, 'test_parent', true ) );
$this->assertContains(
sprintf( '<input class="fm-autocomplete-hidden fm-element" type="hidden" name="test_parent" value="%d" />', $test_data ),
$html
);
}
/**
* Test that a user lacking permission can not add meta to a child post.
*
* @expectedException WPDieException
*/
public function test_alter_child_invalid_reciprocal() {
$test_data = array( $this->child_post_a->ID, $this->child_post_b->ID );
$children = new Fieldmanager_Autocomplete(
array(
'name' => 'test_children',
'limit' => 0,
'datasource' => new Fieldmanager_Datasource_Post(
array(
'query_args' => array(
'post_type' => 'post',
),
'reciprocal' => 'parent_post',
)
),
)
);
wp_set_current_user( $this->author );
$this->save_values( $children, $this->parent_post, $test_data );
}
public function test_post_parent_render() {
$fm = new Fieldmanager_Autocomplete(
array(
'name' => 'test_parent',
'datasource' => new Fieldmanager_Datasource_Post(
array(
'only_save_to_post_parent' => true,
'query_args' => array(
'post_type' => 'post',
),
)
),
)
);
ob_start();
$fm->add_meta_box( 'Test Autocomplete', 'post' )->render_meta_box( $this->child_post_a, array() );
$html = ob_get_clean();
$this->assertRegExp( '/<input[^>]+type=[\'"]hidden[\'"][^>]+value=[\'"]{2}/', $html );
$this->save_values( $fm, $this->child_post_a, $this->parent_post->ID );
$child = get_post( $this->child_post_a->ID );
$this->assertEquals( $this->parent_post->ID, $child->post_parent );
$this->assertEquals( '', get_post_meta( $this->child_post_a->ID, 'test_parent', true ) );
ob_start();
$fm->add_meta_box( 'Test Autocomplete', 'post' )->render_meta_box( $this->child_post_a, array() );
$html = ob_get_clean();
$this->assertRegExp( "/<input[^>]+type=['\"]hidden['\"][^>]+value=['\"]{$this->parent_post->ID}['\"]/", $html );
}
public function test_options_post_parent_render() {
$fm = new Fieldmanager_Select(
array(
'name' => 'test_parent',
'datasource' => new Fieldmanager_Datasource_Post(
array(
'only_save_to_post_parent' => true,
'query_args' => array(
'post_type' => 'post',
'post_status' => 'draft',
),
)
),
)
);
ob_start();
$fm->add_meta_box( 'Test Select', 'post' )->render_meta_box( $this->child_post_a, array() );
$html = ob_get_clean();
$this->assertRegExp(
sprintf( '#<option\s*value="%s"\s*>%s</option>#i', $this->parent_post->ID, $this->parent_post->post_title ),
$html
);
$this->save_values( $fm, $this->child_post_a, $this->parent_post->ID );
$child = get_post( $this->child_post_a->ID );
$this->assertEquals( $this->parent_post->ID, $child->post_parent );
$this->assertEquals( '', get_post_meta( $this->child_post_a->ID, 'test_parent', true ) );
ob_start();
$fm->add_meta_box( 'Test Select', 'post' )->render_meta_box( $this->child_post_a, array() );
$html = ob_get_clean();
$this->assertRegExp(
sprintf( '#<option\s*value="%s"\s*selected(?:\s*=\s*"selected")?\s*>%s</option>#i', $this->parent_post->ID, $this->parent_post->post_title ),
$html
);
}
/**
* @expectedIncorrectUsage Fieldmanager_Datasource_Post::$save_to_post_parent
*/
public function test_repeatable_post_parent_invalid() {
$fm = new Fieldmanager_Autocomplete(
array(
'name' => 'test_limitless_datasource',
'limit' => 0,
'datasource' => new Fieldmanager_Datasource_Post(
array(
'only_save_to_post_parent' => true,
'query_args' => array( 'post_type' => 'post' ),
)
),
)
);
}
/**
* @expectedIncorrectUsage Fieldmanager_Datasource_Post::$save_to_post_parent
*/
public function test_repeatable_options_post_parent_invalid() {
$fm = new Fieldmanager_Select(
array(
'name' => 'test_limitless_datasource',
'limit' => 0,
'datasource' => new Fieldmanager_Datasource_Post(
array(
'only_save_to_post_parent' => true,
'query_args' => array( 'post_type' => 'post' ),
)
),
)
);
}
/**
* @expectedIncorrectUsage Fieldmanager_Datasource_Post::$save_to_post_parent
*/
public function test_inherited_repeatable_post_parent_invalid() {
$fm = new Fieldmanager_Group(
array(
'name' => 'test_limitless_datasource',
'limit' => 0,
'children' => array(
'field' => new Fieldmanager_Autocomplete(
array(
'datasource' => new Fieldmanager_Datasource_Post(
array(
'only_save_to_post_parent' => true,
'query_args' => array( 'post_type' => 'post' ),
)
),
)
),
),
)
);
}
/**
* Test save_to_post_parent_only logic
*/
public function test_unsetting_post_parent_only() {
$fm = new Fieldmanager_Autocomplete(
array(
'name' => 'test_parent',
'datasource' => new Fieldmanager_Datasource_Post(
array(
'only_save_to_post_parent' => true,
'query_args' => array(
'post_type' => 'post',
),
)
),
)
);
$this->save_values( $fm, $this->child_post_a, $this->parent_post->ID );
$child = get_post( $this->child_post_a->ID );
$this->assertEquals( $this->parent_post->ID, $child->post_parent );
$this->save_values( $fm, $this->child_post_a, '' );
$child = get_post( $this->child_post_a->ID );
$this->assertEquals( null, $child->post_parent );
}
}