@@ -, +, @@ - Everybody has different data, so we cannot make any assumptions about - Tests have to clean up every change to the database using SQL - Transactions in tests happen to be committed sometimes, resulting in prove --norc \ --Mt::lib::Bootstrap=database,koha_test,marcflavour,UNIMARC @localhost use usually was untouched (you can edit the test file and comment the lines related to a transaction (txn_begin, txn_rollback)) --- .proverc | 1 + t/lib/Bootstrap.pm | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 .proverc create mode 100644 t/lib/Bootstrap.pm --- a/.proverc +++ a/.proverc @@ -0,0 +1, @@ +-Mt::lib::Bootstrap=database,koha_test --- a/t/lib/Bootstrap.pm +++ a/t/lib/Bootstrap.pm @@ -0,0 +1,72 @@ +package t::lib::Bootstrap; + +use Modern::Perl; + +use DBI; +use File::Temp qw( tempfile ); +use XML::LibXML; + +sub import { + my ($self, %args) = @_; + + unless (defined $args{database}) { + die "Test database is not defined"; + } + + $args{marcflavour} //= 'MARC21'; + + my $xml = XML::LibXML->load_xml(location => $ENV{KOHA_CONF}); + my $root = $xml->documentElement(); + my ($databaseElement) = $root->findnodes('//config/database'); + my $currentDatabase = $databaseElement->textContent(); + + if ($currentDatabase eq $args{database}) { + die "Test database is the same as database in KOHA_CONF, abort!"; + } + + $databaseElement->firstChild()->setData($args{database}); + + my ($fh, $filename) = tempfile('koha-conf.XXXXXX', TMPDIR => 1, UNLINK => 1); + $xml->toFH($fh); + + $ENV{KOHA_CONF} = $filename; + + require C4::Context; + C4::Context->import; + + require C4::Installer; + C4::Installer->import; + + require C4::Languages; + + my $host = C4::Context->config('hostname'); + my $port = C4::Context->config('port'); + my $database = C4::Context->config('database'); + my $user = C4::Context->config('user'); + my $pass = C4::Context->config('pass'); + + say "Create test database..."; + + my $dbh = DBI->connect("dbi:mysql:;host=$host;port=$port", $user, $pass, { + RaiseError => 1, + PrintError => 0, + }); + + $dbh->do("DROP DATABASE IF EXISTS $database"); + $dbh->do("CREATE DATABASE $database"); + + my $installer = C4::Installer->new(); + $installer->load_db_schema(); + $installer->set_marcflavour_syspref($args{marcflavour}); + my (undef, $fwklist) = $installer->marc_framework_sql_list('en', $args{marcflavour}); + my @frameworks; + foreach my $fwk (@$fwklist) { + foreach my $framework (@{ $fwk->{frameworks} }) { + push @frameworks, $framework->{fwkfile}; + } + } + my $all_languages = C4::Languages::getAllLanguages(); + $installer->load_sql_in_order($all_languages, @frameworks); +} + +1; --