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