Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\node\Kernel;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
Chris@0
|
6 use Drupal\node\Entity\Node;
|
Chris@0
|
7 use Drupal\node\Entity\NodeType;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Tests that conditions, provided by the node module, are working properly.
|
Chris@0
|
11 *
|
Chris@0
|
12 * @group node
|
Chris@0
|
13 */
|
Chris@0
|
14 class NodeConditionTest extends EntityKernelTestBase {
|
Chris@0
|
15
|
Chris@0
|
16 public static $modules = ['node'];
|
Chris@0
|
17
|
Chris@0
|
18 protected function setUp() {
|
Chris@0
|
19 parent::setUp();
|
Chris@0
|
20
|
Chris@0
|
21 // Create the node bundles required for testing.
|
Chris@0
|
22 $type = NodeType::create(['type' => 'page', 'name' => 'page']);
|
Chris@0
|
23 $type->save();
|
Chris@0
|
24 $type = NodeType::create(['type' => 'article', 'name' => 'article']);
|
Chris@0
|
25 $type->save();
|
Chris@0
|
26 $type = NodeType::create(['type' => 'test', 'name' => 'test']);
|
Chris@0
|
27 $type->save();
|
Chris@0
|
28 }
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * Tests conditions.
|
Chris@0
|
32 */
|
Chris@0
|
33 public function testConditions() {
|
Chris@0
|
34 $manager = $this->container->get('plugin.manager.condition', $this->container->get('container.namespaces'));
|
Chris@0
|
35 $this->createUser();
|
Chris@0
|
36
|
Chris@0
|
37 // Get some nodes of various types to check against.
|
Chris@0
|
38 $page = Node::create(['type' => 'page', 'title' => $this->randomMachineName(), 'uid' => 1]);
|
Chris@0
|
39 $page->save();
|
Chris@0
|
40 $article = Node::create(['type' => 'article', 'title' => $this->randomMachineName(), 'uid' => 1]);
|
Chris@0
|
41 $article->save();
|
Chris@0
|
42 $test = Node::create(['type' => 'test', 'title' => $this->randomMachineName(), 'uid' => 1]);
|
Chris@0
|
43 $test->save();
|
Chris@0
|
44
|
Chris@0
|
45 // Grab the node type condition and configure it to check against node type
|
Chris@0
|
46 // of 'article' and set the context to the page type node.
|
Chris@0
|
47 $condition = $manager->createInstance('node_type')
|
Chris@0
|
48 ->setConfig('bundles', ['article' => 'article'])
|
Chris@0
|
49 ->setContextValue('node', $page);
|
Chris@0
|
50 $this->assertFalse($condition->execute(), 'Page type nodes fail node type checks for articles.');
|
Chris@0
|
51 // Check for the proper summary.
|
Chris@0
|
52 $this->assertEqual('The node bundle is article', $condition->summary());
|
Chris@0
|
53
|
Chris@0
|
54 // Set the node type check to page.
|
Chris@0
|
55 $condition->setConfig('bundles', ['page' => 'page']);
|
Chris@0
|
56 $this->assertTrue($condition->execute(), 'Page type nodes pass node type checks for pages');
|
Chris@0
|
57 // Check for the proper summary.
|
Chris@0
|
58 $this->assertEqual('The node bundle is page', $condition->summary());
|
Chris@0
|
59
|
Chris@0
|
60 // Set the node type check to page or article.
|
Chris@0
|
61 $condition->setConfig('bundles', ['page' => 'page', 'article' => 'article']);
|
Chris@0
|
62 $this->assertTrue($condition->execute(), 'Page type nodes pass node type checks for pages or articles');
|
Chris@0
|
63 // Check for the proper summary.
|
Chris@0
|
64 $this->assertEqual('The node bundle is page or article', $condition->summary());
|
Chris@0
|
65
|
Chris@0
|
66 // Set the context to the article node.
|
Chris@0
|
67 $condition->setContextValue('node', $article);
|
Chris@0
|
68 $this->assertTrue($condition->execute(), 'Article type nodes pass node type checks for pages or articles');
|
Chris@0
|
69
|
Chris@0
|
70 // Set the context to the test node.
|
Chris@0
|
71 $condition->setContextValue('node', $test);
|
Chris@0
|
72 $this->assertFalse($condition->execute(), 'Test type nodes pass node type checks for pages or articles');
|
Chris@0
|
73
|
Chris@0
|
74 // Check a greater than 2 bundles summary scenario.
|
Chris@0
|
75 $condition->setConfig('bundles', ['page' => 'page', 'article' => 'article', 'test' => 'test']);
|
Chris@0
|
76 $this->assertEqual('The node bundle is page, article or test', $condition->summary());
|
Chris@0
|
77
|
Chris@0
|
78 // Test Constructor injection.
|
Chris@0
|
79 $condition = $manager->createInstance('node_type', ['bundles' => ['article' => 'article'], 'context' => ['node' => $article]]);
|
Chris@0
|
80 $this->assertTrue($condition->execute(), 'Constructor injection of context and configuration working as anticipated.');
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 }
|