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