13 use SL::InstanceConfiguration;
17 use Support::TestSetup;
26 my ($dbh, $query, %params) = @_;
29 return if $query->execute(@{ $params{bind} || [] });
30 BAIL_OUT($dbh->errstr);
33 return if $dbh->do($query, undef, @{ $params{bind} || [] });
35 BAIL_OUT($params{message} . ": " . $dbh->errstr) if $params{message};
36 BAIL_OUT("Query failed: " . $dbh->errstr . " ; query: $query");
39 sub verify_configuration {
40 SL::LxOfficeConf->read;
42 my %config = %{ $::lx_office_conf{'testing/database'} || {} };
43 my @unset = sort grep { !$config{$_} } qw(host port db user template);
45 BAIL_OUT("Missing entries in configuration in section [testing/database]: " . join(' ', @unset)) if @unset;
51 $SIG{__DIE__} = sub { Carp::confess( @_ ) } if $::lx_office_conf{debug}->{backtrace_on_die};
52 $::lxdebug = LXDebug->new(target => LXDebug::STDERR_TARGET);
53 $::lxdebug->disable_sub_tracing;
54 $::locale = Locale->new($::lx_office_conf{system}->{language});
55 $::form = Support::TestSetup->create_new_form;
56 $::auth = SL::Auth->new(unit_tests_database => 1);
57 $::locale = Locale->new('de');
58 $::instance_conf = SL::InstanceConfiguration->new;
59 $db_cfg = $::lx_office_conf{'testing/database'};
62 sub drop_and_create_database {
64 'dbi:Pg:dbname=' . $db_cfg->{template} . ';host=' . $db_cfg->{host} . ';port=' . $db_cfg->{port},
67 SL::DBConnect->get_options,
70 my $dbh_template = SL::DBConnect->connect(@dbi_options) || BAIL_OUT("No database connection to the template database: " . $DBI::errstr);
71 my $auth_dbh = $::auth->dbconnect(1);
74 dbg("Database exists; dropping");
75 $auth_dbh->disconnect;
77 dbh_do($dbh_template, "DROP DATABASE \"" . $db_cfg->{db} . "\"", message => "Database could not be dropped");
80 dbg("Creating database");
82 dbh_do($dbh_template, "CREATE DATABASE \"" . $db_cfg->{db} . "\" TEMPLATE \"" . $db_cfg->{template} . "\" ENCODING 'UNICODE'", message => "Database could not be created");
83 $dbh_template->disconnect;
88 ok(1, "Database has been setup sucessfully.");
93 my ($dbupdater, $control_or_file) = @_;
95 my $file = ref($control_or_file) ? ("sql/Pg-upgrade2" . ($dbupdater->{auth} ? "-auth" : "") . "/$control_or_file->{file}") : $control_or_file;
96 my $control = ref($control_or_file) ? $control_or_file : undef;
98 dbg("Applying $file");
100 my $error = $dbupdater->process_file($dbh, $file, $control);
102 BAIL_OUT("Error applying $file: $error") if $error;
105 sub create_initial_schema {
106 dbg("Creating initial schema");
109 'dbi:Pg:dbname=' . $db_cfg->{db} . ';host=' . $db_cfg->{host} . ';port=' . $db_cfg->{port},
112 SL::DBConnect->get_options(PrintError => 0, PrintWarn => 0),
115 $dbh = SL::DBConnect->connect(@dbi_options) || BAIL_OUT("Database connection failed: " . $DBI::errstr);
116 $::auth->{dbh} = $dbh;
117 my $dbupdater = SL::DBUpgrade2->new(form => $::form, return_on_error => 1, silent => 1);
118 my $coa = 'Germany-DATEV-SKR03EU';
120 apply_dbupgrade($dbupdater, "sql/lx-office.sql");
121 apply_dbupgrade($dbupdater, "sql/${coa}-chart.sql");
123 dbh_do($dbh, qq|UPDATE defaults SET coa = '${coa}', accounting_method = 'cash', profit_determination = 'income', inventory_system = 'periodic', curr = 'EUR'|);
124 dbh_do($dbh, qq|CREATE TABLE schema_info (tag TEXT, login TEXT, itime TIMESTAMP DEFAULT now(), PRIMARY KEY (tag))|);
127 sub create_initial_auth_schema {
128 dbg("Creating initial auth schema");
130 my $dbupdater = SL::DBUpgrade2->new(form => $::form, return_on_error => 1, auth => 1);
131 apply_dbupgrade($dbupdater, 'sql/auth_db.sql');
136 my $dbupdater = SL::DBUpgrade2->new(form => $::form, return_on_error => 1, auth => $params{auth});
137 my @unapplied_scripts = $dbupdater->unapplied_upgrade_scripts($dbh);
139 apply_dbupgrade($dbupdater, $_) for @unapplied_scripts;
142 sub create_client_user_and_employee {
143 dbg("Creating client, user, group and employee");
145 dbh_do($dbh, qq|DELETE FROM auth.clients|);
146 dbh_do($dbh, qq|INSERT INTO auth.clients (id, name, dbhost, dbport, dbname, dbuser, dbpasswd, is_default) VALUES (1, 'Unit-Tests', ?, ?, ?, ?, ?, TRUE)|,
147 bind => [ @{ $db_cfg }{ qw(host port db user password) } ]);
148 dbh_do($dbh, qq|INSERT INTO auth."user" (id, login) VALUES (1, 'unittests')|);
149 dbh_do($dbh, qq|INSERT INTO auth."group" (id, name) VALUES (1, 'Vollzugriff')|);
150 dbh_do($dbh, qq|INSERT INTO auth.clients_users (client_id, user_id) VALUES (1, 1)|);
151 dbh_do($dbh, qq|INSERT INTO auth.clients_groups (client_id, group_id) VALUES (1, 1)|);
152 dbh_do($dbh, qq|INSERT INTO auth.user_group (user_id, group_id) VALUES (1, 1)|);
155 default_printer_id => '',
156 template_format => '',
158 email => 'unit@tester',
160 dateformat => 'dd.mm.yy',
161 show_form_details => '',
162 name => 'Unit Tester',
164 hide_cvar_search_options => '',
165 numberformat => '1.000,00',
170 stylesheet => 'lx-office-erp.css',
171 mandatory_departments => 0,
175 my $sth = $dbh->prepare(qq|INSERT INTO auth.user_config (user_id, cfg_key, cfg_value) VALUES (1, ?, ?)|) || BAIL_OUT($dbh->errstr);
176 dbh_do($dbh, $sth, bind => [ $_, $config{$_} ]) for sort keys %config;
179 $sth = $dbh->prepare(qq|INSERT INTO auth.group_rights (group_id, "right", granted) VALUES (1, ?, TRUE)|) || BAIL_OUT($dbh->errstr);
180 dbh_do($dbh, $sth, bind => [ $_ ]) for sort $::auth->all_rights;
183 dbh_do($dbh, qq|INSERT INTO employee (id, login, name) VALUES (1, 'unittests', 'Unit Tester')|);
185 $::auth->set_client(1) || BAIL_OUT("\$::auth->set_client(1) failed");
186 %::myconfig = $::auth->read_user(login => 'unittests');
189 verify_configuration();
191 drop_and_create_database();
192 create_initial_schema();
193 create_initial_auth_schema();
194 apply_upgrades(auth => 1);
195 create_client_user_and_employee();