Skip to content

Commit

Permalink
- Patch #449198 by boombatower: cealn up test loading and related API.
Browse files Browse the repository at this point in the history
  • Loading branch information
dries committed Jun 8, 2009
1 parent 5e9cbe3 commit fc03528
Show file tree
Hide file tree
Showing 41 changed files with 412 additions and 155 deletions.
25 changes: 20 additions & 5 deletions includes/registry.inc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
// $Id: registry.inc,v 1.16 2009-05-16 16:04:42 dries Exp $
// $Id: registry.inc,v 1.17 2009-06-08 09:23:50 dries Exp $

/**
* @file
Expand Down Expand Up @@ -45,11 +45,19 @@ function _registry_rebuild() {
system_get_files_database($modules, 'module');
// Get the list of files we are going to parse.
$files = array();
foreach ($modules as $module) {
foreach ($modules as &$module) {
$dir = dirname($module->filepath);

// Store the module directory for use in hook_registry_files_alter().
$module->dir = $dir;

// Parse the .info file for all modules, reguardless of their status so the
// list of files can then be used in hook_registry_files_alter()
// implementations.
$module->info = drupal_parse_info_file($dir . '/' . $module->name . '.info');

if ($module->status) {
// Parse .info file only for enabled modules.
$module->info = drupal_parse_info_file(dirname($module->filepath) . '/' . $module->name . '.info');
$dir = dirname($module->filepath);
// Add files for enabled modules to the registry.
foreach ($module->info['files'] as $file) {
$files["$dir/$file"] = array('module' => $module->name, 'weight' => $module->weight);
}
Expand All @@ -59,6 +67,13 @@ function _registry_rebuild() {
$files["$filename"] = array('module' => '', 'weight' => 0);
}

// Allow modules to manually modify the list of files before the registry
// parses them. The $modules array provides the .info file information, which
// includes the list of files registered to each module. Any files in the
// list can then be added to the list of files that the registry will parse,
// or modify attributes of a file.
drupal_alter('registry_files', $files, $modules);

foreach (registry_get_parsed_files() as $filename => $file) {
// Add the md5 to those files we've already parsed.
if (isset($files[$filename])) {
Expand Down
3 changes: 2 additions & 1 deletion modules/aggregator/aggregator.info
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; $Id: aggregator.info,v 1.11 2009-03-15 18:59:05 webchick Exp $
; $Id: aggregator.info,v 1.12 2009-06-08 09:23:50 dries Exp $
name = Aggregator
description = "Aggregates syndicated content (RSS, RDF, and Atom feeds)."
package = Core
Expand All @@ -11,3 +11,4 @@ files[] = aggregator.fetcher.inc
files[] = aggregator.parser.inc
files[] = aggregator.processor.inc
files[] = aggregator.install
files[] = aggregator.test
48 changes: 47 additions & 1 deletion modules/block/block.api.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
// $Id: block.api.php,v 1.3 2009-01-26 14:08:42 dries Exp $
// $Id: block.api.php,v 1.4 2009-06-08 09:23:50 dries Exp $

/**
* @file
Expand Down Expand Up @@ -171,6 +171,52 @@ function hook_block_view($delta = '') {
return $block;
}

/**
* Act on blocks prior to rendering.
*
* This hook allows you to add, remove or modify blocks in the block list. The
* block list contains the block definitions not the rendered blocks. The blocks
* are rendered after the modules have had a chance to manipulate the block
* list.
* Alternatively you can set $block->content here, which will override the
* content of the block and prevent hook_block_view() from running.
*
* @param $blocks
* An array of $blocks, keyed by $bid
*
* This example shows how to achieve language specific visibility setting for
* blocks.
*/
function hook_block_list_alter(&$blocks) {
global $language, $theme_key;

$result = db_query('SELECT module, delta, language FROM {my_table}');
$block_languages = array();
foreach ($result as $record) {
$block_languages[$record->module][$record->delta][$record->language] = TRUE;
}

foreach ($blocks as $key => $block) {
// Any module using this alter should inspect the data before changing it,
// to ensure it is what they expect.
if ($block->theme != $theme_key || $block->status != 1) {
// This block was added by a contrib module, leave it in the list.
continue;
}

if (!isset($block_languages[$block->module][$block->delta])) {
// No language setting for this block, leave it in the list.
continue;
}

if (!isset($block_languages[$block->module][$block->delta][$language->language])) {
// This block should not be displayed with the active language, remove
// from the list.
unset($blocks[$key]);
}
}
}

/**
* @} End of "addtogroup hooks".
*/
3 changes: 2 additions & 1 deletion modules/block/block.info
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; $Id: block.info,v 1.12 2009-02-03 12:30:14 dries Exp $
; $Id: block.info,v 1.13 2009-06-08 09:23:50 dries Exp $

