Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / t / 000setup_database.t
index 5cf6a28..4e739b7 100755 (executable)
@@ -14,8 +14,9 @@ use SL::InstanceConfiguration;
 use SL::LXDebug;
 use SL::Layout::None;
 use SL::LxOfficeConf;
+use Support::TestSetup;
 
-our ($db_cfg, $dbh);
+our ($db_cfg, $dbh, $superuser_dbh);
 
 sub dbg {
   # diag(@_);
@@ -51,7 +52,7 @@ sub setup {
   $::lxdebug       = LXDebug->new(target => LXDebug::STDERR_TARGET);
   $::lxdebug->disable_sub_tracing;
   $::locale        = Locale->new($::lx_office_conf{system}->{language});
-  $::form          = Form->new;
+  $::form          = Support::TestSetup->create_new_form;
   $::auth          = SL::Auth->new(unit_tests_database => 1);
   $::locale        = Locale->new('de');
   $::instance_conf = SL::InstanceConfiguration->new;
@@ -66,7 +67,6 @@ sub drop_and_create_database {
     SL::DBConnect->get_options,
   );
 
-  $::auth->reset;
   my $dbh_template = SL::DBConnect->connect(@dbi_options) || BAIL_OUT("No database connection to the template database: " . $DBI::errstr);
   my $auth_dbh     = $::auth->dbconnect(1);
 
@@ -75,8 +75,6 @@ sub drop_and_create_database {
     $auth_dbh->disconnect;
 
     dbh_do($dbh_template, "DROP DATABASE \"" . $db_cfg->{db} . "\"", message => "Database could not be dropped");
-
-    $::auth->reset;
   }
 
   dbg("Creating database");
@@ -87,7 +85,8 @@ sub drop_and_create_database {
 
 sub report_success {
   $dbh->disconnect;
-  ok(1, "Database has been setup sucessfully.");
+  $superuser_dbh->disconnect if $superuser_dbh;
+  ok(1, "Database has been set up successfully.");
   done_testing();
 }
 
@@ -99,7 +98,8 @@ sub apply_dbupgrade {
 
   dbg("Applying $file");
 
-  my $error = $dbupdater->process_file($dbh, $file, $control);
+  my $script_dbh = $control && $control->{superuser_privileges} ? ($superuser_dbh // $dbh) : $dbh;
+  my $error      = $dbupdater->process_file($script_dbh, $file, $control);
 
   BAIL_OUT("Error applying $file: $error") if $error;
 }
@@ -119,6 +119,17 @@ sub create_initial_schema {
   my $dbupdater  = SL::DBUpgrade2->new(form => $::form, return_on_error => 1, silent => 1);
   my $coa        = 'Germany-DATEV-SKR03EU';
 
+  if ($db_cfg->{superuser_user} && ($db_cfg->{superuser_user} ne $db_cfg->{user})) {
+    @dbi_options = (
+      'dbi:Pg:dbname=' . $db_cfg->{db} . ';host=' . $db_cfg->{host} . ';port=' . $db_cfg->{port},
+      $db_cfg->{superuser_user},
+      $db_cfg->{superuser_password},
+      SL::DBConnect->get_options(PrintError => 0, PrintWarn => 0),
+    );
+
+    $superuser_dbh = SL::DBConnect->connect(@dbi_options) || BAIL_OUT("Database superuser connection failed: " . $DBI::errstr);
+  }
+
   apply_dbupgrade($dbupdater, "sql/lx-office.sql");
   apply_dbupgrade($dbupdater, "sql/${coa}-chart.sql");
 
@@ -165,7 +176,6 @@ sub create_client_user_and_employee {
     signature                => '',
     hide_cvar_search_options => '',
     numberformat             => '1.000,00',
-    vclimit                  => 0,
     favorites                => '',
     copies                   => '',
     menustyle                => 'v3',