bug 8215: (followup) make sure C4::CourseReserves doesn't export anything
[koha.git] / t / db_dependent / Circulation.t
1 #!/usr/bin/perl
2
3 use Test::More tests => 16;
4
5 BEGIN {
6     use_ok('C4::Circulation');
7 }
8
9 my $CircControl = C4::Context->preference('CircControl');
10 my $HomeOrHoldingBranch = C4::Context->preference('HomeOrHoldingBranch');
11
12 my $item = {
13     homebranch => 'ItemHomeBranch',
14     holdingbranch => 'ItemHoldingBranch'
15 };
16
17 my $borrower = {
18     branchcode => 'BorrowerBranch'
19 };
20
21 # No userenv, PickupLibrary
22 C4::Context->set_preference('CircControl', 'PickupLibrary');
23 is(
24     C4::Context->preference('CircControl'),
25     'PickupLibrary',
26     'CircControl changed to PickupLibrary'
27 );
28 is(
29     C4::Circulation::_GetCircControlBranch($item, $borrower),
30     $item->{$HomeOrHoldingBranch},
31     '_GetCircControlBranch returned item branch (no userenv defined)'
32 );
33
34 # No userenv, PatronLibrary
35 C4::Context->set_preference('CircControl', 'PatronLibrary');
36 is(
37     C4::Context->preference('CircControl'),
38     'PatronLibrary',
39     'CircControl changed to PatronLibrary'
40 );
41 is(
42     C4::Circulation::_GetCircControlBranch($item, $borrower),
43     $borrower->{branchcode},
44     '_GetCircControlBranch returned borrower branch'
45 );
46
47 # No userenv, ItemHomeLibrary
48 C4::Context->set_preference('CircControl', 'ItemHomeLibrary');
49 is(
50     C4::Context->preference('CircControl'),
51     'ItemHomeLibrary',
52     'CircControl changed to ItemHomeLibrary'
53 );
54 is(
55     $item->{$HomeOrHoldingBranch},
56     C4::Circulation::_GetCircControlBranch($item, $borrower),
57     '_GetCircControlBranch returned item branch'
58 );
59
60 diag('Now, set a userenv');
61 C4::Context->_new_userenv('xxx');
62 C4::Context::set_userenv(0,0,0,'firstname','surname', 'CurrentBranch', 'CurrentBranchName', '', '', '');
63 is(C4::Context->userenv->{branch}, 'CurrentBranch', 'userenv set');
64
65 # Userenv set, PickupLibrary
66 C4::Context->set_preference('CircControl', 'PickupLibrary');
67 is(
68     C4::Context->preference('CircControl'),
69     'PickupLibrary',
70     'CircControl changed to PickupLibrary'
71 );
72 is(
73     C4::Circulation::_GetCircControlBranch($item, $borrower),
74     'CurrentBranch',
75     '_GetCircControlBranch returned current branch'
76 );
77
78 # Userenv set, PatronLibrary
79 C4::Context->set_preference('CircControl', 'PatronLibrary');
80 is(
81     C4::Context->preference('CircControl'),
82     'PatronLibrary',
83     'CircControl changed to PatronLibrary'
84 );
85 is(
86     C4::Circulation::_GetCircControlBranch($item, $borrower),
87     $borrower->{branchcode},
88     '_GetCircControlBranch returned borrower branch'
89 );
90
91 # Userenv set, ItemHomeLibrary
92 C4::Context->set_preference('CircControl', 'ItemHomeLibrary');
93 is(
94     C4::Context->preference('CircControl'),
95     'ItemHomeLibrary',
96     'CircControl changed to ItemHomeLibrary'
97 );
98 is(
99     C4::Circulation::_GetCircControlBranch($item, $borrower),
100     $item->{$HomeOrHoldingBranch},
101     '_GetCircControlBranch returned item branch'
102 );
103
104 # Reset initial configuration
105 C4::Context->set_preference('CircControl', $CircControl);
106 is(
107     C4::Context->preference('CircControl'),
108     $CircControl,
109     'CircControl reset to its initial value'
110 );
111
112 # Test C4::Circulation::ProcessOfflinePayment
113 my $sth = C4::Context->dbh->prepare("SELECT COUNT(*) FROM accountlines WHERE amount = '-123.45' AND accounttype = 'Pay'");
114 $sth->execute();
115 my ( $original_count ) = $sth->fetchrow_array();
116
117 C4::Context->dbh->do("INSERT INTO borrowers ( cardnumber, surname, firstname, categorycode, branchcode ) VALUES ( '99999999999', 'Hall', 'Kyle', 'S', 'MPL' )");
118
119 C4::Circulation::ProcessOfflinePayment({ cardnumber => '99999999999', amount => '123.45' });
120
121 $sth->execute();
122 my ( $new_count ) = $sth->fetchrow_array();
123
124 ok( $new_count == $original_count  + 1, 'ProcessOfflinePayment makes payment correctly' );
125
126 C4::Context->dbh->do("DELETE FROM accountlines WHERE borrowernumber IN ( SELECT borrowernumber FROM borrowers WHERE cardnumber = '99999999999' )");
127 C4::Context->dbh->do("DELETE FROM borrowers WHERE cardnumber = '99999999999'");