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>.
20 use Test::More tests => 14;
23 use t::lib::TestBuilder;
33 my $schema = Koha::Database->new()->schema();
34 my $dbh = $schema->storage->dbh;
35 my $builder = t::lib::TestBuilder->new();
38 $dbh->{RaiseError} = 1;
39 $schema->storage->txn_begin();
41 my $itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
42 my $category = $builder->build({ source => 'Category' })->{ categorycode };
43 my $branch_1 = $builder->build({ source => 'Branch' });
44 my $branch_2 = $builder->build({ source => 'Branch' });
46 my $c4_context = Test::MockModule->new('C4::Context');
47 $c4_context->mock('userenv', sub {
48 { branch => $branch_1->{ branchcode } }
50 t::lib::Mocks::mock_preference('item-level_itypes', '0');
52 my $biblionumber = create_biblio('Test 1', $itemtype);
54 biblionumber => $biblionumber,
55 barcode => 'GTI_BARCODE_001',
56 homebranch => $branch_1->{ branchcode },
60 $biblionumber = create_biblio('Test 2', $itemtype);
62 biblionumber => $biblionumber,
63 barcode => 'GTI_BARCODE_002',
64 homebranch => $branch_2->{ branchcode },
67 my $borrowernumber = Koha::Patron->new({
69 categorycode => $category,
70 branchcode => $branch_1->{ branchcode }
71 })->store->borrowernumber;
72 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
74 AddIssue($borrower, 'GTI_BARCODE_001');
75 AddIssue($borrower, 'GTI_BARCODE_002');
81 my @issues = GetTopIssues({count => 10, itemtype => $itemtype});
82 is(scalar @issues, 2);
83 is($issues[0]->{title}, 'Test 1');
84 is($issues[1]->{title}, 'Test 2');
86 @issues = GetTopIssues({count => 1, itemtype => $itemtype});
87 is(scalar @issues, 1);
88 is($issues[0]->{title}, 'Test 1');
90 @issues = GetTopIssues({count => 10, branch => $branch_2->{ branchcode }});
91 is(scalar @issues, 1);
92 is($issues[0]->{title}, 'Test 2');
94 @issues = GetTopIssues({count => 10, ccode => 'GTI_CCODE'});
95 is(scalar @issues, 1);
96 is($issues[0]->{title}, 'Test 1');
98 @issues = GetTopIssues({count => 10, itemtype => $itemtype, newness => 1});
99 is(scalar @issues, 2);
100 is($issues[0]->{title}, 'Test 1');
101 is($issues[1]->{title}, 'Test 2');
105 SET datecreated = DATE_SUB(datecreated, INTERVAL 2 DAY)
106 WHERE biblionumber = ?
107 }, undef, $biblionumber);
109 @issues = GetTopIssues({count => 10, itemtype => $itemtype, newness => 1});
110 is(scalar @issues, 1);
111 is($issues[0]->{title}, 'Test 1');
117 $schema->storage->txn_rollback();
120 my ($title, $itemtype) = @_;
122 my ($title_tag, $title_subfield) = GetMarcFromKohaField( 'biblio.title' );
123 my ($it_tag, $it_subfield) = GetMarcFromKohaField( 'biblioitems.itemtype' );
125 my $record = MARC::Record->new();
126 $record->append_fields(
127 MARC::Field->new($title_tag, ' ', ' ', $title_subfield => $title),
128 MARC::Field->new($it_tag, ' ', ' ', $it_subfield => $itemtype),
131 my ($biblionumber) = AddBiblio($record, '');
133 return $biblionumber;