Bug 24175: Unit tests
[srvgit] / t / db_dependent / SIP / Transaction.t
1 #!/usr/bin/perl
2
3 # Tests for SIP::ILS::Transaction
4 # Current state is very rudimentary. Please help to extend it!
5
6 use Modern::Perl;
7 use Test::More tests => 6;
8
9 use Koha::Database;
10 use t::lib::TestBuilder;
11 use t::lib::Mocks;
12 use C4::SIP::ILS::Patron;
13 use C4::SIP::ILS::Transaction::RenewAll;
14 use C4::SIP::ILS::Transaction::Checkout;
15 use C4::SIP::ILS::Transaction::FeePayment;
16 use C4::SIP::ILS::Transaction::Hold;
17
18 use C4::Reserves;
19 use Koha::IssuingRules;
20
21 my $schema = Koha::Database->new->schema;
22 $schema->storage->txn_begin;
23
24 my $builder = t::lib::TestBuilder->new();
25 my $borr1 = $builder->build({ source => 'Borrower' });
26 my $card = $borr1->{cardnumber};
27 my $sip_patron = C4::SIP::ILS::Patron->new( $card );
28
29 # Create transaction RenewAll, assign patron, and run (no items)
30 my $transaction = C4::SIP::ILS::Transaction::RenewAll->new();
31 is( ref $transaction, "C4::SIP::ILS::Transaction::RenewAll", "New transaction created" );
32 is( $transaction->patron( $sip_patron ), $sip_patron, "Patron assigned to transaction" );
33 isnt( $transaction->do_renew_all, undef, "RenewAll on zero items" );
34
35 subtest fill_holds_at_checkout => sub {
36     plan tests => 6;
37
38
39     my $category = $builder->build({ source => 'Category', value => { category_type => 'A' }});
40     my $branch   = $builder->build({ source => 'Branch' });
41     my $borrower = $builder->build({ source => 'Borrower', value =>{
42         branchcode => $branch->{branchcode},
43         categorycode=>$category->{categorycode}
44         }
45     });
46     t::lib::Mocks::mock_userenv({ branchcode => $branch->{branchcode}, flags => 1 });
47
48     my $itype = $builder->build({ source => 'Itemtype', value =>{notforloan=>0} });
49     my $biblio = $builder->build({ source => 'Biblio' });
50     my $biblioitem = $builder->build({ source => 'Biblioitem', value=>{biblionumber=>$biblio->{biblionumber}} });
51     my $item1 = $builder->build({ source => 'Item', value => {
52         barcode       => 'barcode4test',
53         homebranch    => $branch->{branchcode},
54         holdingbranch => $branch->{branchcode},
55         biblionumber  => $biblio->{biblionumber},
56         itype         => $itype->{itemtype},
57         notforloan       => 0,
58         }
59     });
60     my $item2 = $builder->build({ source => 'Item', value => {
61         homebranch    => $branch->{branchcode},
62         holdingbranch => $branch->{branchcode},
63         biblionumber  => $biblio->{biblionumber},
64         itype         => $itype->{itemtype},
65         notforloan       => 0,
66         }
67     });
68
69     Koha::IssuingRule->new({
70         categorycode     => $borrower->{categorycode},
71         itemtype         => $itype->{itemtype},
72         branchcode       => $branch->{branchcode},
73         onshelfholds     => 1,
74         reservesallowed  => 3,
75         holds_per_record => 3,
76         issuelength      => 5,
77         lengthunit       => 'days',
78     })->store;
79
80     my $reserve1 = AddReserve($branch->{branchcode},$borrower->{borrowernumber},$biblio->{biblionumber});
81     my $reserve2 = AddReserve($branch->{branchcode},$borrower->{borrowernumber},$biblio->{biblionumber});
82     my $bib = Koha::Biblios->find( $biblio->{biblionumber} );
83     is( $bib->holds->count(), 2, "Bib has 2 holds");
84
85     my $sip_patron = C4::SIP::ILS::Patron->new( $borrower->{cardnumber} );
86     my $sip_item   = C4::SIP::ILS::Item->new( $item1->{barcode} );
87     my $transaction = C4::SIP::ILS::Transaction::Checkout->new();
88     is( ref $transaction, "C4::SIP::ILS::Transaction::Checkout", "New transaction created" );
89     is( $transaction->patron( $sip_patron ), $sip_patron, "Patron assigned to transaction" );
90     is( $transaction->item( $sip_item ), $sip_item, "Item assigned to transaction" );
91     my $checkout = $transaction->do_checkout();
92     use Data::Dumper; # Temporary debug statement
93     is( $bib->holds->count(), 1, "Bib has 1 holds remaining") or diag Dumper $checkout;
94
95     t::lib::Mocks::mock_preference('itemBarcodeInputFilter', 'whitespace');
96     $sip_item   = C4::SIP::ILS::Item->new( ' barcode 4 test ');
97     $transaction = C4::SIP::ILS::Transaction::Checkout->new();
98     is( $sip_item->{barcode}, $item1->{barcode}, "Item assigned to transaction" );
99 };
100
101 subtest "FeePayment->pay tests" => sub {
102
103     plan tests => 5;
104
105     # Create a borrower and add some outstanding debts to their account
106     my $patron = $builder->build( { source => 'Borrower' } );
107     my $account =
108       Koha::Account->new( { patron_id => $patron->{borrowernumber} } );
109     my $debt1 = $account->add_debit(
110         { type => 'ACCOUNT', amount => 100, interface => 'commandline' } );
111     my $debt2 = $account->add_debit(
112         { type => 'ACCOUNT', amount => 200, interface => 'commandline' } );
113
114     # Instantiate a new FeePayment transaction object
115     my $trans = C4::SIP::ILS::Transaction::FeePayment->new();
116     is(
117         ref $trans,
118         "C4::SIP::ILS::Transaction::FeePayment",
119         "New fee transaction created"
120     );
121
122     # Test the 'pay' method
123     # FIXME: pay should not require a borrowernumber
124     # (we should reach out to the transaction which should contain a patron object)
125     my $pay_type = '00';    # 00 - Cash, 01 - VISA, 02 - Creditcard
126     my $ok =
127       $trans->pay( $patron->{borrowernumber}, 100, $pay_type, $debt1->id, 0,
128         0 );
129     ok( $ok, "FeePayment transaction succeeded" );
130     $debt1->discard_changes;
131     is( $debt1->amountoutstanding + 0, 0,
132         "Debt1 was reduced to 0 as expected" );
133     my $offsets = Koha::Account::Offsets->search(
134         { debit_id => $debt1->id, credit_id => { '!=' => undef } } );
135     is( $offsets->count, 1, "FeePayment produced an offset line correctly" );
136     my $credit = $offsets->next->credit;
137     is( $credit->payment_type, 'SIP00', "Payment type was set correctly" );
138 };
139
140 subtest cancel_hold => sub {
141     plan tests => 7;
142
143     my $category = $builder->build({ source => 'Category', value => { category_type => 'A' }});
144     my $branch   = $builder->build({ source => 'Branch' });
145     my $borrower = $builder->build({ source => 'Borrower', value =>{
146         branchcode => $branch->{branchcode},
147         categorycode=>$category->{categorycode}
148         }
149     });
150     t::lib::Mocks::mock_userenv({ branchcode => $branch->{branchcode}, flags => 1 });
151
152     my $itype = $builder->build({ source => 'Itemtype', value =>{notforloan=>0} });
153     my $biblio = $builder->build_sample_biblio();
154     my $item = $builder->build_sample_item({
155         homebranch    => $branch->{branchcode},
156         holdingbranch => $branch->{branchcode},
157         biblionumber  => $biblio->biblionumber,
158         itype         => $itype->{itemtype},
159         notforloan       => 0,
160     });
161
162     Koha::IssuingRule->new({
163         categorycode     => $borrower->{categorycode},
164         itemtype         => $itype->{itemtype},
165         branchcode       => $branch->{branchcode},
166         onshelfholds     => 1,
167         reservesallowed  => 3,
168         holds_per_record => 3,
169         issuelength      => 5,
170         lengthunit       => 'days',
171     })->store;
172
173     my $reserve1 = AddReserve($branch->{branchcode},$borrower->{borrowernumber},$biblio->biblionumber,undef,undef,undef,undef,undef,undef,$item->itemnumber);
174     is( $biblio->holds->count(), 1, "Hold was placed on bib");
175     is( $item->holds->count(),1,"Hold was placed on specific item");
176
177     my $sip_patron = C4::SIP::ILS::Patron->new( $borrower->{cardnumber} );
178     my $sip_item   = C4::SIP::ILS::Item->new( $item->barcode );
179     my $transaction = C4::SIP::ILS::Transaction::Hold->new();
180     is( ref $transaction, "C4::SIP::ILS::Transaction::Hold", "New transaction created" );
181     is( $transaction->patron( $sip_patron ), $sip_patron, "Patron assigned to transaction" );
182     is( $transaction->item( $sip_item ), $sip_item, "Item assigned to transaction" );
183     my $hold = $transaction->drop_hold();
184     is( $biblio->holds->count(), 0, "Bib has 0 holds remaining");
185     is( $item->holds->count(), 0,  "Item has 0 holds remaining");
186 };
187
188 $schema->storage->txn_rollback;