Bug 22721: Remove frameworkcode parameter in GetMarcFromKohaField calls
[koha-equinox.git] / t / db_dependent / Circulation / GetTopIssues.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 => 14;
21 use Test::MockModule;
22 use t::lib::Mocks;
23 use t::lib::TestBuilder;
24
25 use C4::Context;
26 use C4::Circulation;
27 use C4::Biblio;
28 use C4::Items;
29
30 use Koha::Database;
31 use Koha::Patrons;
32
33 my $schema  = Koha::Database->new()->schema();
34 my $dbh     = $schema->storage->dbh;
35 my $builder = t::lib::TestBuilder->new();
36
37 # Start transaction
38 $dbh->{RaiseError} = 1;
39 $schema->storage->txn_begin();
40
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' });
45
46 my $c4_context = Test::MockModule->new('C4::Context');
47 $c4_context->mock('userenv', sub {
48     { branch => $branch_1->{ branchcode } }
49 });
50 t::lib::Mocks::mock_preference('item-level_itypes', '0');
51
52 my $biblionumber = create_biblio('Test 1', $itemtype);
53 AddItem({
54     barcode => 'GTI_BARCODE_001',
55     homebranch => $branch_1->{ branchcode },
56     ccode => 'GTI_CCODE',
57 }, $biblionumber);
58
59 $biblionumber = create_biblio('Test 2', $itemtype);
60 AddItem({
61     barcode => 'GTI_BARCODE_002',
62     homebranch => $branch_2->{ branchcode },
63 }, $biblionumber);
64
65 my $borrowernumber = Koha::Patron->new({
66     userid => 'gti.test',
67     categorycode => $category,
68     branchcode => $branch_1->{ branchcode }
69 })->store->borrowernumber;
70 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
71
72 AddIssue($borrower, 'GTI_BARCODE_001');
73 AddIssue($borrower, 'GTI_BARCODE_002');
74
75 #
76 # Start of tests
77 #
78
79 my @issues = GetTopIssues({count => 10, itemtype => $itemtype});
80 is(scalar @issues, 2);
81 is($issues[0]->{title}, 'Test 1');
82 is($issues[1]->{title}, 'Test 2');
83
84 @issues = GetTopIssues({count => 1, itemtype => $itemtype});
85 is(scalar @issues, 1);
86 is($issues[0]->{title}, 'Test 1');
87
88 @issues = GetTopIssues({count => 10, branch => $branch_2->{ branchcode }});
89 is(scalar @issues, 1);
90 is($issues[0]->{title}, 'Test 2');
91
92 @issues = GetTopIssues({count => 10, ccode => 'GTI_CCODE'});
93 is(scalar @issues, 1);
94 is($issues[0]->{title}, 'Test 1');
95
96 @issues = GetTopIssues({count => 10, itemtype => $itemtype, newness => 1});
97 is(scalar @issues, 2);
98 is($issues[0]->{title}, 'Test 1');
99 is($issues[1]->{title}, 'Test 2');
100
101 $dbh->do(q{
102     UPDATE biblio
103     SET datecreated = DATE_SUB(datecreated, INTERVAL 2 DAY)
104     WHERE biblionumber = ?
105 }, undef, $biblionumber);
106
107 @issues = GetTopIssues({count => 10, itemtype => $itemtype, newness => 1});
108 is(scalar @issues, 1);
109 is($issues[0]->{title}, 'Test 1');
110
111 #
112 # End of tests
113 #
114
115 $schema->storage->txn_rollback();
116
117 sub create_biblio {
118     my ($title, $itemtype) = @_;
119
120     my ($title_tag, $title_subfield) = GetMarcFromKohaField( 'biblio.title' );
121     my ($it_tag, $it_subfield) = GetMarcFromKohaField( 'biblioitems.itemtype' );
122
123     my $record = MARC::Record->new();
124     $record->append_fields(
125         MARC::Field->new($title_tag, ' ', ' ', $title_subfield => $title),
126         MARC::Field->new($it_tag, ' ', ' ', $it_subfield => $itemtype),
127     );
128
129     my ($biblionumber) = AddBiblio($record, '');
130
131     return $biblionumber;
132 }