Completed
Push — master ( 178f75...a30e65 )
by Scott Kingsley
03:43
created

WP_Test_Fields_API_Testcase::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 17
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
/**
4
 * Class WP_Test_Fields_API_Testcase
5
 *
6
 * @uses PHPUnit_Framework_TestCase
7
 */
8
class WP_Test_Fields_API_Testcase extends WP_UnitTestCase {
9
10
	public $object_type = 'post';
11
	public $object_name = 'my_custom_post_type';
12
13
	public function tearDown() {
14
15
		// Do main teardown
16
		parent::tearDown();
17
18
		/**
19
		 * @var $wp_fields WP_Fields_API
20
		 */
21
		global $wp_fields;
22
23
		// Reset WP Fields instance for testing purposes
24
		$wp_fields->remove_form( true, true );
25
		$wp_fields->remove_section( true, true );
26
		$wp_fields->remove_field( true, true );
27
		$wp_fields->remove_control( true, true );
28
29
	}
30
31
	/**
32
	 * Test Fields API is setup
33
	 *
34
	 * @covers WP_Fields_API::__construct
35
	 */
36
	public function test_api() {
37
38
		/**
39
		 * @var $wp_fields WP_Fields_API
40
		 */
41
		global $wp_fields;
42
43
		$this->assertTrue( is_a( $wp_fields, 'WP_Fields_API' ) );
44
45
	}
46
47
	/**
48
	 * Test WP_Fields_API::add_form()
49
	 *
50
	 * @param string $object_type
51
	 * @param string $object_name
52
	 */
53
	public function test_add_form( $object_type = 'post', $object_name = null ) {
54
55
		/**
56
		 * @var $wp_fields WP_Fields_API
57
		 */
58
		global $wp_fields;
59
60
		$wp_fields->add_form( $object_type, 'my_test_form', $object_name );
61
62
	}
63
64
	/**
65
	 * Test WP_Fields_API::add_form()
66
	 *
67
	 * @param string $object_type
68
	 * @param string $object_name
0 ignored issues
show
Bug introduced by
There is no parameter named $object_name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
69
	 */
70
	public function test_add_form_invalid( $object_type = 'post' ) {
71
72
		/**
73
		 * @var $wp_fields WP_Fields_API
74
		 */
75
		global $wp_fields;
76
77
		$wp_fields->add_form( $object_type, null, null, array() );
78
79
	}
80
81
	/**
82
	 * Test WP_Fields_API::get_forms()
83
	 */
84
	public function test_get_forms() {
85
86
		/**
87
		 * @var $wp_fields WP_Fields_API
88
		 */
89
		global $wp_fields;
90
91
		// Add a form
92
		$this->test_add_form( $this->object_type, $this->object_name );
93
94
		// Get forms for object type / name
95
		$forms = $wp_fields->get_forms( $this->object_type, $this->object_name );
96
97
		$this->assertEquals( 1, count( $forms ) );
98
99
		$this->assertArrayHasKey( 'my_test_form', $forms );
100
101
		// Get a form that doesn't exist
102
		$forms = $wp_fields->get_forms( $this->object_type, 'some_other_post_type' );
103
104
		$this->assertEquals( 0, count( $forms ) );
105
106
		// Get all forms for object type
107
		$forms = $wp_fields->get_forms( $this->object_type, true );
108
109
		$this->assertEquals( 1, count( $forms ) );
110
111
		$form_ids = wp_list_pluck( $forms, 'id' );
112
113
		$this->assertContains( 'my_test_form', $form_ids );
114
115
		// Get all forms for all object types
116
		$forms = $wp_fields->get_forms();
117
118
		// Each array item is an object type with an array of object names
119
		$this->assertEquals( 1, count( $forms ) );
120
121
		// Array keys are object types
122
		$this->assertArrayHasKey( $this->object_type, $forms );
123
124
	}
125
126
	/**
127
	 * Test WP_Fields_API::get_forms()
128
	 */
129
	public function test_get_forms_no_object_name() {
130
131
		/**
132
		 * @var $wp_fields WP_Fields_API
133
		 */
134
		global $wp_fields;
135
136
		// Add a form
137
		$this->test_add_form( $this->object_type );
138
139
		// Get forms for object type / name
140
		$forms = $wp_fields->get_forms( $this->object_type );
141
142
		$this->assertEquals( 1, count( $forms ) );
143
144
		$this->assertArrayHasKey( 'my_test_form', $forms );
145
146
	}
147
148
	/**
149
	 * Test WP_Fields_API::get_form()
150
	 */
151
	public function test_get_form() {
152
153
		/**
154
		 * @var $wp_fields WP_Fields_API
155
		 */
156
		global $wp_fields;
157
158
		// Add a form
159
		$this->test_add_form( $this->object_type, $this->object_name );
160
161
		// Form exists for this object type / name
162
		$form = $wp_fields->get_form( $this->object_type, 'my_test_form', $this->object_name );
163
164
		$this->assertNotEmpty( $form );
165
166
		$this->assertEquals( 'my_test_form', $form->id );
167
168
		// Form doesn't exist for this object type / name
169
		$form = $wp_fields->get_form( $this->object_type, 'my_test_form1' );
170
171
		$this->assertEmpty( $form );
172
173
		// Form doesn't exist for this object type / name
174
		$form = $wp_fields->get_form( $this->object_type, 'my_test_form2', $this->object_name );
175
176
		$this->assertEmpty( $form );
177
178
	}
179
180
	/**
181
	 * Test WP_Fields_API::remove_form()
182
	 */
183
	public function test_remove_form() {
184
185
		/**
186
		 * @var $wp_fields WP_Fields_API
187
		 */
188
		global $wp_fields;
189
190
		// Add a form
191
		$this->test_add_form( $this->object_type, $this->object_name );
192
193
		// Form exists for this object type / name
194
		$form = $wp_fields->get_form( $this->object_type, 'my_test_form', $this->object_name );
195
196
		$this->assertNotEmpty( $form );
197
198
		$this->assertEquals( 'my_test_form', $form->id );
199
200
		// Remove form
201
		$wp_fields->remove_form( $this->object_type, 'my_test_form', $this->object_name );
202
203
		// Form no longer exists for this object type / name
204
		$form = $wp_fields->get_form( $this->object_type, 'my_test_form', $this->object_name );
205
206
		$this->assertEmpty( $form );
207
208
	}
209
210
	/**
211
	 * Test WP_Fields_API::remove_form()
212
	 */
213 View Code Duplication
	public function test_remove_form_by_object_type() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
214
215
		/**
216
		 * @var $wp_fields WP_Fields_API
217
		 */
218
		global $wp_fields;
219
220
		// Add a form
221
		$this->test_add_form( $this->object_type, $this->object_name );
222
223
		// Remove form
224
		$wp_fields->remove_form( $this->object_type, null, true );
225
226
		// Form no longer exists for this object type / name
227
		$form = $wp_fields->get_form( $this->object_type, 'my_test_form', $this->object_name );
228
229
		$this->assertEmpty( $form );
230
231
	}
