Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / file / src / Tests / DownloadTest.php
1 <?php
2
3 namespace Drupal\file\Tests;
4
5 /**
6  * Tests for download/file transfer functions.
7  *
8  * @group file
9  */
10 class DownloadTest extends FileManagedTestBase {
11   protected function setUp() {
12     parent::setUp();
13     // Clear out any hook calls.
14     file_test_reset();
15   }
16
17   /**
18    * Test the public file transfer system.
19    */
20   public function testPublicFileTransfer() {
21     // Test generating a URL to a created file.
22     $file = $this->createFile();
23     $url = file_create_url($file->getFileUri());
24     // URLs can't contain characters outside the ASCII set so $filename has to be
25     // encoded.
26     $filename = $GLOBALS['base_url'] . '/' . \Drupal::service('stream_wrapper_manager')->getViaScheme('public')->getDirectoryPath() . '/' . rawurlencode($file->getFilename());
27     $this->assertEqual($filename, $url, 'Correctly generated a URL for a created file.');
28     $this->drupalHead($url);
29     $this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the created file.');
30
31     // Test generating a URL to a shipped file (i.e. a file that is part of
32     // Drupal core, a module or a theme, for example a JavaScript file).
33     $filepath = 'core/assets/vendor/jquery/jquery.min.js';
34     $url = file_create_url($filepath);
35     $this->assertEqual($GLOBALS['base_url'] . '/' . $filepath, $url, 'Correctly generated a URL for a shipped file.');
36     $this->drupalHead($url);
37     $this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');
38   }
39
40   /**
41    * Test the private file transfer system.
42    */
43   public function testPrivateFileTransferWithoutPageCache() {
44     $this->doPrivateFileTransferTest();
45   }
46
47   /**
48    * Test the private file transfer system.
49    */
50   protected function doPrivateFileTransferTest() {
51     // Set file downloads to private so handler functions get called.
52
53     // Create a file.
54     $contents = $this->randomMachineName(8);
55     $file = $this->createFile(NULL, $contents, 'private');
56     // Created private files without usage are by default not accessible
57     // for a user different from the owner, but createFile always uses uid 1
58     // as the owner of the files. Therefore make it permanent to allow access
59     // if a module allows it.
60     $file->setPermanent();
61     $file->save();
62
63     $url  = file_create_url($file->getFileUri());
64
65     // Set file_test access header to allow the download.
66     file_test_set_return('download', ['x-foo' => 'Bar']);
67     $this->drupalGet($url);
68     $this->assertEqual($this->drupalGetHeader('x-foo'), 'Bar', 'Found header set by file_test module on private download.');
69     $this->assertFalse($this->drupalGetHeader('x-drupal-cache'), 'Page cache is disabled on private file download.');
70     $this->assertResponse(200, 'Correctly allowed access to a file when file_test provides headers.');
71
72     // Test that the file transferred correctly.
73     $this->assertEqual($contents, $this->content, 'Contents of the file are correct.');
74
75     // Deny access to all downloads via a -1 header.
76     file_test_set_return('download', -1);
77     $this->drupalHead($url);
78     $this->assertResponse(403, 'Correctly denied access to a file when file_test sets the header to -1.');
79
80     // Try non-existent file.
81     $url = file_create_url('private://' . $this->randomMachineName());
82     $this->drupalHead($url);
83     $this->assertResponse(404, 'Correctly returned 404 response for a non-existent file.');
84   }
85
86   /**
87    * Test file_create_url().
88    */
89   public function testFileCreateUrl() {
90
91     // Tilde (~) is excluded from this test because it is encoded by
92     // rawurlencode() in PHP 5.2 but not in PHP 5.3, as per RFC 3986.
93     // @see http://php.net/manual/function.rawurlencode.php#86506
94     // "Special" ASCII characters.
95     $basename = " -._!$'\"()*@[]?&+%#,;=:\n\x00" .
96       // Characters that look like a percent-escaped string.
97       "%23%25%26%2B%2F%3F" .
98       // Characters from various non-ASCII alphabets.
99       "éøïвβ中國書۞";
100     $basename_encoded = '%20-._%21%24%27%22%28%29%2A%40%5B%5D%3F%26%2B%25%23%2C%3B%3D%3A__' .
101       '%2523%2525%2526%252B%252F%253F' .
102       '%C3%A9%C3%B8%C3%AF%D0%B2%CE%B2%E4%B8%AD%E5%9C%8B%E6%9B%B8%DB%9E';
103
104     // Public files should not be served by Drupal, so their URLs should not be
105     // routed through Drupal, whereas private files should be served by Drupal,
106     // so they need to be. The difference is most apparent when $script_path
107     // is not empty (i.e., when not using clean URLs).
108     $clean_url_settings = [
109       'clean' => '',
110       'unclean' => 'index.php/',
111     ];
112     $public_directory_path = \Drupal::service('stream_wrapper_manager')->getViaScheme('public')->getDirectoryPath();
113     foreach ($clean_url_settings as $clean_url_setting => $script_path) {
114       $clean_urls = $clean_url_setting == 'clean';
115       $request = $this->prepareRequestForGenerator($clean_urls);
116       $base_path = $request->getSchemeAndHttpHost() . $request->getBasePath();
117       $this->checkUrl('public', '', $basename, $base_path . '/' . $public_directory_path . '/' . $basename_encoded);
118       $this->checkUrl('private', '', $basename, $base_path . '/' . $script_path . 'system/files/' . $basename_encoded);
119     }
120     $this->assertEqual(file_create_url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='), 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==', t('Generated URL matches expected URL.'));
121   }
122
123   /**
124    * Download a file from the URL generated by file_create_url().
125    *
126    * Create a file with the specified scheme, directory and filename; check that
127    * the URL generated by file_create_url() for the specified file equals the
128    * specified URL; fetch the URL and then compare the contents to the file.
129    *
130    * @param string $scheme
131    *   A scheme, e.g. "public".
132    * @param string $directory
133    *   A directory, possibly "".
134    * @param string $filename
135    *   A filename.
136    * @param string $expected_url
137    *   The expected URL.
138    */
139   private function checkUrl($scheme, $directory, $filename, $expected_url) {
140     // Convert $filename to a valid filename, i.e. strip characters not
141     // supported by the filesystem, and create the file in the specified
142     // directory.
143     $filepath = file_create_filename($filename, $directory);
144     $directory_uri = $scheme . '://' . dirname($filepath);
145     file_prepare_directory($directory_uri, FILE_CREATE_DIRECTORY);
146     $file = $this->createFile($filepath, NULL, $scheme);
147
148     $url = file_create_url($file->getFileUri());
149     $this->assertEqual($url, $expected_url);
150
151     if ($scheme == 'private') {
152       // Tell the implementation of hook_file_download() in file_test.module
153       // that this file may be downloaded.
154       file_test_set_return('download', ['x-foo' => 'Bar']);
155     }
156
157     $this->drupalGet($url);
158     if ($this->assertResponse(200) == 'pass') {
159       $this->assertRaw(file_get_contents($file->getFileUri()), 'Contents of the file are correct.');
160     }
161
162     $file->delete();
163   }
164
165 }