Line 0
Link Here
|
0 |
- |
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 => 2; |
20 |
|
21 |
use t::lib::Mocks; |
22 |
use t::lib::TestBuilder; |
23 |
|
24 |
BEGIN { |
25 |
use_ok('Koha::Template::Plugin::Branches'); |
26 |
} |
27 |
|
28 |
my $schema = Koha::Database->schema; |
29 |
my $builder = t::lib::TestBuilder->new; |
30 |
|
31 |
subtest 'all' => sub { |
32 |
plan tests => 2; |
33 |
|
34 |
$schema->storage->txn_begin; |
35 |
|
36 |
my $plugin = Koha::Template::Plugin::Branches->new(); |
37 |
subtest 'when given no parameters' => sub { |
38 |
plan tests => 1; |
39 |
my $libraries = $plugin->all(); |
40 |
my $library_count = Koha::Libraries->search()->count(); |
41 |
|
42 |
is( scalar @$libraries, $library_count, 'We get all the branches' ); |
43 |
}; |
44 |
|
45 |
subtest 'when given parameter "ip_limit"' => sub { |
46 |
plan tests => 4; |
47 |
t::lib::Mocks::mock_preference( 'StaffLoginRestrictLibraryByIP', '' ); |
48 |
$ENV{REMOTE_ADDR} = '127.0.0.1'; |
49 |
my $library = $builder->build_object( { class => 'Koha::Libraries', value => { branchip => '127.0.0.2' } } ); |
50 |
my $libraries = $plugin->all( { ip_limit => 1 } ); |
51 |
my $library_count = Koha::Libraries->search()->count(); |
52 |
|
53 |
is( |
54 |
scalar @$libraries, $library_count, |
55 |
'We get all the libraries when ip_limit passed but StaffLoginRestrictLibraryIP not enabled' |
56 |
); |
57 |
|
58 |
t::lib::Mocks::mock_preference( 'StaffLoginRestrictLibraryByIP', '1' ); |
59 |
$library_count = Koha::Libraries->search( { branchip => [ undef, '127.0.0.1' ] } )->count(); |
60 |
$libraries = $plugin->all( { ip_limit => 1 } ); |
61 |
is( |
62 |
scalar @$libraries, $library_count, |
63 |
'We remove non-matching libraries when ip_limit passed and StaffLoginRestrictLibraryIP enabled' |
64 |
); |
65 |
|
66 |
$ENV{REMOTE_ADDR} = '127.0.0.2'; |
67 |
$libraries = $plugin->all( { ip_limit => 1 } ); |
68 |
$library_count = Koha::Libraries->search( { branchip => [ undef, '127.0.0.2' ] } )->count(); |
69 |
is( |
70 |
scalar @$libraries, $library_count, |
71 |
'We get all the expected libraries when ip_limit passed and StaffLoginRestrictLibraryIP and IP matches' |
72 |
); |
73 |
|
74 |
$library->branchip("127.0.*.*"); |
75 |
$library_count = Koha::Libraries->search( { branchip => [ undef, '127.0.0.2', '127.0.*.*' ] } )->count(); |
76 |
is( |
77 |
scalar @$libraries, $library_count, |
78 |
'We get all the expected libraries when ip_limit passed and StaffLoginRestrictLibraryIP and IP matches patternwise' |
79 |
); |
80 |
}; |
81 |
|
82 |
$schema->storage->txn_rollback; |
83 |
}; |