Lines 16-28
Link Here
|
16 |
|
16 |
|
17 |
use Modern::Perl; |
17 |
use Modern::Perl; |
18 |
|
18 |
|
19 |
use Test::More tests => 18; |
19 |
use Test::More tests => 3; |
|
|
20 |
use Test::MockModule; |
20 |
|
21 |
|
21 |
use C4::Context; |
22 |
use C4::Context; |
22 |
use C4::Biblio qw(AddBiblio); |
23 |
use C4::Biblio qw(AddBiblio); |
23 |
use C4::Items qw(AddItem); |
24 |
use C4::Items qw(AddItem); |
24 |
use Koha::Database; |
25 |
use Koha::Database; |
25 |
|
26 |
|
|
|
27 |
use Clone qw(clone); |
28 |
use List::MoreUtils qw(any); |
29 |
|
26 |
use t::lib::TestBuilder; |
30 |
use t::lib::TestBuilder; |
27 |
use t::lib::Mocks; |
31 |
use t::lib::Mocks; |
28 |
|
32 |
|
Lines 30-175
BEGIN {
Link Here
|
30 |
use_ok('Koha::Template::Plugin::Branches'); |
34 |
use_ok('Koha::Template::Plugin::Branches'); |
31 |
} |
35 |
} |
32 |
|
36 |
|
33 |
my $schema = Koha::Database->schema; |
37 |
my $schema = Koha::Database->schema; |
34 |
$schema->storage->txn_begin; |
|
|
35 |
my $builder = t::lib::TestBuilder->new; |
38 |
my $builder = t::lib::TestBuilder->new; |
36 |
my $library = $builder->build({ |
39 |
|
37 |
source => 'Branch', |
40 |
subtest 'all() tests' => sub { |
38 |
value => { |
41 |
|
39 |
branchcode => 'MYLIBRARY', |
42 |
plan tests => 16; |
40 |
} |
43 |
|
41 |
}); |
44 |
$schema->storage->txn_begin; |
42 |
my $another_library = $builder->build({ |
45 |
|
43 |
source => 'Branch', |
46 |
my $library = $builder->build({ |
44 |
value => { |
47 |
source => 'Branch', |
45 |
branchcode => 'ANOTHERLIB', |
48 |
value => { |
46 |
} |
49 |
branchcode => 'MYLIBRARY', |
47 |
}); |
50 |
} |
48 |
|
51 |
}); |
49 |
my $plugin = Koha::Template::Plugin::Branches->new(); |
52 |
my $another_library = $builder->build({ |
50 |
ok($plugin, "initialized Branches plugin"); |
53 |
source => 'Branch', |
51 |
|
54 |
value => { |
52 |
my $name = $plugin->GetName($library->{branchcode}); |
55 |
branchcode => 'ANOTHERLIB', |
53 |
is($name, $library->{branchname}, 'retrieved expected name for library'); |
56 |
} |
54 |
|
57 |
}); |
55 |
$name = $plugin->GetName('__ANY__'); |
58 |
|
56 |
is($name, '', 'received empty string as name of the "__ANY__" placeholder library code'); |
59 |
my $plugin = Koha::Template::Plugin::Branches->new(); |
57 |
|
60 |
ok($plugin, "initialized Branches plugin"); |
58 |
$name = $plugin->GetName(undef); |
61 |
|
59 |
is($name, '', 'received empty string as name of NULL/undefined library code'); |
62 |
my $name = $plugin->GetName($library->{branchcode}); |
60 |
|
63 |
is($name, $library->{branchname}, 'retrieved expected name for library'); |
61 |
$library = $plugin->GetLoggedInBranchcode(); |
64 |
|
62 |
is($library, '', 'no active library if there is no active user session'); |
65 |
$name = $plugin->GetName('__ANY__'); |
63 |
|
66 |
is($name, '', 'received empty string as name of the "__ANY__" placeholder library code'); |
64 |
t::lib::Mocks::mock_userenv({ branchcode => 'MYLIBRARY' }); |
67 |
|
65 |
$library = $plugin->GetLoggedInBranchcode(); |
68 |
$name = $plugin->GetName(undef); |
66 |
is($library, 'MYLIBRARY', 'GetLoggedInBranchcode() returns active library'); |
69 |
is($name, '', 'received empty string as name of NULL/undefined library code'); |
67 |
|
70 |
|
68 |
t::lib::Mocks::mock_preference( 'IndependentBranches', 0 ); |
71 |
$library = $plugin->GetLoggedInBranchcode(); |
69 |
my $libraries = $plugin->all(); |
72 |
is($library, '', 'no active library if there is no active user session'); |
70 |
ok( scalar(@$libraries) > 1, 'If IndependentBranches is not set, all libraries should be returned' ); |
73 |
|
71 |
is( grep ( { $_->{branchcode} eq 'MYLIBRARY' and $_->{selected} == 1 } @$libraries ), 1, 'Without selected parameter, my library should be preselected' ); |
74 |
t::lib::Mocks::mock_userenv({ branchcode => 'MYLIBRARY' }); |
72 |
is( grep ( { $_->{branchcode} eq 'ANOTHERLIB' and not exists $_->{selected} } @$libraries ), 1, 'Without selected parameter, other library should not be preselected' ); |
75 |
$library = $plugin->GetLoggedInBranchcode(); |
73 |
$libraries = $plugin->all( { selected => 'ANOTHERLIB' } ); |
76 |
is($library, 'MYLIBRARY', 'GetLoggedInBranchcode() returns active library'); |
74 |
is( grep ( { $_->{branchcode} eq 'MYLIBRARY' and not exists $_->{selected} } @$libraries ), 1, 'With selected parameter, my library should not be preselected' ); |
77 |
|
75 |
is( grep ( { $_->{branchcode} eq 'ANOTHERLIB' and $_->{selected} == 1 } @$libraries ), 1, 'With selected parameter, other library should be preselected' ); |
78 |
t::lib::Mocks::mock_preference( 'IndependentBranches', 0 ); |
76 |
$libraries = $plugin->all( { selected => '' } ); |
79 |
my $libraries = $plugin->all(); |
77 |
is( grep ( { exists $_->{selected} } @$libraries ), 0, 'With selected parameter set to an empty string, no library should be preselected' ); |
80 |
ok( scalar(@$libraries) > 1, 'If IndependentBranches is not set, all libraries should be returned' ); |
78 |
|
81 |
is( grep ( { $_->{branchcode} eq 'MYLIBRARY' and $_->{selected} == 1 } @$libraries ), 1, 'Without selected parameter, my library should be preselected' ); |
79 |
my $total = @{$plugin->all}; |
82 |
is( grep ( { $_->{branchcode} eq 'ANOTHERLIB' and not exists $_->{selected} } @$libraries ), 1, 'Without selected parameter, other library should not be preselected' ); |
80 |
my $pickupable = @{$plugin->all( { search_params => { pickup_location => 1 } }) }; |
83 |
$libraries = $plugin->all( { selected => 'ANOTHERLIB' } ); |
81 |
my $yet_another_library = $builder->build({ |
84 |
is( grep ( { $_->{branchcode} eq 'MYLIBRARY' and not exists $_->{selected} } @$libraries ), 1, 'With selected parameter, my library should not be preselected' ); |
82 |
source => 'Branch', |
85 |
is( grep ( { $_->{branchcode} eq 'ANOTHERLIB' and $_->{selected} == 1 } @$libraries ), 1, 'With selected parameter, other library should be preselected' ); |
83 |
value => { |
86 |
$libraries = $plugin->all( { selected => '' } ); |
84 |
branchcode => 'CANTPICKUP', |
87 |
is( grep ( { exists $_->{selected} } @$libraries ), 0, 'With selected parameter set to an empty string, no library should be preselected' ); |
85 |
pickup_location => 0, |
88 |
|
86 |
} |
89 |
my $total = @{$plugin->all}; |
87 |
}); |
90 |
my $pickupable = @{$plugin->all( { search_params => { pickup_location => 1 } }) }; |
88 |
is(@{$plugin->all( { search_params => { pickup_location => 1 } }) }, $pickupable, |
91 |
my $yet_another_library = $builder->build({ |
89 |
'Adding a new library with pickups' |
92 |
source => 'Branch', |
90 |
.' disabled does not increase the amount returned by ->pickup_locations'); |
93 |
value => { |
91 |
is(@{$plugin->all}, $total+1, 'However, adding a new library increases' |
94 |
branchcode => 'CANTPICKUP', |
92 |
.' the total amount gotten with ->all'); |
95 |
pickup_location => 0, |
93 |
|
96 |
} |
94 |
t::lib::Mocks::mock_preference( 'IndependentBranches', 1 ); |
97 |
}); |
95 |
$libraries = $plugin->all(); |
98 |
is(@{$plugin->all( { search_params => { pickup_location => 1 } }) }, $pickupable, |
96 |
is( scalar(@$libraries), 1, 'If IndependentBranches is set, only 1 library should be returned' ); |
99 |
'Adding a new library with pickups' |
97 |
$libraries = $plugin->all( { unfiltered => 1 } ); |
100 |
.' disabled does not increase the amount returned by ->pickup_locations'); |
98 |
ok( scalar(@$libraries) > 1, 'If IndependentBranches is set, all libraries should be returned if the unfiltered flag is set' ); |
101 |
is(@{$plugin->all}, $total+1, 'However, adding a new library increases' |
99 |
|
102 |
.' the total amount gotten with ->all'); |
100 |
subtest 'UseBranchTransferLimits = OFF' => sub { |
103 |
|
101 |
plan tests => 5; |
104 |
t::lib::Mocks::mock_preference( 'IndependentBranches', 1 ); |
102 |
|
105 |
$libraries = $plugin->all(); |
103 |
my $from = Koha::Library->new({ |
106 |
is( scalar(@$libraries), 1, 'If IndependentBranches is set, only 1 library should be returned' ); |
104 |
branchcode => 'zzzfrom', |
107 |
$libraries = $plugin->all( { unfiltered => 1 } ); |
105 |
branchname => 'zzzfrom', |
108 |
ok( scalar(@$libraries) > 1, 'If IndependentBranches is set, all libraries should be returned if the unfiltered flag is set' ); |
106 |
branchnotes => 'zzzfrom', |
109 |
|
107 |
})->store; |
110 |
$schema->storage->txn_rollback; |
108 |
my $to = Koha::Library->new({ |
|
|
109 |
branchcode => 'zzzto', |
110 |
branchname => 'zzzto', |
111 |
branchnotes => 'zzzto', |
112 |
})->store; |
113 |
|
114 |
my ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY'); |
115 |
# Create item instance for testing. |
116 |
my ($item_bibnum1, $item_bibitemnum1, $itemnumber1) |
117 |
= AddItem({ homebranch => $from->branchcode, |
118 |
holdingbranch => $from->branchcode } , $bibnum); |
119 |
my ($item_bibnum2, $item_bibitemnum2, $itemnumber2) |
120 |
= AddItem({ homebranch => $from->branchcode, |
121 |
holdingbranch => $from->branchcode } , $bibnum); |
122 |
my ($item_bibnum3, $item_bibitemnum3, $itemnumber3) |
123 |
= AddItem({ homebranch => $from->branchcode, |
124 |
holdingbranch => $from->branchcode } , $bibnum); |
125 |
my $biblio = Koha::Biblios->find($bibnum); |
126 |
|
127 |
t::lib::Mocks::mock_preference('UseBranchTransferLimits', 0); |
128 |
t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype'); |
129 |
t::lib::Mocks::mock_preference('item-level_itypes', 1); |
130 |
Koha::Item::Transfer::Limits->delete; |
131 |
Koha::Item::Transfer::Limit->new({ |
132 |
fromBranch => $from->branchcode, |
133 |
toBranch => $to->branchcode, |
134 |
itemtype => $biblio->itemtype, |
135 |
})->store; |
136 |
my $total_pickup = Koha::Libraries->search({ |
137 |
pickup_location => 1 |
138 |
})->count; |
139 |
|
140 |
# Test TT plugin |
141 |
my $pickup = Koha::Template::Plugin::Branches::pickup_locations({ biblio => $bibnum }); |
142 |
is(C4::Context->preference('UseBranchTransferLimits'), 0, 'Given system ' |
143 |
.'preference UseBranchTransferLimits is switched OFF,'); |
144 |
is(@{$pickup}, $total_pickup, 'Then the total number of pickup locations ' |
145 |
.'equal number of libraries with pickup_location => 1'); |
146 |
|
147 |
t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype'); |
148 |
t::lib::Mocks::mock_preference('item-level_itypes', 1); |
149 |
$pickup = Koha::Template::Plugin::Branches::pickup_locations({ biblio => $bibnum }); |
150 |
is(@{$pickup}, $total_pickup, '...when ' |
151 |
.'BranchTransferLimitsType = itemtype and item-level_itypes = 1'); |
152 |
t::lib::Mocks::mock_preference('item-level_itypes', 0); |
153 |
$pickup = Koha::Template::Plugin::Branches::pickup_locations({ biblio => $bibnum }); |
154 |
is(@{$pickup}, $total_pickup, '...as well as when ' |
155 |
.'BranchTransferLimitsType = itemtype and item-level_itypes = 0'); |
156 |
t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode'); |
157 |
$pickup = Koha::Template::Plugin::Branches::pickup_locations({ biblio => $bibnum }); |
158 |
is(@{$pickup}, $total_pickup, '...as well as when ' |
159 |
.'BranchTransferLimitsType = ccode'); |
160 |
|
161 |
t::lib::Mocks::mock_preference('item-level_itypes', 1); |
162 |
}; |
111 |
}; |
163 |
|
112 |
|
164 |
sub create_helper_biblio { |
113 |
subtest 'pickup_locations() tests' => sub { |
165 |
my $itemtype = shift; |
114 |
|
166 |
my ($bibnum, $title, $bibitemnum); |
115 |
plan tests => 8; |
167 |
my $bib = MARC::Record->new(); |
116 |
|
168 |
$title = 'Silence in the library'; |
117 |
my $library_1 = { branchcode => 'A' }; |
169 |
$bib->append_fields( |
118 |
my $library_2 = { branchcode => 'B' }; |
170 |
MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'), |
119 |
my $library_3 = { branchcode => 'C' }; |
171 |
MARC::Field->new('245', ' ', ' ', a => $title), |
120 |
my @library_array = ( $library_1, $library_2, $library_3 ); |
172 |
MARC::Field->new('942', ' ', ' ', c => $itemtype), |
121 |
|
|
|
122 |
my $libraries = Test::MockModule->new('Koha::Libraries'); |
123 |
$libraries->mock( |
124 |
'pickup_locations', |
125 |
sub { |
126 |
my $result = clone(\@library_array); |
127 |
return $result; |
128 |
} |
173 |
); |
129 |
); |
174 |
return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, ''); |
130 |
|
175 |
} |
131 |
my $plugin = Koha::Template::Plugin::Branches->new(); |
|
|
132 |
my $pickup_locations = $plugin->pickup_locations(); |
133 |
|
134 |
is( scalar @{$pickup_locations}, 3, 'Libraries count is correct' ); |
135 |
for my $library ( @{$pickup_locations} ) { |
136 |
ok( ( any { $_->{branchcode} eq $library->{branchcode} } @library_array ), |
137 |
'Library ' . $library->{branchcode} . ' in results' ); |
138 |
} |
139 |
|
140 |
$pickup_locations = $plugin->pickup_locations({ selected => $library_2->{branchcode} }); |
141 |
my @selected = grep { exists $_->{selected} } @{ $pickup_locations }; |
142 |
is( scalar @selected, 1, '(param) Only one is selected' ); |
143 |
is( $selected[0]->{branchcode}, $library_2->{branchcode}, '(param) The selected one is the right one' ); |
144 |
|
145 |
t::lib::Mocks::mock_userenv({ branchcode => $library_3->{branchcode} }); |
146 |
$pickup_locations = $plugin->pickup_locations(); |
147 |
@selected = grep { exists $_->{selected} } @{ $pickup_locations }; |
148 |
is( scalar @selected, 1, '(userenv) Only one is selected' ); |
149 |
is( $selected[0]->{branchcode}, $library_3->{branchcode}, '(userenv) The selected one is the right one' ); |
150 |
|
151 |
}; |
176 |
- |
|
|