From 04481122cebee65f707fdbbdcb306671f18fd38e Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 6 May 2014 12:01:29 +0200 Subject: [PATCH] Improved test environment sandboxing --- data/config.ini | 3 -- lib/chibi-sql | 2 +- src/core.php | 33 ++++++++---- tests/.gitignore | 3 ++ tests/config.ini | 130 ++++++++++++++++++++++++++++++++++++++++++++++ tests/run-all.php | 40 ++++++-------- 6 files changed, 172 insertions(+), 39 deletions(-) create mode 100644 tests/config.ini diff --git a/data/config.ini b/data/config.ini index 7b786abb..660cdbf9 100644 --- a/data/config.ini +++ b/data/config.ini @@ -1,6 +1,3 @@ -[chibi] -enableCache=1 - [main] dbDriver = "sqlite" dbLocation = "./data/db.sqlite" diff --git a/lib/chibi-sql b/lib/chibi-sql index cf0b4bd2..a7e3dddb 160000 --- a/lib/chibi-sql +++ b/lib/chibi-sql @@ -1 +1 @@ -Subproject commit cf0b4bd2253d0d3240a87ce31f77ee587b306638 +Subproject commit a7e3dddbaeb6ba5b711ae3c916307a0896aee758 diff --git a/src/core.php b/src/core.php index d5cb9f61..558a052a 100644 --- a/src/core.php +++ b/src/core.php @@ -31,30 +31,40 @@ function getContext() return $context; } -function resetEnvironment() +function prepareConfig($testEnvironment) { //load config manually global $config; global $rootDir; - global $startTime; - $configPaths = - [ - $rootDir . DS . 'data' . DS . 'config.ini', - $rootDir . DS . 'data' . DS . 'local.ini', - $rootDir . DS . 'tests' . DS . 'test.ini', - ]; + $configPaths = []; + if (!$testEnvironment) + { + $configPaths []= $rootDir . DS . 'data' . DS . 'config.ini'; + $configPaths []= $rootDir . DS . 'data' . DS . 'local.ini'; + } + else + { + $configPaths []= $rootDir . DS . 'tests' . DS . 'config.ini'; + } + $config = new \Chibi\Config(); foreach ($configPaths as $path) if (file_exists($path)) $config->loadIni($path); $config->rootDir = $rootDir; +} +function prepareEnvironment($testEnvironment) +{ //prepare context global $context; + global $startTime; $context = new StdClass; $context->startTime = $startTime; + $config = getConfig(); + //extension sanity checks $requiredExtensions = ['pdo', 'pdo_' . $config->main->dbDriver, 'gd', 'openssl', 'fileinfo']; foreach ($requiredExtensions as $ext) @@ -71,8 +81,9 @@ function resetEnvironment() \Chibi\Database::connect( $config->main->dbDriver, TextHelper::absolutePath($config->main->dbLocation), - $config->main->dbUser, - $config->main->dbPass); + isset($config->main->dbUser) ? $config->main->dbUser : null, + isset($config->main->dbPass) ? $config->main->dbPass : null); } -resetEnvironment(); +prepareConfig(false); +prepareEnvironment(false); diff --git a/tests/.gitignore b/tests/.gitignore index 2fa69c24..d818063e 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -1 +1,4 @@ db.sqlite +files +thumbs +logs diff --git a/tests/config.ini b/tests/config.ini new file mode 100644 index 00000000..22f79736 --- /dev/null +++ b/tests/config.ini @@ -0,0 +1,130 @@ +[main] +filesPath = "./tests/files/" +thumbsPath = "./tests/thumbs/" +logsPath = "./tests/logs/{yyyy-mm}.log" +mediaPath = "./public_html/media/" +title = "szurubooru/tests" +salt = "salt..." + +[misc] +featuredPostMaxDays=7 +debugQueries=0 +logAnonymousUploads=1 + +[help] +title=Help +subTitles[help]=General help +subTitles[rules]=Rules +subTitles[privacy]=Privacy policy +paths[help]=./data/help.md +paths[rules]=./data/rules.md +paths[privacy]=./data/privacy.md + +[browsing] +usersPerPage=8 +postsPerPage=20 +logsPerPage=250 +tagsPerPage=100 +tagsRelated=15 +thumbWidth=150 +thumbHeight=150 +thumbStyle=outside +endlessScrollingDefault=1 +showPostTagTitlesDefault=0 +showDislikedPostsDefault=1 +maxSearchTokens=4 +maxRelatedPosts=50 + +[posts] +maxSourceLength = 200 + +[comments] +minLength = 5 +maxLength = 2000 +commentsPerPage = 10 +maxCommentsInList = 5 + +[registration] +staffActivation = 0 +passMinLength = 5 +passRegex = "/^.+$/" +userNameMinLength = 3 +userNameMaxLength = 20 +userNameRegex = "/^[\w_-]+$/ui" + +needEmailForRegistering = 0 +needEmailForCommenting = 0 +needEmailForUploading = 0 +confirmationEmailEnabled = 1 +confirmationEmailSenderName = "{host} mailing system" +confirmationEmailSenderEmail = "noreply@{host}" +confirmationEmailSubject = "{host} - account activation" +confirmationEmailBody = "Hello,{nl}{nl}You received this e-mail because someone registered a user with this e-mail address at {host}. If it's you, visit {link} to finish registration process, otherwise you may ignore and delete this e-mail.{nl}{nl}Kind regards,{nl}{host} mailing system" +passwordResetEmailSenderName = "{host} mailing system" +passwordResetEmailSenderEmail = "noreply@{host}" +passwordResetEmailSubject = "{host} - password reset" +passwordResetEmailBody = "Hello,{nl}{nl}You received this e-mail because someone requested a password reset for user with this e-mail address at {host}. If it's you, visit {link} to finish password reset process, otherwise you may ignore and delete this e-mail.{nl}{nl}Kind regards,{nl}{host} mailing system" + +[privileges] +registerAccount=anonymous +;registerAccount=nobody + +uploadPost=registered +listPosts=anonymous +listPosts.sketchy=registered +listPosts.unsafe=registered +listPosts.hidden=moderator +viewPost=anonymous +viewPost.sketchy=registered +viewPost.unsafe=registered +viewPost.hidden=moderator +retrievePost=anonymous +favoritePost=registered +editPostSafety.own=registered +editPostSafety.all=moderator +editPostTags=registered +editPostThumb=moderator +editPostSource=moderator +editPostRelations.own=registered +editPostRelations.all=moderator +editPostFile=moderator +massTag.own=registered +massTag.all=power-user +hidePost=moderator +deletePost=moderator +featurePost=moderator +scorePost=registered +flagPost=registered + +listUsers=registered +viewUser=registered +viewUserEmail.all=admin +viewUserEmail.own=registered +changeUserPassword.own=registered +changeUserPassword.all=admin +changeUserEmail.own=registered +changeUserEmail.all=admin +changeUserAccessRank=admin +changeUserName=moderator +changeUserSettings.all=nobody +changeUserSettings.own=registered +acceptUserRegistration=moderator +banUser.own=nobody +banUser.all=admin +deleteUser.own=registered +deleteUser.all=nobody +flagUser=registered + +listComments=anonymous +addComment=registered +deleteComment.own=registered +deleteComment.all=moderator +editComment.own=registered +editComment.all=admin + +listTags=anonymous +mergeTags=moderator +renameTags=moderator + +listLogs=moderator +viewLog=moderator diff --git a/tests/run-all.php b/tests/run-all.php index e5628403..1374c136 100644 --- a/tests/run-all.php +++ b/tests/run-all.php @@ -3,8 +3,6 @@ require_once __DIR__ . '/../src/core.php'; require_once __DIR__ . '/../src/upgrade.php'; \Chibi\Autoloader::registerFileSystem(__DIR__); -$configPath = __DIR__ . '/test.ini'; - $options = getopt('cf:', ['clean', 'filter:']); $cleanDatabase = (isset($options['c']) or isset($options['clean'])); @@ -16,36 +14,29 @@ elseif (isset($options['filter'])) else $filter = null; +$dbPath = __DIR__ . '/db.sqlite'; + +if (file_exists($dbPath) and $cleanDatabase) + unlink($dbPath); + try { - $dbPath = __DIR__ . '/db.sqlite'; - - if (file_exists($dbPath) and $cleanDatabase) - unlink($dbPath); - - $configIni = - [ - '[main]', - 'dbDriver = "sqlite"', - 'dbLocation = "' . $dbPath . '"', - 'filesPath = "' . __DIR__ . 'files', - 'thumbsPath = "' . __DIR__ . 'thumbs', - 'logsPath = "/dev/null"', - '[registration]', - 'needEmailForRegistering = 0', - 'needEmailForCommenting = 0', - 'needEmailForUploading = 0' - ]; - - file_put_contents($configPath, implode(PHP_EOL, $configIni)); - + resetEnvironment(); upgradeDatabase(); runAll($filter); } finally { - unlink($configPath); +} + +function resetEnvironment() +{ + global $dbPath; + prepareConfig(true); + getConfig()->main->dbDriver = 'sqlite'; + getConfig()->main->dbLocation = $dbPath; + prepareEnvironment(true); } function getTestMethods($filter) @@ -151,6 +142,7 @@ function runAll($filter) function runSingle($callback) { resetEnvironment(); + resetEnvironment(true); \Chibi\Database::rollback(function() use ($callback) { $callback();