function NodeLoadMultipleTest::testNodeMultipleLoad

Same name in other branches
  1. 9 core/modules/node/tests/src/Kernel/NodeLoadMultipleTest.php \Drupal\Tests\node\Kernel\NodeLoadMultipleTest::testNodeMultipleLoad()
  2. 8.9.x core/modules/node/tests/src/Functional/NodeLoadMultipleTest.php \Drupal\Tests\node\Functional\NodeLoadMultipleTest::testNodeMultipleLoad()
  3. 11.x core/modules/node/tests/src/Kernel/NodeLoadMultipleTest.php \Drupal\Tests\node\Kernel\NodeLoadMultipleTest::testNodeMultipleLoad()

Creates four nodes and ensures that they are loaded correctly.

File

core/modules/node/tests/src/Kernel/NodeLoadMultipleTest.php, line 35

Class

NodeLoadMultipleTest
Tests the loading of multiple nodes.

Namespace

Drupal\Tests\node\Kernel

Code

public function testNodeMultipleLoad() : void {
    $node1 = $this->drupalCreateNode([
        'type' => 'article',
        'promote' => 1,
    ]);
    $node2 = $this->drupalCreateNode([
        'type' => 'article',
        'promote' => 1,
    ]);
    $node3 = $this->drupalCreateNode([
        'type' => 'article',
        'promote' => 0,
    ]);
    $node4 = $this->drupalCreateNode([
        'type' => 'page',
        'promote' => 0,
    ]);
    // Load nodes with only a condition. Nodes 3 and 4 will be loaded.
    $nodes = $this->container
        ->get('entity_type.manager')
        ->getStorage('node')
        ->loadByProperties([
        'promote' => 0,
    ]);
    $this->assertEquals($node3->label(), $nodes[$node3->id()]
        ->label(), 'Node was loaded.');
    $this->assertEquals($node4->label(), $nodes[$node4->id()]
        ->label(), 'Node was loaded.');
    $this->assertCount(2, $nodes);
    // Load nodes by nid. Nodes 1, 2 and 4 will be loaded.
    $nodes = Node::loadMultiple([
        1,
        2,
        4,
    ]);
    $this->assertCount(3, $nodes);
    $this->assertTrue(isset($nodes[$node1->id()]), 'Node is correctly keyed in the array');
    $this->assertTrue(isset($nodes[$node2->id()]), 'Node is correctly keyed in the array');
    $this->assertTrue(isset($nodes[$node4->id()]), 'Node is correctly keyed in the array');
    foreach ($nodes as $node) {
        $this->assertIsObject($node);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.