Issue #2043527 by tim.plunkett, vijaycs85, dawehner: Fixed Theme name is included in block machine name but should be stored as a key instead.

8.0.x
Alex Pott 2013-10-16 01:51:01 +01:00
parent 3f38e917a5
commit 9fdc6a9c35
45 changed files with 157 additions and 237 deletions

View File

@ -279,14 +279,12 @@ function _block_get_renderable_region($list = array()) {
}
}
else {
$key_components = explode('.', $key);
$id = array_pop($key_components);
$build[$key] = array(
'#block' => $block,
'#weight' => $block->get('weight'),
'#pre_render' => array('_block_get_renderable_block'),
'#cache' => array(
'keys' => array($id, $settings['module']),
'keys' => array($key, $settings['module']),
'granularity' => $settings['cache'],
'bin' => 'block',
'tags' => array('content' => TRUE),
@ -383,9 +381,15 @@ function block_theme_initialize($theme) {
$regions = system_region_list($theme, REGIONS_VISIBLE);
$default_theme_blocks = entity_load_multiple_by_properties('block', array('theme' => $default_theme));
foreach ($default_theme_blocks as $default_theme_block_id => $default_theme_block) {
list(, $machine_name) = explode('.', $default_theme_block_id);
if (strpos($default_theme_block_id, $default_theme . '_') === 0) {
$id = str_replace($default_theme, $theme, $default_theme_block_id);
}
else {
$id = $theme . '_' . $default_theme_block_id;
}
$block = $default_theme_block->createDuplicate();
$block->set('id', $theme . '.' . $machine_name);
$block->set('id', $id);
$block->set('theme', $theme);
// If the region isn't supported by the theme, assign the block to the
// theme's default region.
if (!isset($regions[$block->get('region')])) {
@ -559,9 +563,7 @@ function template_preprocess_block(&$variables) {
// Create a valid HTML ID and make sure it is unique.
if ($id = $variables['elements']['#block']->id()) {
$config_id = explode('.', $id);
$machine_name = array_pop($config_id);
$variables['attributes']['id'] = drupal_html_id('block-' . $machine_name);
$variables['attributes']['id'] = drupal_html_id('block-' . $id);
}
}

View File

@ -149,7 +149,7 @@ class CustomBlockCreationTest extends CustomBlockTestBase {
// Place the block.
$instance = array(
'machine_name' => drupal_strtolower($edit['info']),
'id' => drupal_strtolower($edit['info']),
'settings[label]' => $edit['info'],
'region' => 'sidebar_first',
);

View File

@ -104,7 +104,7 @@ class CustomBlockFieldTest extends CustomBlockTestBase {
$url = 'admin/structure/block/add/custom_block:' . $block->uuid() . '/' . \Drupal::config('system.theme')->get('default');
// Place the block.
$instance = array(
'machine_name' => drupal_strtolower($edit['info']),
'id' => drupal_strtolower($edit['info']),
'settings[label]' => $edit['info'],
'region' => 'sidebar_first',
);

View File

@ -186,7 +186,7 @@ class CustomBlockTypeTest extends CustomBlockTestBase {
$destination = 'admin/structure/block/add/custom_block:' . $block->uuid() . '/' . $theme;
$this->assertUrl(url($destination, array('absolute' => TRUE)));
$this->drupalPostForm(NULL, array(), t('Save block'));
$this->assertUrl(url("admin/structure/block/list/$theme", array('absolute' => TRUE, 'query' => array('block-placement' => drupal_html_class($theme . '.' . $edit['info'])))));
$this->assertUrl(url("admin/structure/block/list/$theme", array('absolute' => TRUE, 'query' => array('block-placement' => drupal_html_class($edit['info'])))));
}
else {
$this->fail('Could not load created block.');

View File

@ -93,14 +93,10 @@ class BlockFormController extends EntityFormController {
public function form(array $form, array &$form_state) {
$entity = $this->entity;
$form['#tree'] = TRUE;
$form['id'] = array(
'#type' => 'value',
'#value' => $entity->id(),
);
$form['settings'] = $entity->getPlugin()->buildConfigurationForm(array(), $form_state);
// If creating a new block, calculate a safe default machine name.
$form['machine_name'] = array(
$form['id'] = array(
'#type' => 'machine_name',
'#maxlength' => 64,
'#description' => $this->t('A unique name for this block instance. Must be alpha-numeric and underscore separated.'),
@ -294,15 +290,6 @@ class BlockFormController extends EntityFormController {
public function validate(array $form, array &$form_state) {
parent::validate($form, $form_state);
$entity = $this->entity;
if ($entity->isNew()) {
form_set_value($form['id'], $form_state['values']['theme'] . '.' . $form_state['values']['machine_name'], $form_state);
}
if (!empty($form['machine_name']['#disabled'])) {
// Get machine name from original value (without prepended theme name).
$config_id = explode('.', $form_state['values']['machine_name']);
$form_state['values']['machine_name'] = array_pop($config_id);
}
// Remove empty lines from the role visibility list.
$form_state['values']['visibility']['role']['roles'] = array_filter($form_state['values']['visibility']['role']['roles']);
// The Block Entity form puts all block plugin form elements in the
@ -311,7 +298,7 @@ class BlockFormController extends EntityFormController {
'values' => &$form_state['values']['settings']
);
// Call the plugin validate handler.
$entity->getPlugin()->validateConfigurationForm($form, $settings);
$this->entity->getPlugin()->validateConfigurationForm($form, $settings);
}
/**

View File

@ -125,24 +125,13 @@ class Block extends ConfigEntityBase implements BlockInterface {
return $settings['label'];
}
/**
* {@inheritdoc}
*/
public function get($property_name) {
// The theme is stored in the entity ID.
$value = parent::get($property_name);
if ($property_name == 'theme' && !$value) {
list($value) = explode('.', $this->id());
}
return $value;
}
/**
* Overrides \Drupal\Core\Config\Entity\ConfigEntityBase::getExportProperties();
*/
public function getExportProperties() {
$properties = parent::getExportProperties();
$names = array(
'theme',
'region',
'weight',
'plugin',

View File

@ -48,13 +48,13 @@ class BlockHookOperationTest extends WebTestBase {
public function testBlockOperationAlter() {
// Add a test block, any block will do.
// Set the machine name so the test_operation link can be built later.
$block_machine_name = Unicode::strtolower($this->randomName(16));
$this->drupalPlaceBlock('system_powered_by_block', array('machine_name' => $block_machine_name));
$block_id = Unicode::strtolower($this->randomName(16));
$this->drupalPlaceBlock('system_powered_by_block', array('id' => $block_id));
// Get the Block listing.
$this->drupalGet('admin/structure/block');
$test_operation_link = 'admin/structure/block/manage/stark.' . $block_machine_name . '/test_operation';
$test_operation_link = 'admin/structure/block/manage/' . $block_id . '/test_operation';
// Test if the test_operation link is on the page.
$this->assertLinkByHref($test_operation_link);
}

View File

@ -40,7 +40,7 @@ class BlockHtmlIdTest extends WebTestBase {
// Enable our test blocks.
$this->drupalPlaceBlock('system_menu_block:tools');
$this->drupalPlaceBlock('test_html_id', array('machine_name' => 'test_id_block'));
$this->drupalPlaceBlock('test_html_id', array('id' => 'test_id_block'));
}
/**

View File

@ -62,7 +62,7 @@ class BlockLanguageTest extends WebTestBase {
// Enable a standard block and set the visibility setting for one language.
$edit = array(
'visibility[language][langcodes][en]' => TRUE,
'machine_name' => strtolower($this->randomName(8)),
'id' => strtolower($this->randomName(8)),
'region' => 'sidebar_first',
);
$this->drupalPostForm('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme, $edit, t('Save block'));
@ -99,7 +99,6 @@ class BlockLanguageTest extends WebTestBase {
),
),
),
'machine_name' => 'language_block_test',
);
$block = $this->drupalPlaceBlock('system_powered_by_block', $edit);

View File

@ -45,17 +45,17 @@ class BlockRenderOrderTest extends WebTestBase {
// Enable test blocks and place them in the same region.
$region = 'header';
$test_blocks = array(
'stark.powered' => array(
'stark_powered' => array(
'weight' => '-3',
'machine_name' => 'powered',
'id' => 'stark_powered',
),
'stark.by' => array(
'stark_by' => array(
'weight' => '3',
'machine_name' => 'by',
'id' => 'stark_by',
),
'stark.drupal' => array(
'stark_drupal' => array(
'weight' => '3',
'machine_name' => 'drupal',
'id' => 'stark_drupal',
),
);
@ -65,7 +65,7 @@ class BlockRenderOrderTest extends WebTestBase {
'label' => 'Test Block',
'region' => $region,
'weight' => $test_block['weight'],
'machine_name' => $test_block['machine_name'],
'id' => $test_block['id'],
));
}
@ -74,14 +74,13 @@ class BlockRenderOrderTest extends WebTestBase {
$controller = $this->container->get('entity.manager')->getStorageController('block');
foreach ($controller->loadMultiple() as $return_block) {
$settings = $return_block->get('settings');
$id = $return_block->get('id');
if ($return_block_weight = $return_block->get('weight')) {
$this->assertTrue($test_blocks[$id]['weight'] == $return_block_weight, 'Block weight is set as "' . $return_block_weight . '" for ' . $id . ' block.');
$position[$id] = strpos($test_content, 'block-' . $test_blocks[$id]['machine_name']);
$position[$id] = strpos($test_content, drupal_html_class('block-' . $test_blocks[$id]['id']));
}
}
$this->assertTrue($position['stark.powered'] < $position['stark.by'], 'Blocks with different weight are rendered in the correct order.');
$this->assertTrue($position['stark.drupal'] < $position['stark.by'], 'Blocks with identical weight are rendered in reverse alphabetical order.');
$this->assertTrue($position['stark_powered'] < $position['stark_by'], 'Blocks with different weight are rendered in the correct order.');
$this->assertTrue($position['stark_drupal'] < $position['stark_by'], 'Blocks with identical weight are rendered in reverse alphabetical order.');
}
}

View File

@ -76,7 +76,8 @@ class BlockStorageUnitTest extends DrupalUnitTestBase {
// Create a block with only required values.
$entity = $this->controller->create(array(
'id' => 'stark.test_block',
'id' => 'test_block',
'theme' => 'stark',
'plugin' => 'test_html_id',
));
$entity->save();
@ -84,16 +85,17 @@ class BlockStorageUnitTest extends DrupalUnitTestBase {
$this->assertTrue($entity instanceof Block, 'The newly created entity is a Block.');
// Verify all of the block properties.
$actual_properties = \Drupal::config('block.block.stark.test_block')->get();
$actual_properties = \Drupal::config('block.block.test_block')->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.test_block',
'id' => 'test_block',
'weight' => NULL,
'status' => TRUE,
'langcode' => language_default()->id,
'theme' => 'stark',
'region' => -1,
'plugin' => 'test_html_id',
'settings' => array(
@ -113,7 +115,7 @@ class BlockStorageUnitTest extends DrupalUnitTestBase {
* Tests the rendering of blocks.
*/
protected function loadTests() {
$entity = $this->controller->load('stark.test_block');
$entity = $this->controller->load('test_block');
$this->assertTrue($entity instanceof Block, 'The loaded entity is a Block.');
@ -129,7 +131,7 @@ class BlockStorageUnitTest extends DrupalUnitTestBase {
*/
protected function renderTests() {
// Test the rendering of a block.
$entity = entity_load('block', 'stark.test_block');
$entity = entity_load('block', 'test_block');
$output = entity_view($entity, 'block');
$expected = array();
$expected[] = '<div class="block block-block-test" id="block-test-block">';
@ -149,7 +151,8 @@ class BlockStorageUnitTest extends DrupalUnitTestBase {
// Test the rendering of a block with a given title.
$entity = $this->controller->create(array(
'id' => 'stark.test_block2',
'id' => 'test_block2',
'theme' => 'stark',
'plugin' => 'test_html_id',
'settings' => array(
'label' => 'Powered by Bananas',
@ -178,7 +181,7 @@ class BlockStorageUnitTest extends DrupalUnitTestBase {
* Tests the deleting of blocks.
*/
protected function deleteTests() {
$entity = $this->controller->load('stark.test_block');
$entity = $this->controller->load('test_block');
// Ensure that the storage isn't currently empty.
$config_storage = $this->container->get('config.storage');
@ -205,7 +208,7 @@ class BlockStorageUnitTest extends DrupalUnitTestBase {
$entities = $this->controller->loadMultiple();
$entity = reset($entities);
$this->assertEqual($entity->id(), 'stark.test_block', 'The default test block was loaded.');
$this->assertEqual($entity->id(), 'test_block', 'The default test block was loaded.');
}
}

View File

@ -40,7 +40,7 @@ class BlockTemplateSuggestionsUnitTest extends WebTestBase {
$block = entity_create('block', array(
'plugin' => 'system_menu_block:admin',
'region' => 'footer',
'id' => \Drupal::config('system.theme')->get('default') . '.machinename',
'id' => 'machinename',
));
$variables = array();

View File

@ -32,7 +32,7 @@ class BlockTest extends BlockTestBase {
// Enable a standard block.
$default_theme = \Drupal::config('system.theme')->get('default');
$edit = array(
'machine_name' => strtolower($this->randomName(8)),
'id' => strtolower($this->randomName(8)),
'region' => 'sidebar_first',
'settings[label]' => $title,
);
@ -73,7 +73,7 @@ class BlockTest extends BlockTestBase {
// Enable a standard block.
$default_theme = \Drupal::config('system.theme')->get('default');
$edit = array(
'machine_name' => strtolower($this->randomName(8)),
'id' => strtolower($this->randomName(8)),
'region' => 'sidebar_first',
'settings[label]' => $title,
'visibility[path][visibility]' => BLOCK_VISIBILITY_LISTED,
@ -103,15 +103,14 @@ class BlockTest extends BlockTestBase {
$block = array();
$block['id'] = 'system_powered_by_block';
$block['settings[label]'] = $this->randomName(8);
$block['machine_name'] = strtolower($this->randomName(8));
$block['theme'] = \Drupal::config('system.theme')->get('default');
$block['region'] = 'header';
// Set block title to confirm that interface works and override any custom titles.
$this->drupalPostForm('admin/structure/block/add/' . $block['id'] . '/' . $block['theme'], array('settings[label]' => $block['settings[label]'], 'machine_name' => $block['machine_name'], 'region' => $block['region']), t('Save block'));
$this->drupalPostForm('admin/structure/block/add/' . $block['id'] . '/' . $block['theme'], array('settings[label]' => $block['settings[label]'], 'id' => $block['id'], 'region' => $block['region']), t('Save block'));
$this->assertText(t('The block configuration has been saved.'), 'Block title set.');
// Check to see if the block was created by checking its configuration.
$instance = entity_load('block', $block['theme'] . '.' . $block['machine_name']);
$instance = entity_load('block', $block['id']);
$this->assertEqual($instance->label(), $block['settings[label]'], 'Stored block title found.');
@ -122,7 +121,7 @@ class BlockTest extends BlockTestBase {
// Set the block to the disabled region.
$edit = array();
$edit['blocks[' . $block['theme'] . '.' . $block['machine_name'] . '][region]'] = -1;
$edit['blocks[' . $block['id'] . '][region]'] = -1;
$this->drupalPostForm('admin/structure/block', $edit, t('Save blocks'));
// Confirm that the block is now listed as disabled.
@ -133,11 +132,11 @@ class BlockTest extends BlockTestBase {
$this->assertNoText(t($block['settings[label]']));
// Check for <div id="block-my-block-instance-name"> if the machine name
// is my_block_instance_name.
$xpath = $this->buildXPathQuery('//div[@id=:id]/*', array(':id' => 'block-' . strtr(strtolower($block['machine_name']), '-', '_')));
$xpath = $this->buildXPathQuery('//div[@id=:id]/*', array(':id' => 'block-' . str_replace('_', '-', strtolower($block['id']))));
$this->assertNoFieldByXPath($xpath, FALSE, 'Block found in no regions.');
// Test deleting the block from the edit form.
$this->drupalGet('admin/structure/block/manage/' . $block['theme'] . '.' . $block['machine_name']);
$this->drupalGet('admin/structure/block/manage/' . $block['id']);
$this->drupalPostForm(NULL, array(), t('Delete'));
$this->assertRaw(t('Are you sure you want to delete the block %name?', array('%name' => $block['settings[label]'])));
$this->drupalPostForm(NULL, array(), t('Delete'));
@ -154,17 +153,17 @@ class BlockTest extends BlockTestBase {
foreach (array('bartik', 'stark', 'seven') as $theme) {
// Select the 'Powered by Drupal' block to be placed.
$block = array();
$block['machine_name'] = strtolower($this->randomName(8));
$block['id'] = strtolower($this->randomName());
$block['theme'] = $theme;
$block['region'] = 'content';
$this->drupalPostForm('admin/structure/block/add/system_powered_by_block', $block, t('Save block'));
$this->assertText(t('The block configuration has been saved.'));
$this->assertUrl('admin/structure/block/list/' . $theme . '?block-placement=' . drupal_html_class($theme . ':' . $block['machine_name']));
$this->assertUrl('admin/structure/block/list/' . $theme . '?block-placement=' . drupal_html_class($block['id']));
// Set the default theme and ensure the block is placed.
$theme_settings->set('default', $theme)->save();
$this->drupalGet('');
$elements = $this->xpath('//div[@id = :id]', array(':id' => drupal_html_id('block-' . $block['machine_name'])));
$elements = $this->xpath('//div[@id = :id]', array(':id' => drupal_html_id('block-' . $block['id'])));
$this->assertTrue(!empty($elements), 'The block was found.');
}
}
@ -176,11 +175,11 @@ class BlockTest extends BlockTestBase {
$block_name = 'system_powered_by_block';
// Create a random title for the block.
$title = $this->randomName(8);
$machine_name = strtolower($this->randomName(8));
$id = strtolower($this->randomName(8));
// Enable a standard block.
$default_theme = \Drupal::config('system.theme')->get('default') ?: 'stark';
$edit = array(
'machine_name' => $machine_name,
'id' => $id,
'region' => 'sidebar_first',
'settings[label]' => $title,
);
@ -193,7 +192,7 @@ class BlockTest extends BlockTestBase {
$edit = array(
'settings[label_display]' => FALSE,
);
$this->drupalPostForm('admin/structure/block/manage/' . $default_theme . '.' . $machine_name, $edit, t('Save block'));
$this->drupalPostForm('admin/structure/block/manage/' . $id, $edit, t('Save block'));
$this->assertText('The block configuration has been saved.', 'Block was saved');
$this->drupalGet('user');
@ -216,7 +215,7 @@ class BlockTest extends BlockTestBase {
// Set the created block to a specific region.
$block += array('theme' => \Drupal::config('system.theme')->get('default'));
$edit = array();
$edit['blocks[' . $block['theme'] . '.' . $block['machine_name'] . '][region]'] = $region;
$edit['blocks[' . $block['id'] . '][region]'] = $region;
$this->drupalPostForm('admin/structure/block', $edit, t('Save blocks'));
// Confirm that the block was moved to the proper region.
@ -229,7 +228,7 @@ class BlockTest extends BlockTestBase {
// Confirm that the custom block was found at the proper region.
$xpath = $this->buildXPathQuery('//div[@class=:region-class]//div[@id=:block-id]/*', array(
':region-class' => 'region region-' . drupal_html_class($region),
':block-id' => 'block-' . strtr(strtolower($block['machine_name']), '-', '_'),
':block-id' => 'block-' . str_replace('_', '-', strtolower($block['id'])),
));
$this->assertFieldByXPath($xpath, NULL, t('Block found in %region_name region.', array('%region_name' => drupal_html_class($region))));
}
@ -247,7 +246,6 @@ class BlockTest extends BlockTestBase {
// Add a test block.
$block = array();
$block['id'] = 'test_cache';
$block['machine_name'] = strtolower($this->randomName(8));
$block['theme'] = \Drupal::config('system.theme')->get('default');
$block['region'] = 'header';
$block = $this->drupalPlaceBlock('test_cache', array('region' => 'header'));

View File

@ -66,40 +66,10 @@ abstract class BlockTestBase extends WebTestBase {
'sidebar_second',
'footer',
);
$block_storage = $this->container->get('entity.manager')->getStorageController('block');
$blocks = $block_storage->loadByProperties(array('theme' => \Drupal::config('system.theme')->get('default')));
foreach ($blocks as $block) {
$block->delete();
}
}
/**
* Moves a block to a given region via the UI and confirms the result.
*
* @param array $block
* An array of information about the block, including the following keys:
* - module: The module providing the block.
* - title: The title of the block.
* - delta: The block's delta key.
* @param string $region
* The machine name of the theme region to move the block to, for example
* 'header' or 'sidebar_first'.
*/
function moveBlockToRegion(array $block, $region) {
// Set the created block to a specific region.
$edit = array();
$edit['blocks[0][region]'] = $region;
$this->drupalPostForm('admin/structure/block', $edit, t('Save blocks'));
// Confirm that the block was moved to the proper region.
$this->assertText(t('The block settings have been updated.'), format_string('Block successfully moved to %region_name region.', array( '%region_name' => $region)));
// Confirm that the block is being displayed.
$this->drupalGet('');
$this->assertText(t($block['title']), 'Block successfully being displayed on the page.');
// Confirm that the custom block was found at the proper region.
$xpath = $this->buildXPathQuery('//div[@class=:region-class]//div[@id=:block-id]/*', array(
':region-class' => 'region region-' . drupal_html_class($region),
':block-id' => 'block-' . strtr(strtolower($block['machine_name']), '-', '_'),
));
$this->assertFieldByXPath($xpath, NULL, t('Block found in %region_name region.', array('%region_name' => drupal_html_class($region))));
}
}

View File

@ -51,14 +51,14 @@ class BlockUiTest extends WebTestBase {
'label' => 'Tools',
'tr' => '5',
'plugin_id' => 'system_menu_block:tools',
'settings' => array('region' => 'sidebar_second', 'machine_name' => 'tools'),
'settings' => array('region' => 'sidebar_second', 'id' => 'tools'),
'test_weight' => '-1',
),
array(
'label' => 'Powered by Drupal',
'tr' => '12',
'plugin_id' => 'system_powered_by_block',
'settings' => array('region' => 'footer', 'machine_name' => 'powered'),
'settings' => array('region' => 'footer', 'id' => 'powered'),
'test_weight' => '0',
),
);
@ -92,24 +92,24 @@ class BlockUiTest extends WebTestBase {
$element = $this->xpath('//*[@id="blocks"]/tbody/tr[' . $values['tr'] . ']/td[1]/text()');
$this->assertTrue((string)$element[0] == $values['label'], 'The "' . $values['label'] . '" block title is set inside the ' . $values['settings']['region'] . ' region.');
// Look for a test block region select form element.
$this->assertField('blocks[stark.' . $values['settings']['machine_name'] . '][region]', 'The block "' . $values['label'] . '" has a region assignment field.');
$this->assertField('blocks[' . $values['settings']['id'] . '][region]', 'The block "' . $values['label'] . '" has a region assignment field.');
// Move the test block to the header region.
$edit['blocks[stark.' . $values['settings']['machine_name'] . '][region]'] = 'header';
$edit['blocks[' . $values['settings']['id'] . '][region]'] = 'header';
// Look for a test block weight select form element.
$this->assertField('blocks[stark.' . $values['settings']['machine_name'] . '][weight]', 'The block "' . $values['label'] . '" has a weight assignment field.');
$this->assertField('blocks[' . $values['settings']['id'] . '][weight]', 'The block "' . $values['label'] . '" has a weight assignment field.');
// Change the test block's weight.
$edit['blocks[stark.' . $values['settings']['machine_name'] . '][weight]'] = $values['test_weight'];
$edit['blocks[' . $values['settings']['id'] . '][weight]'] = $values['test_weight'];
}
$this->drupalPostForm('admin/structure/block', $edit, t('Save blocks'));
foreach ($this->testBlocks as $values) {
// Check if the region and weight settings changes have persisted.
$this->assertOptionSelected(
'edit-blocks-stark' . $values['settings']['machine_name'] . '-region',
'edit-blocks-' . $values['settings']['id'] . '-region',
'header',
'The block "' . $values['label'] . '" has the correct region assignment (header).'
);
$this->assertOptionSelected(
'edit-blocks-stark' . $values['settings']['machine_name'] . '-weight',
'edit-blocks-' . $values['settings']['id'] . '-weight',
$values['test_weight'],
'The block "' . $values['label'] . '" has the correct weight assignment (' . $values['test_weight'] . ').'
);
@ -146,17 +146,17 @@ class BlockUiTest extends WebTestBase {
public function testMachineNameSuggestion() {
$url = 'admin/structure/block/add/test_block_instantiation/stark';
$this->drupalGet($url);
$this->assertFieldByName('machine_name', 'displaymessage', 'Block form uses raw machine name suggestion when no instance already exists.');
$this->assertFieldByName('id', 'displaymessage', 'Block form uses raw machine name suggestion when no instance already exists.');
$this->drupalPostForm($url, array(), 'Save block');
// Now, check to make sure the form starts by autoincrementing correctly.
$this->drupalGet($url);
$this->assertFieldByName('machine_name', 'displaymessage_2', 'Block form appends _2 to plugin-suggested machine name when an instance already exists.');
$this->assertFieldByName('id', 'displaymessage_2', 'Block form appends _2 to plugin-suggested machine name when an instance already exists.');
$this->drupalPostForm($url, array(), 'Save block');
// And verify that it continues working beyond just the first two.
$this->drupalGet($url);
$this->assertFieldByName('machine_name', 'displaymessage_3', 'Block form appends _3 to plugin-suggested machine name when two instances already exist.');
$this->assertFieldByName('id', 'displaymessage_3', 'Block form appends _3 to plugin-suggested machine name when two instances already exist.');
}
}

View File

@ -36,16 +36,21 @@ class NewDefaultThemeBlocksTest extends WebTestBase {
$default_theme = \Drupal::config('system.theme')->get('default');
// Add several block instances.
$this->adminUser = $this->drupalCreateUser(array('administer blocks'));
$this->drupalLogin($this->adminUser);
$this->drupalLogin($this->drupalCreateUser(array('administer blocks')));
// Add two instances of the user login block.
$this->drupalPlaceBlock('user_login_block');
$this->drupalPlaceBlock('user_login_block');
$this->drupalPlaceBlock('user_login_block', array(
'id' => $default_theme . '_' . strtolower($this->randomName(8)),
));
$this->drupalPlaceBlock('user_login_block', array(
'id' => $default_theme . '_' . strtolower($this->randomName(8)),
));
// Add an instance of a different block.
$this->drupalPlaceBlock('system_powered_by_block');
$this->drupalLogout($this->adminUser);
$this->drupalPlaceBlock('system_powered_by_block', array(
'id' => $default_theme . '_' . strtolower($this->randomName(8)),
));
$this->drupalLogout();
// Enable a different theme.
$new_theme = 'bartik';
@ -56,30 +61,18 @@ class NewDefaultThemeBlocksTest extends WebTestBase {
->save();
// Ensure that the new theme has all the blocks as the previous default.
// @todo Replace the string manipulation below once the configuration
// system provides a method for extracting an ID in a given namespace.
$default_prefix = "block.block.$default_theme";
$new_prefix = "block.block.$new_theme";
$default_block_names = config_get_storage_names_with_prefix($default_prefix);
$new_blocks = array_flip(config_get_storage_names_with_prefix($new_prefix));
$default_block_names = $this->container->get('entity.query')->get('block')
->condition('theme', $default_theme)
->execute();
$new_blocks = $this->container->get('entity.query')->get('block')
->condition('theme', $new_theme)
->execute();
$this->assertTrue(count($default_block_names) == count($new_blocks), 'The new default theme has the same number of blocks as the previous theme.');
foreach ($default_block_names as $default_block_name) {
// Make sure the configuration object name is in the expected format.
if (strpos($default_block_name, $default_prefix) === 0) {
// Remove the matching block from the list of blocks in the new theme.
// E.g., if the old theme has block.block.stark.admin,
// unset block.block.bartik.admin.
$id = substr($default_block_name, (strlen($default_prefix) + 1));
unset($new_blocks[$new_prefix . '.' . $id]);
}
else {
$this->fail(format_string(
'%block is not an expected block instance name.',
array(
'%block' => $default_block_name,
)
));
}
// Remove the matching block from the list of blocks in the new theme.
// E.g., if the old theme has block.block.stark_admin,
// unset block.block.bartik_admin.
unset($new_blocks[str_replace($default_theme . '_', $new_theme . '_', $default_block_name)]);
}
$this->assertTrue(empty($new_blocks), 'The new theme has exactly the same blocks as the previous default theme.');
}

View File

@ -184,11 +184,11 @@ class DisplayBlockTest extends ViewTestBase {
$this->assertTrue(empty($elements), 'The label field is not found for Views blocks.');
// Test that that machine name field is hidden from display and has been
// saved as expected from the default value.
$this->assertNoFieldById('edit-machine-name', 'stark.views_block__test_view_block_1', 'The machine name is hidden on the views block form.');
$this->assertNoFieldById('edit-machine-name', 'views_block__test_view_block_1', 'The machine name is hidden on the views block form.');
// Save the block.
$this->drupalPostForm(NULL, array(), t('Save block'));
$storage = $this->container->get('entity.manager')->getStorageController('block');
$block = $storage->load('stark.views_block__test_view_block_block_1');
$block = $storage->load('views_block__test_view_block_block_1');
// This will only return a result if our new block has been created with the
// expected machine name.
$this->assertTrue(!empty($block), 'The expected block was loaded.');
@ -196,7 +196,7 @@ class DisplayBlockTest extends ViewTestBase {
for ($i = 2; $i <= 3; $i++) {
// Place the same block again and make sure we have a new ID.
$this->drupalPostForm('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme, array(), t('Save block'));
$block = $storage->load('stark.views_block__test_view_block_block_1_' . $i);
$block = $storage->load('views_block__test_view_block_block_1_' . $i);
// This will only return a result if our new block has been created with the
// expected machine name.
$this->assertTrue(!empty($block), 'The expected block was loaded.');
@ -209,14 +209,14 @@ class DisplayBlockTest extends ViewTestBase {
$this->drupalPostForm('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme, $edit, t('Save block'));
$block = $storage->load('stark.views_block__test_view_block_block_1_4');
$block = $storage->load('views_block__test_view_block_block_1_4');
$config = $block->getPlugin()->getConfiguration();
$this->assertEqual(10, $config['items_per_page'], "'Items per page' is properly saved.");
$edit['settings[override][items_per_page]'] = 5;
$this->drupalPostForm('admin/structure/block/manage/stark.views_block__test_view_block_block_1_4', $edit, t('Save block'));
$this->drupalPostForm('admin/structure/block/manage/views_block__test_view_block_block_1_4', $edit, t('Save block'));
$block = $storage->load('stark.views_block__test_view_block_block_1_4');
$block = $storage->load('views_block__test_view_block_block_1_4');
$config = $block->getPlugin()->getConfiguration();
$this->assertEqual(5, $config['items_per_page'], "'Items per page' is properly saved.");

View File

@ -1,4 +1,5 @@
id: stark.test_block
id: test_block
theme: stark
weight: ''
status: '1'
langcode: en

View File

@ -43,7 +43,7 @@ class LanguageSwitchingTest extends WebTestBase {
*/
function testLanguageBlock() {
// Enable the language switching block.
$block = $this->drupalPlaceBlock('language_block:' . Language::TYPE_INTERFACE, array('machine_name' => 'test_language_block'));
$block = $this->drupalPlaceBlock('language_block:' . Language::TYPE_INTERFACE, array('id' => 'test_language_block'));
// Add language.
$edit = array(

View File

@ -408,7 +408,7 @@ class LanguageUILanguageNegotiationTest extends WebTestBase {
$this->drupalGet('admin/config/regional/language/detection');
// Enable the language switcher block.
$this->drupalPlaceBlock('language_block:' . Language::TYPE_INTERFACE, array('machine_name' => 'test_language_block'));
$this->drupalPlaceBlock('language_block:' . Language::TYPE_INTERFACE, array('id' => 'test_language_block'));
// Access the front page without specifying any valid URL language prefix
// and having as browser language preference a non-default language.

View File

@ -61,7 +61,7 @@ class NodeBlockFunctionalTest extends NodeTestBase {
));
// Enable the recent content block with two items.
$block = $this->drupalPlaceBlock('node_recent_block', array('machine_name' => 'test_block', 'block_count' => 2));
$block = $this->drupalPlaceBlock('node_recent_block', array('id' => 'test_block', 'block_count' => 2));
// Test that block is not visible without nodes.
$this->drupalGet('');

View File

@ -40,7 +40,7 @@ class NodeSyndicateBlockTest extends NodeTestBase {
*/
public function testSyndicateBlock() {
// Place the "Syndicate" block and confirm that it is rendered.
$this->drupalPlaceBlock('node_syndicate_block', array('machine_name' => 'test_syndicate_block'));
$this->drupalPlaceBlock('node_syndicate_block', array('id' => 'test_syndicate_block'));
$this->drupalGet('');
$this->assertFieldByXPath('//div[@id="block-test-syndicate-block"]/*', NULL, 'Syndicate block found.');
}

View File

@ -353,7 +353,7 @@ abstract class WebTestBase extends TestBase {
* @endcode
* The following defaults are provided:
* - label: Random string.
* - machine_name: Random string.
* - id: Random string.
* - region: 'sidebar_first'.
* - theme: The default theme.
* - visibility: Empty array.
@ -368,20 +368,18 @@ abstract class WebTestBase extends TestBase {
$settings += array(
'plugin' => $plugin_id,
'region' => 'sidebar_first',
'machine_name' => strtolower($this->randomName(8)),
'id' => strtolower($this->randomName(8)),
'theme' => \Drupal::config('system.theme')->get('default'),
'label' => $this->randomName(8),
'visibility' => array(),
'weight' => 0,
);
foreach (array('region', 'machine_name', 'theme', 'plugin', 'visibility', 'weight') as $key) {
foreach (array('region', 'id', 'theme', 'plugin', 'visibility', 'weight') as $key) {
$values[$key] = $settings[$key];
// Remove extra values that do not belong in the settings array.
unset($settings[$key]);
}
$values['settings'] = $settings;
// Build the ID out of the theme and machine_name.
$values['id'] = $values['theme'] . '.' . $values['machine_name'];
$block = entity_create('block', $values);
$block->save();
return $block;

View File

@ -43,14 +43,12 @@ class BreadcrumbTest extends MenuTestBase {
// This test puts menu links in the Tools menu and then tests for their
// presence on the page, so we need to ensure that the Tools block will be
// displayed in the default theme and admin theme.
$settings = array(
'machine_name' => 'system_menu_tools',
// displayed in the admin theme.
$this->drupalPlaceBlock('system_menu_block:tools', array(
'machine' => 'system_menu_tools',
'region' => 'content',
);
$this->drupalPlaceBlock('system_menu_block:tools', $settings);
$settings['theme'] = \Drupal::config('system.theme')->get('admin');
$this->drupalPlaceBlock('system_menu_block:tools', $settings);
'theme' => \Drupal::config('system.theme')->get('admin'),
));
}
/**
@ -279,7 +277,7 @@ class BreadcrumbTest extends MenuTestBase {
// ('taxonomy/term/%') should never be translated and appear in any menu
// other than the breadcrumb trail.
$elements = $this->xpath('//div[@id=:menu]/descendant::a[@href=:href]', array(
':menu' => 'block-system-menu-tools',
':menu' => 'block-bartik-tools',
':href' => url($link['link_path']),
));
$this->assertTrue(count($elements) == 1, "Link to {$link['link_path']} appears only once.");

View File

@ -54,7 +54,7 @@ class AccessDeniedTest extends WebTestBase {
$this->drupalPostForm('admin/config/system/site-information', $edit, t('Save configuration'));
// Enable the user login block.
$this->drupalPlaceBlock('user_login_block', array('machine_name' => 'login'));
$this->drupalPlaceBlock('user_login_block', array('id' => 'login'));
// Log out and check that the user login block is shown on custom 403 pages.
$this->drupalLogout();
@ -82,7 +82,7 @@ class AccessDeniedTest extends WebTestBase {
$edit = array(
'region' => -1,
);
$this->drupalPostForm('admin/structure/block/manage/stark.login', $edit, t('Save block'));
$this->drupalPostForm('admin/structure/block/manage/login', $edit, t('Save block'));
// Check that we can log in from the 403 page.
$this->drupalLogout();

View File

@ -43,7 +43,7 @@ class BlockUpgradePathTest extends UpgradePathTestBase {
// Confirm that the custom block has been created, and title matches input.
$settings = array(
'settings[label]' => $this->randomName(255),
'machine_name' => strtolower($this->randomName(8)),
'id' => strtolower($this->randomName(8)),
'region' => 'sidebar_first',
);
$this->drupalPostForm('admin/structure/block/add/system_powered_by_block/' . \Drupal::config('system.theme')->get('default'), $settings, t('Save block'));
@ -52,7 +52,7 @@ class BlockUpgradePathTest extends UpgradePathTestBase {
// Try to add a block with a title over 255 characters.
$settings = array(
'settings[label]' => $this->randomName(256),
'machine_name' => strtolower($this->randomName(8)),
'id' => strtolower($this->randomName(8)),
'region' => 'sidebar_first',
);
$this->drupalPostForm('admin/structure/block/add/system_powered_by_block/' . \Drupal::config('system.theme')->get('default'), $settings, t('Save block'));

View File

@ -9,7 +9,7 @@ namespace Drupal\views\Plugin\Block;
use Drupal\block\Annotation\Block;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Config\Entity\Query\Query;
use Drupal\Component\Utility\Xss;
use Symfony\Component\DependencyInjection\ContainerInterface;
@ -84,36 +84,6 @@ class ViewsBlock extends ViewsBlockBase {
}
}
/**
* Generates a views block instance ID.
*
* @param \Drupal\Core\Entity\EntityStorageControllerInterface $manager
* The block storage controller.
*
* @return string
* The new block instance ID.
*/
public function generateBlockInstanceID(EntityStorageControllerInterface $manager) {
$original_id = 'views_block__' . $this->view->storage->id() . '_' . $this->view->current_display;
// Get an array of block IDs without the theme prefix.
$block_ids = array_map(function ($block_id) {
$parts = explode('.', $block_id);
return end($parts);
}, array_keys($manager->loadMultiple()));
// Iterate through potential IDs until we get a new one. E.g.
// 'views_block__MYVIEW_PAGE_1_2'
$count = 1;
$id = $original_id;
while (in_array($id, $block_ids)) {
$id = $original_id . '_' . ++$count;
}
return $id;
}
/**
* {@inheritdoc}
*/

View File

@ -120,11 +120,11 @@ class ExposedFormTest extends ViewTestBase {
$this->drupalGet('test_exposed_block');
// Test there is an exposed form in a block.
$xpath = $this->buildXPathQuery('//div[@id=:id]/div/form/@id', array(':id' => 'block-' . $block->get('machine_name')));
$xpath = $this->buildXPathQuery('//div[@id=:id]/div/form/@id', array(':id' => drupal_html_id('block-' . $block->id())));
$this->assertFieldByXpath($xpath, $this->getExpectedExposedFormId($view), 'Expected form found in views block.');
// Test there is not an exposed form in the view page content area.
$xpath = $this->buildXPathQuery('//div[@class="view-content"]/form/@id', array(':id' => 'block-' . $block->get('machine_name')));
$xpath = $this->buildXPathQuery('//div[@class="view-content"]/form/@id', array(':id' => drupal_html_id('block-' . $block->id())));
$this->assertNoFieldByXpath($xpath, $this->getExpectedExposedFormId($view), 'No exposed form found in views content region.');
// Test there is only one views exposed form on the page.

View File

@ -268,9 +268,7 @@ abstract class ViewTestBase extends WebTestBase {
* The result from the xpath query.
*/
protected function findBlockInstance(Block $block) {
$config_id = explode('.', $block->id());
$machine_name = array_pop($config_id);
return $this->xpath('//div[@id = :id]', array(':id' => 'block-' . $machine_name));
return $this->xpath('//div[@id = :id]', array(':id' => 'block-' . $block->id()));
}
/**

View File

@ -1,4 +1,5 @@
id: stark.admin
id: stark_admin
theme: stark
weight: '1'
status: '1'
langcode: en

View File

@ -1,4 +1,5 @@
id: stark.login
id: stark_login
theme: stark
weight: '0'
status: '1'
langcode: en

View File

@ -1,4 +1,5 @@
id: stark.tools
id: stark_tools
theme: stark
weight: '0'
status: '1'
langcode: en

View File

@ -1,4 +1,5 @@
id: bartik.breadcrumbs
id: bartik_breadcrumbs
theme: bartik
uuid: a29e843f-d19c-4528-ab02-2fc335e12b1e
weight: '-5'
status: '0'

View File

@ -1,4 +1,5 @@
id: bartik.content
id: bartik_content
theme: bartik
uuid: 2cab5a0c-de08-4b5c-9700-f0243a6fb000
weight: '0'
status: '1'

View File

@ -1,4 +1,5 @@
id: bartik.footer
id: bartik_footer
theme: bartik
uuid: a6c75fc2-5ca1-403e-ab37-557c7244e8c0
weight: '0'
status: '1'

View File

@ -1,4 +1,5 @@
id: bartik.help
id: bartik_help
theme: bartik
uuid: 6ea8e05a-6793-4ecf-8801-015dc6e1013e
weight: '0'
status: '1'

View File

@ -1,4 +1,5 @@
id: bartik.login
id: bartik_login
theme: bartik
uuid: 961f4152-3e91-4c9f-9114-20a5375675d0
weight: '0'
status: '1'

View File

@ -1,4 +1,5 @@
id: bartik.powered
id: bartik_powered
theme: bartik
uuid: 37cff101-27dc-478d-9d00-b58b9d884039
weight: '10'
status: '1'

View File

@ -1,4 +1,5 @@
id: bartik.search
id: bartik_search
theme: bartik
uuid: 51f70058-a370-4410-9000-a65488d00e6c
weight: '-1'
status: '1'

View File

@ -1,4 +1,5 @@
id: bartik.tools
id: bartik_tools
theme: bartik
uuid: 0dca3209-c6fa-4043-a407-7afb952cfc5e
weight: '0'
status: '1'

View File

@ -1,4 +1,5 @@
id: seven.breadcrumbs
id: seven_breadcrumbs
theme: seven
uuid: f8d0d0fb-daf7-4c91-8a09-9cb643279f03
weight: '-2'
status: '0'

View File

@ -1,4 +1,5 @@
id: seven.content
id: seven_content
theme: seven
uuid: 3c9e3337-e0f4-42c3-8a00-f1d8be09b0ea
weight: '0'
status: '1'

View File

@ -1,4 +1,5 @@
id: seven.help
id: seven_help
theme: seven
uuid: 367d09b7-9638-4faf-bf07-7fe31b2226a0
weight: '0'
status: '1'

View File

@ -1,4 +1,5 @@
id: seven.login
id: seven_login
theme: seven
uuid: 10a9888b-2247-408d-9702-2c0cc9cacba2
weight: '10'
status: '1'

View File

@ -40,7 +40,7 @@ class StandardTest extends WebTestBase {
$this->drupalGet('admin/structure/block/add/system_menu_block:main/bartik');
$this->drupalPostForm(NULL, array(
'region' => 'sidebar_first',
'machine_name' => 'main_navigation',
'id' => 'main_navigation',
), t('Save block'));
// Verify admin user can see the block.
$this->drupalGet('');
@ -51,7 +51,7 @@ class StandardTest extends WebTestBase {
$this->drupalGet('admin/structure/block');
$elements = $this->xpath('//div[@role=:role and @id=:id]', array(
':role' => 'complementary',
':id' => 'block-help',
':id' => 'block-bartik-help',
));
$this->assertEqual(count($elements), 1, 'Found complementary role on help block.');
@ -59,7 +59,7 @@ class StandardTest extends WebTestBase {
$this->drupalGet('');
$elements = $this->xpath('//div[@role=:role and @id=:id]', array(
':role' => 'complementary',
':id' => 'block-powered',
':id' => 'block-bartik-powered',
));
$this->assertEqual(count($elements), 1, 'Found complementary role on powered by block.');