Bug 35979: (follow-up) Add check in ->enqueue
[koha.git] / t / db_dependent / check_sysprefs.t
1 #!/usr/bin/perl
2
3 # Copyright (C) 2010 BibLibre
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Carp;
23 use Getopt::Long;
24 use C4::Context;
25 use Array::Utils qw(array_minus);
26
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::More tests => 2;
33
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";
38
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;
43
44 subtest 'Compare database with sysprefs.sql file' => sub {
45     if ( !$showsql ) {
46         cmp_ok(
47             $num_sysprefs, '>', 0,
48             "Found $num_sysprefs sysprefs"
49         );
50     }
51
52     check_db($ref_syspref);
53 };
54
55 subtest 'Compare sysprefs.sql with YAML files' => sub {
56     plan tests => 2;
57
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
61
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
69
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";
74     }
75
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";
80     }
81 };
82
83
84 #
85 # Get sysprefs from SQL file populating sysprefs table with INSERT statement.
86 #
87 # Example:
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')
91 #
92 sub get_syspref_from_file {
93     my $fh = shift;
94     my %syspref;
95     while (<$fh>) {
96         next if /^--/;    # Comment line
97         my $query = $_;
98         if ( $_ =~ /\([\s]*\'([\w\-:]+)\'/ ) {
99             my $variable = $1;
100             if ($variable) {
101                 $syspref{$variable} = $query;
102             }
103         }
104     }
105     return \%syspref;
106 }
107
108 #  Get system preferences from YAML files
109 sub get_syspref_from_yaml {
110     my @prefs;
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> ) {
114                 chomp $row;
115                 my $pref;
116                 if ( $row =~ /pref: (.*)/ ) {
117                     $pref = $1;
118                     $pref =~ s/["']//ig;
119                     push @prefs, $pref;
120                 }
121             }
122         } else {
123             warn "Could not open file '$file' $!";
124         }
125     }
126     return \@prefs;
127 }
128
129 sub check_db {
130     my $sysprefs = shift;
131
132     # Checking the number of sysprefs in the database
133     my $query = "SELECT COUNT(*) FROM systempreferences";
134     my $sth   = $dbh->prepare($query);
135     $sth->execute;
136     my $res     = $sth->fetchrow_arrayref;
137     my $dbcount = $res->[0];
138     if ( !$showsql ) {
139         cmp_ok( $dbcount, ">=", scalar( keys %$sysprefs ),
140 "There are at least as many sysprefs in the database as in the sysprefs.sql"
141         );
142     }
143
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 ) {
148         $sth->execute($_);
149         my $res   = $sth->fetchrow_arrayref;
150         my $count = $res->[0];
151         if ( !$showsql ) {
152             is( $count, 1, "Syspref $_ exists in the database" );
153         }
154         else {
155             if ( $count != 1 ) {
156                 print $sysprefs->{$_};
157             }
158         }
159     }
160 }
161
162 =head1 NAME
163
164 syspref.t
165
166 =head1 DESCRIPTION
167
168 This test checks for missing system preferences in the database
169 and the sysprefs.sql file.
170
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
173 in it.
174
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