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