Bug 19977: Open only .pref files in Local Use tab (sysprefs)
[koha.git] / t / db_dependent / Members / GetAllIssues.t
blob643320169cb3e240f8411fed039847c9a68a8688
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use Test::More tests => 16;
21 use Test::MockModule;
23 use t::lib::TestBuilder;
25 use C4::Biblio;
26 use C4::Items;
27 use C4::Members;
28 use C4::Circulation;
29 use Koha::Libraries;
30 use Koha::Patrons;
31 use MARC::Record;
33 my $schema = Koha::Database->schema;
34 my $dbh = C4::Context->dbh;
35 $schema->storage->txn_begin;
37 my $builder = t::lib::TestBuilder->new;
39 $dbh->do(q|DELETE FROM issues|);
40 $dbh->do(q|DELETE FROM borrowers|);
41 $dbh->do(q|DELETE FROM items|);
42 $dbh->do(q|DELETE FROM branches|);
43 $dbh->do(q|DELETE FROM biblio|);
44 $dbh->do(q|DELETE FROM categories|);
46 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
47 my $categorycode = $builder->build( { source => 'Category' } )->{categorycode};
48 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
50 my %item_infos = (
51 homebranch => $branchcode,
52 holdingbranch => $branchcode,
53 itype => $itemtype
56 my ($biblionumber1) = AddBiblio( MARC::Record->new, '' );
57 my $itemnumber1 =
58 AddItem( { barcode => '0101', %item_infos }, $biblionumber1 );
59 my $itemnumber2 =
60 AddItem( { barcode => '0102', %item_infos }, $biblionumber1 );
62 my ($biblionumber2) = AddBiblio( MARC::Record->new, '' );
63 my $itemnumber3 =
64 AddItem( { barcode => '0203', %item_infos }, $biblionumber2 );
66 my $borrowernumber1 =
67 AddMember( categorycode => $categorycode, branchcode => $branchcode );
68 my $borrowernumber2 =
69 AddMember( categorycode => $categorycode, branchcode => $branchcode );
70 my $borrower1 = Koha::Patrons->find( $borrowernumber1 )->unblessed;
71 my $borrower2 = Koha::Patrons->find( $borrowernumber2 )->unblessed;
73 my $module = new Test::MockModule('C4::Context');
74 $module->mock( 'userenv', sub { { branch => $branchcode } } );
76 my $issues = C4::Members::GetAllIssues();
77 is( $issues, undef, 'GetAllIssues without borrower number returns undef' );
79 $issues = C4::Members::GetAllIssues($borrowernumber1);
80 is( @$issues, 0, 'GetAllIssues returns the correct number of elements' );
81 $issues = C4::Members::GetAllIssues($borrowernumber2);
82 is( @$issues, 0, 'GetAllIssues returns the correct number of elements' );
84 AddIssue( $borrower1, '0101' );
85 $issues = C4::Members::GetAllIssues($borrowernumber1);
86 my $issues_with_order =
87 C4::Members::GetAllIssues( $borrowernumber1, 'date_due desc' );
88 is_deeply( $issues, $issues_with_order,
89 'The value by default for the argument order in GellAllIssues is date_due_desc'
91 is( @$issues, 1, 'GetAllIssues returns the correct number of elements' );
92 is( $issues->[0]->{itemnumber}, $itemnumber1, '' );
93 $issues = C4::Members::GetAllIssues($borrowernumber2);
94 is( @$issues, 0, 'GetAllIssues returns the correct number of elements' );
96 AddIssue( $borrower1, '0102' );
97 $issues = C4::Members::GetAllIssues($borrowernumber1);
98 is( @$issues, 2, 'GetAllIssues returns the correct number of elements' );
99 is( $issues->[0]->{itemnumber}, $itemnumber1, '' );
100 is( $issues->[1]->{itemnumber}, $itemnumber2, '' );
101 $issues = C4::Members::GetAllIssues($borrowernumber2);
102 is( @$issues, 0, 'GetAllIssues returns the correct number of elements' );
104 AddIssue( $borrower2, '0203' );
105 $issues = C4::Members::GetAllIssues($borrowernumber1);
106 is( @$issues, 2, 'GetAllIssues returns the correct number of elements' );
107 is( $issues->[0]->{itemnumber}, $itemnumber1, '' );
108 is( $issues->[1]->{itemnumber}, $itemnumber2, '' );
109 $issues = C4::Members::GetAllIssues($borrowernumber2);
110 is( @$issues, 1, 'GetAllIssues returns the correct number of elements' );
111 is( $issues->[0]->{itemnumber}, $itemnumber3, '' );
113 $schema->storage->txn_begin;