system_retrieve_file example

/** * 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());
    // cSpell:disable-next-line     $filename = 'Файл для тестирования ' . $this->randomMachineName();
    $url = \Drupal::service('file_url_generator')->generateAbsoluteString($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->assertEquals('public://' . $encoded_filename$retrieved_file, 'Sane path for downloaded file returned (public:// scheme).');
    
Home | Imprint | This part of the site doesn't use cookies.