From 2e25beb703d01dd3ce7a044f2152533c264a51d1 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Wed, 7 Mar 2018 12:04:11 +1300 Subject: [PATCH] BUG Prevent assets folder being destroyed on behat tests ENHANCEMENT Shift into vendormodule --- composer.json | 10 +++- src/TestSessionController.php | 10 +--- src/TestSessionEnvironment.php | 90 ++++++++++++++++++++++++++++- templates/TestSession_end.ss | 4 +- templates/TestSession_inprogress.ss | 4 +- templates/TestSession_start.ss | 4 +- 6 files changed, 104 insertions(+), 18 deletions(-) diff --git a/composer.json b/composer.json index ca3ae5e..7a63288 100644 --- a/composer.json +++ b/composer.json @@ -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/" diff --git a/src/TestSessionController.php b/src/TestSessionController.php index 8cfe67e..4a53bca 100644 --- a/src/TestSessionController.php +++ b/src/TestSessionController.php @@ -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() diff --git a/src/TestSessionEnvironment.php b/src/TestSessionEnvironment.php index f6b6c91..6e62a7d 100644 --- a/src/TestSessionEnvironment.php +++ b/src/TestSessionEnvironment.php @@ -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'; + } } diff --git a/templates/TestSession_end.ss b/templates/TestSession_end.ss index b630505..9fcc407 100644 --- a/templates/TestSession_end.ss +++ b/templates/TestSession_end.ss @@ -4,8 +4,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') %>
diff --git a/templates/TestSession_inprogress.ss b/templates/TestSession_inprogress.ss index f629b62..c1b8364 100644 --- a/templates/TestSession_inprogress.ss +++ b/templates/TestSession_inprogress.ss @@ -4,8 +4,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') %>
diff --git a/templates/TestSession_start.ss b/templates/TestSession_start.ss index ee6a893..93b232a 100644 --- a/templates/TestSession_start.ss +++ b/templates/TestSession_start.ss @@ -4,8 +4,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') %>