3 namespace Drupal\Tests\system\Functional\FileTransfer;
5 use Drupal\Core\FileTransfer\FileTransferException;
6 use Drupal\Core\StreamWrapper\PublicStream;
7 use Drupal\Tests\BrowserTestBase;
10 * Tests that the jail is respected and that protocols using recursive file move
15 class FileTransferTest extends BrowserTestBase {
16 protected $hostname = 'localhost';
17 protected $username = 'drupal';
18 protected $password = 'password';
19 protected $port = '42';
21 protected function setUp() {
23 $this->testConnection = TestFileTransfer::factory(\Drupal::root(), ['hostname' => $this->hostname, 'username' => $this->username, 'password' => $this->password, 'port' => $this->port]);
26 public function _getFakeModuleFiles() {
40 public function _buildFakeModule() {
41 $location = 'temporary://fake';
42 if (is_dir($location)) {
45 exec('rm -Rf ' . escapeshellarg($location), $output, $ret);
47 throw new Exception('Error removing fake module directory.');
51 $files = $this->_getFakeModuleFiles();
52 $this->_writeDirectory($location, $files);
56 public function _writeDirectory($base, $files = []) {
58 foreach ($files as $key => $file) {
59 if (is_array($file)) {
60 $this->_writeDirectory($base . DIRECTORY_SEPARATOR . $key, $file);
63 // just write the filename into the file
64 file_put_contents($base . DIRECTORY_SEPARATOR . $file, $file);
69 public function testJail() {
70 $source = $this->_buildFakeModule();
72 // This convoluted piece of code is here because our testing framework does
73 // not support expecting exceptions.
76 $this->testConnection->copyDirectory($source, sys_get_temp_dir());
78 catch (FileTransferException $e) {
81 $this->assertTrue($gotit, 'Was not able to copy a directory outside of the jailed area.');
85 $this->testConnection->copyDirectory($source, \Drupal::root() . '/' . PublicStream::basePath());
87 catch (FileTransferException $e) {
90 $this->assertTrue($gotit, 'Was able to copy a directory inside of the jailed area');