1 node.test NodeLoadMultipleUnitTest::testNodeMultipleLoad()

Creates four nodes and ensures that they are loaded correctly.

File

core/modules/node/tests/node.test, line 48
Tests for node.module.

Class

NodeLoadMultipleUnitTest
Tests the node_load_multiple() function.

Code

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