3 # Copyright (C) 2010 BibLibre
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
25 use Array::Utils qw(array_minus);
27 # When this option is set, no tests are performed.
28 # The missing sysprefs are displayed as sql inserts instead.
30 GetOptions( 'showsql' => \$showsql );
32 use Test::More tests => 2;
34 our $dbh = C4::Context->dbh;
35 my $intranetdir = C4::Context->config('intranetdir');
36 my $root_dir = $intranetdir . '/installer/data/mysql/mandatory';
37 my $base_syspref_file = "sysprefs.sql";
39 open my $ref_fh, '<', "$root_dir/$base_syspref_file" or croak "Can't open '$root_dir/$base_syspref_file': $!";
40 my $ref_syspref = get_syspref_from_file($ref_fh);
41 my @ref_sysprefs = sort { lc $a cmp lc $b } keys %$ref_syspref;
42 my $num_sysprefs = scalar @ref_sysprefs;
44 subtest 'Compare database with sysprefs.sql file' => sub {
47 $num_sysprefs, '>', 0,
48 "Found $num_sysprefs sysprefs"
52 check_db($ref_syspref);
55 subtest 'Compare sysprefs.sql with YAML files' => sub {
58 my $yaml_prefs = get_syspref_from_yaml();
59 my @yaml_mod = @$yaml_prefs;
60 @yaml_mod = grep !/marcflavour/, @yaml_mod; # Added by web installer
62 my @sysprefs_mod = @ref_sysprefs;
63 @sysprefs_mod = grep !/ElasticsearchIndexStatus_authorities/, @sysprefs_mod; # Not to be changed manually
64 @sysprefs_mod = grep !/ElasticsearchIndexStatus_biblios/, @sysprefs_mod; # Not to be changed manually
65 @sysprefs_mod = grep !/OPACdidyoumean/, @sysprefs_mod; # Separate configuration page
66 @sysprefs_mod = grep !/UsageStatsID/, @sysprefs_mod; # Separate configuration page
67 @sysprefs_mod = grep !/UsageStatsLastUpdateTime/, @sysprefs_mod; # Separate configuration page
68 @sysprefs_mod = grep !/UsageStatsPublicID/, @sysprefs_mod; # Separate configuration page
70 my @missing_yaml = array_minus( @sysprefs_mod, @yaml_mod );
71 is( scalar @missing_yaml, 0, "No system preference entries missing from sysprefs.sql" );
72 if ( scalar @missing_yaml > 0 ) {
73 print "System preferences missing from YAML:\n * " . join( "\n * ", @missing_yaml ) . "\n";
76 my @missing_sysprefs = array_minus( @yaml_mod, @sysprefs_mod );
77 is( scalar @missing_sysprefs, 0, "No system preference entries missing from YAML files" );
78 if ( scalar @missing_sysprefs > 0 ) {
79 print "System preferences missing from sysprefs.sql:\n * " . join( "\n * ", @missing_sysprefs ) . "\n";
85 # Get sysprefs from SQL file populating sysprefs table with INSERT statement.
88 # INSERT INTO `systempreferences` (variable,value,explanation,options,type)
89 # VALUES('AmazonLocale','US','Use to set the Locale of your Amazon.com Web Services',
90 # 'US|CA|DE|FR|JP|UK','Choice')
92 sub get_syspref_from_file {
96 next if /^--/; # Comment line
98 if ( $_ =~ /\([\s]*\'([\w\-:]+)\'/ ) {
101 $syspref{$variable} = $query;
108 # Get system preferences from YAML files
109 sub get_syspref_from_yaml {
111 foreach my $file ( glob( $intranetdir . "/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/*.pref" ) ) {
112 if ( open( my $fh, '<:encoding(UTF-8)', $file ) ) {
113 while ( my $row = <$fh> ) {
116 if ( $row =~ /pref: (.*)/ ) {
123 warn "Could not open file '$file' $!";
130 my $sysprefs = shift;
132 # Checking the number of sysprefs in the database
133 my $query = "SELECT COUNT(*) FROM systempreferences";
134 my $sth = $dbh->prepare($query);
136 my $res = $sth->fetchrow_arrayref;
137 my $dbcount = $res->[0];
139 cmp_ok( $dbcount, ">=", scalar( keys %$sysprefs ),
140 "There are at least as many sysprefs in the database as in the sysprefs.sql"
144 # Checking for missing sysprefs in the database
145 $query = "SELECT COUNT(*) FROM systempreferences WHERE variable=?";
146 $sth = $dbh->prepare($query);
147 foreach ( keys %$sysprefs ) {
149 my $res = $sth->fetchrow_arrayref;
150 my $count = $res->[0];
152 is( $count, 1, "Syspref $_ exists in the database" );
156 print $sysprefs->{$_};
168 This test checks for missing system preferences in the database
169 and the sysprefs.sql file.
171 System prefereces are gathered from the installation and YAML files.
172 The database is then queried to check if all the system preferneces are
177 prove -v xt/check_sysprefs.t
179 If you want to display the missing sysprefs as sql inserts :
180 perl check_sysprefs.t --showsql