Bug 23349: Add batch operations to staff interface catalog search results
[koha.git] / t / db_dependent / check_sysprefs.t
blobe57b15e68e92cd6247191dcaaa7cfd37c8e0bb9a
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
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.
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.
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>.
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