8a1fa4842f9d99dcced02d1c6239d684f346ad8e
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Plugin / ContextPluginTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Plugin;
4
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;
12
13 /**
14  * Tests that contexts are properly set and working within plugins.
15  *
16  * @group Plugin
17  */
18 class ContextPluginTest extends KernelTestBase {
19
20   public static $modules = ['system', 'user', 'node', 'field', 'filter', 'text'];
21
22   /**
23    * Tests basic context definition and value getters and setters.
24    */
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']);
30     $type->save();
31
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]);
37
38     // Try to get context that is missing its definition.
39     try {
40       $plugin->getContextDefinition('not_exists');
41       $this->fail('The user context should not yet be set.');
42     }
43     catch (ContextException $e) {
44       $this->assertEqual($e->getMessage(), 'The not_exists context is not a valid context.');
45     }
46
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());
50
51     // Test the getContextDefinition() method for a valid context.
52     $this->assertEqual($plugin->getContextDefinition('user')->getLabel(), $user_context_definition->getLabel());
53
54     // Try to get a context with valid definition.
55     $this->assertNotNull($plugin->getContext('user'), 'Succeeded to get a context with a valid definition.');
56
57     // Try to get a value of a valid context, while this value has not been set.
58     try {
59       $plugin->getContextValue('user');
60     }
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.');
63     }
64
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.');
69
70     // Set an appropriate context value and check to make sure its methods work
71     // as expected.
72     $user = User::create(['name' => $name]);
73     $plugin->setContextValue('user', $user);
74
75     $this->assertEqual($plugin->getContextValue('user')->getUsername(), $user->getUsername());
76     $this->assertEqual($user->label(), $plugin->getTitle());
77
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.');
81
82     // Test Complex compound context handling.
83     $complex_plugin = $manager->createInstance('complex_context');
84     $complex_plugin->setContextValue('user', $user);
85
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');
90
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());
96
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());
101
102     // Test the title method for the complex context plugin.
103     $this->assertEqual($user->label() . ' -- ' . $node->label(), $complex_plugin->getTitle());
104   }
105
106 }