Version 1
[yaffs-website] / web / core / modules / node / tests / src / Functional / NodeLoadMultipleTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Functional;
4
5 use Drupal\node\Entity\Node;
6
7 /**
8  * Tests the loading of multiple nodes.
9  *
10  * @group node
11  */
12 class NodeLoadMultipleTest extends NodeTestBase {
13
14   /**
15    * Enable Views to test the frontpage against Node::loadMultiple() results.
16    *
17    * @var array
18    */
19   public static $modules = ['views'];
20
21   protected function setUp() {
22     parent::setUp();
23     $web_user = $this->drupalCreateUser(['create article content', 'create page content']);
24     $this->drupalLogin($web_user);
25   }
26
27   /**
28    * Creates four nodes and ensures that they are loaded correctly.
29    */
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]);
35
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]));
49
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');
59     }
60   }
61
62 }