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