TYPO3 CMS  TYPO3_7-6
PharStreamWrapperInterceptorTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
21 {
25  protected $testExtensionsToLoad = [
26  'typo3/sysext/core/Tests/Functional/Fixtures/Extensions/test_resources'
27  ];
28 
33  'typo3/sysext/core/Tests/Functional/Fixtures/Extensions/test_resources/bundle.phar' => 'fileadmin/bundle.phar'
34  ];
35 
36  protected function setUp()
37  {
38  parent::setUp();
39 
40  if (!in_array('phar', stream_get_wrappers())) {
41  $this->markTestSkipped('Phar stream wrapper is not registered');
42  }
43  // PharStreamWrapper is not initialized here since it relies on being
44  // properly defined in \TYPO3\CMS\Core\Core\Bootstrap - thus, it tests
45  // are expected to fail in case PharStreamWrapper is not initialized
46  }
47 
49  {
50  $allowedPath = 'typo3conf/ext/test_resources/bundle.phar';
51 
52  return [
53  'root directory' => [
54  $allowedPath,
55  ['Classes', 'Resources']
56  ],
57  'Classes/Domain/Model directory' => [
58  $allowedPath . '/Classes/Domain/Model',
59  ['DemoModel.php']
60  ],
61  'Resources directory' => [
62  $allowedPath . '/Resources',
63  ['content.txt']
64  ],
65  ];
66  }
67 
74  public function directoryOpenAllowsInvocation($path)
75  {
76  $path = $this->getInstancePath() . '/' . $path;
77  $handle = opendir('phar://' . $path);
78  self::assertInternalType('resource', $handle);
79  }
80 
88  public function directoryReadAllowsInvocation($path, array $expectation)
89  {
90  $path = $this->getInstancePath() . '/' . $path;
91 
92  $items = [];
93  $handle = opendir('phar://' . $path);
94  while (false !== $item = readdir($handle)) {
95  $items[] = $item;
96  }
97 
98  self::assertSame($expectation, $items);
99  }
100 
108  public function directoryCloseAllowsInvocation($path, array $expectation)
109  {
110  $path = $this->getInstancePath() . '/' . $path;
111 
112  $handle = opendir('phar://' . $path);
113  closedir($handle);
114 
115  self::assertFalse(is_resource($handle));
116  }
117 
119  {
120  $deniedPath = 'fileadmin/bundle.phar';
121 
122  return [
123  'root directory' => [
124  $deniedPath,
125  ['Classes', 'Resources']
126  ],
127  'Classes/Domain/Model directory' => [
128  $deniedPath . '/Classes/Domain/Model',
129  ['DemoModel.php']
130  ],
131  'Resources directory' => [
132  $deniedPath . '/Resources',
133  ['content.txt']
134  ],
135  ];
136  }
137 
146  public function directoryActionDeniesInvocation($path)
147  {
148  $path = $this->getInstancePath() . '/' . $path;
149  opendir('phar://' . $path);
150  }
151 
156  {
157  $allowedPath = 'typo3conf/ext/test_resources/bundle.phar';
158 
159  return [
160  'filesize base file' => [
161  'filesize',
162  $allowedPath,
163  0, // Phar base file always has zero size when accessed through phar://
164  ],
165  'filesize Resources/content.txt' => [
166  'filesize',
167  $allowedPath . '/Resources/content.txt',
168  21,
169  ],
170  'is_file base file' => [
171  'is_file',
172  $allowedPath,
173  false, // Phar base file is not a file when accessed through phar://
174  ],
175  'is_file Resources/content.txt' => [
176  'is_file',
177  $allowedPath . '/Resources/content.txt',
178  true,
179  ],
180  'is_dir base file' => [
181  'is_dir',
182  $allowedPath,
183  true, // Phar base file is a directory when accessed through phar://
184  ],
185  'is_dir Resources/content.txt' => [
186  'is_dir',
187  $allowedPath . '/Resources/content.txt',
188  false,
189  ],
190  'file_exists base file' => [
191  'file_exists',
192  $allowedPath,
193  true
194  ],
195  'file_exists Resources/content.txt' => [
196  'file_exists',
197  $allowedPath . '/Resources/content.txt',
198  true
199  ],
200  ];
201  }
202 
211  public function urlStatAllowsInvocation($functionName, $path, $expectation)
212  {
213  $path = $this->getInstancePath() . '/' . $path;
214 
215  self::assertSame(
216  $expectation,
217  call_user_func($functionName, 'phar://' . $path)
218  );
219  }
220 
225  {
226  $deniedPath = 'fileadmin/bundle.phar';
227 
228  return [
229  'filesize base file' => [
230  'filesize',
231  $deniedPath,
232  0, // Phar base file always has zero size when accessed through phar://
233  ],
234  'filesize Resources/content.txt' => [
235  'filesize',
236  $deniedPath . '/Resources/content.txt',
237  21,
238  ],
239  'is_file base file' => [
240  'is_file',
241  $deniedPath,
242  false, // Phar base file is not a file when accessed through phar://
243  ],
244  'is_file Resources/content.txt' => [
245  'is_file',
246  $deniedPath . '/Resources/content.txt',
247  true,
248  ],
249  'is_dir base file' => [
250  'is_dir',
251  $deniedPath,
252  true, // Phar base file is a directory when accessed through phar://
253  ],
254  'is_dir Resources/content.txt' => [
255  'is_dir',
256  $deniedPath . '/Resources/content.txt',
257  false,
258  ],
259  'file_exists base file' => [
260  'file_exists',
261  $deniedPath,
262  true
263  ],
264  'file_exists Resources/content.txt' => [
265  'file_exists',
266  $deniedPath . '/Resources/content.txt',
267  true
268  ],
269  ];
270  }
271 
282  public function urlStatDeniesInvocation($functionName, $path)
283  {
284  $path = $this->getInstancePath() . '/' . $path;
285  call_user_func($functionName, 'phar://' . $path);
286  }
287 
292  {
293  $allowedPath = $this->getInstancePath() . '/typo3conf/ext/test_resources/bundle.phar';
294  $handle = fopen('phar://' . $allowedPath . '/Resources/content.txt', 'r');
295  self::assertInternalType('resource', $handle);
296  }
297 
302  {
303  $allowedPath = $this->getInstancePath() . '/typo3conf/ext/test_resources/bundle.phar';
304  $handle = fopen('phar://' . $allowedPath . '/Resources/content.txt', 'r');
305  $content = fread($handle, 1024);
306  self::assertSame('TYPO3 demo text file.', $content);
307  }
308 
313  {
314  $allowedPath = $this->getInstancePath() . '/typo3conf/ext/test_resources/bundle.phar';
315  $handle = fopen('phar://' . $allowedPath . '/Resources/content.txt', 'r');
316  fread($handle, 1024);
317  self::assertTrue(feof($handle));
318  }
319 
324  {
325  $allowedPath = $this->getInstancePath() . '/typo3conf/ext/test_resources/bundle.phar';
326  $handle = fopen('phar://' . $allowedPath . '/Resources/content.txt', 'r');
327  fclose($handle);
328  self::assertFalse(is_resource($handle));
329  }
330 
335  {
336  $allowedPath = $this->getInstancePath() . '/typo3conf/ext/test_resources/bundle.phar';
337  $content = file_get_contents('phar://' . $allowedPath . '/Resources/content.txt');
338  self::assertSame('TYPO3 demo text file.', $content);
339  }
340 
345  {
346  $allowedPath = $this->getInstancePath() . '/typo3conf/ext/test_resources/bundle.phar';
347  include('phar://' . $allowedPath . '/Classes/Domain/Model/DemoModel.php');
348 
349  self::assertTrue(
350  class_exists(
351  \TYPO3Demo\Demo\Domain\Model\DemoModel::class,
352  false
353  )
354  );
355  }
356 
363  {
364  $allowedPath = $this->getInstancePath() . '/fileadmin/bundle.phar';
365  fopen('phar://' . $allowedPath . '/Resources/content.txt', 'r');
366  }
367 
374  {
375  $allowedPath = $this->getInstancePath() . '/fileadmin/bundle.phar';
376  file_get_contents('phar://' . $allowedPath . '/Resources/content.txt');
377  }
378 
385  {
386  $allowedPath = $this->getInstancePath() . '/fileadmin/bundle.phar';
387  include('phar://' . $allowedPath . '/Classes/Domain/Model/DemoModel.php');
388  }
389 }