Bug 18501: Don't use paidfor for DB fields not mapped with a MARC field
[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
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Carp;
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" 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" );
43 }
44
45 check_db($ref_syspref);
46
47 #
48 # Get sysprefs from SQL file populating sysprefs table with INSERT statement.
49 #
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')
54 #
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;
65             }
66         }
67     }
68     return \%syspref;
69 }
70
71 sub check_db {
72     my $sysprefs = shift;
73
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"
83         );
84     }
85
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" );
95         }
96         else {
97             if ( $count != 1 ) {
98                 print $sysprefs->{$_};
99             }
100         }
101     }
102 }
103
104 =head1 NAME
105
106 syspref.t
107
108 =head1 DESCRIPTION
109
110 This test checks for missing sysprefs in the database.
111
112 Sysprefs are gathered from the installation file. The database is
113 then queried to check if all the sysprefs are in it.
114
115 =head1 USAGE
116
117 prove -v xt/check_sysprefs.t
118
119 If you want to display the missing sysprefs as sql inserts :
120 perl check_sysprefs.t --showsql
121
122 =cut