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