$type = NodeType::
create(['type' => 'page', 'name' => 'Page'
]);
$type->
save();
$name =
$this->
randomMachineName();
$manager =
new MockBlockManager();
$plugin =
$manager->
createInstance('user_name'
);
// Create a node, add it as context, catch the exception.
$node = Node::
create(['type' => 'page', 'title' =>
$name]);
// Try to get context that is missing its definition.
try { $plugin->
getContextDefinition('not_exists'
);
$this->
fail('The user context should not yet be set.'
);
} catch (ContextException
$e) { $this->
assertEquals('The not_exists context is not a valid context.',
$e->
getMessage());
} // Test the getContextDefinitions() method.
$user_context_definition = EntityContextDefinition::
fromEntityTypeId('user'
)->
setLabel('User'
);
$this->
assertEquals($plugin->
getContextDefinitions()['user'
]->
getLabel(),
$user_context_definition->
getLabel());
// Test the getContextDefinition() method for a valid context.