View source  
  <?php
namespace Drupal\system\Tests\Plugin;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\node\Entity\NodeType;
use Drupal\plugin_test\Plugin\MockBlockManager;
use Drupal\simpletest\KernelTestBase;
class ContextPluginTest extends KernelTestBase {
  public static $modules = array(
    'system',
    'user',
    'node',
    'field',
    'filter',
    'text',
  );
  
  function testContext() {
    $this
      ->installEntitySchema('user');
    $this
      ->installEntitySchema('node');
    $this
      ->installEntitySchema('node_type');
    $type = NodeType::create([
      'type' => 'page',
      'name' => 'Page',
    ]);
    $type
      ->save();
    $name = $this
      ->randomMachineName();
    $manager = new MockBlockManager();
    $plugin = $manager
      ->createInstance('user_name');
    
    $node = entity_create('node', array(
      'title' => $name,
      'type' => 'page',
    ));
    
    try {
      $plugin
        ->getContextDefinition('not_exists');
      $this
        ->fail('The user context should not yet be set.');
    } catch (ContextException $e) {
      $this
        ->assertEqual($e
        ->getMessage(), 'The not_exists context is not a valid context.');
    }
    
    $user_context_definition = ContextDefinition::create('entity:user')
      ->setLabel(t('User'));
    $this
      ->assertEqual($plugin
      ->getContextDefinitions()['user']
      ->getLabel(), $user_context_definition
      ->getLabel());
    
    $this
      ->assertEqual($plugin
      ->getContextDefinition('user')
      ->getLabel(), $user_context_definition
      ->getLabel());
    
    $this
      ->assertNotNull($plugin
      ->getContext('user'), 'Succeeded to get a context with a valid definition.');
    
    try {
      $plugin
        ->getContextValue('user');
    } catch (ContextException $e) {
      $this
        ->assertIdentical("The 'entity:user' context is required and not present.", $e
        ->getMessage(), 'Requesting a non-set value of a required context should throw a context exception.');
    }
    
    $plugin
      ->setContextValue('user', $node);
    $violations = $plugin
      ->validateContexts();
    $this
      ->assertTrue(!empty($violations), 'The provided context value does not pass validation.');
    
    $user = entity_create('user', array(
      'name' => $name,
    ));
    $plugin
      ->setContextValue('user', $user);
    $this
      ->assertEqual($plugin
      ->getContextValue('user')
      ->getUsername(), $user
      ->getUsername());
    $this
      ->assertEqual($user
      ->label(), $plugin
      ->getTitle());
    
    $plugin = $manager
      ->createInstance('user_name_optional');
    $this
      ->assertNull($plugin
      ->getContextValue('user'), 'Requesting a non-set value of a valid context should return NULL.');
    
    $complex_plugin = $manager
      ->createInstance('complex_context');
    $complex_plugin
      ->setContextValue('user', $user);
    
    $values = $complex_plugin
      ->getContextValues();
    $this
      ->assertNull($values['node'], 'The node context is not yet set.');
    $this
      ->assertNotNull($values['user'], 'The user context is set');
    $complex_plugin
      ->setContextValue('node', $node);
    $context_wrappers = $complex_plugin
      ->getContexts();
    
    $this
      ->assertEqual($context_wrappers['user']
      ->getContextValue()
      ->label(), $user
      ->label());
    $this
      ->assertEqual($context_wrappers['node']
      ->getContextValue()
      ->label(), $node
      ->label());
    
    $contexts = $complex_plugin
      ->getContextValues();
    $this
      ->assertEqual($contexts['user']
      ->label(), $user
      ->label());
    $this
      ->assertEqual($contexts['node']
      ->label(), $node
      ->label());
    
    $this
      ->assertEqual($user
      ->label() . ' -- ' . $node
      ->label(), $complex_plugin
      ->getTitle());
  }
}