3 namespace Drupal\Tests\system\Functional\System;
5 use Drupal\Tests\BrowserTestBase;
8 * Tests HTTP file fetching and error handling.
12 class RetrieveFileTest extends BrowserTestBase {
14 * Invokes system_retrieve_file() in several scenarios.
16 public function testFileRetrieving() {
17 // Test 404 handling by trying to fetch a randomly named file.
18 drupal_mkdir($sourcedir = 'public://' . $this->randomMachineName());
19 $filename = 'Файл для тестирования ' . $this->randomMachineName();
20 $url = file_create_url($sourcedir . '/' . $filename);
21 $retrieved_file = system_retrieve_file($url);
22 $this->assertFalse($retrieved_file, 'Non-existent file not fetched.');
24 // Actually create that file, download it via HTTP and test the returned path.
25 file_put_contents($sourcedir . '/' . $filename, 'testing');
26 $retrieved_file = system_retrieve_file($url);
28 // URLs could not contains characters outside the ASCII set so $filename
30 $encoded_filename = rawurlencode($filename);
32 $this->assertEqual($retrieved_file, 'public://' . $encoded_filename, 'Sane path for downloaded file returned (public:// scheme).');
33 $this->assertTrue(is_file($retrieved_file), 'Downloaded file does exist (public:// scheme).');
34 $this->assertEqual(filesize($retrieved_file), 7, 'File size of downloaded file is correct (public:// scheme).');
35 file_unmanaged_delete($retrieved_file);
37 // Test downloading file to a different location.
38 drupal_mkdir($targetdir = 'temporary://' . $this->randomMachineName());
39 $retrieved_file = system_retrieve_file($url, $targetdir);
40 $this->assertEqual($retrieved_file, "$targetdir/$encoded_filename", 'Sane path for downloaded file returned (temporary:// scheme).');
41 $this->assertTrue(is_file($retrieved_file), 'Downloaded file does exist (temporary:// scheme).');
42 $this->assertEqual(filesize($retrieved_file), 7, 'File size of downloaded file is correct (temporary:// scheme).');
43 file_unmanaged_delete($retrieved_file);
45 file_unmanaged_delete_recursive($sourcedir);
46 file_unmanaged_delete_recursive($targetdir);