View | Details | Raw Unified | Return to bug 38936
Collapse All | Expand All

(-)a/t/db_dependent/Output.t (-1 / +167 lines)
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
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 => 2;
21
use Test::MockModule;
22
use Test::Warn;
23
24
use CGI qw ( -utf8 );
25
26
use t::lib::Mocks;
27
use t::lib::TestBuilder;
28
29
BEGIN {
30
    use_ok(
31
        'C4::Output',
32
        qw( redirect_if_opac_suppressed )
33
    );
34
}
35
36
my $builder = t::lib::TestBuilder->new();
37
my $schema  = Koha::Database->new()->schema();
38
39
subtest 'redirect_if_opac_suppressed() tests' => sub {
40
41
    plan tests => 2;
42
43
    $schema->storage->txn_begin;
44
45
    local *STDOUT;
46
    my $stdout;
47
48
    # variable controlling opac suppression status
49
    my $opac_suppressed;
50
51
    my $auth_mock = Test::MockModule->new('C4::Auth');
52
    $auth_mock->mock( 'safe_exit', sub { warn "safe_exit_called" } );
53
54
    my $biblio_mock = Test::MockModule->new('Koha::Biblio');
55
    $biblio_mock->mock( 'opac_suppressed', sub { return $opac_suppressed; } );
56
57
    my $biblio = $builder->build_sample_biblio();
58
59
    my $query = CGI->new();
60
61
    subtest 'not suppressed tests' => sub {
62
63
        plan tests => 2;
64
65
        open STDOUT, '>', \$stdout;
66
        $opac_suppressed = 0;
67
68
        my $warnings;
69
        local $SIG{__WARN__} = sub { $warnings = shift };
70
        redirect_if_opac_suppressed( $query, $biblio );
71
72
        is( $stdout, undef, 'No redirection if the biblio is not suppressed' );
73
        ok( !$warnings, "safe_exit not called" );
74
75
        close STDOUT;
76
    };
77
78
    subtest 'suppressed tests' => sub {
79
80
        plan tests => 11;
81
82
        $opac_suppressed = 1;
83
84
        {
85
            open STDOUT, '>', \$stdout;
86
            t::lib::Mocks::mock_preference( 'OpacSuppressionByIPRange', undef );
87
            t::lib::Mocks::mock_preference( 'OpacSuppressionRedirect',  1 );
88
89
            warning_is {
90
                redirect_if_opac_suppressed( $query, $biblio );
91
            }
92
            q{safe_exit_called},
93
                'Safe exit called on redirection';
94
95
            like( $stdout, qr{Status: 302 Found} );
96
            like( $stdout, qr{Location: /cgi-bin/koha/opac-blocked.pl} );
97
98
            undef $stdout;
99
100
            close STDOUT;
101
        }
102
103
        {
104
            open STDOUT, '>', \$stdout;
105
            t::lib::Mocks::mock_preference( 'OpacSuppressionByIPRange', undef );
106
            t::lib::Mocks::mock_preference( 'OpacSuppressionRedirect',  0 );
107
108
            warning_is {
109
                redirect_if_opac_suppressed( $query, $biblio );
110
            }
111
            q{safe_exit_called},
112
                'Safe exit called on redirection';
113
114
            like( $stdout, qr{Status: 302 Found} );
115
            like( $stdout, qr{Location: /cgi-bin/koha/errors/404.pl} );
116
117
            undef $stdout;
118
119
            close STDOUT;
120
        }
121
122
        # now IP ranges
123
        {
124
            open STDOUT, '>', \$stdout;
125
126
            t::lib::Mocks::mock_preference( 'OpacSuppressionByIPRange', '192.168' );
127
            t::lib::Mocks::mock_preference( 'OpacSuppressionRedirect',  0 );
128
129
            $ENV{REMOTE_ADDR} = '200.16.23.1';
130
131
            warning_is {
132
                redirect_if_opac_suppressed( $query, $biblio );
133
            }
134
            q{safe_exit_called},
135
                'Safe exit called on redirection';
136
137
            like( $stdout, qr{Status: 302 Found} );
138
            like( $stdout, qr{Location: /cgi-bin/koha/errors/404.pl} );
139
140
            undef $stdout;
141
142
            close STDOUT;
143
        }
144
145
        {
146
            open STDOUT, '>', \$stdout;
147
148
            t::lib::Mocks::mock_preference( 'OpacSuppressionByIPRange', '192.168' );
149
            t::lib::Mocks::mock_preference( 'OpacSuppressionRedirect',  0 );
150
151
            $ENV{REMOTE_ADDR} = '192.168.0.115';
152
153
            my $warnings;
154
            local $SIG{__WARN__} = sub { $warnings = shift };
155
            redirect_if_opac_suppressed( $query, $biblio );
156
157
            is( $stdout, undef, 'No redirection if the IP is on the range' );
158
            ok( !$warnings, "safe_exit not called" );
159
160
            undef $stdout;
161
162
            close STDOUT;
163
        }
164
    };
165
166
    $schema->storage->txn_rollback;
167
};

Return to bug 38936