232
233
	/**
234
	 * Test WP_Fields_API::remove_form()
235
	 */
236
	public function test_remove_form_default_object() {
237
238
		/**
239
		 * @var $wp_fields WP_Fields_API
240
		 */
241
		global $wp_fields;
242
243
		// Add a form
244
		$this->test_add_form( $this->object_type );
245
246
		// Remove form
247
		$wp_fields->remove_form( $this->object_type, 'my_test_form' );
248
249
		// Form no longer exists for this object type / name
250
		$form = $wp_fields->get_form( $this->object_type, 'my_test_form' );
251
252
		$this->assertEmpty( $form );
253
254
	}
255
256
	/**
257
	 * Test WP_Fields_API::remove_form()
258
	 */
259 View Code Duplication
	public function test_remove_form_by_object_name() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
260
261
		/**
262
		 * @var $wp_fields WP_Fields_API
263
		 */
264
		global $wp_fields;
265
266
		// Add a form
267
		$this->test_add_form( $this->object_type, $this->object_name );
268
269
		// Remove form
270
		$wp_fields->remove_form( $this->object_type, true, $this->object_name );
271
272
		// Form no longer exists for this object type / name
273
		$form = $wp_fields->get_form( $this->object_type, 'my_test_form', $this->object_name );
274
275
		$this->assertEmpty( $form );
276
277
	}
278
279
	/**
280
	 * Test WP_Fields_API::add_section()
281
	 *
282
	 * @param string $object_type
283
	 * @param string $object_name
284
	 */
285
	public function test_add_section( $object_type = 'post', $object_name = null ) {
286
287
		/**
288
		 * @var $wp_fields WP_Fields_API
289
		 */
290
		global $wp_fields;
291
292
		// Add a form
293
		$this->test_add_form( $object_type, $object_name );
294
295
		$wp_fields->add_section( $object_type, 'my_test_section', $object_name, array(
296
			'form' => 'my_test_form',
297
		) );
298
299
	}
300
301
	/**
302
	 * Test WP_Fields_API::get_sections()
303
	 */
