Merge pull request #56 from open-sausages/pulls/2/fix-assets

BUG Prevent assets folder being destroyed on behat tests
This commit is contained in:
Daniel Hensby 2018-03-07 11:40:40 +00:00 committed by GitHub
commit 961069473c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 104 additions and 18 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "silverstripe/testsession", "name": "silverstripe/testsession",
"type": "silverstripe-module", "type": "silverstripe-vendormodule",
"description": "Support module for browser-based test sessions, e.g. for Behat behaviour testing", "description": "Support module for browser-based test sessions, e.g. for Behat behaviour testing",
"homepage": "http://silverstripe.org", "homepage": "http://silverstripe.org",
"license": "BSD-3-Clause", "license": "BSD-3-Clause",
@ -16,12 +16,16 @@
], ],
"require": { "require": {
"composer/installers": "*", "composer/installers": "*",
"silverstripe/framework": "^4@dev" "silverstripe/framework": "^4@dev",
"silverstripe/vendor-plugin": "^1.3"
}, },
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"2.x-dev": "2.1.x-dev" "2.x-dev": "2.1.x-dev"
} },
"expose": [
"client"
]
}, },
"scripts": { "scripts": {
"lint": "phpcs -s src/ tests/" "lint": "phpcs -s src/ tests/"

View File

@ -31,6 +31,7 @@ use SilverStripe\View\Requirements;
*/ */
class TestSessionController extends Controller class TestSessionController extends Controller
{ {
private static $url_segment = 'dev/testsession';
private static $allowed_actions = array( private static $allowed_actions = array(
'index', 'index',
@ -77,13 +78,8 @@ class TestSessionController extends Controller
return; return;
} }
Requirements::javascript('http://code.jquery.com/jquery-1.7.2.min.js'); Requirements::javascript('//code.jquery.com/jquery-1.7.2.min.js');
Requirements::javascript('testsession/client/js/testsession.js'); Requirements::javascript('silverstripe/testsession:client/js/testsession.js');
}
public function Link($action = null)
{
return Controller::join_links(Director::baseURL(), 'dev/testsession', $action);
} }
public function index() public function index()

View File

@ -2,9 +2,11 @@
namespace SilverStripe\TestSession; namespace SilverStripe\TestSession;
use DirectoryIterator;
use Exception; use Exception;
use InvalidArgumentException; use InvalidArgumentException;
use LogicException; use LogicException;
use SilverStripe\Assets\Filesystem;
use SilverStripe\Core\Environment; use SilverStripe\Core\Environment;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPRequest;
@ -163,6 +165,9 @@ class TestSessionEnvironment
$this->applyState($state); $this->applyState($state);
// Back up /assets folder
$this->backupAssets();
$this->extend('onAfterStartTestSession'); $this->extend('onAfterStartTestSession');
} }
@ -179,6 +184,73 @@ class TestSessionEnvironment
$this->extend('onAfterUpdateTestSession'); $this->extend('onAfterUpdateTestSession');
} }
/**
* Backup all assets from /assets to /assets_backup.
* Note: Only does file move, no files ever duplicated / deleted
*/
protected function backupAssets()
{
// Ensure files backed up to assets dir
$backupFolder = $this->getAssetsBackupfolder();
if (!is_dir($backupFolder)) {
Filesystem::makeFolder($backupFolder);
}
$this->moveRecursive(ASSETS_PATH, $backupFolder, ['.htaccess', 'web.config', '.protected']);
}
/**
* Restore all assets to /assets folder.
* Note: Only does file move, no files ever duplicated / deleted
*/
public function restoreAssets()
{
// Ensure files backed up to assets dir
$backupFolder = $this->getAssetsBackupfolder();
if (is_dir($backupFolder)) {
// Move all files
Filesystem::makeFolder(ASSETS_PATH);
$this->moveRecursive($backupFolder, ASSETS_PATH);
Filesystem::removeFolder($backupFolder);
}
}
/**
* Recursively move files from one directory to another
*
* @param string $src Source of files being moved
* @param string $dest Destination of files being moved
* @param array $ignore List of files to not move
*/
protected function moveRecursive($src, $dest, $ignore = [])
{
// If source is not a directory stop processing
if (!is_dir($src)) {
return;
}
// If the destination directory does not exist create it
if (!is_dir($dest) && !mkdir($dest)) {
// If the destination directory could not be created stop processing
return;
}
// Open the source directory to read in files
$iterator = new DirectoryIterator($src);
foreach ($iterator as $file) {
if ($file->isFile()) {
if (!in_array($file->getFilename(), $ignore)) {
rename($file->getRealPath(), $dest . DIRECTORY_SEPARATOR . $file->getFilename());
}
} elseif (!$file->isDot() && $file->isDir()) {
// If a dir is ignored, still move children but don't remove self
$this->moveRecursive($file->getRealPath(), $dest . DIRECTORY_SEPARATOR . $file);
if (!in_array($file->getFilename(), $ignore)) {
Filesystem::removeFolder($file->getRealPath());
}
}
}
}
/** /**
* Assumes the database has already been created in startTestSession(), as this method can be called from * Assumes the database has already been created in startTestSession(), as this method can be called from
* _config.php where we don't yet have a DB connection. * _config.php where we don't yet have a DB connection.
@ -395,6 +467,10 @@ class TestSessionEnvironment
{ {
$this->extend('onBeforeEndTestSession'); $this->extend('onBeforeEndTestSession');
// Restore assets
$this->restoreAssets();
// Reset DB
$tempDB = new TempDatabase(); $tempDB = new TempDatabase();
if ($tempDB->isUsed()) { if ($tempDB->isUsed()) {
$state = $this->getState(); $state = $this->getState();
@ -472,4 +548,14 @@ class TestSessionEnvironment
$path = Director::getAbsFile($this->getFilePath()); $path = Director::getAbsFile($this->getFilePath());
return (file_exists($path)) ? json_decode(file_get_contents($path)) : new stdClass; return (file_exists($path)) ? json_decode(file_get_contents($path)) : new stdClass;
} }
/**
* Path where assets should be backed up during testing
*
* @return string
*/
protected function getAssetsBackupfolder()
{
return PUBLIC_PATH . DIRECTORY_SEPARATOR . 'assets_backup';
}
} }

