Bug 12647: PQF QueryParser driver and unit tests fixes
[koha.git] / t / Acquisition / Invoice.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use C4::Context;
5
6 use Test::More tests => 50;
7 use Test::MockModule;
8
9 my $module = new Test::MockModule('C4::Context');
10 $module->mock('_new_dbh', sub {
11     my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
12         || die "Cannot create handle: $DBI::errstr\n";
13     return $dbh;
14 });
15
16 my $dbh = C4::Context->dbh;
17
18 use_ok('C4::Acquisition');
19
20 # We need to add a resultset to avoid DBI fail
21 # ("DBI bind_columns: invalid number of arguments...")
22 my $rs = [
23     [qw(one two three four)],
24     [1, 2, 3, 4]
25 ];
26
27 $dbh->{mock_add_resultset} = $rs;
28 my @invoices = C4::Acquisition::GetInvoices(
29     supplierid => "supplierid",
30     invoicenumber => "invoicenumber",
31     suppliername => "suppliername",
32     shipmentdatefrom => "shipmentdatefrom",
33     shipmentdateto => "shipmentdateto",
34     billingdatefrom => "billingdatefrom",
35     billingdateto => "billingdateto",
36     isbneanissn => "isbneanissn",
37     title => "title",
38     author => "author",
39     publisher => "publisher",
40     publicationyear => "publicationyear",
41     branchcode => "branchcode",
42 );
43 my $history = $dbh->{mock_all_history};
44
45 ok(scalar(@$history) > 0);
46 my @bound_params = @{ $history->[-1]->{bound_params} };
47 is(scalar(@bound_params), 16);
48 is($bound_params[0], 'supplierid');
49 is($bound_params[1], '%invoicenumber%');
50 is($bound_params[2], '%suppliername%');
51 is($bound_params[3], 'shipmentdatefrom');
52 is($bound_params[4], 'shipmentdateto');
53 is($bound_params[5], 'billingdatefrom');
54 is($bound_params[6], 'billingdateto');
55 is($bound_params[7], 'isbneanissn');
56 is($bound_params[8], 'isbneanissn');
57 is($bound_params[9], 'isbneanissn');
58 is($bound_params[10], 'title');
59 is($bound_params[11], 'author');
60 is($bound_params[12], 'publisher');
61 is($bound_params[13], 'publicationyear');
62 is($bound_params[14], 'publicationyear');
63 is($bound_params[15], 'branchcode');
64
65 $dbh->{mock_clear_history} = 1;
66 $dbh->{mock_add_resultset} = $rs;
67 GetInvoice(42);
68 $history = $dbh->{mock_all_history};
69 is(scalar(@$history), 1);
70 @bound_params = @{ $history->[0]->{bound_params} };
71 is(scalar(@bound_params), 1);
72 is($bound_params[0], 42);
73
74 $dbh->{mock_clear_history} = 1;
75 $dbh->{mock_add_resultset} = $rs;
76 $dbh->{mock_add_resultset} = $rs;
77 my $invoice = GetInvoiceDetails(42);
78 $history = $dbh->{mock_all_history};
79 is(scalar(@$history), 2);
80 @bound_params = @{ $history->[0]->{bound_params} };
81 is(scalar(@bound_params), 1);
82 is($bound_params[0], 42);
83 @bound_params = @{ $history->[1]->{bound_params} };
84 is(scalar(@bound_params), 1);
85 is($bound_params[0], 42);
86 ok(exists $invoice->{orders});
87
88 $dbh->{mock_clear_history} = 1;
89 is(AddInvoice(booksellerid => 1), undef);   # Fails because of a missing parameter
90 $history = $dbh->{mock_all_history};
91 is(scalar(@$history), 0);
92
93 $dbh->{mock_clear_history} = 1;
94 AddInvoice(invoicenumber => 'invoice', booksellerid => 1, unknown => "unknown");
95 $history = $dbh->{mock_all_history};
96 is(scalar(@$history), 1);
97 @bound_params = @{ $history->[0]->{bound_params} };
98 is(scalar(@bound_params), 2);
99 ok(grep /^1$/, @bound_params);
100 ok(grep /^invoice$/, @bound_params);
101 ok(not grep /unknown/, @bound_params);
102
103 $dbh->{mock_clear_history} = 1;
104 is(ModInvoice(booksellerid => 1), undef);   # Fails because of a missing parameter
105 $history = $dbh->{mock_all_history};
106 is(scalar(@$history), 0);
107
108 $dbh->{mock_clear_history} = 1;
109 ModInvoice(invoiceid => 3, invoicenumber => 'invoice', unknown => "unknown");
110 $history = $dbh->{mock_all_history};
111 is(scalar(@$history), 1);
112 @bound_params = @{ $history->[0]->{bound_params} };
113 is(scalar(@bound_params), 2);
114 ok(grep /^3$/, @bound_params);
115 ok(grep /^invoice$/, @bound_params);
116 ok(not grep /unknown/, @bound_params);
117
118 $dbh->{mock_clear_history} = 1;
119 CloseInvoice(42);
120 $history = $dbh->{mock_all_history};
121 is(scalar(@$history), 1);
122 @bound_params = @{ $history->[0]->{bound_params} };
123 is(scalar(@bound_params), 1);
124 is($bound_params[0], 42);
125
126 $dbh->{mock_clear_history} = 1;
127 ReopenInvoice(42);
128 $history = $dbh->{mock_all_history};
129 is(scalar(@$history), 1);
130 @bound_params = @{ $history->[0]->{bound_params} };
131 is(scalar(@bound_params), 1);
132 is($bound_params[0], 42);
133 my $checkordersrs = [
134     [qw(COUNT)],
135     [2]
136 ];
137
138 $dbh->{mock_add_resultset} = $checkordersrs;
139 is(DelInvoice(42), undef, "Invoices with items don't get deleted");
140
141 $checkordersrs = [
142     [qw(COUNT)],
143     [0]
144 ];
145
146 my $deleters = [
147     [qw(COUNT)],
148     [1]
149 ];
150
151 $dbh->{mock_add_resultset} = $checkordersrs;
152 $dbh->{mock_add_resultset} = $deleters;
153 ok(DelInvoice(42), "Invoices without items do get deleted");