/** * Grant permissions to a specific role, if it exists. * * @param string $role * Role machine name. * @param string $permission * Permission machine name. * @param string $module * Module name. * * @return bool * TRUE if operation was successful, FALSE otherwise. */ public function grantPermission($role, $permission, $module = NULL) { $permission_rebuilt =& drupal_static(__CLASS__ . ':' . __FUNCTION__); if (!$permission_rebuilt) { // Make sure the list of available node types is up to date. node_types_rebuild(); // Reset hook_permission() cached information. module_implements('permission', FALSE, TRUE); $permission_rebuilt = TRUE; } $permissions = is_array($permission) ? $permission : array($permission); $role_object = user_role_load_by_name($role); if ($role_object) { // Use code from user_role_grant_permissions() in order to be able // to force medule field in special cases. $modules = user_permission_get_modules(); // Grant new permissions for the role. foreach ($permissions as $name) { $modules[$name] = isset($modules[$name]) ? $modules[$name] : $module; db_merge('role_permission')->key(array('rid' => $role_object->rid, 'permission' => $name))->fields(array('module' => $modules[$name]))->execute(); } // Clear the user access cache. drupal_static_reset('user_access'); drupal_static_reset('user_role_permissions'); return TRUE; } else { return FALSE; } }
/** * Create a petition content type and webform user enable it. */ protected function webformUserCreateType() { $type = node_type_set_defaults(); $type->name = t('Petition'); $type->type = 'petition'; $type->description = t('Webform user petition type.'); $type->title_label = t('Title'); $type->has_title = $type->title_label != ''; $type->base = 'node_content'; $type->custom = TRUE; $type->modified = TRUE; // Save or reset persistent variable values. $variables = array('node_submitted' => 0, 'comment' => COMMENT_NODE_HIDDEN, 'webform_user' => 1, 'webform_user_default_fields' => array('webform_user_all_profile_fields' => 'webform_user_all_profile_fields')); foreach ($variables as $key => $value) { $variable_new = $key . '_' . $type->type; if (is_array($value)) { $value = array_keys(array_filter($value)); } variable_set($variable_new, $value); } $status = node_type_save($type); node_types_rebuild(); node_add_body_field($type); // Add as a webform. $webform_node_types = variable_get('webform_node_types', array('webform')); $webform_node_types_primary = variable_get('webform_node_types_primary', array('webform')); $webform_node_types = array_merge($webform_node_types, array('petition')); $webform_node_types_primary = array_merge($webform_node_types_primary, array('petition')); variable_set('webform_node_types', array_unique($webform_node_types)); variable_set('webform_node_types_primary', array_unique($webform_node_types_primary)); }
/** * Create test content type. */ protected static function createTestContentType() { $name = self::randomName(8); $type = (object) array('type' => $name, 'name' => $name, 'base' => 'node_content', 'description' => '', 'help' => '', 'title_label' => 'Title', 'body_label' => 'Body', 'has_title' => 1, 'has_body' => 1, 'orig_type' => '', 'old_type' => '', 'module' => 'node', 'custom' => 1, 'modified' => 1, 'locked' => 0); node_type_save($type); node_types_rebuild(); node_add_body_field($type); self::$contentType = $type; }
/** * Registry Rebuild needs to aggressively clear all caches, * not just some bins (at least to attempt it) also *before* * attempting to rebuild registry, or it may not be able * to fix the problem at all, if it relies on some cached * and no longer valid data/paths etc. This problem has been * confirmed and reproduced many times with option --fire-bazooka * which is available only in the Drush variant, but it confirms * the importance of starting with real, raw and not cached * in any way site state. While the --no-cache-clear option * still disables this procedure, --fire-bazooka takes precedence * and forces all caches clear action. All caches are cleared * by default in the PHP script variant. */ function registry_rebuild_cc_all() { if (function_exists('cache_clear_all')) { cache_clear_all('*', 'cache', TRUE); cache_clear_all('*', 'cache_form', TRUE); } else { cache('cache')->deleteAll(); cache('cache_form')->deleteAll(); } if (function_exists('module_rebuild_cache')) { // D5-D6 module_list(TRUE, FALSE); module_rebuild_cache(); } if (function_exists('drupal_flush_all_caches')) { // D6+ drupal_flush_all_caches(); } else { // D5 cache_clear_all(); system_theme_data(); node_types_rebuild(); menu_rebuild(); } print "All caches have been cleared with registry_rebuild_cc_all.<br/>\n"; }
/** * Creates a custom content type based on default settings. * * @param $settings * An array of settings to change from the defaults. * Example: 'type' => 'foo'. * @return * Created content type. */ protected function drupalCreateContentType($settings = array()) { // Find a non-existent random type name. do { $name = strtolower($this->randomName(8)); } while (node_type_get_type($name)); // Populate defaults array. $defaults = array('type' => $name, 'name' => $name, 'base' => 'node_content', 'description' => '', 'help' => '', 'title_label' => 'Title', 'body_label' => 'Body', 'has_title' => 1, 'has_body' => 1); // Imposed values for a custom type. $forced = array('orig_type' => '', 'old_type' => '', 'module' => 'node', 'custom' => 1, 'modified' => 1, 'locked' => 0); $type = $forced + $settings + $defaults; $type = (object) $type; $saved_type = node_type_save($type); node_types_rebuild(); menu_rebuild(); node_add_body_field($type); $this->assertEqual($saved_type, SAVED_NEW, t('Created content type %type.', array('%type' => $type->type))); // Reset permissions so that permissions for this content type are available. $this->checkPermissions(array(), TRUE); return $type; }
/** * Generates a random database prefix, runs the install scripts on the * prefixed database and enable the specified modules. After installation * many caches are flushed and the internal browser is setup so that the * page requests will run on the new prefix. A temporary files directory * is created with the same name as the database prefix. * * @param ... * List of modules to enable for the duration of the test. */ protected function setUp() { global $db_prefix, $user, $language; // Store necessary current values before switching to prefixed database. $this->originalLanguage = $language; $this->originalLanguageDefault = variable_get('language_default'); $this->originalPrefix = $db_prefix; $this->originalFileDirectory = file_directory_path(); $this->originalProfile = drupal_get_profile(); $clean_url_original = variable_get('clean_url', 0); // Generate temporary prefixed database to ensure that tests have a clean starting point. $db_prefix_new = Database::getConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}'); db_update('simpletest_test_id')->fields(array('last_prefix' => $db_prefix_new))->condition('test_id', $this->testId)->execute(); $db_prefix = $db_prefix_new; // 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. $public_files_directory = $this->originalFileDirectory . '/simpletest/' . substr($db_prefix, 10); $private_files_directory = $public_files_directory . '/private'; $temp_files_directory = $private_files_directory . '/temp'; // Create the directories file_prepare_directory($public_files_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); file_prepare_directory($private_files_directory, FILE_CREATE_DIRECTORY); file_prepare_directory($temp_files_directory, FILE_CREATE_DIRECTORY); $this->generatedTestFiles = FALSE; // Log fatal errors. ini_set('log_errors', 1); ini_set('error_log', $public_files_directory . '/error.log'); // Reset all statics so that test is performed with a clean environment. drupal_static_reset(); include_once DRUPAL_ROOT . '/includes/install.inc'; drupal_install_system(); $this->preloadRegistry(); // Include the default profile variable_set('install_profile', 'standard'); $profile_details = install_profile_info('standard', 'en'); // Install the modules specified by the default profile. drupal_install_modules($profile_details['dependencies'], TRUE); drupal_static_reset('_node_types_build'); if ($modules = func_get_args()) { // Install modules needed for this test. drupal_install_modules($modules, TRUE); } // Because the schema is static cached, we need to flush // it between each run. If we don't, then it will contain // stale data for the previous run's database prefix and all // calls to it will fail. drupal_get_schema(NULL, TRUE); // Run default profile tasks. $install_state = array(); drupal_install_modules(array('standard'), TRUE); // Rebuild caches. node_types_rebuild(); actions_synchronize(); _drupal_flush_css_js(); $this->refreshVariables(); $this->checkPermissions(array(), TRUE); // Log in with a clean $user. $this->originalUser = $user; drupal_save_session(FALSE); $user = user_load(1); // Restore necessary variables. variable_set('install_task', 'done'); variable_set('clean_url', $clean_url_original); variable_set('site_mail', '*****@*****.**'); // Set up English language. unset($GLOBALS['conf']['language_default']); $language = language_default(); // Set path variables variable_set('file_public_path', $public_files_directory); variable_set('file_private_path', $private_files_directory); variable_set('file_temporary_path', $temp_files_directory); // Use the test mail class instead of the default mail handler class. variable_set('mail_system', array('default-system' => 'TestingMailSystem')); drupal_set_time_limit($this->timeLimit); }
/** * Disables a drupal module in the test database. * * @param string $name Name of the module. * @return boolean Success. * @see drupalModuleEnable() */ function drupalModuleDisable($name) { if (!module_exists($name)) { $this->pass(" [module] {$name} already disabled"); return TRUE; } unset($this->_modules[$key]); $form_state['values'] = array('status' => $this->_modules, 'op' => t('Save configuration')); drupal_execute('system_modules', $form_state); //rebuilding all caches drupal_rebuild_theme_registry(); node_types_rebuild(); menu_rebuild(); cache_clear_all('schema', 'cache'); module_rebuild_cache(); }
/** * Installation task; perform final steps and display a 'finished' page. * * @param $install_state * An array of information about the current installation state. * @return * A message informing the user that the installation is complete. */ function install_finished(&$install_state) { drupal_set_title(st('@drupal installation complete', array('@drupal' => drupal_install_profile_name())), PASS_THROUGH); $messages = drupal_set_message(); $output = '<p>' . st('Congratulations, @drupal has been successfully installed.', array('@drupal' => drupal_install_profile_name())) . '</p>'; $output .= '<p>' . (isset($messages['error']) ? st('Review the messages above before continuing on to <a href="@url">your new site</a>.', array('@url' => url(''))) : st('You may now visit <a href="@url">your new site</a>.', array('@url' => url('')))) . '</p>'; if (module_exists('help')) { $output .= '<p>' . st('For more information on configuring Drupal, refer to the <a href="@help">help section</a>.', array('@help' => url('admin/help'))) . '</p>'; } // Rebuild the module and theme data, in case any newly-installed modules // need to modify it via hook_system_info_alter(). We need to clear the // theme static cache first, to make sure that the theme data is actually // rebuilt. drupal_static_reset('_system_rebuild_theme_data'); system_rebuild_module_data(); system_rebuild_theme_data(); // Rebuild menu and registry to get content type links registered by the // profile, and possibly any other menu items created through the tasks. menu_rebuild(); // Rebuild the database cache of node types, so that any node types added // by newly installed modules are registered correctly and initialized with // the necessary fields. node_types_rebuild(); // Register actions declared by any modules. actions_synchronize(); // Randomize query-strings on css/js files, to hide the fact that this is a // new install, not upgraded yet. _drupal_flush_css_js(); // Remember the profile which was used. variable_set('install_profile', drupal_get_profile()); // Install profiles are always loaded last db_update('system')->fields(array('weight' => 1000))->condition('type', 'module')->condition('name', drupal_get_profile())->execute(); // Cache a fully-built schema. drupal_get_schema(NULL, TRUE); // Run cron to populate update status tables (if available) so that users // will be warned if they've installed an out of date Drupal version. // Will also trigger indexing of profile-supplied content or feeds. drupal_cron_run(); return $output; }
/** * Generates a random database prefix, runs the install scripts on the * prefixed database and enable the specified modules. After installation * many caches are flushed and the internal browser is setup so that the * page requests will run on the new prefix. A temporary files directory * is created with the same name as the database prefix. * * @param ... * List of modules to enable for the duration of the test. */ protected function setUp() { global $db_prefix, $user, $language; // Store necessary current values before switching to prefixed database. $this->originalLanguage = $language; $this->originalLanguageDefault = variable_get('language_default'); $this->originalPrefix = $db_prefix; $this->originalFileDirectory = file_directory_path(); $clean_url_original = variable_get('clean_url', 0); // Generate temporary prefixed database to ensure that tests have a clean starting point. $db_prefix = Database::getConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}'); include_once DRUPAL_ROOT . '/includes/install.inc'; drupal_install_system(); $this->preloadRegistry(); // Add the specified modules to the list of modules in the default profile. // Install the modules specified by the default profile. $core_modules = drupal_get_profile_modules('default', 'en'); drupal_install_modules($core_modules, TRUE); node_type_clear(); // Install additional modules one at a time in order to make sure that the // list of modules is updated between each module's installation. $modules = func_get_args(); foreach ($modules as $module) { drupal_install_modules(array($module), TRUE); } // Because the schema is static cached, we need to flush // it between each run. If we don't, then it will contain // stale data for the previous run's database prefix and all // calls to it will fail. drupal_get_schema(NULL, TRUE); // Run default profile tasks. $task = 'profile'; default_profile_tasks($task, ''); // Rebuild caches. node_types_rebuild(); actions_synchronize(); _drupal_flush_css_js(); $this->refreshVariables(); $this->checkPermissions(array(), TRUE); // Log in with a clean $user. $this->originalUser = $user; drupal_save_session(FALSE); $user = user_load(1); // Restore necessary variables. variable_set('install_profile', 'default'); variable_set('install_task', 'profile-finished'); variable_set('clean_url', $clean_url_original); variable_set('site_mail', '*****@*****.**'); // Set up English language. unset($GLOBALS['conf']['language_default']); $language = language_default(); // Make sure our drupal_mail_wrapper function is called instead of the // default mail handler. variable_set('smtp_library', drupal_get_path('module', 'simpletest') . '/drupal_web_test_case.php'); // Use temporary files directory with the same prefix as database. variable_set('file_directory_path', $this->originalFileDirectory . '/' . $db_prefix); $directory = file_directory_path(); // Create the files directory. file_check_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); set_time_limit($this->timeLimit); }
/** * lms_create_content_type create content type by given information. * * @param mixed $content_type_machine_name * @param mixed $content_type_display_name * @param mixed $content_type_desc * @return void */ function lms_create_content_type($content_type_machine_name, $content_type_display_name, $content_type_desc, $og_type = 'group_post_standard') { // 0. delete the existing type with the same name if exists node_type_delete($content_type_machine_name); // 1. Create content type firstly. $node_type = array( 'type' => $content_type_machine_name, 'name' => $content_type_display_name, 'module' => 'node', 'description' => $content_type_desc, 'custom' => TRUE, 'modified' => TRUE, 'locked' => FALSE, ); $node_type = (object)_node_type_set_defaults($node_type); node_type_save($node_type); // // 2. Add fileds to the content type // $instance = array( // 'field_name' => 'lms_node_type', // 'entity_type' => 'node', // 'bundle' => $content_type_machine_name, // 'label' => t('lms node name'), // 'description' => t('indicator of the node type'), // 'widget' => array( // 'type' => 'text_textfield', // 'weight' => 10, // ), // ); // field_create_instance($instance); node_types_rebuild(); menu_rebuild(); // set og type if ($og_type && $og_type != 'ommitted') { lms_set_content_type_as_og_group_post($content_type_machine_name, $og_type); } }
/** * Implements Drupal\configuration\Config\Configuration::saveToActiveStore(). */ public function saveToActiveStore(ConfigIteratorSettings &$settings) { node_types_rebuild(); $exist = FALSE; $roles = static::get_roles(); $permissions_by_role = static::get_permissions(FALSE); $map = user_permission_get_modules(); $permission = $this->getData(); $perm = $permission['permission']; foreach ($roles as $role) { if (isset($map[$perm])) { $exist = TRUE; if (in_array($role, $permission['roles'])) { $permissions_by_role[$role][$perm] = TRUE; } else { $permissions_by_role[$role][$perm] = FALSE; } } } if (!$exist) { drupal_set_message(t('Configuration Management: Permission %permission does not exist and can not be set.', array('%permission' => $perm)), 'error'); } // Write the updated permissions. foreach ($roles as $rid => $role) { if (isset($permissions_by_role[$role])) { user_role_change_permissions($rid, $permissions_by_role[$role]); } } $settings->addInfo('imported', $this->getUniqueId()); }
/** * tearDown implementation, setting back switched modules etc */ function tearDown() { if ($this->_modules != $this->_originalModules) { $form_state['values'] = array('status' => $this->_originalModules, 'op' => t('Save configuration')); drupal_execute('system_modules', $form_state); //rebuilding all caches drupal_rebuild_theme_registry(); node_types_rebuild(); menu_rebuild(); cache_clear_all('schema', 'cache'); module_rebuild_cache(); $this->_modules = $this->_originalModules; } foreach ($this->_cleanupVariables as $name => $value) { if (is_null($value)) { variable_del($name); } else { variable_set($name, $value); } } $this->_cleanupVariables = array(); //delete nodes foreach ($this->_cleanupNodes as $nid) { node_delete($nid); } $this->_cleanupNodes = array(); //delete roles while (sizeof($this->_cleanupRoles) > 0) { $rid = array_pop($this->_cleanupRoles); db_query("DELETE FROM {role} WHERE rid = %d", $rid); db_query("DELETE FROM {permission} WHERE rid = %d", $rid); } //delete users and their content while (sizeof($this->_cleanupUsers) > 0) { $uid = array_pop($this->_cleanupUsers); // cleanup nodes this user created $result = db_query("SELECT nid FROM {node} WHERE uid = %d", $uid); while ($node = db_fetch_array($result)) { node_delete($node['nid']); } user_delete(array(), $uid); } //delete content types foreach ($this->_cleanupContentTypes as $type) { node_type_delete($type); } $this->_cleanupContentTypes = array(); //Output drupal warnings and messages into assert messages $drupal_msgs = drupal_get_messages(); foreach ($drupal_msgs as $type => $msgs) { foreach ($msgs as $msg) { $this->assertTrue(TRUE, "{$type}: {$msg}"); } } parent::tearDown(); }