View source
<?php
namespace Drupal\node\Tests\Condition;
use Drupal\system\Tests\Entity\EntityUnitTestBase;
class NodeConditionTest extends EntityUnitTestBase {
public static $modules = array(
'node',
);
protected function setUp() {
parent::setUp();
$type = entity_create('node_type', array(
'type' => 'page',
'name' => 'page',
));
$type
->save();
$type = entity_create('node_type', array(
'type' => 'article',
'name' => 'article',
));
$type
->save();
$type = entity_create('node_type', array(
'type' => 'test',
'name' => 'test',
));
$type
->save();
}
function testConditions() {
$manager = $this->container
->get('plugin.manager.condition', $this->container
->get('container.namespaces'));
$this
->createUser();
$page = entity_create('node', array(
'type' => 'page',
'title' => $this
->randomMachineName(),
'uid' => 1,
));
$page
->save();
$article = entity_create('node', array(
'type' => 'article',
'title' => $this
->randomMachineName(),
'uid' => 1,
));
$article
->save();
$test = entity_create('node', array(
'type' => 'test',
'title' => $this
->randomMachineName(),
'uid' => 1,
));
$test
->save();
$condition = $manager
->createInstance('node_type')
->setConfig('bundles', array(
'article' => 'article',
))
->setContextValue('node', $page);
$this
->assertFalse($condition
->execute(), 'Page type nodes fail node type checks for articles.');
$this
->assertEqual('The node bundle is article', $condition
->summary());
$condition
->setConfig('bundles', array(
'page' => 'page',
));
$this
->assertTrue($condition
->execute(), 'Page type nodes pass node type checks for pages');
$this
->assertEqual('The node bundle is page', $condition
->summary());
$condition
->setConfig('bundles', array(
'page' => 'page',
'article' => 'article',
));
$this
->assertTrue($condition
->execute(), 'Page type nodes pass node type checks for pages or articles');
$this
->assertEqual('The node bundle is page or article', $condition
->summary());
$condition
->setContextValue('node', $article);
$this
->assertTrue($condition
->execute(), 'Article type nodes pass node type checks for pages or articles');
$condition
->setContextValue('node', $test);
$this
->assertFalse($condition
->execute(), 'Test type nodes pass node type checks for pages or articles');
$condition
->setConfig('bundles', array(
'page' => 'page',
'article' => 'article',
'test' => 'test',
));
$this
->assertEqual('The node bundle is page, article or test', $condition
->summary());
$condition = $manager
->createInstance('node_type', array(
'bundles' => array(
'article' => 'article',
),
'context' => array(
'node' => $article,
),
));
$this
->assertTrue($condition
->execute(), 'Constructor injection of context and configuration working as anticipated.');
}
}