diff --git a/lib/WebGUI/Config.pm b/lib/WebGUI/Config.pm index 4982a4504..03634a7f9 100644 --- a/lib/WebGUI/Config.pm +++ b/lib/WebGUI/Config.pm @@ -17,6 +17,7 @@ package WebGUI::Config; use strict; use Class::InsideOut qw(readonly id register); use Cwd (); +use File::Spec; use base 'Config::JSON'; my %config = (); @@ -169,8 +170,10 @@ sub new { my $class = shift; my $webguiPath = Cwd::realpath(shift); my $filename = shift; - my $noCache = shift; - my $fullPath = Cwd::realpath($webguiPath.'/etc/'.$filename); + my $noCache = shift; + my $fullPath = File::Spec->file_name_is_absolute($filename) + ? $filename + : Cwd::realpath($webguiPath.'/etc/'.$filename); if ($config{$fullPath}) { return $config{$fullPath}; } else { diff --git a/lib/WebGUI/Workflow/Activity/DeleteExpiredSessions.pm b/lib/WebGUI/Workflow/Activity/DeleteExpiredSessions.pm index 418dea6c5..c32f3fcd1 100644 --- a/lib/WebGUI/Workflow/Activity/DeleteExpiredSessions.pm +++ b/lib/WebGUI/Workflow/Activity/DeleteExpiredSessions.pm @@ -87,7 +87,7 @@ sub execute { $self->session->errorHandler->warn("More than 1 old userLoginLog rows found, removing offending rows"); $self->session->db->write("delete from userLoginLog where lastPageViewed = timeStamp and sessionId = ? ", [$sessionId] ); } - my $session = WebGUI::Session->open($self->session->config->getWebguiRoot, $self->session->config->getFilename, undef, undef, $sessionId, 1); + my $session = WebGUI::Session->open($self->session->config->getWebguiRoot, $self->session->config->getFilePath, undef, undef, $sessionId, 1); if (defined $session) { $session->var->end; $session->close; diff --git a/t/AdSpace.t b/t/AdSpace.t index 378f0d2e8..66f7fedfd 100644 --- a/t/AdSpace.t +++ b/t/AdSpace.t @@ -14,7 +14,6 @@ use lib "$FindBin::Bin/lib"; use WebGUI::Test; use WebGUI::Session; use WebGUI::AdSpace::Ad; -use WebGUI::AdSpace; use Test::More; use Test::Deep; @@ -31,9 +30,12 @@ my $newAdSpaceSettings = { my $numTests = 35; # increment this value for each test you create $numTests += 2 * scalar keys %{ $newAdSpaceSettings }; +++$numTests; ##For conditional testing on module load plan tests => $numTests; +my $loaded = use_ok('WebGUI::AdSpace'); + my $session = WebGUI::Test->session; my ($adSpace, $alfred, $alfred2, $bruce, $catWoman, ); my ($jokerAd, $penguinAd, $twoFaceAd); diff --git a/t/Asset/Asset.t b/t/Asset/Asset.t index 0a311ac15..e1f560f00 100644 --- a/t/Asset/Asset.t +++ b/t/Asset/Asset.t @@ -372,6 +372,10 @@ is($importNode->fixUrl('1234'.'_'x235 . 'abcdefghij'), '1234'.'_'x235 . 'abcdefg is($importNode->fixUrl('1234'.'_'x250 . 'abcdefghij'), '1234'.'_'x216, 'fixUrl truncates long URLs over 250 characters to 220 characters'); is $importNode->fixUrl('---'), '-', '... 3 dashes are collapsed down to a single dash'; +WebGUI::Test->originalConfig('extrasURL'); +WebGUI::Test->originalConfig('uploadsURL'); +WebGUI::Test->originalConfig('assets'); + $session->config->set('extrasURL', '/extras'); $session->config->set('uploadsURL', '/uploads'); diff --git a/t/Asset/AssetExportHtml.t b/t/Asset/AssetExportHtml.t index ce08e16c8..1fe9bed6a 100644 --- a/t/Asset/AssetExportHtml.t +++ b/t/Asset/AssetExportHtml.t @@ -17,6 +17,7 @@ use strict; use lib "$FindBin::Bin/../lib"; use Test::More; use WebGUI::Test; # Must use this before any other WebGUI modules +use WebGUI::PseudoRequest; use WebGUI::Session; use WebGUI::Asset; @@ -36,6 +37,8 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests +WebGUI::Test->originalConfig('exportPath'); + my $testRan = 1; plan tests => 126; # Increment this number for each test you create diff --git a/t/Asset/Snippet.t b/t/Asset/Snippet.t index 0a1415ce8..5449c3315 100644 --- a/t/Asset/Snippet.t +++ b/t/Asset/Snippet.t @@ -79,6 +79,7 @@ $snippet->update({ snippet => q|^SQL(select value from settings where name="");| }); +WebGUI::Test->originalConfig('macros'); $session->config->addToHash('macros', 'SQL', 'SQL'); is($snippet->view(), 'WebGUI', 'Interpolating macros in works with template in the correct order'); diff --git a/t/Asset/Template/HTMLTemplateExpr.t b/t/Asset/Template/HTMLTemplateExpr.t index ef61dd777..b8b31da3e 100644 --- a/t/Asset/Template/HTMLTemplateExpr.t +++ b/t/Asset/Template/HTMLTemplateExpr.t @@ -27,6 +27,7 @@ plan tests => 2 + $num_tests; my $session = WebGUI::Test->session; # put your tests here +WebGUI::Test->originalConfig('templateParsers'); my $module = use_ok('HTML::Template::Expr'); my $plugin = use_ok('WebGUI::Asset::Template::HTMLTemplateExpr'); diff --git a/t/Asset/Wobject/Survey/ExpressionEngine.t b/t/Asset/Wobject/Survey/ExpressionEngine.t index a901adf64..0d1edcecd 100644 --- a/t/Asset/Wobject/Survey/ExpressionEngine.t +++ b/t/Asset/Wobject/Survey/ExpressionEngine.t @@ -36,6 +36,7 @@ SKIP: { my $e = "WebGUI::Asset::Wobject::Survey::ExpressionEngine"; + WebGUI::Test->originalConfig('enableSurveyExpressionEngine'); $session->config->set( 'enableSurveyExpressionEngine', 0 ); is( $e->run( $session, 'jump { 1 } target' ), undef, "Nothing happens unless we turn on enableSurveyExpressionEngine in config" ); diff --git a/t/Asset/Wobject/Survey/ResponseJSON.t b/t/Asset/Wobject/Survey/ResponseJSON.t index 1b40ea0df..3b31bccc2 100644 --- a/t/Asset/Wobject/Survey/ResponseJSON.t +++ b/t/Asset/Wobject/Survey/ResponseJSON.t @@ -336,6 +336,7 @@ cmp_deeply($rJSON->responseScores(indexBy => 'variable'), { s1q0 => 100, s1q1 => # #################################################### # Turn on the survey Expression Engine +WebGUI::Test->originalConfig('enableSurveyExpressionEngine'); $session->config->set('enableSurveyExpressionEngine', 1); $rJSON->survey->section([0])->{variable} = 's0'; $rJSON->survey->question([0,0])->{variable} = 's0q0'; # surveyOrder index = 0 diff --git a/t/Asset/Wobject/Survey/Test.t b/t/Asset/Wobject/Survey/Test.t index dfdb1919d..d4eb6d5ee 100644 --- a/t/Asset/Wobject/Survey/Test.t +++ b/t/Asset/Wobject/Survey/Test.t @@ -40,6 +40,7 @@ my $user = WebGUI::User->new( $session, 'new' ); WebGUI::Test->addToCleanup($user); my $import_node = WebGUI::Asset->getImportNode($session); +WebGUI::Test->originalConfig('enableSurveyExpressionEngine'); $session->config->set('enableSurveyExpressionEngine', 1); # Create a Survey diff --git a/t/AssetAspect/RssFeed.t b/t/AssetAspect/RssFeed.t index 0c950b983..43686bfdd 100644 --- a/t/AssetAspect/RssFeed.t +++ b/t/AssetAspect/RssFeed.t @@ -31,6 +31,8 @@ use WebGUI::Asset::RssAspectDummy; # Init my $session = WebGUI::Test->session; +WebGUI::Test->originalConfig('exportPath'); + #---------------------------------------------------------------------------- # Tests diff --git a/t/Cache/CHI.t b/t/Cache/CHI.t index 8f5a87fce..1299df88c 100644 --- a/t/Cache/CHI.t +++ b/t/Cache/CHI.t @@ -34,6 +34,8 @@ plan tests => 3; # Increment this number for each test you create # put your tests here use_ok( 'WebGUI::Cache::CHI' ); +WebGUI::Test->originalConfig('cacheType'); +WebGUI::Test->originalConfig('cache'); $session->config->set('cacheType', 'WebGUI::Cache::CHI'); $session->config->set('cache', { driver => 'FastMmap', }); diff --git a/t/Crud/serialize.t b/t/Crud/serialize.t index cd758f2fe..36e2b1121 100644 --- a/t/Crud/serialize.t +++ b/t/Crud/serialize.t @@ -36,7 +36,7 @@ use_ok('WebGUI::Serialize'); WebGUI::Serialize->crud_createTable($session); WebGUI::Test->addToCleanup(sub { WebGUI::Serialize->crud_dropTable($session); -}) +}); my $cereal = WebGUI::Serialize->create($session); isa_ok($cereal, 'WebGUI::Serialize'); diff --git a/t/Group/resetGroupFields.t b/t/Group/resetGroupFields.t index ff5ef5fdc..86227a08d 100644 --- a/t/Group/resetGroupFields.t +++ b/t/Group/resetGroupFields.t @@ -91,6 +91,7 @@ my $workflow = WebGUI::Workflow->create($session, ); WebGUI::Test->addToCleanup($workflow); +WebGUI::Test->originalConfig('workflowActivities'); $session->config->addToArray('workflowActivities/User', 'WebGUI::Workflow::Activity::AddUserToGroup'); my $userActivity = $workflow->addActivity('WebGUI::Workflow::Activity::AddUserToGroup'); diff --git a/t/Macro.t b/t/Macro.t index 230d083c5..d0aecdec2 100644 --- a/t/Macro.t +++ b/t/Macro.t @@ -34,6 +34,7 @@ $registeredUser->username('TimBob'); WebGUI::Test->addToCleanup($registeredUser); $session->user({user => $registeredUser}); +WebGUI::Test->originalConfig('macros'); ##Overwrite any local configuration so that we know how to call it. foreach my $macro (qw/ GroupText LoginToggle PageTitle MacroStart MacroEnd MacroNest diff --git a/t/Operation/User/service.t b/t/Operation/User/service.t index 632e72931..23494198c 100644 --- a/t/Operation/User/service.t +++ b/t/Operation/User/service.t @@ -29,6 +29,7 @@ use Data::Dumper; #---------------------------------------------------------------------------- # Init my $session = WebGUI::Test->session; +WebGUI::Test->originalConfig( "serviceSubnets" ); $session->config->delete('serviceSubnets'); my ( $response, $responseObj, $auth, $userAndy, $userRed ); diff --git a/t/Session/Url.t b/t/Session/Url.t index 0bd50342d..8c5d1d838 100644 --- a/t/Session/Url.t +++ b/t/Session/Url.t @@ -85,6 +85,7 @@ is( $url2, $url.'?a=b;c=d', 'append second pair'); # ####################################### +WebGUI::Test->originalConfig('gateway'); $session->config->set('gateway', '/'); is( $session->config->get('gateway'), '/', 'Set gateway for downstream tests'); @@ -130,6 +131,7 @@ my $setting_hostToUse = $session->setting->get('hostToUse'); $session->setting->set('hostToUse', 'HTTP_HOST'); my $sitename = $session->config->get('sitename')->[0]; is( $session->url->getSiteURL, 'http://'.$sitename, 'getSiteURL from config as http_host'); +WebGUI::Test->originalConfig('webServerPort'); $session->url->setSiteURL('http://webgui.org'); is( $session->url->getSiteURL, 'http://webgui.org', 'override config setting with setSiteURL'); @@ -149,6 +151,7 @@ $mockEnv{HTTP_HOST} = "devsite.com"; $session->url->setSiteURL(undef); is( $session->url->getSiteURL, 'http://'.$sitename, 'getSiteURL where requested host is not a configured site'); +WebGUI::Test->originalConfig('sitename'); $session->config->addToArray('sitename', 'devsite.com'); $session->url->setSiteURL(undef); is( $session->url->getSiteURL, 'http://devsite.com', 'getSiteURL where requested host is not the first configured site'); @@ -268,8 +271,9 @@ is($session->url->makeAbsolute('page1'), '/page1', 'makeAbsolute: default baseUr # ####################################### -my $extras = $session->config->get('extrasURL'); +my $extras = WebGUI::Test->originalConfig('extrasURL'); +WebGUI::Test->originalConfig('cdn'); $session->config->delete('cdn'); is($session->url->extras, $extras.'/', 'extras method returns URL to extras with a trailing slash'); @@ -426,6 +430,8 @@ is( # ####################################### +WebGUI::Test->originalConfig('sslEnabled'); + ##Test all the false cases, first $session->config->set('sslEnabled', 0); diff --git a/t/Storage.t b/t/Storage.t index 95a341fd2..e170ddf23 100644 --- a/t/Storage.t +++ b/t/Storage.t @@ -91,6 +91,7 @@ is($guidStorage->getDirectoryId, $newGuid, '... getDirectoryId'); # #################################################### +WebGUI::Test->originalConfig('cdn'); $session->config->delete('cdn'); # Note: the CDN configuration will be reverted after CDN tests below diff --git a/t/User.t b/t/User.t index efe6a063e..e7aa39d19 100644 --- a/t/User.t +++ b/t/User.t @@ -483,6 +483,7 @@ $dude->deleteFromGroups([12]); my $origEnvHash = $session->env->{_env}; my %newEnv = ( REMOTE_ADDR => '194.168.0.2' ); $session->env->{_env} = \%newEnv; +WebGUI::Test->originalConfig('adminModeSubnets'); $session->config->set('adminModeSubnets', ['194.168.0.0/24']); ok(!$dude->isInGroup(12), 'user is not in group 12'); @@ -834,6 +835,7 @@ isa_ok( $newCreateUser, 'WebGUI::User', 'create() returns a WebGUI::User' ); ################################################################ $session->setting->set('preventProxyCache', 0); +WebGUI::Test->originalConfig('profileModuleIdentifier'); my $profileModuleId = $session->config->get('profileModuleIdentifier'); is( $newFish->getProfileUrl('cellblock'), diff --git a/t/Workflow/Activity/DeleteExpiredSessions.t b/t/Workflow/Activity/DeleteExpiredSessions.t index 6a364d910..6f0206128 100644 --- a/t/Workflow/Activity/DeleteExpiredSessions.t +++ b/t/Workflow/Activity/DeleteExpiredSessions.t @@ -53,9 +53,6 @@ my $origSessionTimeout = $session->setting->get('sessionTimeout'); my $sessionCount = $session->db->quickScalar('select count(*) from userSession'); my $scratchCount = $session->db->quickScalar('select count(*) from userSessionScratch'); -note $sessionCount; -note $scratchCount; - my @sessions; foreach (1..2) { diff --git a/t/i18n/template.t b/t/i18n/template.t index c2c763e4d..cc5c365c4 100644 --- a/t/i18n/template.t +++ b/t/i18n/template.t @@ -31,6 +31,7 @@ my $session = WebGUI::Test->session; my $lib = WebGUI::Test->lib; ## Remove all macros but International, and set them to call WebGUI::Macro::Callback +WebGUI::Test->originalConfig('macros'); my $originalMacros = $session->config->get('macros'); my $configFileMacros = {}; for my $macro (keys %$originalMacros) { diff --git a/t/lib/WebGUI/Test.pm b/t/lib/WebGUI/Test.pm index 3d0589aef..55f55d3fa 100644 --- a/t/lib/WebGUI/Test.pm +++ b/t/lib/WebGUI/Test.pm @@ -32,22 +32,22 @@ use Log::Log4perl; # load early to ensure proper order of END blocks use Clone qw(clone); use File::Basename qw(dirname fileparse); use File::Spec::Functions qw(abs2rel rel2abs catdir catfile updir); +use IO::Handle (); use IO::Select (); +use Cwd (); use Scalar::Util qw( blessed ); use List::MoreUtils qw( any ); use Carp qw( carp croak ); use JSON qw( from_json to_json ); -use File::Copy (); -use File::Temp (); use Scope::Guard; -our ($WEBGUI_TEST_ROOT, $WEBGUI_TEST_COLLATERAL, $WEBGUI_ROOT, $WEBGUI_LIB); - BEGIN { -our $WEBGUI_TEST_ROOT = rel2abs( catdir( dirname( __FILE__ ), (updir) x 2 ) ); - -our $WEBGUI_TEST_COLLATERAL = catdir( +our $WEBGUI_TEST_ROOT = File::Spec->catdir( + File::Spec->catpath((File::Spec->splitpath(__FILE__))[0,1], ''), + (File::Spec->updir) x 2 +); +our $WEBGUI_TEST_COLLATERAL = File::Spec->catdir( $WEBGUI_TEST_ROOT, 'supporting_collateral' ); @@ -86,30 +86,21 @@ our @EXPORT_OK = qw(session config collateral); my $CLASS = __PACKAGE__; -my $original_config_file; sub import { - if ( ! $original_config_file ) { - my $config = $ENV{WEBGUI_CONFIG}; - die "Enviroment variable WEBGUI_CONFIG must be set to the full path to a WebGUI config file.\n" - unless $config; + our $CONFIG_FILE = $ENV{ WEBGUI_CONFIG }; - for my $tryPath ( - rel2abs( $config ), - rel2abs( $config, $CLASS->root, 'etc' ) - ) { - if ( -e $tryPath ) { - $config = $tryPath; - } - } + die "Enviroment variable WEBGUI_CONFIG must be set to the full path to a WebGUI config file.\n" + unless $CONFIG_FILE; + die "WEBGUI_CONFIG path '$CONFIG_FILE' does not exist.\n" + unless -e $CONFIG_FILE; + die "WEBGUI_CONFIG path '$CONFIG_FILE' is not a file.\n" + unless -f _; + die "WEBGUI_CONFIG path '$CONFIG_FILE' is not readable by effective uid '$>'.\n" + unless -r _; + + my $etcDir = Cwd::realpath(File::Spec->catdir($CLASS->root, 'etc')); + $CONFIG_FILE = File::Spec->abs2rel($CONFIG_FILE, $etcDir); - die "WEBGUI_CONFIG path '$config' does not exist.\n" - unless -e $config; - die "WEBGUI_CONFIG path '$config' is not a file.\n" - unless -f _; - die "WEBGUI_CONFIG path '$config' is not readable by effective uid '$>'.\n" - unless -r _; - $original_config_file = abs2rel( $config, catdir( $CLASS->root, 'etc') ); - } goto &{ $_[0]->can('SUPER::import') }; } @@ -370,26 +361,8 @@ Returns the name of the WebGUI config file used for this test. =cut -my $config_copy; sub file { - return $config_copy - if $config_copy; - my $config_base = fileparse( $original_config_file, '.conf' ); - my (undef, $config_copy_abs) = File::Temp::tempfile( - "$config_base-XXXX", - SUFFIX => '.conf', - UNLINK => 0, - OPEN => 0, - TMPDIR => 1, - ); - File::Copy::copy($original_config_file, $config_copy_abs); - $CLASS->addToCleanup(sub { - unlink $config_copy_abs; - undef $config_copy; - undef $config; - }); - $config_copy = abs2rel( $config_copy_abs, catdir( $WEBGUI_ROOT, 'etc') ); - return $config_copy; + return our $CONFIG_FILE; } #---------------------------------------------------------------------------- @@ -478,7 +451,7 @@ Optionally adds a filename to the end. sub getTestCollateralPath { my $class = shift; my @path = @_; - return catfile(our $WEBGUI_TEST_COLLATERAL, @path); + return File::Spec->catfile(our $WEBGUI_TEST_COLLATERAL, @path); } sub collateral { @@ -596,6 +569,7 @@ sub prepareMailServer { $CLASS->session->setting->set( 'smtpServer', $SMTP_HOST . ':' . $SMTP_PORT ); + $CLASS->originalConfig('emailToLog'); $CLASS->session->config->set( 'emailToLog', 0 ); # Let it start up yo @@ -618,6 +592,38 @@ sub prepareMailServer { #---------------------------------------------------------------------------- +=head2 originalConfig ( $param ) + +Stores the original data from the config file, to be restored +automatically at the end of the test. This is a class method. + +=cut + +my %originalConfig; +sub originalConfig { + my ($class, $param) = @_; + my $safeValue = my $value = $CLASS->session->config->get($param); + if (ref $value) { + $safeValue = clone $value; + } + # add cleanup handler if this is the first time we were run + if (! keys %originalConfig) { + $class->addToCleanup(sub { + while (my ($key, $value) = each %originalConfig) { + if (defined $value) { + $CLASS->session->config->set($key, $value); + } + else { + $CLASS->session->config->delete($key); + } + } + }); + } + $originalConfig{$param} = $safeValue; +} + +#---------------------------------------------------------------------------- + =head2 getMail ( ) Read a sent mail from the prepared mail server (L)