node.test 29.8 KB
Newer Older
1
2
3
<?php
// $Id$

4
5
6
7
8
/**
 * Test the node_load_multiple() function.
 */
class NodeLoadMultipleUnitTest extends DrupalWebTestCase {

9
  public static function getInfo() {
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
    return array(
      'name' => t('Load multiple nodes'),
      'description' => t('Test the loading of multiple nodes.'),
      'group' => t('Node'),
    );
  }

  function setUp() {
    parent::setUp();
    $web_user = $this->drupalCreateUser(array('create article content', 'create page content'));
    $this->drupalLogin($web_user);
  }

  /**
   * Create four nodes and ensure they're loaded correctly.
   */
  function testNodeMultipleLoad() {
    $node1 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
    $node2 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
    $node3 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 0));
    $node4 = $this->drupalCreateNode(array('type' => 'page', 'promote' => 0));

    // Confirm that promoted nodes appear in the default node listing.
    $this->drupalGet('node');
    $this->assertText($node1->title, t('Node title appears on the default listing.'));
    $this->assertText($node2->title, t('Node title appears on the default listing.'));
    $this->assertNoText($node3->title, t('Node title does not appear in the default listing.'));
    $this->assertNoText($node4->title, t('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, t('Node was loaded.'));
    $this->assertEqual($node4->title, $nodes[$node4->nid]->title, t('Node was loaded.'));
    $count = count($nodes);
    $this->assertTrue($count == 2, t('@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, t('@count nodes loaded', array('@count' => $count)));
    $this->assertTrue(isset($nodes[$node1->nid]), t('Node is correctly keyed in the array'));
    $this->assertTrue(isset($nodes[$node2->nid]), t('Node is correctly keyed in the array'));
    $this->assertTrue(isset($nodes[$node4->nid]), t('Node is correctly keyed in the array'));
    foreach ($nodes as $node) {
      $this->assertTrue(is_object($node), t('Node is an object'));
    }

    // Load nodes by nid, where type = article. Nodes 1, 2 and 3 will be loaded.
    $nodes = node_load_multiple(array(1, 2, 3, 4), array('type' => 'article'));
    $count = count($nodes);
    $this->assertTrue($count == 3, t('@count nodes loaded', array('@count' => $count)));
    $this->assertEqual($nodes[$node1->nid]->title, $node1->title, t('Node successfully loaded.'));
    $this->assertEqual($nodes[$node2->nid]->title, $node2->title, t('Node successfully loaded.'));
    $this->assertEqual($nodes[$node3->nid]->title, $node3->title, t('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' => 'article'));
    $count = count($nodes);
    $this->assertTrue($count == 3, t('@count nodes loaded.', array('@count' => $count)));
    $this->assertEqual($nodes[$node1->nid]->title, $node1->title, t('Node successfully loaded'));
    $this->assertEqual($nodes[$node2->nid]->title, $node2->title, t('Node successfully loaded'));
    $this->assertEqual($nodes[$node3->nid]->title, $node3->title, t('Node successfully loaded'));
    $this->assertFalse(isset($nodes[$node4->nid]), t('Node was not loaded'));

    // Load nodes by nid, where type = article and promote = 0.
    $nodes = node_load_multiple(array(1, 2, 3, 4), array('type' => 'article', 'promote' => 0));
    $count = count($nodes);
    $this->assertTrue($count == 1, t('@count node loaded', array('@count' => $count)));
    $this->assertEqual($nodes[$node3->nid]->title, $node3->title, t('Node successfully loaded.'));
  }
}

84
class NodeRevisionsTestCase extends DrupalWebTestCase {
85
86
87
  protected $nodes;
  protected $logs;

88
  public static function getInfo() {
89
90
    return array(
      'name' => t('Node revisions'),
91
92
      'description' => t('Create a node with revisions and test viewing, reverting, and deleting revisions.'),
      'group' => t('Node'),
93
94
95
    );
  }

96
97
  function setUp() {
    parent::setUp();
98

99
    // Create and login user.
100
    $web_user = $this->drupalCreateUser(array('view revisions', 'revert revisions', 'edit any page content',
101
                                               'delete revisions', 'delete any page content'));
102
    $this->drupalLogin($web_user);
103

104
105
106
107
    // Create initial node.
    $node = $this->drupalCreateNode();
    $settings = get_object_vars($node);
    $settings['revision'] = 1;
108

109
110
    $nodes = array();
    $logs = array();
111

112
113
    // Get original node.
    $nodes[] = $node;
114

115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    // 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->drupalCreateNode($settings);
      $node = node_load($node->nid); // Make sure we get revision information.
      $settings = get_object_vars($node);

      $nodes[] = $node;
    }

    $this->nodes = $nodes;
    $this->logs = $logs;
130
131
132
  }

  /**
133
   * Check node revision related operations.
134
   */
135
136
137
  function testRevisions() {
    $nodes = $this->nodes;
    $logs = $this->logs;
138

139
140
141
142
143
    // Get last node for simple checks.
    $node = $nodes[3];

    // Confirm the correct revision text appears on "view revisions" page.
    $this->drupalGet("node/$node->nid/revisions/$node->vid/view");
144
    $this->assertText($node->body[0]['value'], t('Correct text displays for version.'));
145
146
147
148
149
150
151
152
153
154
155
156
157

    // Confirm the correct log message appears on "revisions overview" page.
    $this->drupalGet("node/$node->nid/revisions");
    foreach ($logs as $log) {
      $this->assertText($log, t('Log message found.'));
    }

    // Confirm that revisions revert properly.
    $this->drupalPost("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))), t('Revision reverted.'));
    $reverted_node = node_load($node->nid);
158
    $this->assertTrue(($nodes[1]->body[0]['value'] == $reverted_node->body[0]['value']), t('Node reverted correctly.'));
159
160
161
162
163
164

    // Confirm revisions delete properly.
    $this->drupalPost("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)), t('Revision deleted.'));
165
    $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, t('Revision not found.'));
166
167
168
169
  }
}

class PageEditTestCase extends DrupalWebTestCase {
170
  public static function getInfo() {
171
    return array(
172
173
174
175
      'name' => t('Node edit'),
      'description' => t('Create a node and test node edit functionality.'),
      'group' => t('Node'),
    );
176
177
  }

178
179
180
  function setUp() {
    parent::setUp();

181
182
    $web_user = $this->drupalCreateUser(array('edit own page content', 'create page content'));
    $this->drupalLogin($web_user);
183
  }
184

185
186
187
188
  /**
   * Check node edit functionality.
   */
  function testPageEdit() {
189
    $body_key = 'body[0][value]';
190
191
192
    // Create node to edit.
    $edit = array();
    $edit['title'] = $this->randomName(8);
193
    $edit[$body_key] = $this->randomName(16);
194
195
    $this->drupalPost('node/add/page', $edit, t('Save'));

196
    // Check that the node exists in the database.
197
    $node = $this->drupalGetNodeByTitle($edit['title']);
198
    $this->assertTrue($node, t('Node found in database.'));
199

200
    // Check that "edit" link points to correct page.
201
    $this->clickLink(t('Edit'));
202
    $edit_url = url("node/$node->nid/edit", array('absolute' => TRUE));
203
204
    $actual_url = $this->getURL();
    $this->assertEqual($edit_url, $actual_url, t('On edit page.'));
205

206
207
208
    // Check that the title and body fields are displayed with the correct values.
    $this->assertLink(t('Edit'), 0, t('Edit tab found.'));
    $this->assertFieldByName('title', $edit['title'], t('Title field displayed.'));
209
    $this->assertFieldByName($body_key, $edit[$body_key], t('Body field displayed.'));
210

211
212
213
    // Edit the content of the node.
    $edit = array();
    $edit['title'] = $this->randomName(8);
214
    $edit[$body_key] = $this->randomName(16);
215
216
217
218
219
    // Stay on the current page, without reloading.
    $this->drupalPost(NULL, $edit, t('Save'));

    // Check that the title and body fields are displayed with the updated values.
    $this->assertText($edit['title'], t('Title displayed.'));
220
    $this->assertText($edit[$body_key], t('Body displayed.'));
221
222
223
224
  }
}

class PagePreviewTestCase extends DrupalWebTestCase {
225
  public static function getInfo() {
226
    return array(
227
228
229
230
      'name' => t('Node preview'),
      'description' => t('Test node preview functionality.'),
      'group' => t('Node'),
    );
231
232
  }

233
234
235
  function setUp() {
    parent::setUp();

236
237
    $web_user = $this->drupalCreateUser(array('edit own page content', 'create page content'));
    $this->drupalLogin($web_user);
238
  }
239

240
241
242
243
  /**
   * Check the node preview functionality.
   */
  function testPagePreview() {
244
245
    $body_key = 'body[0][value]';

246
247
248
    // Fill in node creation form and preview node.
    $edit = array();
    $edit['title'] = $this->randomName(8);
249
    $edit[$body_key] = $this->randomName(16);
250
251
    $this->drupalPost('node/add/page', $edit, t('Preview'));

252
    // Check that the preview is displaying the title and body.
253
    $this->assertTitle(t('Preview | Drupal'), t('Page title is preview.'));
254
    $this->assertText($edit['title'], t('Title displayed.'));
255
    $this->assertText($edit[$body_key], t('Body displayed.'));
256

257
258
    // Check that the title and body fields are displayed with the correct values.
    $this->assertFieldByName('title', $edit['title'], t('Title field displayed.'));
259
    $this->assertFieldByName($body_key, $edit[$body_key], t('Body field displayed.'));
260
  }
261
262
263
264
265

  /**
   * Check the node preview functionality, when using revisions.
   */
  function testPagePreviewWithRevisions() {
266
    $body_key = 'body[0][value]';
267
268
269
270
271
272
    // Force revision on page content.
    variable_set('node_options_page', array('status', 'revision'));

    // Fill in node creation form and preview node.
    $edit = array();
    $edit['title'] = $this->randomName(8);
273
    $edit[$body_key] = $this->randomName(16);
274
275
276
277
278
279
    $edit['log'] = $this->randomName(32);
    $this->drupalPost('node/add/page', $edit, t('Preview'));

    // Check that the preview is displaying the title and body.
    $this->assertTitle(t('Preview | Drupal'), t('Page title is preview.'));
    $this->assertText($edit['title'], t('Title displayed.'));
280
    $this->assertText($edit[$body_key], t('Body displayed.'));
281
282
283

    // Check that the title and body fields are displayed with the correct values.
    $this->assertFieldByName('title', $edit['title'], t('Title field displayed.'));
284
    $this->assertFieldByName($body_key, $edit[$body_key], t('Body field displayed.'));
285
286
287
288

    // Check that the log field has the correct value.
    $this->assertFieldByName('log', $edit['log'], t('Log field displayed.'));
  }
289
290
}

291
class PageCreationTestCase extends DrupalWebTestCase {
292
  public static function getInfo() {
293
    return array(
294
295
      'name' => t('Node creation'),
      'description' => t('Create a node and test saving it.'),
296
297
298
299
      'group' => t('Node'),
    );
  }

300
301
  function setUp() {
    parent::setUp();
302

303
    $web_user = $this->drupalCreateUser(array('create page content', 'edit own page content'));
304
    $this->drupalLogin($web_user);
305
  }
306

307
308
309
310
311
  /**
   * Create a page node and verify its consistency in the database.
   */
  function testPageCreation() {
    // Create a node.
312
    $edit = array();
313
    $edit['title'] = $this->randomName(8);
314
    $edit['body[0][value]'] = $this->randomName(16);
315
316
    $this->drupalPost('node/add/page', $edit, t('Save'));

317
318
    // Check that the page has been created.
    $this->assertRaw(t('!post %title has been created.', array('!post' => 'Page', '%title' => $edit['title'])), t('Page created.'));
319

320
    // Check that the node exists in the database.
321
    $node = $this->drupalGetNodeByTitle($edit['title']);
322
    $this->assertTrue($node, t('Node found in database.'));
323
324
325
326
  }
}

class PageViewTestCase extends DrupalWebTestCase {
327
  public static function getInfo() {
328
    return array(
329
330
      'name' => t('Node edit permissions'),
      'description' => t('Create a node and test edit permissions.'),
331
332
      'group' => t('Node'),
    );
333
334
  }

335
336
337
  /**
   * Creates a node and then an anonymous and unpermissioned user attempt to edit the node.
   */
338
  function testPageView() {
339
    // Create a node to view.
340
    $node = $this->drupalCreateNode();
341
    $this->assertTrue(node_load($node->nid), t('Node created.'));
342

343
    // Try to edit with anonymous user.
344
345
346
    $html = $this->drupalGet("node/$node->nid/edit");
    $this->assertResponse(403);

347
348
349
    // Create a user without permission to edit node.
    $web_user = $this->drupalCreateUser(array('access content'));
    $this->drupalLogin($web_user);
350

351
352
    // Attempt to access edit page.
    $this->drupalGet("node/$node->nid/edit");
353
354
    $this->assertResponse(403);

355
    // Create user with permission to edit node.
356
    $web_user = $this->drupalCreateUser(array('bypass node access'));
357
358
359
360
361
    $this->drupalLogin($web_user);

    // Attempt to access edit page.
    $this->drupalGet("node/$node->nid/edit");
    $this->assertResponse(200);
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
class SummaryLengthTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => t('Summary length'),
      'description' => t('Test summary length.'),
      'group' => t('Node'),
    );
  }

  /**
   * Creates a node and then an anonymous and unpermissioned user attempt to edit the node.
   */
  function testSummaryLength() {
    // Create a node to view.
    $settings = array(
      'body' => 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 Drupalism? 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->drupalCreateNode($settings);
    $this->assertTrue(node_load($node->nid), t('Node created.'));

    // Create user with permission to view the node.
    $web_user = $this->drupalCreateUser(array('access content', 'administer content types'));
    $this->drupalLogin($web_user);

    // Attempt to access the front page.
    $this->drupalGet("node");
    // The node teaser when it has 600 characters in length
    $expected = 'What is a Drupalism?';
    $this->assertRaw($expected, t('Check that the summary is 600 characters in length'), 'Node');
    
    // Edit the teaser lenght for 'page' content type
    $edit = array (
      'teaser_length' => 200,
    );
    $this->drupalPost('admin/build/node-type/page', $edit, t('Save content type'));
    // Attempt to access the front page again and check if the summary is now only 200 characters in length.
    $this->drupalGet("node");
    $this->assertNoRaw($expected, t('Check that the summary is not longer than 200 characters'), 'Node');
  }
}

407
class NodeTitleXSSTestCase extends DrupalWebTestCase {
408
  public static function getInfo() {
409
    return array(
410
411
      'name' => t('Node title XSS filtering'),
      'description' => t('Create a node with dangerous tags in its title and test that they are escaped.'),
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
      'group' => t('Node'),
    );
  }

  function testNodeTitleXSS() {
    // Prepare a user to do the stuff.
    $web_user = $this->drupalCreateUser(array('create page content', 'edit any page content'));
    $this->drupalLogin($web_user);

    $xss = '<script>alert("xss")</script>';

    $edit = array(
      'title' => $xss . $this->randomName(),
    );
    $this->drupalPost('node/add/page', $edit, t('Preview'));
    $this->assertNoRaw($xss, t('Harmful tags are escaped when previewing a node.'));

    $node = $this->drupalCreateNode($edit);

    $this->drupalGet('node/' . $node->nid);
432
433
    // assertTitle() decodes HTML-entities inside the <title> element.
    $this->assertTitle($edit['title'] . ' | Drupal', t('Title is diplayed when viewing a node.'));
434
435
436
437
438
    $this->assertNoRaw($xss, t('Harmful tags are escaped when viewing a node.'));

    $this->drupalGet('node/' . $node->nid . '/edit');
    $this->assertNoRaw($xss, t('Harmful tags are escaped when editing a node.'));
  }
439
}
440
441

class NodeBlockTestCase extends DrupalWebTestCase {
442
  public static function getInfo() {
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
    return array(
      'name' => t('Block availability'),
      'description' => t('Check if the syndicate block is available.'),
      'group' => t('Node'),
    );
  }

  function setUp() {
    parent::setUp();

    // Create and login user
    $admin_user = $this->drupalCreateUser(array('administer blocks'));
    $this->drupalLogin($admin_user);
  }

  function testSearchFormBlock() {
    // Set block title to confirm that the interface is availble.
    $this->drupalPost('admin/build/block/configure/node/syndicate', array('title' => $this->randomName(8)), t('Save block'));
    $this->assertText(t('The block configuration has been saved.'), t('Block configuration set.'));

    // Set the block to a region to confirm block is availble.
    $edit = array();
    $edit['node_syndicate[region]'] = 'footer';
    $this->drupalPost('admin/build/block', $edit, t('Save blocks'));
    $this->assertText(t('The block settings have been updated.'), t('Block successfully move to footer region.'));
  }
}
470
471
472
473
474

/**
 * Check that the post information displays when enabled for a content type.
 */
class NodePostSettingsTestCase extends DrupalWebTestCase {
475
  public static function getInfo() {
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
    return array(
      'name' => t('Node post information display'),
      'description' => t('Check that the post information (submitted by Username on date) text displays appropriately.'),
      'group' => t('Node'),
    );
  }

  function setUp() {
    parent::setUp();

    $web_user = $this->drupalCreateUser(array('create page content', 'administer content types', 'access user profiles'));
    $this->drupalLogin($web_user);
  }

  /**
   * Set page content type to display post information and confirm its presence on a new node.
   */
  function testPagePostInfo() {

    // Set page content type to display post information.
    $edit = array();
    $edit['node_submitted'] = TRUE;
    $this->drupalPost('admin/build/node-type/page', $edit, t('Save content type'));

    // Create a node.
    $edit = array();
    $edit['title'] = $this->randomName(8);
503
    $edit['body[0][value]'] = $this->randomName(16);
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
    $this->drupalPost('node/add/page', $edit, t('Save'));

    // Check that the post information is displayed.
    $node = $this->drupalGetNodeByTitle($edit['title']);
    $this->assertRaw(theme('node_submitted', $node), t('Post information is displayed.'));
  }

  /**
   * Set page content type to not display post information and confirm its absence on a new node.
   */
  function testPageNotPostInfo() {

    // Set page content type to display post information.
    $edit = array();
    $edit['node_submitted'] = FALSE;
    $this->drupalPost('admin/build/node-type/page', $edit, t('Save content type'));

    // Create a node.
    $edit = array();
    $edit['title'] = $this->randomName(8);
524
    $edit['body[0][value]'] = $this->randomName(16);
525
526
527
528
529
530
    $this->drupalPost('node/add/page', $edit, t('Save'));

    // Check that the post information is displayed.
    $node = $this->drupalGetNodeByTitle($edit['title']);
    $this->assertNoRaw(theme('node_submitted', $node), t('Post information is not displayed.'));
  }
531
532
533
534
}

/**
 * Ensure that data added to nodes by other modules appears in RSS feeds.
535
 *
536
537
538
539
540
 * 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 DrupalWebTestCase {
541
  public static function getInfo() {
542
543
544
545
546
547
548
549
    return array(
      'name' => t('Node RSS Content'),
      'description' => t('Ensure that data added to nodes by other modules appears in RSS feeds.'),
      'group' => t('Node'),
    );
  }

  function setUp() {
550
    // Enable dummy module that implements hook_node_view.
551
552
553
554
555
556
557
558
559
560
561
562
    parent::setUp('node_test');
  }

  /**
   * Create a new node and ensure that it includes the custom data when added
   * to an RSS feed.
   */
  function testNodeRSSContent() {
    // Create a node.
    $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));

    $this->drupalGet('rss.xml');
563

564
    // Check that content added in 'rss' build mode appear in RSS feed.
565
566
567
    $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, t('Node content designated for RSS appear in RSS feed.'));

