JFormTest::tearDown()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright  Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
4
 * @license    GNU General Public License version 2 or later; see LICENSE
5
 */
6
7
namespace Joomla\Form\Tests;
8
9
use Joomla\Language\Language;
10
use Joomla\Language\Text;
11
use Joomla\Form\Form;
12
use Joomla\Form\FormHelper;
13
use Joomla\Form\Rule;
14
15
/**
16
 * Test class for JForm.
17
 *
18
 * @since  1.0
19
 */
20
class JFormTest extends \PHPUnit_Framework_TestCase
21
{
22
	/**
23
	 * Test showXml
24
	 *
25
	 * @param   string  $form  The form.
26
	 *
27
	 * @return void
28
	 */
29
	private function _showXml($form)
30
	{
31
		$dom = new \DOMDocument('1.0');
32
		$dom->preserveWhiteSpace = false;
33
		$dom->formatOutput = true;
34
		$dom->loadXML($form->getXml()->asXML());
0 ignored issues
show
Bug introduced by
The method getXml cannot be called on $form (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
35
		echo $dom->saveXML();
36
	}
37
38
	/**
39
	 * Set up for testing
40
	 *
41
	 * @return void
42
	 */
43
	public function setUp()
44
	{
45
		parent::setUp();
46
47
		include_once 'JFormDataHelper.php';
48
	}
49
50
	/**
51
	 * Tear down test
52
	 *
53
	 * @return void
54
	 */
55
	protected function tearDown()
56
	{
57
	}
58
59
	/**
60
	 * Test the Form::addNode method.
61
	 *
62
	 * @return void
63
	 */
64 View Code Duplication
	public function testAddNode()
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...
65
	{
66
		// The source data.
67
		$xml1 = simplexml_load_string('<form><fields /></form>');
68
69
		// The new data for adding the field.
70
		$xml2 = simplexml_load_string('<form><field name="foo" /></form>');
71
72
		if ($xml1 === false || $xml2 === false)
73
		{
74
			$this->fail('Error in text XML data');
75
		}
76
77
		JFormInspector::addNode($xml1->fields, $xml2->field);
78
79
		$fields = $xml1->xpath('fields/field[@name="foo"]');
80
		$this->assertThat(
81
			count($fields),
82
			$this->equalTo(1),
83
			'Line:' . __LINE__ . ' The field should be added, ungrouped.'
84
		);
85
	}
86
87
	/**
88
	 * Tests the Form::bind method.
89
	 *
90
	 * This method is used to load data into the JForm object.
91
	 *
92
	 * @return void
93
	 */
94
	public function testBind()
95
	{
96
		$form = new JFormInspector('form1');
97
98
		$xml = JFormDataHelper::$bindDocument;
99
100
		// Check the test data loads ok.
101
		$this->assertThat(
102
			$form->load($xml),
103
			$this->isTrue(),
104
			'Line:' . __LINE__ . ' XML string should load successfully.'
105
		);
106
107
		$data = array(
108
			'title' => 'Joomla Framework',
109
			'author' => 'Should not bind',
110
			'params' => array(
111
				'show_title' => 1,
112
				'show_abstract' => 0,
113
				'show_author' => 1,
114
				'categories' => array(
115
					1,
116
					2
117
				),
118
				'keywords' => array('en-GB' => 'Joomla', 'fr-FR' => 'Joomla')
119
			)
120
		);
121
122
		$this->assertThat(
123
			$form->bind($data),
124
			$this->isTrue(),
125
			'Line:' . __LINE__ . ' The data should bind successfully.'
126
		);
127
128
		$data = $form->getData();
129
		$this->assertThat(
130
			$data->get('title'),
131
			$this->equalTo('Joomla Framework'),
132
			'Line:' . __LINE__ . ' The data should bind to form field elements.'
133
		);
134
135
		$this->assertThat(
136
			$data->get('author'),
137
			$this->isNull(),
138
			'Line:' . __LINE__ . ' The data should not bind to unknown form field elements.'
139
		);
140
141
		$this->assertThat(
142
			is_array($data->get('params.categories')),
143
			$this->isTrue(),
144
			'Line:' . __LINE__ . ' The categories param should be an array.'
145
		);
146
	}
147
148
	/**
149
	 * Testing methods used by the instantiated object.
150
	 *
151
	 * @return void
152
	 */
153
	public function testConstruct()
154
	{
155
		$form = new JFormInspector('form1');
156
157
		$this->assertThat(
158
			($form instanceof Form),
159
			$this->isTrue(),
160
			'Line:' . __LINE__ . ' The JForm constuctor should return a JForm object.'
161
		);
162
163
		// Check the integrity of the options.
164
165
		$options = $form->getOptions();
166
		$this->assertThat(
167
			isset($options['control']),
168
			$this->isTrue(),
169
			'Line:' . __LINE__ . ' The JForm object should contain an options array with a control setting.'
170
		);
171
172
		$options = $form->getOptions();
173
		$this->assertThat(
174
			$options['control'],
175
			$this->isFalse(),
176
			'Line:' . __LINE__ . ' The control setting should be false by default.'
177
		);
178
179
		$form = new JFormInspector('form1', array('control' => 'jform'));
180
181
		$options = $form->getOptions();
182
		$this->assertThat(
183
			$options['control'],
184
			$this->equalTo('jform'),
185
			'Line:' . __LINE__ . ' The control setting should be what is passed in the constructor.'
186
		);
187
	}
188
189
	/**
190
	 * Test for Form::filter method.
191
	 *
192
	 * @return void
193
	 */
194
	public function testFilter()
195
	{
196
		$form = new JFormInspector('form1');
197
198
		// Check the test data loads ok.
199
		$this->assertThat(
200
			$form->load(JFormDataHelper::$filterDocument),
201
			$this->isTrue(),
202
			'Line:' . __LINE__ . ' XML string should load successfully.'
203
		);
204
205
		$data = array(
206
			'word' => 'Joomla! Framework',
207
			'author' => 'Should not bind',
208
			'params' => array(
209
				'show_title' => 1,
210
				'show_author' => false,
211
			),
212
			'default' => ''
213
		);
214
215
		$filtered = $form->filter($data);
216
217
		$this->assertThat(
218
			is_array($filtered),
219
			$this->isTrue(),
220
			'Line:' . __LINE__ . ' The filtered result should be an array.'
221
		);
222
223
		// Test that filtering is occuring (not that all filters work - done in testFilterField).
224
225
		$this->assertThat(
226
			$filtered['word'],
227
			$this->equalTo('JoomlaFramework'),
228
			'Line:' . __LINE__ . ' The variable should be filtered by the "word" filter.'
229
		);
230
231
		$this->assertThat(
232
			isset($filtered['author']),
233
			$this->isFalse(),
234
			'Line:' . __LINE__ . ' A variable in the data not present in the form should not exist.'
235
		);
236
237
		$this->assertThat(
238
			$filtered['params']['show_title'],
239
			$this->equalTo(1),
240
			'Line:' . __LINE__ . ' The nested variable should be present.'
241
		);
242
243
		$this->assertThat(
244
			$filtered['params']['show_author'],
245
			$this->equalTo(0),
246
			'Line:' . __LINE__ . ' The nested variable should be present.'
247
		);
248
	}
249
250
	/**
251
	 * Test for Form::filterField method.
252
	 *
253
	 * @return void
254
	 */
255
	public function testFilterField()
256
	{
257
		$form = new JFormInspector('form1');
258
259
		// Check the test data loads ok.
260
		$this->assertThat(
261
			$form->load(JFormDataHelper::$filterDocument),
262
			$this->isTrue(),
263
			'Line:' . __LINE__ . ' XML string should load successfully.'
264
		);
265
266
		$input = '<script>alert();</script> <p>Some text.</p>';
267
268
		$this->assertThat(
269
			$form->filterField($form->findField('function'), $input),
270
			$this->equalTo('function'),
271
			'Line:' . __LINE__ . ' The function filter should be correctly applied.'
272
		);
273
274
		$this->assertThat(
275
			$form->filterField($form->findField('int'), 'A1B2C3'),
276
			$this->equalTo(1),
277
			'Line:' . __LINE__ . ' The "int" filter should be correctly applied.'
278
		);
279
280
		$this->assertThat(
281
			$form->filterField($form->findField('method'), $input),
282
			$this->equalTo('method'),
283
			'Line:' . __LINE__ . ' The class method filter should be correctly applied.'
284
		);
285
286
		$this->assertThat(
287
			$form->filterField($form->findField('raw'), $input),
288
			$this->equalTo($input),
289
			'Line:' . __LINE__ . ' "The safehtml" filter should be correctly applied.'
290
		);
291
292
		$this->assertThat(
293
			$form->filterField($form->findField('safehtml'), $input),
294
			$this->equalTo('alert(); <p>Some text.</p>'),
295
			'Line:' . __LINE__ . ' "The safehtml" filter should be correctly applied.'
296
		);
297
298
		$this->assertThat(
299
			$form->filterField($form->findField('unset'), $input),
300
			$this->equalTo(null),
301
			'Line:' . __LINE__ . ' The value should be unset.'
302
		);
303
304
		$this->assertThat(
305
			$form->filterField($form->findField('word'), $input),
306
			$this->equalTo('scriptalertscriptpSometextp'),
307
			'Line:' . __LINE__ . ' The "word" filter should be correctly applied.'
308
		);
309
310
		$this->assertThat(
311
			$form->filterField($form->findField('url'), 'http://example.com'),
312
			$this->equalTo('http://example.com'),
313
			'Line:' . __LINE__ . ' A field with a valid protocol should return as is.'
314
		);
315
316
		$this->assertThat(
317
			$form->filterField($form->findField('url'), 'http://<script>alert();</script> <p>Some text.</p>'),
318
			$this->equalTo('http://alert(); Some text.'),
319
			'Line:' . __LINE__ . ' A "url" with scripts should be should be filtered.'
320
		);
321
322
		$this->assertThat(
323
			$form->filterField($form->findField('url'), 'https://example.com'),
324
			$this->equalTo('https://example.com'),
325
			'Line:' . __LINE__ . ' A field with a valid protocol that is not http should return as is.'
326
		);
327
328
		$this->assertThat(
329
			$form->filterField($form->findField('url'), 'example.com'),
330
			$this->equalTo('http://example.com'),
331
			'Line:' . __LINE__ . ' A field without a protocol should return with a http:// protocol.'
332
		);
333
334
		$this->assertThat(
335
			$form->filterField($form->findField('url'), 'hptarr.com'),
336
			$this->equalTo('http://hptarr.com'),
337
			'Line:' . __LINE__ . ' A field without a protocol and starts with t should return with a http:// protocol.'
338
		);
339
340
		$this->assertThat(
341
			$form->filterField($form->findField('url'), ''),
342
			$this->equalTo(''),
343
			'Line:' . __LINE__ . ' An empty "url" filter return nothing.'
344
		);
345
346
		$this->assertThat(
347
			$form->filterField($form->findField('default'), $input),
348
			$this->equalTo('alert(); Some text.'),
349
			'Line:' . __LINE__ . ' The default strict filter should be correctly applied.'
350
		);
351
352
		$this->assertThat(
353
			$form->filterField($form->findField('tel'), '222.3333333333'),
354
			$this->equalTo('222.3333333333'),
355
			'Line:' . __LINE__ . ' The tel filter should be correctly applied.'
356
		);
357
		$this->assertThat(
358
			$form->filterField($form->findField('tel'), '+222.3333333333'),
359
			$this->equalTo('222.3333333333'),
360
			'Line:' . __LINE__ . ' The tel filter should be correctly applied.'
361
		);
362
		$this->assertThat(
363
			$form->filterField($form->findField('tel'), '+2,2,2.3,3,3,3,3,3,3,3,3,3,3,3'),
364
			$this->equalTo('222.333333333333'),
365
			'Line:' . __LINE__ . ' The tel filter should be correctly applied.'
366
		);
367
		$this->assertThat(
368
			$form->filterField($form->findField('tel'), '33333333333'),
369
			$this->equalTo('.33333333333'),
370
			'Line:' . __LINE__ . ' The tel filter should be correctly applied.'
371
		);
372
		$this->assertThat(
373
			$form->filterField($form->findField('tel'), '222333333333333'),
374
			$this->equalTo('222.333333333333'),
375
			'Line:' . __LINE__ . ' The tel filter should be correctly applied.'
376
		);
377
		$this->assertThat(
378
			$form->filterField($form->findField('tel'), '1 (202) 555-5555'),
379
			$this->equalTo('1.2025555555'),
380
			'Line:' . __LINE__ . ' The tel filter should be correctly applied.'
381
		);
382
		$this->assertThat(
383
			$form->filterField($form->findField('tel'), '+222.33333333333x444'),
384
			$this->equalTo('222.33333333333'),
385
			'Line:' . __LINE__ . ' The tel filter should be correctly applied.'
386
		);
387
		$this->assertThat(
388
			$form->filterField($form->findField('tel'), 'ABCabc/?.!*x'),
389
			$this->equalTo(''),
390
			'Line:' . __LINE__ . ' The tel filter should be correctly applied.'
391
		);
392
	}
393
394
	/**
395
	 * Test the Form::findField method.
396
	 *
397
	 * @return void
398
	 */
399
	public function testFindField()
400
	{
401
		// Prepare the form.
402
		$form = new JFormInspector('form1');
403
404
		$xml = JFormDataHelper::$findFieldDocument;
405
406
		// Check the test data loads ok.
407
		$this->assertThat(
408
			$form->load($xml),
409
			$this->isTrue(),
410
			'Line:' . __LINE__ . ' XML string should load successfully.'
411
		);
412
413
		// Error handling.
414
415
		$this->assertThat(
416
			$form->findField('bogus'),
417
			$this->isFalse(),
418
			'Line:' . __LINE__ . ' An ungrouped field that does not exist should return false.'
419
		);
420
421
		$this->assertThat(
422
			$form->findField('title', 'bogus'),
423
			$this->isFalse(),
424
			'Line:' . __LINE__ . ' An field in a group that does not exist should return false.'
425
		);
426
427
		// Test various find combinations.
428
429
		$field = $form->findField('title', null);
430
		$this->assertThat(
431
			(string) $field['place'],
432
			$this->equalTo('root'),
433
			'Line:' . __LINE__ . ' A known ungrouped field should load successfully.'
434
		);
435
436
		$field = $form->findField('title', 'params');
437
		$this->assertThat(
438
			(string) $field['place'],
439
			$this->equalTo('child'),
440
			'Line:' . __LINE__ . ' A known grouped field should load successfully.'
441
		);
442
443
		$field = $form->findField('alias');
444
		$this->assertThat(
445
			(string) $field['name'],
446
			$this->equalTo('alias'),
447
			'Line:' . __LINE__ . ' A known field in a fieldset should load successfully.'
448
		);
449
450
		$field = $form->findField('show_title', 'params');
451
		$this->assertThat(
452
			(string) $field['default'],
453
			$this->equalTo('1'),
454
			'Line:' . __LINE__ . ' A known field in a group fieldset should load successfully.'
455
		);
456
	}
457
458
	/**
459
	 * Tests the Form::findFieldsByFieldset method.
460
	 *
461
	 * @return void
462
	 */
463
	public function testFindFieldsByFieldset()
464
	{
465
		// Prepare the form.
466
		$form = new JFormInspector('form1');
467
468
		$this->assertThat(
469
			$form->load(JFormDataHelper::$findFieldsByFieldsetDocument),
470
			$this->isTrue(),
471
			'Line:' . __LINE__ . ' XML string should load successfully.'
472
		);
473
474
		// Error handling.
475
476
		$this->assertThat(
477
			$form->findFieldsByFieldset('bogus'),
478
			$this->equalTo(array()),
479
			'Line:' . __LINE__ . ' An unknown fieldset should return an empty array.'
480
		);
481
482
		// Test regular usage.
483
484
		$this->assertThat(
485
			count($form->findFieldsByFieldset('params-basic')),
486
			$this->equalTo(3),
487
			'Line:' . __LINE__ . ' The params-basic fieldset has 3 fields.'
488
		);
489
490
		$this->assertThat(
491
			count($form->findFieldsByFieldset('params-advanced')),
492
			$this->equalTo(2),
493
			'Line:' . __LINE__ . ' The params-advanced fieldset has 2 fields.'
494
		);
495
	}
496
497
	/**
498
	 * Test the Form::findFieldsByGroup method.
499
	 *
500
	 * @return void
501
	 */
502
	public function testFindFieldsByGroup()
503
	{
504
		// Prepare the form.
505
		$form = new JFormInspector('form1');
506
507
		$this->assertThat(
508
			$form->load(JFormDataHelper::$findFieldsByGroupDocument),
509
			$this->isTrue(),
510
			'Line:' . __LINE__ . ' XML string should load successfully.'
511
		);
512
513
		// Error handling.
514
515
		$this->assertThat(
516
			$form->findFieldsByGroup('bogus'),
517
			$this->equalTo(array()),
518
			'Line:' . __LINE__ . ' A group that does not exist should return an empty array.'
519
		);
520
521
		// Test all fields.
522
523
		$this->assertThat(
524
			count($form->findFieldsByGroup()),
525
			$this->equalTo(11),
526
			'Line:' . __LINE__ . ' There are 9 field elements in total.'
527
		);
528
529
		// Test ungrouped fields.
530
531
		$this->assertThat(
532
			count($form->findFieldsByGroup(false)),
533
			$this->equalTo(4),
534
			'Line:' . __LINE__ . ' There are 4 ungrouped field elements.'
535
		);
536
537
		// Test grouped fields.
538
539
		$this->assertThat(
540
			count($form->findFieldsByGroup('details')),
541
			$this->equalTo(2),
542
			'Line:' . __LINE__ . ' The details group has 2 field elements.'
543
		);
544
545
		$this->assertThat(
546
			count($form->findFieldsByGroup('params')),
547
			$this->equalTo(3),
548
			'Line:' . __LINE__ . ' The params group has 3 field elements, including one nested in a fieldset.'
549
		);
550
551
		// Test nested fields.
552
553
		$this->assertThat(
554
			count($form->findFieldsByGroup('level1', true)),
555
			$this->equalTo(2),
556
			'Line:' . __LINE__ . ' There should be 2 nested fields.'
557
		);
558
	}
559
560
	/**
561
	 * Test the Form::findGroup method.
562
	 *
563
	 * @return void
564
	 */
565
	public function testFindGroup()
566
	{
567
		// Prepare the form.
568
		$form = new JFormInspector('form1');
569
570
		$this->assertThat(
571
			$form->load(JFormDataHelper::$findGroupDocument),
572
			$this->isTrue(),
573
			'Line:' . __LINE__ . ' XML string should load successfully.'
574
		);
575
576
		$this->assertThat(
577
			$form->findGroup('bogus'),
578
			$this->equalTo(array()),
579
			'Line:' . __LINE__ . ' A group that does not exist should return an empty array.'
580
		);
581
582
		$this->assertThat(
583
			count($form->findGroup('params')),
584
			$this->equalTo(1),
585
			'Line:' . __LINE__ . ' The group should have one element.'
586
		);
587
588
		$this->assertThat(
589
			$form->findGroup('bogus.data'),
590
			$this->equalTo(array()),
591
			'Line:' . __LINE__ . ' A group path that does not exist should return an empty array.'
592
		);
593
594
		// Check that an existant field returns something.
595
		$this->assertThat(
596
			count($form->findGroup('params.cache')),
597
			$this->equalTo(1),
598
			'Line:' . __LINE__ . ' The group should have one element.'
599
		);
600
	}
601
602
	/**
603
	 * Test for Form::getErrors method.
604
	 *
605
	 * @return void
606
	 */
607
	public function testGetErrors()
608
	{
609
		$form = new JFormInspector('form1');
610
611
		$this->assertThat(
612
			$form->load(JFormDataHelper::$validateDocument),
613
			$this->isTrue(),
614
			'Line:' . __LINE__ . ' XML string should load successfully.'
615
		);
616
617
		$fail = array(
618
			'boolean' => 'comply',
619
			'required' => '',
620
		);
621
622
		$this->assertThat(
623
			$form->validate($fail),
624
			$this->isFalse(),
625
			'Line:' . __LINE__ . ' Validating this data should fail.'
626
		);
627
628
		$errors = $form->getErrors($fail);
0 ignored issues
show
Unused Code introduced by
The call to JFormInspector::getErrors() has too many arguments starting with $fail.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
629
		$this->assertThat(
630
			count($errors),
631
			$this->equalTo(3),
632
			'Line:' . __LINE__ . ' This data should invoke 3 errors.'
633
		);
634
635
		$this->assertThat(
636
			$errors[0] instanceof \Exception,
637
			$this->isTrue(),
638
			'Line:' . __LINE__ . ' The errors should be exception objects.'
639
		);
640
	}
641
642
	/**
643
	 * Test the Form::getField method.
644
	 *
645
	 * @return void
646
	 */
647
	public function testGetField()
648
	{
649
		// Prepare the form.
650
		$form = new JFormInspector('form1');
651
652
		$this->assertThat(
653
			$form->load(JFormDataHelper::$getFieldDocument),
654
			$this->isTrue(),
655
			'Line:' . __LINE__ . ' XML string should load successfully.'
656
		);
657
658
		// Check for errors.
659
660
		$this->assertThat(
661
			$form->getField('bogus'),
662
			$this->isFalse(),
663
			'Line:' . __LINE__ . ' A field that does not exist should return false.'
664
		);
665
666
		$this->assertThat(
667
			$form->getField('show_title'),
668
			$this->isFalse(),
669
			'Line:' . __LINE__ . ' A field that does exists in a group, without declaring the group, should return false.'
670
		);
671
672
		$this->assertThat(
673
			$form->getField('show_title', 'bogus'),
674
			$this->isFalse(),
675
			'Line:' . __LINE__ . ' A field in a group that does not exist should return false.'
676
		);
677
678
		// Checking value defaults.
679
680
		$this->assertThat(
681
			$form->getField('title')->value,
682
			$this->equalTo(''),
683
			'Line:' . __LINE__ . ' Prior to binding data, the defaults in the field should be used.'
684
		);
685
686
		$this->assertThat(
687
			$form->getField('show_title', 'params')->value,
688
			$this->equalTo(1),
689
			'Line:' . __LINE__ . ' Prior to binding data, the defaults in the field should be used.'
690
		);
691
692
		// Check values after binding.
693
694
		$data = array(
695
			'title' => 'The title',
696
			'show_title' => 3,
697
			'params' => array(
698
				'show_title' => 2,
699
			)
700
		);
701
702
		$this->assertThat(
703
			$form->bind($data),
704
			$this->isTrue(),
705
			'Line:' . __LINE__ . ' The input data should bind successfully.'
706
		);
707
708
		$this->assertThat(
709
			$form->getField('title')->value,
710
			$this->equalTo('The title'),
711
			'Line:' . __LINE__ . ' Check the field value bound correctly.'
712
		);
713
714
		$this->assertThat(
715
			$form->getField('show_title', 'params')->value,
716
			$this->equalTo(2),
717
			'Line:' . __LINE__ . ' Check the field value bound correctly.'
718
		);
719
720
		// Check binding with an object.
721
722
		$data = new \stdClass;
723
		$data->title = 'The new title';
724
		$data->show_title = 5;
725
		$data->params = new \stdClass;
726
		$data->params->show_title = 4;
727
728
		$this->assertThat(
729
			$form->bind($data),
730
			$this->isTrue(),
731
			'Line:' . __LINE__ . ' The input data should bind successfully.'
732
		);
733
734
		$this->assertThat(
735
			$form->getField('title')->value,
736
			$this->equalTo('The new title'),
737
			'Line:' . __LINE__ . ' Check the field value bound correctly.'
738
		);
739
740
		$this->assertThat(
741
			$form->getField('show_title', 'params')->value,
742
			$this->equalTo(4),
743
			'Line:' . __LINE__ . ' Check the field value bound correctly.'
744
		);
745
	}
746
747
	/**
748
	 * Test for Form::getFieldAttribute method.
749
	 *
750
	 * @return void
751
	 */
752 View Code Duplication
	public function testGetFieldAttribute()
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...
753
	{
754
		$form = new JFormInspector('form1');
755
756
		$this->assertThat(
757
			$form->load(JFormDataHelper::$getFieldDocument),
758
			$this->isTrue(),
759
			'Line:' . __LINE__ . ' XML string should load successfully.'
760
		);
761
762
		// Test error handling.
763
764
		$this->assertThat(
765
			$form->getFieldAttribute('bogus', 'unknown', 'Help'),
766
			$this->equalTo('Help'),
767
			'Line:' . __LINE__ . ' The default value of the unknown field should be returned.'
768
		);
769
770
		$this->assertThat(
771
			$form->getFieldAttribute('title', 'unknown', 'Use this'),
772
			$this->equalTo('Use this'),
773
			'Line:' . __LINE__ . ' The default value of the unknown attribute should be returned.'
774
		);
775
776
		// Test general usage.
777
778
		$this->assertThat(
779
			$form->getFieldAttribute('title', 'description'),
780
			$this->equalTo('The title.'),
781
			'Line:' . __LINE__ . ' The value of the attribute should be returned.'
782
		);
783
784
		$this->assertThat(
785
			$form->getFieldAttribute('title', 'description', 'Use this'),
786
			$this->equalTo('The title.'),
787
			'Line:' . __LINE__ . ' The value of the attribute should be returned.'
788
		);
789
	}
790
791
	/**
792
	 * Test the Form::getFormControl method.
793
	 *
794
	 * @return void
795
	 */
796
	public function testGetFormControl()
797
	{
798
		$form = new Form('form8ion');
0 ignored issues
show
Deprecated Code introduced by
The class Joomla\Form\Form has been deprecated with message: The joomla/form package is deprecated

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
799
800
		$this->assertThat(
801
			$form->getFormControl(),
802
			$this->equalTo(''),
803
			'Line:' . __LINE__ . ' A form control that has not been specified should return nothing.'
804
		);
805
806
		$form = new Form('form8ion', array('control' => 'jform'));
0 ignored issues
show
Deprecated Code introduced by
The class Joomla\Form\Form has been deprecated with message: The joomla/form package is deprecated

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
807
808
		$this->assertThat(
809
			$form->getFormControl(),
810
			$this->equalTo('jform'),
811
			'Line:' . __LINE__ . ' The form control should agree with the options passed in the constructor.'
812
		);
813
	}
814
815
	/**
816
	 * Test for Form::getInstance.
817
	 *
818
	 * @return void
819
	 */
820
	public function testGetInstance()
821
	{
822
		$this->markTestIncomplete();
823
	}
824
825
	/**
826
	 * Test for Form::getGroup method.
827
	 *
828
	 * @return void
829
	 */
830
	public function testGetGroup()
831
	{
832
		$form = new JFormInspector('form1');
833
834
		$this->assertThat(
835
			$form->load(JFormDataHelper::$findFieldsByGroupDocument),
836
			$this->isTrue(),
837
			'Line:' . __LINE__ . ' XML string should load successfully.'
838
		);
839
840
		// Test error handling.
841
842
		$this->assertThat(
843
			$form->getGroup('bogus'),
844
			$this->equalTo(array()),
845
			'Line:' . __LINE__ . ' A group that does not exist should return an empty array.'
846
		);
847
848
		// Test general usage.
849
850
		$this->assertThat(
851
			count($form->getGroup('params')),
852
			$this->equalTo(3),
853
			'Line:' . __LINE__ . ' The params group should have 3 field elements.'
854
		);
855
856
		$this->assertThat(
857
			count($form->getGroup('level1', true)),
858
			$this->equalTo(2),
859
			'Line:' . __LINE__ . ' The level1 group should have 2 nested field elements.'
860
		);
861
862
		$this->assertThat(
863
			array_keys($form->getGroup('level1', true)),
864
			$this->equalTo(array('level1_field1', 'level1_level2_field2')),
865
			'Line:' . __LINE__ . ' The level1 group should have 2 nested field elements.'
866
		);
867
868
		$this->assertThat(
869
			count($form->getGroup('level1.level2')),
870
			$this->equalTo(1),
871
			'Line:' . __LINE__ . ' The level2 group should have 1 field element.'
872
		);
873
	}
874
875
	/**
876
	 * Test for Form::getInput method.
877
	 *
878
	 * @return void
879
	 */
880
	public function testGetInput()
881
	{
882
		$form = new JFormInspector('form1');
883
884
		$this->assertThat(
885
			$form->load(JFormDataHelper::$loadFieldDocument),
886
			$this->isTrue(),
887
			'Line:' . __LINE__ . ' XML string should load successfully.'
888
		);
889
890
		$this->assertThat(
891
			$form->getInput('title', null, 'The Title'),
892
			$this->equalTo('<input type="text" name="title" id="title_id" value="The Title" class="inputbox required"/>'),
893
			'Line:' . __LINE__ . ' The method should return a simple input text field.'
894
		);
895
896
		$this->assertThat(
897
			$form->getInput('show_title', 'params', '0'),
898
			$this->equalTo(
899
				'<fieldset id="params_show_title" class="radio">' .
900
					'<input type="radio" id="params_show_title0" name="params[show_title]" value="1"/>' .
901
					'<label for="params_show_title0">' . Text::_('JYes') . '</label>' .
0 ignored issues
show
Deprecated Code introduced by
The method Joomla\Language\Text::_() has been deprecated with message: 2.0 Will be replaced with a `translate` method.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
902
					'<input type="radio" id="params_show_title1" name="params[show_title]" value="0" checked="checked"/>' .
903
					'<label for="params_show_title1">' . Text::_('JNo') . '</label>' .
0 ignored issues
show
Deprecated Code introduced by
The method Joomla\Language\Text::_() has been deprecated with message: 2.0 Will be replaced with a `translate` method.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
904
					'</fieldset>'
905
			),
906
			'Line:' . __LINE__ . ' The method should return a radio list.'
907
		);
908
909
		$form = new JFormInspector('form1', array('control' => 'jform'));
910
911
		$this->assertThat(
912
			$form->load(JFormDataHelper::$loadFieldDocument),
913
			$this->isTrue(),
914
			'Line:' . __LINE__ . ' XML string should load successfully.'
915
		);
916
917
		$this->assertThat(
918
			$form->getInput('colours', 'params', 'blue'),
919
			$this->equalTo(
920
				'<select id="jform_params_colours" name="jform[params][colours][]" multiple="multiple">' .
921
					"\n" . '	<option value="red">Red</option>' .
922
					"\n" . '	<option value="blue" selected="selected">Blue</option>' .
923
					"\n" . '	<option value="green">Green</option>' .
924
					"\n" . '	<option value="yellow">Yellow</option>' .
925
					"\n" . '</select>' .
926
					"\n"
927
			),
928
			'Line:' . __LINE__ . ' XML string should load successfully.'
929
		);
930
931
		// Test translate default
932
		$this->assertThat(
933
			$form->getInput('translate_default'),
934
			$this->equalTo(
935
				'<input type="text" name="jform[translate_default]" id="jform_translate_default" value="DEFAULT_KEY"/>'
936
			),
937
			'Line:' . __LINE__ .
938
			' The method should return a simple input text field whose value is untranslated since the DEFAULT_KEY does not exist in the language.'
939
		);
940
941
		$lang = Language::getInstance();
0 ignored issues
show
Deprecated Code introduced by
The method Joomla\Language\Language::getInstance() has been deprecated with message: 2.0 Use LanguageFactory::getLanguage() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
942
		$debug = $lang->setDebug(true);
943
		$this->assertThat(
944
			$form->getInput('translate_default'),
945
			$this->equalTo(
946
				'<input type="text" name="jform[translate_default]" id="jform_translate_default" value="??DEFAULT_KEY??"/>'
947
			),
948
			'Line:' . __LINE__ . ' The method should return a simple input text field whose value is marked untranslated.'
949
		);
950
951
		$lang->load('form_test', __DIR__);
952
		$this->assertThat(
953
			$form->getInput('translate_default'),
954
			$this->equalTo(
955
				'<input type="text" name="jform[translate_default]" id="jform_translate_default" value="My Default"/>'
956
			),
957
			'Line:' . __LINE__ . ' The method should return a simple input text field whose value is translated.'
958
		);
959
		$lang->setDebug($debug);
960
	}
961
962
	/**
963
	 * Test for Form::getLabel method.
964
	 *
965
	 * @return void
966
	 */
967
	public function testGetLabel()
968
	{
969
		$form = new JFormInspector('form1');
970
971
		$this->assertThat(
972
			$form->load(JFormDataHelper::$loadFieldDocument),
973
			$this->isTrue(),
974
			'Line:' . __LINE__ . ' XML string should load successfully.'
975
		);
976
977
		$expected = '<label id="title_id-lbl" for="title_id" class="hasTip required" ' .
978
				'title="Title::The title.">Title<span class="star">&#160;*</span></label>';
979
980
		$this->assertThat(
981
			$form->getLabel('title'),
982
			$this->equalTo($expected),
983
			'Line:' . __LINE__ . ' The method should return a simple label field.'
984
		);
985
	}
986
987
	/**
988
	 * Test the Form::getName method.
989
	 *
990
	 * @return void
991
	 */
992
	public function testGetName()
993
	{
994
		$form = new Form('form1');
0 ignored issues
show
Deprecated Code introduced by
The class Joomla\Form\Form has been deprecated with message: The joomla/form package is deprecated

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
995
996
		$this->assertThat(
997
			$form->getName(),
998
			$this->equalTo('form1'),
999
			'Line:' . __LINE__ . ' The form name should agree with the argument passed in the constructor.'
1000
		);
1001
	}
1002
1003
	/**
1004
	 * Test for Form::getValue method.
1005
	 *
1006
	 * @return void
1007
	 */
1008
	public function testGetValue()
1009
	{
1010
		$form = new JFormInspector('form1');
1011
1012
		$this->assertThat(
1013
			$form->load(JFormDataHelper::$loadFieldDocument),
1014
			$this->isTrue(),
1015
			'Line:' . __LINE__ . ' XML string should load successfully.'
1016
		);
1017
1018
		$data = array(
1019
			'title' => 'Avatar',
1020
		);
1021
1022
		$this->assertThat(
1023
			$form->bind($data),
1024
			$this->isTrue(),
1025
			'Line:' . __LINE__ . ' The data should bind successfully.'
1026
		);
1027
1028
		$this->assertThat(
1029
			$form->getValue('title'),
1030
			$this->equalTo('Avatar'),
1031
			'Line:' . __LINE__ . ' The bind value should be returned.'
1032
		);
1033
	}
1034
1035
	/**
1036
	 * Test for Form::getFieldset method.
1037
	 *
1038
	 * @return void
1039
	 */
1040 View Code Duplication
	public function testGetFieldset()
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...
1041
	{
1042
		// Prepare the form.
1043
		$form = new JFormInspector('form1');
1044
1045
		$this->assertThat(
1046
			$form->load(JFormDataHelper::$getFieldsetDocument),
1047
			$this->isTrue(),
1048
			'Line:' . __LINE__ . ' XML string should load successfully.'
1049
		);
1050
1051
		$this->assertThat(
1052
			$form->getFieldset('bogus'),
1053
			$this->equalTo(array()),
1054
			'Line:' . __LINE__ . ' A fieldset that does not exist should return an empty array.'
1055
		);
1056
1057
		$this->assertThat(
1058
			count($form->getFieldset('params-basic')),
1059
			$this->equalTo(4),
1060
			'Line:' . __LINE__ . ' There are 3 field elements in a fieldset and 1 field element marked with the fieldset attribute.'
1061
		);
1062
	}
1063
1064
	/**
1065
	 * Test for Form::getFieldsets method.
1066
	 *
1067
	 * @return void
1068
	 */
1069
	public function testGetFieldsets()
1070
	{
1071
		// Prepare the form.
1072
		$form = new JFormInspector('form1');
1073
1074
		$this->assertThat(
1075
			$form->load(JFormDataHelper::$getFieldsetsDocument),
1076
			$this->isTrue(),
1077
			'Line:' . __LINE__ . ' XML string should load successfully.'
1078
		);
1079
1080
		$sets = $form->getFieldsets();
1081
		$this->assertThat(
1082
			count($sets),
1083
			$this->equalTo(3),
1084
			'Line:' . __LINE__ . ' The source data has 3 fieldsets in total.'
1085
		);
1086
1087
		$this->assertThat(
1088
			$sets['params-advanced']->name,
1089
			$this->equalTo('params-advanced'),
1090
			'Line:' . __LINE__ . ' Ensure the fieldset name is correct.'
1091
		);
1092
1093
		$this->assertThat(
1094
			$sets['params-advanced']->label,
1095
			$this->equalTo('Advanced Options'),
1096
			'Line:' . __LINE__ . ' Ensure the fieldset label is correct.'
1097
		);
1098
1099
		$this->assertThat(
1100
			$sets['params-advanced']->description,
1101
			$this->equalTo('The advanced options'),
1102
			'Line:' . __LINE__ . ' Ensure the fieldset description is correct.'
1103
		);
1104
1105
		// Test loading by group.
1106
1107
		$this->assertThat(
1108
			$form->getFieldsets('bogus'),
1109
			$this->equalTo(array()),
1110
			'Line:' . __LINE__ . ' A fieldset that in a group that does not exist should return an empty array.'
1111
		);
1112
1113
		$sets = $form->getFieldsets('details');
1114
		$this->assertThat(
1115
			count($sets),
1116
			$this->equalTo(1),
1117
			'Line:' . __LINE__ . ' The details group has one field marked with a fieldset'
1118
		);
1119
1120
		$this->assertThat(
1121
			$sets['params-legacy']->name,
1122
			$this->equalTo('params-legacy'),
1123
			'Line:' . __LINE__ . ' Ensure the fieldset name is correct.'
1124
		);
1125
	}
1126
1127
	/**
1128
	 * Test the Form::load method.
1129
	 *
1130
	 * This method can load an XML data object, or parse an XML string.
1131
	 *
1132
	 * @return void
1133
	 */
1134
	public function testLoad()
1135
	{
1136
		$form = new JFormInspector('form1');
1137
1138
		$this->assertThat(
1139
			$form->load(JFormDataHelper::$loadDocument),
1140
			$this->isTrue(),
1141
			'Line:' . __LINE__ . ' XML string should load successfully.'
1142
		);
1143
1144
		$this->assertThat(
1145
			($form->getXML() instanceof \SimpleXMLElement),
1146
			$this->isTrue(),
1147
			'Line:' . __LINE__ . ' The internal XML should be a SimpleXMLElement object.'
1148
		);
1149
1150
		// Test replace false.
1151
1152
		$this->assertThat(
1153
			$form->load(JFormDataHelper::$loadMergeDocument, false),
1154
			$this->isTrue(),
1155
			'Line:' . __LINE__ . ' XML string should load successfully.'
1156
		);
1157
1158
		$this->assertThat(
1159
			count($form->getXML()->xpath('/form/fields/field')),
1160
			$this->equalTo(4),
1161
			'Line:' . __LINE__ . ' There are 2 new ungrouped field and one existing field should merge, resulting in 4 total.'
1162
		);
1163
1164
		// Test replace true (default).
1165
1166
		$form = new JFormInspector('form1');
1167
1168
		$this->assertThat(
1169
			$form->load(JFormDataHelper::$loadDocument),
1170
			$this->isTrue(),
1171
			'Line:' . __LINE__ . ' XML string should load successfully.'
1172
		);
1173
1174
		$this->assertThat(
1175
			$form->load(JFormDataHelper::$loadMergeDocument),
1176
			$this->isTrue(),
1177
			'Line:' . __LINE__ . ' XML string should load successfully.'
1178
		);
1179
1180
		// @todo remove: $this->_showXml($form);die;
1181
1182
		$this->assertThat(
1183
			count($form->findFieldsByGroup(false)),
1184
			$this->equalTo(6),
1185
			'Line:' . __LINE__ . ' There are 2 original ungrouped fields, 1 replaced and 4 new, resulting in 6 total.'
1186
		);
1187
1188
		$this->assertThat(
1189
			count($form->getXML()->xpath('//fields[@name]')),
1190
			$this->equalTo(2),
1191
			'Line:' . __LINE__ . ' The XML has 2 fields tags with a name attribute.'
1192
		);
1193
1194
		$this->assertThat(
1195
			count($form->getXML()->xpath('//fields[@name="params"]/field')),
1196
			$this->equalTo(2),
1197
			'Line:' . __LINE__ . ' The params fields have been merged ending with 2 elements.'
1198
		);
1199
1200
		$this->assertThat(
1201
			count($form->getXML()->xpath('/form/fields/fields[@name="params"]/field[@name="show_abstract"]')),
1202
			$this->equalTo(1),
1203
			'Line:' . __LINE__ . ' The show_title in the params group has been replaced by show_abstract.'
1204
		);
1205
1206
		$originalform = new JFormInspector('form1');
1207
		$originalform->load(JFormDataHelper::$loadDocument);
1208
		$originalset = $originalform->getXML()->xpath('/form/fields/field');
1209
		$set = $form->getXML()->xpath('/form/fields/field');
1210
1211
		for ($i = 0; $i < count($originalset); $i++)
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
1212
		{
1213
			$this->assertThat(
1214
				(string) ($originalset[$i]->attributes()->name) == (string) ($set[$i]->attributes()->name),
1215
				$this->isTrue(),
1216
				'Line:' . __LINE__ . ' Replace should leave fields in the original order.'
1217
			);
1218
		}
1219
	}
1220
1221
	/**
1222
	 * Test the Form::load method for cases of unexpected or bad input.
1223
	 *
1224
	 * This method can load an XML data object, or parse an XML string.
1225
	 *
1226
	 * @return void
1227
	 */
1228
	public function testLoad_BadInput()
1229
	{
1230
		$form = new JFormInspector('form1');
1231
1232
		$this->assertThat(
1233
			$form->load(123),
1234
			$this->isFalse(),
1235
			'Line:' . __LINE__ . ' A non-string should return false.'
1236
		);
1237
1238
		$this->assertThat(
1239
			$form->load('junk'),
1240
			$this->isFalse(),
1241
			'Line:' . __LINE__ . ' An invalid string should return false.'
1242
		);
1243
1244
		$this->assertThat(
1245
			$form->getXml(),
1246
			$this->isNull(),
1247
			'Line:' . __LINE__ . ' The internal XML should be false as returned from simplexml_load_string.'
1248
		);
1249
1250
		$this->assertThat(
1251
			$form->load('<notform><test /></notform>'),
1252
			$this->isTrue(),
1253
			'Line:' . __LINE__ . ' Invalid root node name from string should still load.'
1254
		);
1255
1256
		$this->assertThat(
1257
			$form->getXml()->getName(),
1258
			$this->equalTo('form'),
1259
			'Line:' . __LINE__ . ' The internal XML should still be named "form".'
1260
		);
1261
1262
		// Test for irregular object input.
1263
1264
		$form = new JFormInspector('form1');
1265
1266
		$this->assertThat(
1267
			$form->load(simplexml_load_string('<notform><test /></notform>')),
1268
			$this->isTrue(),
1269
			'Line:' . __LINE__ . ' Invalid root node name from XML object should still load.'
1270
		);
1271
1272
		$this->assertThat(
1273
			$form->getXml()->getName(),
1274
			$this->equalTo('form'),
1275
			'Line:' . __LINE__ . ' The internal XML should still be named "form".'
1276
		);
1277
	}
1278
1279
	/**
1280
	 * Test the Form::load method for XPath data.
1281
	 *
1282
	 * This method can load an XML data object, or parse an XML string.
1283
	 *
1284
	 * @return void
1285
	 */
1286
	public function testLoad_XPath()
1287
	{
1288
		$form = new JFormInspector('form1');
1289
1290
		$this->assertThat(
1291
			$form->load(JFormDataHelper::$loadXPathDocument, true, '/extension/fields'),
1292
			$this->isTrue(),
1293
			'Line:' . __LINE__ . ' XML string should load successfully.'
1294
		);
1295
1296
		$this->assertThat(
1297
			$form->getXml()->getName(),
1298
			$this->equalTo('form'),
1299
			'Line:' . __LINE__ . ' The internal XML should still be named "form".'
1300
		);
1301
1302
		// @todo remove: $this->_showXml($form);die;
1303
		$this->assertThat(
1304
			count($form->getXml()->fields->fields),
1305
			$this->equalTo(2),
1306
			'Line:' . __LINE__ . ' The test data has 2 fields.'
1307
		);
1308
	}
1309
1310
	/**
1311
	 * Test for Form::loadField method.
1312
	 *
1313
	 * @return void
1314
	 */
1315
	public function testLoadField()
1316
	{
1317
		$form = new JFormInspector('form1');
1318
1319
		$this->assertThat(
1320
			$form->load(JFormDataHelper::$loadFieldDocument),
1321
			$this->isTrue(),
1322
			'Line:' . __LINE__ . ' XML string should load successfully.'
1323
		);
1324
1325
		// Error handling.
1326
1327
		$this->assertThat(
1328
			$form->loadField('bogus'),
1329
			$this->isFalse(),
1330
			'Line:' . __LINE__ . ' An unknown field should return false.'
1331
		);
1332
1333
		// Test correct usage.
1334
1335
		$field = $form->getField('title');
1336
		$field = $form->loadField($field);
0 ignored issues
show
Unused Code introduced by
$field is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1337
	}
1338
1339
	/**
1340
	 * Test the Form::loadFile method.
1341
	 *
1342
	 * This method loads a file and passes the string to the Form::load method.
1343
	 *
1344
	 * @return void
1345
	 */
1346
	public function testLoadFile()
1347
	{
1348
		$form = new JFormInspector('form1');
1349
1350
		// Test for files that don't exist.
1351
1352
		$this->assertThat(
1353
			$form->loadFile('/tmp/example.xml'),
1354
			$this->isFalse(),
1355
			'Line:' . __LINE__ . ' A file path that does not exist should return false.'
1356
		);
1357
1358
		$this->assertThat(
1359
			$form->loadFile('notfound'),
1360
			$this->isFalse(),
1361
			'Line:' . __LINE__ . ' A file name that does not exist should return false.'
1362
		);
1363
1364
		// Testing loading a file by full path.
1365
1366
		$this->assertThat(
1367
			$form->loadFile(__DIR__ . '/example.xml'),
1368
			$this->isTrue(),
1369
			'Line:' . __LINE__ . ' XML file by full path should load successfully.'
1370
		);
1371
1372
		$this->assertThat(
1373
			($form->getXML() instanceof \SimpleXMLElement),
1374
			$this->isTrue(),
1375
			'Line:' . __LINE__ . ' XML string should parse successfully.'
1376
		);
1377
1378
		// Testing loading a file by file name.
1379
1380
		$form = new JFormInspector('form1');
1381
		FormHelper::addFormPath(__DIR__);
1382
1383
		$this->assertThat(
1384
			$form->loadFile('example'),
1385
			$this->isTrue(),
1386
			'Line:' . __LINE__ . ' XML file by name should load successfully.'
1387
		);
1388
1389
		$this->assertThat(
1390
			($form->getXML() instanceof \SimpleXMLElement),
1391
			$this->isTrue(),
1392
			'Line:' . __LINE__ . ' XML string should parse successfully.'
1393
		);
1394
	}
1395
1396
	/**
1397
	 * Test the Form::mergeNode method.
1398
	 *
1399
	 * @return void
1400
	 */
1401 View Code Duplication
	public function testMergeNode()
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...
1402
	{
1403
		// The source data.
1404
		$xml1 = simplexml_load_string('<form><field name="foo" /></form>');
1405
1406
		// The new data for adding the field.
1407
		$xml2 = simplexml_load_string('<form><field name="bar" type="text" /></form>');
1408
1409
		if ($xml1 === false || $xml2 === false)
1410
		{
1411
			$this->fail('Line:' . __LINE__ . ' Error in text XML data');
1412
		}
1413
1414
		JFormInspector::mergeNode($xml1->field, $xml2->field);
1415
1416
		$fields = $xml1->xpath('field[@name="foo"] | field[@type="text"]');
1417
		$this->assertThat(
1418
			count($fields),
1419
			$this->equalTo(1),
1420
			'Line:' . __LINE__ . ' Existing attribute "name" should merge, new attribute "type" added.'
1421
		);
1422
	}
1423
1424
	/**
1425
	 * Test the Form::mergeNode method.
1426
	 *
1427
	 * @return void
1428
	 */
1429
	public function testMergeNodes()
1430
	{
1431
		// The source data.
1432
		$xml1 = simplexml_load_string('<form><fields><field name="foo" /></fields></form>');
1433
1434
		// The new data for adding the field.
1435
		$xml2 = simplexml_load_string('<form><fields><field name="foo" type="text" /><field name="soap" /></fields></form>');
1436
1437
		if ($xml1 === false || $xml2 === false)
1438
		{
1439
			$this->fail('Line:' . __LINE__ . ' Error in text XML data');
1440
		}
1441
1442
		JFormInspector::mergeNodes($xml1->fields, $xml2->fields);
1443
1444
		$this->assertThat(
1445
			count($xml1->xpath('fields/field')),
1446
			$this->equalTo(2),
1447
			'Line:' . __LINE__ . ' The merge should have two field tags, one existing, one new.'
1448
		);
1449
1450
		$this->assertThat(
1451
			count($xml1->xpath('fields/field[@name="foo"] | fields/field[@type="text"]')),
1452
			$this->equalTo(1),
1453
			'Line:' . __LINE__ . ' A field of the same name should merge.'
1454
		);
1455
1456
		$this->assertThat(
1457
			count($xml1->xpath('fields/field[@name="soap"]')),
1458
			$this->equalTo(1),
1459
			'Line:' . __LINE__ . ' A new field should be added.'
1460
		);
1461
	}
1462
1463
	/**
1464
	 * Test for Form::removeField method.
1465
	 *
1466
	 * @return void
1467
	 */
1468
	public function testRemoveField()
1469
	{
1470
		$form = new JFormInspector('form1');
1471
1472
		$this->assertThat(
1473
			$form->load(JFormDataHelper::$loadDocument),
1474
			$this->isTrue(),
1475
			'Line:' . __LINE__ . ' XML string should load successfully.'
1476
		);
1477
1478
		$this->assertThat(
1479
			$form->removeField('title'),
1480
			$this->isTrue(),
1481
			'Line:' . __LINE__ . ' The removeField method should return true.'
1482
		);
1483
1484
		$this->assertThat(
1485
			$form->findField('title'),
1486
			$this->isFalse(),
1487
			'Line:' . __LINE__ . ' The field should be removed.'
1488
		);
1489
1490
		$this->assertThat(
1491
			$form->removeField('show_title', 'params'),
1492
			$this->isTrue(),
1493
			'Line:' . __LINE__ . ' The removeField method should return true.'
1494
		);
1495
1496
		$this->assertThat(
1497
			$form->findField('show_title', 'params'),
1498
			$this->isFalse(),
1499
			'Line:' . __LINE__ . ' The field should be removed.'
1500
		);
1501
	}
1502
1503
	/**
1504
	 * Test for Form::removeGroup method.
1505
	 *
1506
	 * @return void
1507
	 */
1508 View Code Duplication
	public function testRemoveGroup()
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...
1509
	{
1510
		$form = new JFormInspector('form1');
1511
1512
		$this->assertThat(
1513
			$form->load(JFormDataHelper::$loadDocument),
1514
			$this->isTrue(),
1515
			'Line:' . __LINE__ . ' XML string should load successfully.'
1516
		);
1517
1518
		$this->assertThat(
1519
			$form->removeGroup('params'),
1520
			$this->isTrue(),
1521
			'Line:' . __LINE__ . ' The removeGroup method should return true.'
1522
		);
1523
1524
		$this->assertThat(
1525
			$form->findGroup('params'),
1526
			$this->equalTo(array()),
1527
			'Line:' . __LINE__ . ' The group should be removed, returning an empty array.'
1528
		);
1529
	}
1530
1531
	/**
1532
	 * Test for Form::setField method.
1533
	 *
1534
	 * @return void
1535
	 */
1536
	public function testReset()
1537
	{
1538
		$form = new JFormInspector('form1');
1539
1540
		$this->assertThat(
1541
			$form->load(JFormDataHelper::$loadDocument),
1542
			$this->isTrue(),
1543
			'Line:' . __LINE__ . ' XML string should load successfully.'
1544
		);
1545
1546
		$data = array(
1547
			'title' => 'Joomla Framework',
1548
			'params' => array(
1549
				'show_title' => 2
1550
			)
1551
		);
1552
1553
		$this->assertThat(
1554
			$form->bind($data),
1555
			$this->isTrue(),
1556
			'Line:' . __LINE__ . ' The data should bind successfully.'
1557
		);
1558
1559
		$this->assertThat(
1560
			$form->getValue('title'),
1561
			$this->equalTo('Joomla Framework'),
1562
			'Line:' . __LINE__ . ' Confirm the field value is set.'
1563
		);
1564
1565
		$this->assertThat(
1566
			$form->getValue('show_title', 'params'),
1567
			$this->equalTo(2),
1568
			'Line:' . __LINE__ . ' Confirm the field value is set.'
1569
		);
1570
1571
		// Test reset on the data only.
1572
1573
		$this->assertThat(
1574
			$form->reset(),
1575
			$this->isTrue(),
1576
			'Line:' . __LINE__ . ' The reset method should return true.'
1577
		);
1578
1579
		$this->assertThat(
1580
			$form->getField('title'),
1581
			$this->logicalNot($this->isFalse()),
1582
			'Line:' . __LINE__ . ' The field should still exist.'
1583
		);
1584
1585
		$this->assertThat(
1586
			$form->getValue('title'),
1587
			$this->equalTo(null),
1588
			'Line:' . __LINE__ . ' The field value should be reset.'
1589
		);
1590
1591
		$this->assertThat(
1592
			$form->getValue('show_title', 'params'),
1593
			$this->equalTo(null),
1594
			'Line:' . __LINE__ . ' The field value should be reset.'
1595
		);
1596
1597
		// Test reset of data and the internal XML.
1598
1599
		$this->assertThat(
1600
			$form->reset(true),
1601
			$this->isTrue(),
1602
			'Line:' . __LINE__ . ' The reset method should return true.'
1603
		);
1604
1605
		$this->assertThat(
1606
			$form->getField('title'),
1607
			$this->isFalse(),
1608
			'Line:' . __LINE__ . ' The known field should be removed.'
1609
		);
1610
1611
		$this->assertThat(
1612
			$form->findGroup('params'),
1613
			$this->equalTo(array()),
1614
			'Line:' . __LINE__ . ' The known group should be removed, returning an empty array.'
1615
		);
1616
	}
1617
1618
	/**
1619
	 * Test for Form::setField method.
1620
	 *
1621
	 * @return void
1622
	 */
1623
	public function testSetField()
1624
	{
1625
		$form = new JFormInspector('form1');
1626
1627
		$this->assertThat(
1628
			$form->load(JFormDataHelper::$loadDocument),
1629
			$this->isTrue(),
1630
			'Line:' . __LINE__ . ' XML string should load successfully.'
1631
		);
1632
1633
		$xml1 = simplexml_load_string('<form><field name="title" required="true" /></form>');
1634
1635
		if ($xml1 === false)
1636
		{
1637
			$this->fail('Error in text XML data');
1638
		}
1639
1640
		// Test without replace.
1641
1642
		$this->assertThat(
1643
			$form->setField($xml1->field[0], null, false),
1644
			$this->isTrue(),
1645
			'Line:' . __LINE__ . ' The setField method should return true.'
1646
		);
1647
1648
		$this->assertThat(
1649
			$form->getFieldAttribute('title', 'required', 'default'),
1650
			$this->equalTo('default'),
1651
			'Line:' . __LINE__ . ' The label should contain just the field name.'
1652
		);
1653
1654
		// Test with replace.
1655
1656
		$this->assertThat(
1657
			$form->setField($xml1->field[0], null, true),
1658
			$this->isTrue(),
1659
			'Line:' . __LINE__ . ' The setField method should return true.'
1660
		);
1661
1662
		$this->assertThat(
1663
			$form->getFieldAttribute('title', 'required', 'default'),
1664
			$this->equalTo('true'),
1665
			'Line:' . __LINE__ . ' The label should contain just the new label.'
1666
		);
1667
	}
1668
1669
	/**
1670
	 * Test for Form::setFieldAttribute method.
1671
	 *
1672
	 * @return void
1673
	 */
1674 View Code Duplication
	public function testSetFieldAttribute()
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...
1675
	{
1676
		$form = new JFormInspector('form1');
1677
1678
		$this->assertThat(
1679
			$form->load(JFormDataHelper::$loadDocument),
1680
			$this->isTrue(),
1681
			'Line:' . __LINE__ . ' XML string should load successfully.'
1682
		);
1683
1684
		$this->assertThat(
1685
			$form->setFieldAttribute('title', 'label', 'The Title'),
1686
			$this->isTrue(),
1687
			'Line:' . __LINE__ . ' The method should return true.'
1688
		);
1689
1690
		$this->assertThat(
1691
			$form->getFieldAttribute('title', 'label'),
1692
			$this->equalTo('The Title'),
1693
			'Line:' . __LINE__ . ' The new value should be set.'
1694
		);
1695
1696
		$this->assertThat(
1697
			$form->setFieldAttribute('show_title', 'label', 'Show Title', 'params'),
1698
			$this->isTrue(),
1699
			'Line:' . __LINE__ . ' The method should return true.'
1700
		);
1701
1702
		$this->assertThat(
1703
			$form->getFieldAttribute('show_title', 'label', 'default', 'params'),
1704
			$this->equalTo('Show Title'),
1705
			'Line:' . __LINE__ . ' The new value of the grouped field should be set.'
1706
		);
1707
	}
1708
1709
	/**
1710
	 * Test for Form::setFields method.
1711
	 *
1712
	 * @return void
1713
	 */
1714
	public function testSetFields()
1715
	{
1716
		$form = new JFormInspector('form1');
1717
1718
		$this->assertThat(
1719
			$form->load(JFormDataHelper::$loadDocument),
1720
			$this->isTrue(),
1721
			'Line:' . __LINE__ . ' XML string should load successfully.'
1722
		);
1723
1724
		$xml1 = simplexml_load_string('<form><field name="title" required="true" /><field name="ordering" /></form>');
1725
1726
		if ($xml1 === false)
1727
		{
1728
			$this->fail('Error in text XML data');
1729
		}
1730
1731
		// Test without replace.
1732
1733
		$this->assertThat(
1734
			$form->setFields($xml1->field, null, false),
1735
			$this->isTrue(),
1736
			'Line:' . __LINE__ . ' The setFields method should return true.'
1737
		);
1738
1739
		$this->assertThat(
1740
			$form->getFieldAttribute('title', 'required', 'default'),
1741
			$this->equalTo('default'),
1742
			'Line:' . __LINE__ . ' The label should contain just the field name.'
1743
		);
1744
1745
		$this->assertThat(
1746
			$form->getField('ordering'),
1747
			$this->logicalNot($this->isFalse()),
1748
			'Line:' . __LINE__ . ' The label should contain just the field name.'
1749
		);
1750
	}
1751
1752
	/**
1753
	 * Test for Form::setValue method.
1754
	 *
1755
	 * @return void
1756
	 */
1757
	public function testSetValue()
1758
	{
1759
		$form = new JFormInspector('form1');
1760
1761
		$this->assertThat(
1762
			$form->load(JFormDataHelper::$loadDocument),
1763
			$this->isTrue(),
1764
			'Line:' . __LINE__ . ' XML string should load successfully.'
1765
		);
1766
1767
		// Test error handling.
1768
1769
		$this->assertThat(
1770
			$form->setValue('bogus', null, 'Unknown'),
1771
			$this->isFalse(),
1772
			'Line:' . __LINE__ . ' An unknown field cannot have its value set.'
1773
		);
1774
1775
		// Test regular usage.
1776
1777
		$this->assertThat(
1778
			$form->setValue('title', null, 'The Title'),
1779
			$this->isTrue(),
1780
			'Line:' . __LINE__ . ' Should return true for a known field.'
1781
		);
1782
1783
		$this->assertThat(
1784
			$form->getValue('title', null, 'default'),
1785
			$this->equalTo('The Title'),
1786
			'Line:' . __LINE__ . ' The new value should return.'
1787
		);
1788
1789
		$this->assertThat(
1790
			$form->setValue('show_title', 'params', '3'),
1791
			$this->isTrue(),
1792
			'Line:' . __LINE__ . ' Should return true for a known field.'
1793
		);
1794
1795
		$this->assertThat(
1796
			$form->getValue('show_title', 'params', 'default'),
1797
			$this->equalTo('3'),
1798
			'Line:' . __LINE__ . ' The new value should return.'
1799
		);
1800
	}
1801
1802
	/**
1803
	 * Test for Form::syncPaths method.
1804
	 *
1805
	 * @return void
1806
	 */
1807
	public function testSyncPaths()
1808
	{
1809
		$form = new JFormInspector('testSyncPaths');
1810
1811
		$this->assertThat(
1812
			$form->load(JFormDataHelper::$syncPathsDocument),
1813
			$this->isTrue(),
1814
			'Line:' . __LINE__ . ' XML string should load successfully.'
1815
		);
1816
1817
		$fieldPaths = FormHelper::addFieldPath();
0 ignored issues
show
Deprecated Code introduced by
The method Joomla\Form\FormHelper::addFieldPath() has been deprecated with message: 2.0 Field objects should be autoloaded

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1818
		$formPaths = FormHelper::addFormPath();
1819
		$rulePaths = FormHelper::addRulePath();
0 ignored issues
show
Deprecated Code introduced by
The method Joomla\Form\FormHelper::addRulePath() has been deprecated with message: 2.0 Rule objects should be autoloaded

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1820
1821
		$this->assertThat(
1822
			in_array(JPATH_ROOT . '/field1', $fieldPaths),
1823
			$this->isTrue(),
1824
			'Line:' . __LINE__ . ' The field path from the XML file should be present.'
1825
		);
1826
1827
		$this->assertThat(
1828
			in_array(JPATH_ROOT . '/field2', $fieldPaths),
1829
			$this->isTrue(),
1830
			'Line:' . __LINE__ . ' The field path from the XML file should be present.'
1831
		);
1832
1833
		$this->assertThat(
1834
			in_array(JPATH_ROOT . '/field3', $fieldPaths),
1835
			$this->isTrue(),
1836
			'Line:' . __LINE__ . ' The field path from the XML file should be present.'
1837
		);
1838
1839
		$this->assertThat(
1840
			in_array(JPATH_ROOT . '/form1', $formPaths),
1841
			$this->isTrue(),
1842
			'Line:' . __LINE__ . ' The form path from the XML file should be present.'
1843
		);
1844
1845
		$this->assertThat(
1846
			in_array(JPATH_ROOT . '/form2', $formPaths),
1847
			$this->isTrue(),
1848
			'Line:' . __LINE__ . ' The form path from the XML file should be present.'
1849
		);
1850
1851
		$this->assertThat(
1852
			in_array(JPATH_ROOT . '/form3', $formPaths),
1853
			$this->isTrue(),
1854
			'Line:' . __LINE__ . ' The form path from the XML file should be present.'
1855
		);
1856
1857
		$this->assertThat(
1858
			in_array(JPATH_ROOT . '/rule1', $rulePaths),
1859
			$this->isTrue(),
1860
			'Line:' . __LINE__ . ' The rule path from the XML file should be present.'
1861
		);
1862
1863
		$this->assertThat(
1864
			in_array(JPATH_ROOT . '/rule2', $rulePaths),
1865
			$this->isTrue(),
1866
			'Line:' . __LINE__ . ' The rule path from the XML file should be present.'
1867
		);
1868
1869
		$this->assertThat(
1870
			in_array(JPATH_ROOT . '/rule3', $rulePaths),
1871
			$this->isTrue(),
1872
			'Line:' . __LINE__ . ' The rule path from the XML file should be present.'
1873
		);
1874
	}
1875
1876
	/**
1877
	 * Test for Form::validate method.
1878
	 *
1879
	 * @return void
1880
	 */
1881
	public function testValidate()
1882
	{
1883
		$form = new JFormInspector('form1');
1884
1885
		$this->assertThat(
1886
			$form->load(JFormDataHelper::$validateDocument),
1887
			$this->isTrue(),
1888
			'Line:' . __LINE__ . ' XML string should load successfully.'
1889
		);
1890
1891
		$pass = array(
1892
			'boolean' => 'false',
1893
			'optional' => 'Optional',
1894
			'required' => 'Supplied',
1895
			'group' => array(
1896
				'level1' => 'open'
1897
			)
1898
		);
1899
1900
		$fail = array(
1901
			'boolean' => 'comply',
1902
			'required' => '',
1903
		);
1904
1905
		// Test error conditions.
1906
1907
		$this->assertThat(
1908
			$form->validate($pass, 'bogus'),
1909
			$this->isFalse(),
1910
			'Line:' . __LINE__ . ' Validating an unknown group should return false.'
1911
		);
1912
1913
		$this->assertThat(
1914
			$form->validate($fail),
1915
			$this->isFalse(),
1916
			'Line:' . __LINE__ . ' Any validation failures should return false.'
1917
		);
1918
1919
		// Test expected behaviour.
1920
1921
		$this->assertThat(
1922
			$form->validate($pass),
1923
			$this->isTrue(),
1924
			'Line:' . __LINE__ . ' Validation on this data should pass.'
1925
		);
1926
1927
		$this->assertThat(
1928
			$form->validate($pass, 'group'),
1929
			$this->isTrue(),
1930
			'Line:' . __LINE__ . ' Validating an unknown group should return false.'
1931
		);
1932
	}
1933
1934
	/**
1935
	 * Test for Form::validateField method.
1936
	 *
1937
	 * return   void
1938
	 *
1939
	 * @covers  Joomla\Form\Form::validateField
1940
	 * @since   1.0
1941
	 *
1942
	 * @return void
1943
	 */
1944
	public function testValidateField()
1945
	{
1946
		$form = new JFormInspector('form1');
1947
1948
		$this->assertThat(
1949
			$form->load(JFormDataHelper::$validateFieldDocument),
1950
			$this->isTrue(),
1951
			'Line:' . __LINE__ . ' XML string should load successfully.'
1952
		);
1953
1954
		$xml = $form->getXML();
1955
1956
		// Test error handling.
1957
1958
		$element = $xml->xpath('fields/field[@name="boolean"]');
1959
		$field = array_pop($element);
1960
		$result = $form->validateField($field);
1961
		$this->assertThat(
1962
			$result instanceof \UnexpectedValueException,
1963
			$this->isTrue(),
1964
			'Line:' . __LINE__ . ' A failed validation should return an exception.'
1965
		);
1966
1967
		$element = $xml->xpath('fields/field[@name="required"]');
1968
		$field = array_pop($element);
1969
		$result = $form->validateField($field);
1970
		$this->assertThat(
1971
			$result instanceof \RuntimeException,
1972
			$this->isTrue(),
1973
			'Line:' . __LINE__ . ' A required field missing a value should return an exception.'
1974
		);
1975
1976
		// Test general usage.
1977
1978
		$element = $xml->xpath('fields/field[@name="boolean"]');
1979
		$field = array_pop($element);
1980
		$this->assertThat(
1981
			$form->validateField($field, null, 'true'),
1982
			$this->isTrue(),
1983
			'Line:' . __LINE__ . ' A field with a passing validate attribute set should return true.'
1984
		);
1985
1986
		$element = $xml->xpath('fields/field[@name="optional"]');
1987
		$field = array_pop($element);
1988
		$this->assertThat(
1989
			$form->validateField($field),
1990
			$this->isTrue(),
1991
			'Line:' . __LINE__ . ' A field without required set should return true.'
1992
		);
1993
1994
		$element = $xml->xpath('fields/field[@name="required"]');
1995
		$field = array_pop($element);
1996
		$this->assertThat(
1997
			$form->validateField($field, null, 'value'),
1998
			$this->isTrue(),
1999
			'Line:' . __LINE__ . ' A required field with a value should return true.'
2000
		);
2001
	}
2002
2003
	/**
2004
	 * Test for Form::validateField method for missing rule exception.
2005
	 *
2006
	 * return   void
2007
	 *
2008
	 * @covers  Joomla\Form\Form::validateField
2009
	 * @since   1.0
2010
	 *
2011
	 * @expectedException  \UnexpectedValueException
2012
	 *
2013
	 * @return void
2014
	 */
2015
	public function testValidateField_missingRule()
2016
	{
2017
		$form = new JFormInspector('form1');
2018
		$form->load(JFormDataHelper::$validateFieldDocument);
2019
		$xml = $form->getXML();
2020
		$element = $xml->xpath('fields/field[@name="missingrule"]');
2021
		$field = array_pop($element);
2022
		$result = $form->validateField($field, null, 'value');
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2023
	}
2024
}
2025