Lines 1-193
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::More tests => 43; |
30 |
|
31 |
BEGIN { |
32 |
use_ok( 'C4::Contract', qw( GetContracts GetContract AddContract ModContract DelContract ) ); |
33 |
} |
34 |
|
35 |
my $schema = Koha::Database->new->schema; |
36 |
$schema->storage->txn_begin; |
37 |
my $dbh = C4::Context->dbh; |
38 |
|
39 |
$dbh->do(q|DELETE FROM aqbasket|); |
40 |
$dbh->do(q|DELETE FROM aqcontract|); |
41 |
$dbh->do(q|DELETE FROM aqbooksellers|); |
42 |
|
43 |
my $bookseller1 = Koha::Acquisition::Bookseller->new( { name => 'My first bookseller' } )->store; |
44 |
my $bookseller_id1 = $bookseller1->id; |
45 |
isnt( $bookseller_id1, undef, 'AddBookseller does not return undef' ); |
46 |
my $bookseller2 = Koha::Acquisition::Bookseller->new( { name => 'My second bookseller' } )->store; |
47 |
my $bookseller_id2 = $bookseller2->id; |
48 |
isnt( $bookseller_id2, undef, 'AddBookseller does not return undef' ); |
49 |
my $contracts = GetContracts(); |
50 |
is( @$contracts, 0, 'GetContracts returns the correct number of contracts' ); |
51 |
my $contract = GetContract(); |
52 |
is( $contract, undef, 'GetContract without argument returns undef' ); |
53 |
|
54 |
my $my_contract1 = { |
55 |
contractstartdate => '2014-06-01', |
56 |
contractenddate => '2014-06-30', |
57 |
contractname => 'My contract name', |
58 |
contractdescription => 'My contract description', |
59 |
booksellerid => $bookseller_id1, |
60 |
}; |
61 |
my $my_contract_id1 = AddContract(); |
62 |
is( $my_contract_id1, undef, 'AddContract without argument returns undef' ); |
63 |
$my_contract_id1 = AddContract($my_contract1); |
64 |
isnt( $my_contract_id1, undef, 'AddContract does not return undef' ); |
65 |
|
66 |
$contracts = GetContracts(); |
67 |
is( @$contracts, 1, 'AddContract adds a contract' ); |
68 |
|
69 |
$contract = GetContract(); |
70 |
is( $contract, undef, 'GetContract without argument returns undef' ); |
71 |
$contract = GetContract( { contractnumber => $my_contract_id1 } ); |
72 |
is( |
73 |
$contract->{contractstartdate}, $my_contract1->{contractstartdate}, |
74 |
'AddContract stores the contract start date correctly.' |
75 |
); |
76 |
is( |
77 |
$contract->{contractenddate}, $my_contract1->{contractenddate}, |
78 |
'AddContract stores the contract end date correctly.' |
79 |
); |
80 |
is( $contract->{contractname}, $my_contract1->{contractname}, 'AddContract stores the contract name correctly.' ); |
81 |
is( |
82 |
$contract->{contractdescription}, $my_contract1->{contractdescription}, |
83 |
'AddContract stores the contract description correctly.' |
84 |
); |
85 |
is( $contract->{booksellerid}, $my_contract1->{booksellerid}, 'AddContract stores the bookseller id correctly.' ); |
86 |
|
87 |
my $now = dt_from_string; |
88 |
my $three_more_days = $now + DateTime::Duration->new( days => 3 ); |
89 |
|
90 |
$my_contract1 = { |
91 |
contractstartdate => $now->ymd, |
92 |
contractenddate => $three_more_days->ymd, |
93 |
contractname => 'My modified contract name', |
94 |
contractdescription => 'My modified contract description', |
95 |
booksellerid => $bookseller_id2, |
96 |
}; |
97 |
my $mod_status = ModContract($my_contract1); |
98 |
is( $mod_status, undef, 'ModContract without the contract number returns 0E0' ); |
99 |
|
100 |
$my_contract1->{contractnumber} = $my_contract_id1; |
101 |
$mod_status = ModContract($my_contract1); |
102 |
is( $mod_status, 1, 'ModContract returns true' ); |
103 |
$contracts = GetContracts(); |
104 |
is( @$contracts, 1, 'ModContract does not modify the number of contracts' ); |
105 |
$contract = GetContract( { contractnumber => $my_contract_id1 } ); |
106 |
is( |
107 |
$contract->{contractstartdate}, $my_contract1->{contractstartdate}, |
108 |
'ModContract updates the contract start date correctly.' |
109 |
); |
110 |
is( |
111 |
$contract->{contractenddate}, $my_contract1->{contractenddate}, |
112 |
'ModContract updates the contract end date correctly.' |
113 |
); |
114 |
is( $contract->{contractname}, $my_contract1->{contractname}, 'ModContract updates the contract name correctly.' ); |
115 |
is( |
116 |
$contract->{contractdescription}, $my_contract1->{contractdescription}, |
117 |
'ModContract updates the contract description correctly.' |
118 |
); |
119 |
is( $contract->{booksellerid}, $my_contract1->{booksellerid}, 'ModContract updates the bookseller id correctly.' ); |
120 |
|
121 |
my $my_contract2 = { |
122 |
contractstartdate => '2013-08-05', |
123 |
contractenddate => '2013-09-25', |
124 |
contractname => 'My other contract name', |
125 |
contractdescription => 'My other description contract name', |
126 |
booksellerid => $bookseller_id1, |
127 |
}; |
128 |
my $my_contract_id2 = AddContract($my_contract2); |
129 |
$contracts = GetContracts( { booksellerid => $bookseller_id1 } ); |
130 |
is( @$contracts, 1, 'GetContracts returns the correct number of contracts' ); |
131 |
$contracts = GetContracts( { activeonly => 1 } ); |
132 |
is( @$contracts, 1, 'GetContracts with active only returns only current contracts' ); |
133 |
$contracts = GetContracts( { booksellerid => $bookseller_id2 } ); |
134 |
is( @$contracts, 1, 'GetContracts returns the correct number of contracts' ); |
135 |
$contracts = GetContracts(); |
136 |
is( @$contracts, 2, 'GetContracts returns the correct number of contracts' ); |
137 |
|
138 |
is( $contracts->[0]->{contractnumber}, $my_contract_id1, 'GetContracts returns the contract number correctly' ); |
139 |
is( |
140 |
$contracts->[0]->{contractstartdate}, $my_contract1->{contractstartdate}, |
141 |
'GetContracts returns the contract start date correctly.' |
142 |
); |
143 |
is( |
144 |
$contracts->[0]->{contractenddate}, $my_contract1->{contractenddate}, |
145 |
'GetContracts returns the contract end date correctly.' |
146 |
); |
147 |
is( |
148 |
$contracts->[0]->{contractname}, $my_contract1->{contractname}, |
149 |
'GetContracts returns the contract name correctly.' |
150 |
); |
151 |
is( |
152 |
$contracts->[0]->{contractdescription}, $my_contract1->{contractdescription}, |
153 |
'GetContracts returns the contract description correctly.' |
154 |
); |
155 |
is( |
156 |
$contracts->[0]->{booksellerid}, $my_contract1->{booksellerid}, |
157 |
'GetContracts returns the bookseller id correctly.' |
158 |
); |
159 |
|
160 |
is( $contracts->[1]->{contractnumber}, $my_contract_id2, 'GetContracts returns the contract number correctly' ); |
161 |
is( |
162 |
$contracts->[1]->{contractstartdate}, $my_contract2->{contractstartdate}, |
163 |
'GetContracts returns the contract start date correctly.' |
164 |
); |
165 |
is( |
166 |
$contracts->[1]->{contractenddate}, $my_contract2->{contractenddate}, |
167 |
'GetContracts returns the contract end date correctly.' |
168 |
); |
169 |
is( |
170 |
$contracts->[1]->{contractname}, $my_contract2->{contractname}, |
171 |
'GetContracts returns the contract name correctly.' |
172 |
); |
173 |
is( |
174 |
$contracts->[1]->{contractdescription}, $my_contract2->{contractdescription}, |
175 |
'GetContracts returns the contract description correctly.' |
176 |
); |
177 |
is( |
178 |
$contracts->[1]->{booksellerid}, $my_contract2->{booksellerid}, |
179 |
'GetContracts returns the bookseller id correctly.' |
180 |
); |
181 |
|
182 |
my $del_status = DelContract(); |
183 |
is( $del_status, undef, 'DelContract without contract number returns undef' ); |
184 |
|
185 |
$del_status = DelContract( { contractnumber => $my_contract_id1 } ); |
186 |
is( $del_status, 1, 'DelContract returns true' ); |
187 |
$contracts = GetContracts(); |
188 |
is( @$contracts, 1, 'DelContract deletes a contract' ); |
189 |
|
190 |
$del_status = DelContract( { contractnumber => $my_contract_id2 } ); |
191 |
is( $del_status, 1, 'DelContract returns true' ); |
192 |
$contracts = GetContracts(); |
193 |
is( @$contracts, 0, 'DelContract deletes a contract' ); |
194 |
- |