3 namespace Drupal\Tests\update\Functional;
5 use Drupal\Core\Extension\InfoParserDynamic;
6 use Drupal\Core\Updater\Updater;
8 use Drupal\Tests\TestFileCreationTrait;
11 * Tests the Update Manager module's upload and extraction functionality.
15 class UpdateUploadTest extends UpdateTestBase {
17 use TestFileCreationTrait {
18 getTestFiles as drupalGetTestFiles;
26 public static $modules = ['update', 'update_test'];
28 protected function setUp() {
30 $admin_user = $this->drupalCreateUser(['administer modules', 'administer software updates', 'administer site configuration']);
31 $this->drupalLogin($admin_user);
35 * Tests upload, extraction, and update of a module.
37 public function testUploadModule() {
38 // Ensure that the update information is correct before testing.
39 update_get_available(TRUE);
41 // Images are not valid archives, so get one and try to install it. We
42 // need an extra variable to store the result of drupalGetTestFiles()
43 // since reset() takes an argument by reference and passing in a constant
44 // emits a notice in strict mode.
45 $imageTestFiles = $this->drupalGetTestFiles('image');
46 $invalidArchiveFile = reset($imageTestFiles);
48 'files[project_upload]' => $invalidArchiveFile->uri,
50 // This also checks that the correct archive extensions are allowed.
51 $this->drupalPostForm('admin/modules/install', $edit, t('Install'));
52 $this->assertText(t('Only files with the following extensions are allowed: @archive_extensions.', ['@archive_extensions' => archiver_get_extensions()]), 'Only valid archives can be uploaded.');
53 $this->assertUrl('admin/modules/install');
55 // Check to ensure an existing module can't be reinstalled. Also checks that
56 // the archive was extracted since we can't know if the module is already
57 // installed until after extraction.
58 $validArchiveFile = __DIR__ . '/../../aaa_update_test.tar.gz';
60 'files[project_upload]' => $validArchiveFile,
62 $this->drupalPostForm('admin/modules/install', $edit, t('Install'));
63 $this->assertText(t('@module_name is already installed.', ['@module_name' => 'AAA Update test']), 'Existing module was extracted and not reinstalled.');
64 $this->assertUrl('admin/modules/install');
66 // Ensure that a new module can be extracted and installed.
67 $updaters = drupal_get_updaters();
68 $moduleUpdater = $updaters['module']['class'];
69 $installedInfoFilePath = $this->container->get('update.root') . '/' . $moduleUpdater::getRootDirectoryRelativePath() . '/update_test_new_module/update_test_new_module.info.yml';
70 $this->assertFalse(file_exists($installedInfoFilePath), 'The new module does not exist in the filesystem before it is installed with the Update Manager.');
71 $validArchiveFile = __DIR__ . '/../../update_test_new_module/8.x-1.0/update_test_new_module.tar.gz';
73 'files[project_upload]' => $validArchiveFile,
75 $this->drupalPostForm('admin/modules/install', $edit, t('Install'));
76 // Check that submitting the form takes the user to authorize.php.
77 $this->assertUrl('core/authorize.php');
78 $this->assertTitle('Update manager | Drupal');
79 // Check for a success message on the page, and check that the installed
80 // module now exists in the expected place in the filesystem.
81 $this->assertRaw(t('Installed %project_name successfully', ['%project_name' => 'update_test_new_module']));
82 $this->assertTrue(file_exists($installedInfoFilePath), 'The new module exists in the filesystem after it is installed with the Update Manager.');
83 // Ensure the links are relative to the site root and not
84 // core/authorize.php.
85 $this->assertLink(t('Install another module'));
86 $this->assertLinkByHref(Url::fromRoute('update.module_install')->toString());
87 $this->assertLink(t('Enable newly added modules'));
88 $this->assertLinkByHref(Url::fromRoute('system.modules_list')->toString());
89 $this->assertLink(t('Administration pages'));
90 $this->assertLinkByHref(Url::fromRoute('system.admin')->toString());
91 // Ensure we can reach the "Install another module" link.
92 $this->clickLink(t('Install another module'));
93 $this->assertResponse(200);
94 $this->assertUrl('admin/modules/install');
96 // Check that the module has the correct version before trying to update
97 // it. Since the module is installed in sites/simpletest, which only the
98 // child site has access to, standard module API functions won't find it
99 // when called here. To get the version, the info file must be parsed
101 $info_parser = new InfoParserDynamic();
102 $info = $info_parser->parse($installedInfoFilePath);
103 $this->assertEqual($info['version'], '8.x-1.0');
105 // Enable the module.
106 $this->drupalPostForm('admin/modules', ['modules[update_test_new_module][enable]' => TRUE], t('Install'));
108 // Define the update XML such that the new module downloaded above needs an
109 // update from 8.x-1.0 to 8.x-1.1.
110 $update_test_config = $this->config('update_test.settings');
112 'update_test_new_module' => [
113 'project' => 'update_test_new_module',
116 $update_test_config->set('system_info', $system_info)->save();
118 'update_test_new_module' => '1_1',
120 $this->refreshUpdateStatus($xml_mapping);
122 // Run the updates for the new module.
123 $this->drupalPostForm('admin/reports/updates/update', ['projects[update_test_new_module]' => TRUE], t('Download these updates'));
124 $this->drupalPostForm(NULL, ['maintenance_mode' => FALSE], t('Continue'));
125 $this->assertText(t('Update was completed successfully.'));
126 $this->assertRaw(t('Installed %project_name successfully', ['%project_name' => 'update_test_new_module']));
128 // Parse the info file again to check that the module has been updated to
130 $info = $info_parser->parse($installedInfoFilePath);
131 $this->assertEqual($info['version'], '8.x-1.1');
135 * Ensures that archiver extensions are properly merged in the UI.
137 public function testFileNameExtensionMerging() {
138 $this->drupalGet('admin/modules/install');
139 // Make sure the bogus extension supported by update_test.module is there.
140 $this->assertPattern('/file extensions are supported:.*update-test-extension/', "Found 'update-test-extension' extension.");
141 // Make sure it didn't clobber the first option from core.
142 $this->assertPattern('/file extensions are supported:.*tar/', "Found 'tar' extension.");
146 * Checks the messages on update manager pages when missing a security update.
148 public function testUpdateManagerCoreSecurityUpdateMessages() {
151 'version' => '8.0.0',
154 $this->config('update_test.settings')
155 ->set('system_info', $setting)
156 ->set('xml_map', ['drupal' => '0.2-sec'])
158 $this->config('update.settings')
159 ->set('fetch.url', Url::fromRoute('update_test.update_test')->setAbsolute()->toString())
161 // Initialize the update status.
162 $this->drupalGet('admin/reports/updates');
164 // Now, make sure none of the Update manager pages have duplicate messages
165 // about core missing a security update.
167 $this->drupalGet('admin/modules/install');
168 $this->assertNoText(t('There is a security update available for your version of Drupal.'));
170 $this->drupalGet('admin/modules/update');
171 $this->assertNoText(t('There is a security update available for your version of Drupal.'));
173 $this->drupalGet('admin/appearance/install');
174 $this->assertNoText(t('There is a security update available for your version of Drupal.'));
176 $this->drupalGet('admin/appearance/update');
177 $this->assertNoText(t('There is a security update available for your version of Drupal.'));
179 $this->drupalGet('admin/reports/updates/install');
180 $this->assertNoText(t('There is a security update available for your version of Drupal.'));
182 $this->drupalGet('admin/reports/updates/update');
183 $this->assertNoText(t('There is a security update available for your version of Drupal.'));
185 $this->drupalGet('admin/update/ready');
186 $this->assertNoText(t('There is a security update available for your version of Drupal.'));
190 * Tests only an *.info.yml file are detected without supporting files.
192 public function testUpdateDirectory() {
193 $type = Updater::getUpdaterFromDirectory(\Drupal::root() . '/core/modules/update/tests/modules/aaa_update_test');
194 $this->assertEqual($type, 'Drupal\\Core\\Updater\\Module', 'Detected a Module');
196 $type = Updater::getUpdaterFromDirectory(\Drupal::root() . '/core/modules/update/tests/themes/update_test_basetheme');
197 $this->assertEqual($type, 'Drupal\\Core\\Updater\\Theme', 'Detected a Theme.');