From 34617418f507b8429aecffbec9245cb3bd3bc4b1 Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Wed, 12 Aug 2015 12:14:51 +0300 Subject: [PATCH] Bug 13906: (followup) Add SystemPreferenceFactory (TestObjectFactory) --- t/lib/TestObjects/ObjectFactory.pm | 5 ++ t/lib/TestObjects/SystemPreferenceFactory.pm | 128 +++++++++++++++++++++++++++ t/lib/TestObjects/objectFactories.t | 38 ++++++++ 3 files changed, 171 insertions(+) create mode 100644 t/lib/TestObjects/SystemPreferenceFactory.pm diff --git a/t/lib/TestObjects/ObjectFactory.pm b/t/lib/TestObjects/ObjectFactory.pm index 1650acf..32873f6 100644 --- a/t/lib/TestObjects/ObjectFactory.pm +++ b/t/lib/TestObjects/ObjectFactory.pm @@ -107,6 +107,11 @@ sub tearDownTestContext { t::lib::TestObjects::LetterTemplateFactory->deleteTestGroup($stash->{letterTemplates}); delete $stash->{letterTemplates}; } + if ($stash->{systempreferences}) { + require t::lib::TestObjects::SystemPreferenceFactory; + t::lib::TestObjects::SystemPreferenceFactory->deleteTestGroup($stash->{systempreferences}); + delete $stash->{systempreferences}; + } } sub getHashKey { diff --git a/t/lib/TestObjects/SystemPreferenceFactory.pm b/t/lib/TestObjects/SystemPreferenceFactory.pm new file mode 100644 index 0000000..4385599 --- /dev/null +++ b/t/lib/TestObjects/SystemPreferenceFactory.pm @@ -0,0 +1,128 @@ +package t::lib::TestObjects::SystemPreferenceFactory; + +# Copyright Vaara-kirjastot 2015 +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# + +use Modern::Perl; +use Carp; +use Scalar::Util qw(blessed); + +use C4::Members; +use Koha::Borrowers; + +use base qw(t::lib::TestObjects::ObjectFactory); + +=head createTestGroup( $data [, $hashKey, $testContexts...] ) +@OVERLOADED + + my $preferences = t::lib::TestObjects::SystemPreferenceFactory->createTestGroup([ + {preference => 'ValidateEmailAddress', + value => 1, + }, + {preference => 'ValidatePhoneNumber', + value => 'OFF', + }, + ], undef, $testContext1, $testContext2, $testContext3); + + #Do test stuff... + + t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext1); + t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext2); + t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext3); + +See t::lib::TestObjects::ObjectFactory for more documentation +=cut + +sub createTestGroup { + my ($class, $objects, $hashKey, $featureStash, $scenarioStash, $stepStash) = @_; + $class->_validateStashes($featureStash, $scenarioStash, $stepStash); + + my %objects; + foreach my $pref (@$objects) { + if (not defined C4::Context->preference($pref->{preference})) { + carp "preference " . $pref->{preference} . " not found"; + next; + } + + + # Take current values + my $preference = $pref->{preference}; + my $old_preference_value = C4::Context->preference($pref->{preference}); + + # Set new values + C4::Context->set_preference($pref->{preference}, $pref->{value}); + + # Check if preference is not yet stored + next if exists $featureStash->{systempreferences}->{$preference}; + next if exists $scenarioStash->{systempreferences}->{$preference}; + next if exists $stepStash->{systempreferences}->{$preference}; + + # Set old value to pref + $pref->{value} = $old_preference_value; + my $key = $class->getHashKey($pref, $preference, $hashKey); + $objects{$key} = $pref; + } + + $class->_persistToStashes(\%objects, 'systempreferences', $featureStash, $scenarioStash, $stepStash); + + return \%objects; +} + +=head validateAndPopulateDefaultValues +@OVERLOAD + +Validates given Object parameters and makes sure that critical fields are given +and populates defaults for missing values. +=cut + +sub validateAndPopulateDefaultValues { + my ($class, $preference, $hashKey) = @_; + $class->SUPER::validateAndPopulateDefaultValues($preference, $hashKey); + + $preference->{value} = C4::Context->preference($preference->{preference}) unless $preference->{value}; + print "populateDefaultVals: ".$preference->{value}."\n"; +} + +=head deleteTestGroup +@OVERLOADED + + my $records = createTestGroup(); + ##Do funky stuff + deleteTestGroup($prefs); + +Removes the given test group from the DB. + +=cut + +sub deleteTestGroup { + my ($class, $preferences) = @_; + + while( my ($key, $pref) = each %$preferences) { + C4::Context->set_preference($pref->{preference}, $pref->{value}); + } +} +sub _deleteTestGroupFromIdentifiers { + my ($class, $testGroupIdentifiers) = @_; + + my $schema = Koha::Database->new_schema(); + foreach my $key (@$testGroupIdentifiers) { + $schema->resultset('Borrower')->find({"cardnumber" => $key})->delete(); + } +} + +1; diff --git a/t/lib/TestObjects/objectFactories.t b/t/lib/TestObjects/objectFactories.t index b6c6885..5cf3821 100644 --- a/t/lib/TestObjects/objectFactories.t +++ b/t/lib/TestObjects/objectFactories.t @@ -36,6 +36,8 @@ use t::lib::TestObjects::CheckoutFactory; use Koha::Checkouts; use t::lib::TestObjects::LetterTemplateFactory; use Koha::LetterTemplates; +use t::lib::TestObjects::SystemPreferenceFactory; +use C4::Context; my $testContext = {}; #Gather all created Objects here so we can finally remove them all. @@ -246,7 +248,43 @@ subtest 't::lib::TestObjects::LetterTemplateFactory' => sub { ok(not(defined($letterTemplate)), "LetterTemplate 'circulation-ODUE1-CPL-print' deleted"); }; +########## SystemPreferenceFactory subtests ########## +subtest 't::lib::TestObjects::SystemPreferenceFactory' => sub { + my $subtestContext = {}; + ##Create and Delete using dependencies in the $testContext instantiated in previous subtests. + + # take syspref 'opacuserlogin' and save its current value + my $current_pref_value = C4::Context->preference("opacuserlogin"); + + is(C4::Context->preference("opacuserlogin"), C4::Context->preference("opacuserlogin"), "System Preference opacuserlogin original value '".((C4::Context->preference("opacuserlogin")) ? C4::Context->preference("opacuserlogin"):0)."'"); + + # reverse the value for testing + my $pref_new_value = !$current_pref_value; + + + my $objects = t::lib::TestObjects::SystemPreferenceFactory->createTestGroup([ + {preference => 'opacuserlogin', + value => $pref_new_value # set the reversed value + }, + ], undef, $subtestContext, undef, undef); + + my $letterTemplate = + is(C4::Context->preference("opacuserlogin"), $pref_new_value, "System Preference opacuserlogin reversed to '".(($pref_new_value) ? $pref_new_value:0)."'"); + # let's change it again to test that only the original preference value is saved + $objects = t::lib::TestObjects::SystemPreferenceFactory->createTestGroup([ + {preference => 'opacuserlogin', + value => 2 # set the reversed value + }, + ], undef, $subtestContext, undef, undef); + + is(C4::Context->preference("opacuserlogin"), 2, "System Preference opacuserlogin set to '2'"); + + #Delete them + t::lib::TestObjects::SystemPreferenceFactory->deleteTestGroup($subtestContext->{systempreferences}); + is(C4::Context->preference("opacuserlogin"), $current_pref_value, "System Preference opacuserlogin restored to '".(($current_pref_value) ? $current_pref_value:0)."' after test group deletion"); + +}; ########## Global test context subtests ########## subtest 't::lib::TestObjects::ObjectFactory clearing global test context' => sub { -- 1.9.1