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;
22 use t::lib::TestBuilder;
32 my $schema = Koha::Database->new()->schema();
33 my $dbh = $schema->storage->dbh;
34 my $builder = t::lib::TestBuilder->new();
37 $dbh->{RaiseError} = 1;
38 $schema->storage->txn_begin();
40 my $itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
41 my $category = $builder->build({ source => 'Category' })->{ categorycode };
42 my $branch_1 = $builder->build({ source => 'Branch' });
43 my $branch_2 = $builder->build({ source => 'Branch' });
45 my $c4_context = Test::MockModule->new('C4::Context');
46 $c4_context->mock('userenv', sub {
47 { branch => $branch_1->{ branchcode } }
49 C4::Context->set_preference('item-level_itypes', '0');
51 my $biblionumber = create_biblio('Test 1', $itemtype);
53 barcode => 'GTI_BARCODE_001',
54 homebranch => $branch_1->{ branchcode },
58 $biblionumber = create_biblio('Test 2', $itemtype);
60 barcode => 'GTI_BARCODE_002',
61 homebranch => $branch_2->{ branchcode },
64 my $borrowernumber = AddMember(
66 categorycode => $category,
67 branchcode => $branch_1->{ branchcode }
69 my $borrower = GetMember(borrowernumber => $borrowernumber);
71 AddIssue($borrower, 'GTI_BARCODE_001');
72 AddIssue($borrower, 'GTI_BARCODE_002');
78 my @issues = GetTopIssues({count => 10, itemtype => $itemtype});
79 is(scalar @issues, 2);
80 is($issues[0]->{title}, 'Test 1');
81 is($issues[1]->{title}, 'Test 2');
83 @issues = GetTopIssues({count => 1, itemtype => $itemtype});
84 is(scalar @issues, 1);
85 is($issues[0]->{title}, 'Test 1');
87 @issues = GetTopIssues({count => 10, branch => $branch_2->{ branchcode }});
88 is(scalar @issues, 1);
89 is($issues[0]->{title}, 'Test 2');
91 @issues = GetTopIssues({count => 10, ccode => 'GTI_CCODE'});
92 is(scalar @issues, 1);
93 is($issues[0]->{title}, 'Test 1');
95 @issues = GetTopIssues({count => 10, itemtype => $itemtype, newness => 1});
96 is(scalar @issues, 2);
97 is($issues[0]->{title}, 'Test 1');
98 is($issues[1]->{title}, 'Test 2');
102 SET datecreated = DATE_SUB(datecreated, INTERVAL 2 DAY)
103 WHERE biblionumber = ?
104 }, undef, $biblionumber);
106 @issues = GetTopIssues({count => 10, itemtype => $itemtype, newness => 1});
107 is(scalar @issues, 1);
108 is($issues[0]->{title}, 'Test 1');
114 $schema->storage->txn_rollback();
117 my ($title, $itemtype) = @_;
119 my ($title_tag, $title_subfield) = GetMarcFromKohaField('biblio.title', '');
120 my ($it_tag, $it_subfield) = GetMarcFromKohaField('biblioitems.itemtype', '');
122 my $record = MARC::Record->new();
123 $record->append_fields(
124 MARC::Field->new($title_tag, ' ', ' ', $title_subfield => $title),
125 MARC::Field->new($it_tag, ' ', ' ', $it_subfield => $itemtype),
128 my ($biblionumber) = AddBiblio($record, '');
130 return $biblionumber;