View | Details | Raw Unified | Return to bug 13906
Collapse All | Expand All

(-)a/t/lib/TestObjects/ObjectFactory.pm (+5 lines)
Lines 107-112 sub tearDownTestContext { Link Here
107
        t::lib::TestObjects::LetterTemplateFactory->deleteTestGroup($stash->{letterTemplates});
107
        t::lib::TestObjects::LetterTemplateFactory->deleteTestGroup($stash->{letterTemplates});
108
        delete $stash->{letterTemplates};
108
        delete $stash->{letterTemplates};
109
    }
109
    }
110
    if ($stash->{systempreferences}) {
111
        require t::lib::TestObjects::SystemPreferenceFactory;
112
        t::lib::TestObjects::SystemPreferenceFactory->deleteTestGroup($stash->{systempreferences});
113
        delete $stash->{systempreferences};
114
    }
110
}
115
}
111
116
112
sub getHashKey {
117
sub getHashKey {
(-)a/t/lib/TestObjects/SystemPreferenceFactory.pm (+128 lines)
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;
(-)a/t/lib/TestObjects/objectFactories.t (-1 / +38 lines)
Lines 36-41 use t::lib::TestObjects::CheckoutFactory; Link Here
36
use Koha::Checkouts;
36
use Koha::Checkouts;
37
use t::lib::TestObjects::LetterTemplateFactory;
37
use t::lib::TestObjects::LetterTemplateFactory;
38
use Koha::LetterTemplates;
38
use Koha::LetterTemplates;
39
use t::lib::TestObjects::SystemPreferenceFactory;
40
use C4::Context;
39
41
40
42
41
my $testContext = {}; #Gather all created Objects here so we can finally remove them all.
43
my $testContext = {}; #Gather all created Objects here so we can finally remove them all.
Lines 246-252 subtest 't::lib::TestObjects::LetterTemplateFactory' => sub { Link Here
246
    ok(not(defined($letterTemplate)), "LetterTemplate 'circulation-ODUE1-CPL-print' deleted");
248
    ok(not(defined($letterTemplate)), "LetterTemplate 'circulation-ODUE1-CPL-print' deleted");
247
};
249
};
248
250
251
########## SystemPreferenceFactory subtests ##########
252
subtest 't::lib::TestObjects::SystemPreferenceFactory' => sub {
253
    my $subtestContext = {};
254
    ##Create and Delete using dependencies in the $testContext instantiated in previous subtests.
255
256
    # take syspref 'opacuserlogin' and save its current value
257
    my $current_pref_value = C4::Context->preference("opacuserlogin");
258
    
259
    is(C4::Context->preference("opacuserlogin"), C4::Context->preference("opacuserlogin"), "System Preference opacuserlogin original value '".((C4::Context->preference("opacuserlogin")) ? C4::Context->preference("opacuserlogin"):0)."'");
260
       
261
    # reverse the value for testing
262
    my $pref_new_value = !$current_pref_value;
263
    
264
    
265
    my $objects = t::lib::TestObjects::SystemPreferenceFactory->createTestGroup([
266
                    {preference => 'opacuserlogin',
267
                    value      => $pref_new_value # set the reversed value
268
                    },
269
                    ], undef, $subtestContext, undef, undef);
270
271
    my $letterTemplate = 
272
    is(C4::Context->preference("opacuserlogin"), $pref_new_value, "System Preference opacuserlogin reversed to '".(($pref_new_value) ? $pref_new_value:0)."'");
249
273
274
    # let's change it again to test that only the original preference value is saved
275
    $objects = t::lib::TestObjects::SystemPreferenceFactory->createTestGroup([
276
            {preference => 'opacuserlogin',
277
             value      => 2 # set the reversed value
278
            },
279
            ], undef, $subtestContext, undef, undef);
280
    
281
    is(C4::Context->preference("opacuserlogin"), 2, "System Preference opacuserlogin set to '2'");
282
283
    #Delete them
284
    t::lib::TestObjects::SystemPreferenceFactory->deleteTestGroup($subtestContext->{systempreferences});
285
    is(C4::Context->preference("opacuserlogin"), $current_pref_value, "System Preference opacuserlogin restored to '".(($current_pref_value) ? $current_pref_value:0)."' after test group deletion");
286
287
};
250
288
251
########## Global test context subtests ##########
289
########## Global test context subtests ##########
252
subtest 't::lib::TestObjects::ObjectFactory clearing global test context' => sub {
290
subtest 't::lib::TestObjects::ObjectFactory clearing global test context' => sub {
253
- 

Return to bug 13906