Bug 18553 - Incorrect Loading... tag on circulation.tt when no clubs defined
[koha.git] / t / db_dependent / check_sysprefs.t
blobb179c4a027634dadc669cd83b5367d53cf1f9a35
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
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Modern::Perl;
22 use Carp;
23 use Getopt::Long;
24 use C4::Context;
26 # When this option is set, no tests are performed.
27 # The missing sysprefs are displayed as sql inserts instead.
28 our $showsql = 0;
29 GetOptions( 'showsql' => \$showsql );
31 use Test::More qw(no_plan);
32 our $dbh = C4::Context->dbh;
33 my $root_dir = C4::Context->config('intranetdir') . '/installer/data/mysql';
34 my $base_syspref_file = "sysprefs.sql";
36 open my $ref_fh, '<', "$root_dir/$base_syspref_file" or croak "Can't open '$root_dir/$base_syspref_file': $!";
37 my $ref_syspref = get_syspref_from_file($ref_fh);
38 my @ref_sysprefs = sort { lc $a cmp lc $b } keys %$ref_syspref;
39 my $num_sysprefs = scalar @ref_sysprefs;
40 if ( !$showsql ) {
41 cmp_ok( $num_sysprefs, '>', 0,
42 "Found $num_sysprefs sysprefs" );
45 check_db($ref_syspref);
48 # Get sysprefs from SQL file populating sysprefs table with INSERT statement.
50 # Example:
51 # INSERT INTO `systempreferences` (variable,value,explanation,options,type)
52 # VALUES('AmazonLocale','US','Use to set the Locale of your Amazon.com Web Services',
53 # 'US|CA|DE|FR|JP|UK','Choice')
55 sub get_syspref_from_file {
56 my $fh = shift;
57 my %syspref;
58 while (<$fh>) {
59 next if /^--/; # Comment line
60 my $query = $_;
61 if ( $_ =~ /\([\s]*\'([\w\-:]+)\'/ ) {
62 my $variable = $1;
63 if ($variable) {
64 $syspref{$variable} = $query;
68 return \%syspref;
71 sub check_db {
72 my $sysprefs = shift;
74 # Checking the number of sysprefs in the database
75 my $query = "SELECT COUNT(*) FROM systempreferences";
76 my $sth = $dbh->prepare($query);
77 $sth->execute;
78 my $res = $sth->fetchrow_arrayref;
79 my $dbcount = $res->[0];
80 if ( !$showsql ) {
81 cmp_ok( $dbcount, ">=", scalar( keys %$sysprefs ),
82 "There are at least as many sysprefs in the database as in the sysprefs.sql"
86 # Checking for missing sysprefs in the database
87 $query = "SELECT COUNT(*) FROM systempreferences WHERE variable=?";
88 $sth = $dbh->prepare($query);
89 foreach ( keys %$sysprefs ) {
90 $sth->execute($_);
91 my $res = $sth->fetchrow_arrayref;
92 my $count = $res->[0];
93 if ( !$showsql ) {
94 is( $count, 1, "Syspref $_ exists in the database" );
96 else {
97 if ( $count != 1 ) {
98 print $sysprefs->{$_};
104 =head1 NAME
106 syspref.t
108 =head1 DESCRIPTION
110 This test checks for missing sysprefs in the database.
112 Sysprefs are gathered from the installation file. The database is
113 then queried to check if all the sysprefs are in it.
115 =head1 USAGE
117 prove -v xt/check_sysprefs.t
119 If you want to display the missing sysprefs as sql inserts :
120 perl check_sysprefs.t --showsql
122 =cut