mirror of
https://github.com/silverstripe/silverstripe-testsession
synced 2024-10-22 14:06:00 +02:00
Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
ee49a440fb | ||
|
cb34aa869f | ||
|
5c8070044c | ||
|
8e3f71afb7 | ||
|
fe5e87598f | ||
|
ffe1bc8fa2 | ||
|
132c00f122 | ||
|
9b91b2de98 | ||
|
ecac4295af | ||
|
c4786cd955 | ||
|
ffe829485e | ||
|
fc63ebe8a9 | ||
|
8535a680ca | ||
|
8db444605a | ||
|
cefce74559 | ||
|
a1e8643ea7 | ||
|
0f5cb30743 | ||
|
7733cc7c95 | ||
|
5e2ef7e52c | ||
|
e44774dbf0 |
11
.github/workflows/ci.yml
vendored
Normal file
11
.github/workflows/ci.yml
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
ci:
|
||||
name: CI
|
||||
uses: silverstripe/gha-ci/.github/workflows/ci.yml@v1
|
16
.github/workflows/dispatch-ci.yml
vendored
Normal file
16
.github/workflows/dispatch-ci.yml
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
name: Dispatch CI
|
||||
|
||||
on:
|
||||
# At 2:30 PM UTC, only on Monday and Tuesday
|
||||
schedule:
|
||||
- cron: '30 14 * * 1,2'
|
||||
|
||||
jobs:
|
||||
dispatch-ci:
|
||||
name: Dispatch CI
|
||||
# Only run cron on the silverstripe account
|
||||
if: (github.event_name == 'schedule' && github.repository_owner == 'silverstripe') || (github.event_name != 'schedule')
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Dispatch CI
|
||||
uses: silverstripe/gha-dispatch-ci@v1
|
17
.github/workflows/keepalive.yml
vendored
Normal file
17
.github/workflows/keepalive.yml
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
name: Keepalive
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
# The 4th of every month at 10:50am UTC
|
||||
schedule:
|
||||
- cron: '50 10 4 * *'
|
||||
|
||||
jobs:
|
||||
keepalive:
|
||||
name: Keepalive
|
||||
# Only run cron on the silverstripe account
|
||||
if: (github.event_name == 'schedule' && github.repository_owner == 'silverstripe') || (github.event_name != 'schedule')
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Keepalive
|
||||
uses: silverstripe/gha-keepalive@v1
|
@ -1,69 +0,0 @@
|
||||
inherit: true
|
||||
|
||||
checks:
|
||||
php:
|
||||
verify_property_names: true
|
||||
verify_argument_usable_as_reference: true
|
||||
verify_access_scope_valid: true
|
||||
useless_calls: true
|
||||
use_statement_alias_conflict: true
|
||||
variable_existence: true
|
||||
unused_variables: true
|
||||
unused_properties: true
|
||||
unused_parameters: true
|
||||
unused_methods: true
|
||||
unreachable_code: true
|
||||
too_many_arguments: true
|
||||
sql_injection_vulnerabilities: true
|
||||
simplify_boolean_return: true
|
||||
side_effects_or_types: true
|
||||
security_vulnerabilities: true
|
||||
return_doc_comments: true
|
||||
return_doc_comment_if_not_inferrable: true
|
||||
require_scope_for_properties: true
|
||||
require_scope_for_methods: true
|
||||
require_php_tag_first: true
|
||||
psr2_switch_declaration: true
|
||||
psr2_class_declaration: true
|
||||
property_assignments: true
|
||||
prefer_while_loop_over_for_loop: true
|
||||
precedence_mistakes: true
|
||||
precedence_in_conditions: true
|
||||
phpunit_assertions: true
|
||||
php5_style_constructor: true
|
||||
parse_doc_comments: true
|
||||
parameter_non_unique: true
|
||||
parameter_doc_comments: true
|
||||
param_doc_comment_if_not_inferrable: true
|
||||
optional_parameters_at_the_end: true
|
||||
one_class_per_file: true
|
||||
no_unnecessary_if: true
|
||||
no_trailing_whitespace: true
|
||||
no_property_on_interface: true
|
||||
no_non_implemented_abstract_methods: true
|
||||
no_error_suppression: true
|
||||
no_duplicate_arguments: true
|
||||
no_commented_out_code: true
|
||||
newline_at_end_of_file: true
|
||||
missing_arguments: true
|
||||
method_calls_on_non_object: true
|
||||
instanceof_class_exists: true
|
||||
foreach_traversable: true
|
||||
fix_line_ending: true
|
||||
fix_doc_comments: true
|
||||
duplication: true
|
||||
deprecated_code_usage: true
|
||||
deadlock_detection_in_loops: true
|
||||
code_rating: true
|
||||
closure_use_not_conflicting: true
|
||||
catch_class_exists: true
|
||||
blank_line_after_namespace_declaration: false
|
||||
avoid_multiple_statements_on_same_line: true
|
||||
avoid_duplicate_types: true
|
||||
avoid_conflicting_incrementers: true
|
||||
avoid_closing_tag: true
|
||||
assignment_of_null_return: true
|
||||
argument_type_checks: true
|
||||
|
||||
filter:
|
||||
paths: [code/*, tests/*]
|
@ -1,6 +1,6 @@
|
||||
# Browser Test Session Module
|
||||
|
||||
[![Build Status](https://travis-ci.org/silverstripe-labs/silverstripe-testsession.svg)](https://travis-ci.org/silverstripe-labs/silverstripe-testsession)
|
||||
[![CI](https://github.com/silverstripe/silverstripe-testsession/actions/workflows/ci.yml/badge.svg)](https://github.com/silverstripe/silverstripe-testsession/actions/workflows/ci.yml)
|
||||
|
||||
## Overview
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
*It's completely possible to allow any user to become an admin, or do other nefarious things, if this is installed on a live site.*
|
||||
|
||||
This module starts a testing session in a browser,
|
||||
in order to test a SilverStripe application in a clean state.
|
||||
in order to test a Silverstripe application in a clean state.
|
||||
Usually the session is started on a fresh database with only default records loaded.
|
||||
Further data can be loaded from YAML fixtures or database dumps.
|
||||
|
||||
@ -26,7 +26,7 @@ E.g. the silverstripe-behat-extension may use it through this module APIs,
|
||||
allowing us to introduce some grey-box testing techniques.
|
||||
|
||||
The module also serves as an initializer for the
|
||||
[SilverStripe Behat Extension](https://github.com/silverstripe-labs/silverstripe-behat-extension/).
|
||||
[Silverstripe Behat Extension](https://github.com/silverstripe-labs/silverstripe-behat-extension/).
|
||||
It is required for Behat because the Behat CLI test runner needs to persist
|
||||
test configuration just for the tested browser connection,
|
||||
available on arbitary URL endpoints. For example,
|
||||
@ -35,7 +35,7 @@ into a temporary database table for inspection by the CLI-based process.
|
||||
|
||||
## Setup
|
||||
|
||||
Simply require the module in a SilverStripe webroot (3.0 or newer):
|
||||
Simply require the module in a Silverstripe webroot (3.0 or newer):
|
||||
|
||||
composer require --dev silverstripe/behat-extension
|
||||
|
||||
|
@ -19,10 +19,10 @@
|
||||
"silverstripe/framework": "^4@dev",
|
||||
"silverstripe/vendor-plugin": "^1.3"
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"2.x-dev": "2.2.x-dev"
|
||||
"require-dev": {
|
||||
"squizlabs/php_codesniffer": "^3.5"
|
||||
},
|
||||
"extra": {
|
||||
"expose": [
|
||||
"client"
|
||||
]
|
||||
|
@ -2,6 +2,8 @@
|
||||
<ruleset name="SilverStripe">
|
||||
<description>CodeSniffer ruleset for SilverStripe coding conventions.</description>
|
||||
|
||||
<file>src</file>
|
||||
|
||||
<!-- base rules are PSR-2 -->
|
||||
<rule ref="PSR2" >
|
||||
<!-- Current exclusions -->
|
||||
|
@ -104,7 +104,7 @@ class TestSessionController extends Controller
|
||||
$id = null;
|
||||
} else {
|
||||
$generator = Injector::inst()->get(RandomGenerator::class);
|
||||
$id = substr($generator->randomToken(), 0, 10);
|
||||
$id = substr($generator->randomToken() ?? '', 0, 10);
|
||||
$this->getRequest()->getSession()->set('TestSessionId', $id);
|
||||
}
|
||||
|
||||
@ -113,7 +113,7 @@ class TestSessionController extends Controller
|
||||
|
||||
// Remove unnecessary items of form-specific data from being saved in the test session
|
||||
$params = array_diff_key(
|
||||
$params,
|
||||
$params ?? [],
|
||||
array(
|
||||
'action_set' => true,
|
||||
'action_start' => true,
|
||||
@ -170,7 +170,7 @@ class TestSessionController extends Controller
|
||||
throw new LogicException("No test session in progress.");
|
||||
}
|
||||
|
||||
$newSessionStates = array_diff_key($request->getVars(), array('url' => true));
|
||||
$newSessionStates = array_diff_key($request->getVars() ?? [], array('url' => true));
|
||||
if (!$newSessionStates) {
|
||||
throw new LogicException('No query parameters detected');
|
||||
}
|
||||
@ -288,7 +288,7 @@ class TestSessionController extends Controller
|
||||
|
||||
// Remove unnecessary items of form-specific data from being saved in the test session
|
||||
$params = array_diff_key(
|
||||
$params,
|
||||
$params ?? [],
|
||||
array(
|
||||
'action_set' => true,
|
||||
'action_start' => true,
|
||||
@ -399,7 +399,7 @@ class TestSessionController extends Controller
|
||||
$path = BASE_PATH . '/' . $path;
|
||||
}
|
||||
|
||||
if ($path && file_exists($path)) {
|
||||
if ($path && file_exists($path ?? '')) {
|
||||
$it = new FilesystemIterator($path);
|
||||
foreach ($it as $fileinfo) {
|
||||
if ($fileinfo->getExtension() != 'sql') {
|
||||
|
@ -80,7 +80,6 @@ class TestSessionEnvironment
|
||||
|
||||
public function __construct($id = null)
|
||||
{
|
||||
$this->constructExtensions();
|
||||
if ($id) {
|
||||
$this->id = $id;
|
||||
}
|
||||
@ -102,7 +101,7 @@ class TestSessionEnvironment
|
||||
public function getFilePath()
|
||||
{
|
||||
if ($this->id) {
|
||||
$path = Director::getAbsFile(sprintf($this->config()->get('test_state_id_file'), $this->id));
|
||||
$path = Director::getAbsFile(sprintf($this->config()->get('test_state_id_file') ?? '', $this->id));
|
||||
} else {
|
||||
$path = Director::getAbsFile($this->config()->get('test_state_file'));
|
||||
}
|
||||
@ -115,7 +114,7 @@ class TestSessionEnvironment
|
||||
*/
|
||||
public function isRunningTests()
|
||||
{
|
||||
return (file_exists($this->getFilePath()));
|
||||
return (file_exists($this->getFilePath() ?? ''));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -161,7 +160,7 @@ class TestSessionEnvironment
|
||||
|
||||
// Convert to JSON and back so we can share the applyState() code between this and ->loadFromFile()
|
||||
$json = json_encode($state, JSON_FORCE_OBJECT);
|
||||
$state = json_decode($json);
|
||||
$state = json_decode($json ?? '');
|
||||
|
||||
$this->applyState($state);
|
||||
|
||||
@ -177,7 +176,7 @@ class TestSessionEnvironment
|
||||
|
||||
// Convert to JSON and back so we can share the appleState() code between this and ->loadFromFile()
|
||||
$json = json_encode($state, JSON_FORCE_OBJECT);
|
||||
$state = json_decode($json);
|
||||
$state = json_decode($json ?? '');
|
||||
|
||||
$this->applyState($state);
|
||||
|
||||
@ -192,7 +191,7 @@ class TestSessionEnvironment
|
||||
{
|
||||
// Ensure files backed up to assets dir
|
||||
$backupFolder = $this->getAssetsBackupfolder();
|
||||
if (!is_dir($backupFolder)) {
|
||||
if (!is_dir($backupFolder ?? '')) {
|
||||
Filesystem::makeFolder($backupFolder);
|
||||
}
|
||||
$this->moveRecursive(ASSETS_PATH, $backupFolder, ['.htaccess', 'web.config', '.protected']);
|
||||
@ -206,7 +205,7 @@ class TestSessionEnvironment
|
||||
{
|
||||
// Ensure files backed up to assets dir
|
||||
$backupFolder = $this->getAssetsBackupfolder();
|
||||
if (is_dir($backupFolder)) {
|
||||
if (is_dir($backupFolder ?? '')) {
|
||||
// Move all files
|
||||
Filesystem::makeFolder(ASSETS_PATH);
|
||||
$this->moveRecursive($backupFolder, ASSETS_PATH);
|
||||
@ -224,12 +223,12 @@ class TestSessionEnvironment
|
||||
protected function moveRecursive($src, $dest, $ignore = [])
|
||||
{
|
||||
// If source is not a directory stop processing
|
||||
if (!is_dir($src)) {
|
||||
if (!is_dir($src ?? '')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If the destination directory does not exist create it
|
||||
if (!is_dir($dest) && !mkdir($dest)) {
|
||||
if (!is_dir($dest ?? '') && !mkdir($dest ?? '')) {
|
||||
// If the destination directory could not be created stop processing
|
||||
return;
|
||||
}
|
||||
@ -238,13 +237,13 @@ class TestSessionEnvironment
|
||||
$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());
|
||||
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)) {
|
||||
if (!in_array($file->getFilename(), $ignore ?? [])) {
|
||||
Filesystem::removeFolder($file->getRealPath());
|
||||
}
|
||||
}
|
||||
@ -305,8 +304,8 @@ class TestSessionEnvironment
|
||||
|
||||
// Set existing one, assumes it already has been created
|
||||
$prefix = Environment::getEnv('SS_DATABASE_PREFIX') ?: 'ss_';
|
||||
$pattern = strtolower(sprintf('#^%stmpdb.*#', preg_quote($prefix, '#')));
|
||||
if (!preg_match($pattern, $dbName)) {
|
||||
$pattern = strtolower(sprintf('#^%stmpdb.*#', preg_quote($prefix ?? '', '#')));
|
||||
if (!preg_match($pattern ?? '', $dbName ?? '')) {
|
||||
throw new InvalidArgumentException("Invalid database name format");
|
||||
}
|
||||
|
||||
@ -324,7 +323,7 @@ class TestSessionEnvironment
|
||||
$mailer = (isset($state->mailer)) ? $state->mailer : null;
|
||||
|
||||
if ($mailer) {
|
||||
if (!class_exists($mailer) || !is_subclass_of($mailer, 'SilverStripe\\Control\\Email\\Mailer')) {
|
||||
if (!class_exists($mailer ?? '') || !is_subclass_of($mailer, 'SilverStripe\\Control\\Email\\Mailer')) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Class "%s" is not a valid class, or subclass of Mailer',
|
||||
$mailer
|
||||
@ -358,13 +357,13 @@ class TestSessionEnvironment
|
||||
*/
|
||||
public function importDatabase($path, $requireDefaultRecords = false)
|
||||
{
|
||||
$sql = file_get_contents($path);
|
||||
$sql = file_get_contents($path ?? '');
|
||||
|
||||
// Split into individual query commands, removing comments
|
||||
$sqlCmds = array_filter(preg_split(
|
||||
'/;\n/',
|
||||
preg_replace(array('/^$\n/m', '/^(\/|#).*$\n/m'), '', $sql)
|
||||
));
|
||||
preg_replace(array('/^$\n/m', '/^(\/|#).*$\n/m'), '', $sql ?? '') ?? ''
|
||||
) ?? []);
|
||||
|
||||
// Execute each query
|
||||
foreach ($sqlCmds as $sqlCmd) {
|
||||
@ -401,7 +400,7 @@ class TestSessionEnvironment
|
||||
$content = json_encode($state);
|
||||
}
|
||||
$old = umask(0);
|
||||
file_put_contents($this->getFilePath(), $content, LOCK_EX);
|
||||
file_put_contents($this->getFilePath() ?? '', $content, LOCK_EX);
|
||||
umask($old);
|
||||
}
|
||||
|
||||
@ -409,8 +408,8 @@ class TestSessionEnvironment
|
||||
{
|
||||
if ($this->isRunningTests()) {
|
||||
try {
|
||||
$contents = file_get_contents($this->getFilePath());
|
||||
$json = json_decode($contents);
|
||||
$contents = file_get_contents($this->getFilePath() ?? '');
|
||||
$json = json_decode($contents ?? '');
|
||||
|
||||
$this->applyState($json);
|
||||
} catch (Exception $e) {
|
||||
@ -428,8 +427,8 @@ class TestSessionEnvironment
|
||||
{
|
||||
$file = $this->getFilePath();
|
||||
|
||||
if (file_exists($file)) {
|
||||
if (!unlink($file)) {
|
||||
if (file_exists($file ?? '')) {
|
||||
if (!unlink($file ?? '')) {
|
||||
throw new \Exception('Unable to remove the testsession state file, please remove it manually. File '
|
||||
. 'path: ' . $file);
|
||||
}
|
||||
@ -484,14 +483,14 @@ class TestSessionEnvironment
|
||||
public function loadFixtureIntoDb($fixtureFile)
|
||||
{
|
||||
$realFile = realpath(BASE_PATH . '/' . $fixtureFile);
|
||||
$baseDir = realpath(Director::baseFolder());
|
||||
if (!$realFile || !file_exists($realFile)) {
|
||||
$baseDir = realpath(Director::baseFolder() ?? '');
|
||||
if (!$realFile || !file_exists($realFile ?? '')) {
|
||||
throw new LogicException("Fixture file doesn't exist");
|
||||
} elseif (substr($realFile, 0, strlen($baseDir)) != $baseDir) {
|
||||
} elseif (substr($realFile ?? '', 0, strlen($baseDir ?? '')) != $baseDir) {
|
||||
throw new LogicException("Fixture file must be inside $baseDir");
|
||||
} elseif (substr($realFile, -4) != '.yml') {
|
||||
} elseif (substr($realFile ?? '', -4) != '.yml') {
|
||||
throw new LogicException("Fixture file must be a .yml file");
|
||||
} elseif (!preg_match('/^([^\/.][^\/]+)\/tests\//', $fixtureFile)) {
|
||||
} elseif (!preg_match('/^([^\/.][^\/]+)\/tests\//', $fixtureFile ?? '')) {
|
||||
throw new LogicException("Fixture file must be inside the tests subfolder of one of your modules.");
|
||||
}
|
||||
|
||||
@ -530,7 +529,10 @@ class TestSessionEnvironment
|
||||
public function getState()
|
||||
{
|
||||
$path = Director::getAbsFile($this->getFilePath());
|
||||
return (file_exists($path)) ? json_decode(file_get_contents($path)) : new stdClass;
|
||||
if (file_exists($path ?? '')) {
|
||||
return json_decode(file_get_contents($path)) ?: new stdClass;
|
||||
}
|
||||
return new stdClass;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -548,7 +550,8 @@ class TestSessionEnvironment
|
||||
*
|
||||
* @param mixed $state
|
||||
*/
|
||||
public function connectToDatabase($state = null) {
|
||||
public function connectToDatabase($state = null)
|
||||
{
|
||||
if ($state == null) {
|
||||
$state = $this->getState();
|
||||
}
|
||||
|
@ -69,7 +69,7 @@ class TestSessionHTTPMiddleware implements HTTPMiddleware
|
||||
$mailer = $testState->mailer;
|
||||
Injector::inst()->registerService(new $mailer(), Mailer::class);
|
||||
Email::config()->set("send_all_emails_to", null);
|
||||
Email::config()->update('admin_email', 'no-reply@example.com');
|
||||
Email::config()->set('admin_email', 'no-reply@example.com');
|
||||
}
|
||||
|
||||
// Connect to the test session database
|
||||
@ -82,7 +82,7 @@ class TestSessionHTTPMiddleware implements HTTPMiddleware
|
||||
// 'testsession.stubfile' state parameter.
|
||||
if (isset($testState->stubfile)) {
|
||||
$file = $testState->stubfile;
|
||||
if (!Director::isLive() && $file && file_exists($file)) {
|
||||
if (!Director::isLive() && $file && file_exists($file ?? '')) {
|
||||
include_once($file);
|
||||
}
|
||||
}
|
||||
|
@ -39,8 +39,8 @@ class TestSessionStubCodeWriter
|
||||
$header = '';
|
||||
|
||||
// Create file incl. header if it doesn't exist
|
||||
if (!file_exists($this->getFilePath())) {
|
||||
touch($this->getFilePath());
|
||||
if (!file_exists($this->getFilePath() ?? '')) {
|
||||
touch($this->getFilePath() ?? '');
|
||||
if ($this->debug) {
|
||||
$header .= "<?php\n// Generated by " . $trace[1]['class'] . " on " . date('Y-m-d H:i:s') . "\n\n";
|
||||
} else {
|
||||
@ -52,13 +52,13 @@ class TestSessionStubCodeWriter
|
||||
if ($this->debug) {
|
||||
$header .= "// Added by " . $trace[1]['class'] . '::' . $trace[1]['function'] . "\n";
|
||||
}
|
||||
file_put_contents($path, $header . $php . "\n", FILE_APPEND);
|
||||
file_put_contents($path ?? '', $header . $php . "\n", FILE_APPEND);
|
||||
}
|
||||
|
||||
public function reset()
|
||||
{
|
||||
if (file_exists($this->getFilePath())) {
|
||||
unlink($this->getFilePath());
|
||||
if (file_exists($this->getFilePath() ?? '')) {
|
||||
unlink($this->getFilePath() ?? '');
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user