304
	public function test_get_sections() {
305
306
		/**
307
		 * @var $wp_fields WP_Fields_API
308
		 */
309
		global $wp_fields;
310
311
		// Add a form
312
		$this->test_add_section( $this->object_type, $this->object_name );
313
314
		// Get sections for object type / name
315
		$sections = $wp_fields->get_sections( $this->object_type, $this->object_name );
316
317
		$this->assertEquals( 1, count( $sections ) );
318
319
		$this->assertArrayHasKey( 'my_test_section', $sections );
320
321
		// Get a section that doesn't exist
322
		$sections = $wp_fields->get_sections( $this->object_type, 'some_other_post_type' );
323
324
		$this->assertEquals( 0, count( $sections ) );
325
326
		// Get sections by form
327
		$sections = $wp_fields->get_sections( $this->object_type, $this->object_name, 'my_test_form' );
328
329
		$this->assertEquals( 1, count( $sections ) );
330
331
		$this->assertArrayHasKey( 'my_test_section', $sections );
332
333
		$this->assertEquals( 'my_test_form', $sections['my_test_section']->get_form()->id );
334
335
		// Get sections *from* form
336
		$form = $wp_fields->get_form( $this->object_type, 'my_test_form', $this->object_name );
337
338
		$sections = $form->get_children( 'section' );
339
340
		$this->assertEquals( 1, count( $sections ) );
341
342
		$this->assertArrayHasKey( 'my_test_section', $sections );
343
344
		$this->assertEquals( 'my_test_form', $sections['my_test_section']->get_form()->id );
345
346
		// Get all sections for object type
347
		$sections = $wp_fields->get_sections( $this->object_type, true );
348
349
		$this->assertEquals( 1, count( $sections ) );
350
351
		$section_ids = wp_list_pluck( $sections, 'id' );
352
353
		$this->assertContains( 'my_test_section', $section_ids );
354
355
		// Get all sections for all object types
356
		$sections = $wp_fields->get_sections();
357
358
		// Each array item is an object type with an array of object names
359
		$this->assertEquals( 1, count( $sections ) );
360
361
		$this->assertArrayHasKey( $this->object_type, $sections );
362
363
	}
364
365
	/**
366
	 * Test WP_Fields_API::get_section()
367
	 */
368 View Code Duplication
	public function test_get_section() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
369
370
		/**
371
		 * @var $wp_fields WP_Fields_API
372
		 */
373
		global $wp_fields;
374
375
		// Add a form
376
		$this->test_add_section( $this->object_type, $this->object_name );
377
378
		// Section exists for this object type / name
379
		$section = $wp_fields->get_section( $this->object_type, 'my_test_section', $this->object_name );
380
381
		$this->assertNotEmpty( $section );
382
383
		$this->assertEquals( 'my_test_section', $section->id );
384
		$this->assertEquals( 'my_test_form', $section->get_form()->id );
385
386
		// Section doesn't exist for this object type / name
387
		$section = $wp_fields->get_section( $this->object_type, 'my_test_section', 'some_other_post_type' );
388
389
		$this->assertEmpty( $section );
390
391
		// Section doesn't exist for this object type / name
392
		$section = $wp_fields->get_section( $this->object_type, 'my_test_section2', $this->object_name );
393
394
		$this->assertEmpty( $section );
395
396
	}
397
398
	/**
399
	 * Test WP_Fields_API::remove_section()
400
	 */
401
	public function test_remove_section() {
402
403
		/**
404
		 * @var $wp_fields WP_Fields_API
405
		 */
406
		global $wp_fields;
407
408
		// Add a form
409
		$this->test_add_section( $this->object_type, $this->object_name );
410
411
		// Section exists for this object type / name
412
		$section = $wp_fields->get_section( $this->object_type, 'my_test_section', $this->object_name );
413
414
		$this->assertNotEmpty( $section );
415
416
		$this->assertEquals( 'my_test_section', $section->id );
417
418
		// Remove section
419
		$wp_fields->remove_section( $this->object_type, 'my_test_section', $this->object_name );
420
421
		// Section no longer exists for this object type / name
422
		$section = $wp_fields->get_section( $this->object_type, 'my_test_section', $this->object_name );
423
424
		$this->assertEmpty( $section );
425
426
	}
427
428
	/**
429
	 * Test WP_Fields_API::add_field()
430
	 *
431
	 * @param string $object_type
432
	 * @param string $object_name
433
	 */
