Bug 23463: Replace AddItem calls with Koha::Item->store
[koha.git] / t / db_dependent / Template / Plugin / Branches.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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, see <http://www.gnu.org/licenses>.
16
17 use Modern::Perl;
18
19 use Test::More tests => 3;
20 use Test::MockModule;
21
22 use C4::Context;
23 use C4::Biblio qw(AddBiblio);
24 use Koha::Database;
25
26 use Clone qw(clone);
27 use List::MoreUtils qw(any);
28
29 use t::lib::TestBuilder;
30 use t::lib::Mocks;
31
32 BEGIN {
33     use_ok('Koha::Template::Plugin::Branches');
34 }
35
36 my $schema  = Koha::Database->schema;
37 my $builder = t::lib::TestBuilder->new;
38
39 subtest 'all() tests' => sub {
40
41     plan tests => 16;
42
43     $schema->storage->txn_begin;
44
45     my $library = $builder->build({
46         source => 'Branch',
47         value => {
48             branchcode => 'MYLIBRARY',
49         }
50     });
51     my $another_library = $builder->build({
52         source => 'Branch',
53         value => {
54             branchcode => 'ANOTHERLIB',
55         }
56     });
57
58     my $plugin = Koha::Template::Plugin::Branches->new();
59     ok($plugin, "initialized Branches plugin");
60
61     my $name = $plugin->GetName($library->{branchcode});
62     is($name, $library->{branchname}, 'retrieved expected name for library');
63
64     $name = $plugin->GetName('__ANY__');
65     is($name, '', 'received empty string as name of the "__ANY__" placeholder library code');
66
67     $name = $plugin->GetName(undef);
68     is($name, '', 'received empty string as name of NULL/undefined library code');
69
70     $library = $plugin->GetLoggedInBranchcode();
71     is($library, '', 'no active library if there is no active user session');
72
73     t::lib::Mocks::mock_userenv({ branchcode => 'MYLIBRARY' });
74     $library = $plugin->GetLoggedInBranchcode();
75     is($library, 'MYLIBRARY', 'GetLoggedInBranchcode() returns active library');
76
77     t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
78     my $libraries = $plugin->all();
79     ok( scalar(@$libraries) > 1, 'If IndependentBranches is not set, all libraries should be returned' );
80     is( grep ( { $_->{branchcode} eq 'MYLIBRARY'  and $_->{selected} == 1 } @$libraries ),       1, 'Without selected parameter, my library should be preselected' );
81     is( grep ( { $_->{branchcode} eq 'ANOTHERLIB' and not exists $_->{selected} } @$libraries ), 1, 'Without selected parameter, other library should not be preselected' );
82     $libraries = $plugin->all( { selected => 'ANOTHERLIB' } );
83     is( grep ( { $_->{branchcode} eq 'MYLIBRARY'  and not exists $_->{selected} } @$libraries ), 1, 'With selected parameter, my library should not be preselected' );
84     is( grep ( { $_->{branchcode} eq 'ANOTHERLIB' and $_->{selected} == 1 } @$libraries ),       1, 'With selected parameter, other library should be preselected' );
85     $libraries = $plugin->all( { selected => '' } );
86     is( grep ( { exists $_->{selected} } @$libraries ), 0, 'With selected parameter set to an empty string, no library should be preselected' );
87
88     my $total = @{$plugin->all};
89     my $pickupable = @{$plugin->all( { search_params => { pickup_location => 1 } }) };
90     my $yet_another_library = $builder->build({
91         source => 'Branch',
92         value => {
93             branchcode => 'CANTPICKUP',
94             pickup_location => 0,
95         }
96     });
97     is(@{$plugin->all( { search_params => { pickup_location => 1 } }) }, $pickupable,
98        'Adding a new library with pickups'
99        .' disabled does not increase the amount returned by ->pickup_locations');
100     is(@{$plugin->all}, $total+1, 'However, adding a new library increases'
101        .' the total amount gotten with ->all');
102
103     t::lib::Mocks::mock_preference( 'IndependentBranches', 1 );
104     $libraries = $plugin->all();
105     is( scalar(@$libraries), 1, 'If IndependentBranches is set, only 1 library should be returned' );
106     $libraries = $plugin->all( { unfiltered => 1 } );
107     ok( scalar(@$libraries) > 1, 'If IndependentBranches is set, all libraries should be returned if the unfiltered flag is set' );
108
109     $schema->storage->txn_rollback;
110 };
111
112 subtest 'pickup_locations() tests' => sub {
113
114     plan tests => 8;
115
116     my $library_1 = { branchcode => 'A' };
117     my $library_2 = { branchcode => 'B' };
118     my $library_3 = { branchcode => 'C' };
119     my @library_array = ( $library_1, $library_2, $library_3 );
120
121     my $libraries = Test::MockModule->new('Koha::Libraries');
122     $libraries->mock(
123         'pickup_locations',
124         sub {
125             my $result = clone(\@library_array);
126             return @$result;
127         }
128     );
129
130     my $plugin           = Koha::Template::Plugin::Branches->new();
131     my $pickup_locations = $plugin->pickup_locations();
132
133     is( scalar @{$pickup_locations}, 3, 'Libraries count is correct' );
134     for my $library ( @{$pickup_locations} ) {
135         ok( ( any { $_->{branchcode} eq $library->{branchcode} } @library_array ),
136             'Library ' . $library->{branchcode} . ' in results' );
137     }
138
139     $pickup_locations = $plugin->pickup_locations({ selected => $library_2->{branchcode} });
140     my @selected = grep { exists $_->{selected} } @{ $pickup_locations };
141     is( scalar @selected, 1, '(param) Only one is selected' );
142     is( $selected[0]->{branchcode}, $library_2->{branchcode}, '(param) The selected one is the right one' );
143
144     t::lib::Mocks::mock_userenv({ branchcode => $library_3->{branchcode} });
145     $pickup_locations = $plugin->pickup_locations();
146     @selected = grep { exists $_->{selected} } @{ $pickup_locations };
147     is( scalar @selected, 1, '(userenv) Only one is selected' );
148     is( $selected[0]->{branchcode}, $library_3->{branchcode}, '(userenv) The selected one is the right one' );
149
150 };