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