View File

@ -4,8 +4,8 @@
<meta charset="utf-8"> <meta charset="utf-8">
<% base_tag %> <% base_tag %>
$MetaTags $MetaTags
<% require css('framework/client/dist/styles/debug.css') %> <% require css('silverstripe/framework:client/styles/debug.css') %>
<% require css('testsession/client/styles/styles.css') %> <% require css('silverstripe/testsession:client/styles/styles.css') %>
</head> </head>
<body> <body>
<div class="info"> <div class="info">

View File

@ -4,8 +4,8 @@
<meta charset="utf-8"> <meta charset="utf-8">
<% base_tag %> <% base_tag %>
$MetaTags $MetaTags
<% require css('framework/client/dist/styles/debug.css') %> <% require css('silverstripe/framework:client/styles/debug.css') %>
<% require css('testsession/client/styles/styles.css') %> <% require css('silverstripe/testsession:client/styles/styles.css') %>
</head> </head>
<body> <body>
<div class="info"> <div class="info">

View File

@ -4,8 +4,8 @@
<meta charset="utf-8"> <meta charset="utf-8">
<% base_tag %> <% base_tag %>
$MetaTags $MetaTags
<% require css('framework/client/dist/styles/debug.css') %> <% require css('silverstripe/framework:client/styles/debug.css') %>
<% require css('testsession/client/styles/styles.css') %> <% require css('silverstripe/testsession:client/styles/styles.css') %>
</head> </head>
<body> <body>
<div class="info"> <div class="info">