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