Bug 14097 : Changing AddReserve prototype call
[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 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.
11 #
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.
15 #
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.
19
20 use strict;
21 use warnings;
22
23 use Getopt::Long;
24 use C4::Context;
25
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 );
30
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";
35
36 open( my $ref_fh, "<$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 if ( !$showsql ) {
40     cmp_ok( $#ref_sysprefs, '>=', 0,
41         "Found " . ( $#ref_sysprefs + 1 ) . " sysprefs" );
42 }
43
44 check_db($ref_syspref);
45
46 #
47 # Get sysprefs from SQL file populating sysprefs table with INSERT statement.
48 #
49 # Example:
50 # INSERT INTO `systempreferences` (variable,value,explanation,options,type)
51 # VALUES('AmazonLocale','US','Use to set the Locale of your Amazon.com Web Services',
52 # 'US|CA|DE|FR|JP|UK','Choice')
53 #
54 sub get_syspref_from_file {
55     my $fh = shift;
56     my %syspref;
57     while (<$fh>) {
58         next if /^--/;    # Comment line
59         my $query = $_;
60         if ( $_ =~ /\([\s]*\'([\w\-:]+)\'/ ) {
61             my $variable = $1;
62             if ($variable) {
63                 $syspref{$variable} = $query;
64             }
65         }
66     }
67     return \%syspref;
68 }
69
70 sub check_db {
71     my $sysprefs = shift;
72
73     # Checking the number of sysprefs in the database
74     my $query = "SELECT COUNT(*) FROM systempreferences";
75     my $sth   = $dbh->prepare($query);
76     $sth->execute;
77     my $res     = $sth->fetchrow_arrayref;
78     my $dbcount = $res->[0];
79     if ( !$showsql ) {
80         cmp_ok( $dbcount, ">=", scalar( keys %$sysprefs ),
81 "There are at least as many sysprefs in the database as in the sysprefs.sql"
82         );
83     }
84
85     # Checking for missing sysprefs in the database
86     $query = "SELECT COUNT(*) FROM systempreferences WHERE variable=?";
87     $sth   = $dbh->prepare($query);
88     foreach ( keys %$sysprefs ) {
89         $sth->execute($_);
90         my $res   = $sth->fetchrow_arrayref;
91         my $count = $res->[0];
92         if ( !$showsql ) {
93             is( $count, 1, "Syspref $_ exists in the database" );
94         }
95         else {
96             if ( $count != 1 ) {
97                 print $sysprefs->{$_};
98             }
99         }
100     }
101 }
102
103 =head1 NAME
104
105 syspref.t
106
107 =head1 DESCRIPTION
108
109 This test checks for missing sysprefs in the database.
110
111 Sysprefs are gathered from the installation file. The database is
112 then queried to check if all the sysprefs are in it.
113
114 =head1 USAGE
115
116 prove -v xt/check_sysprefs.t
117
118 If you want to display the missing sysprefs as sql inserts :
119 perl check_sysprefs.t --showsql
120
121 =cut