434
	public function test_add_field( $object_type = 'post', $object_name = null ) {
435
436
		/**
437
		 * @var $wp_fields WP_Fields_API
438
		 */
439
		global $wp_fields;
440
441
		// Add a section for the control
442
		$this->test_add_section( $object_type, $object_name );
443
444
		$wp_fields->add_field( $object_type, 'my_test_field', $object_name, array(
445
			'control' => array(
446
				'id'      => 'my_test_field_control',
447
				'label'   => 'My Test Field',
448
				'type'    => 'text',
449
				'section' => 'my_test_section',
450
			),
451
		) );
452
453
	}
454
455
	/**
456
	 * Test WP_Fields_API::get_fields()
457
	 */
458
	public function test_get_fields() {
459
460
		/**
461
		 * @var $wp_fields WP_Fields_API
462
		 */
463
		global $wp_fields;
464
465
		// Add a field
466
		$this->test_add_field( $this->object_type, $this->object_name );
467
468
		// Get fields for object type / name
469
		$fields = $wp_fields->get_fields( $this->object_type, $this->object_name );
470
471
		$this->assertEquals( 1, count( $fields ) );
472
473
		$this->assertArrayHasKey( 'my_test_field', $fields );
474
475
		// Get a field that doesn't exist
476
		$fields = $wp_fields->get_fields( $this->object_type, 'some_other_post_type' );
477
478
		$this->assertEquals( 0, count( $fields ) );
479
480
	}
481
482
	/**
483
	 * Test WP_Fields_API::get_field()
484
	 */
485 View Code Duplication
	public function test_get_field() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
486
487
		/**
488
		 * @var $wp_fields WP_Fields_API
489
		 */
490
		global $wp_fields;
491
492
		// Add a field
493
		$this->test_add_field( $this->object_type, $this->object_name );
494
495
		// Field exists for this object type / name
496
		$field = $wp_fields->get_field( $this->object_type, 'my_test_field', $this->object_name );
497
498
		$this->assertNotEmpty( $field );
499
500
		$this->assertEquals( 'my_test_field', $field->id );
501
502
		// Field doesn't exist for this object type / name
503
		$field = $wp_fields->get_field( $this->object_type, 'my_test_field', 'some_other_post_type' );
504
505
		$this->assertEmpty( $field );
506
507
		// Field doesn't exist for this object type / name
508
		$field = $wp_fields->get_field( $this->object_type, 'my_test_field2', $this->object_name );
509
510
		$this->assertEmpty( $field );
511
512
	}
513
514
	/**
515
	 * Test WP_Fields_API::remove_field()
516
	 */
517
	public function test_remove_field() {
518
519
		/**
520
		 * @var $wp_fields WP_Fields_API
521
		 */
522
		global $wp_fields;
523
524
		// Add a field
525
		$this->test_add_field( $this->object_type, $this->object_name );
526
527
		// Field exists for this object type / name
528
		$field = $wp_fields->get_field( $this->object_type, 'my_test_field', $this->object_name );
529
530
		$this->assertNotEmpty( $field );
531
532
		$this->assertEquals( 'my_test_field', $field->id );
533
534
		// Remove field
535
		$wp_fields->remove_field( $this->object_type, 'my_test_field', $this->object_name );
536
537
		// Field no longer exists for this object type / name
538
		$field = $wp_fields->get_field( $this->object_type, 'my_test_field', $this->object_name );
539
540
		$this->assertEmpty( $field );
541
542
	}
543
544
	/**
545
	 * Test WP_Fields_API::add_control()
546
	 *
547
	 * @param string $object_type
548
	 * @param string $object_name
549
	 */
550
	public function test_add_control( $object_type = 'post', $object_name = null ) {
551
552
		/**
553
		 * @var $wp_fields WP_Fields_API
554
		 */
555
		global $wp_fields;
556
557
		// Add a field for the control
558
		$this->test_add_field( $object_type, $object_name );
559
560
		$wp_fields->add_control( $object_type, 'my_test_control', $object_name, array(
561
			'section' => 'my_test_section',
562
			'field'   => 'my_test_field',
563
			'label'   => 'My Test Control Field',
564
			'type'    => 'text',
565
		) );
566
567
	}
568
569
	/**
570
	 * Test WP_Fields_API::get_controls()
571
	 */
