Bug 19832: Unit tests
[koha.git] / 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 => 4;
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
16 use C4::Reserves;
17 use Koha::IssuingRules;
18
19 my $schema = Koha::Database->new->schema;
20 $schema->storage->txn_begin;
21
22 my $builder = t::lib::TestBuilder->new();
23 my $borr1 = $builder->build({ source => 'Borrower' });
24 my $card = $borr1->{cardnumber};
25 my $sip_patron = C4::SIP::ILS::Patron->new( $card );
26
27 # Create transaction RenewAll, assign patron, and run (no items)
28 my $transaction = C4::SIP::ILS::Transaction::RenewAll->new();
29 is( ref $transaction, "C4::SIP::ILS::Transaction::RenewAll", "New transaction created" );
30 is( $transaction->patron( $sip_patron ), $sip_patron, "Patron assigned to transaction" );
31 isnt( $transaction->do_renew_all, undef, "RenewAll on zero items" );
32
33 subtest fill_holds_at_checkout => sub {
34     plan tests => 5;
35
36
37     my $category = $builder->build({ source => 'Category' });
38     my $branch   = $builder->build({ source => 'Branch' });
39     my $borrower = $builder->build({ source => 'Borrower', value =>{
40         branchcode => $branch->{branchcode},
41         categorycode=>$category->{categorycode}
42         }
43     });
44     t::lib::Mocks::mock_userenv({ branchcode => $branch->{branchcode}, flags => 1 });
45
46     my $itype = $builder->build({ source => 'Itemtype', value =>{notforloan=>0} });
47     my $biblio = $builder->build({ source => 'Biblio' });
48     my $biblioitem = $builder->build({ source => 'Biblioitem', value=>{biblionumber=>$biblio->{biblionumber}} });
49     my $item1 = $builder->build({ source => 'Item', value => {
50         homebranch    => $branch->{branchcode},
51         holdingbranch => $branch->{branchcode},
52         biblionumber  => $biblio->{biblionumber},
53         itype         => $itype->{itemtype},
54         notforloan       => 0,
55         }
56     });
57     my $item2 = $builder->build({ source => 'Item', value => {
58         homebranch    => $branch->{branchcode},
59         holdingbranch => $branch->{branchcode},
60         biblionumber  => $biblio->{biblionumber},
61         itype         => $itype->{itemtype},
62         notforloan       => 0,
63         }
64     });
65
66     Koha::IssuingRule->new({
67         categorycode     => $borrower->{categorycode},
68         itemtype         => $itype->{itemtype},
69         branchcode       => $branch->{branchcode},
70         onshelfholds     => 1,
71         reservesallowed  => 3,
72         holds_per_record => 3,
73         issuelength      => 5,
74         lengthunit       => 'days',
75         maxissueqty      => 10,
76     })->store;
77
78     my $reserve1 = AddReserve($branch->{branchcode},$borrower->{borrowernumber},$biblio->{biblionumber});
79     my $reserve2 = AddReserve($branch->{branchcode},$borrower->{borrowernumber},$biblio->{biblionumber});
80     my $bib = Koha::Biblios->find( $biblio->{biblionumber} );
81     is( $bib->holds->count(), 2, "Bib has 2 holds");
82
83     my $sip_patron = C4::SIP::ILS::Patron->new( $borrower->{cardnumber} );
84     my $sip_item   = C4::SIP::ILS::Item->new( $item1->{barcode} );
85     my $transaction = C4::SIP::ILS::Transaction::Checkout->new();
86     is( ref $transaction, "C4::SIP::ILS::Transaction::Checkout", "New transaction created" );
87     is( $transaction->patron( $sip_patron ), $sip_patron, "Patron assigned to transaction" );
88     is( $transaction->item( $sip_item ), $sip_item, "Item assigned to transaction" );
89     $transaction->do_checkout();
90     is( $bib->holds->count(), 1, "Bib has 1 holds remaining");
91
92
93 };
94 $schema->storage->txn_rollback;