Bug 12493: unit tests for the module C4::Contract.pm
[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 use C4::Context;
22 use C4::Bookseller;
23
24 use Test::More tests => 40;
25
26 BEGIN {
27     use_ok('C4::Contract');
28 }
29
30 my $dbh = C4::Context->dbh;
31 $dbh->{AutoCommit} = 0;
32 $dbh->{RaiseError} = 1;
33
34 $dbh->do(q|DELETE FROM aqbasket|);
35 $dbh->do(q|DELETE FROM aqcontract|);
36 $dbh->do(q|DELETE FROM aqbooksellers|);
37
38
39 my $bookseller_id1 = C4::Bookseller::AddBookseller( { name => 'My first bookseller' } );
40 isnt( $bookseller_id1, undef, 'AddBookseller does not return undef' );
41 my $bookseller_id2 = C4::Bookseller::AddBookseller( { name => 'My second bookseller' } );
42 isnt( $bookseller_id2, undef, 'AddBookseller does not return undef' );
43 my $contracts = GetContracts();
44 is( @$contracts, 0, 'GetContracts returns the correct number of contracts' );
45
46 my $my_contract1 = {
47     contractstartdate   => '2014-06-01',
48     contractenddate     => '2014-06-30',
49     contractname        => 'My contract name',
50     contractdescription => 'My contract description',
51     booksellerid        => $bookseller_id1,
52 };
53 my $my_contract_id1 = AddContract($my_contract1);
54 isnt( $my_contract_id1, undef, 'AddContract does not return undef' );
55 $contracts = GetContracts();
56 is( @$contracts, 1, 'AddContract adds a contract' );
57 my $contract = GetContract( { contractnumber => $my_contract_id1 } );
58 is( $contract->{contractstartdate}, $my_contract1->{contractstartdate}, 'AddContract stores the contract start date correctly.' );
59 is( $contract->{contractenddate}, $my_contract1->{contractenddate}, 'AddContract stores the contract end date correctly.' );
60 is( $contract->{contractname}, $my_contract1->{contractname}, 'AddContract stores the contract name correctly.' );
61 is( $contract->{contractdescription}, $my_contract1->{contractdescription}, 'AddContract stores the contract description correctly.' );
62 is( $contract->{booksellerid}, $my_contract1->{booksellerid}, 'AddContract stores the bookseller id correctly.' );
63
64
65 $my_contract1 = {
66     contractstartdate   => '2015-07-02',
67     contractenddate     => '2015-07-31',
68     contractname        => 'My modified contract name',
69     contractdescription => 'My modified contract description',
70     booksellerid        => $bookseller_id2,
71 };
72 my $mod_status = ModContract($my_contract1);
73 is( $mod_status, '0E0', 'ModContract without the contract number returns 0E0' );
74
75 $my_contract1->{contractnumber} = $my_contract_id1;
76 $mod_status = ModContract($my_contract1);
77 is( $mod_status, 1, 'ModContract returns true' );
78 $contracts = GetContracts();
79 is( @$contracts, 1, 'ModContract does not modify the number of contracts' );
80 $contract = GetContract( { contractnumber => $my_contract_id1 } );
81 is( $contract->{contractstartdate}, $my_contract1->{contractstartdate}, 'ModContract updates the contract start date correctly.' );
82 is( $contract->{contractenddate}, $my_contract1->{contractenddate}, 'ModContract updates the contract end date correctly.' );
83 is( $contract->{contractname}, $my_contract1->{contractname}, 'ModContract updates the contract name correctly.' );
84 is( $contract->{contractdescription}, $my_contract1->{contractdescription}, 'ModContract updates the contract description correctly.' );
85 is( $contract->{booksellerid}, $my_contract1->{booksellerid}, 'ModContract updates the bookseller id correctly.' );
86
87
88 my $my_contract2 = {
89     contractstartdate   => '2013-08-05',
90     contractenddate     => '2013-09-25',
91     contractname        => 'My other contract name',
92     contractdescription => 'My other description contract name',
93     booksellerid        => $bookseller_id1,
94 };
95 my $my_contract_id2 = AddContract($my_contract2);
96 $contracts = GetContracts( { booksellerid => $bookseller_id1 } );
97 is( @$contracts, 1, 'GetContracts returns the correct number of contracts' );
98 $contracts = GetContracts({
99     booksellerid => $bookseller_id1,
100     activeonly => 1
101 });
102 is( @$contracts, 0, 'GetContracts with active only returns only current contracts' );
103 $contracts = GetContracts( { booksellerid => $bookseller_id2 } );
104 is( @$contracts, 1, 'GetContracts returns the correct number of contracts' );
105 $contracts = GetContracts();
106 is( @$contracts, 2, 'GetContracts returns the correct number of contracts' );
107
108 is( $contracts->[0]->{contractnumber}, $my_contract_id1, 'GetContracts returns the contract number correctly' );
109 is( $contracts->[0]->{contractstartdate}, $my_contract1->{contractstartdate}, 'GetContracts returns the contract start date correctly.' );
110 is( $contracts->[0]->{contractenddate}, $my_contract1->{contractenddate}, 'GetContracts returns the contract end date correctly.' );
111 is( $contracts->[0]->{contractname}, $my_contract1->{contractname}, 'GetContracts returns the contract name correctly.' );
112 is( $contracts->[0]->{contractdescription}, $my_contract1->{contractdescription}, 'GetContracts returns the contract description correctly.' );
113 is( $contracts->[0]->{booksellerid}, $my_contract1->{booksellerid}, 'GetContracts returns the bookseller id correctly.' );
114
115 is( $contracts->[1]->{contractnumber}, $my_contract_id2, 'GetContracts returns the contract number correctly' );
116 is( $contracts->[1]->{contractstartdate}, $my_contract2->{contractstartdate}, 'GetContracts returns the contract start date correctly.' );
117 is( $contracts->[1]->{contractenddate}, $my_contract2->{contractenddate}, 'GetContracts returns the contract end date correctly.' );
118 is( $contracts->[1]->{contractname}, $my_contract2->{contractname}, 'GetContracts returns the contract name correctly.' );
119 is( $contracts->[1]->{contractdescription}, $my_contract2->{contractdescription}, 'GetContracts returns the contract description correctly.' );
120 is( $contracts->[1]->{booksellerid}, $my_contract2->{booksellerid}, 'GetContracts returns the bookseller id correctly.' );
121
122
123 my $del_status = DelContract();
124 is( $del_status, undef, 'DelContract without contract number returns undef' );
125
126 $del_status = DelContract( { contractnumber => $my_contract_id1  } );
127 is( $del_status, 1, 'DelContract returns true' );
128 $contracts = GetContracts();
129 is( @$contracts, 1, 'DelContract deletes a contract' );
130
131 $del_status = DelContract( { contractnumber => $my_contract_id2 } );
132 is( $del_status, 1, 'DelContract returns true' );
133 $contracts = GetContracts();
134 is( @$contracts, 0, 'DelContract deletes a contract' );
135
136 $dbh->rollback;