name = Block
description = Controls the boxes that are displayed around the main content.
Expand All @@ -8,3 +8,4 @@ core = 7.x
files[] = block.module
files[] = block.admin.inc
files[] = block.install
files[] = block.test
127 changes: 82 additions & 45 deletions modules/block/block.module
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
// $Id: block.module,v 1.343 2009-06-08 04:55:34 webchick Exp $
// $Id: block.module,v 1.344 2009-06-08 09:23:50 dries Exp $

/**
* @file
Expand Down Expand Up @@ -586,30 +586,64 @@ function block_list($region) {
* Load blocks information from the database.
*/
function _block_load_blocks() {
global $user, $theme_key;
global $theme_key;

$blocks = array();
$rids = array_keys($user->roles);
$query = db_select('block', 'b');
$query->leftJoin('block_role', 'r', 'b.module = r.module AND b.delta = r.delta');
$result = $query
->distinct()
->fields('b')
->condition('b.theme', $theme_key)
->condition('b.status', 1)
->condition(db_or()
->condition('r.rid', $rids, 'IN')
->isNull('r.rid')
)
->orderBy('b.region')
->orderBy('b.weight')
->orderBy('b.module')
->addTag('block_load')
->execute();
foreach ($result as $block) {
if (!isset($blocks[$block->region])) {
$blocks[$block->region] = array();

$block_list = $result->fetchAllAssoc('bid');
// Allow modules to modify the block list.
drupal_alter('block_list', $block_list);

$blocks = array();
foreach ($block_list as $block) {
$blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
}
return $blocks;
}

/**
* Implement hook_block_list_alter().
*
* Check the page, role and user specific visibilty settings. Remove the block
* if the visibility conditions are not met.
*/
function block_block_list_alter(&$blocks) {
global $user, $theme_key;

// Build an array of roles for each block.
$block_roles = array();
$result = db_query('SELECT module, delta, rid FROM {block_role}');
foreach ($result as $record) {
$block_roles[$record->module][$record->delta][] = $record->rid;
}

foreach ($blocks as $key => $block) {
if ($block->theme != $theme_key || $block->status != 1) {
// This block was added by a contrib module, leave it in the list.
continue;
}

// If a block has no roles associated, it is displayed for every role.
// For blocks with roles associated, if none of the user's roles matches
// the settings from this block, remove it from the block list.
if (!isset($block_roles[$block->module][$block->delta])) {
// No roles associated.
}
elseif (!array_intersect($block_roles[$block->module][$block->delta], array_keys($user->roles))) {
// No match.
unset($blocks[$key]);
continue;
}

// Use the user's block visibility setting, if necessary.
if ($block->custom != 0) {
if ($user->uid && isset($user->block[$block->module][$block->delta])) {
Expand All @@ -622,6 +656,10 @@ function _block_load_blocks() {
else {
$enabled = TRUE;
}
if (!$enabled) {
unset($blocks[$key]);
continue;
}

// Match path if necessary.
if ($block->pages) {
Expand All @@ -647,12 +685,10 @@ function _block_load_blocks() {
else {
$page_match = TRUE;
}
$block->enabled = $enabled;
$block->page_match = $page_match;
$blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
if (!$page_match) {
unset($blocks[$key]);
}
}

return $blocks;
}

/**
Expand All @@ -668,38 +704,39 @@ function _block_render_blocks($region_blocks) {
foreach ($region_blocks as $key => $block) {
// Render the block content if it has not been created already.
if (!isset($block->content)) {
// Erase the block from the static array - we'll put it back if it has content.
// Erase the block from the static array - we'll put it back if it has
// content.
unset($region_blocks[$key]);
if ($block->enabled && $block->page_match) {
// Try fetching the block from cache. Block caching is not compatible with
// node_access modules. We also preserve the submission of forms in blocks,
// by fetching from cache only if the request method is 'GET' (or 'HEAD').
if (!count(module_implements('node_grants')) && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD') && ($cid = _block_get_cache_id($block)) && ($cache = cache_get($cid, 'cache_block'))) {
$array = $cache->data;
}
else {
$array = module_invoke($block->module, 'block_view', $block->delta);
if (isset($cid)) {
cache_set($cid, $array, 'cache_block', CACHE_TEMPORARY);
}
// Try fetching the block from cache. Block caching is not compatible
// with node_access modules. We also preserve the submission of forms in
// blocks, by fetching from cache only if the request method is 'GET'
// (or 'HEAD').
if (!count(module_implements('node_grants')) && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD') && ($cid = _block_get_cache_id($block)) && ($cache = cache_get($cid, 'cache_block'))) {
$array = $cache->data;
}
else {
$array = module_invoke($block->module, 'block_view', $block->delta);
if (isset($cid)) {
cache_set($cid, $array, 'cache_block', CACHE_TEMPORARY);
}
}

if (isset($array) && is_array($array)) {
foreach ($array as $k => $v) {
$block->$k = $v;
}
if (isset($array) && is_array($array)) {
foreach ($array as $k => $v) {
$block->$k = $v;
}
if (isset($block->content) && $block->content) {
// Override default block title if a custom display title is present.
if ($block->title) {
// Check plain here to allow module generated titles to keep any markup.
$block->subject = $block->title == '<none>' ? '' : check_plain($block->title);
}
if (!isset($block->subject)) {
$block->subject = '';
}
$region_blocks["{$block->module}_{$block->delta}"] = $block;
}
if (isset($block->content) && $block->content) {
// Override default block title if a custom display title is present.
if ($block->title) {
// Check plain here to allow module generated titles to keep any
// markup.
$block->subject = $block->title == '<none>' ? '' : check_plain($block->title);
}
if (!isset($block->subject)) {
$block->subject = '';
}
$region_blocks["{$block->module}_{$block->delta}"] = $block;
}
}
}
Expand Down
33 changes: 32 additions & 1 deletion modules/block/block.test
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
// $Id: block.test,v 1.19 2009-05-31 07:46:54 webchick Exp $
// $Id: block.test,v 1.20 2009-06-08 09:23:50 dries Exp $

/**
* @file
Expand Down Expand Up @@ -99,6 +99,37 @@ class BlockTestCase extends DrupalWebTestCase {
$this->assertRaw('<h1>Full HTML</h1>', t('Box successfully being displayed using Full HTML.'));
}

/**
* Test block visibility.
*/
function testBlockVisibility() {
$block = array();
$block['title'] = 'Syndicate';
$block['module'] = 'node';
$block['delta'] = 'syndicate';

// Set the block to be hidden on any user path, and to be shown only to
// authenticated users.
$edit = array();
$edit['pages'] = 'user*';
$edit['roles[2]'] = TRUE;
$this->drupalPost('admin/build/block/configure/' . $block['module'] . '/' . $block['delta'], $edit, t('Save block'));

// Move block to the left sidebar.
$this->moveBlockToRegion($block, $this->regions[1]);

$this->drupalGet('');
$this->assertText('Syndicate', t('Block was displayed on the front page.'));

$this->drupalGet('user*');
$this->assertNoText('Syndicate', t('Block was not displayed according to block visibility rules.'));

// Confirm that the block is not displayed to anonymous users.
$this->drupalLogout();
$this->drupalGet('');
$this->assertNoText('Syndicate', t('Block was not displayed to anonymous users.'));
}

/**
* Test configuring and moving a module-define block to specific regions.
*/
Expand Down
3 changes: 2 additions & 1 deletion modules/blog/blog.info
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; $Id: blog.info,v 1.10 2008-10-11 02:32:36 webchick Exp $
; $Id: blog.info,v 1.11 2009-06-08 09:23:51 dries Exp $

name = Blog
description = Enables multi-user blogs.
Expand All @@ -7,3 +7,4 @@ version = VERSION
core = 7.x
files[] = blog.module
files[] = blog.pages.inc
files[] = blog.test
3 changes: 2 additions & 1 deletion modules/blogapi/blogapi.info
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; $Id: blogapi.info,v 1.9 2008-10-11 02:32:37 webchick Exp $
; $Id: blogapi.info,v 1.10 2009-06-08 09:23:51 dries Exp $

name = Blog API
description = Allows users to post content using applications that support XML-RPC blog APIs.
Expand All @@ -7,3 +7,4 @@ version = VERSION
core = 7.x
files[] = blogapi.module
files[] = blogapi.install
files[] = blogapi.test
3 changes: 2 additions & 1 deletion modules/book/book.info
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; $Id: book.info,v 1.11 2008-10-13 19:59:41 dries Exp $
; $Id: book.info,v 1.12 2009-06-08 09:23:51 dries Exp $

name = Book
description = Allows users to create and organize related content in an outline.
Expand All @@ -9,3 +9,4 @@ files[] = book.module
files[] = book.admin.inc
files[] = book.pages.inc
files[] = book.install
files[] = book.test
3 changes: 2 additions & 1 deletion modules/comment/comment.info
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; $Id: comment.info,v 1.9 2008-10-11 02:32:41 webchick Exp $
; $Id: comment.info,v 1.10 2009-06-08 09:23:51 dries Exp $

name = Comment
description = Allows users to comment on and discuss published content.
Expand All @@ -9,3 +9,4 @@ files[] = comment.module
files[] = comment.admin.inc
files[] = comment.pages.inc
files[] = comment.install
files[] = comment.test
Loading

0 comments on commit fc03528

Please sign in to comment.