bd067d232b6c536ba843d217044947d4fa09f5dc
[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   my ($client, $login) = @_;
20
21   die 'need client and login' unless $client && $login;
22
23   package main;
24
25   $::lxdebug       = LXDebug->new(target => LXDebug::STDERR_TARGET);
26   $::lxdebug->disable_sub_tracing;
27   $::locale        = Locale->new($::lx_office_conf{system}->{language});
28   $::form          = Form->new;
29   $::auth          = SL::Auth->new;
30   die "Cannot find client with ID or name '$client'" if !$::auth->set_client($client);
31
32   $::instance_conf = SL::InstanceConfiguration->new;
33   $::request       = SL::Request->new( cgi => CGI->new({}), layout => SL::Layout::None->new );
34
35   die 'cannot reach auth db'               unless $::auth->session_tables_present;
36
37   $::auth->restore_session;
38
39   require "bin/mozilla/common.pl";
40
41   die "cannot find user $login"            unless %::myconfig = $::auth->read_user(login => $login);
42
43   $::form->{login} = $login; # normaly implicit at login
44
45   die "cannot find locale for user $login" unless $::locale   = Locale->new($::myconfig{countrycode});
46
47   $::instance_conf->init;
48
49   $SIG{__DIE__} = sub { Carp::confess( @_ ) } if $::lx_office_conf{debug}->{backtrace_on_die};
50
51   return 1;
52 }
53
54 sub login {
55   SL::LxOfficeConf->read;
56
57   my $login        = shift || $::lx_office_conf{testing}{login}        || 'demo';
58   my $client        = shift || $::lx_office_conf{testing}{client}      || '';
59   _login($client, $login);
60 }
61
62 sub templates_cache_writable {
63   my $dir = $::lx_office_conf{paths}->{userspath} . '/templates-cache';
64   return 1 if -w $dir;
65
66   # Try actually creating a file. Due to ACLs this might be possible
67   # even if the basic Unix permissions and Perl's -w test say
68   # otherwise.
69   my $file = "${dir}/.writetest";
70   my $out  = IO::File->new($file, "w") || return 0;
71   $out->close;
72   unlink $file;
73
74   return 1;
75 }
76
77 1;