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 |
|
37 |
my $plugin = Koha::Template::Plugin::Branches->new(); |
38 |
subtest 'when given no parameters' => sub { |
39 |
plan tests => 1; |
40 |
my $libraries = $plugin->all(); |
41 |
my $library_count = Koha::Libraries->search()->count(); |
42 |
|
43 |
is(scalar @$libraries, $library_count, 'We get all the branches'); |
44 |
}; |
45 |
|
46 |
subtest 'when given parameter "ip_limit"' => sub { |
47 |
plan tests => 4; |
48 |
t::lib::Mocks::mock_preference('StaffLoginRestrictLibraryByIP', ''); |
49 |
$ENV{REMOTE_ADDR} = '127.0.0.1'; |
50 |
my $library = $builder->build_object( { class => 'Koha::Libraries', value => { branchip => '127.0.0.2' } } ); |
51 |
my $libraries = $plugin->all({ ip_limit => 1 }); |
52 |
my $library_count = Koha::Libraries->search()->count(); |
53 |
|
54 |
is(scalar @$libraries, $library_count, 'We get all the libraries when ip_limit passed but StaffLoginRestrictLibraryIP not enabled'); |
55 |
|
56 |
t::lib::Mocks::mock_preference('StaffLoginRestrictLibraryByIP', '1'); |
57 |
$library_count = Koha::Libraries->search({ branchip => [undef,'127.0.0.1'] })->count(); |
58 |
$libraries = $plugin->all({ ip_limit => 1 }); |
59 |
is(scalar @$libraries, $library_count , 'We remove non-matching libraries when ip_limit passed and StaffLoginRestrictLibraryIP enabled'); |
60 |
|
61 |
$ENV{REMOTE_ADDR} = '127.0.0.2'; |
62 |
$libraries = $plugin->all({ ip_limit => 1 }); |
63 |
$library_count = Koha::Libraries->search({ branchip => [undef,'127.0.0.2'] })->count(); |
64 |
is(scalar @$libraries, $library_count, 'We get all the expected libraries when ip_limit passed and StaffLoginRestrictLibraryIP and IP matches'); |
65 |
|
66 |
$library->branchip("127.0.*.*"); |
67 |
$library_count = Koha::Libraries->search({ branchip => [undef,'127.0.0.2','127.0.*.*'] })->count(); |
68 |
is(scalar @$libraries, $library_count, 'We get all the expected libraries when ip_limit passed and StaffLoginRestrictLibraryIP and IP matches patternwise'); |
69 |
}; |
70 |
|
71 |
$schema->storage->txn_rollback; |
72 |
}; |