Upgraded drupal core with security updates
[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    * Invokes system_retrieve_file() in several scenarios.
15    */
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.');
23
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);
27
28     // URLs could not contains characters outside the ASCII set so $filename
29     // has to be encoded.
30     $encoded_filename = rawurlencode($filename);
31
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);
36
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);
44
45     file_unmanaged_delete_recursive($sourcedir);
46     file_unmanaged_delete_recursive($targetdir);
47   }
48
49 }