diff --git a/core/lib/Drupal/Component/Plugin/Context/Context.php b/core/lib/Drupal/Component/Plugin/Context/Context.php index b377c75..ce8ccf4 100644 --- a/core/lib/Drupal/Component/Plugin/Context/Context.php +++ b/core/lib/Drupal/Component/Plugin/Context/Context.php @@ -22,7 +22,7 @@ class Context { protected $context; /** - * The PSR-0 class or an array definition to which context should conform. + * The class name or an array definition to which a context must conform. * * @var mixed * A string or array. diff --git a/core/modules/system/lib/Drupal/system/Tests/Plugin/ContextPluginTest.php b/core/modules/system/lib/Drupal/system/Tests/Plugin/ContextPluginTest.php index b08aadb..19a53f9 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Plugin/ContextPluginTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Plugin/ContextPluginTest.php @@ -7,16 +7,17 @@ namespace Drupal\system\Tests\Plugin; -use Drupal\simpletest\WebTestBase; +use Drupal\simpletest\UnitTestBase; use Drupal\plugin_test\Plugin\MockBlockManager; +use Drupal\plugin_test\MockNode; +use Drupal\plugin_test\MockUser; +use Drupal\Component\Plugin\Exception\PluginException; use Drupal\Component\Plugin\Exception\ContextException; /** * Tests that derivative plugins are correctly discovered. */ -class ContextPluginTest extends WebTestBase { - - public static $modules = array('user', 'node'); +class ContextPluginTest extends UnitTestBase { public static function getInfo() { return array( @@ -34,15 +35,60 @@ function testContext() { $manager = new MockBlockManager(); $plugin = $manager->createInstance('user_name'); // Create a node, add it as context, catch the exception. - $node = entity_create('node', array('title' => $name)); + $node = new MockNode(array('title' => $name)); + + // Try to get a valid context that has not been set. + try { + $plugin->getContext('user'); + } + catch (PluginException $e) { + $this->assertEqual($e->getMessage(), 'The user context is not yet set.'); + } + + // Try to call a method of the plugin that requires context before it has + // been set. + try { + $plugin->getTitle(); + } + catch (PluginException $e) { + $this->assertEqual($e->getMessage(), 'The user context is not yet set.'); + } + + // Try to get a context that is not valid. + try { + $plugin->getContext('node'); + } + catch (PluginException $e) { + $this->assertEqual($e->getMessage(), 'The node context is not a valid context.'); + } + + // Try to pass the wrong class type as a context. try { $plugin->setContext('user', $node); } catch (ContextException $e) { - $this->assertEqual($e->getMessage(), 'The context passed was not an instance of Drupal\user\Plugin\Core\Entity\User.'); + $this->assertEqual($e->getMessage(), 'The context passed was not an instance of Drupal\plugin_test\MockUser.'); } - $user = entity_create('user', array('name' => $name)); + + // Set an appropriate context appropriately and check to make sure its + // methods work as expected. + $user = new MockUser(array('name' => $name)); $plugin->setContext('user', $user); $this->assertEqual($user->label(), $plugin->getTitle()); + + // Test the getContextDefinitions() method. + $this->assertIdentical($plugin->getContextDefinitions(), array('user' => 'Drupal\plugin_test\MockUser')); + + // Test the getContextDefinition() method for a valid context. + $this->assertEqual($plugin->getContextDefinition('user'), 'Drupal\plugin_test\MockUser'); + + // Test the getContextDefinition() method for an invalid context. + try { + $plugin->getContextDefinition('node'); + } + catch (PluginException $e) { + $this->assertEqual($e->getMessage(), 'The node context is not a valid context.'); + } + } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Plugin/PluginTestBase.php b/core/modules/system/lib/Drupal/system/Tests/Plugin/PluginTestBase.php index b62878e..f80daf8 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Plugin/PluginTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/Plugin/PluginTestBase.php @@ -74,7 +74,7 @@ public function setUp() { 'label' => 'User Name', 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserNameBlock', 'context' => array( - 'user' => 'Drupal\user\Plugin\Core\Entity\User' + 'user' => 'Drupal\plugin_test\MockUser' ), ), ); diff --git a/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/MockEntity.php b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/MockEntity.php new file mode 100644 index 0000000..941330d --- /dev/null +++ b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/MockEntity.php @@ -0,0 +1,26 @@ +title; + } +} diff --git a/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/MockNode.php b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/MockNode.php new file mode 100644 index 0000000..fba6a65 --- /dev/null +++ b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/MockNode.php @@ -0,0 +1,18 @@ +title = isset($values['title']) ? $values['title'] : ''; + } +} diff --git a/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/MockUser.php b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/MockUser.php new file mode 100644 index 0000000..374ec95 --- /dev/null +++ b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/MockUser.php @@ -0,0 +1,18 @@ +title = isset($values['name']) ? $values['name'] : ''; + } +} diff --git a/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/MockBlockManager.php b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/MockBlockManager.php index 7f65b0c..2ba5dd9 100644 --- a/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/MockBlockManager.php +++ b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/MockBlockManager.php @@ -72,7 +72,7 @@ public function __construct() { 'label' => t('User Name'), 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserNameBlock', 'context' => array( - 'user' => 'Drupal\user\Plugin\Core\Entity\User' + 'user' => 'Drupal\plugin_test\MockUser' ), ));