3 namespace Drupal\Tests\node\Functional;
5 use Drupal\node\Entity\Node;
8 * Tests the loading of multiple nodes.
12 class NodeLoadMultipleTest extends NodeTestBase {
15 * Enable Views to test the frontpage against Node::loadMultiple() results.
19 public static $modules = ['views'];
21 protected function setUp() {
23 $web_user = $this->drupalCreateUser(['create article content', 'create page content']);
24 $this->drupalLogin($web_user);
28 * Creates four nodes and ensures that they are loaded correctly.
30 public function testNodeMultipleLoad() {
31 $node1 = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
32 $node2 = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
33 $node3 = $this->drupalCreateNode(['type' => 'article', 'promote' => 0]);
34 $node4 = $this->drupalCreateNode(['type' => 'page', 'promote' => 0]);
36 // Confirm that promoted nodes appear in the default node listing.
37 $this->drupalGet('node');
38 $this->assertText($node1->label(), 'Node title appears on the default listing.');
39 $this->assertText($node2->label(), 'Node title appears on the default listing.');
40 $this->assertNoText($node3->label(), 'Node title does not appear in the default listing.');
41 $this->assertNoText($node4->label(), 'Node title does not appear in the default listing.');
42 // Load nodes with only a condition. Nodes 3 and 4 will be loaded.
43 $nodes = $this->container->get('entity_type.manager')->getStorage('node')
44 ->loadByProperties(['promote' => 0]);
45 $this->assertEqual($node3->label(), $nodes[$node3->id()]->label(), 'Node was loaded.');
46 $this->assertEqual($node4->label(), $nodes[$node4->id()]->label(), 'Node was loaded.');
47 $count = count($nodes);
48 $this->assertTrue($count == 2, format_string('@count nodes loaded.', ['@count' => $count]));
50 // Load nodes by nid. Nodes 1, 2 and 4 will be loaded.
51 $nodes = Node::loadMultiple([1, 2, 4]);
52 $count = count($nodes);
53 $this->assertTrue(count($nodes) == 3, format_string('@count nodes loaded', ['@count' => $count]));
54 $this->assertTrue(isset($nodes[$node1->id()]), 'Node is correctly keyed in the array');
55 $this->assertTrue(isset($nodes[$node2->id()]), 'Node is correctly keyed in the array');
56 $this->assertTrue(isset($nodes[$node4->id()]), 'Node is correctly keyed in the array');
57 foreach ($nodes as $node) {
58 $this->assertTrue(is_object($node), 'Node is an object');