View | Details | Raw Unified | Return to bug 10337
Collapse All | Expand All

(-)a/t/db_dependent/000-init_db.t (-1 / +115 lines)
Line 0 Link Here
0
- 
1
use Modern::Perl;
2
use Test::More tests => 37;
3
4
use C4::Context;
5
use t::lib::Mocks;
6
7
t::lib::Mocks::mock_config('database', 'koha_ut');
8
9
10
my $koha_structure_file = C4::Context->config('intranetdir') . '/installer/data/mysql/kohastructure.sql';
11
my $sample_files_optional = C4::Context->config('intranetdir') . '/installer/data/mysql/en/optional/*.sql';
12
my $sample_files_mandatory = C4::Context->config('intranetdir') . '/installer/data/mysql/en/mandatory/*.sql';
13
my $marc21_sample_files_mandatory = C4::Context->config('intranetdir') . '/installer/data/mysql/en/marcflavour/marc21/*/*.sql';
14
my $unimarc_sample_files_mandatory = C4::Context->config('intranetdir') . '/installer/data/mysql/en/marcflavour/unimarc/mandatory/*.sql';
15
my $sample_only_param_tables = C4::Context->config('intranetdir') . '/installer/data/mysql/sample_only_param_tables.sql';
16
my $sysprefs = C4::Context->config('intranetdir') . '/installer/data/mysql/sysprefs.sql';
17
my $subtag_registry = C4::Context->config('intranetdir') . '/installer/data/mysql/mandatory/subtag_registry.sql';
18
19
my $version = get_version();
20
21
our $dbh = C4::Context->dbh;
22
our $user = C4::Context->config('user');
23
our $pass = C4::Context->config('pass');
24
our $dbname = C4::Context->config("database");
25
26
# TODO All unit tests launch after this one should use the same database name
27
# instead of the dbname defined in the $KOHA_CONF file
28
# We have to mock C4::Context->config('database') with koha_ut everywhere
29
die q{The SQL database name should be 'koha_ut'}
30
    if $dbname ne 'koha_ut';
31
32
recreate_db( $dbname );
33
# Force C4::Context to recreate a new db handler
34
$dbh->disconnect;
35
$dbh = C4::Context->dbh;
36
initialize_data();
37
update_database();
38
39
40
# We can't insert unimarc AND marc21 files
41
#for my $f ( glob $unimarc_sample_files_mandatory ) {
42
#    execute_sqlfile( $f, "inserting $f");
43
#}
44
45
46
# FIXME This file crashes
47
#execute_sqlfile( $sample_only_param_tables, "inserting $sample_only_param_tables" );
48
49
sub recreate_db {
50
    my $dbname = shift;
51
    ok( $dbh->do(qq{
52
        DROP DATABASE $dbname
53
    }), "drop database $dbname" );
54
55
    is( $dbh->do(qq{
56
        CREATE DATABASE $dbname CHARACTER SET utf8 COLLATE utf8_bin
57
    }), 1, "create database $dbname" );
58
}
59
60
sub initialize_data {
61
    execute_sqlfile( $koha_structure_file, "inserting koha db structure" );
62
63
    for my $f ( glob $sample_files_mandatory ) {
64
        execute_sqlfile( $f, "inserting $f");
65
    }
66
67
68
    for my $f ( glob $sample_files_optional ) {
69
        execute_sqlfile( $f, "inserting $f");
70
    }
71
72
    for my $f ( glob $marc21_sample_files_mandatory ) {
73
        execute_sqlfile( $f, "inserting $f");
74
    }
75
    execute_sqlfile( $sysprefs, "inserting $sysprefs");
76
    execute_sqlfile( $subtag_registry, "inserting $subtag_registry");
77
78
    # set marcflavour (MARC21)
79
    $dbh->do( q{
80
        INSERT INTO `systempreferences` (variable,value,explanation,options,type)
81
        VALUES ('marcflavour','MARC21','Define global MARC flavor (MARC21 or UNIMARC) used for character encoding','MARC21|UNIMARC','Choice')
82
    } );
83
84
    # set version
85
    $dbh->do( qq{
86
        INSERT INTO systempreferences(variable, value, options, explanation, type)
87
        VALUES ('Version', '$version', NULL, 'The Koha database version. WARNING: Do not change this value manually, it is maintained by the webinstaller', NULL)
88
    } );
89
}
90
91
sub execute_sqlfile {
92
    my ( $filepath, $msg ) = @_;
93
    is( system( qq{/usr/bin/mysql -u $user -p'$pass' -D $dbname < $filepath} ), 0, $msg );
94
}
95
96
sub get_version {
97
    do C4::Context->config('intranetdir') . '/kohaversion.pl';
98
    my $version = kohaversion();
99
    $version =~ s/(\d)\.(\d{2})\.(\d{2})\.(\d{3})/$1.$2$3$4/;
100
    return $version;
101
}
102
103
sub update_database {
104
    my $src_path = C4::Context->config('intranetdir');
105
    my $update_db_path = $src_path . '/installer/data/mysql/updatedatabase.pl';
106
107
    my $file = `cat $update_db_path`;
108
    $file =~ s/exit;//;
109
    eval $file;
110
    if ($@) {
111
        fail("updatedatabase.pl process failed: $@");
112
    } else {
113
        pass("updatedatabase.pl process succeeded.");
114
    }
115
}

Return to bug 10337