‪TYPO3CMS  9.5
PharStreamWrapperInterceptorTest.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 use TYPO3\PharStreamWrapper\Exception;
19 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
20 
21 class ‪PharStreamWrapperInterceptorTest extends FunctionalTestCase
22 {
29  'typo3/sysext/core/Tests/Functional/Fixtures/Extensions/test_resources' => 'typo3conf/ext/test_resources',
30  'typo3/sysext/core/Tests/Functional/Fixtures/Extensions/test_resources/bundle.phar' => 'fileadmin/bundle.phar',
31  ];
32 
33  protected function ‪setUp()
34  {
35  parent::setUp();
36  if (!in_array('phar', stream_get_wrappers())) {
37  $this->markTestSkipped('Phar stream wrapper is not registered');
38  }
39  // PharStreamWrapper is not initialized here since it relies on being
40  // properly defined in \TYPO3\CMS\Core\Core\Bootstrap - thus, it tests
41  // are expected to fail in case PharStreamWrapper is not initialized
42  }
43 
45  {
46  $allowedPath = 'typo3conf/ext/test_resources/bundle.phar';
47 
48  return [
49  'root directory' => [
50  $allowedPath,
51  ['Classes', 'Resources']
52  ],
53  'Classes/Domain/Model directory' => [
54  $allowedPath . '/Classes/Domain/Model',
55  ['DemoModel.php']
56  ],
57  'Resources directory' => [
58  $allowedPath . '/Resources',
59  ['content.txt']
60  ],
61  ];
62  }
63 
70  public function ‪directoryOpenAllowsInvocation(string $path)
71  {
72  $path = $this->instancePath . '/' . $path;
73  $handle = opendir('phar://' . $path);
74  self::assertInternalType('resource', $handle);
75  }
76 
84  public function ‪directoryReadAllowsInvocation(string $path, array $expectation)
85  {
86  $path = $this->instancePath . '/' . $path;
87 
88  $items = [];
89  $handle = opendir('phar://' . $path);
90  while (false !== $item = readdir($handle)) {
91  $items[] = $item;
92  }
93 
94  self::assertSame($expectation, $items);
95  }
96 
104  public function ‪directoryCloseAllowsInvocation(string $path, array $expectation)
105  {
106  $path = $this->instancePath . '/' . $path;
107 
108  $handle = opendir('phar://' . $path);
109  closedir($handle);
110 
111  self::assertFalse(is_resource($handle));
112  }
113 
115  {
116  $deniedPath = 'fileadmin/bundle.phar';
117 
118  return [
119  'root directory' => [
120  $deniedPath,
121  ['Classes', 'Resources']
122  ],
123  'Classes/Domain/Model directory' => [
124  $deniedPath . '/Classes/Domain/Model',
125  ['DemoModel.php']
126  ],
127  'Resources directory' => [
128  $deniedPath . '/Resources',
129  ['content.txt']
130  ],
131  ];
132  }
133 
140  public function ‪directoryActionDeniesInvocation(string $path)
141  {
142  self::expectException(Exception::class);
143  self::expectExceptionCode(1530103998);
144 
145  $path = $this->instancePath . '/' . $path;
146  opendir('phar://' . $path);
147  }
148 
152  public function ‪urlStatAllowsInvocationDataProvider(): array
153  {
154  $allowedPath = 'typo3conf/ext/test_resources/bundle.phar';
155 
156  return [
157  'filesize base file' => [
158  'filesize',
159  $allowedPath,
160  0, // Phar base file always has zero size when accessed through phar://
161  ],
162  'filesize Resources/content.txt' => [
163  'filesize',
164  $allowedPath . '/Resources/content.txt',
165  21,
166  ],
167  'is_file base file' => [
168  'is_file',
169  $allowedPath,
170  false, // Phar base file is not a file when accessed through phar://
171  ],
172  'is_file Resources/content.txt' => [
173  'is_file',
174  $allowedPath . '/Resources/content.txt',
175  true,
176  ],
177  'is_dir base file' => [
178  'is_dir',
179  $allowedPath,
180  true, // Phar base file is a directory when accessed through phar://
181  ],
182  'is_dir Resources/content.txt' => [
183  'is_dir',
184  $allowedPath . '/Resources/content.txt',
185  false,
186  ],
187  'file_exists base file' => [
188  'file_exists',
189  $allowedPath,
190  true
191  ],
192  'file_exists Resources/content.txt' => [
193  'file_exists',
194  $allowedPath . '/Resources/content.txt',
195  true
196  ],
197  ];
198  }
199 
208  public function ‪urlStatAllowsInvocation(string $functionName, string $path, $expectation)
209  {
210  $path = $this->instancePath . '/' . $path;
211 
212  self::assertSame(
213  $expectation,
214  call_user_func($functionName, 'phar://' . $path)
215  );
216  }
217 
221  public function ‪urlStatDeniesInvocationDataProvider(): array
222  {
223  $deniedPath = 'fileadmin/bundle.phar';
224 
225  return [
226  'filesize base file' => [
227  'filesize',
228  $deniedPath,
229  0, // Phar base file always has zero size when accessed through phar://
230  ],
231  'filesize Resources/content.txt' => [
232  'filesize',
233  $deniedPath . '/Resources/content.txt',
234  21,
235  ],
236  'is_file base file' => [
237  'is_file',
238  $deniedPath,
239  false, // Phar base file is not a file when accessed through phar://
240  ],
241  'is_file Resources/content.txt' => [
242  'is_file',
243  $deniedPath . '/Resources/content.txt',
244  true,
245  ],
246  'is_dir base file' => [
247  'is_dir',
248  $deniedPath,
249  true, // Phar base file is a directory when accessed through phar://
250  ],
251  'is_dir Resources/content.txt' => [
252  'is_dir',
253  $deniedPath . '/Resources/content.txt',
254  false,
255  ],
256  'file_exists base file' => [
257  'file_exists',
258  $deniedPath,
259  true
260  ],
261  'file_exists Resources/content.txt' => [
262  'file_exists',
263  $deniedPath . '/Resources/content.txt',
264  true
265  ],
266  ];
267  }
268 
277  public function ‪urlStatDeniesInvocation(string $functionName, string $path)
278  {
279  self::expectException(Exception::class);
280  self::expectExceptionCode(1530103998);
281 
282  $path = $this->instancePath . '/' . $path;
283  call_user_func($functionName, 'phar://' . $path);
284  }
285 
290  {
291  $allowedPath = $this->instancePath . '/typo3conf/ext/test_resources/bundle.phar';
292  $handle = fopen('phar://' . $allowedPath . '/Resources/content.txt', 'r');
293  self::assertInternalType('resource', $handle);
294  }
295 
300  {
301  $allowedPath = $this->instancePath . '/typo3conf/ext/test_resources/bundle.phar';
302  $handle = fopen('phar://' . $allowedPath . '/Resources/content.txt', 'r');
303  $content = fread($handle, 1024);
304  self::assertSame('TYPO3 demo text file.', $content);
305  }
306 
311  {
312  $allowedPath = $this->instancePath . '/typo3conf/ext/test_resources/bundle.phar';
313  $handle = fopen('phar://' . $allowedPath . '/Resources/content.txt', 'r');
314  fread($handle, 1024);
315  self::assertTrue(feof($handle));
316  }
317 
322  {
323  $allowedPath = $this->instancePath . '/typo3conf/ext/test_resources/bundle.phar';
324  $handle = fopen('phar://' . $allowedPath . '/Resources/content.txt', 'r');
325  fclose($handle);
326  self::assertFalse(is_resource($handle));
327  }
328 
333  {
334  $allowedPath = $this->instancePath . '/typo3conf/ext/test_resources/bundle.phar';
335  $content = file_get_contents('phar://' . $allowedPath . '/Resources/content.txt');
336  self::assertSame('TYPO3 demo text file.', $content);
337  }
338 
343  {
344  $allowedPath = $this->instancePath . '/typo3conf/ext/test_resources/bundle.phar';
345  include('phar://' . $allowedPath . '/Classes/Domain/Model/DemoModel.php');
346 
347  self::assertTrue(
348  class_exists(
349  \TYPO3Demo\Demo\Domain\Model\DemoModel::class,
350  false
351  )
352  );
353  }
354 
359  {
360  self::expectException(Exception::class);
361  self::expectExceptionCode(1530103998);
362 
363  $allowedPath = $this->instancePath . '/fileadmin/bundle.phar';
364  fopen('phar://' . $allowedPath . '/Resources/content.txt', 'r');
365  }
366 
371  {
372  self::expectException(Exception::class);
373  self::expectExceptionCode(1530103998);
374 
375  $allowedPath = $this->instancePath . '/fileadmin/bundle.phar';
376  file_get_contents('phar://' . $allowedPath . '/Resources/content.txt');
377  }
378 
383  {
384  self::expectException(Exception::class);
385  self::expectExceptionCode(1530103998);
386 
387  $allowedPath = $this->instancePath . '/fileadmin/bundle.phar';
388  include('phar://' . $allowedPath . '/Classes/Domain/Model/DemoModel.php');
389  }
390 }
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\streamOpenAllowsInvocationForFileClose
‪streamOpenAllowsInvocationForFileClose()
Definition: PharStreamWrapperInterceptorTest.php:320
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\urlStatDeniesInvocation
‪urlStatDeniesInvocation(string $functionName, string $path)
Definition: PharStreamWrapperInterceptorTest.php:276
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\urlStatAllowsInvocation
‪urlStatAllowsInvocation(string $functionName, string $path, $expectation)
Definition: PharStreamWrapperInterceptorTest.php:207
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\streamOpenAllowsInvocationForFileGetContents
‪streamOpenAllowsInvocationForFileGetContents()
Definition: PharStreamWrapperInterceptorTest.php:331
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\streamOpenDeniesInvocationForInclude
‪streamOpenDeniesInvocationForInclude()
Definition: PharStreamWrapperInterceptorTest.php:381
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\streamOpenAllowsInvocationForInclude
‪streamOpenAllowsInvocationForInclude()
Definition: PharStreamWrapperInterceptorTest.php:341
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\streamOpenAllowsInvocationForFileRead
‪streamOpenAllowsInvocationForFileRead()
Definition: PharStreamWrapperInterceptorTest.php:298
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\streamOpenDeniesInvocationForFileOpen
‪streamOpenDeniesInvocationForFileOpen()
Definition: PharStreamWrapperInterceptorTest.php:357
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\urlStatDeniesInvocationDataProvider
‪array urlStatDeniesInvocationDataProvider()
Definition: PharStreamWrapperInterceptorTest.php:220
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest
Definition: PharStreamWrapperInterceptorTest.php:22
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\directoryReadAllowsInvocation
‪directoryReadAllowsInvocation(string $path, array $expectation)
Definition: PharStreamWrapperInterceptorTest.php:83
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\streamOpenAllowsInvocationForFileOpen
‪streamOpenAllowsInvocationForFileOpen()
Definition: PharStreamWrapperInterceptorTest.php:288
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\directoryActionDeniesInvocation
‪directoryActionDeniesInvocation(string $path)
Definition: PharStreamWrapperInterceptorTest.php:139
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\$pathsToProvideInTestInstance
‪array $pathsToProvideInTestInstance
Definition: PharStreamWrapperInterceptorTest.php:27
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\streamOpenAllowsInvocationForFileEnd
‪streamOpenAllowsInvocationForFileEnd()
Definition: PharStreamWrapperInterceptorTest.php:309
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\directoryOpenAllowsInvocation
‪directoryOpenAllowsInvocation(string $path)
Definition: PharStreamWrapperInterceptorTest.php:69
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\directoryActionAllowsInvocationDataProvider
‪directoryActionAllowsInvocationDataProvider()
Definition: PharStreamWrapperInterceptorTest.php:43
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\urlStatAllowsInvocationDataProvider
‪array urlStatAllowsInvocationDataProvider()
Definition: PharStreamWrapperInterceptorTest.php:151
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\directoryActionDeniesInvocationDataProvider
‪directoryActionDeniesInvocationDataProvider()
Definition: PharStreamWrapperInterceptorTest.php:113
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\directoryCloseAllowsInvocation
‪directoryCloseAllowsInvocation(string $path, array $expectation)
Definition: PharStreamWrapperInterceptorTest.php:103
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\streamOpenDeniesInvocationForFileGetContents
‪streamOpenDeniesInvocationForFileGetContents()
Definition: PharStreamWrapperInterceptorTest.php:369
‪TYPO3\CMS\Core\Tests\Functional\IO\PharStreamWrapperInterceptorTest\setUp
‪setUp()
Definition: PharStreamWrapperInterceptorTest.php:32
‪TYPO3\CMS\Core\Tests\Functional\IO
Definition: PharStreamWrapperInterceptorTest.php:3