7e5774ea1878e93457d2ba37ac76ada9adcef5da
[kivitendo-erp.git] / t / Support / TestSetup.pm
1 package Support::TestSetup;
2
3 use strict;
4
5 use Data::Dumper;
6 use CGI qw( -no_xhtml);
7 use IO::File;
8 use SL::Auth;
9 use SL::Form;
10 use SL::Locale;
11 use SL::LXDebug;
12 use Data::Dumper;
13 use SL::Layout::None;
14 use SL::LxOfficeConf;
15 use SL::InstanceConfiguration;
16 use SL::Request;
17
18 sub login {
19   $Data::Dumper::Sortkeys = 1;
20   $Data::Dumper::Indent   = 2;
21
22   SL::LxOfficeConf->read;
23
24   my $client = 'Unit-Tests';
25   my $login  = 'unittests';
26
27   package main;
28
29   $::lxdebug       = LXDebug->new(target => LXDebug::STDERR_TARGET);
30   $::lxdebug->disable_sub_tracing;
31   $::locale        = Locale->new($::lx_office_conf{system}->{language});
32   $::form          = Form->new;
33   $::auth          = SL::Auth->new(unit_tests_database => 1);
34   die "Cannot find client with ID or name '$client'" if !$::auth->set_client($client);
35
36   $::instance_conf = SL::InstanceConfiguration->new;
37   $::request       = SL::Request->new( cgi => CGI->new({}), layout => SL::Layout::None->new );
38
39   die 'cannot reach auth db'               unless $::auth->session_tables_present;
40
41   $::auth->restore_session;
42
43   require "bin/mozilla/common.pl";
44
45   die "cannot find user $login"            unless %::myconfig = $::auth->read_user(login => $login);
46
47   $::form->{login} = $login; # normaly implicit at login
48
49   die "cannot find locale for user $login" unless $::locale   = Locale->new($::myconfig{countrycode});
50
51   $SIG{__DIE__} = sub { Carp::confess( @_ ) } if $::lx_office_conf{debug}->{backtrace_on_die};
52
53   return 1;
54 }
55
56 sub templates_cache_writable {
57   my $dir = $::lx_office_conf{paths}->{userspath} . '/templates-cache';
58   return 1 if -w $dir;
59
60   # Try actually creating a file. Due to ACLs this might be possible
61   # even if the basic Unix permissions and Perl's -w test say
62   # otherwise.
63   my $file = "${dir}/.writetest";
64   my $out  = IO::File->new($file, "w") || return 0;
65   $out->close;
66   unlink $file;
67
68   return 1;
69 }
70
71 1;