Commit d3df59e5 authored by catch's avatar catch

Issue #1587036 by aspilicious, Rob Loach: Convert block tests to PSR-0.

parent 56219bfa
......@@ -3,5 +3,4 @@ description = Controls the visual building blocks a page is constructed with. Bl
package = Core
version = VERSION
core = 8.x
files[] = block.test
configure = admin/structure/block
<?php
/**
* @file
* Definition of Drupal\block\Tests\BlockAdminThemeTest.
*/
namespace Drupal\block\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Test the block system with admin themes.
*/
class BlockAdminThemeTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Admin theme block admin accessibility',
'description' => "Check whether the block administer page for a disabled theme accessible if and only if it's the admin theme.",
'group' => 'Block',
);
}
function setUp() {
parent::setUp(array('block'));
}
/**
* Check for the accessibility of the admin theme on the block admin page.
*/
function testAdminTheme() {
// Create administrative user.
$admin_user = $this->drupalCreateUser(array('administer blocks', 'administer themes'));
$this->drupalLogin($admin_user);
// Ensure that access to block admin page is denied when theme is disabled.
$this->drupalGet('admin/structure/block/list/bartik');
$this->assertResponse(403, t('The block admin page for a disabled theme can not be accessed'));
// Enable admin theme and confirm that tab is accessible.
$edit['admin_theme'] = 'bartik';
$this->drupalPost('admin/appearance', $edit, t('Save configuration'));
$this->drupalGet('admin/structure/block/list/bartik');
$this->assertResponse(200, t('The block admin page for the admin theme can be accessed'));
}
}
<?php
/**
* @file
* Definition of Drupal\block\Tests\BlockCacheTest.
*/
namespace Drupal\block\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Test block caching.
*/
class BlockCacheTest extends WebTestBase {
protected $admin_user;
protected $normal_user;
protected $normal_user_alt;
public static function getInfo() {
return array(
'name' => 'Block caching',
'description' => 'Test block caching.',
'group' => 'Block',
);
}
function setUp() {
parent::setUp(array('block', 'block_test'));
// Create an admin user, log in and enable test blocks.
$this->admin_user = $this->drupalCreateUser(array('administer blocks', 'access administration pages'));
$this->drupalLogin($this->admin_user);
// Create additional users to test caching modes.
$this->normal_user = $this->drupalCreateUser();
$this->normal_user_alt = $this->drupalCreateUser();
// Sync the roles, since drupalCreateUser() creates separate roles for
// the same permission sets.
$this->normal_user_alt->roles = $this->normal_user->roles;
$this->normal_user_alt->save();
// Enable our test block.
$edit['blocks[block_test_test_cache][region]'] = 'sidebar_first';
$this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
}
/**
* Test DRUPAL_CACHE_PER_ROLE.
*/
function testCachePerRole() {
$this->setCacheMode(DRUPAL_CACHE_PER_ROLE);
// Enable our test block. Set some content for it to display.
$current_content = $this->randomName();
variable_set('block_test_content', $current_content);
$this->drupalLogin($this->normal_user);
$this->drupalGet('');
$this->assertText($current_content, t('Block content displays.'));
// Change the content, but the cached copy should still be served.
$old_content = $current_content;
$current_content = $this->randomName();
variable_set('block_test_content', $current_content);
$this->drupalGet('');
$this->assertText($old_content, t('Block is served from the cache.'));
// Clear the cache and verify that the stale data is no longer there.
cache_clear_all();
$this->drupalGet('');
$this->assertNoText($old_content, t('Block cache clear removes stale cache data.'));
$this->assertText($current_content, t('Fresh block content is displayed after clearing the cache.'));
// Test whether the cached data is served for the correct users.
$old_content = $current_content;
$current_content = $this->randomName();
variable_set('block_test_content', $current_content);
$this->drupalLogout();
$this->drupalGet('');
$this->assertNoText($old_content, t('Anonymous user does not see content cached per-role for normal user.'));
$this->drupalLogin($this->normal_user_alt);
$this->drupalGet('');
$this->assertText($old_content, t('User with the same roles sees per-role cached content.'));
$this->drupalLogin($this->admin_user);
$this->drupalGet('');
$this->assertNoText($old_content, t('Admin user does not see content cached per-role for normal user.'));
$this->drupalLogin($this->normal_user);
$this->drupalGet('');
$this->assertText($old_content, t('Block is served from the per-role cache.'));
}
/**
* Test DRUPAL_CACHE_GLOBAL.
*/
function testCacheGlobal() {
$this->setCacheMode(DRUPAL_CACHE_GLOBAL);
$current_content = $this->randomName();
variable_set('block_test_content', $current_content);
$this->drupalGet('');
$this->assertText($current_content, t('Block content displays.'));
$old_content = $current_content;
$current_content = $this->randomName();
variable_set('block_test_content', $current_content);
$this->drupalLogout();
$this->drupalGet('user');
$this->assertText($old_content, t('Block content served from global cache.'));
}
/**
* Test DRUPAL_NO_CACHE.
*/
function testNoCache() {
$this->setCacheMode(DRUPAL_NO_CACHE);
$current_content = $this->randomName();
variable_set('block_test_content', $current_content);
// If DRUPAL_NO_CACHE has no effect, the next request would be cached.
$this->drupalGet('');
$this->assertText($current_content, t('Block content displays.'));
// A cached copy should not be served.
$current_content = $this->randomName();
variable_set('block_test_content', $current_content);
$this->drupalGet('');
$this->assertText($current_content, t('DRUPAL_NO_CACHE prevents blocks from being cached.'));
}
/**
* Test DRUPAL_CACHE_PER_USER.
*/
function testCachePerUser() {
$this->setCacheMode(DRUPAL_CACHE_PER_USER);
$current_content = $this->randomName();
variable_set('block_test_content', $current_content);
$this->drupalLogin($this->normal_user);
$this->drupalGet('');
$this->assertText($current_content, t('Block content displays.'));
$old_content = $current_content;
$current_content = $this->randomName();
variable_set('block_test_content', $current_content);
$this->drupalGet('');
$this->assertText($old_content, t('Block is served from per-user cache.'));
$this->drupalLogin($this->normal_user_alt);
$this->drupalGet('');
$this->assertText($current_content, t('Per-user block cache is not served for other users.'));
$this->drupalLogin($this->normal_user);
$this->drupalGet('');
$this->assertText($old_content, t('Per-user block cache is persistent.'));
}
/**
* Test DRUPAL_CACHE_PER_PAGE.
*/
function testCachePerPage() {
$this->setCacheMode(DRUPAL_CACHE_PER_PAGE);
$current_content = $this->randomName();
variable_set('block_test_content', $current_content);
$this->drupalGet('node');
$this->assertText($current_content, t('Block content displays on the node page.'));
$old_content = $current_content;
$current_content = $this->randomName();
variable_set('block_test_content', $current_content);
$this->drupalGet('user');
$this->assertNoText($old_content, t('Block content cached for the node page does not show up for the user page.'));
$this->drupalGet('node');
$this->assertText($old_content, t('Block content cached for the node page.'));
}
/**
* Private helper method to set the test block's cache mode.
*/
private function setCacheMode($cache_mode) {
db_update('block')
->fields(array('cache' => $cache_mode))
->condition('module', 'block_test')
->execute();
$current_mode = db_query("SELECT cache FROM {block} WHERE module = 'block_test'")->fetchField();
if ($current_mode != $cache_mode) {
$this->fail(t('Unable to set cache mode to %mode. Current mode: %current_mode', array('%mode' => $cache_mode, '%current_mode' => $current_mode)));
}
}
}
<?php
/**
* @file
* Definition of Drupal\block\Tests\BlockHiddenRegionTest.
*/
namespace Drupal\block\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests that hidden regions do not inherit blocks when a theme is enabled.
*/
class BlockHiddenRegionTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Blocks not in hidden region',
'description' => 'Checks that a newly enabled theme does not inherit blocks to its hidden regions.',
'group' => 'Block',
);
}
function setUp() {
parent::setUp(array('block', 'block_test', 'search'));
// Enable Search block in default theme.
db_merge('block')
->key(array(
'module' => 'search',
'delta' => 'form',
'theme' => variable_get('theme_default', 'stark'),
))
->fields(array(
'status' => 1,
'weight' => -1,
'region' => 'sidebar_first',
'pages' => '',
'cache' => -1,
))
->execute();
}
/**
* Tests that hidden regions do not inherit blocks when a theme is enabled.
*/
function testBlockNotInHiddenRegion() {
// Create administrative user.
$admin_user = $this->drupalCreateUser(array('administer blocks', 'administer themes', 'search content'));
$this->drupalLogin($admin_user);
// Ensure that the search form block is displayed.
$this->drupalGet('');
$this->assertText('Search', t('Block was displayed on the front page.'));
// Enable "block_test_theme" and set it as the default theme.
$theme = 'block_test_theme';
theme_enable(array($theme));
variable_set('theme_default', $theme);
menu_router_rebuild();
// Ensure that "block_test_theme" is set as the default theme.
$this->drupalGet('admin/structure/block');
$this->assertText('Block test theme(' . t('active tab') . ')', t('Default local task on blocks admin page is the block test theme.'));
// Ensure that the search form block is displayed.
$this->drupalGet('');
$this->assertText('Search', t('Block was displayed on the front page.'));
}
}
<?php
/**
* @file
* Definition of Drupal\block\Tests\BlockHtmlIdTest.
*/
namespace Drupal\block\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Test block HTML id validity.
*/
class BlockHtmlIdTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Block HTML id',
'description' => 'Test block HTML id validity.',
'group' => 'Block',
);
}
function setUp() {
parent::setUp(array('block', 'block_test'));
// Create an admin user, log in and enable test blocks.
$this->admin_user = $this->drupalCreateUser(array('administer blocks', 'access administration pages'));
$this->drupalLogin($this->admin_user);
// Enable our test block.
$edit['blocks[block_test_test_html_id][region]'] = 'sidebar_first';
$this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
// Make sure the block has some content so it will appear
$current_content = $this->randomName();
variable_set('block_test_content', $current_content);
}
/**
* Test valid HTML id.
*/
function testHtmlId() {
$this->drupalGet('');
$this->assertRaw('block-block-test-test-html-id', t('HTML id for test block is valid.'));
}
}
<?php
/**
* @file
* Definition of Drupal\block\Tests\BlockInvalidRegionTest.
*/
namespace Drupal\block\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests that a block assigned to an invalid region triggers the warning.
*/
class BlockInvalidRegionTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Blocks in invalid regions',
'description' => 'Checks that an active block assigned to a non-existing region triggers the warning message and is disabled.',
'group' => 'Block',
);
}
function setUp() {
parent::setUp(array('block', 'block_test'));
// Create an admin user.
$admin_user = $this->drupalCreateUser(array('administer site configuration', 'access administration pages'));
$this->drupalLogin($admin_user);
}
/**
* Tests that blocks assigned to invalid regions work correctly.
*/
function testBlockInInvalidRegion() {
// Enable a test block in the default theme and place it in an invalid region.
db_merge('block')
->key(array(
'module' => 'block_test',
'delta' => 'test_html_id',
'theme' => variable_get('theme_default', 'stark'),
))
->fields(array(
'status' => 1,
'region' => 'invalid_region',
'cache' => DRUPAL_NO_CACHE,
))
->execute();
$warning_message = t('The block %info was assigned to the invalid region %region and has been disabled.', array('%info' => t('Test block html id'), '%region' => 'invalid_region'));
// Clearing the cache should disable the test block placed in the invalid region.
$this->drupalPost('admin/config/development/performance', array(), 'Clear all caches');
$this->assertRaw($warning_message, 'Enabled block was in the invalid region and has been disabled.');
// Clear the cache to check if the warning message is not triggered.
$this->drupalPost('admin/config/development/performance', array(), 'Clear all caches');
$this->assertNoRaw($warning_message, 'Disabled block in the invalid region will not trigger the warning.');
// Place disabled test block in the invalid region of the default theme.
db_merge('block')
->key(array(
'module' => 'block_test',
'delta' => 'test_html_id',
'theme' => variable_get('theme_default', 'stark'),
))
->fields(array(
'region' => 'invalid_region',
'cache' => DRUPAL_NO_CACHE,
))
->execute();
// Clear the cache to check if the warning message is not triggered.
$this->drupalPost('admin/config/development/performance', array(), 'Clear all caches');
$this->assertNoRaw($warning_message, 'Disabled block in the invalid region will not trigger the warning.');
}
}
<?php
/**
* @file
* Definition of Drupal\block\Tests\BlockLanguageTest.
*/
namespace Drupal\block\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Functional tests for the language list configuration forms.
*/
class BlockLanguageTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Language block visibility',
'description' => 'Tests if a block can be configure to be only visibile on a particular language.',
'group' => 'Block',
);
}
function setUp() {
parent::setUp('language', 'block');
}
/**
* Tests the visibility settings for the blocks based on language.
*/
public function testLanguageBlockVisibility() {
// Create a new user, allow him to manage the blocks and the languages.
$admin_user = $this->drupalCreateUser(array(
'administer languages', 'administer blocks',
));
$this->drupalLogin($admin_user);
// Add predefined language.
$edit = array(
'predefined_langcode' => 'fr',
);
$this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
$this->assertText('French', t('Language added successfully.'));
// Check if the visibility setting is available.
$this->drupalGet('admin/structure/block/add');
$this->assertField('langcodes[en]', t('Language visibility field is visible.'));
// Create a new block.
$info_name = $this->randomString(10);
$body = '';
for ($i = 0; $i <= 100; $i++) {
$body .= chr(rand(97, 122));
}
$edit = array(
'regions[stark]' => 'sidebar_first',
'info' => $info_name,
'title' => 'test',
'body[value]' => $body,
);
$this->drupalPost('admin/structure/block/add', $edit, t('Save block'));
// Set visibility setting for one language.
$edit = array(
'langcodes[en]' => TRUE,
);
$this->drupalPost('admin/structure/block/manage/block/1/configure', $edit, t('Save block'));
// Change the default language.
$edit = array(
'site_default' => 'fr',
);
$this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
// Reset the static cache of the language list.
drupal_static_reset('language_list');
// Check that a page has a block
$this->drupalGet('', array('language' => language_load('en')));
$this->assertText($body, t('The body of the custom block appears on the page.'));
// Check that a page doesn't has a block for the current language anymore
$this->drupalGet('', array('language' => language_load('fr')));
$this->assertNoText($body, t('The body of the custom block does not appear on the page.'));
}
/**
* Tests if the visibility settings are removed if the language is deleted.
*/
public function testLanguageBlockVisibilityLanguageDelete() {
// Create a new user, allow him to manage the blocks and the languages.
$admin_user = $this->drupalCreateUser(array(
'administer languages', 'administer blocks',
));
$this->drupalLogin($admin_user);
// Add predefined language.
$edit = array(
'predefined_langcode' => 'fr',
);
$this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
$this->assertText('French', t('Language added successfully.'));
// Create a new block.
$info_name = $this->randomString(10);
$body = '';
for ($i = 0; $i <= 100; $i++) {
$body .= chr(rand(97, 122));
}
$edit = array(
'regions[stark]' => 'sidebar_first',
'info' => $info_name,
'title' => 'test',
'body[value]' => $body,
);
$this->drupalPost('admin/structure/block/add', $edit, t('Save block'));
// Set visibility setting for one language.
$edit = array(
'langcodes[fr]' => TRUE,
);
$this->drupalPost('admin/structure/block/manage/block/1/configure', $edit, t('Save block'));
// Check that we have an entry in the database after saving the setting.
$count = db_query('SELECT COUNT(langcode) FROM {block_language} WHERE module = :module AND delta = :delta', array(
':module' => 'block',
':delta' => '1'
))->fetchField();
$this->assertTrue($count == 1, t('The block language visibility has an entry in the database.'));
// Delete the language.
$this->drupalPost('admin/config/regional/language/delete/fr', array(), t('Delete'));
// Check that the setting related to this language has been deleted.
$count = db_query('SELECT COUNT(langcode) FROM {block_language} WHERE module = :module AND delta = :delta', array(
':module' => 'block',
':delta' => '1'
))->fetchField();
$this->assertTrue($count == 0, t('The block language visibility do not have an entry in the database.'));
}
/**
* Tests if the visibility settings are removed if the block is deleted.
*/
public function testLanguageBlockVisibilityBlockDelete() {
// Create a new user, allow him to manage the blocks and the languages.
$admin_user = $this->drupalCreateUser(array(
'administer languages', 'administer blocks',
));
$this->drupalLogin($admin_user);
// Add predefined language.
$edit = array(
'predefined_langcode' => 'fr',
);
$this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
$this->assertText('French', t('Language added successfully.'));
// Create a new block.
$info_name = $this->randomString(10);
$body = '';
for ($i = 0; $i <= 100; $i++) {