StreamWrapperTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Drupal\KernelTests\Core\File;
  3. use Drupal\Core\DrupalKernel;
  4. use Drupal\Core\File\FileSystemInterface;
  5. use Drupal\Core\Site\Settings;
  6. use Drupal\Core\StreamWrapper\PublicStream;
  7. use Symfony\Component\HttpFoundation\Request;
  8. /**
  9. * Tests stream wrapper functions.
  10. *
  11. * @group File
  12. */
  13. class StreamWrapperTest extends FileTestBase {
  14. /**
  15. * Modules to enable.
  16. *
  17. * @var array
  18. */
  19. public static $modules = ['file_test'];
  20. /**
  21. * A stream wrapper scheme to register for the test.
  22. *
  23. * @var string
  24. */
  25. protected $scheme = 'dummy';
  26. /**
  27. * A fully-qualified stream wrapper class name to register for the test.
  28. *
  29. * @var string
  30. */
  31. protected $classname = 'Drupal\file_test\StreamWrapper\DummyStreamWrapper';
  32. public function setUp() {
  33. parent::setUp();
  34. // Add file_private_path setting.
  35. $request = Request::create('/');
  36. $site_path = DrupalKernel::findSitePath($request);
  37. $this->setSetting('file_private_path', $site_path . '/private');
  38. }
  39. /**
  40. * Test the getClassName() function.
  41. */
  42. public function testGetClassName() {
  43. // Check the dummy scheme.
  44. $this->assertEqual($this->classname, \Drupal::service('stream_wrapper_manager')->getClass($this->scheme), 'Got correct class name for dummy scheme.');
  45. // Check core's scheme.
  46. $this->assertEqual('Drupal\Core\StreamWrapper\PublicStream', \Drupal::service('stream_wrapper_manager')->getClass('public'), 'Got correct class name for public scheme.');
  47. }
  48. /**
  49. * Test the getViaScheme() method.
  50. */
  51. public function testGetInstanceByScheme() {
  52. $instance = \Drupal::service('stream_wrapper_manager')->getViaScheme($this->scheme);
  53. $this->assertEqual($this->classname, get_class($instance), 'Got correct class type for dummy scheme.');
  54. $instance = \Drupal::service('stream_wrapper_manager')->getViaScheme('public');
  55. $this->assertEqual('Drupal\Core\StreamWrapper\PublicStream', get_class($instance), 'Got correct class type for public scheme.');
  56. }
  57. /**
  58. * Test the getViaUri() and getViaScheme() methods and target functions.
  59. */
  60. public function testUriFunctions() {
  61. $config = $this->config('system.file');
  62. /** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
  63. $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
  64. $instance = $stream_wrapper_manager->getViaUri($this->scheme . '://foo');
  65. $this->assertEqual($this->classname, get_class($instance), 'Got correct class type for dummy URI.');
  66. $instance = $stream_wrapper_manager->getViaUri('public://foo');
  67. $this->assertEqual('Drupal\Core\StreamWrapper\PublicStream', get_class($instance), 'Got correct class type for public URI.');
  68. // Test file_uri_target().
  69. $this->assertEqual($stream_wrapper_manager::getTarget('public://foo/bar.txt'), 'foo/bar.txt', 'Got a valid stream target from public://foo/bar.txt.');
  70. $this->assertEqual($stream_wrapper_manager::getTarget('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='), 'image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==', t('Got a valid stream target from a data URI.'));
  71. $this->assertFalse($stream_wrapper_manager::getTarget('foo/bar.txt'), 'foo/bar.txt is not a valid stream.');
  72. $this->assertSame($stream_wrapper_manager::getTarget('public://'), '');
  73. $this->assertSame($stream_wrapper_manager::getTarget('data:'), '');
  74. // Test file_build_uri() and
  75. // Drupal\Core\StreamWrapper\LocalStream::getDirectoryPath().
  76. $this->assertEqual(file_build_uri('foo/bar.txt'), 'public://foo/bar.txt', 'Expected scheme was added.');
  77. $this->assertEqual($stream_wrapper_manager->getViaScheme('public')->getDirectoryPath(), PublicStream::basePath(), 'Expected default directory path was returned.');
  78. $file_system = \Drupal::service('file_system');
  79. assert($file_system instanceof FileSystemInterface);
  80. $this->assertEqual($stream_wrapper_manager->getViaScheme('temporary')->getDirectoryPath(), $file_system->getTempDirectory(), 'Expected temporary directory path was returned.');
  81. $config->set('default_scheme', 'private')->save();
  82. $this->assertEqual(file_build_uri('foo/bar.txt'), 'private://foo/bar.txt', 'Got a valid URI from foo/bar.txt.');
  83. // Test file_create_url()
  84. // TemporaryStream::getExternalUrl() uses Url::fromRoute(), which needs
  85. // route information to work.
  86. $this->container->get('router.builder')->rebuild();
  87. $this->assertStringContainsString('system/temporary?file=test.txt', file_create_url('temporary://test.txt'), 'Temporary external URL correctly built.');
  88. $this->assertStringContainsString(Settings::get('file_public_path') . '/test.txt', file_create_url('public://test.txt'), 'Public external URL correctly built.');
  89. $this->assertStringContainsString('system/files/test.txt', file_create_url('private://test.txt'), 'Private external URL correctly built.');
  90. }
  91. /**
  92. * Test some file handle functions.
  93. */
  94. public function testFileFunctions() {
  95. $filename = 'public://' . $this->randomMachineName();
  96. file_put_contents($filename, str_repeat('d', 1000));
  97. // Open for rw and place pointer at beginning of file so select will return.
  98. $handle = fopen($filename, 'c+');
  99. $this->assertNotFalse($handle, 'Able to open a file for appending, reading and writing.');
  100. // Attempt to change options on the file stream: should all fail.
  101. $this->assertFalse(@stream_set_blocking($handle, 0), 'Unable to set to non blocking using a local stream wrapper.');
  102. $this->assertFalse(@stream_set_blocking($handle, 1), 'Unable to set to blocking using a local stream wrapper.');
  103. $this->assertFalse(@stream_set_timeout($handle, 1), 'Unable to set read time out using a local stream wrapper.');
  104. $this->assertEqual(-1 /*EOF*/, @stream_set_write_buffer($handle, 512), 'Unable to set write buffer using a local stream wrapper.');
  105. // This will test stream_cast().
  106. $read = [$handle];
  107. $write = NULL;
  108. $except = NULL;
  109. $this->assertEqual(1, stream_select($read, $write, $except, 0), 'Able to cast a stream via stream_select.');
  110. // This will test stream_truncate().
  111. $this->assertEqual(1, ftruncate($handle, 0), 'Able to truncate a stream via ftruncate().');
  112. fclose($handle);
  113. $this->assertEqual(0, filesize($filename), 'Able to truncate a stream.');
  114. // Cleanup.
  115. unlink($filename);
  116. }
  117. /**
  118. * Test the scheme functions.
  119. */
  120. public function testGetValidStreamScheme() {
  121. /** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
  122. $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
  123. $this->assertEqual('foo', $stream_wrapper_manager::getScheme('foo://pork//chops'), 'Got the correct scheme from foo://asdf');
  124. $this->assertEqual('data', $stream_wrapper_manager::getScheme('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='), 'Got the correct scheme from a data URI.');
  125. $this->assertFalse($stream_wrapper_manager::getScheme('foo/bar.txt'), 'foo/bar.txt is not a valid stream.');
  126. $this->assertTrue($stream_wrapper_manager->isValidScheme($stream_wrapper_manager::getScheme('public://asdf')), 'Got a valid stream scheme from public://asdf');
  127. $this->assertFalse($stream_wrapper_manager->isValidScheme($stream_wrapper_manager::getScheme('foo://asdf')), 'Did not get a valid stream scheme from foo://asdf');
  128. }
  129. /**
  130. * Tests that phar stream wrapper is registered as expected.
  131. *
  132. * @see \Drupal\Core\StreamWrapper\StreamWrapperManager::register()
  133. */
  134. public function testPharStreamWrapperRegistration() {
  135. if (!in_array('phar', stream_get_wrappers(), TRUE)) {
  136. $this->markTestSkipped('There is no phar stream wrapper registered. PHP is probably compiled without phar support.');
  137. }
  138. // Ensure that phar is not treated as a valid scheme.
  139. $stream_wrapper_manager = $this->container->get('stream_wrapper_manager');
  140. $this->assertFalse($stream_wrapper_manager->getViaScheme('phar'));
  141. // Ensure that calling register again and unregister do not create errors
  142. // due to the PharStreamWrapperManager singleton.
  143. $stream_wrapper_manager->register();
  144. $this->assertContains('public', stream_get_wrappers());
  145. $this->assertContains('phar', stream_get_wrappers());
  146. $stream_wrapper_manager->unregister();
  147. $this->assertNotContains('public', stream_get_wrappers());
  148. // This will have reverted to the builtin phar stream wrapper.
  149. $this->assertContains('phar', stream_get_wrappers());
  150. $stream_wrapper_manager->register();
  151. $this->assertContains('public', stream_get_wrappers());
  152. $this->assertContains('phar', stream_get_wrappers());
  153. }
  154. }