NodeConditionTest.php 3.34 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\condition\Tests\Condition\NodeConditionTest.
 */

namespace Drupal\node\Tests\Condition;

10
use Drupal\system\Tests\Entity\EntityUnitTestBase;
11 12 13 14

/**
 * Tests the node conditions.
 */
15
class NodeConditionTest extends EntityUnitTestBase {
16

17
  public static $modules = array('node');
18 19 20 21 22 23 24 25 26

  public static function getInfo() {
    return array(
      'name' => 'Node Condition Plugins',
      'description' => 'Tests that conditions, provided by the node module, are working properly.',
      'group' => 'Condition API',
    );
  }

27
  public function setUp() {
28
    parent::setUp();
29
    $this->installSchema('node', array('node', 'node_field_data', 'node_field_revision'));
30 31 32 33 34 35
  }

  /**
   * Tests conditions.
   */
  function testConditions() {
36
    $manager = $this->container->get('plugin.manager.condition', $this->container->get('container.namespaces'));
37
    $this->createUser();
38 39

    // Get some nodes of various types to check against.
40
    $page = entity_create('node', array('type' => 'page', 'title' => $this->randomName(), 'uid' => 1));
41
    $page->save();
42
    $article = entity_create('node', array('type' => 'article', 'title' => $this->randomName(), 'uid' => 1));
43
    $article->save();
44
    $test = entity_create('node', array('type' => 'test', 'title' => $this->randomName(), 'uid' => 1));
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    $test->save();

    // Grab the node type condition and configure it to check against node type
    // of 'article' and set the context to the page type node.
    $condition = $manager->createInstance('node_type')
      ->setConfig('bundles', array('article'))
      ->setContextValue('node', $page);
    $this->assertFalse($condition->execute(), 'Page type nodes fail node type checks for articles.');
    // Check for the proper summary.
    $this->assertEqual('The node bundle is article', $condition->summary());

    // Set the node type check to page.
    $condition->setConfig('bundles', array('page'));
    $this->assertTrue($condition->execute(), 'Page type nodes pass node type checks for pages');
    // Check for the proper summary.
    $this->assertEqual('The node bundle is page', $condition->summary());

    // Set the node type check to page or article.
    $condition->setConfig('bundles', array('page', 'article'));
    $this->assertTrue($condition->execute(), 'Page type nodes pass node type checks for pages or articles');
    // Check for the proper summary.
    $this->assertEqual('The node bundle is page or article', $condition->summary());

    // Set the context to the article node.
    $condition->setContextValue('node', $article);
    $this->assertTrue($condition->execute(), 'Article type nodes pass node type checks for pages or articles');

    // Set the context to the test node.
    $condition->setContextValue('node', $test);
    $this->assertFalse($condition->execute(), 'Test type nodes pass node type checks for pages or articles');

    // Check a greater than 2 bundles summary scenario.
    $condition->setConfig('bundles', array('page', 'article', 'test'));
    $this->assertEqual('The node bundle is page, article or test', $condition->summary());

    // Test Constructor injection.
    $condition = $manager->createInstance('node_type', array('bundles' => array('article'), 'context' => array('node' => $article)));
    $this->assertTrue($condition->execute(), 'Constructor injection of context and configuration working as anticipated.');
  }
}