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

(-)a/t/db_dependent/check_sysprefs.t (-77 / +111 lines)
Lines 19-56 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Carp;
22
use File::Slurp qw(read_file);
23
use Getopt::Long;
24
use C4::Context;
23
use C4::Context;
25
use Array::Utils qw(array_minus);
24
use Array::Utils qw(array_minus);
26
25
27
# When this option is set, no tests are performed.
28
# The missing sysprefs are displayed as sql inserts instead.
29
our $showsql = 0;
30
GetOptions( 'showsql' => \$showsql );
31
32
use Test::NoWarnings;
26
use Test::NoWarnings;
33
use Test::More tests => 3;
27
use Test::More tests => 3;
34
28
35
our $dbh = C4::Context->dbh;
29
our $dbh = C4::Context->dbh;
36
my $intranetdir       = C4::Context->config('intranetdir');
30
my $intranetdir      = C4::Context->config('intranetdir');
37
my $root_dir          = $intranetdir . '/installer/data/mysql/mandatory';
31
my $root_dir         = $intranetdir . '/installer/data/mysql/mandatory';
38
my $base_syspref_file = "sysprefs.sql";
32
my $syspref_filepath = "$root_dir/sysprefs.sql";
39
33
40
open my $ref_fh, '<', "$root_dir/$base_syspref_file" or croak "Can't open '$root_dir/$base_syspref_file': $!";
34
my @lines            = read_file($syspref_filepath) or die "Can't open $syspref_filepath: $!";
41
my $ref_syspref  = get_syspref_from_file($ref_fh);
35
my $sysprefs_in_file = get_sysprefs_from_file(@lines);
42
my @ref_sysprefs = sort { lc $a cmp lc $b } keys %$ref_syspref;
43
my $num_sysprefs = scalar @ref_sysprefs;
44
36
45
subtest 'Compare database with sysprefs.sql file' => sub {
37
subtest 'Compare database with sysprefs.sql file' => sub {
46
    if ( !$showsql ) {
38
    ok( scalar( keys %$sysprefs_in_file ), "Found sysprefs" );
47
        cmp_ok(
48
            $num_sysprefs, '>', 0,
49
            "Found $num_sysprefs sysprefs"
50
        );
51
    }
52
39
53
    check_db($ref_syspref);
40
    check_db($sysprefs_in_file);
54
};
41
};
55
42
56
subtest 'Compare sysprefs.sql with YAML files' => sub {
43
subtest 'Compare sysprefs.sql with YAML files' => sub {
Lines 60-87 subtest 'Compare sysprefs.sql with YAML files' => sub { Link Here
60
    my @yaml_mod   = @$yaml_prefs;
47
    my @yaml_mod   = @$yaml_prefs;
61
    @yaml_mod = grep !/marcflavour/, @yaml_mod;    # Added by web installer
48
    @yaml_mod = grep !/marcflavour/, @yaml_mod;    # Added by web installer
62
49
63
    my @sysprefs_mod = @ref_sysprefs;
50
    my @syspref_names_in_file = keys %$sysprefs_in_file;
64
    @sysprefs_mod = grep !/ElasticsearchIndexStatus_authorities/, @sysprefs_mod;    # Not to be changed manually
51
    @syspref_names_in_file = grep !/ElasticsearchIndexStatus_authorities/,
65
    @sysprefs_mod = grep !/ElasticsearchIndexStatus_biblios/,     @sysprefs_mod;    # Not to be changed manually
52
        @syspref_names_in_file;                    # Not to be changed manually
66
    @sysprefs_mod = grep !/OPACdidyoumean/,                       @sysprefs_mod;    # Separate configuration page
53
    @syspref_names_in_file = grep !/ElasticsearchIndexStatus_biblios/,
67
    @sysprefs_mod = grep !/UsageStatsID/,                         @sysprefs_mod;    # Separate configuration page
54
        @syspref_names_in_file;                    # Not to be changed manually
68
    @sysprefs_mod = grep !/UsageStatsLastUpdateTime/,             @sysprefs_mod;    # Separate configuration page
55
    @syspref_names_in_file = grep !/OPACdidyoumean/,           @syspref_names_in_file;    # Separate configuration page
69
    @sysprefs_mod = grep !/UsageStatsPublicID/,                   @sysprefs_mod;    # Separate configuration page
56
    @syspref_names_in_file = grep !/UsageStatsID/,             @syspref_names_in_file;    # Separate configuration page
70
57
    @syspref_names_in_file = grep !/UsageStatsLastUpdateTime/, @syspref_names_in_file;    # Separate configuration page
71
    my @missing_yaml = array_minus( @sysprefs_mod, @yaml_mod );
58
    @syspref_names_in_file = grep !/UsageStatsPublicID/,       @syspref_names_in_file;    # Separate configuration page
59
60
    my @missing_yaml = array_minus( @syspref_names_in_file, @yaml_mod );
72
    is( scalar @missing_yaml, 0, "No system preference entries missing from sysprefs.sql" );
61
    is( scalar @missing_yaml, 0, "No system preference entries missing from sysprefs.sql" );
73
    if ( scalar @missing_yaml > 0 ) {
62
    if ( scalar @missing_yaml > 0 ) {
74
        diag "System preferences missing from YAML:\n  * " . join( "\n  * ", @missing_yaml ) . "\n";
63
        diag "System preferences missing from YAML:\n  * " . join( "\n  * ", @missing_yaml ) . "\n";
75
    }
64
    }
76
65
77
    my @missing_sysprefs = array_minus( @yaml_mod, @sysprefs_mod );
66
    my @missing_sysprefs = array_minus( @yaml_mod, @syspref_names_in_file );
78
    is( scalar @missing_sysprefs, 0, "No system preference entries missing from YAML files" );
67
    is( scalar @missing_sysprefs, 0, "No system preference entries missing from YAML files" );
79
    if ( scalar @missing_sysprefs > 0 ) {
68
    if ( scalar @missing_sysprefs > 0 ) {
80
        diag "System preferences missing from sysprefs.sql:\n  * " . join( "\n  * ", @missing_sysprefs ) . "\n";
69
        diag "System preferences missing from sysprefs.sql:\n  * " . join( "\n  * ", @missing_sysprefs ) . "\n";
81
    }
70
    }
82
};
71
};
83
72
84
#
85
# Get sysprefs from SQL file populating sysprefs table with INSERT statement.
73
# Get sysprefs from SQL file populating sysprefs table with INSERT statement.
86
#
74
#
87
# Example:
75
# Example:
Lines 89-108 subtest 'Compare sysprefs.sql with YAML files' => sub { Link Here
89
# VALUES('AmazonLocale','US','Use to set the Locale of your Amazon.com Web Services',
77
# VALUES('AmazonLocale','US','Use to set the Locale of your Amazon.com Web Services',
90
# 'US|CA|DE|FR|JP|UK','Choice')
78
# 'US|CA|DE|FR|JP|UK','Choice')
91
#
79
#
92
sub get_syspref_from_file {
80
sub get_sysprefs_from_file {
93
    my $fh = shift;
81
    my @lines = @_;
94
    my %syspref;
82
    my $sysprefs;
95
    while (<$fh>) {
83
    for my $line (@lines) {
96
        next if /^--/;    # Comment line
84
        chomp $line;
97
        my $query = $_;
85
        next if $line =~ /^INSERT INTO /;    # first line
98
        if ( $_ =~ /\([\s]*\'([\w\-:]+)\'/ ) {
86
        next if $line =~ /^;$/;              # last line
99
            my $variable = $1;
87
        next if $line =~ /^--/;              # Comment line
100
            if ($variable) {
88
        if (
101
                $syspref{$variable} = $query;
89
            $line =~ m/
90
            '(?<variable>[^'\\]*(?:\\.[^'\\]*)*)',\s*
91
            '(?<value>[^'\\]*(?:\\.[^'\\]*)*)',\s*
92
            (?<options>NULL|'(?<options_content>[^'\\]*(?:\\.[^'\\]*)*)'),\s*
93
            (?<explanation>NULL|'(?<explanation_content>[^'\\]*(?:\\.[^'\\]*)*)'),\s*
94
            (?<type>NULL|'(?<type_content>[^'\\]*(?:\\.[^'\\]*)*)')
95
        /xms
96
            )
97
        {
98
            my $variable    = $+{variable};
99
            my $value       = $+{value};
100
            my $options     = $+{options_content};
101
            my $explanation = $+{explanation_content};
102
            my $type        = $+{type_content};
103
104
            if ($options) {
105
                $options =~ s/\\'/'/g;
106
                $options =~ s/\\\\/\\/g;
107
            }
108
            if ($explanation) {
109
                $explanation =~ s/\\'/'/g;
110
                $explanation =~ s/\\n/\n/g;
102
            }
111
            }
112
113
            # FIXME Explode if already exists?
114
            $sysprefs->{$variable} = {
115
                variable    => $variable,
116
                value       => $value,
117
                options     => $options,
118
                explanation => $explanation,
119
                type        => $type,
120
            };
121
        } else {
122
            die "$line does not match";
103
        }
123
        }
104
    }
124
    }
105
    return \%syspref;
125
    return $sysprefs;
106
}
126
}
107
127
108
#  Get system preferences from YAML files
128
#  Get system preferences from YAML files
Lines 127-167 sub get_syspref_from_yaml { Link Here
127
}
147
}
128
148
129
sub check_db {
149
sub check_db {
130
    my $sysprefs = shift;
150
    my @sysprefs_from_file = @_;
151
152
    my $sysprefs_in_db = $dbh->selectall_hashref(
153
        q{
154
        SELECT * from systempreferences
155
    }, 'variable'
156
    );
131
157
132
    # Checking the number of sysprefs in the database
158
    # Checking the number of sysprefs in the database
133
    my $query = "SELECT COUNT(*) FROM systempreferences";
159
    my @syspref_names_in_db   = keys %$sysprefs_in_db;
134
    my $sth   = $dbh->prepare($query);
160
    my @syspref_names_in_file = keys %$sysprefs_in_file;
135
    $sth->execute;
161
    my @diff                  = array_minus @syspref_names_in_db, @syspref_names_in_file;
136
    my $res     = $sth->fetchrow_arrayref;
162
    is_deeply( [ sort @diff ], [ 'Version', 'marcflavour' ] )
137
    my $dbcount = $res->[0];
163
        or diag sprintf( "Too many sysprefs in DB: %s", join ", ", @diff );
138
    if ( !$showsql ) {
164
139
        cmp_ok(
165
    for my $pref ( sort values %$sysprefs_in_file ) {
140
            $dbcount, ">=", scalar( keys %$sysprefs ),
166
        my $in_db     = $sysprefs_in_db->{ $pref->{variable} };
141
            "There are at least as many sysprefs in the database as in the sysprefs.sql"
167
        my %db_copy   = %$in_db;
142
        );
168
        my %file_copy = %$pref;
143
    }
169
        delete $db_copy{value};
170
        delete $file_copy{value};
171
172
        if ( $pref->{variable} =~ m{^ElasticsearchIndexStatus_} ) {
173
174
            # Exception for the 2 sysprefs ElasticsearchIndexStatus_authorities and ElasticsearchIndexStatus_biblios
175
            # They do not have a type defined
176
            # Will deal with them on a follow-up bugs
177
            next;
178
        }
144
179
145
    # Checking for missing sysprefs in the database
180
        # Do not compare values, they can differ (new vs existing installs)
146
    $query = "SELECT COUNT(*) FROM systempreferences WHERE variable=?";
181
        is_deeply( \%db_copy, \%file_copy, sprintf "Comparing %s", $pref->{variable} );
147
    $sth   = $dbh->prepare($query);
182
        if ( !defined $pref->{type} ) {
148
    foreach ( keys %$sysprefs ) {
183
            fail( sprintf "%s does not have a type in file!", $pref->{variable} );
149
        $sth->execute($_);
184
        }
150
        my $res   = $sth->fetchrow_arrayref;
185
        if ( !defined $in_db->{type} ) {
151
        my $count = $res->[0];
186
            fail( sprintf "%s does not have a type in DB!", $in_db->{variable} );
152
        if ( !$showsql ) {
187
        }
153
            is( $count, 1, "Syspref $_ exists in the database" );
188
        if ( $pref->{type} && $pref->{type} eq 'YesNo' ) {
154
        } else {
189
            like(
155
            if ( $count != 1 ) {
190
                $pref->{value}, qr{^(0|1)$},
156
                print $sysprefs->{$_};
191
                sprintf( "Pref %s must be 0 or 1, found=%s in file", $pref->{variable}, $pref->{value} ),
157
            }
192
            );
193
            like(
194
                $in_db->{value}, qr{^(0|1)$},
195
                sprintf( "Pref %s must be 0 or 1, found=%s in DB", $in_db->{variable}, $in_db->{value} ),
196
            );
158
        }
197
        }
198
199
        # TODO Check on valid 'type'
200
        #like($pref->{type}, qr{^()$});
159
    }
201
    }
160
}
202
}
161
203
162
=head1 NAME
204
=head1 NAME
163
205
164
syspref.t
206
check_sysprefs.t
165
207
166
=head1 DESCRIPTION
208
=head1 DESCRIPTION
167
209
Lines 172-182 System prefereces are gathered from the installation and YAML files. Link Here
172
The database is then queried to check if all the system preferneces are
214
The database is then queried to check if all the system preferneces are
173
in it.
215
in it.
174
216
175
=head1 USAGE
176
177
prove -v xt/check_sysprefs.t
178
179
If you want to display the missing sysprefs as sql inserts :
180
perl check_sysprefs.t --showsql
181
182
=cut
217
=cut
183
- 

Return to bug 41682