-
Notifications
You must be signed in to change notification settings - Fork 19
/
drupal_test_case.php
2575 lines (2386 loc) · 86.9 KB
/
drupal_test_case.php
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* @file
* Test Framework for Drupal based on PHPUnit.
*
* @todo
* - Simpletest's assertFalse casts to boolean whereas PHPUnit requires boolean. See testSiteWideContact().
* - hard coded TRUE at end of drupalLogin() because PHPUnit doesn't return
* anything from an assertion (unlike simpletest). Even if we fix drupalLogin(),
* we have to fix this to get 100% compatibility with simpletest.
* - setUp() only resets DB for mysql. Probably should use Drush and thus
* support postgres and sqlite easily. That buys us auto creation of upal DB
* as well.
* - Unlikely: Instead of DB restore, clone as per http://drupal.org/node/666956.
* - error() could log $caller info.
* - Fix verbose().
* - Fix random test failures.
* - Split into separate class files and add autoloader for upal.
* - Compare speed versus simpletest.
* - move upal_init() to a class thats called early in the suite.
*/
/*
* @todo: Perhaps move these annotations down to the instance classes and tests.
*
* @runTestsInSeparateProcess
* @preserveGlobalState disabled
*/
abstract class DrupalTestCase extends PHPUnit_Framework_TestCase {
/**
* The profile to install as a basis for testing.
*
* @var string
*/
protected $profile = 'standard';
/**
* The URL currently loaded in the internal browser.
*
* @var string
*/
protected $url;
/**
* The handle of the current cURL connection.
*
* @var resource
*/
protected $curlHandle;
/**
* The headers of the page currently loaded in the internal browser.
*
* @var Array
*/
protected $headers;
/**
* The content of the page currently loaded in the internal browser.
*
* @var string
*/
protected $content;
/**
* The content of the page currently loaded in the internal browser (plain text version).
*
* @var string
*/
protected $plainTextContent;
/**
* The value of the Drupal.settings JavaScript variable for the page currently loaded in the internal browser.
*
* @var Array
*/
protected $drupalSettings;
/**
* The parsed version of the page.
*
* @var SimpleXMLElement
*/
protected $elements = NULL;
/**
* The current user logged in using the internal browser.
*
* @var bool
*/
protected $loggedInUser = FALSE;
/**
* The current cookie file used by cURL.
*
* We do not reuse the cookies in further runs, so we do not need a file
* but we still need cookie handling, so we set the jar to NULL.
*/
protected $cookieFile = NULL;
/**
* Additional cURL options.
*
* DrupalWebTestCase itself never sets this but always obeys what is set.
*/
protected $additionalCurlOptions = array();
/**
* The original user, before it was changed to a clean uid = 1 for testing purposes.
*
* @var object
*/
protected $originalUser = NULL;
/**
* The original shutdown handlers array, before it was cleaned for testing purposes.
*
* @var array
*/
protected $originalShutdownCallbacks = array();
/**
* HTTP authentication method
*/
protected $httpauth_method = CURLAUTH_BASIC;
/**
* HTTP authentication credentials (<username>:<password>).
*/
protected $httpauth_credentials = NULL;
/**
* The current session name, if available.
*/
protected $session_name = NULL;
/**
* The current session ID, if available.
*/
protected $session_id = NULL;
/**
* Whether the files were copied to the test files directory.
*/
protected $generatedTestFiles = FALSE;
/**
* The number of redirects followed during the handling of a request.
*/
protected $redirect_count;
public function run(PHPUnit_Framework_TestResult $result = NULL) {
$this->setPreserveGlobalState(FALSE);
return parent::run($result);
}
/**
* Fire an assertion that is always positive.
*
* @param $message
* The message to display along with the assertion.
* @param $group
* The type of assertion - examples are "Browser", "PHP".
* @return
* TRUE.
*/
protected function pass($message = NULL, $group = 'Other') {
return $this->assertTrue(TRUE, $message, $group);
}
/**
* Fire an assertion that is always negative.
*
* @param $message
* The message to display along with the assertion.
* @param $group
* The type of assertion - examples are "Browser", "PHP".
* @return
* FALSE.
*/
//protected function fail($message = NULL, $group = 'Other') {
// return $this->assertTrue(FALSE, $message, $group);
//}
/**
* Fire an error assertion.
*
* @param $message
* The message to display along with the assertion.
* @param $group
* The type of assertion - examples are "Browser", "PHP".
* @param $caller
* The caller of the error.
* @return
* FALSE.
*/
protected function error($message = '', $group = 'Other', array $caller = NULL) {
if ($group == 'User notice') {
// Since 'User notice' is set by trigger_error() which is used for debug
// set the message to a status of 'debug'.
return $this->pass($message, $group);
}
// @todo Pass along $caller info.
return $this->fail('exception: ' . $message, $group);
}
function assertEqual($expected, $actual, $msg = NULL) {
return $this->assertEquals($expected, $actual);
}
function assertIdentical($first, $second, $message = '', $group = 'Other') {
return $this->assertSame($first, $second, $message);
}
/**
* Pass if the internal browser's URL matches the given path.
*
* @param $path
* The expected system path.
* @param $options
* (optional) Any additional options to pass for $path to url().
* @param $message
* Message to display.
* @param $group
* The group this message belongs to, defaults to 'Other'.
*
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertUrl($path, array $options = array(), $message = '', $group = 'Other') {
if (!$message) {
$message = t('Current URL is @url.', array(
'@url' => var_export(url($path, $options), TRUE),
));
}
$options['absolute'] = TRUE;
return $this->assertEqual($this->getUrl(), url($path, $options), $message, $group);
}
/**
* Pass if the raw text IS found on the loaded page, fail otherwise. Raw text
* refers to the raw HTML that the page generated.
*
* @param $raw
* Raw (HTML) string to look for.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to, defaults to 'Other'.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertRaw($raw, $message = '', $group = 'Other') {
if (!$message) {
$message = t('Raw "@raw" found', array('@raw' => $raw));
}
return $this->assertContains($raw, $this->drupalGetContent(), $message, $group);
}
/**
* Pass if the raw text is NOT found on the loaded page, fail otherwise. Raw text
* refers to the raw HTML that the page generated.
*
* @param $raw
* Raw (HTML) string to look for.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to, defaults to 'Other'.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertNoRaw($raw, $message = '', $group = 'Other') {
if (!$message) {
$message = t('Raw "@raw" not found', array('@raw' => $raw));
}
return $this->assertNotContains($raw, $this->drupalGetContent(), $message, $group);
}
/**
* Pass if the text IS found on the text version of the page. The text version
* is the equivalent of what a user would see when viewing through a web browser.
* In other words the HTML has been filtered out of the contents.
*
* @param $text
* Plain text to look for.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to, defaults to 'Other'.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertText($text, $message = '', $group = 'Other') {
return $this->assertTextHelper($text, $message, $group, FALSE);
}
/**
* Pass if the text is NOT found on the text version of the page. The text version
* is the equivalent of what a user would see when viewing through a web browser.
* In other words the HTML has been filtered out of the contents.
*
* @param $text
* Plain text to look for.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to, defaults to 'Other'.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertNoText($text, $message = '', $group = 'Other') {
return $this->assertTextHelper($text, $message, $group, TRUE);
}
/**
* Helper for assertText and assertNoText.
*
* It is not recommended to call this function directly.
*
* @param $text
* Plain text to look for.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to.
* @param $not_exists
* TRUE if this text should not exist, FALSE if it should.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertTextHelper($text, $message = '', $group, $not_exists) {
if ($this->plainTextContent === FALSE) {
$this->plainTextContent = filter_xss($this->drupalGetContent(), array());
}
if (!$message) {
$message = !$not_exists ? t('"@text" found', array('@text' => $text)) : t('"@text" not found', array('@text' => $text));
}
return $this->assertTrue($not_exists == (strpos($this->plainTextContent, $text) === FALSE), $message, $group);
}
/**
* Pass if the text is found ONLY ONCE on the text version of the page.
*
* The text version is the equivalent of what a user would see when viewing
* through a web browser. In other words the HTML has been filtered out of
* the contents.
*
* @param $text
* Plain text to look for.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to, defaults to 'Other'.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertUniqueText($text, $message = '', $group = 'Other') {
return $this->assertUniqueTextHelper($text, $message, $group, TRUE);
}
/**
* Pass if the text is found MORE THAN ONCE on the text version of the page.
*
* The text version is the equivalent of what a user would see when viewing
* through a web browser. In other words the HTML has been filtered out of
* the contents.
*
* @param $text
* Plain text to look for.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to, defaults to 'Other'.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertNoUniqueText($text, $message = '', $group = 'Other') {
return $this->assertUniqueTextHelper($text, $message, $group, FALSE);
}
/**
* Helper for assertUniqueText and assertNoUniqueText.
*
* It is not recommended to call this function directly.
*
* @param $text
* Plain text to look for.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to.
* @param $be_unique
* TRUE if this text should be found only once, FALSE if it should be found more than once.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertUniqueTextHelper($text, $message = '', $group, $be_unique) {
if ($this->plainTextContent === FALSE) {
$this->plainTextContent = filter_xss($this->drupalGetContent(), array());
}
if (!$message) {
$message = '"' . $text . '"' . ($be_unique ? ' found only once' : ' found more than once');
}
$first_occurance = strpos($this->plainTextContent, $text);
if ($first_occurance === FALSE) {
return $this->assertTrue(FALSE, $message, $group);
}
$offset = $first_occurance + strlen($text);
$second_occurance = strpos($this->plainTextContent, $text, $offset);
return $this->assertTrue($be_unique == ($second_occurance === FALSE), $message, $group);
}
/**
* Will trigger a pass if the Perl regex pattern is found in the raw content.
*
* @param $pattern
* Perl regex to look for including the regex delimiters.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertPattern($pattern, $message = '', $group = 'Other') {
if (!$message) {
$message = t('Pattern "@pattern" found', array('@pattern' => $pattern));
}
return $this->assertTrue((bool) preg_match($pattern, $this->drupalGetContent()), $message, $group);
}
/**
* Will trigger a pass if the perl regex pattern is not present in raw content.
*
* @param $pattern
* Perl regex to look for including the regex delimiters.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertNoPattern($pattern, $message = '', $group = 'Other') {
if (!$message) {
$message = t('Pattern "@pattern" not found', array('@pattern' => $pattern));
}
return $this->assertTrue(!preg_match($pattern, $this->drupalGetContent()), $message, $group);
}
/**
* Pass if the page title is the given string.
*
* @param $title
* The string the title should be.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertTitle($title, $message = '', $group = 'Other') {
$actual = (string) current($this->xpath('//title'));
if (!$message) {
$message = t('Page title @actual is equal to @expected.', array(
'@actual' => var_export($actual, TRUE),
'@expected' => var_export($title, TRUE),
));
}
return $this->assertEqual($actual, $title, $message, $group);
}
/**
* Pass if the page title is not the given string.
*
* @param $title
* The string the title should not be.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertNoTitle($title, $message = '', $group = 'Other') {
$actual = (string) current($this->xpath('//title'));
if (!$message) {
$message = t('Page title @actual is not equal to @unexpected.', array(
'@actual' => var_export($actual, TRUE),
'@unexpected' => var_export($title, TRUE),
));
}
return $this->assertNotEqual($actual, $title, $message, $group);
}
/**
* Asserts that a field exists in the current page by the given XPath.
*
* @param $xpath
* XPath used to find the field.
* @param $value
* (optional) Value of the field to assert.
* @param $message
* (optional) Message to display.
* @param $group
* (optional) The group this message belongs to.
*
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertFieldByXPath($xpath, $value = NULL, $message = '', $group = 'Other') {
$fields = $this->xpath($xpath);
// If value specified then check array for match.
$found = TRUE;
if (isset($value)) {
$found = FALSE;
if ($fields) {
foreach ($fields as $field) {
if (isset($field['value']) && $field['value'] == $value) {
// Input element with correct value.
$found = TRUE;
}
elseif (isset($field->option)) {
// Select element found.
if ($this->getSelectedItem($field) == $value) {
$found = TRUE;
}
else {
// No item selected so use first item.
$items = $this->getAllOptions($field);
if (!empty($items) && $items[0]['value'] == $value) {
$found = TRUE;
}
}
}
elseif ((string) $field == $value) {
// Text area with correct text.
$found = TRUE;
}
}
}
}
return $this->assertTrue($fields && $found, $message, $group);
}
/**
* Get the selected value from a select field.
*
* @param $element
* SimpleXMLElement select element.
* @return
* The selected value or FALSE.
*/
protected function getSelectedItem(SimpleXMLElement $element) {
foreach ($element->children() as $item) {
if (isset($item['selected'])) {
return $item['value'];
}
elseif ($item->getName() == 'optgroup') {
if ($value = $this->getSelectedItem($item)) {
return $value;
}
}
}
return FALSE;
}
/**
* Asserts that a field does not exist in the current page by the given XPath.
*
* @param $xpath
* XPath used to find the field.
* @param $value
* (optional) Value of the field to assert.
* @param $message
* (optional) Message to display.
* @param $group
* (optional) The group this message belongs to.
*
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertNoFieldByXPath($xpath, $value = NULL, $message = '', $group = 'Other') {
$fields = $this->xpath($xpath);
// If value specified then check array for match.
$found = TRUE;
if (isset($value)) {
$found = FALSE;
if ($fields) {
foreach ($fields as $field) {
if ($field['value'] == $value) {
$found = TRUE;
}
}
}
}
return $this->assertFalse($fields && $found, $message, $group);
}
/**
* Asserts that a field exists in the current page with the given name and value.
*
* @param $name
* Name of field to assert.
* @param $value
* Value of the field to assert.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertFieldByName($name, $value = '', $message = '') {
return $this->assertFieldByXPath($this->constructFieldXpath('name', $name), $value, $message ? $message : t('Found field by name @name', array('@name' => $name)), t('Browser'));
}
/**
* Asserts that a field does not exist with the given name and value.
*
* @param $name
* Name of field to assert.
* @param $value
* Value of the field to assert.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertNoFieldByName($name, $value = '', $message = '') {
return $this->assertNoFieldByXPath($this->constructFieldXpath('name', $name), $value, $message ? $message : t('Did not find field by name @name', array('@name' => $name)), t('Browser'));
}
/**
* Asserts that a field exists in the current page with the given id and value.
*
* @param $id
* Id of field to assert.
* @param $value
* Value of the field to assert.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertFieldById($id, $value = '', $message = '') {
return $this->assertFieldByXPath($this->constructFieldXpath('id', $id), $value, $message ? $message : t('Found field by id @id', array('@id' => $id)), t('Browser'));
}
/**
* Asserts that a field does not exist with the given id and value.
*
* @param $id
* Id of field to assert.
* @param $value
* Value of the field to assert.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertNoFieldById($id, $value = '', $message = '') {
return $this->assertNoFieldByXPath($this->constructFieldXpath('id', $id), $value, $message ? $message : t('Did not find field by id @id', array('@id' => $id)), t('Browser'));
}
/**
* Asserts that a checkbox field in the current page is checked.
*
* @param $id
* Id of field to assert.
* @param $message
* Message to display.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertFieldChecked($id, $message = '') {
$elements = $this->xpath('//input[@id=:id]', array(':id' => $id));
return $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), $message ? $message : t('Checkbox field @id is checked.', array('@id' => $id)), t('Browser'));
}
/**
* Asserts that a checkbox field in the current page is not checked.
*
* @param $id
* Id of field to assert.
* @param $message
* Message to display.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertNoFieldChecked($id, $message = '') {
$elements = $this->xpath('//input[@id=:id]', array(':id' => $id));
return $this->assertTrue(isset($elements[0]) && empty($elements[0]['checked']), $message ? $message : t('Checkbox field @id is not checked.', array('@id' => $id)), t('Browser'));
}
/**
* Asserts that a select option in the current page is checked.
*
* @param $id
* Id of select field to assert.
* @param $option
* Option to assert.
* @param $message
* Message to display.
* @return
* TRUE on pass, FALSE on fail.
*
* @todo $id is unusable. Replace with $name.
*/
protected function assertOptionSelected($id, $option, $message = '') {
$elements = $this->xpath('//select[@id=:id]//option[@value=:option]', array(':id' => $id, ':option' => $option));
return $this->assertTrue(isset($elements[0]) && !empty($elements[0]['selected']), $message ? $message : t('Option @option for field @id is selected.', array('@option' => $option, '@id' => $id)), t('Browser'));
}
/**
* Asserts that a select option in the current page is not checked.
*
* @param $id
* Id of select field to assert.
* @param $option
* Option to assert.
* @param $message
* Message to display.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertNoOptionSelected($id, $option, $message = '') {
$elements = $this->xpath('//select[@id=:id]//option[@value=:option]', array(':id' => $id, ':option' => $option));
return $this->assertTrue(isset($elements[0]) && empty($elements[0]['selected']), $message ? $message : t('Option @option for field @id is not selected.', array('@option' => $option, '@id' => $id)), t('Browser'));
}
/**
* Asserts that a field exists with the given name or id.
*
* @param $field
* Name or id of field to assert.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertField($field, $message = '', $group = 'Other') {
return $this->assertFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field), NULL, $message, $group);
}
/**
* Asserts that a field does not exist with the given name or id.
*
* @param $field
* Name or id of field to assert.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertNoField($field, $message = '', $group = 'Other') {
return $this->assertNoFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field), NULL, $message, $group);
}
/**
* Asserts that each HTML ID is used for just a single element.
*
* @param $message
* Message to display.
* @param $group
* The group this message belongs to.
* @param $ids_to_skip
* An optional array of ids to skip when checking for duplicates. It is
* always a bug to have duplicate HTML IDs, so this parameter is to enable
* incremental fixing of core code. Whenever a test passes this parameter,
* it should add a "todo" comment above the call to this function explaining
* the legacy bug that the test wishes to ignore and including a link to an
* issue that is working to fix that legacy bug.
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertNoDuplicateIds($message = '', $group = 'Other', $ids_to_skip = array()) {
$status = TRUE;
foreach ($this->xpath('//*[@id]') as $element) {
$id = (string) $element['id'];
if (isset($seen_ids[$id]) && !in_array($id, $ids_to_skip)) {
$this->fail(t('The HTML ID %id is unique.', array('%id' => $id)), $group);
$status = FALSE;
}
$seen_ids[$id] = TRUE;
}
return $this->assertTrue($status, $message, $group);
}
/**
* Helper function: construct an XPath for the given set of attributes and value.
*
* @param $attribute
* Field attributes.
* @param $value
* Value of field.
* @return
* XPath for specified values.
*/
protected function constructFieldXpath($attribute, $value) {
$xpath = '//textarea[@' . $attribute . '=:value]|//input[@' . $attribute . '=:value]|//select[@' . $attribute . '=:value]';
return $this->buildXPathQuery($xpath, array(':value' => $value));
}
/**
* Asserts the page responds with the specified response code.
*
* @param $code
* Response code. For example 200 is a successful page request. For a list
* of all codes see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html.
* @param $message
* Message to display.
* @return
* Assertion result.
*/
protected function assertResponse($code, $message = '') {
$curl_code = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
$match = is_array($code) ? in_array($curl_code, $code) : $curl_code == $code;
return $this->assertTrue($match, $message ? $message : t('HTTP response expected !code, actual !curl_code', array('!code' => $code, '!curl_code' => $curl_code)), t('Browser'));
}
/**
* Asserts the page did not return the specified response code.
*
* @param $code
* Response code. For example 200 is a successful page request. For a list
* of all codes see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html.
* @param $message
* Message to display.
*
* @return
* Assertion result.
*/
protected function assertNoResponse($code, $message = '') {
$curl_code = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
$match = is_array($code) ? in_array($curl_code, $code) : $curl_code == $code;
return $this->assertFalse($match, $message ? $message : t('HTTP response not expected !code, actual !curl_code', array('!code' => $code, '!curl_code' => $curl_code)), t('Browser'));
}
/**
* Asserts that the most recently sent e-mail message has the given value.
*
* The field in $name must have the content described in $value.
*
* @param $name
* Name of field or message property to assert. Examples: subject, body, id, ...
* @param $value
* Value of the field to assert.
* @param $message
* Message to display.
*
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertMail($name, $value = '', $message = '') {
$captured_emails = variable_get('drupal_test_email_collector', array());
$email = end($captured_emails);
return $this->assertTrue($email && isset($email[$name]) && $email[$name] == $value, $message, t('E-mail'));
}
/**
* Asserts that the most recently sent e-mail message has the string in it.
*
* @param $field_name
* Name of field or message property to assert: subject, body, id, ...
* @param $string
* String to search for.
* @param $email_depth
* Number of emails to search for string, starting with most recent.
*
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertMailString($field_name, $string, $email_depth) {
$mails = $this->drupalGetMails();
$string_found = FALSE;
for ($i = sizeof($mails) -1; $i >= sizeof($mails) - $email_depth && $i >= 0; $i--) {
$mail = $mails[$i];
// Normalize whitespace, as we don't know what the mail system might have
// done. Any run of whitespace becomes a single space.
$normalized_mail = preg_replace('/\s+/', ' ', $mail[$field_name]);
$normalized_string = preg_replace('/\s+/', ' ', $string);
$string_found = (FALSE !== strpos($normalized_mail, $normalized_string));
if ($string_found) {
break;
}
}
return $this->assertTrue($string_found, t('Expected text found in @field of email message: "@expected".', array('@field' => $field_name, '@expected' => $string)));
}
/**
* Asserts that the most recently sent e-mail message has the pattern in it.
*
* @param $field_name
* Name of field or message property to assert: subject, body, id, ...
* @param $regex
* Pattern to search for.
*
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertMailPattern($field_name, $regex, $message) {
$mails = $this->drupalGetMails();
$mail = end($mails);
$regex_found = preg_match("/$regex/", $mail[$field_name]);
return $this->assertTrue($regex_found, t('Expected text found in @field of email message: "@expected".', array('@field' => $field_name, '@expected' => $regex)));
}
/**
* Pass if a link with the specified label is found, and optional with the
* specified index.
*
* @param $label
* Text between the anchor tags.
* @param $index
* Link position counting from zero.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to, defaults to 'Other'.
* @return
* TRUE if the assertion succeeded, FALSE otherwise.
*/
protected function assertLink($label, $index = 0, $message = '', $group = 'Other') {
$links = $this->xpath('//a[normalize-space(text())=:label]', array(':label' => $label));
$message = ($message ? $message : t('Link with label %label found.', array('%label' => $label)));
return $this->assertTrue(isset($links[$index]), $message, $group);
}
/**
* Pass if a link with the specified label is not found.
*
* @param $label
* Text between the anchor tags.
* @param $index
* Link position counting from zero.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to, defaults to 'Other'.
* @return
* TRUE if the assertion succeeded, FALSE otherwise.
*/
protected function assertNoLink($label, $message = '', $group = 'Other') {
$links = $this->xpath('//a[normalize-space(text())=:label]', array(':label' => $label));
$message = ($message ? $message : t('Link with label %label not found.', array('%label' => $label)));
return $this->assertTrue(empty($links), $message, $group);
}
/**
* Pass if a link containing a given href (part) is found.
*
* @param $href
* The full or partial value of the 'href' attribute of the anchor tag.
* @param $index
* Link position counting from zero.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to, defaults to 'Other'.
*
* @return
* TRUE if the assertion succeeded, FALSE otherwise.
*/
protected function assertLinkByHref($href, $index = 0, $message = '', $group = 'Other') {
$links = $this->xpath('//a[contains(@href, :href)]', array(':href' => $href));
$message = ($message ? $message : t('Link containing href %href found.', array('%href' => $href)));
return $this->assertTrue(isset($links[$index]), $message, $group);
}
/**
* Pass if a link containing a given href (part) is not found.
*
* @param $href
* The full or partial value of the 'href' attribute of the anchor tag.
* @param $message
* Message to display.
* @param $group
* The group this message belongs to, defaults to 'Other'.
*
* @return
* TRUE if the assertion succeeded, FALSE otherwise.
*/
protected function assertNoLinkByHref($href, $message = '', $group = 'Other') {