
| Current Path : /var/www/html/dataninja.cn/core/modules/system/tests/src/Functional/System/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : /var/www/html/dataninja.cn/core/modules/system/tests/src/Functional/System/RetrieveFileTest.php |
<?php
namespace Drupal\Tests\system\Functional\System;
use Drupal\Tests\BrowserTestBase;
/**
* Tests HTTP file fetching and error handling.
*
* @group system
*/
class RetrieveFileTest extends BrowserTestBase {
/**
* Invokes system_retrieve_file() in several scenarios.
*/
public function testFileRetrieving() {
// Test 404 handling by trying to fetch a randomly named file.
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
$file_system->mkdir($sourcedir = 'public://' . $this->randomMachineName());
$filename = 'Файл для тестирования ' . $this->randomMachineName();
$url = file_create_url($sourcedir . '/' . $filename);
$retrieved_file = system_retrieve_file($url);
$this->assertFalse($retrieved_file, 'Non-existent file not fetched.');
// Actually create that file, download it via HTTP and test the returned path.
file_put_contents($sourcedir . '/' . $filename, 'testing');
$retrieved_file = system_retrieve_file($url);
// URLs could not contains characters outside the ASCII set so $filename
// has to be encoded.
$encoded_filename = rawurlencode($filename);
$this->assertEqual($retrieved_file, 'public://' . $encoded_filename, 'Sane path for downloaded file returned (public:// scheme).');
$this->assertTrue(is_file($retrieved_file), 'Downloaded file does exist (public:// scheme).');
$this->assertEqual(filesize($retrieved_file), 7, 'File size of downloaded file is correct (public:// scheme).');
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
$file_system->delete($retrieved_file);
// Test downloading file to a different location.
$file_system->mkdir($targetdir = 'temporary://' . $this->randomMachineName());
$retrieved_file = system_retrieve_file($url, $targetdir);
$this->assertEqual($retrieved_file, "$targetdir/$encoded_filename", 'Sane path for downloaded file returned (temporary:// scheme).');
$this->assertTrue(is_file($retrieved_file), 'Downloaded file does exist (temporary:// scheme).');
$this->assertEqual(filesize($retrieved_file), 7, 'File size of downloaded file is correct (temporary:// scheme).');
$file_system->delete($retrieved_file);
$file_system->deleteRecursive($sourcedir);
$file_system->deleteRecursive($targetdir);
}
}