Commit 94d877eb authored by Crell's avatar Crell

Merge remote-tracking branch 'upstream/8.x' into remerge

parents 1e0223a1 5b0715d7
......@@ -22,7 +22,6 @@ maintainer. Current component maintainers for Drupal 8:
Ajax system
- Alex Bronstein 'effulgentsia' <http://drupal.org/user/78040>
- Randy Fay 'rfay' <http://drupal.org/user/30906>
- Earl Miles 'merlinofchaos' <http://drupal.org/user/26979>
Base system
......
......@@ -1108,7 +1108,7 @@ function drupal_prepare_form($form_id, &$form, &$form_state) {
* A keyed array containing the current state of the form. The current
* user-submitted data is stored in $form_state['values'], though
* form validation functions are passed an explicit copy of the
* values for the sake of simplicity. Validation handlers can also
* values for the sake of simplicity. Validation handlers can also use
* $form_state to pass information on to submit handlers. For example:
* $form_state['data_for_submission'] = $data;
* This technique is useful when validation requires file parsing,
......
......@@ -2,7 +2,9 @@
/**
* @file
* Multiple language handling functionality.
* Language Negotiation API.
*
* @see http://drupal.org/node/1497272
*/
/**
......@@ -11,10 +13,99 @@
const LANGUAGE_NEGOTIATION_DEFAULT = 'language-default';
/**
* Chooses a language for the given type based on language negotiation settings.
* @defgroup language_negotiation Language Negotiation API functionality
* @{
* Functions to customize the language types and the negotiation process.
*
* The language negotiation API is based on two major concepts:
* - Language types: types of translatable data (the types of data that a user
* can view or request).
* - Language negotiation methods: functions for determining which language to
* use to present a particular piece of data to the user.
* Both language types and language negotiation methods are customizable.
*
* Drupal defines three built-in language types:
* - Interface language: The page's main language, used to present translated
* user interface elements such as titles, labels, help text, and messages.
* - Content language: The language used to present content that is available
* in more than one language (see
* @link field_language Field Language API @endlink for details).
* - URL language: The language associated with URLs. When generating a URL,
* this value will be used by url() as a default if no explicit preference is
* provided.
* Modules can define additional language types through
* hook_language_types_info(), and alter existing language type definitions
* through hook_language_types_info_alter().
*
* Language types may be configurable or fixed. The language negotiation
* methods associated with a configurable language type can be explicitly
* set through the user interface. A fixed language type has predetermined
* (module-defined) language negotiation settings and, thus, does not appear in
* the configuration page. Here is a code snippet that makes the content
* language (which by default inherits the interface language's values)
* configurable:
* @code
* function mymodule_language_types_info_alter(&$language_types) {
* unset($language_types[LANGUAGE_TYPE_CONTENT]['fixed']);
* }
* @endcode
*
* Every language type can have a different set of language negotiation methods
* assigned to it. Different language types often share the same language
* negotiation settings, but they can have independent settings if needed. If
* two language types are configured the same way, their language switcher
* configuration will be functionally identical and the same settings will act
* on both language types.
*
* Drupal defines the following built-in language negotiation methods:
* - URL: Determine the language from the URL (path prefix or domain).
* - Session: Determine the language from a request/session parameter.
* - User: Follow the user's language preference.
* - Browser: Determine the language from the browser's language settings.
* - Default language: Use the default site language.
* Language negotiation methods are simple callback functions that implement a
* particular logic to return a language code. For instance, the URL method
* searches for a valid path prefix or domain name in the current request URL.
* If a language negotiation method does not return a valid language code, the
* next method associated to the language type (based on method weight) is
* invoked.
*
* Modules can define additional language negotiation methods through
* hook_language_negotiation_info(), and alter existing methods through
* hook_language_negotiation_info_alter(). Here is an example snippet that lets
* path prefixes be ignored for administrative paths:
* @code
* function mymodule_language_negotiation_info_alter(&$negotiation_info) {
* // Replace the core function with our own function.
* module_load_include('language', 'inc', 'language.negotiation');
* $negotiation_info[LANGUAGE_NEGOTIATION_URL]['callbacks']['negotiation'] = 'mymodule_from_url';
* $negotiation_info[LANGUAGE_NEGOTIATION_URL]['file'] = drupal_get_path('module', 'mymodule') . '/mymodule.module';
* }
*
* function mymodule_from_url($languages) {
* // Use the core URL language negotiation method to get a valid language
* // code.
* module_load_include('language', 'inc', 'language.negotiation');
* $langcode = language_from_url($languages);
*
* // If we are on an administrative path, override with the default language.
* if (isset($_GET['q']) && strtok($_GET['q'], '/') == 'admin') {
* return language_default()->langcode;
* }
* return $langcode;
* }
* ?>
* @endcode
*
* For more information, see
* @link http://drupal.org/node/1497272 Language Negotiation API @endlink
*/
/**
* Chooses a language based on language negotiation method settings.
*
* @param $type
* The language type key.
* The language type key to find the language for.
*
* @return
* The negotiated language object.
......@@ -68,8 +159,8 @@ function language_types_info() {
* Returns only the configurable language types.
*
* A language type maybe configurable or fixed. A fixed language type is a type
* whose negotiation values are unchangeable and defined while defining the
* language type itself.
* whose language negotiation methods are module-defined and not altered through
* the user interface.
*
* @param $stored
* (optional) By default, retrieves values from the 'language_types' variable
......@@ -162,6 +253,10 @@ function language_types_set() {
*
* @param $type
* The language type.
*
* @return
* The identifier of the first language negotiation method for the given
* language type, or the default method if none exists.
*/
function language_negotiation_method_get_first($type) {
$negotiation = variable_get("language_negotiation_$type", array());
......@@ -169,7 +264,7 @@ function language_negotiation_method_get_first($type) {
}
/**
* Checks if a language negotiation method is enabled for a language type.
* Checks whether a language negotiation method is enabled for a language type.
*
* @param $method_id
* The language negotiation method ID.
......@@ -259,7 +354,7 @@ function language_negotiation_purge() {
* @param $type
* The language type.
* @param $method_weights
* An array of language negotiation method weights keyed by method id.
* An array of language negotiation method weights keyed by method ID.
*/
function language_negotiation_set($type, $method_weights) {
// Save only the necessary fields.
......@@ -278,8 +373,7 @@ function language_negotiation_set($type, $method_weights) {
// If the language negotiation method does not express any preference
// about types, make it available for any configurable type.
$types = array_flip(isset($method['types']) ? $method['types'] : $default_types);
// Check if the language negotiation method is defined and has the right
// type.
// Check whether the method is defined and has the right type.
if (isset($types[$type])) {
$method_data = array();
foreach ($method_fields as $field) {
......@@ -328,13 +422,14 @@ function language_negotiation_info() {
* Invokes a language negotiation method and caches the results.
*
* @param $method_id
* The language negotiation method ID.
* The language negotiation method's identifier.
* @param $method
* (optional) The language negotiation method to be invoked. If not passed it
* will be explicitly loaded through language_negotiation_info().
* (optional) An associative array of information about the method to be
* invoked (see hook_language_negotiation_info() for details). If not passed
* in, it will be loaded through language_negotiation_info().
*
* @return
* The language negotiation method's return value.
* A language object representing the language chosen by the method.
*/
function language_negotiation_method_invoke($method_id, $method = NULL) {
$results = &drupal_static(__FUNCTION__);
......@@ -361,10 +456,10 @@ function language_negotiation_method_invoke($method_id, $method = NULL) {
$results[$method_id] = isset($languages[$langcode]) ? $languages[$langcode] : FALSE;
}
// Since objects are resources we need to return a clone to prevent the
// language negotiation method cache to be unintentionally altered. The same
// language negotiation methods might be used with different language types
// based on configuration.
// Since objects are resources, we need to return a clone to prevent the
// language negotiation method cache from being unintentionally altered. The
// same methods might be used with different language types based on
// configuration.
return !empty($results[$method_id]) ? clone($results[$method_id]) : $results[$method_id];
}
......@@ -381,8 +476,8 @@ function language_from_default() {
/**
* Split the given path into prefix and actual path.
*
* Parse the given path and return the language object identified by the
* prefix and the actual path.
* Parse the given path and return the language object identified by the prefix
* and the actual path.
*
* @param $path
* The path to split.
......@@ -434,3 +529,7 @@ function language_fallback_get_candidates($type = LANGUAGE_TYPE_CONTENT) {
return $fallback_candidates;
}
/**
* @} End of "language_negotiation"
*/
......@@ -79,16 +79,19 @@ function module_list($refresh = FALSE, $bootstrap_refresh = FALSE, $sort = FALSE
// Use the advanced drupal_static() pattern, since this is called very often.
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['list'] = &drupal_static(__FUNCTION__ . ':list', array());
$drupal_static_fast['list'] = &drupal_static(__FUNCTION__ . ':list');
$drupal_static_fast['sorted_list'] = &drupal_static(__FUNCTION__ . ':sorted_list');
}
$list = &$drupal_static_fast['list'];
$sorted_list = &$drupal_static_fast['sorted_list'];
if (empty($list) || $refresh || $fixed_list) {
if (!isset($list) || $refresh || isset($fixed_list)) {
$list = array();
$sorted_list = NULL;
if ($fixed_list) {
// The fixed list may be a completely empty array, thus check for isset().
// Calling code may use this to empty out the module list entirely. For
// example, unit tests need to ensure that no modules are invoked.
if (isset($fixed_list)) {
foreach ($fixed_list as $name => $module) {
drupal_get_filename('module', $name, $module['filename']);
$list[$name] = $name;
......
......@@ -42,7 +42,7 @@ function getMultiple(&$cids) {
/**
* Implements Drupal\Core\Cache\CacheBackendInterface::set().
*/
function set($cid, $data, $expire = CACHE_PERMANENT) {}
function set($cid, $data, $expire = CACHE_PERMANENT, array $tags = array()) {}
/**
* Implements Drupal\Core\Cache\CacheBackendInterface::delete().
......@@ -74,6 +74,11 @@ function expire() {}
*/
function garbageCollection() {}
/**
* Implements Drupal\Core\Cache\CacheBackendInterface::invalidateTags().
*/
public function invalidateTags(array $tags) {}
/**
* Implements Drupal\Core\Cache\CacheBackendInterface::isEmpty().
*/
......
......@@ -819,6 +819,9 @@ public function transactionDepth() {
* @param $name
* Optional name of the savepoint.
*
* @return Drupal\Core\Database\Transaction
* A DatabaseTransaction object.
*
* @see Drupal\Core\Database\Transaction
*/
public function startTransaction($name = '') {
......
......@@ -676,7 +676,7 @@ function node_type_update_nodes($old_type, $type) {
* TRUE to rebuild node types. Equivalent to calling node_types_rebuild().
*
* @return
* Associative array with two components:
* An object with two properties:
* - names: Associative array of the names of node types, keyed by the type.
* - types: Associative array of node type objects, keyed by the type.
* Both of these arrays will include new types that have been defined by
......
......@@ -539,6 +539,181 @@ public function run(array $methods = array()) {
restore_error_handler();
}
/**
* Generates a database prefix for running tests.
*
* The database prefix is used by prepareEnvironment() to setup a public files
* directory for the test to be run, which also contains the PHP error log,
* which is written to in case of a fatal error. Since that directory is based
* on the database prefix, all tests (even unit tests) need to have one, in
* order to access and read the error log.
*
* @see TestBase::prepareEnvironment()
*
* The generated database table prefix is used for the Drupal installation
* being performed for the test. It is also used as user agent HTTP header
* value by the cURL-based browser of DrupalWebTestCase, which is sent to the
* Drupal installation of the test. During early Drupal bootstrap, the user
* agent HTTP header is parsed, and if it matches, all database queries use
* the database table prefix that has been generated here.
*
* @see WebTestBase::curlInitialize()
* @see drupal_valid_test_ua()
* @see WebTestBase::setUp()
*/
protected function prepareDatabasePrefix() {
$this->databasePrefix = 'simpletest' . mt_rand(1000, 1000000);
// As soon as the database prefix is set, the test might start to execute.
// All assertions as well as the SimpleTest batch operations are associated
// with the testId, so the database prefix has to be associated with it.
db_update('simpletest_test_id')
->fields(array('last_prefix' => $this->databasePrefix))
->condition('test_id', $this->testId)
->execute();
}
/**
* Changes the database connection to the prefixed one.
*
* @see WebTestBase::setUp()
*/
protected function changeDatabasePrefix() {
if (empty($this->databasePrefix)) {
$this->prepareDatabasePrefix();
}
// Clone the current connection and replace the current prefix.
$connection_info = Database::getConnectionInfo('default');
Database::renameConnection('default', 'simpletest_original_default');
foreach ($connection_info as $target => $value) {
$connection_info[$target]['prefix'] = array(
'default' => $value['prefix']['default'] . $this->databasePrefix,
);
}
Database::addConnectionInfo('default', 'default', $connection_info['default']);
}
/**
* Prepares the current environment for running the test.
*
* Backups various current environment variables and resets them, so they do
* not interfere with the Drupal site installation in which tests are executed
* and can be restored in TestBase::tearDown().
*
* Also sets up new resources for the testing environment, such as the public
* filesystem and configuration directories.
*
* @see TestBase::tearDown()
*/
protected function prepareEnvironment() {
global $user, $language_interface, $conf;
// Backup current in-memory configuration.
$this->originalConf = $conf;
// Backup statics and globals.
$this->originalContainer = clone drupal_container();
$this->originalLanguage = $language_interface;
$this->originalConfigDirectory = $GLOBALS['config_directory_name'];
// Save further contextual information.
$this->originalFileDirectory = variable_get('file_public_path', conf_path() . '/files');
$this->originalProfile = drupal_get_profile();
$this->originalUser = $user;
// Save and clean the shutdown callbacks array because it is static cached
// and will be changed by the test run. Otherwise it will contain callbacks
// from both environments and the testing environment will try to call the
// handlers defined by the original one.
$callbacks = &drupal_register_shutdown_function();
$this->originalShutdownCallbacks = $callbacks;
$callbacks = array();
// Create test directory ahead of installation so fatal errors and debug
// information can be logged during installation process.
// Use temporary files directory with the same prefix as the database.
$this->public_files_directory = $this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10);
$this->private_files_directory = $this->public_files_directory . '/private';
$this->temp_files_directory = $this->private_files_directory . '/temp';
// Create the directories
file_prepare_directory($this->public_files_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
file_prepare_directory($this->private_files_directory, FILE_CREATE_DIRECTORY);
file_prepare_directory($this->temp_files_directory, FILE_CREATE_DIRECTORY);
$this->generatedTestFiles = FALSE;
// Create and set a new configuration directory and signature key.
// The child site automatically adjusts the global $config_directory_name to
// a test-prefix-specific directory within the public files directory.
// @see config_get_config_directory()
$GLOBALS['config_directory_name'] = 'simpletest/' . substr($this->databasePrefix, 10) . '/config';
$this->configFileDirectory = $this->originalFileDirectory . '/' . $GLOBALS['config_directory_name'];
file_prepare_directory($this->configFileDirectory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
// Log fatal errors.
ini_set('log_errors', 1);
ini_set('error_log', $this->public_files_directory . '/error.log');
// Set the test information for use in other parts of Drupal.
$test_info = &$GLOBALS['drupal_test_info'];
$test_info['test_run_id'] = $this->databasePrefix;
$test_info['in_child_site'] = FALSE;
}
/**
* Deletes created files, database tables, and reverts all environment changes.
*
* This method needs to be invoked for both unit and integration tests.
*
* @see TestBase::prepareDatabasePrefix()
* @see TestBase::changeDatabasePrefix()
* @see TestBase::prepareEnvironment()
*/
protected function tearDown() {
global $user, $language_interface, $conf;
// In case a fatal error occurred that was not in the test process read the
// log to pick up any fatal errors.
simpletest_log_read($this->testId, $this->databasePrefix, get_class($this), TRUE);
$emailCount = count(variable_get('drupal_test_email_collector', array()));
if ($emailCount) {
$message = format_plural($emailCount, '1 e-mail was sent during this test.', '@count e-mails were sent during this test.');
$this->pass($message, t('E-mail'));
}
// Delete temporary files directory.
file_unmanaged_delete_recursive($this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10));
// Restore original database connection.
Database::removeConnection('default');
Database::renameConnection('simpletest_original_default', 'default');
// Reset all static variables.
drupal_static_reset();
// Restore has_run state.
$has_run = &drupal_static('module_load_all');
$has_run = TRUE;
// Restore original in-memory configuration.
$conf = $this->originalConf;
// Restore original statics and globals.
drupal_container($this->originalContainer);
$language_interface = $this->originalLanguage;
$GLOBALS['config_directory_name'] = $this->originalConfigDirectory;
// Restore original shutdown callbacks.
$callbacks = &drupal_register_shutdown_function();
$callbacks = $this->originalShutdownCallbacks;
// Restore original user session.
$user = $this->originalUser;
drupal_save_session(TRUE);
}
/**
* Handle errors during test runs.
*
......
......@@ -38,55 +38,40 @@ function __construct($test_id = NULL) {
protected function setUp() {
global $conf;
// Store necessary current values before switching to the test environment.
$this->originalFileDirectory = variable_get('file_public_path', conf_path() . '/files');
// Create the database prefix for this test.
$this->prepareDatabasePrefix();
// Reset all statics so that test is performed with a clean environment.
// Prepare the environment for running tests.
$this->prepareEnvironment();
$this->originalThemeRegistry = theme_get_registry(FALSE);
// Reset all statics and variables to perform tests in a clean environment.
$conf = array();
drupal_static_reset();
// Generate temporary prefixed database to ensure that tests have a clean starting point.
$this->databasePrefix = Database::getConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}');
// Empty out module list.
module_list(TRUE, FALSE, FALSE, array());
// Prevent module_load_all() from attempting to refresh it.
$has_run = &drupal_static('module_load_all');
$has_run = TRUE;
// Re-implant theme registry.
// Required for l() and other functions to work correctly and not trigger
// database lookups.
$theme_get_registry = &drupal_static('theme_get_registry');
$theme_get_registry[FALSE] = $this->originalThemeRegistry;
// Create test directory.
$public_files_directory = $this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10);
file_prepare_directory($public_files_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
$conf['file_public_path'] = $public_files_directory;
$conf['file_public_path'] = $this->public_files_directory;
// Clone the current connection and replace the current prefix.
$connection_info = Database::getConnectionInfo('default');
Database::renameConnection('default', 'simpletest_original_default');
foreach ($connection_info as $target => $value) {
$connection_info[$target]['prefix'] = array(
'default' => $value['prefix']['default'] . $this->databasePrefix,
);
}
Database::addConnectionInfo('default', 'default', $connection_info['default']);
// Change the database prefix.
// All static variables need to be reset before the database prefix is
// changed, since Drupal\Core\Utility\CacheArray implementations attempt to
// write back to persistent caches when they are destructed.
$this->changeDatabasePrefix();
// Set user agent to be consistent with web test case.
// Set user agent to be consistent with WebTestBase.
$_SERVER['HTTP_USER_AGENT'] = $this->databasePrefix;
// If locale is enabled then t() will try to access the database and
// subsequently will fail as the database is not accessible.
$module_list = module_list();
if (isset($module_list['locale'])) {
$this->originalModuleList = $module_list;
unset($module_list['locale']);
module_list(TRUE, FALSE, FALSE, $module_list);
}
$this->setup = TRUE;
}
protected function tearDown() {
global $conf;
// Get back to the original connection.
Database::removeConnection('default');
Database::renameConnection('simpletest_original_default', 'default');
$conf['file_public_path'] = $this->originalFileDirectory;
// Restore modules if necessary.
if (isset($this->originalModuleList)) {
module_list(TRUE, FALSE, FALSE, $this->originalModuleList);
}
}
}
......@@ -534,117 +534,6 @@ protected function drupalLogout() {
}
}
/**
* Generates a database prefix for running tests.
*
* The generated database table prefix is used for the Drupal installation
* being performed for the test. It is also used as user agent HTTP header
* value by the cURL-based browser of Drupal\simpletest\WebTestBase, which is sent
* to the Drupal installation of the test. During early Drupal bootstrap, the
* user agent HTTP header is parsed, and if it matches, all database queries
* use the database table prefix that has been generated here.
*
* @see Drupal\simpletest\WebTestBase::curlInitialize()
* @see drupal_valid_test_ua()
* @see Drupal\simpletest\WebTestBase::setUp()
*/
protected function prepareDatabasePrefix() {
$this->databasePrefix = 'simpletest' . mt_rand(1000, 1000000);
// As soon as the database prefix is set, the test might start to execute.
// All assertions as well as the SimpleTest batch operations are associated
// with the testId, so the database prefix has to be associated with it.
db_update('simpletest_test_id')
->fields(array('last_prefix' => $this->databasePrefix))
->condition('test_id', $this->testId)
->execute();
}
/**
* Changes the database connection to the prefixed one.
*
* @see Drupal\simpletest\WebTestBase::setUp()
*/
protected function changeDatabasePrefix() {
if (empty($this->databasePrefix)) {
$this->prepareDatabasePrefix();
}
// Clone the current connection and replace the current prefix.
$connection_info = Database::getConnectionInfo('default');
Database::renameConnection('default', 'simpletest_original_default');
foreach ($connection_info as $target => $value) {
$connection_info[$target]['prefix'] = array(
'default' => $value['prefix']['default'] . $this->databasePrefix,
);
}
Database::addConnectionInfo('default', 'default', $connection_info['default']);
}
/**
* Prepares the current environment for running the test.
*
* Backups various current environment variables and resets them, so they do
* not interfere with the Drupal site installation in which tests are executed
* and can be restored in tearDown().
*
* Also sets up new resources for the testing environment, such as the public
* filesystem and configuration directories.
*
* @see Drupal\simpletest\WebTestBase::setUp()
* @see Drupal\simpletest\WebTestBase::tearDown()
*/
protected function prepareEnvironment() {
global $user, $language_interface, $conf;
// Store necessary current values before switching to prefixed database.
$this->originalContainer = clone drupal_container();
$this->originalLanguage = $language_interface;