// Try to get a value of a valid context, while this value has not been set.
try { $plugin->
getContextValue('user'
);
} catch (ContextException
$e) { $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.'
);
} // Try to pass the wrong class type as a context value.
$plugin->
setContextValue('user',
$node);
$violations =
$plugin->
validateContexts();
$this->
assertNotEmpty($violations, 'The provided context value does not pass validation.'
);
// Set an appropriate context value and check to make sure its methods work
// as expected.
$user = User::
create(['name' =>
$name]);
$plugin->
setContextValue('user',
$user);
$this->
assertEquals($user->
getAccountName(),
$plugin->
getContextValue('user'
)->
getAccountName());
$this->
assertEquals($user->
label(),
$plugin->
getTitle());
// Test Optional context handling.