mirror of
https://github.com/silverstripe/silverstripe-testsession
synced 2024-10-22 14:06:00 +02:00
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:
commit
961069473c
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "silverstripe/testsession",
|
||||
"type": "silverstripe-module",
|
||||
"type": "silverstripe-vendormodule",
|
||||
"description": "Support module for browser-based test sessions, e.g. for Behat behaviour testing",
|
||||
"homepage": "http://silverstripe.org",
|
||||
"license": "BSD-3-Clause",
|
||||
@ -16,12 +16,16 @@
|
||||
],
|
||||
"require": {
|
||||
"composer/installers": "*",
|
||||
"silverstripe/framework": "^4@dev"
|
||||
"silverstripe/framework": "^4@dev",
|
||||
"silverstripe/vendor-plugin": "^1.3"
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"2.x-dev": "2.1.x-dev"
|
||||
}
|
||||
},
|
||||
"expose": [
|
||||
"client"
|
||||
]
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "phpcs -s src/ tests/"
|
||||
|
@ -31,6 +31,7 @@ use SilverStripe\View\Requirements;
|
||||
*/
|
||||
class TestSessionController extends Controller
|
||||
{
|
||||
private static $url_segment = 'dev/testsession';
|
||||
|
||||
private static $allowed_actions = array(
|
||||
'index',
|
||||
@ -77,13 +78,8 @@ class TestSessionController extends Controller
|
||||
return;
|
||||
}
|
||||
|
||||
Requirements::javascript('http://code.jquery.com/jquery-1.7.2.min.js');
|
||||
Requirements::javascript('testsession/client/js/testsession.js');
|
||||
}
|
||||
|
||||
public function Link($action = null)
|
||||
{
|
||||
return Controller::join_links(Director::baseURL(), 'dev/testsession', $action);
|
||||
Requirements::javascript('//code.jquery.com/jquery-1.7.2.min.js');
|
||||
Requirements::javascript('silverstripe/testsession:client/js/testsession.js');
|
||||
}
|
||||
|
||||
public function index()
|
||||
|
@ -2,9 +2,11 @@
|
||||
|
||||
namespace SilverStripe\TestSession;
|
||||
|
||||
use DirectoryIterator;
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use LogicException;
|
||||
use SilverStripe\Assets\Filesystem;
|
||||
use SilverStripe\Core\Environment;
|
||||
use SilverStripe\Control\Director;
|
||||
use SilverStripe\Control\HTTPRequest;
|
||||
@ -113,7 +115,7 @@ class TestSessionEnvironment
|
||||
*/
|
||||
public function isRunningTests()
|
||||
{
|
||||
return(file_exists($this->getFilePath()));
|
||||
return (file_exists($this->getFilePath()));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -163,6 +165,9 @@ class TestSessionEnvironment
|
||||
|
||||
$this->applyState($state);
|
||||
|
||||
// Back up /assets folder
|
||||
$this->backupAssets();
|
||||
|
||||
$this->extend('onAfterStartTestSession');
|
||||
}
|
||||
|
||||
@ -179,6 +184,73 @@ class TestSessionEnvironment
|
||||
$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
|
||||
* _config.php where we don't yet have a DB connection.
|
||||
@ -395,6 +467,10 @@ class TestSessionEnvironment
|
||||
{
|
||||
$this->extend('onBeforeEndTestSession');
|
||||
|
||||
// Restore assets
|
||||
$this->restoreAssets();
|
||||
|
||||
// Reset DB
|
||||
$tempDB = new TempDatabase();
|
||||
if ($tempDB->isUsed()) {
|
||||
$state = $this->getState();
|
||||
@ -423,7 +499,7 @@ class TestSessionEnvironment
|
||||
*/
|
||||
public function loadFixtureIntoDb($fixtureFile)
|
||||
{
|
||||
$realFile = realpath(BASE_PATH.'/'.$fixtureFile);
|
||||
$realFile = realpath(BASE_PATH . '/' . $fixtureFile);
|
||||
$baseDir = realpath(Director::baseFolder());
|
||||
if (!$realFile || !file_exists($realFile)) {
|
||||
throw new LogicException("Fixture file doesn't exist");
|
||||
@ -472,4 +548,14 @@ class TestSessionEnvironment
|
||||
$path = Director::getAbsFile($this->getFilePath());
|
||||
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';
|
||||
}
|
||||
}
|
||||
|
@ -4,8 +4,8 @@
|
||||
<meta charset="utf-8">
|
||||
<% base_tag %>
|
||||
$MetaTags
|
||||
<% require css('framework/client/dist/styles/debug.css') %>
|
||||
<% require css('testsession/client/styles/styles.css') %>
|
||||
<% require css('silverstripe/framework:client/styles/debug.css') %>
|
||||
<% require css('silverstripe/testsession:client/styles/styles.css') %>
|
||||
</head>
|
||||
<body>
|
||||
<div class="info">
|
||||
|
@ -4,8 +4,8 @@
|
||||
<meta charset="utf-8">
|
||||
<% base_tag %>
|
||||
$MetaTags
|
||||
<% require css('framework/client/dist/styles/debug.css') %>
|
||||
<% require css('testsession/client/styles/styles.css') %>
|
||||
<% require css('silverstripe/framework:client/styles/debug.css') %>
|
||||
<% require css('silverstripe/testsession:client/styles/styles.css') %>
|
||||
</head>
|
||||
<body>
|
||||
<div class="info">
|
||||
|
@ -4,8 +4,8 @@
|
||||
<meta charset="utf-8">
|
||||
<% base_tag %>
|
||||
$MetaTags
|
||||
<% require css('framework/client/dist/styles/debug.css') %>
|
||||
<% require css('testsession/client/styles/styles.css') %>
|
||||
<% require css('silverstripe/framework:client/styles/debug.css') %>
|
||||
<% require css('silverstripe/testsession:client/styles/styles.css') %>
|
||||
</head>
|
||||
<body>
|
||||
<div class="info">
|
||||
|
Loading…
Reference in New Issue
Block a user