3 namespace Drupal\Tests\node\Kernel;
5 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
6 use Drupal\node\Entity\Node;
7 use Drupal\node\Entity\NodeType;
10 * Tests that conditions, provided by the node module, are working properly.
14 class NodeConditionTest extends EntityKernelTestBase {
16 public static $modules = ['node'];
18 protected function setUp() {
21 // Create the node bundles required for testing.
22 $type = NodeType::create(['type' => 'page', 'name' => 'page']);
24 $type = NodeType::create(['type' => 'article', 'name' => 'article']);
26 $type = NodeType::create(['type' => 'test', 'name' => 'test']);
33 public function testConditions() {
34 $manager = $this->container->get('plugin.manager.condition', $this->container->get('container.namespaces'));
37 // Get some nodes of various types to check against.
38 $page = Node::create(['type' => 'page', 'title' => $this->randomMachineName(), 'uid' => 1]);
40 $article = Node::create(['type' => 'article', 'title' => $this->randomMachineName(), 'uid' => 1]);
42 $test = Node::create(['type' => 'test', 'title' => $this->randomMachineName(), 'uid' => 1]);
45 // Grab the node type condition and configure it to check against node type
46 // of 'article' and set the context to the page type node.
47 $condition = $manager->createInstance('node_type')
48 ->setConfig('bundles', ['article' => 'article'])
49 ->setContextValue('node', $page);
50 $this->assertFalse($condition->execute(), 'Page type nodes fail node type checks for articles.');
51 // Check for the proper summary.
52 $this->assertEqual('The node bundle is article', $condition->summary());
54 // Set the node type check to page.
55 $condition->setConfig('bundles', ['page' => 'page']);
56 $this->assertTrue($condition->execute(), 'Page type nodes pass node type checks for pages');
57 // Check for the proper summary.
58 $this->assertEqual('The node bundle is page', $condition->summary());
60 // Set the node type check to page or article.
61 $condition->setConfig('bundles', ['page' => 'page', 'article' => 'article']);
62 $this->assertTrue($condition->execute(), 'Page type nodes pass node type checks for pages or articles');
63 // Check for the proper summary.
64 $this->assertEqual('The node bundle is page or article', $condition->summary());
66 // Set the context to the article node.
67 $condition->setContextValue('node', $article);
68 $this->assertTrue($condition->execute(), 'Article type nodes pass node type checks for pages or articles');
70 // Set the context to the test node.
71 $condition->setContextValue('node', $test);
72 $this->assertFalse($condition->execute(), 'Test type nodes pass node type checks for pages or articles');
74 // Check a greater than 2 bundles summary scenario.
75 $condition->setConfig('bundles', ['page' => 'page', 'article' => 'article', 'test' => 'test']);
76 $this->assertEqual('The node bundle is page, article or test', $condition->summary());
78 // Test Constructor injection.
79 $condition = $manager->createInstance('node_type', ['bundles' => ['article' => 'article'], 'context' => ['node' => $article]]);
80 $this->assertTrue($condition->execute(), 'Constructor injection of context and configuration working as anticipated.');