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

(-)a/t/lib/TestObjects/BiblioFactory.pm (+2 lines)
Lines 41-46 sub getObjectType { Link Here
41
                        {'biblio.title' => 'I wish I met your mother',
41
                        {'biblio.title' => 'I wish I met your mother',
42
                         'biblio.author'   => 'Pertti Kurikka',
42
                         'biblio.author'   => 'Pertti Kurikka',
43
                         'biblio.copyrightdate' => '1960',
43
                         'biblio.copyrightdate' => '1960',
44
                         'biblio.biblionumber' => 1212,
44
                         'biblioitems.isbn'     => '9519671580',
45
                         'biblioitems.isbn'     => '9519671580',
45
                         'biblioitems.itemtype' => 'BK',
46
                         'biblioitems.itemtype' => 'BK',
46
                        },
47
                        },
Lines 71-76 sub handleTestObject { Link Here
71
    ##First see if the given Record already exists in the DB. For testing purposes we use the isbn as the UNIQUE identifier.
72
    ##First see if the given Record already exists in the DB. For testing purposes we use the isbn as the UNIQUE identifier.
72
    my $resultset = Koha::Database->new()->schema()->resultset('Biblioitem');
73
    my $resultset = Koha::Database->new()->schema()->resultset('Biblioitem');
73
    my $existingBiblio = $resultset->search({isbn => $object->{"biblioitems.isbn"}})->next();
74
    my $existingBiblio = $resultset->search({isbn => $object->{"biblioitems.isbn"}})->next();
75
    $existingBiblio = $resultset->search({biblionumber => $object->{"biblio.biblionumber"}})->next() unless $existingBiblio;
74
    my ($record, $biblionumber, $biblioitemnumber);
76
    my ($record, $biblionumber, $biblioitemnumber);
75
    unless ($existingBiblio) {
77
    unless ($existingBiblio) {
76
        $record = C4::Biblio::TransformKohaToMarc($object);
78
        $record = C4::Biblio::TransformKohaToMarc($object);
(-)a/t/lib/TestObjects/HoldFactory.pm (+206 lines)
Line 0 Link Here
1
package t::lib::TestObjects::HoldFactory;
2
3
# Copyright KohaSuomi 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 DateTime;
24
25
use C4::Context;
26
use Koha::Database;
27
use C4::Circulation;
28
use C4::Members;
29
use C4::Items;
30
use C4::Reserves;
31
use Koha::Borrowers;
32
use Koha::Biblios;
33
use Koha::BiblioItems;
34
use Koha::Items;
35
use Koha::Checkouts;
36
37
use t::lib::TestObjects::BorrowerFactory;
38
use t::lib::TestObjects::ItemFactory;
39
use t::lib::TestObjects::BiblioFactory;
40
41
use base qw(t::lib::TestObjects::ObjectFactory);
42
43
sub new {
44
    my ($class) = @_;
45
46
    my $self = {};
47
    bless($self, $class);
48
    return $self;
49
}
50
51
sub getDefaultHashKey {
52
    return ['reservenotes'];
53
}
54
sub getObjectType {
55
    return 'HASH';
56
}
57
58
=head t::lib::TestObjects::HoldFactory::createTestGroup( $data [, $hashKey], @stashes )
59
60
    my $holds = t::lib::TestObjects::HoldFactory->createTestGroup([
61
                        {#Hold params
62
                        },
63
                        {#More hold params
64
                        },
65
                    ], undef, $testContext1, $testContext2, $testContext3);
66
67
    t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext1);
68
69
@PARAM1, ARRAY of HASHes.
70
  [ {
71
        cardnumber        => '167Azava0001', #Borrower's cardnumber
72
        isbn              => '971040323123', #ISBN of the Biblio, even if the record normally doesn't have a ISBN, you must mock one on it.
73
        barcode           => undef || '911N12032',    #Item's barcode, if this is an Item-level hold.
74
        branchcode        => 'CPL',
75
        waitingdate       => undef || '2015-01-15', #Since when has this hold been waiting for pickup?
76
    },
77
    {
78
        ...
79
    }
80
  ]
81
@PARAM2, String, the HASH-element to use as the returning HASHes key.
82
@PARAM3-5 HASHRef of test contexts. You can save the given objects to multiple
83
                test contexts. Usually one is enough. These test contexts are
84
                used to help tear down DB changes.
85
@RETURNS HASHRef of $hashKey => HASH-objects representing koha.reserves-table columns
86
                The HASH is keyed with <reservenotes>, or the given $hashKey.
87
    Example: {
88
        '11A001-971040323123-43' => {...},
89
        'my-nice-hold' => {...},
90
        ...
91
    }
92
}
93
=cut
94
95
sub handleTestObject {
96
    my ($class, $object, $stashes) = @_;
97
98
    C4::Reserves::AddReserve($object->{branchcode} || 'CPL',
99
                                        $object->{borrower}->borrowernumber,
100
                                        $object->{biblio}->{biblionumber},
101
                                        undef, #constraint
102
                                        undef, #bibitems
103
                                        undef, #priority
104
                                        $object->{reservedate}, #resdate
105
                                        $object->{expirationdate}, #expdate
106
                                        $object->{reservenotes}, #notes
107
                                        undef, #title
108
                                        ($object->{item} ? $object->{item}->itemnumber : undef), #checkitem
109
                                        undef, #found
110
                            );
111
    my $reserve_id = C4::Reserves::GetReserveId({biblionumber   => $object->{biblio}->{biblionumber},
112
                                            itemnumber     => ($object->{item} ? $object->{item}->itemnumber : undef),
113
                                            borrowernumber => $object->{borrower}->borrowernumber,
114
                                        });
115
    unless ($reserve_id) {
116
        die "HoldFactory->handleTestObject():> Couldn't create a reserve. for isbn => ".$object->{isbn}.", barcode => ".$object->{barcode}.
117
            ", item => ".($object->{barcode} ? $object->{barcode} : '')."\n";
118
    }
119
    my $hold = C4::Reserves::GetReserve($reserve_id);
120
    foreach my $key (keys $object) {
121
        $hold->{$key} = $object->{$key};
122
    }
123
124
    if ($object->{waitingdate}) {
125
        eval {
126
            C4::Reserves::ModReserveAffect($hold->{item}->itemnumber, $hold->{borrower}->borrowernumber);
127
128
            #Modify the waitingdate. An ugly hack for a ugly module. Bear with me my men!
129
            my $dbh = C4::Context->dbh;
130
            my $query = "UPDATE reserves SET waitingdate = ? WHERE  reserve_id = ?";
131
            my $sth = $dbh->prepare($query);
132
            $sth->execute( $hold->{waitingdate}, $hold->{reserve_id} );
133
        };
134
        if ($@) {
135
            warn "HoldFactory->handleTestObject():> Error when setting the waitingdate for ".$class->getHashKey($object)."$@";
136
        }
137
    }
138
139
    return $hold;
140
}
141
142
=head validateAndPopulateDefaultValues
143
@OVERLOAD
144
145
Validates given Object parameters and makes sure that critical fields are given
146
and populates defaults for missing values.
147
=cut
148
149
sub validateAndPopulateDefaultValues {
150
    my ($self, $object, $hashKey, $stashes) = @_;
151
    $self->SUPER::validateAndPopulateDefaultValues($object, $hashKey);
152
153
    unless ($object->{cardnumber}) {
154
        croak __PACKAGE__.":> Mandatory parameter 'cardnumber' missing.";
155
    }
156
    unless ($object->{isbn}) {
157
        croak __PACKAGE__.":> Mandatory parameter 'isbn' missing.";
158
    }
159
160
    my $borrowers = t::lib::TestObjects::BorrowerFactory->createTestGroup(
161
                                {cardnumber => $object->{cardnumber}},
162
                                undef, @$stashes);
163
    my $borrower = $borrowers->{ $object->{cardnumber} };
164
    $object->{borrower} = $borrower if $borrower;
165
166
    my $biblios = t::lib::TestObjects::BiblioFactory->createTestGroup({"biblio.title" => "Test holds' Biblio",
167
                                                                           "biblioitems.isbn" => $object->{isbn}},
168
                                                                          undef, @$stashes);
169
    my $biblio = $biblios->{ $object->{isbn} || '971-972-call-me' }; #Get default isbn or the given one
170
    $object->{biblio} = $biblio if $biblio;
171
172
    #Get test Item
173
    if ($object->{barcode}) {
174
        my $items = t::lib::TestObjects::ItemFactory->createTestGroup({barcode => $object->{barcode}, isbn => $object->{isbn}}, undef, @$stashes);
175
        my $item  = $items->{ $object->{barcode} };
176
        $object->{item} = $item;
177
    }
178
179
    return $object;
180
}
181
182
=head
183
184
    my $objects = createTestGroup();
185
    ##Do funky stuff
186
    deleteTestGroup($records);
187
188
Removes the given test group from the DB.
189
190
=cut
191
192
sub deleteTestGroup {
193
    my ($self, $objects) = @_;
194
195
    #For some reason DBIx cannot delete from OldReserves-table so using DBI and not losing sleep
196
    my $dbh = C4::Context->dbh();
197
    my $del_old_sth = $dbh->prepare("DELETE FROM old_reserves WHERE reserve_id = ?");
198
    my $del_sth = $dbh->prepare("DELETE FROM reserves WHERE reserve_id = ?");
199
200
    while( my ($key, $object) = each %$objects) {
201
        $del_sth->execute($object->{reserve_id});
202
        $del_old_sth->execute($object->{reserve_id});
203
    }
204
}
205
206
1;
(-)a/t/lib/TestObjects/ItemFactory.pm (-9 / +9 lines)
Lines 23-28 use Carp; Link Here
23
23
24
use C4::Items;
24
use C4::Items;
25
use Koha::Biblios;
25
use Koha::Biblios;
26
use Koha::BiblioItems;
26
use Koha::Items;
27
use Koha::Items;
27
use Koha::Checkouts;
28
use Koha::Checkouts;
28
29
Lines 64-80 sub handleTestObject { Link Here
64
65
65
    #Look for the parent biblio, if we don't find one, create a default one.
66
    #Look for the parent biblio, if we don't find one, create a default one.
66
    my ($biblionumber, $biblioitemnumber, $itemnumber);
67
    my ($biblionumber, $biblioitemnumber, $itemnumber);
67
    my $biblio;
68
    my $biblios = t::lib::TestObjects::BiblioFactory->createTestGroup({"biblio.title" => "Test Items' Biblio",
68
    $biblio = Koha::Biblios->find({biblionumber => $object->{biblionumber}}) if $object->{biblionumber};
69
                                                                       "biblioitems.isbn" => $object->{isbn},
69
    unless ($biblio) {
70
                                                                       "biblio.biblionumber" => $object->{biblionumber}},
70
        my $biblios = t::lib::TestObjects::BiblioFactory->createTestGroup({"biblio.title" => "Test Items' Biblio",},
71
                                                                          undef, @$stashes);
71
                                                                          undef, @$stashes);
72
        $biblio = $biblios->{'971-972-call-me'};
72
    my $biblio;
73
        $object->{biblionumber} = $biblio->{biblionumber};
73
    foreach my $k (keys %$biblios) {
74
    }
74
        $biblio = $biblios->{$k};
75
    else {
76
        $object->{biblionumber} = $biblio->biblionumber;
77
    }
75
    }
76
    $object->{biblionumber} = $biblio->{biblionumber};
77
    $object->{biblio} = $biblio;
78
78
79
    #Ok we got a biblio, now we can add an Item for it. First see if the Item already exists.
79
    #Ok we got a biblio, now we can add an Item for it. First see if the Item already exists.
80
    my $item;
80
    my $item;
(-)a/t/lib/TestObjects/ObjectFactory.pm (+6 lines)
Lines 171-176 sub tearDownTestContext { Link Here
171
        t::lib::TestObjects::BorrowerFactory->deleteTestGroup($stash->{borrower});
171
        t::lib::TestObjects::BorrowerFactory->deleteTestGroup($stash->{borrower});
172
        delete $stash->{borrower};
172
        delete $stash->{borrower};
173
    }
173
    }
174
    if ($stash->{hold}) {
175
        require t::lib::TestObjects::HoldFactory;
176
        t::lib::TestObjects::HoldFactory->deleteTestGroup($stash->{hold});
177
        delete $stash->{hold};
178
    }
174
    if ($stash->{letterTemplate}) {
179
    if ($stash->{letterTemplate}) {
175
        require t::lib::TestObjects::LetterTemplateFactory;
180
        require t::lib::TestObjects::LetterTemplateFactory;
176
        t::lib::TestObjects::LetterTemplateFactory->deleteTestGroup($stash->{letterTemplate});
181
        t::lib::TestObjects::LetterTemplateFactory->deleteTestGroup($stash->{letterTemplate});
Lines 199-204 sub getHashKey { Link Here
199
    my ($class, $object, $primaryKey, $hashKeys) = @_;
204
    my ($class, $object, $primaryKey, $hashKeys) = @_;
200
205
201
    my @collectedHashKeys;
206
    my @collectedHashKeys;
207
    $hashKeys = $class->getDefaultHashKey unless $hashKeys;
202
    $hashKeys = [$hashKeys] unless ref($hashKeys) eq 'ARRAY';
208
    $hashKeys = [$hashKeys] unless ref($hashKeys) eq 'ARRAY';
203
    foreach my $hashKey (@$hashKeys) {
209
    foreach my $hashKey (@$hashKeys) {
204
        if (ref($object) eq 'HASH') {
210
        if (ref($object) eq 'HASH') {
(-)a/t/lib/TestObjects/objectFactories.t (-1 / +56 lines)
Lines 49-54 use Koha::Serial::Subscription::Frequencies; Link Here
49
use Koha::Serial::Subscription::Numberpatterns;
49
use Koha::Serial::Subscription::Numberpatterns;
50
use Koha::Serial::Serials;
50
use Koha::Serial::Serials;
51
use t::lib::TestObjects::SystemPreferenceFactory;
51
use t::lib::TestObjects::SystemPreferenceFactory;
52
use t::lib::TestObjects::HoldFactory
52
use C4::Context;
53
use C4::Context;
53
use t::lib::TestObjects::MessageQueueFactory;
54
use t::lib::TestObjects::MessageQueueFactory;
54
use C4::Letters;
55
use C4::Letters;
Lines 58-63 my $testContext = {}; #Gather all created Objects here so we can finally remove Link Here
58
59
59
60
60
61
62
########## HoldFactory subtests ##########
63
subtest 't::lib::TestObjects::HoldFactory' => \&testHoldFactory;
64
sub testHoldFactory {
65
    my $subtestContext = {};
66
    ##Create and Delete using dependencies in the $testContext instantiated in previous subtests.
67
    my $holds = t::lib::TestObjects::HoldFactory->createTestGroup(
68
                        {cardnumber        => '1A01',
69
                         isbn              => '971',
70
                         barcode           => '1N01',
71
                         branchcode        => 'CPL',
72
                         waitingdate       => '2015-01-15',
73
                        },
74
                        ['cardnumber','isbn','barcode'], $subtestContext);
75
76
    is($holds->{'1A01-971-1N01'}->{branchcode},
77
       'CPL',
78
       "Hold '1A01-971-1N01' pickup location is 'CPL'.");
79
    is($holds->{'1A01-971-1N01'}->{waitingdate},
80
       '2015-01-15',
81
       "Hold '1A01-971-1N01' waiting date is '2015-01-15'.");
82
83
    #using the default test hold identifier reservenotes to distinguish hard-to-identify holds.
84
    my $holds2 = t::lib::TestObjects::HoldFactory->createTestGroup([
85
                        {cardnumber        => '1A01',
86
                         isbn              => '971',
87
                         barcode           => '1N02',
88
                         branchcode        => 'CPL',
89
                         reservenotes      => 'nice hold',
90
                        },
91
                        {cardnumber        => '1A01',
92
                         barcode           => '1N03',
93
                         isbn              => '971',
94
                         branchcode        => 'CPL',
95
                         reservenotes      => 'better hold',
96
                        },
97
                    ], undef, $subtestContext);
98
99
    is($holds2->{'nice hold'}->{branchcode},
100
        'CPL',
101
        "Hold 'nice hold' pickup location is 'CPL'.");
102
    is($holds2->{'nice hold'}->{borrower}->cardnumber,
103
        '1A01',
104
        "Hold 'nice hold' cardnumber is '1A01'.");
105
    is($holds2->{'better hold'}->{isbn},
106
        '971',
107
        "Hold 'better hold' isbn '971'.");
108
109
    t::lib::TestObjects::HoldFactory->deleteTestGroup($subtestContext->{hold});
110
111
    my $holds_deleted = C4::Reserves::GetReservesFromBiblionumber({biblionumber => $holds->{'1A01-971-1N01'}->{biblio}->{biblionumber}});
112
    ok (not(@$holds_deleted), "Holds deleted");
113
};
114
115
116
61
########## FileFactory subtests ##########
117
########## FileFactory subtests ##########
62
subtest 't::lib::TestObjects::FileFactory' => \&testFileFactory;
118
subtest 't::lib::TestObjects::FileFactory' => \&testFileFactory;
63
sub testFileFactory {
119
sub testFileFactory {
64
- 

Return to bug 13906