Bug 33051: Make 22600075.pl idempotent
[koha.git] / t / Logger.t
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 C4::Context;
21 use Koha::Logger;
22 use t::lib::Mocks;
23
24 use File::Temp qw/tempfile/;
25 use Test::More tests => 1;
26 use Test::Warn;
27 use Test::Exception;
28
29 subtest 'Test01 -- Simple tests for Koha::Logger' => sub {
30     plan tests => 10;
31
32     my $ret;
33     t::lib::Mocks::mock_config('log4perl_conf', undef);
34
35     throws_ok { Koha::Logger->get } qr/Configuration not defined/, 'Logger did not init correctly without config';
36
37     my $log = mytempfile();
38     my $config_file = mytempfile( <<"HERE"
39 log4perl.logger.intranet = WARN, INTRANET
40 log4perl.appender.INTRANET=Log::Log4perl::Appender::File
41 log4perl.appender.INTRANET.filename=$log
42 log4perl.appender.INTRANET.mode=append
43 log4perl.appender.INTRANET.layout=PatternLayout
44 log4perl.appender.INTRANET.layout.ConversionPattern=[%d] [%p] %m %l%n
45 HERE
46     );
47
48     t::lib::Mocks::mock_config('log4perl_conf', $config_file);
49
50     my $login = getlogin || getpwuid($<) || q{};
51
52     SKIP: {
53         skip "Running as root user", 1 if $login eq 'root';
54
55         system("chmod 400 $log");
56         throws_ok { Koha::Logger->get } qr/Permission denied/, 'Logger did not init correctly without permission';
57         system("chmod 700 $log");
58     }
59
60     my $logger = Koha::Logger->get( { interface => 'intranet' } );
61     is( exists $logger->{logger}, 1, 'Log4perl config found');
62     is( $logger->warn('Message 1'), 1, '->warn returned a value' );
63     warning_is { $ret = $logger->catastrophe }
64                "ERROR: Unsupported method catastrophe",
65                "Undefined method raises warning";
66     is( $ret, undef, "'catastrophe' method undefined");
67
68     Koha::Logger->put_mdc( 'foo', 'bar' );
69     is( Koha::Logger->get_mdc( 'foo' ), 'bar', "MDC value via put_mdc is correct" );
70
71     Koha::Logger->put_mdc( 'foo', undef );
72     is( Koha::Logger->get_mdc( 'foo' ), undef, "Updated MDC value to undefined via put_mdc is correct" );
73
74     Koha::Logger->put_mdc( 'foo', 'baz' );
75     is( Koha::Logger->get_mdc( 'foo' ), 'baz', "Updated MDC value via put_mdc is correct" );
76
77     Koha::Logger->clear_mdc();
78     is( Koha::Logger->get_mdc( 'foo' ), undef, "MDC value was cleared by clear_mdc" );
79 };
80
81 sub mytempfile {
82     my ( $fh, $fn ) = tempfile( SUFFIX => '.logger.test', UNLINK => 1 );
83     print $fh $_[0]//'';
84     close $fh;
85     return $fn;
86 }
87