Backup of db before drupal security update
[yaffs-website] / web / core / modules / update / tests / src / Functional / UpdateUploadTest.php
1 <?php
2
3 namespace Drupal\Tests\update\Functional;
4
5 use Drupal\Core\Extension\InfoParserDynamic;
6 use Drupal\Core\Updater\Updater;
7 use Drupal\Core\Url;
8 use Drupal\Tests\TestFileCreationTrait;
9
10 /**
11  * Tests the Update Manager module's upload and extraction functionality.
12  *
13  * @group update
14  */
15 class UpdateUploadTest extends UpdateTestBase {
16
17   use TestFileCreationTrait {
18     getTestFiles as drupalGetTestFiles;
19   }
20
21   /**
22    * Modules to enable.
23    *
24    * @var array
25    */
26   public static $modules = ['update', 'update_test'];
27
28   protected function setUp() {
29     parent::setUp();
30     $admin_user = $this->drupalCreateUser(['administer modules', 'administer software updates', 'administer site configuration']);
31     $this->drupalLogin($admin_user);
32   }
33
34   /**
35    * Tests upload, extraction, and update of a module.
36    */
37   public function testUploadModule() {
38     // Ensure that the update information is correct before testing.
39     update_get_available(TRUE);
40
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);
47     $edit = [
48       'files[project_upload]' => $invalidArchiveFile->uri,
49     ];
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');
54
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';
59     $edit = [
60       'files[project_upload]' => $validArchiveFile,
61     ];
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');
65
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';
72     $edit = [
73       'files[project_upload]' => $validArchiveFile,
74     ];
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');
95
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
100     // directly instead.
101     $info_parser = new InfoParserDynamic();
102     $info = $info_parser->parse($installedInfoFilePath);
103     $this->assertEqual($info['version'], '8.x-1.0');
104
105     // Enable the module.
106     $this->drupalPostForm('admin/modules', ['modules[update_test_new_module][enable]' => TRUE], t('Install'));
107
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');
111     $system_info = [
112       'update_test_new_module' => [
113         'project' => 'update_test_new_module',
114       ],
115     ];
116     $update_test_config->set('system_info', $system_info)->save();
117     $xml_mapping = [
118       'update_test_new_module' => '1_1',
119     ];
120     $this->refreshUpdateStatus($xml_mapping);
121
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']));
127
128     // Parse the info file again to check that the module has been updated to
129     // 8.x-1.1.
130     $info = $info_parser->parse($installedInfoFilePath);
131     $this->assertEqual($info['version'], '8.x-1.1');
132   }
133
134   /**
135    * Ensures that archiver extensions are properly merged in the UI.
136    */
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.");
143   }
144
145   /**
146    * Checks the messages on update manager pages when missing a security update.
147    */
148   public function testUpdateManagerCoreSecurityUpdateMessages() {
149     $setting = [
150       '#all' => [
151         'version' => '8.0.0',
152       ],
153     ];
154     $this->config('update_test.settings')
155       ->set('system_info', $setting)
156       ->set('xml_map', ['drupal' => '0.2-sec'])
157       ->save();
158     $this->config('update.settings')
159       ->set('fetch.url', Url::fromRoute('update_test.update_test')->setAbsolute()->toString())
160       ->save();
161     // Initialize the update status.
162     $this->drupalGet('admin/reports/updates');
163
164     // Now, make sure none of the Update manager pages have duplicate messages
165     // about core missing a security update.
166
167     $this->drupalGet('admin/modules/install');
168     $this->assertNoText(t('There is a security update available for your version of Drupal.'));
169
170     $this->drupalGet('admin/modules/update');
171     $this->assertNoText(t('There is a security update available for your version of Drupal.'));
172
173     $this->drupalGet('admin/appearance/install');
174     $this->assertNoText(t('There is a security update available for your version of Drupal.'));
175
176     $this->drupalGet('admin/appearance/update');
177     $this->assertNoText(t('There is a security update available for your version of Drupal.'));
178
179     $this->drupalGet('admin/reports/updates/install');
180     $this->assertNoText(t('There is a security update available for your version of Drupal.'));
181
182     $this->drupalGet('admin/reports/updates/update');
183     $this->assertNoText(t('There is a security update available for your version of Drupal.'));
184
185     $this->drupalGet('admin/update/ready');
186     $this->assertNoText(t('There is a security update available for your version of Drupal.'));
187   }
188
189   /**
190    * Tests only an *.info.yml file are detected without supporting files.
191    */
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');
195
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.');
198   }
199
200 }