Bug 13757: (regression test) Attribute with value 0 should be stored
[koha.git] / t / Logger.t
blobcef4ea193c350924c8b3443a08ac8a7c9259ea76
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use C4::Context;
21 use Koha::Logger;
23 use File::Temp qw/tempfile/;
24 use Test::MockModule;
25 use Test::More tests => 1;
26 use Test::Warn;
28 subtest 'Test01 -- Simple tests for Koha::Logger' => sub {
29 plan tests => 8;
30 test01();
33 sub test01 {
35 my $ret;
36 my $mContext = new Test::MockModule('C4::Context');
37 $mContext->mock( 'config', sub { return; } );
39 my $logger= Koha::Logger->get;
40 is( exists $logger->{logger}, '', 'No log4perl config');
41 my $d= $logger->debug('Message 1');
42 is( $d, undef, 'No return value for debug call');
44 my $log = mytempfile();
45 my $conf = mytempfile( <<"HERE"
46 log4perl.logger.intranet = WARN, INTRANET
47 log4perl.appender.INTRANET=Log::Log4perl::Appender::File
48 log4perl.appender.INTRANET.filename=$log
49 log4perl.appender.INTRANET.mode=append
50 log4perl.appender.INTRANET.layout=PatternLayout
51 log4perl.appender.INTRANET.layout.ConversionPattern=[%d] [%p] %m %l %n
52 HERE
54 $mContext->mock( 'config', sub { return $conf; } );
55 $logger= Koha::Logger->get({ interface => 'intranet' });
56 is( exists $logger->{logger}, 1, 'Log4perl config found');
57 is( $logger->warn('Message 2'), 1, 'Message 2 returned a value' );
58 warning_is { $ret = $logger->catastrophe }
59 "ERROR: Unsupported method catastrophe",
60 "Undefined method raises warning";
61 is( $ret, undef, "'catastrophe' method undefined");
62 system("chmod 400 $log");
63 warnings_are { $ret = $logger->warn('Message 3') }
64 [ "Log file not writable for log4perl",
65 "warn: Message 3" ],
66 "Warnings raised if log file is not writeable";
67 is( $ret, undef, 'Message 3 returned undef' );
70 sub mytempfile {
71 my ( $fh, $fn ) = tempfile( SUFFIX => '.logger.test', UNLINK => 1 );
72 print $fh $_[0]//'';
73 close $fh;
74 return $fn;