You are here

public function UpdateUploadTest::testUploadModule in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/update/src/Tests/UpdateUploadTest.php \Drupal\update\Tests\UpdateUploadTest::testUploadModule()

Tests upload, extraction, and update of a module.

File

core/modules/update/src/Tests/UpdateUploadTest.php, line 37
Contains \Drupal\update\Tests\UpdateUploadTest.

Class

UpdateUploadTest
Tests the Update Manager module's upload and extraction functionality.

Namespace

Drupal\update\Tests

Code

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/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');
}