Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
WriteTest.php
Go to the documentation of this file.
1 <?php
9 
13 
18 class WriteTest extends \PHPUnit\Framework\TestCase
19 {
25  private $testDirectories = [];
26 
30  public function testInstance()
31  {
32  $dir = $this->getDirectoryInstance('newDir1', 0777);
33  $this->assertTrue($dir instanceof ReadInterface);
34  $this->assertTrue($dir instanceof WriteInterface);
35  }
36 
45  public function testCreate($basePath, $permissions, $path)
46  {
47  $directory = $this->getDirectoryInstance($basePath, $permissions);
48  $this->assertTrue($directory->create($path));
49  $this->assertTrue($directory->isExist($path));
50  }
51 
57  public function createProvider()
58  {
59  return [
60  ['newDir1', 0777, "newDir1"],
61  ['newDir1', 0777, "root_dir1/subdir1/subdir2"],
62  ['newDir2', 0777, "root_dir2/subdir"],
63  ['newDir1', 0777, "."]
64  ];
65  }
66 
67  public function testCreateOutside()
68  {
69  $exceptions = 0;
70  $dir = $this->getDirectoryInstance('newDir1', 0777);
71  try {
72  $dir->create('../../outsideDir');
73  } catch (ValidatorException $exception) {
74  $exceptions++;
75  }
76  try {
77  $dir->create('//./..///../outsideDir');
78  } catch (ValidatorException $exception) {
79  $exceptions++;
80  }
81  try {
82  $dir->create('\..\..\outsideDir');
83  } catch (ValidatorException $exception) {
84  $exceptions++;
85  }
86  $this->assertEquals(3, $exceptions);
87  }
88 
95  public function testDelete($path)
96  {
97  $directory = $this->getDirectoryInstance('newDir', 0777);
98  $directory->create($path);
99  $this->assertTrue($directory->isExist($path));
100  $directory->delete($path);
101  $this->assertFalse($directory->isExist($path));
102  }
103 
109  public function deleteProvider()
110  {
111  return [['subdir'], ['subdir/subsubdir']];
112  }
113 
114  public function testDeleteOutside()
115  {
116  $exceptions = 0;
117  $dir = $this->getDirectoryInstance('newDir1', 0777);
118  try {
119  $dir->delete('../../Directory');
120  } catch (ValidatorException $exception) {
121  $exceptions++;
122  }
123  try {
124  $dir->delete('//./..///../Directory');
125  } catch (ValidatorException $exception) {
126  $exceptions++;
127  }
128  try {
129  $dir->delete('\..\..\Directory');
130  } catch (ValidatorException $exception) {
131  $exceptions++;
132  }
133  $this->assertEquals(3, $exceptions);
134  }
135 
145  public function testRename($basePath, $permissions, $name, $newName)
146  {
147  $directory = $this->getDirectoryInstance($basePath, $permissions);
148  $directory->touch($name);
149  $created = $directory->read();
150  $directory->renameFile($name, $newName);
151  $renamed = $directory->read();
152  $this->assertTrue(in_array($name, $created));
153  $this->assertTrue(in_array($newName, $renamed));
154  $this->assertFalse(in_array($name, $renamed));
155  }
156 
162  public function renameProvider()
163  {
164  return [['newDir1', 0777, 'first_name.txt', 'second_name.txt']];
165  }
166 
167  public function testRenameOutside()
168  {
169  $exceptions = 0;
170  $dir = $this->getDirectoryInstance('newDir1', 0777);
171  try {
172  $dir->renameFile('../../Directory/ReadTest.php', 'RenamedTest');
173  } catch (ValidatorException $exception) {
174  $exceptions++;
175  }
176  try {
177  $dir->renameFile(
178  '//./..///../Directory/ReadTest.php',
179  'RenamedTest'
180  );
181  } catch (ValidatorException $exception) {
182  $exceptions++;
183  }
184  try {
185  $dir->renameFile('\..\..\Directory\ReadTest.php', 'RenamedTest');
186  } catch (ValidatorException $exception) {
187  $exceptions++;
188  }
189  $this->assertEquals(3, $exceptions);
190  }
191 
202  public function testRenameTargetDir($firstDir, $secondDir, $permission, $name, $newName)
203  {
204  $dir1 = $this->getDirectoryInstance($firstDir, $permission);
205  $dir2 = $this->getDirectoryInstance($secondDir, $permission);
206 
207  $dir1->touch($name);
208  $created = $dir1->read();
209  $dir1->renameFile($name, $newName, $dir2);
210  $oldPlace = $dir1->read();
211 
212  $this->assertTrue(in_array($name, $created));
213  $this->assertFalse(in_array($name, $oldPlace));
214  }
215 
221  public function renameTargetDirProvider()
222  {
223  return [['dir1', 'dir2', 0777, 'first_name.txt', 'second_name.txt']];
224  }
225 
235  public function testCopy($basePath, $permissions, $name, $newName)
236  {
237  $directory = $this->getDirectoryInstance($basePath, $permissions);
238  $file = $directory->openFile($name, 'w+');
239  $file->close();
240  $directory->copyFile($name, $newName);
241  $this->assertTrue($directory->isExist($name));
242  $this->assertTrue($directory->isExist($newName));
243  }
244 
250  public function copyProvider()
251  {
252  return [
253  ['newDir1', 0777, 'first_name.txt', 'second_name.txt'],
254  ['newDir1', 0777, 'subdir/first_name.txt', 'subdir/second_name.txt']
255  ];
256  }
257 
258  public function testCopyOutside()
259  {
260  $exceptions = 0;
261  $dir = $this->getDirectoryInstance('newDir1', 0777);
262  $dir->touch('test_file_for_copy_outside.txt');
263  try {
264  $dir->copyFile('../../Directory/ReadTest.php', 'CopiedTest');
265  } catch (ValidatorException $exception) {
266  $exceptions++;
267  }
268  try {
269  $dir->copyFile(
270  '//./..///../Directory/ReadTest.php',
271  'CopiedTest'
272  );
273  } catch (ValidatorException $exception) {
274  $exceptions++;
275  }
276  try {
277  $dir->copyFile('\..\..\Directory\ReadTest.php', 'CopiedTest');
278  } catch (ValidatorException $exception) {
279  $exceptions++;
280  }
281  try {
282  $dir->copyFile(
283  'test_file_for_copy_outside.txt',
284  '../../Directory/copied_outside.txt'
285  );
286  } catch (ValidatorException $exception) {
287  $exceptions++;
288  }
289  $this->assertEquals(4, $exceptions);
290  }
291 
302  public function testCopyTargetDir($firstDir, $secondDir, $permission, $name, $newName)
303  {
304  $dir1 = $this->getDirectoryInstance($firstDir, $permission);
305  $dir2 = $this->getDirectoryInstance($secondDir, $permission);
306 
307  $file = $dir1->openFile($name, 'w+');
308  $file->close();
309  $dir1->copyFile($name, $newName, $dir2);
310 
311  $this->assertTrue($dir1->isExist($name));
312  $this->assertTrue($dir2->isExist($newName));
313  }
314 
320  public function copyTargetDirProvider()
321  {
322  return [
323  ['dir1', 'dir2', 0777, 'first_name.txt', 'second_name.txt'],
324  ['dir1', 'dir2', 0777, 'subdir/first_name.txt', 'subdir/second_name.txt']
325  ];
326  }
327 
331  public function testChangePermissions()
332  {
333  $directory = $this->getDirectoryInstance('newDir1', 0777);
334  $directory->create('test_directory');
335  $this->assertTrue($directory->changePermissions('test_directory', 0644));
336  }
337 
339  {
340  $exceptions = 0;
341  $dir = $this->getDirectoryInstance('newDir1', 0777);
342  try {
343  $dir->changePermissions('../../Directory', 0777);
344  } catch (ValidatorException $exception) {
345  $exceptions++;
346  }
347  try {
348  $dir->changePermissions('//./..///../Directory', 0777);
349  } catch (ValidatorException $exception) {
350  $exceptions++;
351  }
352  try {
353  $dir->changePermissions('\..\..\Directory', 0777);
354  } catch (ValidatorException $exception) {
355  $exceptions++;
356  }
357  $this->assertEquals(3, $exceptions);
358  }
359 
364  {
365  $directory = $this->getDirectoryInstance('newDir1', 0777);
366  $directory->create('test_directory');
367  $directory->create('test_directory/subdirectory');
368  $directory->writeFile('test_directory/subdirectory/test_file.txt', 'Test Content');
369 
370  $this->assertTrue($directory->changePermissionsRecursively('test_directory', 0777, 0644));
371  }
372 
374  {
375  $exceptions = 0;
376  $dir = $this->getDirectoryInstance('newDir1', 0777);
377  try {
378  $dir->changePermissionsRecursively('../foo', 0777, 0777);
379  } catch (ValidatorException $exception) {
380  $exceptions++;
381  }
382  try {
383  $dir->changePermissionsRecursively('//./..///foo', 0777, 0777);
384  } catch (ValidatorException $exception) {
385  $exceptions++;
386  }
387  try {
388  $dir->changePermissionsRecursively('\..\foo', 0777, 0777);
389  } catch (ValidatorException $exception) {
390  $exceptions++;
391  }
392  $this->assertEquals(3, $exceptions);
393  }
394 
404  public function testTouch($basePath, $permissions, $path, $time)
405  {
406  $directory = $this->getDirectoryInstance($basePath, $permissions);
407  $directory->openFile($path);
408  $this->assertTrue($directory->touch($path, $time));
409  $this->assertEquals($time, $directory->stat($path)['mtime']);
410  }
411 
417  public function touchProvider()
418  {
419  return [
420  ['test_directory', 0777, 'touch_file.txt', time() - 3600],
421  ['test_directory', 0777, 'subdirectory/touch_file.txt', time() - 3600]
422  ];
423  }
424 
425  public function testTouchOutside()
426  {
427  $exceptions = 0;
428  $dir = $this->getDirectoryInstance('newDir1', 0777);
429  try {
430  $dir->touch('../../foo.tst');
431  } catch (ValidatorException $exception) {
432  $exceptions++;
433  }
434  try {
435  $dir->touch('//./..///../foo.tst');
436  } catch (ValidatorException $exception) {
437  $exceptions++;
438  }
439  try {
440  $dir->touch('\..\..\foo.tst');
441  } catch (ValidatorException $exception) {
442  $exceptions++;
443  }
444  $this->assertEquals(3, $exceptions);
445  }
446 
450  public function testIsWritable()
451  {
452  $directory = $this->getDirectoryInstance('newDir1', 0777);
453  $directory->create('bar');
454  $this->assertFalse($directory->isWritable('not_existing_dir'));
455  $this->assertTrue($directory->isWritable('bar'));
456  }
457 
458  public function testIsWritableOutside()
459  {
460  $exceptions = 0;
461  $dir = $this->getDirectoryInstance('newDir1', 0777);
462  try {
463  $dir->isWritable('../../Directory');
464  } catch (ValidatorException $exception) {
465  $exceptions++;
466  }
467  try {
468  $dir->isWritable('//./..///../Directory');
469  } catch (ValidatorException $exception) {
470  $exceptions++;
471  }
472  try {
473  $dir->isWritable('\..\..\Directory');
474  } catch (ValidatorException $exception) {
475  $exceptions++;
476  }
477  $this->assertEquals(3, $exceptions);
478  }
479 
489  public function testOpenFile($basePath, $permissions, $path, $mode)
490  {
491  $directory = $this->getDirectoryInstance($basePath, $permissions);
492  $file = $directory->openFile($path, $mode);
493  $this->assertTrue($file instanceof \Magento\Framework\Filesystem\File\WriteInterface);
494  $file->close();
495  }
496 
502  public function openFileProvider()
503  {
504  return [
505  ['newDir1', 0777, 'newFile.txt', 'w+'],
506  ['newDir1', 0777, 'subdirectory/newFile.txt', 'w+']
507  ];
508  }
509 
510  public function testOpenFileOutside()
511  {
512  $exceptions = 0;
513  $dir = $this->getDirectoryInstance('newDir1', 0777);
514  try {
515  $dir->openFile('../../Directory/ReadTest.php');
516  } catch (ValidatorException $exception) {
517  $exceptions++;
518  }
519  try {
520  $dir->openFile('//./..///../Directory/ReadTest.php');
521  } catch (ValidatorException $exception) {
522  $exceptions++;
523  }
524  try {
525  $dir->openFile('\..\..\Directory\ReadTest.php');
526  } catch (ValidatorException $exception) {
527  $exceptions++;
528  }
529  $this->assertEquals(3, $exceptions);
530  }
531 
540  public function testWriteFile($path, $content, $extraContent)
541  {
542  $directory = $this->getDirectoryInstance('writeFileDir', 0777);
543  $directory->writeFile($path, $content);
544  $this->assertEquals($content, $directory->readFile($path));
545  $directory->writeFile($path, $extraContent);
546  $this->assertEquals($extraContent, $directory->readFile($path));
547  }
548 
557  public function testWriteFileAppend($path, $content, $extraContent)
558  {
559  $directory = $this->getDirectoryInstance('writeFileDir', 0777);
560  $directory->writeFile($path, $content, 'a+');
561  $this->assertEquals($content, $directory->readFile($path));
562  $directory->writeFile($path, $extraContent, 'a+');
563  $this->assertEquals($content . $extraContent, $directory->readFile($path));
564  }
565 
571  public function writeFileProvider()
572  {
573  return [['file1', '123', '456'], ['folder1/file1', '123', '456']];
574  }
575 
576  public function testWriteFileOutside()
577  {
578  $exceptions = 0;
579  $dir = $this->getDirectoryInstance('newDir1', 0777);
580  try {
581  $dir->writeFile('../../Directory/ReadTest.php', 'tst');
582  } catch (ValidatorException $exception) {
583  $exceptions++;
584  }
585  try {
586  $dir->writeFile('//./..///../Directory/ReadTest.php', 'tst');
587  } catch (ValidatorException $exception) {
588  $exceptions++;
589  }
590  try {
591  $dir->writeFile('\..\..\Directory\ReadTest.php', 'tst');
592  } catch (ValidatorException $exception) {
593  $exceptions++;
594  }
595  $this->assertEquals(3, $exceptions);
596  }
597 
601  public function tearDown()
602  {
604  foreach ($this->testDirectories as $directory) {
605  if ($directory->isExist()) {
606  $directory->delete();
607  }
608  }
609  }
610 
619  private function getDirectoryInstance($path, $permissions)
620  {
621  $fullPath = __DIR__ . '/../_files/' . $path;
624  $directoryFactory = $objectManager->create(\Magento\Framework\Filesystem\Directory\WriteFactory::class);
625  $directory = $directoryFactory->create($fullPath, DriverPool::FILE, $permissions);
626  $this->testDirectories[] = $directory;
627  return $directory;
628  }
629 }
testRenameTargetDir($firstDir, $secondDir, $permission, $name, $newName)
Definition: WriteTest.php:202
$objectManager
Definition: bootstrap.php:17
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
testTouch($basePath, $permissions, $path, $time)
Definition: WriteTest.php:404
testCreate($basePath, $permissions, $path)
Definition: WriteTest.php:45
testCopyTargetDir($firstDir, $secondDir, $permission, $name, $newName)
Definition: WriteTest.php:302
testRename($basePath, $permissions, $name, $newName)
Definition: WriteTest.php:145
testOpenFile($basePath, $permissions, $path, $mode)
Definition: WriteTest.php:489
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
testWriteFile($path, $content, $extraContent)
Definition: WriteTest.php:540
testCopy($basePath, $permissions, $name, $newName)
Definition: WriteTest.php:235
$permissions
testWriteFileAppend($path, $content, $extraContent)
Definition: WriteTest.php:557
if(!isset($_GET['name'])) $name
Definition: log.php:14