1<?php 2 3namespace Drupal\Tests\node\Kernel; 4 5use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; 6use Drupal\node\Entity\Node; 7use Drupal\node\Entity\NodeType; 8 9/** 10 * Tests that conditions, provided by the node module, are working properly. 11 * 12 * @group node 13 */ 14class NodeConditionTest extends EntityKernelTestBase { 15 16 public static $modules = ['node']; 17 18 protected function setUp() { 19 parent::setUp(); 20 21 // Create the node bundles required for testing. 22 $type = NodeType::create(['type' => 'page', 'name' => 'page']); 23 $type->save(); 24 $type = NodeType::create(['type' => 'article', 'name' => 'article']); 25 $type->save(); 26 $type = NodeType::create(['type' => 'test', 'name' => 'test']); 27 $type->save(); 28 } 29 30 /** 31 * Tests conditions. 32 */ 33 public function testConditions() { 34 $manager = $this->container->get('plugin.manager.condition'); 35 $this->createUser(); 36 37 // Get some nodes of various types to check against. 38 $page = Node::create(['type' => 'page', 'title' => $this->randomMachineName(), 'uid' => 1]); 39 $page->save(); 40 $article = Node::create(['type' => 'article', 'title' => $this->randomMachineName(), 'uid' => 1]); 41 $article->save(); 42 $test = Node::create(['type' => 'test', 'title' => $this->randomMachineName(), 'uid' => 1]); 43 $test->save(); 44 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()); 53 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()); 59 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()); 65 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'); 69 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'); 73 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()); 77 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.'); 81 } 82 83} 84