Commit 9fdc6a9c authored by alexpott's avatar alexpott

Issue #2043527 by tim.plunkett, vijaycs85, dawehner: Fixed Theme name is...

Issue #2043527 by tim.plunkett, vijaycs85, dawehner: Fixed Theme name is included in block machine name but should be stored as a key instead.
parent 3f38e917
......@@ -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);
}
}
......
......@@ -149,7 +149,7 @@ public function testBlockDelete() {
// Place the block.
$instance = array(
'machine_name' => drupal_strtolower($edit['info']),
'id' => drupal_strtolower($edit['info']),
'settings[label]' => $edit['info'],
'region' => 'sidebar_first',
);
......
......@@ -104,7 +104,7 @@ public function testBlockFields() {
$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',
);
......
......@@ -186,7 +186,7 @@ public function testsCustomBlockAddTypes() {
$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.');
......
......@@ -93,14 +93,10 @@ public static function create(ContainerInterface $container) {
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 @@ protected function actions(array $form, array &$form_state) {
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 @@ public function validate(array $form, array &$form_state) {
'values' => &$form_state['values']['settings']
);
// Call the plugin validate handler.
$entity->getPlugin()->validateConfigurationForm($form, $settings);
$this->entity->getPlugin()->validateConfigurationForm($form, $settings);
}
/**
......
......@@ -125,24 +125,13 @@ public function label($langcode = NULL) {
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',
......
......@@ -48,13 +48,13 @@ public function setUp() {
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);
}
......
......@@ -40,7 +40,7 @@ function setUp() {
// 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'));
}
/**
......
......@@ -62,7 +62,7 @@ public function testLanguageBlockVisibility() {
// 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 @@ public function testLanguageBlockVisibilityLanguageDelete() {
),
),
),
'machine_name' => 'language_block_test',
);
$block = $this->drupalPlaceBlock('system_powered_by_block', $edit);
......
......@@ -45,17 +45,17 @@ function testBlockRenderOrder() {
// 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 @@ function testBlockRenderOrder() {
'label' => 'Test Block',
'region' => $region,
'weight' => $test_block['weight'],
'machine_name' => $test_block['machine_name'],
'id' => $test_block['id'],
));
}
......@@ -74,14 +74,13 @@ function testBlockRenderOrder() {
$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.');
}
}
......@@ -76,7 +76,8 @@ protected function createTests() {
// 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 @@ protected function createTests() {
$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 @@ protected function createTests() {
* 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 @@ protected function loadTests() {
*/
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 @@ protected function renderTests() {
// 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 @@ protected function renderTests() {
* 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 @@ public function testDefaultBlocks() {
$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.');
}
}
......@@ -40,7 +40,7 @@ function testBlockThemeHookSuggestions() {
$block = entity_create('block', array(
'plugin' => 'system_menu_block:admin',
'region' => 'footer',
'id' => \Drupal::config('system.theme')->get('default') . '.machinename',
'id' => 'machinename',
));
$variables = array();
......
......@@ -32,7 +32,7 @@ function testBlockVisibility() {
// 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 @@ function testBlockVisibilityListedEmpty() {
// 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 @@ function testBlock() {
$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 @@ function testBlock() {
// 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 @@ function testBlock() {
$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 @@ public function testBlockThemeSelector() {
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 @@ function testHideBlockTitle() {
$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 @@ function testHideBlockTitle() {
$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 @@ function moveBlockToRegion(array $block, $region) {
// 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 @@ function moveBlockToRegion(array $block, $region) {
// 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 @@ function testBlockRehash() {
// 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'));
......
......@@ -66,40 +66,10 @@ function setUp() {
'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))));
}
}
......@@ -51,14 +51,14 @@ function setUp() {
'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 @@ function testBlockAdminUiPage() {
$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 @@ public function testCandidateBlockList() {
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.');
}
}
......@@ -36,16 +36,21 @@ function testNewDefaultThemeBlocks() {
$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 @@ function testNewDefaultThemeBlocks() {
->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.');
}
......
......@@ -184,11 +184,11 @@ public function testViewsBlockForm() {
$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 @@ public function testViewsBlockForm() {
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 @@ public function testViewsBlockForm() {