Line 0
Link Here
|
0 |
- |
1 |
package t::lib::Bootstrap; |
|
|
2 |
|
3 |
use Modern::Perl; |
4 |
|
5 |
use DBI; |
6 |
use File::Temp qw( tempfile ); |
7 |
use XML::LibXML; |
8 |
|
9 |
sub import { |
10 |
my ($self, %args) = @_; |
11 |
|
12 |
unless (defined $args{database}) { |
13 |
die "Test database is not defined"; |
14 |
} |
15 |
|
16 |
$args{marcflavour} //= 'MARC21'; |
17 |
|
18 |
my $xml = XML::LibXML->load_xml(location => $ENV{KOHA_CONF}); |
19 |
my $root = $xml->documentElement(); |
20 |
my ($databaseElement) = $root->findnodes('//config/database'); |
21 |
my $currentDatabase = $databaseElement->textContent(); |
22 |
|
23 |
if ($currentDatabase eq $args{database}) { |
24 |
die "Test database is the same as database in KOHA_CONF, abort!"; |
25 |
} |
26 |
|
27 |
$databaseElement->firstChild()->setData($args{database}); |
28 |
|
29 |
my ($fh, $filename) = tempfile('koha-conf.XXXXXX', TMPDIR => 1, UNLINK => 1); |
30 |
$xml->toFH($fh); |
31 |
|
32 |
$ENV{KOHA_CONF} = $filename; |
33 |
|
34 |
require C4::Context; |
35 |
C4::Context->import; |
36 |
|
37 |
require C4::Installer; |
38 |
C4::Installer->import; |
39 |
|
40 |
require C4::Languages; |
41 |
|
42 |
my $host = C4::Context->config('hostname'); |
43 |
my $port = C4::Context->config('port'); |
44 |
my $database = C4::Context->config('database'); |
45 |
my $user = C4::Context->config('user'); |
46 |
my $pass = C4::Context->config('pass'); |
47 |
|
48 |
say "Create test database..."; |
49 |
|
50 |
my $dbh = DBI->connect("dbi:mysql:;host=$host;port=$port", $user, $pass, { |
51 |
RaiseError => 1, |
52 |
PrintError => 0, |
53 |
}); |
54 |
|
55 |
$dbh->do("DROP DATABASE IF EXISTS $database"); |
56 |
$dbh->do("CREATE DATABASE $database"); |
57 |
|
58 |
my $installer = C4::Installer->new(); |
59 |
$installer->load_db_schema(); |
60 |
$installer->set_marcflavour_syspref($args{marcflavour}); |
61 |
my (undef, $fwklist) = $installer->marc_framework_sql_list('en', $args{marcflavour}); |
62 |
my @frameworks; |
63 |
foreach my $fwk (@$fwklist) { |
64 |
foreach my $framework (@{ $fwk->{frameworks} }) { |
65 |
push @frameworks, $framework->{fwkfile}; |
66 |
} |
67 |
} |
68 |
my $all_languages = C4::Languages::getAllLanguages(); |
69 |
$installer->load_sql_in_order($all_languages, @frameworks); |
70 |
} |
71 |
|
72 |
1; |