|
Line 0
Link Here
|
|
|
1 |
package t::lib::TestObjects::SystemPreferenceFactory; |
| 2 |
|
| 3 |
# Copyright Vaara-kirjastot 2015 |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it under the |
| 8 |
# terms of the GNU General Public License as published by the Free Software |
| 9 |
# Foundation; either version 3 of the License, or (at your option) any later |
| 10 |
# version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License along |
| 17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
| 18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
# |
| 20 |
|
| 21 |
use Modern::Perl; |
| 22 |
use Carp; |
| 23 |
use Scalar::Util qw(blessed); |
| 24 |
|
| 25 |
use C4::Members; |
| 26 |
use Koha::Borrowers; |
| 27 |
|
| 28 |
use base qw(t::lib::TestObjects::ObjectFactory); |
| 29 |
|
| 30 |
=head createTestGroup( $data [, $hashKey, $testContexts...] ) |
| 31 |
@OVERLOADED |
| 32 |
|
| 33 |
my $preferences = t::lib::TestObjects::SystemPreferenceFactory->createTestGroup([ |
| 34 |
{preference => 'ValidateEmailAddress', |
| 35 |
value => 1, |
| 36 |
}, |
| 37 |
{preference => 'ValidatePhoneNumber', |
| 38 |
value => 'OFF', |
| 39 |
}, |
| 40 |
], undef, $testContext1, $testContext2, $testContext3); |
| 41 |
|
| 42 |
#Do test stuff... |
| 43 |
|
| 44 |
t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext1); |
| 45 |
t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext2); |
| 46 |
t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext3); |
| 47 |
|
| 48 |
See t::lib::TestObjects::ObjectFactory for more documentation |
| 49 |
=cut |
| 50 |
|
| 51 |
sub createTestGroup { |
| 52 |
my ($class, $objects, $hashKey, $featureStash, $scenarioStash, $stepStash) = @_; |
| 53 |
$class->_validateStashes($featureStash, $scenarioStash, $stepStash); |
| 54 |
|
| 55 |
my %objects; |
| 56 |
foreach my $pref (@$objects) { |
| 57 |
if (not defined C4::Context->preference($pref->{preference})) { |
| 58 |
carp "preference " . $pref->{preference} . " not found"; |
| 59 |
next; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
# Take current values |
| 64 |
my $preference = $pref->{preference}; |
| 65 |
my $old_preference_value = C4::Context->preference($pref->{preference}); |
| 66 |
|
| 67 |
# Set new values |
| 68 |
C4::Context->set_preference($pref->{preference}, $pref->{value}); |
| 69 |
|
| 70 |
# Check if preference is not yet stored |
| 71 |
next if exists $featureStash->{systempreferences}->{$preference}; |
| 72 |
next if exists $scenarioStash->{systempreferences}->{$preference}; |
| 73 |
next if exists $stepStash->{systempreferences}->{$preference}; |
| 74 |
|
| 75 |
# Set old value to pref |
| 76 |
$pref->{value} = $old_preference_value; |
| 77 |
my $key = $class->getHashKey($pref, $preference, $hashKey); |
| 78 |
$objects{$key} = $pref; |
| 79 |
} |
| 80 |
|
| 81 |
$class->_persistToStashes(\%objects, 'systempreferences', $featureStash, $scenarioStash, $stepStash); |
| 82 |
|
| 83 |
return \%objects; |
| 84 |
} |
| 85 |
|
| 86 |
=head validateAndPopulateDefaultValues |
| 87 |
@OVERLOAD |
| 88 |
|
| 89 |
Validates given Object parameters and makes sure that critical fields are given |
| 90 |
and populates defaults for missing values. |
| 91 |
=cut |
| 92 |
|
| 93 |
sub validateAndPopulateDefaultValues { |
| 94 |
my ($class, $preference, $hashKey) = @_; |
| 95 |
$class->SUPER::validateAndPopulateDefaultValues($preference, $hashKey); |
| 96 |
|
| 97 |
$preference->{value} = C4::Context->preference($preference->{preference}) unless $preference->{value}; |
| 98 |
print "populateDefaultVals: ".$preference->{value}."\n"; |
| 99 |
} |
| 100 |
|
| 101 |
=head deleteTestGroup |
| 102 |
@OVERLOADED |
| 103 |
|
| 104 |
my $records = createTestGroup(); |
| 105 |
##Do funky stuff |
| 106 |
deleteTestGroup($prefs); |
| 107 |
|
| 108 |
Removes the given test group from the DB. |
| 109 |
|
| 110 |
=cut |
| 111 |
|
| 112 |
sub deleteTestGroup { |
| 113 |
my ($class, $preferences) = @_; |
| 114 |
|
| 115 |
while( my ($key, $pref) = each %$preferences) { |
| 116 |
C4::Context->set_preference($pref->{preference}, $pref->{value}); |
| 117 |
} |
| 118 |
} |
| 119 |
sub _deleteTestGroupFromIdentifiers { |
| 120 |
my ($class, $testGroupIdentifiers) = @_; |
| 121 |
|
| 122 |
my $schema = Koha::Database->new_schema(); |
| 123 |
foreach my $key (@$testGroupIdentifiers) { |
| 124 |
$schema->resultset('Borrower')->find({"cardnumber" => $key})->delete(); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
1; |