Lines 1-194
Link Here
|
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Copyright 2014 Biblibre SARL |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use C4::Context; |
23 |
use Koha::Database; |
24 |
use Koha::DateUtils qw( dt_from_string ); |
25 |
use Koha::Acquisition::Booksellers; |
26 |
|
27 |
use DateTime::Duration; |
28 |
|
29 |
use Test::NoWarnings; |
30 |
use Test::More tests => 44; |
31 |
|
32 |
BEGIN { |
33 |
use_ok( 'C4::Contract', qw( GetContracts GetContract AddContract ModContract DelContract ) ); |
34 |
} |
35 |
|
36 |
my $schema = Koha::Database->new->schema; |
37 |
$schema->storage->txn_begin; |
38 |
my $dbh = C4::Context->dbh; |
39 |
|
40 |
$dbh->do(q|DELETE FROM aqbasket|); |
41 |
$dbh->do(q|DELETE FROM aqcontract|); |
42 |
$dbh->do(q|DELETE FROM aqbooksellers|); |
43 |
|
44 |
my $bookseller1 = Koha::Acquisition::Bookseller->new( { name => 'My first bookseller' } )->store; |
45 |
my $bookseller_id1 = $bookseller1->id; |
46 |
isnt( $bookseller_id1, undef, 'AddBookseller does not return undef' ); |
47 |
my $bookseller2 = Koha::Acquisition::Bookseller->new( { name => 'My second bookseller' } )->store; |
48 |
my $bookseller_id2 = $bookseller2->id; |
49 |
isnt( $bookseller_id2, undef, 'AddBookseller does not return undef' ); |
50 |
my $contracts = GetContracts(); |
51 |
is( @$contracts, 0, 'GetContracts returns the correct number of contracts' ); |
52 |
my $contract = GetContract(); |
53 |
is( $contract, undef, 'GetContract without argument returns undef' ); |
54 |
|
55 |
my $my_contract1 = { |
56 |
contractstartdate => '2014-06-01', |
57 |
contractenddate => '2014-06-30', |
58 |
contractname => 'My contract name', |
59 |
contractdescription => 'My contract description', |
60 |
booksellerid => $bookseller_id1, |
61 |
}; |
62 |
my $my_contract_id1 = AddContract(); |
63 |
is( $my_contract_id1, undef, 'AddContract without argument returns undef' ); |
64 |
$my_contract_id1 = AddContract($my_contract1); |
65 |
isnt( $my_contract_id1, undef, 'AddContract does not return undef' ); |
66 |
|
67 |
$contracts = GetContracts(); |
68 |
is( @$contracts, 1, 'AddContract adds a contract' ); |
69 |
|
70 |
$contract = GetContract(); |
71 |
is( $contract, undef, 'GetContract without argument returns undef' ); |
72 |
$contract = GetContract( { contractnumber => $my_contract_id1 } ); |
73 |
is( |
74 |
$contract->{contractstartdate}, $my_contract1->{contractstartdate}, |
75 |
'AddContract stores the contract start date correctly.' |
76 |
); |
77 |
is( |
78 |
$contract->{contractenddate}, $my_contract1->{contractenddate}, |
79 |
'AddContract stores the contract end date correctly.' |
80 |
); |
81 |
is( $contract->{contractname}, $my_contract1->{contractname}, 'AddContract stores the contract name correctly.' ); |
82 |
is( |
83 |
$contract->{contractdescription}, $my_contract1->{contractdescription}, |
84 |
'AddContract stores the contract description correctly.' |
85 |
); |
86 |
is( $contract->{booksellerid}, $my_contract1->{booksellerid}, 'AddContract stores the bookseller id correctly.' ); |
87 |
|
88 |
my $now = dt_from_string; |
89 |
my $three_more_days = $now + DateTime::Duration->new( days => 3 ); |
90 |
|
91 |
$my_contract1 = { |
92 |
contractstartdate => $now->ymd, |
93 |
contractenddate => $three_more_days->ymd, |
94 |
contractname => 'My modified contract name', |
95 |
contractdescription => 'My modified contract description', |
96 |
booksellerid => $bookseller_id2, |
97 |
}; |
98 |
my $mod_status = ModContract($my_contract1); |
99 |
is( $mod_status, undef, 'ModContract without the contract number returns 0E0' ); |
100 |
|
101 |
$my_contract1->{contractnumber} = $my_contract_id1; |
102 |
$mod_status = ModContract($my_contract1); |
103 |
is( $mod_status, 1, 'ModContract returns true' ); |
104 |
$contracts = GetContracts(); |
105 |
is( @$contracts, 1, 'ModContract does not modify the number of contracts' ); |
106 |
$contract = GetContract( { contractnumber => $my_contract_id1 } ); |
107 |
is( |
108 |
$contract->{contractstartdate}, $my_contract1->{contractstartdate}, |
109 |
'ModContract updates the contract start date correctly.' |
110 |
); |
111 |
is( |
112 |
$contract->{contractenddate}, $my_contract1->{contractenddate}, |
113 |
'ModContract updates the contract end date correctly.' |
114 |
); |
115 |
is( $contract->{contractname}, $my_contract1->{contractname}, 'ModContract updates the contract name correctly.' ); |
116 |
is( |
117 |
$contract->{contractdescription}, $my_contract1->{contractdescription}, |
118 |
'ModContract updates the contract description correctly.' |
119 |
); |
120 |
is( $contract->{booksellerid}, $my_contract1->{booksellerid}, 'ModContract updates the bookseller id correctly.' ); |
121 |
|
122 |
my $my_contract2 = { |
123 |
contractstartdate => '2013-08-05', |
124 |
contractenddate => '2013-09-25', |
125 |
contractname => 'My other contract name', |
126 |
contractdescription => 'My other description contract name', |
127 |
booksellerid => $bookseller_id1, |
128 |
}; |
129 |
my $my_contract_id2 = AddContract($my_contract2); |
130 |
$contracts = GetContracts( { booksellerid => $bookseller_id1 } ); |
131 |
is( @$contracts, 1, 'GetContracts returns the correct number of contracts' ); |
132 |
$contracts = GetContracts( { activeonly => 1 } ); |
133 |
is( @$contracts, 1, 'GetContracts with active only returns only current contracts' ); |
134 |
$contracts = GetContracts( { booksellerid => $bookseller_id2 } ); |
135 |
is( @$contracts, 1, 'GetContracts returns the correct number of contracts' ); |
136 |
$contracts = GetContracts(); |
137 |
is( @$contracts, 2, 'GetContracts returns the correct number of contracts' ); |
138 |
|
139 |
is( $contracts->[0]->{contractnumber}, $my_contract_id1, 'GetContracts returns the contract number correctly' ); |
140 |
is( |
141 |
$contracts->[0]->{contractstartdate}, $my_contract1->{contractstartdate}, |
142 |
'GetContracts returns the contract start date correctly.' |
143 |
); |
144 |
is( |
145 |
$contracts->[0]->{contractenddate}, $my_contract1->{contractenddate}, |
146 |
'GetContracts returns the contract end date correctly.' |
147 |
); |
148 |
is( |
149 |
$contracts->[0]->{contractname}, $my_contract1->{contractname}, |
150 |
'GetContracts returns the contract name correctly.' |
151 |
); |
152 |
is( |
153 |
$contracts->[0]->{contractdescription}, $my_contract1->{contractdescription}, |
154 |
'GetContracts returns the contract description correctly.' |
155 |
); |
156 |
is( |
157 |
$contracts->[0]->{booksellerid}, $my_contract1->{booksellerid}, |
158 |
'GetContracts returns the bookseller id correctly.' |
159 |
); |
160 |
|
161 |
is( $contracts->[1]->{contractnumber}, $my_contract_id2, 'GetContracts returns the contract number correctly' ); |
162 |
is( |
163 |
$contracts->[1]->{contractstartdate}, $my_contract2->{contractstartdate}, |
164 |
'GetContracts returns the contract start date correctly.' |
165 |
); |
166 |
is( |
167 |
$contracts->[1]->{contractenddate}, $my_contract2->{contractenddate}, |
168 |
'GetContracts returns the contract end date correctly.' |
169 |
); |
170 |
is( |
171 |
$contracts->[1]->{contractname}, $my_contract2->{contractname}, |
172 |
'GetContracts returns the contract name correctly.' |
173 |
); |
174 |
is( |
175 |
$contracts->[1]->{contractdescription}, $my_contract2->{contractdescription}, |
176 |
'GetContracts returns the contract description correctly.' |
177 |
); |
178 |
is( |
179 |
$contracts->[1]->{booksellerid}, $my_contract2->{booksellerid}, |
180 |
'GetContracts returns the bookseller id correctly.' |
181 |
); |
182 |
|
183 |
my $del_status = DelContract(); |
184 |
is( $del_status, undef, 'DelContract without contract number returns undef' ); |
185 |
|
186 |
$del_status = DelContract( { contractnumber => $my_contract_id1 } ); |
187 |
is( $del_status, 1, 'DelContract returns true' ); |
188 |
$contracts = GetContracts(); |
189 |
is( @$contracts, 1, 'DelContract deletes a contract' ); |
190 |
|
191 |
$del_status = DelContract( { contractnumber => $my_contract_id2 } ); |
192 |
is( $del_status, 1, 'DelContract returns true' ); |
193 |
$contracts = GetContracts(); |
194 |
is( @$contracts, 0, 'DelContract deletes a contract' ); |
195 |
- |