Bug 15407: Koha::Patron::Categories - replace C4::Category->all
[koha.git] / t / db_dependent / Circulation / CheckIfIssuedToPatron.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 22;
21 use Test::MockModule;
22
23 use C4::Biblio;
24 use C4::Items;
25 use C4::Members;
26 use Koha::Library;
27 use MARC::Record;
28
29 BEGIN {
30     use_ok('C4::Circulation');
31 }
32
33
34 my $dbh = C4::Context->dbh;
35 $dbh->{AutoCommit} = 0;
36 $dbh->{RaiseError} = 1;
37
38 $dbh->do(q|DELETE FROM issues|);
39 $dbh->do(q|DELETE FROM items|);
40 $dbh->do(q|DELETE FROM borrowers|);
41 $dbh->do(q|DELETE FROM branches|);
42 $dbh->do(q|DELETE FROM biblio|);
43 $dbh->do(q|DELETE FROM items|);
44 $dbh->do(q|DELETE FROM categories|);
45
46
47 my $branchcode = 'B';
48 Koha::Library->new( {branchcode => $branchcode, branchname => 'Branch' } )->store;
49
50 my $categorycode = 'C';
51 $dbh->do("INSERT INTO categories(categorycode) VALUES(?)", undef, $categorycode);
52
53 my %item_branch_infos = (
54     homebranch => $branchcode,
55     holdingbranch => $branchcode,
56 );
57
58 my ($biblionumber1) = AddBiblio(MARC::Record->new, '');
59 my $barcode1 = '0101';
60 AddItem({ barcode => $barcode1, %item_branch_infos }, $biblionumber1);
61 my ($biblionumber2) = AddBiblio(MARC::Record->new, '');
62 my $barcode2 = '0202';
63 AddItem({ barcode => $barcode2, %item_branch_infos }, $biblionumber2);
64
65 my $borrowernumber1 = AddMember(categorycode => $categorycode, branchcode => $branchcode);
66 my $borrowernumber2 = AddMember(categorycode => $categorycode, branchcode => $branchcode);
67 my $borrower1 = GetMember(borrowernumber => $borrowernumber1);
68 my $borrower2 = GetMember(borrowernumber => $borrowernumber2);
69
70 my $module = new Test::MockModule('C4::Context');
71 $module->mock('userenv', sub { { branch => $branchcode } });
72
73
74 my $check_if_issued = C4::Circulation::CheckIfIssuedToPatron();
75 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without argument returns undef' );
76 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron(undef, $biblionumber1);
77 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the borrower number returns undef' );
78 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, undef);
79 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the biblio number returns undef' );
80 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber1);
81 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns unef' );
82 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber2);
83 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
84 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber1);
85 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
86 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber2);
87 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
88
89 AddIssue($borrower1, '0101');
90 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron();
91 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without argument returns undef' );
92 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron(undef, $biblionumber1);
93 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the borrower number returns undef' );
94 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, undef);
95 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the biblio number returns undef' );
96 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber1);
97 is( $check_if_issued, 1, 'CheckIfIssuedToPatron returns true' );
98 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber2);
99 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
100 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber1);
101 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
102 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber2);
103 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
104
105 AddIssue($borrower2, '0202');
106 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron();
107 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without argument returns undef' );
108 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron(undef, $biblionumber1);
109 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the borrower number returns undef' );
110 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, undef);
111 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the biblio number returns undef' );
112 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber1);
113 is( $check_if_issued, 1, 'CheckIfIssuedToPatron returns true' );
114 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber2);
115 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
116 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber1);
117 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
118 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber2);
119 is( $check_if_issued, 1, 'CheckIfIssuedToPatron returns true' );
120
121 $dbh->rollback();
122
123 1;