Ejemplo n.º 1
0
 /**
  * Determines which Updater class can operate on the given directory.
  *
  * @param string $directory
  *   Extracted Drupal project.
  *
  * @return string
  *   The class name which can work with this project type.
  *
  * @throws \Drupal\Core\Updater\UpdaterException
  */
 public static function getUpdaterFromDirectory($directory)
 {
     // Gets a list of possible implementing classes.
     $updaters = drupal_get_updaters();
     foreach ($updaters as $updater) {
         $class = $updater['class'];
         if (call_user_func(array($class, 'canUpdateDirectory'), $directory)) {
             return $class;
         }
     }
     throw new UpdaterException(t('Cannot determine the type of project.'));
 }
Ejemplo n.º 2
0
 /**
  * Tests upload and extraction of a module.
  */
 public function testUploadModule()
 {
     // Images are not valid archives, so get one and try to install it. We
     // need an extra variable to store the result of drupalGetTestFiles()
     // since reset() takes an argument by reference and passing in a constant
     // emits a notice in strict mode.
     $imageTestFiles = $this->drupalGetTestFiles('image');
     $invalidArchiveFile = reset($imageTestFiles);
     $edit = array('files[project_upload]' => $invalidArchiveFile->uri);
     // This also checks that the correct archive extensions are allowed.
     $this->drupalPostForm('admin/modules/install', $edit, t('Install'));
     $this->assertText(t('Only files with the following extensions are allowed: @archive_extensions.', array('@archive_extensions' => archiver_get_extensions())), 'Only valid archives can be uploaded.');
     $this->assertUrl('admin/modules/install');
     // Check to ensure an existing module can't be reinstalled. Also checks that
     // the archive was extracted since we can't know if the module is already
     // installed until after extraction.
     $validArchiveFile = drupal_get_path('module', 'update') . '/tests/aaa_update_test.tar.gz';
     $edit = array('files[project_upload]' => $validArchiveFile);
     $this->drupalPostForm('admin/modules/install', $edit, t('Install'));
     $this->assertText(t('@module_name is already installed.', array('@module_name' => 'AAA Update test')), 'Existing module was extracted and not reinstalled.');
     $this->assertUrl('admin/modules/install');
     // Ensure that a new module can be extracted and installed.
     $updaters = drupal_get_updaters();
     $moduleUpdater = $updaters['module']['class'];
     $installedInfoFilePath = $this->container->get('update.root') . '/' . $moduleUpdater::getRootDirectoryRelativePath() . '/update_test_new_module/update_test_new_module.info.yml';
     $this->assertFalse(file_exists($installedInfoFilePath), 'The new module does not exist in the filesystem before it is installed with the Update Manager.');
     $validArchiveFile = drupal_get_path('module', 'update') . '/tests/update_test_new_module.tar.gz';
     $edit = array('files[project_upload]' => $validArchiveFile);
     $this->drupalPostForm('admin/modules/install', $edit, t('Install'));
     // Check that submitting the form takes the user to authorize.php.
     $this->assertUrl('core/authorize.php');
     // Check for a success message on the page, and check that the installed
     // module now exists in the expected place in the filesystem.
     $this->assertRaw(t('Installed %project_name successfully', array('%project_name' => 'update_test_new_module')));
     $this->assertTrue(file_exists($installedInfoFilePath), 'The new module exists in the filesystem after it is installed with the Update Manager.');
     // Ensure the links are relative to the site root and not
     // core/authorize.php.
     $this->assertLink(t('Install another module'));
     $this->assertLinkByHref(Url::fromRoute('update.module_install')->toString());
     $this->assertLink(t('Enable newly added modules'));
     $this->assertLinkByHref(Url::fromRoute('system.modules_list')->toString());
     $this->assertLink(t('Administration pages'));
     $this->assertLinkByHref(Url::fromRoute('system.admin')->toString());
     // Ensure we can reach the "Install another module" link.
     $this->clickLink(t('Install another module'));
     $this->assertResponse(200);
     $this->assertUrl('admin/modules/install');
 }
Ejemplo n.º 3
0
 protected function setUp()
 {
     parent::setUp();
     // Change the root path which Update Manager uses to install and update
     // projects to be inside the testing site directory. See
     // \Drupal\update\UpdateRootFactory::get() for equivalent changes to the
     // test child site.
     $request = \Drupal::request();
     $update_root = $this->container->get('update.root') . '/' . DrupalKernel::findSitePath($request);
     $this->container->set('update.root', $update_root);
     \Drupal::setContainer($this->container);
     // Create the directories within the root path within which the Update
     // Manager will install projects.
     foreach (drupal_get_updaters() as $updater_info) {
         $updater = $updater_info['class'];
         $install_directory = $update_root . '/' . $updater::getRootDirectoryRelativePath();
         if (!is_dir($install_directory)) {
             mkdir($install_directory);
         }
     }
 }
Ejemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, array &$form_state)
 {
     // Store maintenance_mode setting so we can restore it when done.
     $_SESSION['maintenance_mode'] = $this->state->get('system.maintenance_mode');
     if ($form_state['values']['maintenance_mode'] == TRUE) {
         $this->state->set('system.maintenance_mode', TRUE);
     }
     if (!empty($_SESSION['update_manager_update_projects'])) {
         // Make sure the Updater registry is loaded.
         drupal_get_updaters();
         $updates = array();
         $directory = _update_manager_extract_directory();
         $projects = $_SESSION['update_manager_update_projects'];
         unset($_SESSION['update_manager_update_projects']);
         $project_real_location = NULL;
         foreach ($projects as $project => $url) {
             $project_location = $directory . '/' . $project;
             $updater = Updater::factory($project_location);
             $project_real_location = drupal_realpath($project_location);
             $updates[] = array('project' => $project, 'updater_name' => get_class($updater), 'local_url' => $project_real_location);
         }
         // If the owner of the last directory we extracted is the same as the
         // owner of our configuration directory (e.g. sites/default) where we're
         // trying to install the code, there's no need to prompt for FTP/SSH
         // credentials. Instead, we instantiate a Drupal\Core\FileTransfer\Local
         // and invoke update_authorize_run_update() directly.
         if (fileowner($project_real_location) == fileowner(conf_path())) {
             $this->moduleHandler->loadInclude('update', 'inc', 'update.authorize');
             $filetransfer = new Local(DRUPAL_ROOT);
             update_authorize_run_update($filetransfer, $updates);
         } else {
             system_authorized_init('update_authorize_run_update', drupal_get_path('module', 'update') . '/update.authorize.inc', array($updates), $this->t('Update manager'));
             $form_state['redirect'] = system_authorized_get_url();
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $local_cache = NULL;
     if ($form_state->getValue('project_url')) {
         $local_cache = update_manager_file_get($form_state->getValue('project_url'));
         if (!$local_cache) {
             drupal_set_message($this->t('Unable to retrieve Drupal project from %url.', array('%url' => $form_state->getValue('project_url'))), 'error');
             return;
         }
     } elseif ($_FILES['files']['name']['project_upload']) {
         $validators = array('file_validate_extensions' => array(archiver_get_extensions()));
         if (!($finfo = file_save_upload('project_upload', $validators, NULL, 0, FILE_EXISTS_REPLACE))) {
             // Failed to upload the file. file_save_upload() calls
             // drupal_set_message() on failure.
             return;
         }
         $local_cache = $finfo->getFileUri();
     }
     $directory = _update_manager_extract_directory();
     try {
         $archive = update_manager_archive_extract($local_cache, $directory);
     } catch (\Exception $e) {
         drupal_set_message($e->getMessage(), 'error');
         return;
     }
     $files = $archive->listContents();
     if (!$files) {
         drupal_set_message($this->t('Provided archive contains no files.'), 'error');
         return;
     }
     // Unfortunately, we can only use the directory name to determine the
     // project name. Some archivers list the first file as the directory (i.e.,
     // MODULE/) and others list an actual file (i.e., MODULE/README.TXT).
     $project = strtok($files[0], '/\\');
     $archive_errors = $this->moduleHandler->invokeAll('verify_update_archive', array($project, $local_cache, $directory));
     if (!empty($archive_errors)) {
         drupal_set_message(array_shift($archive_errors), 'error');
         // @todo: Fix me in D8: We need a way to set multiple errors on the same
         // form element and have all of them appear!
         if (!empty($archive_errors)) {
             foreach ($archive_errors as $error) {
                 drupal_set_message($error, 'error');
             }
         }
         return;
     }
     // Make sure the Updater registry is loaded.
     drupal_get_updaters();
     $project_location = $directory . '/' . $project;
     try {
         $updater = Updater::factory($project_location, $this->root);
     } catch (\Exception $e) {
         drupal_set_message($e->getMessage(), 'error');
         return;
     }
     try {
         $project_title = Updater::getProjectTitle($project_location);
     } catch (\Exception $e) {
         drupal_set_message($e->getMessage(), 'error');
         return;
     }
     if (!$project_title) {
         drupal_set_message($this->t('Unable to determine %project name.', array('%project' => $project)), 'error');
     }
     if ($updater->isInstalled()) {
         drupal_set_message($this->t('%project is already installed.', array('%project' => $project_title)), 'error');
         return;
     }
     $project_real_location = drupal_realpath($project_location);
     $arguments = array('project' => $project, 'updater_name' => get_class($updater), 'local_url' => $project_real_location);
     // This process is inherently difficult to test therefore use a state flag.
     $test_authorize = FALSE;
     if (drupal_valid_test_ua()) {
         $test_authorize = \Drupal::state()->get('test_uploaders_via_prompt', FALSE);
     }
     // If the owner of the directory we extracted is the same as the owner of
     // our configuration directory (e.g. sites/default) where we're trying to
     // install the code, there's no need to prompt for FTP/SSH credentials.
     // Instead, we instantiate a Drupal\Core\FileTransfer\Local and invoke
     // update_authorize_run_install() directly.
     if (fileowner($project_real_location) == fileowner($this->sitePath) && !$test_authorize) {
         $this->moduleHandler->loadInclude('update', 'inc', 'update.authorize');
         $filetransfer = new Local($this->root);
         $response = call_user_func_array('update_authorize_run_install', array_merge(array($filetransfer), $arguments));
         if ($response instanceof Response) {
             $form_state->setResponse($response);
         }
     } else {
         // The page title must be passed here to ensure it is initially used when
         // authorize.php loads for the first time with the FTP/SSH credentials
         // form.
         system_authorized_init('update_authorize_run_install', __DIR__ . '/../../update.authorize.inc', $arguments, $this->t('Update manager'));
         $form_state->setRedirectUrl(system_authorized_get_url());
     }
 }
Ejemplo n.º 6
0
 /**
  * Tests upload, extraction, and update of a module.
  */
 public function testUploadModule()
 {
     // Images are not valid archives, so get one and try to install it. We
     // need an extra variable to store the result of drupalGetTestFiles()
     // since reset() takes an argument by reference and passing in a constant
     // emits a notice in strict mode.
     $imageTestFiles = $this->drupalGetTestFiles('image');
     $invalidArchiveFile = reset($imageTestFiles);
     $edit = array('files[project_upload]' => $invalidArchiveFile->uri);
     // This also checks that the correct archive extensions are allowed.
     $this->drupalPostForm('admin/modules/install', $edit, t('Install'));
     $this->assertText(t('Only files with the following extensions are allowed: @archive_extensions.', array('@archive_extensions' => archiver_get_extensions())), 'Only valid archives can be uploaded.');
     $this->assertUrl('admin/modules/install');
     // Check to ensure an existing module can't be reinstalled. Also checks that
     // the archive was extracted since we can't know if the module is already
     // installed until after extraction.
     $validArchiveFile = __DIR__ . '/../../tests/aaa_update_test.tar.gz';
     $edit = array('files[project_upload]' => $validArchiveFile);
     $this->drupalPostForm('admin/modules/install', $edit, t('Install'));
     $this->assertText(t('@module_name is already installed.', array('@module_name' => 'AAA Update test')), 'Existing module was extracted and not reinstalled.');
     $this->assertUrl('admin/modules/install');
     // Ensure that a new module can be extracted and installed.
     $updaters = drupal_get_updaters();
     $moduleUpdater = $updaters['module']['class'];
     $installedInfoFilePath = $this->container->get('update.root') . '/' . $moduleUpdater::getRootDirectoryRelativePath() . '/update_test_new_module/update_test_new_module.info.yml';
     $this->assertFalse(file_exists($installedInfoFilePath), 'The new module does not exist in the filesystem before it is installed with the Update Manager.');
     $validArchiveFile = __DIR__ . '/../../tests/update_test_new_module/8.x-1.0/update_test_new_module.tar.gz';
     $edit = array('files[project_upload]' => $validArchiveFile);
     $this->drupalPostForm('admin/modules/install', $edit, t('Install'));
     // Check that submitting the form takes the user to authorize.php.
     $this->assertUrl('core/authorize.php');
     $this->assertTitle('Update manager | Drupal');
     // Check for a success message on the page, and check that the installed
     // module now exists in the expected place in the filesystem.
     $this->assertRaw(t('Installed %project_name successfully', array('%project_name' => 'update_test_new_module')));
     $this->assertTrue(file_exists($installedInfoFilePath), 'The new module exists in the filesystem after it is installed with the Update Manager.');
     // Ensure the links are relative to the site root and not
     // core/authorize.php.
     $this->assertLink(t('Install another module'));
     $this->assertLinkByHref(Url::fromRoute('update.module_install')->toString());
     $this->assertLink(t('Enable newly added modules'));
     $this->assertLinkByHref(Url::fromRoute('system.modules_list')->toString());
     $this->assertLink(t('Administration pages'));
     $this->assertLinkByHref(Url::fromRoute('system.admin')->toString());
     // Ensure we can reach the "Install another module" link.
     $this->clickLink(t('Install another module'));
     $this->assertResponse(200);
     $this->assertUrl('admin/modules/install');
     // Check that the module has the correct version before trying to update
     // it. Since the module is installed in sites/simpletest, which only the
     // child site has access to, standard module API functions won't find it
     // when called here. To get the version, the info file must be parsed
     // directly instead.
     $info_parser = new InfoParserDynamic();
     $info = $info_parser->parse($installedInfoFilePath);
     $this->assertEqual($info['version'], '8.x-1.0');
     // Enable the module.
     $this->drupalPostForm('admin/modules', array('modules[Testing][update_test_new_module][enable]' => TRUE), t('Install'));
     // Define the update XML such that the new module downloaded above needs an
     // update from 8.x-1.0 to 8.x-1.1.
     $update_test_config = $this->config('update_test.settings');
     $system_info = array('update_test_new_module' => array('project' => 'update_test_new_module'));
     $update_test_config->set('system_info', $system_info)->save();
     $xml_mapping = array('update_test_new_module' => '1_1');
     $this->refreshUpdateStatus($xml_mapping);
     // Run the updates for the new module.
     $this->drupalPostForm('admin/reports/updates/update', array('projects[update_test_new_module]' => TRUE), t('Download these updates'));
     $this->drupalPostForm(NULL, array('maintenance_mode' => FALSE), t('Continue'));
     $this->assertText(t('Update was completed successfully.'));
     $this->assertRaw(t('Installed %project_name successfully', array('%project_name' => 'update_test_new_module')));
     // Parse the info file again to check that the module has been updated to
     // 8.x-1.1.
     $info = $info_parser->parse($installedInfoFilePath);
     $this->assertEqual($info['version'], '8.x-1.1');
 }
Ejemplo n.º 7
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     // Store maintenance_mode setting so we can restore it when done.
     $_SESSION['maintenance_mode'] = $this->state->get('system.maintenance_mode');
     if ($form_state->getValue('maintenance_mode') == TRUE) {
         $this->state->set('system.maintenance_mode', TRUE);
     }
     if (!empty($_SESSION['update_manager_update_projects'])) {
         // Make sure the Updater registry is loaded.
         drupal_get_updaters();
         $updates = array();
         $directory = _update_manager_extract_directory();
         $projects = $_SESSION['update_manager_update_projects'];
         unset($_SESSION['update_manager_update_projects']);
         $project_real_location = NULL;
         foreach ($projects as $project => $url) {
             $project_location = $directory . '/' . $project;
             $updater = Updater::factory($project_location, $this->root);
             $project_real_location = drupal_realpath($project_location);
             $updates[] = array('project' => $project, 'updater_name' => get_class($updater), 'local_url' => $project_real_location);
         }
         // If the owner of the last directory we extracted is the same as the
         // owner of our configuration directory (e.g. sites/default) where we're
         // trying to install the code, there's no need to prompt for FTP/SSH
         // credentials. Instead, we instantiate a Drupal\Core\FileTransfer\Local
         // and invoke update_authorize_run_update() directly.
         if (fileowner($project_real_location) == fileowner($this->sitePath)) {
             $this->moduleHandler->loadInclude('update', 'inc', 'update.authorize');
             $filetransfer = new Local($this->root);
             $response = update_authorize_run_update($filetransfer, $updates);
             if ($response instanceof Response) {
                 $form_state->setResponse($response);
             }
         } else {
             // The page title must be passed here to ensure it is initially used
             // when authorize.php loads for the first time with the FTP/SSH
             // credentials form.
             system_authorized_init('update_authorize_run_update', __DIR__ . '/../../update.authorize.inc', array($updates), $this->t('Update manager'));
             $form_state->setRedirectUrl(system_authorized_get_url());
         }
     }
 }
Ejemplo n.º 8
0
 /**
  * @param string $extension
  *
  * @return array Context result generated by Drupal.
  *
  * @throws Oxygen_Exception
  *
  * @see update_authorize_run_update()
  */
 public function updateExtension($extension)
 {
     module_load_include('inc', 'update', 'update.manager');
     drupal_get_updaters();
     $directory = _update_manager_extract_directory();
     $projectLocation = $directory . '/' . $extension;
     $updater = Updater::factory($projectLocation);
     $projectRealLocation = drupal_realpath($projectLocation);
     // If the owner of the directory we extracted is the same as the
     // owner of our configuration directory (e.g. sites/default) where we're
     // trying to install the code, there's no need to prompt for FTP/SSH
     // credentials. Instead, we instantiate a FileTransferLocal and invoke
     // update_authorize_run_install() directly.
     if (fileowner($projectRealLocation) !== fileowner(DRUPAL_ROOT . '/' . conf_path())) {
         throw new Oxygen_Exception(Oxygen_Exception::PROJECT_MANAGER_FILE_SYSTEM_NOT_WRITABLE, array('projectOwner' => fileowner($projectRealLocation), 'siteOwner' => fileowner(conf_path())));
     }
     module_load_include('inc', 'update', 'update.authorize');
     // @TODO: Implement other file transfer types.
     $fileTransfer = new FileTransferLocal(DRUPAL_ROOT);
     update_authorize_batch_copy_project($extension, get_class($updater), $projectRealLocation, $fileTransfer, $context);
     // Reset the cache for this extension only, so we may immediately update the dashboard.
     // Hacky way to do it, but most efficient.
     module_load_include('inc', 'update', 'update.fetch');
     module_load_include('inc', 'update', 'update.compare');
     _update_cache_clear('update_project_projects');
     _update_cache_clear('update_project_data');
     $projects = update_get_projects();
     _update_process_fetch_task($projects[$extension]);
     return $context;
 }
 public function doFileTransferUpdate($update_row)
 {
     $project = $update_row->extension_id;
     // Actually try to download the file.
     if (!($local_cache = update_manager_file_get($update_row->downloadurl))) {
         $this->caller->out(' - Error: Failed to download ' . $project . ' from ' . $update_row->downloadurl);
         return false;
     }
     // Extract it.
     $extract_directory = _update_manager_extract_directory();
     try {
         update_manager_archive_extract($local_cache, $extract_directory);
     } catch (Exception $e) {
         $this->caller->out(' - Error: ' . $e->getMessage());
         return false;
     }
     $archive_errors = update_manager_archive_verify($project, $local_cache, $extract_directory);
     if (!empty($archive_errors)) {
         // We just need to make sure our array keys don't collide, so use the
         // numeric keys from the $archive_errors array.
         foreach ($archive_errors as $key => $error) {
             $this->caller->out(' - Error: ' . $key . ": " . $error);
         }
         return false;
     }
     // Store maintenance_mode setting so we can restore it when done.
     $maintenance_mode = variable_get('maintenance_mode', FALSE);
     if ($maintenance_mode == FALSE) {
         variable_set('maintenance_mode', TRUE);
     }
     // Make sure the Updater registry is loaded.
     drupal_get_updaters();
     $updates = array();
     $directory = _update_manager_extract_directory();
     $project_location = $directory . '/' . $project;
     $updater = Updater::factory($project_location);
     $project_real_location = drupal_realpath($project_location);
     $updater_name = get_class($updater);
     $local_url = $project_real_location;
     // If the owner of the last directory we extracted is the same as the
     // owner of our configuration directory (e.g. sites/default) where we're
     // trying to install the code, there's no need to prompt for FTP/SSH
     // credentials. Instead, we instantiate a FileTransferLocal and invoke
     // update_authorize_run_update() directly.
     //
     // THIS PLUGIN WILL ONLY WORK IF IT IS THE SAME USER / LOCAL.
     if (fileowner($project_real_location) == fileowner(conf_path())) {
         module_load_include('inc', 'update', 'update.authorize');
         $filetransfer = new FileTransferLocal(DRUPAL_ROOT);
         // Modified version of update_authorize_run_update() without the batch process.
         unset($filetransfer->connection);
         $updater = new $updater_name($local_url);
         try {
             if ($updater->isInstalled()) {
                 // This is an update.
                 $tasks = $updater->update($filetransfer);
             }
         } catch (UpdaterException $e) {
             $this->caller->out(' - Error: ' . $e->getMessage());
             return false;
         }
         $this->caller->out(' - ' . ucfirst($project) . ' installed successfully');
         $offline = variable_get('maintenance_mode', FALSE);
         // Now that the update completed, we need to clear the cache of available
         // update data and recompute our status, so prevent show bogus results.
         _update_authorize_clear_update_status();
         // Take the site out of maintenance mode if it was previously that way.
         if ($offline && $maintenance_mode == FALSE) {
             variable_set('maintenance_mode', FALSE);
             $this->caller->out(' - Your site has been taken out of maintenance mode.');
         }
         // File Update Completed, return True to process Database Update.
         return true;
     } else {
         $this->caller->out(" - Error: User doesn't have access to file transfers. FTP credentials are required.");
         return false;
     }
 }