3 namespace Drupal\KernelTests\Core\Plugin;
5 use Drupal\Component\Plugin\Exception\ContextException;
6 use Drupal\Core\Plugin\Context\EntityContextDefinition;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\node\Entity\Node;
9 use Drupal\node\Entity\NodeType;
10 use Drupal\plugin_test\Plugin\MockBlockManager;
11 use Drupal\user\Entity\User;
14 * Tests that contexts are properly set and working within plugins.
18 class ContextPluginTest extends KernelTestBase {
20 public static $modules = ['system', 'user', 'node', 'field', 'filter', 'text'];
23 * Tests basic context definition and value getters and setters.
25 public function testContext() {
26 $this->installEntitySchema('user');
27 $this->installEntitySchema('node');
28 $this->installEntitySchema('node_type');
29 $type = NodeType::create(['type' => 'page', 'name' => 'Page']);
32 $name = $this->randomMachineName();
33 $manager = new MockBlockManager();
34 $plugin = $manager->createInstance('user_name');
35 // Create a node, add it as context, catch the exception.
36 $node = Node::create(['type' => 'page', 'title' => $name]);
38 // Try to get context that is missing its definition.
40 $plugin->getContextDefinition('not_exists');
41 $this->fail('The user context should not yet be set.');
43 catch (ContextException $e) {
44 $this->assertEqual($e->getMessage(), 'The not_exists context is not a valid context.');
47 // Test the getContextDefinitions() method.
48 $user_context_definition = EntityContextDefinition::fromEntityTypeId('user')->setLabel(t('User'));
49 $this->assertEqual($plugin->getContextDefinitions()['user']->getLabel(), $user_context_definition->getLabel());
51 // Test the getContextDefinition() method for a valid context.
52 $this->assertEqual($plugin->getContextDefinition('user')->getLabel(), $user_context_definition->getLabel());
54 // Try to get a context with valid definition.
55 $this->assertNotNull($plugin->getContext('user'), 'Succeeded to get a context with a valid definition.');
57 // Try to get a value of a valid context, while this value has not been set.
59 $plugin->getContextValue('user');
61 catch (ContextException $e) {
62 $this->assertSame("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.');
65 // Try to pass the wrong class type as a context value.
66 $plugin->setContextValue('user', $node);
67 $violations = $plugin->validateContexts();
68 $this->assertTrue(!empty($violations), 'The provided context value does not pass validation.');
70 // Set an appropriate context value and check to make sure its methods work
72 $user = User::create(['name' => $name]);
73 $plugin->setContextValue('user', $user);
75 $this->assertEqual($plugin->getContextValue('user')->getUsername(), $user->getUsername());
76 $this->assertEqual($user->label(), $plugin->getTitle());
78 // Test Optional context handling.
79 $plugin = $manager->createInstance('user_name_optional');
80 $this->assertNull($plugin->getContextValue('user'), 'Requesting a non-set value of a valid context should return NULL.');
82 // Test Complex compound context handling.
83 $complex_plugin = $manager->createInstance('complex_context');
84 $complex_plugin->setContextValue('user', $user);
86 // With only the user context set, try to get the context values.
87 $values = $complex_plugin->getContextValues();
88 $this->assertNull($values['node'], 'The node context is not yet set.');
89 $this->assertNotNull($values['user'], 'The user context is set');
91 $complex_plugin->setContextValue('node', $node);
92 $context_wrappers = $complex_plugin->getContexts();
93 // Make sure what came out of the wrappers is good.
94 $this->assertEqual($context_wrappers['user']->getContextValue()->label(), $user->label());
95 $this->assertEqual($context_wrappers['node']->getContextValue()->label(), $node->label());
97 // Make sure what comes out of the context values is good.
98 $contexts = $complex_plugin->getContextValues();
99 $this->assertEqual($contexts['user']->label(), $user->label());
100 $this->assertEqual($contexts['node']->label(), $node->label());
102 // Test the title method for the complex context plugin.
103 $this->assertEqual($user->label() . ' -- ' . $node->label(), $complex_plugin->getTitle());