FileTransferTest.php 2.5 KB
Newer Older
1 2
<?php

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 11 12 13
 * Tests that the jail is respected and that protocols using recursive file move
 * operations work.
 *
 * @group FileTransfer
14
 */
15
class FileTransferTest extends BrowserTestBase {
16 17 18 19 20
  protected $hostname = 'localhost';
  protected $username = 'drupal';
  protected $password = 'password';
  protected $port = '42';

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 38 39
    return $files;
  }

40
  public function _buildFakeModule() {
41
    $location = 'temporary://fake';
42 43
    if (is_dir($location)) {
      $ret = 0;
44
      $output = [];
45 46 47 48 49 50 51 52 53 54 55
      exec('rm -Rf ' . escapeshellarg($location), $output, $ret);
      if ($ret != 0) {
        throw new Exception('Error removing fake module directory.');
      }
    }

    $files = $this->_getFakeModuleFiles();
    $this->_writeDirectory($location, $files);
    return $location;
  }

56
  public function _writeDirectory($base, $files = []) {
57 58 59 60 61 62
    mkdir($base);
    foreach ($files as $key => $file) {
      if (is_array($file)) {
        $this->_writeDirectory($base . DIRECTORY_SEPARATOR . $key, $file);
      }
      else {
63
        // just write the filename into the file
64 65 66 67 68
        file_put_contents($base . DIRECTORY_SEPARATOR . $file, $file);
      }
    }
  }

69
  public function testJail() {
70 71 72 73 74 75
    $source = $this->_buildFakeModule();

    // This convoluted piece of code is here because our testing framework does
    // not support expecting exceptions.
    $gotit = FALSE;
    try {
76
      $this->testConnection->copyDirectory($source, sys_get_temp_dir());
77 78 79 80 81 82 83 84
    }
    catch (FileTransferException $e) {
      $gotit = TRUE;
    }
    $this->assertTrue($gotit, 'Was not able to copy a directory outside of the jailed area.');

    $gotit = TRUE;
    try {
85
      $this->testConnection->copyDirectory($source, \Drupal::root() . '/' . PublicStream::basePath());
86 87 88 89 90 91
    }
    catch (FileTransferException $e) {
      $gotit = FALSE;
    }
    $this->assertTrue($gotit, 'Was able to copy a directory inside of the jailed area');
  }
92

93
}