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

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

Return to bug 38936