-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules_forms.test
791 lines (700 loc) · 28.5 KB
/
rules_forms.test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
<?php
/**
* @file
*/
/**
*
*/
class RulesFormsInterfaceTestCase extends DrupalWebTestCase {
/**
* Provides a user with permissions to administer rules forms.
*/
protected $privileged_user;
/**
* Defines test information.
*/
public static function getInfo() {
return array(
'name' => 'Form activation and interface',
'description' => 'Tests the administrative interface and form event activation process.',
'group' => 'Rules Forms',
);
}
/**
* Sets up the test.
*/
public function setUp() {
parent::setUp('rules_forms', 'rules');
$this->privileged_user = $this->drupalCreateUser(array(
'administer nodes',
'access content',
'create article content',
'administer rules forms rules',
'administer rules forms',
));
$this->drupalLogin($this->privileged_user);
}
/**
* Tests the administrative interface.
*/
public function testFormActivation() {
$this->drupalGet(RULES_FORMS_ADMIN_PATH);
$this->assertNoText(t('Forms where events are activated'), t('No forms are activated yet.'));
$this->assertNoText(t('Form elements'));
// Activate form event messages.
$edit = array();
$edit['enable_form_activation_message'] = 1;
$this->drupalPost(RULES_FORMS_ADMIN_PATH, $edit, t('Save settings'));
$this->assertText(t('The settings have been saved.'));
$this->drupalGet(RULES_FORMS_ADMIN_PATH);
$this->assertFieldChecked('edit-enable-form-activation-message');
$this->drupalGet('node/add/article');
$this->assertLink('article_node_form', 0, t('Article node form activation link is displayed.'));
// Activate the form.
$this->clickLink('article_node_form');
$this->assertText(t('Custom form label'));
$edit = array();
$edit['form_id_label'] = 'Article node form';
$this->drupalPost('admin/config/workflow/rules/forms/article_node_form/activate/node%25252Fadd%25252Farticle', $edit, t('Activate'));
$this->assertText('article_node_form ' . t('has been activated.'));
// Ensure that the newly activated form is shown in administration.
$this->drupalGet(RULES_FORMS_ADMIN_PATH);
$this->assertText(t('Forms where events are activated'), t('Forms have been activated.'));
$this->assertFieldByName('form_events[article_node_form]');
$this->assertFieldByName('reset_form');
$this->drupalGet('node/add/article');
$form_events = variable_get('rules_forms_event_info', array());
$this->assertTrue(!empty($form_events['article_node_form']), t('Article node for was activated.'));
$this->assertTrue(!empty($form_events['article_node_form']['path']), t('Article node form path was stored.'));
$this->assertTrue(!empty($form_events['article_node_form']['elements']), t('Article node form elements were stored.'));
// Deactivate events for the form.
$edit = array();
$edit['form_events[article_node_form]'] = 1;
$this->drupalPost(RULES_FORMS_ADMIN_PATH, $edit, t('Deactivate events'));
$this->drupalGet(RULES_FORMS_ADMIN_PATH);
$this->assertNoFieldByName('form_events[article_node_form]');
$this->assertNoFieldByName('reset_form');
// Deactivate form event messages.
$edit = array();
$edit['enable_form_activation_message'] = FALSE;
$this->drupalPost(RULES_FORMS_ADMIN_PATH, $edit, t('Save settings'));
$this->drupalGet(RULES_FORMS_ADMIN_PATH);
$this->assertNoFieldChecked('edit-enable-form-activation-message');
}
}
/**
* Base class for testing Rules Forms rules.
*/
class RulesFormsRulesTestCase extends DrupalWebTestCase {
/**
* Provides a user with permissions to administer rules forms.
*/
protected $privileged_user;
/**
* A test form for passing as an argument in events.
*/
protected static $_form = array(
'form_id' => array(
'#type' => 'value',
'#value' => 'test_form',
),
'title' => array(
'#type' => 'textfield',
'#title' => 'test title',
'#default_value' => 'tset',
),
'body' => array(
LANGUAGE_NONE => array(array(
'value' => array(
'#type' => 'textarea',
'#title' => 'test body',
'#default_value' => 'tset',
),
),
),
),
'fieldset' => array(
'#type' => 'fieldset',
'#title' => 'test fieldset',
'select' => array(
'#type' => 'select',
'#title' => 'tset select',
'#options' => array('first' => 'option1', 'second' => 'option2'),
'#value' => 'tset',
'#default_value' => 'test',
),
'checkboxes' => array(
'#type' => 'checkboxes',
'#title' => 'test checkboxes',
'#options' => array('frist' => '1option', 'sceond' => '2option'),
'#value' => 'tset',
'#default_value' => 'test',
),
),
);
/**
* A test form state array for passing as an argument in events.
*/
protected static $_form_state = array(
'values' => array(
'title' => 'tset',
'body' => 'tset',
),
);
/**
* Defines a form ID for a form whose events are activated in tests.
*/
protected static $_form_id = 'test_form';
/**
* Defines testable settings for the rules_forms_form_info variable.
*/
protected static $_form_info = array(
'test_form' => array(
'label' => 'Test form',
'elements' => array(
'textfield:title' => array('type' => 'textfield', 'label' => 'Title'),
'textarea:body:und:0:value' => array('type' => 'textarea', 'label' => 'Body'),
'select:fieldset:select' => array('type' => 'select', 'label' => 'Select'),
'checkboxes:fieldset:checkboxes' => array('type' => 'checkboxes', 'label' => 'Checkboxes'),
),
'path' => 'node/add/article',
'buttons' => FALSE,
'validate' => array(),
'submit' => array(),
'reset' => FALSE,
),
);
/**
* Sets up the test.
*/
public function setUp() {
parent::setUp('rules_forms', 'rules');
$this->privileged_user = $this->drupalCreateUser(array(
'administer nodes',
'access content',
'create article content',
'administer rules forms rules',
'administer rules forms',
'administer site configuration',
));
$this->drupalLogin($this->privileged_user);
}
/**
* Enables form events for the node add form via the interface.
*/
protected function _enableNodeForm() {
$this->drupalGet(RULES_FORMS_ADMIN_PATH);
$edit = array();
$edit['enable_form_activation_message'] = 1;
$this->drupalPost(RULES_FORMS_ADMIN_PATH, $edit, t('Save settings'));
$this->drupalGet('node/add/article');
$this->clickLink('article_node_form');
$edit = array();
$edit['form_id_label'] = 'Article node form';
$edit['button_validate_submit'] = 1;
$this->drupalPost('admin/config/workflow/rules/forms/article_node_form/activate/node%25252Fadd%25252Farticle', $edit, t('Activate'));
}
}
/**
* Tests Rules Forms events.
*/
class RulesFormsEventsTestCase extends RulesFormsRulesTestCase {
/**
* Defines test information.
*/
public static function getInfo() {
return array(
'name' => 'Rules Forms events',
'description' => 'Tests Rules Forms events.',
'group' => 'Rules Forms',
);
}
/**
* Enables modules and logs in the privileged user.
*/
public function setUp() {
parent::setUp();
$this->_enableNodeForm();
}
/**
* Tests the form built event.
*/
public function testFormBuilt() {
$rule = rules_reaction_rule();
$rule->event('rules_forms_article_node_form_form_built')
->action('drupal_message', array('message' => 'Form built successful!'));
$this->drupalGet('node/add/article');
$this->assertNoText('Form built successful!');
$rule->save('test');
$this->drupalGet('node/add/article');
$this->assertText('Form built successful!');
}
/**
* Tests the form validate event.
*/
public function testFormValidate() {
// We use the site information page to perform form validation since the node
// edit form uses button level validation.
$edit = array();
$edit['form_id_label'] = 'Article node form';
$edit['button_validate_submit'] = 1;
$this->drupalPost('admin/config/workflow/rules/forms/system_site_information_settings/activate/admin%25252Fconfig%25252Fsystem%25252Fsite-information', $edit, t('Activate'));
$rule = rules_reaction_rule();
$rule->event('rules_forms_system_site_information_settings_form_validate')
->action('drupal_message', array('message' => 'Form validate successful!'));
$edit = array();
$edit['site_name'] = 'test';
$edit['site_mail'] = 'test@test.com';
$this->drupalPost('admin/config/system/site-information', $edit, t('Save configuration'));
$this->assertNoText('Form validate successful!');
$rule->save('test');
$this->drupalPost('admin/config/system/site-information', $edit, t('Save configuration'));
$this->assertText('Form validate successful!');
}
/**
* Tests the form submit event.
*/
public function testFormSubmit() {
$rule = rules_reaction_rule();
$rule->event('rules_forms_article_node_form_form_submit')
->action('drupal_message', array('message' => 'Form submit successful!'));
$edit = array();
$edit['title'] = 'test';
$this->drupalPost('node/add/article', $edit, t('Save'));
$this->assertNoText('Form submit successful!');
$rule->save('test');
$this->drupalPost('node/add/article', $edit, t('Save'));
$this->assertText('Form submit successful!');
}
/**
* Tests the button validate event.
*/
public function testButtonValidate() {
$rule = rules_reaction_rule();
$rule->event('rules_forms_article_node_form_button_submit_actions_preview_validate')
->action('drupal_message', array('message' => 'Button level validate successful!'));
$edit = array();
$edit['title'] = 'test';
$this->drupalPost('node/add/article', $edit, t('Preview'));
$this->assertNoText('Button level validate successful!');
$rule->save('test');
$this->drupalPost('node/add/article', $edit, t('Preview'));
$this->assertText('Button level validate successful!');
}
/**
* Tests the button submit event.
*/
public function testButtonSubmit() {
$rule = rules_reaction_rule();
$rule->event('rules_forms_article_node_form_button_submit_actions_submit_submit')
->action('drupal_message', array('message' => 'Button level submit successful!'));
$edit = array();
$edit['title'] = 'test';
$this->drupalPost('node/add/article', $edit, t('Save'));
$this->assertNoText('Button level submit successful!');
$rule->save('test');
$this->drupalPost('node/add/article', $edit, t('Save'));
$this->assertText('Button level submit successful!');
}
}
/**
* Tests Rules Forms conditions.
*/
class RulesFormsConditionsTestCase extends RulesFormsRulesTestCase {
/**
* Defines test information.
*/
public static function getInfo() {
return array(
'name' => 'Rules Forms conditions',
'description' => 'Tests Rules Forms condition callbacks.',
'group' => 'Rules Forms',
);
}
/**
* Enables modules and logs in the privileged user.
*/
public function setUp() {
parent::setUp();
}
/**
* Tests callback for Condition: Element has value.
*/
public function testElementValueCondition() {
// Test the condition with a regular value in the field.
$form = array();
$test_form['test'] = array('#type' => 'textfield', '#value' => 'tset', '#default_value' => 'tset');
$form = new ArrayObject($test_form);
$form_state = new ArrayObject(array());
// Test with a correctly matching field value.
$condition1 = rules_condition('rules_forms_element_value', array('element' => 'textfield:test', 'value' => 'tset', 'regex' => 0));
$result1 = $condition1->executeByArgs(array('form' => $form, 'form_state' => $form_state));
$this->assertTrue($result1, t('Element values are equal.'));
// Test with an incorrectly matching field value.
$condition2 = rules_condition('rules_forms_element_value', array('element' => 'textfield:test', 'value' => 'test', 'regex' => 0));
$result2 = $condition2->executeByArgs(array('form' => $form, 'form_state' => $form_state));
$this->assertFalse($result2, t('Element values are not equal.'));
// Test with a correctly matching regular expression.
$condition3 = rules_condition('rules_forms_element_value', array('element' => 'textfield:test', 'value' => '/se/', 'regex' => 1));
$result3 = $condition3->executeByArgs(array('form' => $form, 'form_state' => $form_state));
$this->assertTrue($result3, t('Regular expression evaluation: value is equal.'));
// Test with a correctly matching regular expression.
$condition4 = rules_condition('rules_forms_element_value', array('element' => 'textfield:test', 'value' => '/es/', 'regex' => 1));
$result4 = $condition4->executeByArgs(array('form' => $form, 'form_state' => $form_state));
$this->assertFalse($result4, t('Regular expression evaluation: value is not equal.'));
// Test the condition with only a default value in the field.
unset($test_form['test']['#value']);
$form = new ArrayObject($test_form);
// Test with a correctly matching default field value.
$condition5 = rules_condition('rules_forms_element_value', array('element' => 'textfield:test', 'value' => 'tset', 'regex' => 0));
$result5 = $condition5->executeByArgs(array('form' => $form, 'form_state' => $form_state));
$this->assertTrue($result5, t('Element values are equal.'));
// Test with an incorrectly matching default field value.
$condition6 = rules_condition('rules_forms_element_value', array('element' => 'textfield:test', 'value' => 'test', 'regex' => 0));
$result6 = $condition6->executeByArgs(array('form' => $form, 'form_state' => $form_state));
$this->assertFalse($result6, t('Element values are not equal.'));
// Test with a correctly matching regular expression default value.
$condition7 = rules_condition('rules_forms_element_value', array('element' => 'textfield:test', 'value' => '/se/', 'regex' => 1));
$result7 = $condition7->executeByArgs(array('form' => $form, 'form_state' => $form_state));
$this->assertTrue($result7, t('Regular expression evaluation: value is equal.'));
// Test with a correctly matching regular expression.
$condition8 = rules_condition('rules_forms_element_value', array('element' => 'textfield:test', 'value' => '/es/', 'regex' => 1));
$result8 = $condition8->executeByArgs(array('form' => $form, 'form_state' => $form_state));
$this->assertFalse($result8, t('Regular expression evaluation: value is not equal.'));
}
/**
* Tests callback for Condition: Element value has changed.
*/
public function testElementValueChangedCondition() {
$form = self::$_form;
$form_state = self::$_form_state;
// rules_forms_event_build will set session variables.
variable_set('rules_forms_form_info', self::$_form_info);
rules_forms_event_build($form, $form_state, self::$_form_id);
// Execute condition without changing the value.
$form_state['values']['title'] = 'tset';
$condition1 = rules_condition('rules_forms_element_changed', array('element' => 'textfield:title'));
$result1 = $condition1->executeByArgs(array('form' => $form, 'form_state' => $form_state));
$this->assertFalse($result1, 'Form value has not changed.');
// Execute condition with changing the value.
$form_state['values']['title'] = 'test';
$condition2 = rules_condition('rules_forms_element_changed', array('element' => 'textfield:title'));
$result2 = $condition2->executeByArgs(array('form' => $form, 'form_state' => $form_state));
$this->assertTrue($result2, 'Form value has changed.');
}
/**
* Tests callback for Condition: Form button was clicked.
*/
public function testFormButtonClickedCondition() {
$form = self::$_form;
$form_state = self::$_form_state;
$form['test_submit'] = array(
'#name' => 'test_submit',
'#type' => 'submit',
'#value' => 'Submit',
'#submit' => 'rules_forms_test_submit',
);
// Test for condition not met.
$form_state['triggering_element'] = array('#name' => 'test_button', '#type' => 'button', '#value' => 'Test');
$condition1 = rules_condition('rules_forms_button_clicked', array('element' => 'submit:test_submit'));
$result1 = $condition1->executeByArgs(array('form' => $form, 'form_state' => $form_state));
$this->assertFalse($result1, 'Clicked button condition not met.');
// Test for condition met.
$form_state['triggering_element'] = $form['test_submit'];
$condition2 = rules_condition('rules_forms_button_clicked', array('element' => 'submit:test_submit'));
$result2 = $condition2->executeByArgs(array('form' => $form, 'form_state' => $form_state));
$this->assertTrue($result2, 'Clicked button condition met.');
}
}
/**
* Tests Rules Forms actions.
*/
class RulesFormsActionsTestCase extends RulesFormsRulesTestCase {
/**
* Defines test information.
*/
public static function getInfo() {
return array(
'name' => 'Rules Forms actions',
'description' => 'Tests Rules Forms action callbacks.',
'group' => 'Rules Forms',
);
}
/**
* Enables modules and logs in the privileged user.
*/
public function setUp() {
parent::setUp();
}
/**
* Tests callback for Action: Set element title.
*/
public function testSetTitleAction() {
$form = array();
$form['test'] = array('#type' => 'textfield', '#title' => 'tset');
$form = new ArrayObject($form);
$action = rules_action('rules_forms_set_title', array('element' => 'textfield:test', 'title' => 'test'));
$action->executeByArgs(array('form' => $form));
$this->assertEqual($form['test']['#title'], 'test');
}
/**
* Tests callback for Action: Set element description.
*/
public function testSetDescriptionAction() {
$form = array();
$form['test'] = array('#type' => 'textfield', '#description' => 'tset');
$form = new ArrayObject($form);
$action = rules_action('rules_forms_set_description', array('element' => 'textfield:test', 'description' => 'test'));
$action->executeByArgs(array('form' => $form));
$this->assertEqual($form['test']['#description'], 'test');
}
/**
* Tests callback for Action: Hide form element.
*/
public function testHideAction() {
$form = array();
$form['test'] = array('#type' => 'textfield', '#access' => FALSE);
$form = new ArrayObject($form);
$action = rules_action('rules_forms_set_access', array('element' => 'textfield:test', 'access' => 1));
$action->executeByArgs(array('form' => $form));
$this->assertFalse($form['test']['#access']);
}
/**
* Tests callback for Action: Disable form element.
*/
public function testSetDisabledAction() {
$form = array();
$form['test'] = array('#type' => 'textfield', '#disabled' => FALSE);
$form = new ArrayObject($form);
$action = rules_action('rules_forms_set_disabled', array('element' => 'textfield:test', 'disabled' => 1));
$action->executeByArgs(array('form' => $form));
$this->assertTrue($form['test']['#disabled']);
}
/**
* Tests callback for Action: Require form element.
*/
public function testSetRequiredAction() {
$form = array();
$form['test'] = array('#type' => 'textfield', '#required' => FALSE);
$form = new ArrayObject($form);
$action = rules_action('rules_forms_set_required', array('element' => 'textfield:test', 'require' => 1));
$action->executeByArgs(array('form' => $form));
$this->assertTrue($form['test']['#required']);
}
/**
* Tests callback for Action: Set multiple value options.
*/
public function testSetOptionsAction() {
$form = array();
$form['test'] = array('#type' => 'textfield', '#options' => array('first' => 'tset'));
$form = new ArrayObject($form);
$action = rules_action('rules_forms_set_options', array('element' => 'textfield:test', 'options' => 'first|test' . "\r\n" . 'second|tset'));
$action->executeByArgs(array('form' => $form));
$this->assertEqual($form['test']['#options'], array('first' => 'test', 'second' => 'tset'));
}
/**
* Tests callback for Action: Set default value of a form element.
*/
public function testSetDefaultAction() {
$form = array();
$form['test'] = array('#type' => 'textfield', '#default_value' => 'tset');
$form = new ArrayObject($form);
$action = rules_action('rules_forms_set_default', array('element' => 'textfield:test', 'value' => 'test'));
$action->executeByArgs(array('form' => $form));
$this->assertEqual($form['test']['#default_value'], 'test');
}
/**
* Tests callback for Action: Set element weight.
*/
public function testSetWeightAction() {
$form = array();
$form['test'] = array('#type' => 'textfield', '#weight' => 0);
$form = new ArrayObject($form);
$action = rules_action('rules_forms_set_weight', array('element' => 'textfield:test', 'weight' => 20));
$action->executeByArgs(array('form' => $form));
$this->assertEqual($form['test']['#weight'], 20);
}
/**
* Tests callback for Action: Set element prefix/suffix HTML.
*/
public function testSetPrefixSuffixAction() {
$form = array();
$form['test'] = array(
'#type' => 'textfield',
'#prefix' => 'tset',
'#suffix' => 'test',
);
$form = new ArrayObject($form);
$action = rules_action('rules_forms_set_prefix_suffix', array('element' => 'textfield:test', 'prefix' => '<div id="test">', 'suffix' => '</div>'));
$action->executeByArgs(array('form' => $form));
$this->assertEqual($form['test']['#prefix'], '<div id="test">');
$this->assertEqual($form['test']['#suffix'], '</div>');
}
}
/**
* Tests Rules Forms actions.
*/
class RulesFormsAPITestCase extends RulesFormsRulesTestCase {
/**
* Defines test information.
*/
public static function getInfo() {
return array(
'name' => 'API functions',
'description' => 'Tests Rules Forms API functions.',
'group' => 'Rules Forms',
);
}
/**
* Enables modules and logs in the privileged user.
*/
public function setUp() {
variable_set('rules_forms_form_info', self::$_form_info);
parent::setUp();
}
/**
* Tests _rules_forms_get_element().
*/
public function testGetElement() {
// Test retrieving the element.
$form = self::$_form;
$element_id = 'select:fieldset:select';
$element = &_rules_forms_get_element($form, $element_id);
$this->assertEqual($element, array(
'#type' => 'select',
'#title' => 'tset select',
'#options' => array('first' => 'option1', 'second' => 'option2'),
'#value' => 'tset',
'#default_value' => 'test',
));
// Test the reference.
$element['#title'] = 'test select';
$this->assertEqual($form['fieldset']['select']['#title'], 'test select');
}
/**
* Tests _rules_forms_get_elements().
*/
public function testGetElements() {
variable_set('rules_forms_form_info', self::$_form_info);
$form = self::$_form;
$elements = &_rules_forms_get_elements($form);
foreach ($elements as $element_id => &$element) {
$element['test'] = 'test';
}
// Ensure that references are preserved by _rules_forms_get_elements().
$message = 'Ensure the reference to $form is preserved.';
$this->assertTrue(isset($form['title']['test']) && $form['title']['test'] == 'test', $message);
$this->assertTrue(isset($form['body']['und'][0]['value']['test']) && $form['body']['und'][0]['value']['test'] == 'test', $message);
$this->assertTrue(isset($form['fieldset']['select']['test']) && $form['fieldset']['select']['test'] == 'test', $message);
$this->assertTrue(isset($form['fieldset']['checkboxes']['test']) && $form['fieldset']['checkboxes']['test'] == 'test', $message);
$form = array('form_id' => array('#value' => 'test'));
$array = &_rules_forms_get_elements($form);
$this->assertTrue(empty($array));
}
/**
* Tests rules_forms_get_form_info().
*/
public function testGetFormInfo() {
variable_set('rules_forms_form_info', self::$_form_info);
// Test getting all form info.
$info = rules_forms_get_form_info();
$this->assertEqual($info, self::$_form_info);
// Test getting info by form ID.
$form_id = key(self::$_form_info);
$form_info = rules_forms_get_form_info($form_id);
$this->assertEqual($form_info, self::$_form_info[$form_id]);
// Test getting info by $form array.
$form_info = rules_forms_get_form_info(self::$_form);
$this->assertEqual($form_info, self::$_form_info[self::$_form['form_id']['#value']]);
}
/**
* Tests rules_forms_save_form_info().
*/
public function testSaveFormInfo() {
variable_set('rules_forms_form_info', self::$_form_info);
$form_info = self::$_form_info;
$form_info['label'] = 'save test';
rules_forms_save_form_info(self::$_form_id, $form_info);
$result = variable_get('rules_forms_form_info', array());
$this->assertTrue($result[self::$_form_id]['label'] == 'save test');
}
/**
* Tests rules_forms_get_element_info().
*/
public function testGetElementInfo() {
variable_set('rules_forms_form_info', self::$_form_info);
$element_info = rules_forms_get_element_info(self::$_form_id);
$this->assertEqual($element_info, self::$_form_info[self::$_form_id]['elements']);
}
/**
* Tests rules_forms_get_element_ids().
*/
public function testGetElementIds() {
variable_set('rules_forms_form_info', self::$_form_info);
$element_ids = rules_forms_get_element_ids(self::$_form_id);
$this->assertEqual($element_ids, array_keys(self::$_form_info[self::$_form_id]['elements']));
}
/**
* Tests rules_forms_activate_form().
*/
public function testActivateForm() {
variable_set('rules_forms_form_info', self::$_form_info);
$form_id = 'activate_test';
$form_info = array('label' => 'Activate test', 'elements' => array(), 'buttons' => FALSE);
rules_forms_activate_form($form_id, $form_info);
$info = variable_get('rules_forms_form_info', array());
$this->assertEqual($info[$form_id], $form_info, 'Ensure the form was activated.');
}
/**
* Tests rules_forms_deactivate_form().
*/
public function testDeactivateForm() {
$this->testActivateForm();
$info = variable_get('rules_forms_form_info', array());
$this->assertTrue(isset($info['activate_test']), 'Ensure the form was activated.');
rules_forms_deactivate_form('activate_test');
$new_info = variable_get('rules_forms_form_info', array());
$this->assertTrue(!isset($new_info['activate_test']), 'Ensure the form was deactivated.');
}
/**
* Tests rules_forms_event_build().
*/
public function testEventBuild() {
variable_set('rules_forms_form_info', self::$_form_info);
$form_id = self::$_form['form_id']['#value'];
rules_forms_event_build(self::$_form, self::$_form_state, $form_id);
foreach (self::$_form_info[$form_id]['elements'] as $element_id => $info) {
$this->assertTrue(isset($_SESSION['rules_forms_form_values'][$form_id][$element_id]), 'Ensure the session variable was set for the element.');
// Values should always be 'tset' whether it came from #value or #default_value.
$this->assertTrue($_SESSION['rules_forms_form_values'][$form_id][$element_id] == 'tset');
}
}
/**
* Tests rules_forms_event_submit().
*/
public function testEventSubmit() {
variable_set('rules_forms_form_info', self::$_form_info);
// Ensure that the session variables are unset for the form.
$form_id = self::$_form['form_id']['#value'];
$_SESSION['rules_forms_form_values'][$form_id] = array('textfield:title' => 'tset', 'textarea:body:und:0:value' => 'tset');
rules_forms_event_submit(self::$_form, self::$_form_state);
$this->assertFalse(isset($_SESSION['rules_forms_form_values'][$form_id]), 'Ensure the session variable is unset.');
}
/**
* Tests rules_forms_event_button_submit().
*/
public function testEventButtonSubmit() {
// Add the button to the $form_info array.
$form_info = self::$_form_info;
$form_info[self::$_form_id]['submit']['fieldset:button:test'] = array();
variable_set('rules_forms_form_info', $form_info);
// Indicate that this button was clicked.
$form_id = self::$_form['form_id']['#value'];
$form_state = self::$_form_state;
$form_state['triggering_element'] = array('#rules_forms_element_id' => 'fieldset:button:test');
// Ensure that the session variables are unset for the form.
$_SESSION['rules_forms_form_values'][$form_id] = array('textfield:title' => 'tset', 'textarea:body:und:0:value' => 'tset');
rules_forms_event_button_submit(self::$_form, $form_state);
$this->assertFalse(isset($_SESSION['rules_forms_form_values'][$form_id]), 'Ensure the session variable is unset.');
}
}