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