diff --git a/core/modules/block/lib/Drupal/block/Plugin/Core/Entity/Block.php b/core/modules/block/lib/Drupal/block/Plugin/Core/Entity/Block.php index e8ccc63..0ff8ecc 100644 --- a/core/modules/block/lib/Drupal/block/Plugin/Core/Entity/Block.php +++ b/core/modules/block/lib/Drupal/block/Plugin/Core/Entity/Block.php @@ -74,9 +74,11 @@ class Block extends ConfigEntityBase { /** * The region this block is placed in. * + * The 'disabled' region is represented by -1. + * * @var string */ - protected $region; + protected $region = -1; /** * Settings to control the block visibility. @@ -135,6 +137,12 @@ class Block extends ConfigEntityBase { */ public function getPlugin() { if (!$this->instance) { + // Throw an exception if no plugin string was provided. + if (!$this->plugin) { + throw new PluginException(format_string("The block '@block' did not specify a plugin.", array('@block' => $this->id()))); + } + + // Create an instance of the plugin and store its configuration. try { $this->instance = drupal_container()->get('plugin.manager.block')->createInstance($this->plugin, $this->configuration); $this->configuration += $this->instance->getConfig(); @@ -142,7 +150,7 @@ public function getPlugin() { catch (PluginException $e) { // Ignore blocks belonging to disabled modules, but re-throw valid // exceptions when the module is enabled and the plugin is misconfigured. - if (module_exists($this->module)) { + if (empty($this->module) || module_exists($this->module)) { throw $e; } } diff --git a/core/modules/block/lib/Drupal/block/Tests/BlockStorageUnitTest.php b/core/modules/block/lib/Drupal/block/Tests/BlockStorageUnitTest.php new file mode 100644 index 0000000..b1cc516 --- /dev/null +++ b/core/modules/block/lib/Drupal/block/Tests/BlockStorageUnitTest.php @@ -0,0 +1,208 @@ + 'Block storage', + 'description' => 'Tests the storage of blocks.', + 'group' => 'Block' + ); + } + + protected function setUp() { + parent::setUp(); + + $this->enableModules(array('block')); + $this->controller = entity_get_controller('block'); + } + + /** + * Tests CRUD operations. + */ + public function testBlockCRUD() { + $this->assertTrue($this->controller instanceof BlockStorageController, 'The block storage controller is loaded.'); + + // Run each test method in the same installation. + $this->createTests(); + $this->loadTests(); + $this->renderTests(); + $this->deleteTests(); + } + + /** + * Tests the creation of blocks. + */ + protected function createTests() { + // Attempt to create a block without a plugin. + try { + $entity = $this->controller->create(array()); + $this->fail('A block without a plugin was created with no exception thrown.'); + } + catch (PluginException $e) { + $this->assertEqual('The block \'\' did not specify a plugin.', $e->getMessage(), 'An exception was thrown when a block was created without a plugin.'); + } + + // Create a block with only required values. + $entity = $this->controller->create(array( + 'id' => 'stark.powered', + 'plugin' => 'system_powered_by_block', + )); + $entity->save(); + + $this->assertTrue($entity instanceof Block, 'The newly created entity is a Block.'); + + // Verify all of the block properties. + $actual_properties = config('plugin.core.block.stark.powered')->get(); + $this->assertTrue(!empty($actual_properties['uuid']), 'The block UUID is set.'); + unset($actual_properties['uuid']); + + // Ensure that default values are filled in. + $expected_properties = array( + 'id' => 'stark.powered', + 'label' => '', + 'region' => '-1', + 'weight' => '', + 'cache' => '-1', + 'module' => 'system', + 'theme' => 'stark', + 'status' => '1', + 'visibility' => array(), + 'plugin' => 'system_powered_by_block', + 'configuration' => array( + 'subject' => t('Powered by Drupal'), + ), + ); + $this->assertIdentical($actual_properties, $expected_properties, 'The block properties are exported correctly.'); + + $this->assertTrue($entity->getPlugin() instanceof SystemPoweredByBlock, 'The entity has an instance of the correct block plugin.'); + } + + /** + * Tests the rendering of blocks. + */ + protected function loadTests() { + $entities = $this->controller->load(array('stark.powered')); + $entity = reset($entities); + + $this->assertTrue($entity instanceof Block, 'The loaded entity is a Block.'); + + // Verify several properties of the block. + $this->assertEqual($entity->get('region'), '-1'); + $this->assertTrue($entity->get('status')); + $this->assertEqual($entity->get('theme'), 'stark'); + $this->assertEqual($entity->get('module'), 'system'); + $this->assertTrue($entity->uuid()); + } + + /** + * Tests the rendering of blocks. + */ + protected function renderTests() { + $entity = $this->controller->create(array( + 'id' => 'stark.powered', + 'plugin' => 'system_powered_by_block', + )); + + // Test the rendering of a block. + $output = entity_view($entity, 'block'); + $expected = array(); + $expected[] = '
'; + $expected[] = ''; + $expected[] = '

Powered by Drupal

'; + $expected[] = ' '; + $expected[] = '
'; + $expected[] = ' Powered by Drupal
'; + $expected[] = '
'; + $expected[] = ''; + $expected_output = implode("\n", $expected); + $this->assertEqual(render($output), $expected_output, 'The block rendered correctly.'); + + // Reset the HTML IDs so that the next render is not affected. + drupal_static_reset('drupal_html_id'); + + // Test the rendering of a block with a given title. + $entity->set('label', 'Powered by Bananas'); + $output = entity_view($entity, 'block'); + $expected = array(); + $expected[] = '
'; + $expected[] = ''; + $expected[] = '

Powered by Bananas

'; + $expected[] = ' '; + $expected[] = '
'; + $expected[] = ' Powered by Drupal
'; + $expected[] = '
'; + $expected[] = ''; + $expected_output = implode("\n", $expected); + $this->assertEqual(render($output), $expected_output, 'The block rendered correctly.'); + } + + /** + * Tests the deleting of blocks. + */ + protected function deleteTests() { + $entities = $this->controller->load(array('stark.powered')); + $entity = reset($entities); + + // Ensure that the storage isn't currently empty. + $config_storage = $this->container->get('config.storage'); + $config = $config_storage->listAll('plugin.core.block.'); + $this->assertFalse(empty($config), 'There are blocks in config storage.'); + + // Delete the block. + $entity->delete(); + + // Ensure that the storage is now empty. + $config = $config_storage->listAll('plugin.core.block.'); + $this->assertTrue(empty($config), 'There are no blocks in config storage.'); + } + + /** + * Tests the installation of default blocks. + */ + public function testDefaultBlocks() { + $entities = $this->controller->load(); + $this->assertTrue(empty($entities), 'There are no blocks initially.'); + + // Enable the Help module, which provides a default block. + $this->enableModules(array('help')); + + $entities = $this->controller->load(); + $entity = reset($entities); + $this->assertEqual($entity->id(), 'seven.help', 'The default help block was loaded.'); + } + +}