Commit 89125fb9 authored by webchick's avatar webchick
Browse files

Issue #1874598 by xjm, mojzis: Add BlockTestBase.

parent 467590dc
......@@ -9,196 +9,19 @@
use Drupal\simpletest\WebTestBase;
class BlockTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('block', 'custom_block', 'test_page_test');
protected $regions;
protected $adminUser;
/**
* Provides testing for basic block module functionality.
*/
class BlockTest extends BlockTestBase {
public static function getInfo() {
return array(
'name' => 'Block functionality',
'description' => 'Custom block functionality.',
'description' => 'Tests basic block functionality.',
'group' => 'Block',
);
}
function setUp() {
parent::setUp();
// Use the test page as the front page.
config('system.site')->set('page.front', 'test-page')->save();
// Create Filtered HTML text format.
$filtered_html_format = entity_create('filter_format', array(
'format' => 'filtered_html',
'name' => 'Filtered HTML',
));
$filtered_html_format->save();
// Create Full HTML text format.
$full_html_format = entity_create('filter_format', array(
'format' => 'full_html',
'name' => 'Full HTML',
));
$full_html_format->save();
$this->checkPermissions(array(), TRUE);
// Create and log in an administrative user having access to the Full HTML
// text format.
$this->adminUser = $this->drupalCreateUser(array(
'administer blocks',
filter_permission_name($filtered_html_format),
filter_permission_name($full_html_format),
'access administration pages',
));
$this->drupalLogin($this->adminUser);
// Define the existing regions.
$this->regions = array();
$this->regions[] = 'header';
$this->regions[] = 'sidebar_first';
$this->regions[] = 'content';
$this->regions[] = 'sidebar_second';
$this->regions[] = 'footer';
}
/**
* Test creating custom block, moving it to a specific region and then deleting it.
*/
public function testCustomBlock() {
$default_theme = config('system.theme')->get('default');
// Clear the block cache to load the Custom Block module's block definitions.
$this->container->get('plugin.manager.block')->clearCachedDefinitions();
// Enable a second theme.
theme_enable(array('seven'));
// Confirm that the add block link appears on block overview pages.
$this->drupalGet("admin/structure/block/list/block_plugin_ui:$default_theme/add");
$this->assertLink(t('Add custom block'));
// But not on the normal admin page.
$this->drupalGet('admin/structure/block');
$this->assertNoLink(t('Add custom block'));
// Confirm that hidden regions are not shown as options for block placement
// when adding a new block.
theme_enable(array('bartik'));
$themes = list_themes();
$this->drupalGet('admin/structure/block/add');
foreach ($themes as $key => $theme) {
if ($theme->status) {
foreach ($theme->info['regions_hidden'] as $hidden_region) {
$elements = $this->xpath('//select[@id=:id]//option[@value=:value]', array(':id' => 'edit-regions-' . $key, ':value' => $hidden_region));
$this->assertFalse(isset($elements[0]), format_string('The hidden region @region is not available for @theme.', array('@region' => $hidden_region, '@theme' => $key)));
}
}
}
// Add a new custom block by filling out the input form on block/add/basic.
$info = strtolower($this->randomName(8));
$langcode = LANGUAGE_NOT_SPECIFIED;
$values = array(
'info' => $info,
"block_body[$langcode][0][value]" => $this->randomName(8)
);
$this->drupalPost('block/add/basic', $values, t('Save'));
$custom_block['machine_name'] = $info;
$custom_block['label'] = $this->randomName(8);
$custom_block['region'] = $this->regions[0];
$this->drupalPost(NULL, $custom_block, t('Save block'));
$block = entity_load('block', $default_theme . '.' . $info);
// Confirm that the custom block has been created, and then query the created bid.
$this->assertText(t('The block configuration has been saved.'), 'Custom block instance successfully created.');
// Check that block_block_view() returns the correct content.
$data = entity_view($block, 'content');
$output = render($data);
$this->drupalSetcontent($output);
$elements = $this->xpath('//div[@class=:class]', array(':class' => 'field-item even'));
$this->assertEqual($values["block_body[$langcode][0][value]"], $elements[0], 'BlockInterface::build() provides correct block content.');
// Check whether the block can be moved to all available regions.
$custom_block['module'] = 'block';
foreach ($this->regions as $region) {
$this->moveBlockToRegion($custom_block, $region);
}
// Verify presence of configure and delete links for custom block.
$this->drupalGet('admin/structure/block');
$config_block_id = "admin/structure/block/manage/$default_theme.$info";
$this->assertLinkByHref("$config_block_id/configure", 0, 'Custom block configure link found.');
$this->assertLinkByHref("$config_block_id/delete", 0, 'Custom block delete link found.');
// Set visibility only for authenticated users, to verify delete functionality.
$edit = array();
$edit['visibility[role][roles][' . DRUPAL_AUTHENTICATED_RID . ']'] = TRUE;
$this->drupalPost("$config_block_id/configure", $edit, t('Save block'));
// Delete the created custom block & verify that it's been deleted and no longer appearing on the page.
$this->clickLink(t('delete'));
$this->drupalPost("$config_block_id/delete", array(), t('Delete'));
$this->assertRaw(t('The block %title has been removed.', array('%title' => $custom_block['label'])), 'Custom block successfully deleted.');
$this->drupalGet(NULL);
$this->assertNoText(t($custom_block['label']), 'Custom block no longer appears on page.');
}
/**
* Test creating custom block using Full HTML.
*/
public function testCustomBlockFormat() {
$default_theme = config('system.theme')->get('default');
// Add a new custom block by filling out the input form on block/add/basic.
$info = strtolower($this->randomName(8));
$langcode = LANGUAGE_NOT_SPECIFIED;
$values = array(
'info' => $info,
"block_body[$langcode][0][value]" => '<h1>Full HTML</h1>',
"block_body[$langcode][0][format]" => 'full_html',
);
$this->drupalPost('block/add/basic', $values, t('Save'));
// Load the block up from the database.
$blocks = entity_load_multiple('custom_block');
$loaded_block = end($blocks);
$custom_block['machine_name'] = $info;
$custom_block['label'] = $this->randomName(8);
$custom_block['region'] = $this->regions[0];
$this->drupalPost(NULL, $custom_block, t('Save block'));
// Set the created custom block to a specific region.
$edit['blocks[' . $default_theme . '.' . $custom_block['machine_name'] . '][region]'] = $this->regions[1];
$this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
// Confirm that the custom block is being displayed using configured text
// format.
$this->drupalGet('');
$this->assertRaw('<h1>Full HTML</h1>', 'Custom block successfully being displayed using Full HTML.');
// Confirm that a user without access to Full HTML can not see the body
// field, but can still submit the form without errors.
$block_admin = $this->drupalCreateUser(array('administer blocks'));
$this->drupalLogin($block_admin);
$this->drupalGet("block/" . $loaded_block->id() . "/edit");
$this->assertFieldByXPath("//textarea[@name='block_body[und][0][value]' and @disabled='disabled']", t('This field has been disabled because you do not have sufficient permissions to edit it.'), 'Body field contains denied message');
$this->assertNoText(t('Ensure that each block description is unique.'));
// Confirm that the custom block is still being displayed using configured
// text format.
$this->drupalGet('');
$this->assertRaw('<h1>Full HTML</h1>', 'Custom block successfully being displayed using Full HTML.');
}
/**
* Test block visibility.
*/
......
<?php
/**
* @file
* Contains \Drupal\block\Tests\BlockTestBase.
*/
namespace Drupal\block\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Provides setup and helper methods for block module tests.
*/
abstract class BlockTestBase extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('block', 'test_page_test');
/**
* A list of theme regions to test.
*
* @var array
*/
protected $regions;
/**
* A test user with administrative privileges.
*
* @var \Drupal\user\Plugin\Core\Entity\User
*/
protected $adminUser;
function setUp() {
parent::setUp();
// Use the test page as the front page.
config('system.site')->set('page.front', 'test-page')->save();
// Create Full HTML text format.
$full_html_format = entity_create('filter_format', array(
'format' => 'full_html',
'name' => 'Full HTML',
));
$full_html_format->save();
$this->checkPermissions(array(), TRUE);
// Create and log in an administrative user having access to the Full HTML
// text format.
$this->adminUser = $this->drupalCreateUser(array(
'administer blocks',
filter_permission_name($full_html_format),
'access administration pages',
));
$this->drupalLogin($this->adminUser);
// Define the existing regions.
$this->regions = array(
'header',
'sidebar_first',
'content',
'sidebar_second',
'footer',
);
$default_theme = variable_get('theme_default', 'stark');
$manager = $this->container->get('plugin.manager.block');
$instances = config_get_storage_names_with_prefix('plugin.core.block.' . $default_theme);
foreach ($instances as $plugin_id) {
config($plugin_id)->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->drupalPost('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))));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment