Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / tests / src / Functional / System / RetrieveFileTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\System;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests HTTP file fetching and error handling.
9  *
10  * @group system
11  */
12 class RetrieveFileTest extends BrowserTestBase {
13
14   /**
15    * Invokes system_retrieve_file() in several scenarios.
16    */
17   public function testFileRetrieving() {
18     // Test 404 handling by trying to fetch a randomly named file.
19     drupal_mkdir($sourcedir = 'public://' . $this->randomMachineName());
20     $filename = 'Файл для тестирования ' . $this->randomMachineName();
21     $url = file_create_url($sourcedir . '/' . $filename);
22     $retrieved_file = system_retrieve_file($url);
23     $this->assertFalse($retrieved_file, 'Non-existent file not fetched.');
24
25     // Actually create that file, download it via HTTP and test the returned path.
26     file_put_contents($sourcedir . '/' . $filename, 'testing');
27     $retrieved_file = system_retrieve_file($url);
28
29     // URLs could not contains characters outside the ASCII set so $filename
30     // has to be encoded.
31     $encoded_filename = rawurlencode($filename);
32
33     $this->assertEqual($retrieved_file, 'public://' . $encoded_filename, 'Sane path for downloaded file returned (public:// scheme).');
34     $this->assertTrue(is_file($retrieved_file), 'Downloaded file does exist (public:// scheme).');
35     $this->assertEqual(filesize($retrieved_file), 7, 'File size of downloaded file is correct (public:// scheme).');
36     file_unmanaged_delete($retrieved_file);
37
38     // Test downloading file to a different location.
39     drupal_mkdir($targetdir = 'temporary://' . $this->randomMachineName());
40     $retrieved_file = system_retrieve_file($url, $targetdir);
41     $this->assertEqual($retrieved_file, "$targetdir/$encoded_filename", 'Sane path for downloaded file returned (temporary:// scheme).');
42     $this->assertTrue(is_file($retrieved_file), 'Downloaded file does exist (temporary:// scheme).');
43     $this->assertEqual(filesize($retrieved_file), 7, 'File size of downloaded file is correct (temporary:// scheme).');
44     file_unmanaged_delete($retrieved_file);
45
46     file_unmanaged_delete_recursive($sourcedir);
47     file_unmanaged_delete_recursive($targetdir);
48   }
49
50 }