572
	public function test_get_controls() {
573
574
		/**
575
		 * @var $wp_fields WP_Fields_API
576
		 */
577
		global $wp_fields;
578
579
		// Add a control / field / section
580
		$this->test_add_control( $this->object_type, $this->object_name );
581
582
		// Get controls for object type / name
583
		$controls = $wp_fields->get_controls( $this->object_type, $this->object_name );
584
585
		// There are two controls, the default one with the main field and this control
586
		$this->assertEquals( 2, count( $controls ) );
587
588
		$this->assertArrayHasKey( 'my_test_control', $controls );
589
		$this->assertArrayHasKey( 'my_test_field_control', $controls );
590
591
		$this->assertEquals( 'my_test_section', $controls['my_test_control']->get_section()->id );
592
593
		// Get a control that doesn't exist
594
		$controls = $wp_fields->get_controls( $this->object_type, 'some_other_post_type' );
595
596
		$this->assertEquals( 0, count( $controls ) );
597
598
		// Get controls by section
599
		$controls = $wp_fields->get_controls( $this->object_type, $this->object_name, 'my_test_section' );
600
601
		$this->assertEquals( 2, count( $controls ) );
602
603
		$this->assertArrayHasKey( 'my_test_control', $controls );
604
		$this->assertArrayHasKey( 'my_test_field_control', $controls );
605
606
		$this->assertEquals( 'my_test_section', $controls['my_test_control']->get_section()->id );
607
608
		// Get sections *from* form
609
		$section = $wp_fields->get_section( $this->object_type, 'my_test_section', $this->object_name );
610
611
		$controls = $section->get_children( 'control' );
612
613
		$this->assertEquals( 2, count( $controls ) );
614
615
		$this->assertArrayHasKey( 'my_test_control', $controls );
616
		$this->assertArrayHasKey( 'my_test_field_control', $controls );
617
618
		$this->assertEquals( 'my_test_section', $controls['my_test_control']->get_section()->id );
619
620
	}
621
622
	/**
623
	 * Test WP_Fields_API::get_control()
624
	 */
625
	public function test_get_control() {
626
627
		/**
628
		 * @var $wp_fields WP_Fields_API
629
		 */
630
		global $wp_fields;
631
632
		// Add a control / field / section
633
		$this->test_add_control( $this->object_type, $this->object_name );
634
635
		// Control exists for this object type / name
636
		$control = $wp_fields->get_control( $this->object_type, 'my_test_field_control', $this->object_name );
637
638
		$this->assertNotEmpty( $control );
639
640
		$this->assertEquals( 'my_test_field_control', $control->id );
641
		$this->assertNotEmpty( $control->get_field() );
642
		$this->assertEquals( 'my_test_field', $control->get_field()->id );
643
		$this->assertEquals( 'my_test_section', $control->get_section()->id );
644
645
		// Control exists for this object type / name
646
		$control = $wp_fields->get_control( $this->object_type, 'my_test_control', $this->object_name );
647
648
		$this->assertNotEmpty( $control );
649
650
		$this->assertEquals( 'my_test_control', $control->id );
651
		$this->assertNotEmpty( $control->get_field() );
652
		$this->assertEquals( 'my_test_field', $control->get_field()->id );
653
		$this->assertEquals( 'my_test_section', $control->get_section()->id );
654
655
		// Control doesn't exist for this object type / name
656
		$control = $wp_fields->get_control( $this->object_type, 'my_test_control', 'some_other_post_type' );
657
658
		$this->assertEmpty( $control );
659
660
		// Control doesn't exist for this object type / name
661
		$control = $wp_fields->get_control( $this->object_type, 'my_test_control2', $this->object_name );
662
663
		$this->assertEmpty( $control );
664
665
	}
666
667
	/**
668
	 * Test WP_Fields_API::remove_control()
669
	 */
670
	public function test_remove_control() {
671
672
		/**
673
		 * @var $wp_fields WP_Fields_API
674
		 */
675
		global $wp_fields;
676
677
		// Add a control / field / section
678
		$this->test_add_control( $this->object_type, $this->object_name );
679
680
		// Control exists for this object type / name
681
		$control = $wp_fields->get_control( $this->object_type, 'my_test_control', $this->object_name );
682
683
		$this->assertNotEmpty( $control );
684
685
		$this->assertEquals( 'my_test_control', $control->id );
686
		$this->assertEquals( 'my_test_field', $control->get_field()->id );
687
		$this->assertEquals( 'my_test_section', $control->get_section()->id );
688
689
		// Remove control
690
		$wp_fields->remove_control( $this->object_type, 'my_test_control', $this->object_name );
691
692
		// Control no longer exists for this object type / name
693
		$control = $wp_fields->get_control( $this->object_type, 'my_test_control', $this->object_name );
694
695
		$this->assertEmpty( $control );
696
697
		// Remove field's control
698
		$wp_fields->remove_control( $this->object_type, 'my_test_field_control', $this->object_name );
699
700
		// Control no longer exists for this object type / name
701
		$control = $wp_fields->get_control( $this->object_type, 'my_test_field_control', $this->object_name );
702
703
		$this->assertEmpty( $control );
704
705
	}
706
707
}