568
    // Check that content added in build modes other than 'rss' doesn't
569
570
571
572
573
574
575
576
577
578
579
580
581
    // 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, t('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:drupaltest="http://example.com/test-namespace"';
    $this->assertRaw(format_xml_elements(array($test_element)), t('Extra RSS elements appear in RSS feed.'));
    $this->assertRaw($test_ns, t('Extra namespaces appear in RSS feed.'));

582
583
    // Check that content added in 'rss' build mode doesn't appear when
    // viewing node.
584
585
    $this->drupalGet("node/$node->nid");
    $this->assertNoText($rss_only_content, t('Node content designed for RSS doesn\'t appear when viewing node.'));
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
/**
 * Test case to verify hook_node_access_records_alter functionality.
 */
class NodeAccessRecordsAlterUnitTest extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => t('Node access records alter'),
      'description' => t('Test hook_node_access_records_alter when acquiring grants.'),
      'group' => t('Node'),
    );
  }

  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');
  }

  /**
   * Create a node and test the creation of node access rules.
   */
  function testGrantAlter() {
    // Create an article node.
    $node1 = $this->drupalCreateNode(array('type' => 'article'));
    $this->assertTrue(node_load($node1->nid), t('Article 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 = %d', $node1->nid)->fetchAll();
    $this->assertEqual(count($records), 1, t('Returned the correct number of rows.'));
    $this->assertEqual($records[0]->realm, 'test_article_realm', t('Grant with article_realm acquired for node without alteration.'));
    $this->assertEqual($records[0]->gid, 1, t('Grant with gid = 1 acquired for node without alteration.'));

    // Create an unpromoted page node.
    $node2 = $this->drupalCreateNode(array('type' => 'page', 'promote' => 0));
    $this->assertTrue(node_load($node1->nid), t('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 = %d', $node2->nid)->fetchAll();
    $this->assertEqual(count($records), 1, t('Returned the correct number of rows.'));
    $this->assertEqual($records[0]->realm, 'test_page_realm', t('Grant with page_realm acquired for node without alteration.'));
    $this->assertEqual($records[0]->gid, 1, t('Grant with gid = 1 acquired for node without alteration.'));
631

632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
    // Create a promoted page node.
    $node3 = $this->drupalCreateNode(array('type' => 'page', 'promote' => 1));
    $this->assertTrue(node_load($node3->nid), t('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 = %d', $node3->nid)->fetchAll();
    $this->assertEqual(count($records), 1, t('Returned the correct number of rows.'));
    $this->assertEqual($records[0]->realm, 'test_alter_realm', t('Altered grant with alter_realm acquired for node.'));
    $this->assertEqual($records[0]->gid, 2, t('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->drupalCreateUser(array('access content'));
    foreach ($operations as $op) {
      $grants = node_test_node_grants($op, $web_user);
      $altered_grants = drupal_alter($grants, $web_user, $op);
      $this->assertNotEqual($grants, $altered_grants, t('Altered the %op grant for a user.', array('%op' => $op)));
    }
  }
}

655
656
657
658
/**
 * Test case to check node save related functionality, including import-save
 */
class NodeSaveTestCase extends DrupalWebTestCase {
659

660
  public static function getInfo() {
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
    return array(
      'name' => t('Node save'),
      'description' => t('Test node_save() for saving content.'),
      'group' => t('Node'),
    );
  }

  function setUp() {
    parent::setUp();
    // Create a user that is allowed to post; we'll use this to test the submission.
    $web_user = $this->drupalCreateUser(array('create article content'));
    $this->drupalLogin($web_user);
    $this->web_user = $web_user;
  }

  /**
   * Import test, to check if custom node ids are saved properly.
678
   * Workflow:
679
680
681
682
683
684
685
686
   *  - first create a piece of content
   *  - save the content
   *  - check if node exists
   */
  function testImport() {
    // Node ID must be a number that is not in the database.
    $max_nid = db_result(db_query('SELECT MAX(nid) FROM {node}'));
    $test_nid = $max_nid + mt_rand(1000, 1000000);
687
    $title = $this->randomName(8);
688
689
    $node = array(
      'title' => $title,
690
      'body' => array(array('value' => $this->randomName(32))),
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
      'uid' => $this->web_user->uid,
      'type' => 'article',
      'nid' => $test_nid,
      'is_new' => TRUE,
    );
    $node = (object)$node;
    node_save($node);
    // Test the import.
    $node_by_nid = node_load($test_nid);
    $this->assertTrue($node_by_nid, t('Node load by node ID.'));

    $node_by_title = $this->drupalGetNodeByTitle($title);
    $this->assertTrue($node_by_title, t('Node load by node title.'));
  }
}
706
707
708
709
710

/**
 * Tests related to node types.
 */
class NodeTypeTestCase extends DrupalWebTestCase {
711
  public static function getInfo() {
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
    return array(
      'name' => t('Node types'),
      'description' => t('Ensures that node type functions work correctly.'),
      'group' => t('Node'),
    );
  }

  /**
   * Ensure that node type functions (node_type_get_*) work correctly.
   *
   * Load available node types and validate the returned data.
   */
  function testNodeTypeGetFunctions() {
    $node_types = node_type_get_types();
    $node_names = node_type_get_names();

    $this->assertTrue(isset($node_types['article']), t('Node type article is available.'));
    $this->assertTrue(isset($node_types['page']), t('Node type page is available.'));

    $this->assertEqual($node_types['article']->name, $node_names['article'], t('Correct node type base has been returned.'));

    $this->assertEqual($node_types['article'], node_type_get_type('article'), t('Correct node type has been returned.'));
    $this->assertEqual($node_types['article']->name, node_type_get_name('article'), t('Correct node type name has been returned.'));
    $this->assertEqual($node_types['page']->base, node_type_get_base('page'), t('Correct node type base has been returned.'));
  }
}
738
739
740
741
742

/**
 * Rebuild the node_access table.
 */
class NodeAccessRebuildTestCase extends DrupalWebTestCase {
743
  public static function getInfo() {
744
745
746
747
748
749
    return array(
      'name' => t('Node access rebuild'),
      'description' => t('Ensures that node access rebuild functions work correctly.'),
      'group' => t('Node'),
    );
  }
750

751
752
753
754
755
756
757
  function setUp() {
    parent::setUp();

    $web_user = $this->drupalCreateUser(array('administer site configuration', 'access administration pages', 'access site reports'));
    $this->drupalLogin($web_user);
    $this->web_user = $web_user;
  }
758

759
760
761
762
763
764
765
  function testNodeAccessRebuild() {
    $this->drupalGet('admin/reports/status');
    $this->clickLink(t('Rebuild permissions'));
    $this->drupalPost(NULL, array(), t('Rebuild permissions'));
    $this->assertText(t('Content permissions have been rebuilt.'));
  }
}