function StreamWrapperTest::testUriFunctions

Same name in other branches
  1. 7.x modules/simpletest/tests/file.test \StreamWrapperTest::testUriFunctions()
  2. 9 core/tests/Drupal/KernelTests/Core/File/StreamWrapperTest.php \Drupal\KernelTests\Core\File\StreamWrapperTest::testUriFunctions()
  3. 8.9.x core/tests/Drupal/KernelTests/Core/File/StreamWrapperTest.php \Drupal\KernelTests\Core\File\StreamWrapperTest::testUriFunctions()
  4. 11.x core/tests/Drupal/KernelTests/Core/File/StreamWrapperTest.php \Drupal\KernelTests\Core\File\StreamWrapperTest::testUriFunctions()

Tests the getViaUri() and getViaScheme() methods and target functions.

File

core/tests/Drupal/KernelTests/Core/File/StreamWrapperTest.php, line 76

Class

StreamWrapperTest
Tests stream wrapper functions.

Namespace

Drupal\KernelTests\Core\File

Code

public function testUriFunctions() : void {
    $config = $this->config('system.file');
    
    /** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
    $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
    $instance = $stream_wrapper_manager->getViaUri($this->scheme . '://foo');
    $this->assertEquals($this->classname, get_class($instance), 'Got correct class type for dummy URI.');
    $instance = $stream_wrapper_manager->getViaUri('public://foo');
    $this->assertEquals('Drupal\\Core\\StreamWrapper\\PublicStream', get_class($instance), 'Got correct class type for public URI.');
    // Test file_uri_target().
    $this->assertEquals('foo/bar.txt', $stream_wrapper_manager::getTarget('public://foo/bar.txt'), 'Got a valid stream target from public://foo/bar.txt.');
    $this->assertEquals('image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==', $stream_wrapper_manager::getTarget('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='));
    $this->assertFalse($stream_wrapper_manager::getTarget('foo/bar.txt'), 'foo/bar.txt is not a valid stream.');
    $this->assertSame($stream_wrapper_manager::getTarget('public://'), '');
    $this->assertSame($stream_wrapper_manager::getTarget('data:'), '');
    // Test Drupal\Core\StreamWrapper\LocalStream::getDirectoryPath().
    $this->assertEquals(PublicStream::basePath(), $stream_wrapper_manager->getViaScheme('public')
        ->getDirectoryPath(), 'Expected default directory path was returned.');
    $file_system = \Drupal::service('file_system');
    assert($file_system instanceof FileSystemInterface);
    $this->assertEquals($file_system->getTempDirectory(), $stream_wrapper_manager->getViaScheme('temporary')
        ->getDirectoryPath(), 'Expected temporary directory path was returned.');
    // Test FileUrlGeneratorInterface::generateString()
    // TemporaryStream::getExternalUrl() uses Url::fromRoute(), which needs
    // route information to work.
    $file_url_generator = $this->container
        ->get('file_url_generator');
    assert($file_url_generator instanceof FileUrlGeneratorInterface);
    $this->assertStringContainsString('system/temporary?file=test.txt', $file_url_generator->generateString('temporary://test.txt'), 'Temporary external URL correctly built.');
    $this->assertStringContainsString(Settings::get('file_public_path') . '/test.txt', $file_url_generator->generateString('public://test.txt'), 'Public external URL correctly built.');
    $this->assertStringContainsString('system/files/test.txt', $file_url_generator->generateString('private://test.txt'), 'Private external URL correctly built.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.