forked from backdrop/backdrop
-
Notifications
You must be signed in to change notification settings - Fork 2
/
node.test
executable file
·2586 lines (2217 loc) · 103 KB
/
node.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
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
* Tests for node.module.
*/
/**
* Defines a base class for testing the Node module.
*/
class NodeWebTestCase extends BackdropWebTestCase {
function setUp() {
$modules = func_get_args();
if (isset($modules[0]) && is_array($modules[0])) {
$modules = $modules[0];
}
$modules[] = 'node';
parent::setUp($modules);
// Create Page and Post node types.
if ($this->profile != 'standard') {
$this->backdropCreateContentType(array('type' => 'page', 'name' => 'Page'));
$this->backdropCreateContentType(array('type' => 'post', 'name' => 'Post'));
}
}
}
/**
* Tests the node_load_multiple() function.
*/
class NodeLoadMultipleUnitTest extends BackdropWebTestCase {
function setUp() {
parent::setUp();
$web_user = $this->backdropCreateUser(array('create post content', 'create page content'));
$this->backdropLogin($web_user);
}
/**
* Creates four nodes and ensures that they are loaded correctly.
*/
function testNodeMultipleLoad() {
$node1 = $this->backdropCreateNode(array('type' => 'post', 'promote' => 1));
$node2 = $this->backdropCreateNode(array('type' => 'post', 'promote' => 1));
$node3 = $this->backdropCreateNode(array('type' => 'post', 'promote' => 0));
$node4 = $this->backdropCreateNode(array('type' => 'page', 'promote' => 0));
// Confirm that promoted nodes appear in the default node listing.
$this->backdropGet('node');
$this->assertText($node1->title, 'Node title appears on the default listing.');
$this->assertText($node2->title, 'Node title appears on the default listing.');
$this->assertNoText($node3->title, 'Node title does not appear in the default listing.');
$this->assertNoText($node4->title, 'Node title does not appear in the default listing.');
// Load nodes with only a condition. Nodes 3 and 4 will be loaded.
$nodes = node_load_multiple(NULL, array('promote' => 0));
$this->assertEqual($node3->title, $nodes[$node3->nid]->title, 'Node was loaded.');
$this->assertEqual($node4->title, $nodes[$node4->nid]->title, 'Node was loaded.');
$count = count($nodes);
$this->assertTrue($count == 2, format_string('@count nodes loaded.', array('@count' => $count)));
// Load nodes by nid. Nodes 1, 2 and 4 will be loaded.
$nodes = node_load_multiple(array(1, 2, 4));
$count = count($nodes);
$this->assertTrue(count($nodes) == 3, format_string('@count nodes loaded', array('@count' => $count)));
$this->assertTrue(isset($nodes[$node1->nid]), 'Node is correctly keyed in the array');
$this->assertTrue(isset($nodes[$node2->nid]), 'Node is correctly keyed in the array');
$this->assertTrue(isset($nodes[$node4->nid]), 'Node is correctly keyed in the array');
foreach ($nodes as $node) {
$this->assertTrue(is_object($node), 'Node is an object');
}
// Load nodes by nid, where type = post. Nodes 1, 2 and 3 will be loaded.
$nodes = node_load_multiple(array(1, 2, 3, 4), array('type' => 'post'));
$count = count($nodes);
$this->assertTrue($count == 3, format_string('@count nodes loaded', array('@count' => $count)));
$this->assertEqual($nodes[$node1->nid]->title, $node1->title, 'Node successfully loaded.');
$this->assertEqual($nodes[$node2->nid]->title, $node2->title, 'Node successfully loaded.');
$this->assertEqual($nodes[$node3->nid]->title, $node3->title, 'Node successfully loaded.');
$this->assertFalse(isset($nodes[$node4->nid]));
// Now that all nodes have been loaded into the static cache, ensure that
// they are loaded correctly again when a condition is passed.
$nodes = node_load_multiple(array(1, 2, 3, 4), array('type' => 'post'));
$count = count($nodes);
$this->assertTrue($count == 3, format_string('@count nodes loaded.', array('@count' => $count)));
$this->assertEqual($nodes[$node1->nid]->title, $node1->title, 'Node successfully loaded');
$this->assertEqual($nodes[$node2->nid]->title, $node2->title, 'Node successfully loaded');
$this->assertEqual($nodes[$node3->nid]->title, $node3->title, 'Node successfully loaded');
$this->assertFalse(isset($nodes[$node4->nid]), 'Node was not loaded');
// Load nodes by nid, where type = post and promote = 0.
$nodes = node_load_multiple(array(1, 2, 3, 4), array('type' => 'post', 'promote' => 0));
$count = count($nodes);
$this->assertTrue($count == 1, format_string('@count node loaded', array('@count' => $count)));
$this->assertEqual($nodes[$node3->nid]->title, $node3->title, 'Node successfully loaded.');
}
}
/**
* Tests for the hooks invoked during node_load().
*/
class NodeLoadHooksTestCase extends BackdropWebTestCase {
function setUp() {
parent::setUp('node_test');
}
/**
* Tests that hook_node_load() is invoked correctly.
*/
function testHookNodeLoad() {
// Create some sample posts and pages.
$node1 = $this->backdropCreateNode(array('type' => 'post', 'status' => NODE_PUBLISHED));
$node2 = $this->backdropCreateNode(array('type' => 'post', 'status' => NODE_PUBLISHED));
$node3 = $this->backdropCreateNode(array('type' => 'post', 'status' => NODE_NOT_PUBLISHED));
$node4 = $this->backdropCreateNode(array('type' => 'page', 'status' => NODE_NOT_PUBLISHED));
// Check that when a set of nodes that only contains posts is loaded,
// the properties added to the node by node_test_node_load() correctly
// reflect the expected values.
$nodes = node_load_multiple(array(), array('status' => NODE_PUBLISHED));
$loaded_node = end($nodes);
$this->assertEqual($loaded_node->node_test_loaded_nids, array($node1->nid, $node2->nid), 'hook_node_load() received the correct list of node IDs the first time it was called.');
$this->assertEqual($loaded_node->node_test_loaded_types, array('post'), 'hook_node_load() received the correct list of node types the first time it was called.');
// Now, as part of the same page request, load a set of nodes that contain
// both posts and pages, and make sure the parameters passed to
// node_test_node_load() are correctly updated.
$nodes = node_load_multiple(array(), array('status' => NODE_NOT_PUBLISHED));
$loaded_node = end($nodes);
$this->assertEqual($loaded_node->node_test_loaded_nids, array($node4->nid, $node3->nid), 'hook_node_load() received the correct list of node IDs the second time it was called.');
$this->assertEqual($loaded_node->node_test_loaded_types, array('page', 'post'), 'hook_node_load() received the correct list of node types the second time it was called.');
}
}
/**
* Tests the node revision functionality.
*/
class NodeRevisionsTestCase extends BackdropWebTestCase {
/**
* Nodes used by the test.
*
* @var array
*/
protected $nodes;
/**
* The revision messages for node revisions created in the test.
*
* @var array
*/
protected $logs;
function setUp() {
parent::setUp();
// Create and login user.
$web_user = $this->backdropCreateUser(array(
'view revisions',
'revert revisions',
'edit any page content',
'delete revisions',
'delete any page content',
));
$this->backdropLogin($web_user);
// Create initial node.
$node = $this->backdropCreateNode();
$settings = get_object_vars($node);
$settings['revision'] = 1;
$nodes = array();
$logs = array();
// Get original node.
$nodes[] = $node;
// Create three revisions.
$revision_count = 3;
for ($i = 0; $i < $revision_count; $i++) {
$logs[] = $settings['log'] = $this->randomName(32);
// Create revision with random title and body and update variables.
$this->backdropCreateNode($settings);
// Make sure we get revision information.
$node = node_load($node->nid);
$settings = get_object_vars($node);
$nodes[] = $node;
}
$this->nodes = $nodes;
$this->logs = $logs;
}
/**
* Checks node revision related operations.
*/
function testRevisions() {
$nodes = $this->nodes;
$logs = $this->logs;
// Get last node for simple checks.
$node = $nodes[3];
// Confirm the correct revision text appears on "view revisions" page.
$this->backdropGet("node/$node->nid/revisions/$node->vid/view");
$this->assertText($node->body[LANGUAGE_NONE][0]['value'], 'Correct text displays for version.');
// Confirm the correct log message appears on "revisions overview" page.
$this->backdropGet("node/$node->nid/revisions");
foreach ($logs as $log) {
$this->assertText($log, t('Log message found.'));
}
// Confirm that revisions revert properly.
$this->backdropPost("node/$node->nid/revisions/{$nodes[1]->vid}/revert", array(), t('Revert'));
$this->assertRaw(t('@type %title has been reverted back to the revision from %revision-date.', array(
'@type' => 'Page',
'%title' => $nodes[1]->title,
'%revision-date' => format_date($nodes[1]->revision_timestamp),
)), 'Revision reverted.');
$reverted_node = node_load($node->nid);
$this->assertTrue(($nodes[1]->body[LANGUAGE_NONE][0]['value'] == $reverted_node->body[LANGUAGE_NONE][0]['value']), 'Node reverted correctly.');
// Confirm revisions delete properly.
$this->backdropPost("node/$node->nid/revisions/{$nodes[1]->vid}/delete", array(), t('Delete'));
$this->assertRaw(t('Revision from %revision-date of @type %title has been deleted.', array(
'%revision-date' => format_date($nodes[1]->revision_timestamp),
'@type' => 'Page',
'%title' => $nodes[1]->title,
)), 'Revision deleted.');
$this->assertTrue(db_query('SELECT COUNT(vid) FROM {node_revision} WHERE nid = :nid and vid = :vid', array(':nid' => $node->nid, ':vid' => $nodes[1]->vid))->fetchField() == 0, 'Revision not found.');
// Set the revision timestamp to an older date to make sure that the
// confirmation message correctly displays the stored revision date.
$old_revision_date = REQUEST_TIME - 86400;
db_update('node_revision')
->condition('vid', $nodes[2]->vid)
->fields(array(
'timestamp' => $old_revision_date,
))
->execute();
$this->backdropPost("node/$node->nid/revisions/{$nodes[2]->vid}/revert", array(), t('Revert'));
$this->assertRaw(t('@type %title has been reverted back to the revision from %revision-date.', array(
'@type' => 'Page',
'%title' => $nodes[2]->title,
'%revision-date' => format_date($old_revision_date),
)));
}
/**
* Checks that revisions are correctly saved without log messages.
*/
function testNodeRevisionWithoutLogMessage() {
// Create a node with an initial log message.
$log = $this->randomName(10);
$node = $this->backdropCreateNode(array('log' => $log));
// Save over the same revision and explicitly provide an empty log message
// (for example, to mimic the case of a node form submitted with no text in
// the "log message" field), and check that the original log message is
// preserved.
$new_title = $this->randomName(10) . 'testNodeRevisionWithoutLogMessage1';
$node = clone $node;
$node->title = $new_title;
$node->log = '';
$node->revision = FALSE;
$node->save();
$this->backdropGet('node/' . $node->nid);
$this->assertText($new_title, 'New node title appears on the page.');
$node_revision = node_load($node->nid, NULL, TRUE);
$this->assertEqual($node_revision->log, $log, 'After an existing node revision is re-saved without a log message, the original log message is preserved.');
// Create another node with an initial log message.
$node = $this->backdropCreateNode(array('log' => $log));
// Save a new node revision without providing a log message, and check that
// this revision has an empty log message.
$new_title = $this->randomName(10) . 'testNodeRevisionWithoutLogMessage2';
$node = clone $node;
$node->title = $new_title;
$node->revision = TRUE;
$node->log = NULL;
$node->save();
$this->backdropGet('node/' . $node->nid);
$this->assertText($new_title, 'New node title appears on the page.');
$node_revision = node_load($node->nid, NULL, TRUE);
$this->assertTrue(empty($node_revision->log), 'After a new node revision is saved with an empty log message, the log message for the node is empty.');
}
}
/**
* Tests the node edit functionality.
*/
class PageEditTestCase extends BackdropWebTestCase {
/**
* A user with permission to create and edit own page content.
*
* @var object
*/
protected $web_user;
/**
* A user with permission to bypass node access and administer nodes.
*
* @var object
*/
protected $admin_user;
function setUp() {
parent::setUp();
$this->web_user = $this->backdropCreateUser(array('edit own page content', 'create page content'));
$this->admin_user = $this->backdropCreateUser(array('bypass node access', 'administer nodes'));
}
/**
* Checks node edit functionality.
*/
function testPageEdit() {
$this->backdropLogin($this->web_user);
$langcode = LANGUAGE_NONE;
$title_key = "title";
$body_key = "body[$langcode][0][value]";
// Create node to edit.
$edit = array();
$edit[$title_key] = $this->randomName(8);
$edit[$body_key] = $this->randomName(16);
$this->backdropPost('node/add/page', $edit, t('Save'));
// Check that the node exists in the database.
$node = $this->backdropGetNodeByTitle($edit[$title_key]);
$this->assertTrue($node, 'Node found in database.');
// Check that "edit" link points to correct page.
$this->clickLink(t('Edit'));
$edit_url = url("node/$node->nid/edit", array('absolute' => TRUE));
$actual_url = $this->getURL();
$this->assertEqual($edit_url, $actual_url, 'On edit page.');
// Check that the title and body fields are displayed with the correct values.
$active = '<span class="element-invisible">' . t('(active tab)') . '</span>';
$link_text = t('!local-task-title!active', array('!local-task-title' => t('Edit'), '!active' => $active));
$this->assertText(strip_tags($link_text), 0, 'Edit tab found and marked active.');
$this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.');
$this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.');
// Edit the content of the node.
$edit = array();
$edit[$title_key] = $this->randomName(8);
$edit[$body_key] = $this->randomName(16);
// Stay on the current page, without reloading.
$this->backdropPost(NULL, $edit, t('Save'));
// Check that the title and body fields are displayed with the updated values.
$this->assertText($edit[$title_key], 'Title displayed.');
$this->assertText($edit[$body_key], 'Body displayed.');
// Login as a second administrator user.
$second_web_user = $this->backdropCreateUser(array('administer nodes', 'edit any page content'));
$this->backdropLogin($second_web_user);
// Edit the same node, creating a new revision.
$this->backdropGet("node/$node->nid/edit");
$edit = array();
$edit['title'] = $this->randomName(8);
$edit[$body_key] = $this->randomName(16);
$edit['revision'] = TRUE;
$this->backdropPost(NULL, $edit, t('Save'));
// Ensure that the node revision has been created.
$revised_node = $this->backdropGetNodeByTitle($edit['title']);
$this->assertNotIdentical($node->vid, $revised_node->vid, 'A new revision has been created.');
// Ensure that the node author is preserved when it was not changed in the
// edit form.
$this->assertIdentical($node->uid, $revised_node->uid, 'The node author has been preserved.');
// Ensure that the revision authors are different since the revisions were
// made by different users.
$first_node_version = node_load($node->nid, $node->vid);
$second_node_version = node_load($node->nid, $revised_node->vid);
$this->assertNotIdentical($first_node_version->revision_uid, $second_node_version->revision_uid, 'Each revision has a distinct user.');
}
/**
* Tests changing a node's "authored by" field.
*/
function testPageAuthoredBy() {
$this->backdropLogin($this->admin_user);
// Create node to edit.
$langcode = LANGUAGE_NONE;
$body_key = "body[$langcode][0][value]";
$edit = array();
$edit['title'] = $this->randomName(8);
$edit[$body_key] = $this->randomName(16);
$this->backdropPost('node/add/page', $edit, t('Save'));
// Check that the node was authored by the currently logged in user.
$node = $this->backdropGetNodeByTitle($edit['title']);
$this->assertIdentical($node->uid, $this->admin_user->uid, 'Node authored by admin user.');
// Try to change the 'authored by' field to an invalid user name.
$edit = array(
'name' => 'invalid-name',
);
$this->backdropPost('node/' . $node->nid . '/edit', $edit, t('Save'));
$this->assertText('The username invalid-name does not exist.');
// Change the authored by field to an empty string, which should assign
// authorship to the anonymous user (uid 0).
$edit['name'] = '';
$this->backdropPost('node/' . $node->nid . '/edit', $edit, t('Save'));
$node = node_load($node->nid, NULL, TRUE);
$this->assertIdentical($node->uid, '0', 'Node authored by anonymous user.');
// Change the authored by field to another user's name (that is not
// logged in).
$edit['name'] = $this->web_user->name;
$this->backdropPost('node/' . $node->nid . '/edit', $edit, t('Save'));
$node = node_load($node->nid, NULL, TRUE);
$this->assertIdentical($node->uid, $this->web_user->uid, 'Node authored by normal user.');
// Check that normal users cannot change the authored by information.
$this->backdropLogin($this->web_user);
$this->backdropGet('node/' . $node->nid . '/edit');
$this->assertNoFieldByName('name');
}
}
/**
* Tests creating and saving a node.
*/
class NodeCreationTestCase extends BackdropWebTestCase {
function setUp() {
// Enable dummy module that implements hook_node_insert for exceptions.
parent::setUp('node_test_exception');
$web_user = $this->backdropCreateUser(array('create page content', 'edit own page content'));
$this->backdropLogin($web_user);
}
/**
* Creates a "Page" node and verifies its consistency in the database.
*/
function testNodeCreation() {
// Create a node.
$edit = array();
$langcode = LANGUAGE_NONE;
$edit["title"] = $this->randomName(8);
$edit["body[$langcode][0][value]"] = $this->randomName(16);
$this->backdropPost('node/add/page', $edit, t('Save'));
// Check that the Page has been created.
$this->assertRaw(t('!post %title has been created.', array('!post' => 'Page', '%title' => $edit["title"])), 'Page created.');
// Check that the node exists in the database.
$node = $this->backdropGetNodeByTitle($edit["title"]);
$this->assertTrue($node, 'Node found in database.');
}
/**
* Verifies that a transaction rolls back the failed creation.
*/
function testFailedPageCreation() {
// Create a node.
$edit = array(
'uid' => $this->loggedInUser->uid,
'name' => $this->loggedInUser->name,
'type' => 'page',
'langcode' => LANGUAGE_NONE,
'title' => 'testing_transaction_exception',
);
try {
// An exception is generated by node_test_exception_node_insert() if the
// title is 'testing_transaction_exception'.
entity_create('node', $edit)->save();
$this->fail(t('Expected exception has not been thrown.'));
}
catch (Exception $e) {
$this->pass(t('Expected exception has been thrown.'));
}
if (Database::getConnection()->supportsTransactions()) {
// Check that the node does not exist in the database.
$node = $this->backdropGetNodeByTitle($edit['title']);
$this->assertFalse($node, 'Transactions supported, and node not found in database.');
}
else {
// Check that the node exists in the database.
$node = $this->backdropGetNodeByTitle($edit['title']);
$this->assertTrue($node, 'Transactions not supported, and node found in database.');
// Check that the failed rollback was logged.
$records = db_query("SELECT wid FROM {watchdog} WHERE message LIKE 'Explicit rollback failed%'")->fetchAll();
$this->assertTrue(count($records) > 0, 'Transactions not supported, and rollback error logged to watchdog.');
}
// Check that the rollback error was logged.
$records = db_query("SELECT wid FROM {watchdog} WHERE variables LIKE '%Test exception for rollback.%'")->fetchAll();
$this->assertTrue(count($records) > 0, 'Rollback explanatory error logged to watchdog.');
}
/**
* Create an unpublished node and confirm correct redirect behavior.
*/
function testUnpublishedNodeCreation() {
// Set "Page" content type to be unpublished by default.
$node_type = node_type_get_type('page');
$node_type->settings['status_default'] = 0;
node_type_save($node_type);
// Set the front page to the default "node" page.
config_set('system.core', 'site_frontpage', 'node');
// Create a node.
$edit = array();
$edit["title"] = $this->randomName(8);
$edit["body[" . LANGUAGE_NONE . "][0][value]"] = $this->randomName(16);
$this->backdropPost('node/add/page', $edit, t('Save'));
// Check that the user was redirected to the home page.
$this->assertText(t('Welcome to Backdrop'), 'The user is redirected to the home page.');
}
}
/**
* Tests the functionality of node entity edit permissions.
*/
class PageViewTestCase extends BackdropWebTestCase {
/**
* Tests an anonymous and unpermissioned user attempting to edit the node.
*/
function testPageView() {
// Create a node to view.
$node = $this->backdropCreateNode();
$this->assertTrue(node_load($node->nid), 'Node created.');
// Try to edit with anonymous user.
$html = $this->backdropGet("node/$node->nid/edit");
$this->assertResponse(403);
// Create a user without permission to edit node.
$web_user = $this->backdropCreateUser(array('access content'));
$this->backdropLogin($web_user);
// Attempt to access edit page.
$this->backdropGet("node/$node->nid/edit");
$this->assertResponse(403);
// Create user with permission to edit node.
$web_user = $this->backdropCreateUser(array('bypass node access'));
$this->backdropLogin($web_user);
// Attempt to access edit page.
$this->backdropGet("node/$node->nid/edit");
$this->assertResponse(200);
}
}
/**
* Tests the summary length functionality.
*/
class SummaryLengthTestCase extends BackdropWebTestCase {
/**
* Tests the node summary length functionality.
*/
function testSummaryLength() {
// Create a node to view.
$settings = array(
'body' => array(LANGUAGE_NONE => array(array('value' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam vitae arcu at leo cursus laoreet. Curabitur dui tortor, adipiscing malesuada tempor in, bibendum ac diam. Cras non tellus a libero pellentesque condimentum. What is a foobar? Suspendisse ac lacus libero. Ut non est vel nisl faucibus interdum nec sed leo. Pellentesque sem risus, vulputate eu semper eget, auctor in libero. Ut fermentum est vitae metus convallis scelerisque. Phasellus pellentesque rhoncus tellus, eu dignissim purus posuere id. Quisque eu fringilla ligula. Morbi ullamcorper, lorem et mattis egestas, tortor neque pretium velit, eget eleifend odio turpis eu purus. Donec vitae metus quis leo pretium tincidunt a pulvinar sem. Morbi adipiscing laoreet mauris vel placerat. Nullam elementum, nisl sit amet scelerisque malesuada, dolor nunc hendrerit quam, eu ultrices erat est in orci. Curabitur feugiat egestas nisl sed accumsan.'))),
'promote' => 1,
);
$node = $this->backdropCreateNode($settings);
$this->assertTrue(node_load($node->nid), t('Node created.'));
// Create user with permission to view the node.
$web_user = $this->backdropCreateUser(array('access content', 'administer content types'));
$this->backdropLogin($web_user);
// Attempt to access the front page.
$this->backdropGet("node");
// The node teaser when it has 600 characters in length
$expected = 'What is a foobar?';
$this->assertRaw($expected, 'Check that the summary is 600 characters in length', 'Node');
// Change the teaser length for "Page" content type.
$instance = field_info_instance('node', 'body', $node->type);
$instance['display']['teaser']['settings']['trim_length'] = 200;
field_update_instance($instance);
// Attempt to access the front page again and check if the summary is now only 200 characters in length.
$this->backdropGet("node");
$this->assertNoRaw($expected, 'Check that the summary is not longer than 200 characters', 'Node');
}
}
/**
* Tests XSS functionality with a node entity.
*/
class NodeTitleXSSTestCase extends BackdropWebTestCase {
/**
* Tests XSS functionality with a node entity.
*/
function testNodeTitleXSS() {
// Prepare a user to do the stuff.
$web_user = $this->backdropCreateUser(array('create page content', 'edit any page content'));
$this->backdropLogin($web_user);
$xss = '<script>alert("xss")</script>';
$title = $xss . $this->randomName();
$edit = array("title" => $title);
$settings = array('title' => $title);
$node = $this->backdropCreateNode($settings);
$this->backdropGet('node/' . $node->nid);
// assertTitle() decodes HTML-entities inside the <title> element.
$this->assertTitle($edit["title"] . ' | Backdrop', 'Title is displayed when viewing a node.');
$this->assertNoRaw($xss, 'Harmful tags are escaped when viewing a node.');
$this->backdropGet('node/' . $node->nid . '/edit');
$this->assertNoRaw($xss, 'Harmful tags are escaped when editing a node.');
}
}
/**
* Tests the availability of the syndicate block.
*/
class NodeBlockTestCase extends BackdropWebTestCase {
function setUp() {
parent::setUp();
// Create and login user
$admin_user = $this->backdropCreateUser(array('administer blocks'));
$this->backdropLogin($admin_user);
}
/**
* Tests that the "Syndicate" block is shown when enabled.
*/
function testSyndicateBlock() {
$layout = layout_load('default');
$layout->addBlock('node', 'syndicate', 'footer');
$layout->save();
$syndicate_selector = '//*[contains(@class,"block-node-syndicate")]//a[contains(@class,"feed-icon")]';
$this->backdropGet('node');
$elements = $this->xpath($syndicate_selector);
$this->assertEqual(count($elements), 1, 'Syndicate block found on /node.');
$this->backdropGet('user');
$elements = $this->xpath($syndicate_selector);
$this->assertEqual(count($elements), 1, 'Syndicate block not shown on /user.');
}
}
/**
* Checks that the post information displays when enabled for a content type.
*/
class NodePostSettingsTestCase extends BackdropWebTestCase {
function setUp() {
parent::setUp();
$web_user = $this->backdropCreateUser(array('create page content', 'administer content types', 'access user profiles'));
$this->backdropLogin($web_user);
}
/**
* Confirms "Page" content type and post information is on a new node.
*/
function testPagePostInfo() {
// Set "Page" content type to display post information.
$edit = array();
$edit['node_submitted'] = TRUE;
$this->backdropPost('admin/structure/types/manage/page', $edit, t('Save content type'));
// Create a node.
$edit = array();
$langcode = LANGUAGE_NONE;
$edit["title"] = $this->randomName(8);
$edit["body[$langcode][0][value]"] = $this->randomName(16);
$this->backdropPost('node/add/page', $edit, t('Save'));
// Check that the post information is displayed.
$elements = $this->xpath('//div[contains(@class,:class)]', array(':class' => 'submitted'));
$this->assertEqual(count($elements), 1, 'Post information is displayed.');
}
/**
* Confirms absence of post information on a new node.
*/
function testPageNotPostInfo() {
// Set "Page" content type to display post information.
$edit = array();
$edit['node_submitted'] = FALSE;
$this->backdropPost('admin/structure/types/manage/page', $edit, t('Save content type'));
// Create a node.
$edit = array();
$langcode = LANGUAGE_NONE;
$edit["title"] = $this->randomName(8);
$edit["body[$langcode][0][value]"] = $this->randomName(16);
$this->backdropPost('node/add/page', $edit, t('Save'));
// Check that the post information is displayed.
$node = $this->backdropGetNodeByTitle($edit["title"]);
$this->assertNoRaw('<span class="submitted">', 'Post information is not displayed.');
}
}
/**
* Ensures that data added to nodes by other modules appears in RSS feeds.
*
* Create a node, enable the node_test module to ensure that extra data is
* added to the node->content array, then verify that the data appears on the
* sitewide RSS feed at rss.xml.
*/
class NodeRSSContentTestCase extends BackdropWebTestCase {
function setUp() {
// Enable dummy module that implements hook_node_view.
parent::setUp('node_test');
// Use bypass node access permission here, because the test class uses
// hook_grants_alter() to deny access to everyone on node_access
// queries.
$user = $this->backdropCreateUser(array('bypass node access', 'access content', 'create post content'));
$this->backdropLogin($user);
}
/**
* Ensures that a new node includes the custom data when added to an RSS feed.
*/
function testNodeRSSContent() {
// Create a node.
$node = $this->backdropCreateNode(array('type' => 'post', 'promote' => 1));
$this->backdropGet('rss.xml');
// Check that content added in 'rss' view mode appear in RSS feed.
$rss_only_content = t('Extra data that should appear only in the RSS feed for node !nid.', array('!nid' => $node->nid));
$this->assertText($rss_only_content, 'Node content designated for RSS appear in RSS feed.');
// Check that content added in view modes other than 'rss' doesn't
// appear in RSS feed.
$non_rss_content = t('Extra data that should appear everywhere except the RSS feed for node !nid.', array('!nid' => $node->nid));
$this->assertNoText($non_rss_content, "Node content not designed for RSS doesn't appear in RSS feed.");
// Check that extra RSS elements and namespaces are added to RSS feed.
$test_element = array(
'key' => 'testElement',
'value' => t('Value of testElement RSS element for node !nid.', array('!nid' => $node->nid)),
);
$test_ns = 'xmlns:backdroptest="http://example.com/test-namespace"';
$this->assertRaw(format_xml_elements(array($test_element)), 'Extra RSS elements appear in RSS feed.');
$this->assertRaw($test_ns, 'Extra namespaces appear in RSS feed.');
// Check that content added in 'rss' view mode doesn't appear when
// viewing node.
$this->backdropGet("node/$node->nid");
$this->assertNoText($rss_only_content, "Node content designed for RSS doesn't appear when viewing node.");
// Check that the node feed page does not try to interpret additional path
// components as arguments for node_feed() and returns default content.
$this->backdropGet('rss.xml/' . $this->randomName() . '/' . $this->randomName());
$this->assertText($rss_only_content, 'Ignore page arguments when delivering rss.xml.');
}
}
/**
* Tests basic node_access functionality.
*
* Note that hook_node_access_records() is covered in another test class.
*
* @todo Cover hook_node_access in a separate test class.
*/
class NodeAccessUnitTest extends BackdropWebTestCase {
/**
* Asserts node_access() correctly grants or denies access.
*/
function assertNodeAccess($ops, $node, $account) {
foreach ($ops as $op => $result) {
$msg = format_string("node_access returns @result with operation '@op'.", array('@result' => $result ? 'true' : 'false', '@op' => $op));
$this->assertEqual($result, node_access($op, $node, $account), $msg);
}
}
function setUp() {
parent::setUp();
// Clear permissions for authenticated users.
$role = user_role_load(BACKDROP_AUTHENTICATED_ROLE);
$role->permissions = array();
user_role_save($role);
}
/**
* Runs basic tests for node_access function.
*/
function testNodeAccess() {
// Ensures user without 'access content' permission can do nothing.
$web_user1 = $this->backdropCreateUser(array('create page content', 'edit any page content', 'delete any page content'));
$node1 = $this->backdropCreateNode(array('type' => 'page'));
$this->assertNodeAccess(array('create' => FALSE), 'page', $web_user1);
$this->assertNodeAccess(array('view' => FALSE, 'update' => FALSE, 'delete' => FALSE), $node1, $web_user1);
// Ensures user with 'bypass node access' permission can do everything.
$web_user2 = $this->backdropCreateUser(array('bypass node access'));
$node2 = $this->backdropCreateNode(array('type' => 'page'));
$this->assertNodeAccess(array('create' => TRUE), 'page', $web_user2);
$this->assertNodeAccess(array('view' => TRUE, 'update' => TRUE, 'delete' => TRUE), $node2, $web_user2);
// User cannot 'view own unpublished content'.
$web_user3 = $this->backdropCreateUser(array('access content'));
$node3 = $this->backdropCreateNode(array('status' => 0, 'uid' => $web_user3->uid));
$this->assertNodeAccess(array('view' => FALSE), $node3, $web_user3);
// User cannot create content without permission.
$this->assertNodeAccess(array('create' => FALSE), 'page', $web_user3);
// User can 'view own unpublished content', but another user cannot.
$web_user4 = $this->backdropCreateUser(array('access content', 'view own unpublished content'));
$web_user5 = $this->backdropCreateUser(array('access content', 'view own unpublished content'));
$node4 = $this->backdropCreateNode(array('status' => 0, 'uid' => $web_user4->uid));
$this->assertNodeAccess(array('view' => TRUE, 'update' => FALSE), $node4, $web_user4);
$this->assertNodeAccess(array('view' => FALSE), $node4, $web_user5);
// Tests the default access provided for a published node.
$node5 = $this->backdropCreateNode();
$this->assertNodeAccess(array('view' => TRUE, 'update' => FALSE, 'delete' => FALSE), $node5, $web_user3);
}
}
/**
* Tests hook_node_access_records() functionality.
*/
class NodeAccessRecordsUnitTest extends BackdropWebTestCase {
function setUp() {
// Enable dummy module that implements hook_node_grants(),
// hook_node_access_records(), hook_node_grants_alter() and
// hook_node_access_records_alter().
parent::setUp('node_test');
}
/**
* Creates a node and tests the creation of node access rules.
*/
function testNodeAccessRecords() {
// Create an post node.
$node1 = $this->backdropCreateNode(array('type' => 'post'));
$this->assertTrue(node_load($node1->nid), 'Post node created.');
// Check to see if grants added by node_test_node_access_records made it in.
$records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', array(':nid' => $node1->nid))->fetchAll();
$this->assertEqual(count($records), 1, 'Returned the correct number of rows.');
$this->assertEqual($records[0]->realm, 'test_post_realm', 'Grant with post_realm acquired for node without alteration.');
$this->assertEqual($records[0]->gid, 1, 'Grant with gid = 1 acquired for node without alteration.');
// Create an unpromoted "Page" node.
$node2 = $this->backdropCreateNode(array('type' => 'page', 'promote' => 0));
$this->assertTrue(node_load($node2->nid), 'Unpromoted page node created.');
// Check to see if grants added by node_test_node_access_records made it in.
$records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', array(':nid' => $node2->nid))->fetchAll();
$this->assertEqual(count($records), 1, 'Returned the correct number of rows.');
$this->assertEqual($records[0]->realm, 'test_page_realm', 'Grant with page_realm acquired for node without alteration.');
$this->assertEqual($records[0]->gid, 1, 'Grant with gid = 1 acquired for node without alteration.');
// Create an unpromoted, unpublished "Page" node.
$node3 = $this->backdropCreateNode(array('type' => 'page', 'promote' => 0, 'status' => 0));
$this->assertTrue(node_load($node3->nid), 'Unpromoted, unpublished page node created.');
// Check to see if grants added by node_test_node_access_records made it in.
$records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', array(':nid' => $node3->nid))->fetchAll();
$this->assertEqual(count($records), 1, 'Returned the correct number of rows.');
$this->assertEqual($records[0]->realm, 'test_page_realm', 'Grant with page_realm acquired for node without alteration.');
$this->assertEqual($records[0]->gid, 1, 'Grant with gid = 1 acquired for node without alteration.');
// Create a promoted "Page" node.
$node4 = $this->backdropCreateNode(array('type' => 'page', 'promote' => 1));
$this->assertTrue(node_load($node4->nid), 'Promoted page node created.');
// Check to see if grant added by node_test_node_access_records was altered
// by node_test_node_access_records_alter.
$records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', array(':nid' => $node4->nid))->fetchAll();
$this->assertEqual(count($records), 1, 'Returned the correct number of rows.');
$this->assertEqual($records[0]->realm, 'test_alter_realm', 'Altered grant with alter_realm acquired for node.');
$this->assertEqual($records[0]->gid, 2, 'Altered grant with gid = 2 acquired for node.');
// Check to see if we can alter grants with hook_node_grants_alter().
$operations = array('view', 'update', 'delete');
// Create a user that is allowed to access content.
$web_user = $this->backdropCreateUser(array('access content'));
foreach ($operations as $op) {
$grants = node_test_node_grants($op, $web_user);
$altered_grants = $grants;
backdrop_alter('node_grants', $altered_grants, $web_user, $op);
$this->assertNotEqual($grants, $altered_grants, format_string('Altered the %op grant for a user.', array('%op' => $op)));
}
// Check that core does not grant access to an unpublished node when an
// empty $grants array is returned.
$node6 = $this->backdropCreateNode(array('status' => 0, 'disable_node_access' => TRUE));
$records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', array(':nid' => $node6->nid))->fetchAll();
$this->assertEqual(count($records), 0, 'Returned no records for unpublished node.');
}
}
/**
* Tests for Node Access with a non-node base table.
*/
class NodeAccessBaseTableTestCase extends BackdropWebTestCase {
public function setUp() {
parent::setUp('node_access_test');
node_access_rebuild();
state_set('node_access_test_private', TRUE);
}
/**
* Tests the "private" node access functionality.
*
* - Create 2 users with "access content" and "create post" permissions.
* - Each user creates one private and one not private post.
* - Test that each user can view the other user's non-private post.
* - Test that each user cannot view the other user's private post.
* - Test that each user finds only appropriate (non-private + own private)
* in taxonomy listing.
* - Create another user with 'view any private content'.
* - Test that user 4 can view all content created above.
* - Test that user 4 can view all content on taxonomy listing.
*/
function testNodeAccessBasic() {
$num_simple_users = 2;
$simple_users = array();
// nodes keyed by uid and nid: $nodes[$uid][$nid] = $is_private;
$this->nodesByUser = array();
$titles = array(); // Titles keyed by nid
$private_nodes = array(); // Array of nids marked private.
for ($i = 0; $i < $num_simple_users; $i++) {
$simple_users[$i] = $this->backdropCreateUser(array('access content', 'create post content'));
}
foreach ($simple_users as $this->webUser) {
$this->backdropLogin($this->webUser);
foreach (array(0 => 'Public', 1 => 'Private') as $is_private => $type) {
$edit = array(
'title' => t('@private_public Post created by @user', array('@private_public' => $type, '@user' => $this->webUser->name)),
);
if ($is_private) {
$edit['private'] = TRUE;
$edit['body[und][0][value]'] = 'private node';
$edit['field_tags[und]'] = 'private';
}
else {
$edit['body[und][0][value]'] = 'public node';
$edit['field_tags[und]'] = 'public';
}
$this->backdropPost('node/add/post', $edit, t('Save'));
$nid = db_query('SELECT nid FROM {node} WHERE title = :title', array(':title' => $edit['title']))->fetchField();
$private_status = db_query('SELECT private FROM {node_access_test} where nid = :nid', array(':nid' => $nid))->fetchField();
$this->assertTrue($is_private == $private_status, 'The private status of the node was properly set in the node_access_test table.');
if ($is_private) {
$private_nodes[] = $nid;
}
$titles[$nid] = $edit['title'];
$this->nodesByUser[$this->webUser->uid][$nid] = $is_private;
}
}
$this->publicTid = db_query('SELECT tid FROM {taxonomy_term_data} WHERE name = :name', array(':name' => 'public'))->fetchField();
$this->privateTid = db_query('SELECT tid FROM {taxonomy_term_data} WHERE name = :name', array(':name' => 'private'))->fetchField();
$this->assertTrue($this->publicTid, 'Public tid was found');
$this->assertTrue($this->privateTid, 'Private tid was found');
foreach ($simple_users as $this->webUser) {
$this->backdropLogin($this->webUser);
// Check own nodes to see that all are readable.
foreach ($this->nodesByUser as $uid => $data) {
foreach ($data as $nid => $is_private) {
$this->backdropGet('node/' . $nid);
if ($is_private) {
$should_be_visible = $uid == $this->webUser->uid;
}
else {
$should_be_visible = TRUE;
}
$this->assertResponse($should_be_visible ? 200 : 403, strtr('A %private node by user %uid is %visible for user %current_uid.', array(
'%private' => $is_private ? 'private' : 'public',
'%uid' => $uid,
'%visible' => $should_be_visible ? 'visible' : 'not visible',
'%current_uid' => $this->webUser->uid,
)));
}
}