Bug 14778: Make 3 tests pass
[koha.git] / t / db_dependent / check_sysprefs.t
bloba53c57547583e79a0b8430e98a38d6c8321795ae
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 strict;
21 use warnings;
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" );
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" );
44 check_db($ref_syspref);
47 # Get sysprefs from SQL file populating sysprefs table with INSERT statement.
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')
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;
67 return \%syspref;
70 sub check_db {
71 my $sysprefs = shift;
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"
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" );
95 else {
96 if ( $count != 1 ) {
97 print $sysprefs->{$_};
103 =head1 NAME
105 syspref.t
107 =head1 DESCRIPTION
109 This test checks for missing sysprefs in the database.
111 Sysprefs are gathered from the installation file. The database is
112 then queried to check if all the sysprefs are in it.
114 =head1 USAGE
116 prove -v xt/check_sysprefs.t
118 If you want to display the missing sysprefs as sql inserts :
119 perl check_sysprefs.t --showsql
121 =cut