drupal/modules/simpletest/tests/filetransfer.test

200 lines
6.3 KiB
Plaintext

<?php
// $Id$
class FileTranferTest extends DrupalWebTestCase {
protected $hostname = 'localhost';
protected $username = 'drupal';
protected $password = 'password';
protected $port = '42';
public static function getInfo() {
return array(
'name' => t('FileTransfer unit tests'),
'description' => t('Test that the jail is respected and that protocols using recursive file move operations work.'),
'group' => t('System')
);
}
function setUp() {
$this->testConnection = TestFileTransfer::factory(DRUPAL_ROOT, array('hostname' => $this->hostname, 'username' => $this->username, 'password' => $this->password, 'port' => $this->port));
}
function _getFakeModuleFiles() {
$files = array(
'fake.module',
'fake.info',
'theme' => array(
'fake.tpl.php'
),
'inc' => array(
'fake.inc'
)
);
return $files;
}
function _buildFakeModule() {
$location = file_directory_temp() . '/fake';
if (is_dir($location)) {
$ret = 0;
$output = array();
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;
}
function _writeDirectory($base, $files = array()) {
mkdir($base);
foreach ($files as $key => $file) {
if (is_array($file)) {
$this->_writeDirectory($base . DIRECTORY_SEPARATOR . $key, $file);
}
else {
//just write the filename into the file
file_put_contents($base . DIRECTORY_SEPARATOR . $file, $file);
}
}
}
function testJail() {
$source = $this->_buildFakeModule();
// This convoluted piece of code is here because our testing framework does
// not support expecting exceptions.
$gotit = FALSE;
try {
$this->testConnection->copyDirectory($source, '/tmp');
}
catch (FileTransferException $e) {
$gotit = TRUE;
}
$this->assertTrue($gotit, 'Was not able to copy a directory outside of the jailed area.');
$gotit = TRUE;
try {
$this->testConnection->copyDirectory($source, DRUPAL_ROOT . '/'. file_directory_path());
}
catch (FileTransferException $e) {
$gotit = FALSE;
}
$this->assertTrue($gotit, 'Was able to copy a directory inside of the jailed area');
}
function testCopyDirectory() {
$directory = $this->_buildFakeModule();
$drupal_root = DRUPAL_ROOT;
$this->testConnection->shouldIsDirectoryReturnTrue = TRUE;
$this->testConnection->copyDirectory($directory, "{$drupal_root}/sites/all/modules");
$expected_commands = array(
"mkdir {$drupal_root}/sites/all/modules/fake",
"copyFile {$directory}/fake.info {$drupal_root}/sites/all/modules/fake/fake.info",
"copyFile {$directory}/fake.module {$drupal_root}/sites/all/modules/fake/fake.module",
"mkdir {$drupal_root}/sites/all/modules/fake/inc",
"copyFile {$directory}/inc/fake.inc {$drupal_root}/sites/all/modules/fake/inc/fake.inc",
"mkdir {$drupal_root}/sites/all/modules/fake/theme",
"copyFile {$directory}/theme/fake.tpl.php {$drupal_root}/sites/all/modules/fake/theme/fake.tpl.php",
);
$received_commands = $this->testConnection->connection->flushCommands();
$this->assertEqual($received_commands, $expected_commands, 'Expected copy files operations made to sites/all/modules');
$this->testConnection->shouldIsDirectoryReturnTrue = FALSE;
$this->testConnection->copyDirectory($directory, "{$drupal_root}/sites/all/modules/fake");
$expected_commands = array(
"mkdir {$drupal_root}/sites/all/modules/fake",
"copyFile {$directory}/fake.info {$drupal_root}/sites/all/modules/fake/fake.info",
"copyFile {$directory}/fake.module {$drupal_root}/sites/all/modules/fake/fake.module",
"mkdir {$drupal_root}/sites/all/modules/fake/inc",
"copyFile {$directory}/inc/fake.inc {$drupal_root}/sites/all/modules/fake/inc/fake.inc",
"mkdir {$drupal_root}/sites/all/modules/fake/theme",
"copyFile {$directory}/theme/fake.tpl.php {$drupal_root}/sites/all/modules/fake/theme/fake.tpl.php",
);
$received_commands = $this->testConnection->connection->flushCommands();
dd($expected_commands);
dd($received_commands);
$this->assertEqual($received_commands, $expected_commands, 'Expected copy files operations made to sites/all/modules/fake');
}
}
/**
* Mock FileTransfer object for test case.
*/
class TestFileTransfer extends FileTransfer {
protected $host = NULL;
protected $username = NULL;
protected $password = NULL;
protected $port = NULL;
/**
* This is for testing the CopyRecursive logic.
*/
public $shouldIsDirectoryReturnTrue = FALSE;
function __construct($jail, $username, $password, $hostname = 'localhost', $port = 9999) {
parent::__construct($jail, $username, $password, $hostname, $port);
}
static function factory($jail, $settings) {
return new TestFileTransfer($jail, $settings['username'], $settings['password'], $settings['hostname'], $settings['port']);
}
function connect() {
$parts = explode(':', $this->hostname);
$port = (count($parts) == 2) ? $parts[1] : $this->port;
$this->connection = new MockTestConnection();
$this->connection->connectionString = 'test://' . urlencode($this->username) . ':' . urlencode($this->password) . "@$this->host:$this->port/";
}
function copyFileJailed($source, $destination) {
$this->connection->run("copyFile $source $destination");
}
protected function removeDirectoryJailed($directory) {
$this->connection->run("rmdir $directory");
}
function createDirectoryJailed($directory) {
$this->connection->run("mkdir $directory");
}
function removeFileJailed($destination) {
if (!ftp_delete($this->connection, $item)) {
throw new FileTransferException('Unable to remove to file @file.', NULL, array('@file' => $item));
}
}
function isDirectory($path) {
return $this->shouldIsDirectoryReturnTrue;
}
}
/**
* Mock connection object for test case.
*/
class MockTestConnection {
var $commandsRun = array();
var $connectionString;
function run($cmd) {
$this->commandsRun[] = $cmd;
}
function flushCommands() {
$out = $this->commandsRun;
$this->commandsRun = array();
return $out;
}
}