Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[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 => 21;
21 use Test::MockModule;
22 use t::lib::TestBuilder;
23
24 use C4::Circulation qw( CheckIfIssuedToPatron AddIssue );
25 use C4::Items;
26 use C4::Biblio qw( AddBiblio );
27 use Koha::Library;
28 use Koha::Patrons;
29 use MARC::Record;
30
31 my $schema = Koha::Database->schema;
32 $schema->storage->txn_begin;
33 my $dbh = C4::Context->dbh;
34
35 my $builder = t::lib::TestBuilder->new;
36
37 $dbh->do(q|DELETE FROM issues|);
38 $dbh->do(q|DELETE FROM items|);
39 $dbh->do(q|DELETE FROM borrowers|);
40 $dbh->do(q|DELETE FROM branches|);
41 $dbh->do(q|DELETE FROM biblio|);
42 $dbh->do(q|DELETE FROM items|);
43 $dbh->do(q|DELETE FROM categories|);
44
45
46 ## Create sample data
47 # Add a branch
48 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
49
50 # Add a category
51 my $categorycode = $builder->build( { source => 'Category' } )->{categorycode};
52
53 # Add an itemtype
54 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
55
56 my %item_info = (
57     homebranch    => $branchcode,
58     holdingbranch => $branchcode,
59     itype         => $itemtype
60 );
61
62 my ($biblionumber1) = AddBiblio(MARC::Record->new, '');
63 my $barcode1 = '0101';
64 Koha::Item->new({ barcode => $barcode1, %item_info, biblionumber => $biblionumber1 })->store;
65 my ($biblionumber2) = AddBiblio(MARC::Record->new, '');
66 my $barcode2 = '0202';
67 Koha::Item->new({ barcode => $barcode2, %item_info, biblionumber => $biblionumber2 })->store;
68
69 my $borrowernumber1 = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode})->store->borrowernumber;
70 my $borrowernumber2 = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode})->store->borrowernumber;
71 # FIXME following code must be simplified to use the Koha::Patron objects
72 my $borrower1 = Koha::Patrons->find( $borrowernumber1 )->unblessed;
73 my $borrower2 = Koha::Patrons->find( $borrowernumber2 )->unblessed;
74
75 my $module = Test::MockModule->new('C4::Context');
76 $module->mock('userenv', sub { { branch => $branchcode } });
77
78
79 my $check_if_issued = C4::Circulation::CheckIfIssuedToPatron();
80 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without argument returns undef' );
81 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron(undef, $biblionumber1);
82 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the borrower number returns undef' );
83 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, undef);
84 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the biblio number returns undef' );
85 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber1);
86 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns unef' );
87 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber2);
88 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
89 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber1);
90 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
91 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber2);
92 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
93
94 AddIssue($borrower1, '0101');
95 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron();
96 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without argument returns undef' );
97 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron(undef, $biblionumber1);
98 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the borrower number returns undef' );
99 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, undef);
100 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the biblio number returns undef' );
101 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber1);
102 is( $check_if_issued, 1, 'CheckIfIssuedToPatron returns true' );
103 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber2);
104 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
105 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber1);
106 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
107 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber2);
108 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
109
110 AddIssue($borrower2, '0202');
111 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron();
112 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without argument returns undef' );
113 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron(undef, $biblionumber1);
114 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the borrower number returns undef' );
115 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, undef);
116 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the biblio number returns undef' );
117 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber1);
118 is( $check_if_issued, 1, 'CheckIfIssuedToPatron returns true' );
119 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber2);
120 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
121 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber1);
122 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
123 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber2);
124 is( $check_if_issued, 1, 'CheckIfIssuedToPatron returns true' );