Bug 13726: Make Koha::Acq::Bookseller using Koha::Object
[koha.git] / t / db_dependent / Contract.t
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::DateUtils;
24 use Koha::Acquisition::Booksellers;
25
26 use DateTime::Duration;
27
28 use Test::More tests => 43;
29
30 BEGIN {
31     use_ok('C4::Contract');
32 }
33
34 my $dbh = C4::Context->dbh;
35 $dbh->{AutoCommit} = 0;
36 $dbh->{RaiseError} = 1;
37
38 $dbh->do(q|DELETE FROM aqbasket|);
39 $dbh->do(q|DELETE FROM aqcontract|);
40 $dbh->do(q|DELETE FROM aqbooksellers|);
41
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
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( $contract->{contractstartdate}, $my_contract1->{contractstartdate}, 'AddContract stores the contract start date correctly.' );
74 is( $contract->{contractenddate}, $my_contract1->{contractenddate}, 'AddContract stores the contract end date correctly.' );
75 is( $contract->{contractname}, $my_contract1->{contractname}, 'AddContract stores the contract name correctly.' );
76 is( $contract->{contractdescription}, $my_contract1->{contractdescription}, 'AddContract stores the contract description correctly.' );
77 is( $contract->{booksellerid}, $my_contract1->{booksellerid}, 'AddContract stores the bookseller id correctly.' );
78
79 my $now = dt_from_string;
80 my $three_more_days = $now + DateTime::Duration->new( days => 3 );
81
82 $my_contract1 = {
83     contractstartdate   => $now->ymd,
84     contractenddate     => $three_more_days->ymd,
85     contractname        => 'My modified contract name',
86     contractdescription => 'My modified contract description',
87     booksellerid        => $bookseller_id2,
88 };
89 my $mod_status = ModContract($my_contract1);
90 is( $mod_status, undef, 'ModContract without the contract number returns 0E0' );
91
92 $my_contract1->{contractnumber} = $my_contract_id1;
93 $mod_status = ModContract($my_contract1);
94 is( $mod_status, 1, 'ModContract returns true' );
95 $contracts = GetContracts();
96 is( @$contracts, 1, 'ModContract does not modify the number of contracts' );
97 $contract = GetContract( { contractnumber => $my_contract_id1 } );
98 is( $contract->{contractstartdate}, $my_contract1->{contractstartdate}, 'ModContract updates the contract start date correctly.' );
99 is( $contract->{contractenddate}, $my_contract1->{contractenddate}, 'ModContract updates the contract end date correctly.' );
100 is( $contract->{contractname}, $my_contract1->{contractname}, 'ModContract updates the contract name correctly.' );
101 is( $contract->{contractdescription}, $my_contract1->{contractdescription}, 'ModContract updates the contract description correctly.' );
102 is( $contract->{booksellerid}, $my_contract1->{booksellerid}, 'ModContract updates the bookseller id correctly.' );
103
104
105 my $my_contract2 = {
106     contractstartdate   => '2013-08-05',
107     contractenddate     => '2013-09-25',
108     contractname        => 'My other contract name',
109     contractdescription => 'My other description contract name',
110     booksellerid        => $bookseller_id1,
111 };
112 my $my_contract_id2 = AddContract($my_contract2);
113 $contracts = GetContracts( { booksellerid => $bookseller_id1 } );
114 is( @$contracts, 1, 'GetContracts returns the correct number of contracts' );
115 $contracts = GetContracts({
116     activeonly => 1
117 });
118 is( @$contracts, 1, 'GetContracts with active only returns only current contracts' );
119 $contracts = GetContracts( { booksellerid => $bookseller_id2 } );
120 is( @$contracts, 1, 'GetContracts returns the correct number of contracts' );
121 $contracts = GetContracts();
122 is( @$contracts, 2, 'GetContracts returns the correct number of contracts' );
123
124 is( $contracts->[0]->{contractnumber}, $my_contract_id1, 'GetContracts returns the contract number correctly' );
125 is( $contracts->[0]->{contractstartdate}, $my_contract1->{contractstartdate}, 'GetContracts returns the contract start date correctly.' );
126 is( $contracts->[0]->{contractenddate}, $my_contract1->{contractenddate}, 'GetContracts returns the contract end date correctly.' );
127 is( $contracts->[0]->{contractname}, $my_contract1->{contractname}, 'GetContracts returns the contract name correctly.' );
128 is( $contracts->[0]->{contractdescription}, $my_contract1->{contractdescription}, 'GetContracts returns the contract description correctly.' );
129 is( $contracts->[0]->{booksellerid}, $my_contract1->{booksellerid}, 'GetContracts returns the bookseller id correctly.' );
130
131 is( $contracts->[1]->{contractnumber}, $my_contract_id2, 'GetContracts returns the contract number correctly' );
132 is( $contracts->[1]->{contractstartdate}, $my_contract2->{contractstartdate}, 'GetContracts returns the contract start date correctly.' );
133 is( $contracts->[1]->{contractenddate}, $my_contract2->{contractenddate}, 'GetContracts returns the contract end date correctly.' );
134 is( $contracts->[1]->{contractname}, $my_contract2->{contractname}, 'GetContracts returns the contract name correctly.' );
135 is( $contracts->[1]->{contractdescription}, $my_contract2->{contractdescription}, 'GetContracts returns the contract description correctly.' );
136 is( $contracts->[1]->{booksellerid}, $my_contract2->{booksellerid}, 'GetContracts returns the bookseller id correctly.' );
137
138
139 my $del_status = DelContract();
140 is( $del_status, undef, 'DelContract without contract number returns undef' );
141
142 $del_status = DelContract( { contractnumber => $my_contract_id1  } );
143 is( $del_status, 1, 'DelContract returns true' );
144 $contracts = GetContracts();
145 is( @$contracts, 1, 'DelContract deletes a contract' );
146
147 $del_status = DelContract( { contractnumber => $my_contract_id2 } );
148 is( $del_status, 1, 'DelContract returns true' );
149 $contracts = GetContracts();
150 is( @$contracts, 0, 'DelContract deletes a contract' );
151
152 $dbh->rollback;