bug 4151: replace dep on YAML::XS with YAML::Syck
[koha.git] / xt / check_sysprefs.t
blobbc37740f3eb3d7e4135f3d558680b04edc3ff789
1 #!/usr/bin/perl
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
20 use strict;
21 use warnings;
23 use Getopt::Long;
24 use C4::Context;
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);
33 use Test::More qw(no_plan);
34 our $dbh = C4::Context->dbh;
35 my $root_dir = C4::Context->config( 'intranetdir' ) . '/installer/data/mysql';
36 my $base_syspref_file = "en/mandatory/sysprefs.sql";
39 open( my $ref_fh, "<$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 if (!$showsql) {
43 cmp_ok(
44 $#ref_sysprefs, '>=', 0,
45 "Found " . ($#ref_sysprefs + 1) . " sysprefs"
49 check_db($ref_syspref);
53 # Get sysprefs from SQL file populating sysprefs table with INSERT statement.
55 # Exemple:
56 # INSERT INTO `systempreferences` (variable,value,explanation,options,type)
57 # VALUES('AmazonLocale','US','Use to set the Locale of your Amazon.com Web Services',
58 # 'US|CA|DE|FR|JP|UK','Choice')
60 sub get_syspref_from_file {
61 my $fh = shift;
62 my %syspref;
63 while ( <$fh> ) {
64 next if /^--/; # Comment line
65 my $query = $_;
66 if ($_ =~ /\([\s]*\'([\w\-:]+)\'/) {
67 my $variable = $1;
68 if ($variable) {
69 $syspref{$variable} = $query;
73 return \%syspref;
77 sub check_db {
78 my $sysprefs = shift;
80 # Checking the number of sysprefs in the database
81 my $query = "SELECT COUNT(*) FROM systempreferences";
82 my $sth = $dbh->prepare($query);
83 $sth->execute;
84 my $res = $sth->fetchrow_arrayref;
85 my $dbcount = $res->[0];
86 if (!$showsql) {
87 cmp_ok (
88 $dbcount, ">=", scalar(keys %$sysprefs), "There are at least as many sysprefs in the database as in the sysprefs.sql"
92 # Checking for missing sysprefs in the database
93 $query = "SELECT COUNT(*) FROM systempreferences WHERE variable=?";
94 $sth = $dbh->prepare($query);
95 foreach (keys %$sysprefs) {
96 $sth->execute($_);
97 my $res = $sth->fetchrow_arrayref;
98 my $count = $res->[0];
99 if (!$showsql) {
101 $count, 1, "Syspref $_ exists in the database"
103 } else {
104 if ($count != 1) {
105 print $sysprefs->{$_};
112 =head1 NAME
114 syspref.t
116 =head1 DESCRIPTION
118 This test checks for missing sysprefs in the database.
120 Sysprefs are gathered from the installation file. The database is
121 then queried to check if all the sysprefs are in it.
123 =head1 USAGE
125 prove -v check_sysprefs.t
127 If you want to display the missing sysprefs as sql inserts :
128 perl check_sysprefs.t --showsql
130 =cut