function StreamWrapperTest::testUriFunctions in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/system/src/Tests/File/StreamWrapperTest.php \Drupal\system\Tests\File\StreamWrapperTest::testUriFunctions()
Test the getViaUri() and getViaScheme() methods and target functions.
File
- core/
modules/ system/ src/ Tests/ File/ StreamWrapperTest.php, line 78 - Contains \Drupal\system\Tests\File\StreamWrapperTest.
Class
- StreamWrapperTest
- Tests stream wrapper functions.
Namespace
Drupal\system\Tests\FileCode
function testUriFunctions() {
$config = $this
->config('system.file');
$instance = \Drupal::service('stream_wrapper_manager')
->getViaUri($this->scheme . '://foo');
$this
->assertEqual($this->classname, get_class($instance), 'Got correct class type for dummy URI.');
$instance = \Drupal::service('stream_wrapper_manager')
->getViaUri('public://foo');
$this
->assertEqual('Drupal\\Core\\StreamWrapper\\PublicStream', get_class($instance), 'Got correct class type for public URI.');
// Test file_uri_target().
$this
->assertEqual(file_uri_target('public://foo/bar.txt'), 'foo/bar.txt', 'Got a valid stream target from public://foo/bar.txt.');
$this
->assertEqual(file_uri_target('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='), 'image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==', t('Got a valid stream target from a data URI.'));
$this
->assertFalse(file_uri_target('foo/bar.txt'), 'foo/bar.txt is not a valid stream.');
$this
->assertFalse(file_uri_target('public://'), 'public:// has no target.');
$this
->assertFalse(file_uri_target('data:'), 'data: has no target.');
// Test file_build_uri() and
// Drupal\Core\StreamWrapper\LocalStream::getDirectoryPath().
$this
->assertEqual(file_build_uri('foo/bar.txt'), 'public://foo/bar.txt', 'Expected scheme was added.');
$this
->assertEqual(\Drupal::service('stream_wrapper_manager')
->getViaScheme('public')
->getDirectoryPath(), PublicStream::basePath(), 'Expected default directory path was returned.');
$this
->assertEqual(\Drupal::service('stream_wrapper_manager')
->getViaScheme('temporary')
->getDirectoryPath(), $config
->get('path.temporary'), 'Expected temporary directory path was returned.');
$config
->set('default_scheme', 'private')
->save();
$this
->assertEqual(file_build_uri('foo/bar.txt'), 'private://foo/bar.txt', 'Got a valid URI from foo/bar.txt.');
// Test file_create_url()
// TemporaryStream::getExternalUrl() uses Url::fromRoute(), which needs
// route information to work.
$this
->installSchema('system', 'router');
$this->container
->get('router.builder')
->rebuild();
$this
->assertTrue(strpos(file_create_url('temporary://test.txt'), 'system/temporary?file=test.txt'), 'Temporary external URL correctly built.');
$this
->assertTrue(strpos(file_create_url('public://test.txt'), Settings::get('file_public_path') . '/test.txt'), 'Public external URL correctly built.');
$this
->assertTrue(strpos(file_create_url('private://test.txt'), 'system/files/test.txt'), 'Private external URL correctly built.');
}