04b1f663614cc79bfa83748ad43dc25955b02117
[yaffs-website] / web / core / modules / system / tests / src / Functional / FileTransfer / FileTransferTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\FileTransfer;
4
5 use Drupal\Core\FileTransfer\FileTransferException;
6 use Drupal\Core\StreamWrapper\PublicStream;
7 use Drupal\Tests\BrowserTestBase;
8
9 /**
10  * Tests that the jail is respected and that protocols using recursive file move
11  * operations work.
12  *
13  * @group FileTransfer
14  */
15 class FileTransferTest extends BrowserTestBase {
16   protected $hostname = 'localhost';
17   protected $username = 'drupal';
18   protected $password = 'password';
19   protected $port = '42';
20
21   protected function setUp() {
22     parent::setUp();
23     $this->testConnection = TestFileTransfer::factory(\Drupal::root(), ['hostname' => $this->hostname, 'username' => $this->username, 'password' => $this->password, 'port' => $this->port]);
24   }
25
26   public function _getFakeModuleFiles() {
27     $files = [
28       'fake.module',
29       'fake.info.yml',
30       'theme' => [
31         'fake.html.twig'
32       ],
33       'inc' => [
34         'fake.inc'
35       ]
36     ];
37     return $files;
38   }
39
40   public function _buildFakeModule() {
41     $location = 'temporary://fake';
42     if (is_dir($location)) {
43       $ret = 0;
44       $output = [];
45       exec('rm -Rf ' . escapeshellarg($location), $output, $ret);
46       if ($ret != 0) {
47         throw new Exception('Error removing fake module directory.');
48       }
49     }
50
51     $files = $this->_getFakeModuleFiles();
52     $this->_writeDirectory($location, $files);
53     return $location;
54   }
55
56   public function _writeDirectory($base, $files = []) {
57     mkdir($base);
58     foreach ($files as $key => $file) {
59       if (is_array($file)) {
60         $this->_writeDirectory($base . DIRECTORY_SEPARATOR . $key, $file);
61       }
62       else {
63         // just write the filename into the file
64         file_put_contents($base . DIRECTORY_SEPARATOR . $file, $file);
65       }
66     }
67   }
68
69   public function testJail() {
70     $source = $this->_buildFakeModule();
71
72     // This convoluted piece of code is here because our testing framework does
73     // not support expecting exceptions.
74     $gotit = FALSE;
75     try {
76       $this->testConnection->copyDirectory($source, sys_get_temp_dir());
77     }
78     catch (FileTransferException $e) {
79       $gotit = TRUE;
80     }
81     $this->assertTrue($gotit, 'Was not able to copy a directory outside of the jailed area.');
82
83     $gotit = TRUE;
84     try {
85       $this->testConnection->copyDirectory($source, \Drupal::root() . '/' . PublicStream::basePath());
86     }
87     catch (FileTransferException $e) {
88       $gotit = FALSE;
89     }
90     $this->assertTrue($gotit, 'Was able to copy a directory inside of the jailed area');
91   }
92
93 }