Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG Prevent assets folder being destroyed on behat tests #56

Merged
merged 1 commit into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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/"
Expand Down
10 changes: 3 additions & 7 deletions src/TestSessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*/
class TestSessionController extends Controller
{
private static $url_segment = 'dev/testsession';

private static $allowed_actions = array(
'index',
Expand Down Expand Up @@ -77,13 +78,8 @@ public function init()
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()
Expand Down
90 changes: 88 additions & 2 deletions src/TestSessionEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -113,7 +115,7 @@ public function getFilePath()
*/
public function isRunningTests()
{
return(file_exists($this->getFilePath()));
return (file_exists($this->getFilePath()));
}

/**
Expand Down Expand Up @@ -163,6 +165,9 @@ public function startTestSession($state = null, $id = null)

$this->applyState($state);

// Back up /assets folder
$this->backupAssets();

$this->extend('onAfterStartTestSession');
}

Expand All @@ -179,6 +184,73 @@ public function updateTestSession($state)
$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.
Expand Down Expand Up @@ -395,6 +467,10 @@ public function endTestSession()
{
$this->extend('onBeforeEndTestSession');

// Restore assets
$this->restoreAssets();

// Reset DB
$tempDB = new TempDatabase();
if ($tempDB->isUsed()) {
$state = $this->getState();
Expand Down Expand Up @@ -423,7 +499,7 @@ public function endTestSession()
*/
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");
Expand Down Expand Up @@ -472,4 +548,14 @@ public function getState()
$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 changes: 2 additions & 2 deletions templates/TestSession_end.ss
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
4 changes: 2 additions & 2 deletions templates/TestSession_inprogress.ss
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
4 changes: 2 additions & 2 deletions templates/TestSession_start.ss
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down