|
Lines 19-211
Link Here
|
| 19 |
|
19 |
|
| 20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
| 21 |
|
21 |
|
| 22 |
use File::Slurp qw(read_file); |
|
|
| 23 |
use C4::Context; |
| 24 |
use Array::Utils qw(array_minus); |
22 |
use Array::Utils qw(array_minus); |
| 25 |
|
23 |
|
| 26 |
use Test::NoWarnings; |
24 |
use Test::NoWarnings; |
| 27 |
use Test::More tests => 3; |
25 |
use Test::More tests => 3; |
| 28 |
|
26 |
|
|
|
27 |
use C4::Context; |
| 28 |
|
| 29 |
use Koha::Devel::Sysprefs; |
| 30 |
use Koha::Config::SysPrefs; |
| 31 |
|
| 29 |
our $dbh = C4::Context->dbh; |
32 |
our $dbh = C4::Context->dbh; |
| 30 |
my $intranetdir = C4::Context->config('intranetdir'); |
33 |
my $intranetdir = C4::Context->config('intranetdir'); |
| 31 |
my $root_dir = $intranetdir . '/installer/data/mysql/mandatory'; |
|
|
| 32 |
my $syspref_filepath = "$root_dir/sysprefs.sql"; |
| 33 |
|
34 |
|
| 34 |
my @lines = read_file($syspref_filepath) or die "Can't open $syspref_filepath: $!"; |
35 |
my @exceptions = qw( |
| 35 |
my @sysprefs_in_file = get_sysprefs_from_file(@lines); |
36 |
marcflavour |
|
|
37 |
ElasticsearchIndexStatus_authorities |
| 38 |
ElasticsearchIndexStatus_biblios |
| 39 |
OPACdidyoumean |
| 40 |
UsageStatsID |
| 41 |
UsageStatsLastUpdateTime |
| 42 |
UsageStatsPublicID |
| 43 |
); |
| 44 |
|
| 45 |
my @sysprefs_in_sql_file = Koha::Devel::Sysprefs->new->get_sysprefs_from_file(); |
| 36 |
|
46 |
|
| 37 |
subtest 'Compare database with sysprefs.sql file' => sub { |
47 |
subtest 'Compare database with sysprefs.sql file' => sub { |
| 38 |
ok( scalar(@sysprefs_in_file), "Found sysprefs" ); |
48 |
ok( scalar(@sysprefs_in_sql_file), "Found sysprefs" ); |
| 39 |
|
49 |
|
| 40 |
check_db(@sysprefs_in_file); |
50 |
check_db(@sysprefs_in_sql_file); |
| 41 |
}; |
51 |
}; |
| 42 |
|
52 |
|
| 43 |
subtest 'Compare sysprefs.sql with YAML files' => sub { |
53 |
subtest 'Compare sysprefs.sql with YAML files' => sub { |
| 44 |
plan tests => 2; |
54 |
plan tests => 2; |
| 45 |
|
55 |
|
| 46 |
my $yaml_prefs = get_syspref_from_yaml(); |
56 |
my $yaml_prefs = Koha::Config::SysPrefs->get_all_from_yml; |
| 47 |
my @yaml_mod = @$yaml_prefs; |
57 |
my @syspref_names_in_yaml_files = keys %$yaml_prefs; |
| 48 |
@yaml_mod = grep !/marcflavour/, @yaml_mod; # Added by web installer |
58 |
@syspref_names_in_yaml_files = array_minus @syspref_names_in_yaml_files, @exceptions; |
| 49 |
|
59 |
|
| 50 |
my @syspref_names_in_file = map { $_->{variable} } @sysprefs_in_file; |
60 |
my @syspref_names_in_sql_file = map { $_->{variable} } @sysprefs_in_sql_file; |
| 51 |
@syspref_names_in_file = grep !/ElasticsearchIndexStatus_authorities/, |
61 |
@syspref_names_in_sql_file = array_minus @syspref_names_in_sql_file, @exceptions; |
| 52 |
@syspref_names_in_file; # Not to be changed manually |
62 |
|
| 53 |
@syspref_names_in_file = grep !/ElasticsearchIndexStatus_biblios/, |
63 |
my @missing_yaml = array_minus( @syspref_names_in_sql_file, @syspref_names_in_yaml_files ); |
| 54 |
@syspref_names_in_file; # Not to be changed manually |
|
|
| 55 |
@syspref_names_in_file = grep !/OPACdidyoumean/, @syspref_names_in_file; # Separate configuration page |
| 56 |
@syspref_names_in_file = grep !/UsageStatsID/, @syspref_names_in_file; # Separate configuration page |
| 57 |
@syspref_names_in_file = grep !/UsageStatsLastUpdateTime/, @syspref_names_in_file; # Separate configuration page |
| 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 ); |
| 61 |
is( scalar @missing_yaml, 0, "No system preference entries missing from sysprefs.sql" ); |
64 |
is( scalar @missing_yaml, 0, "No system preference entries missing from sysprefs.sql" ); |
| 62 |
if ( scalar @missing_yaml > 0 ) { |
65 |
if ( scalar @missing_yaml > 0 ) { |
| 63 |
diag "System preferences missing from YAML:\n * " . join( "\n * ", @missing_yaml ) . "\n"; |
66 |
diag "System preferences missing from YAML:\n * " . join( "\n * ", @missing_yaml ) . "\n"; |
| 64 |
} |
67 |
} |
| 65 |
|
68 |
|
| 66 |
my @missing_sysprefs = array_minus( @yaml_mod, @syspref_names_in_file ); |
69 |
my @missing_sysprefs = array_minus( @syspref_names_in_yaml_files, @syspref_names_in_sql_file ); |
| 67 |
is( scalar @missing_sysprefs, 0, "No system preference entries missing from YAML files" ); |
70 |
is( scalar @missing_sysprefs, 0, "No system preference entries missing from YAML files" ); |
| 68 |
if ( scalar @missing_sysprefs > 0 ) { |
71 |
if ( scalar @missing_sysprefs > 0 ) { |
| 69 |
diag "System preferences missing from sysprefs.sql:\n * " . join( "\n * ", @missing_sysprefs ) . "\n"; |
72 |
diag "System preferences missing from sysprefs.sql:\n * " . join( "\n * ", @missing_sysprefs ) . "\n"; |
| 70 |
} |
73 |
} |
| 71 |
}; |
74 |
}; |
| 72 |
|
75 |
|
| 73 |
# Get sysprefs from SQL file populating sysprefs table with INSERT statement. |
|
|
| 74 |
# |
| 75 |
# Example: |
| 76 |
# INSERT INTO `systempreferences` (variable,value,explanation,options,type) |
| 77 |
# VALUES('AmazonLocale','US','Use to set the Locale of your Amazon.com Web Services', |
| 78 |
# 'US|CA|DE|FR|JP|UK','Choice') |
| 79 |
# |
| 80 |
sub get_sysprefs_from_file { |
| 81 |
my @lines = @_; |
| 82 |
my @sysprefs; |
| 83 |
for my $line (@lines) { |
| 84 |
chomp $line; |
| 85 |
next if $line =~ /^INSERT INTO /; # first line |
| 86 |
next if $line =~ /^;$/; # last line |
| 87 |
next if $line =~ /^--/; # Comment line |
| 88 |
if ( |
| 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; |
| 111 |
} |
| 112 |
|
| 113 |
# FIXME Explode if already exists? |
| 114 |
push @sysprefs, { |
| 115 |
variable => $variable, |
| 116 |
value => $value, |
| 117 |
options => $options, |
| 118 |
explanation => $explanation, |
| 119 |
type => $type, |
| 120 |
}; |
| 121 |
} else { |
| 122 |
die "$line does not match"; |
| 123 |
} |
| 124 |
} |
| 125 |
return @sysprefs; |
| 126 |
} |
| 127 |
|
| 128 |
# Get system preferences from YAML files |
| 129 |
sub get_syspref_from_yaml { |
| 130 |
my @prefs; |
| 131 |
foreach my $file ( glob( $intranetdir . "/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/*.pref" ) ) { |
| 132 |
if ( open( my $fh, '<:encoding(UTF-8)', $file ) ) { |
| 133 |
while ( my $row = <$fh> ) { |
| 134 |
chomp $row; |
| 135 |
my $pref; |
| 136 |
if ( $row =~ /pref: (.*)/ ) { |
| 137 |
$pref = $1; |
| 138 |
$pref =~ s/["']//ig; |
| 139 |
push @prefs, $pref; |
| 140 |
} |
| 141 |
} |
| 142 |
} else { |
| 143 |
warn "Could not open file '$file' $!"; |
| 144 |
} |
| 145 |
} |
| 146 |
return \@prefs; |
| 147 |
} |
| 148 |
|
| 149 |
sub check_db { |
76 |
sub check_db { |
| 150 |
my @sysprefs_from_file = @_; |
77 |
my @sysprefs_from_file = @_; |
| 151 |
|
78 |
|
| 152 |
# FIXME FrameworksLoaded is a temporary syspref created during the installation process |
79 |
# FIXME FrameworksLoaded is a temporary syspref created during the installation process |
| 153 |
# We should either rewrite the code to avoid its need, or delete it once the installation is finished. |
80 |
# We should either rewrite the code to avoid its need, or delete it once the installation is finished. |
| 154 |
my $sysprefs_in_db = $dbh->selectall_hashref( |
81 |
my $sysprefs_in_db = $dbh->selectall_arrayref( |
| 155 |
q{ |
82 |
q{ |
| 156 |
SELECT * from systempreferences |
83 |
SELECT * from systempreferences |
| 157 |
WHERE variable <> 'FrameworksLoaded' |
84 |
WHERE variable NOT IN ('marcflavour', 'Version', 'FrameworksLoaded') |
| 158 |
}, 'variable' |
85 |
ORDER BY variable |
|
|
86 |
}, { Slice => {} } |
| 159 |
); |
87 |
); |
| 160 |
|
88 |
|
|
|
89 |
my $yaml_prefs = Koha::Config::SysPrefs->get_all_from_yml; |
| 90 |
|
| 161 |
# Checking the number of sysprefs in the database |
91 |
# Checking the number of sysprefs in the database |
| 162 |
my @syspref_names_in_db = keys %$sysprefs_in_db; |
92 |
my @syspref_names_in_db = map { $_->{variable} } @$sysprefs_in_db; |
| 163 |
my @syspref_names_in_file = map { $_->{variable} } @sysprefs_in_file; |
93 |
my @syspref_names_in_sql_file = map { $_->{variable} } @sysprefs_in_sql_file; |
| 164 |
my @diff = array_minus @syspref_names_in_db, @syspref_names_in_file; |
94 |
my @diff = array_minus @syspref_names_in_db, @syspref_names_in_sql_file; |
| 165 |
is_deeply( [ sort @diff ], [ 'Version', 'marcflavour' ] ) |
95 |
is( scalar(@diff), 0 ) |
| 166 |
or diag sprintf( "Too many sysprefs in DB: %s", join ", ", @diff ); |
96 |
or diag sprintf( "Too many sysprefs in DB: %s", join ", ", @diff ); |
| 167 |
|
97 |
|
| 168 |
my @sorted_names_in_file = sort { |
98 |
is_deeply( \@syspref_names_in_sql_file, \@syspref_names_in_db, 'Syspref in sysprefs.sql must be sorted by name' ); |
| 169 |
$b =~ s/_/ZZZ/g; # mysql sorts underscore last, if you modify this qa-test-tools will need adjustments |
99 |
for my $pref (@sysprefs_in_sql_file) { |
| 170 |
lc($a) cmp lc($b) |
100 |
my ($in_db) = grep { $_->{variable} eq $pref->{variable} } @$sysprefs_in_db; |
| 171 |
} @syspref_names_in_file; |
|
|
| 172 |
is_deeply( \@syspref_names_in_file, \@sorted_names_in_file, 'Syspref in sysprefs.sql must be sorted by name' ); |
| 173 |
for my $pref (@sysprefs_in_file) { |
| 174 |
my $in_db = $sysprefs_in_db->{ $pref->{variable} }; |
| 175 |
my %db_copy = %$in_db; |
101 |
my %db_copy = %$in_db; |
| 176 |
my %file_copy = %$pref; |
102 |
my %file_copy = %$pref; |
| 177 |
delete $db_copy{value}; |
103 |
delete $db_copy{value}; |
| 178 |
delete $file_copy{value}; |
104 |
delete $file_copy{value}; |
| 179 |
|
105 |
|
| 180 |
if ( $pref->{variable} =~ m{^ElasticsearchIndexStatus_} ) { |
106 |
delete $db_copy{options}; |
| 181 |
|
107 |
delete $db_copy{explanation}; |
| 182 |
# Exception for the 2 sysprefs ElasticsearchIndexStatus_authorities and ElasticsearchIndexStatus_biblios |
108 |
delete $db_copy{type}; |
| 183 |
# They do not have a type defined |
|
|
| 184 |
# Will deal with them on a follow-up bugs |
| 185 |
next; |
| 186 |
} |
| 187 |
|
109 |
|
| 188 |
# Do not compare values, they can differ (new vs existing installs) |
110 |
# Do not compare values, they can differ (new vs existing installs) |
| 189 |
is_deeply( \%db_copy, \%file_copy, sprintf "Comparing %s", $pref->{variable} ); |
111 |
is_deeply( \%db_copy, \%file_copy, sprintf "Comparing %s", $pref->{variable} ); |
| 190 |
if ( !defined $pref->{type} ) { |
112 |
if ( defined $in_db->{options} ) { |
| 191 |
fail( sprintf "%s does not have a type in file!", $pref->{variable} ); |
113 |
fail( sprintf "%s has 'options' set in DB, must be NULL!", $in_db->{variable} ); |
| 192 |
} |
114 |
} |
| 193 |
if ( !defined $in_db->{type} ) { |
115 |
if ( defined $in_db->{explanation} ) { |
| 194 |
fail( sprintf "%s does not have a type in DB!", $in_db->{variable} ); |
116 |
fail( sprintf "%s has 'explanation' set in DB, must be NULL!", $in_db->{variable} ); |
| 195 |
} |
117 |
} |
| 196 |
if ( $pref->{type} && $pref->{type} eq 'YesNo' ) { |
118 |
if ( defined $in_db->{type} ) { |
| 197 |
like( |
119 |
fail( sprintf "%s has 'type' set in DB, must be NULL!", $in_db->{variable} ); |
| 198 |
$pref->{value}, qr{^(0|1)$}, |
|
|
| 199 |
sprintf( "Pref %s must be 0 or 1, found=%s in file", $pref->{variable}, $pref->{value} ), |
| 200 |
); |
| 201 |
like( |
| 202 |
$in_db->{value}, qr{^(0|1)$}, |
| 203 |
sprintf( "Pref %s must be 0 or 1, found=%s in DB", $in_db->{variable}, $in_db->{value} ), |
| 204 |
); |
| 205 |
} |
120 |
} |
| 206 |
|
121 |
|
| 207 |
# TODO Check on valid 'type' |
122 |
next if grep { $_ eq $pref->{variable} } @exceptions; |
| 208 |
#like($pref->{type}, qr{^()$}); |
123 |
|
|
|
124 |
my $yaml_pref = $yaml_prefs->{ $pref->{variable} }; |
| 125 |
if ( $yaml_pref->{type} eq 'select' && ref( $yaml_pref->{choices} ) ) { |
| 126 |
my @choices = sort keys %{ $yaml_pref->{choices} }; |
| 127 |
if ( scalar(@choices) == 2 && $choices[0] eq "0" && $choices[1] eq "1" ) { |
| 128 |
like( |
| 129 |
$pref->{value}, qr{^(0|1)$}, |
| 130 |
sprintf( "Pref %s must be 0 or 1, found=%s in file", $pref->{variable}, $pref->{value} ), |
| 131 |
); |
| 132 |
like( |
| 133 |
$in_db->{value}, qr{^(0|1)$}, |
| 134 |
sprintf( "Pref %s must be 0 or 1, found=%s in DB", $in_db->{variable}, $in_db->{value} ), |
| 135 |
); |
| 136 |
|
| 137 |
} |
| 138 |
} |
| 209 |
} |
139 |
} |
| 210 |
} |
140 |
} |
| 211 |
|
141 |
|
| 212 |
- |
|
|