Merge commit 'kc/master'
[koha.git] / xt / syspref.t
1 #!/usr/bin/perl 
2
3 # Copyright (C) 2009 Tamil s.a.r.l.
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 Koha; 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 Test::More qw(no_plan);
24
25 use C4::Context;
26
27 my $root_dir = 'installer/data/mysql';
28 my $base_syspref_file = "en/mandatory/sysprefs.sql";
29 my @trans_syspref_files = qw(
30     de-DE/mandatory/sysprefs.sql
31     fr-FR/1-Obligatoire/unimarc_standard_systemprefs.sql
32     uk-UA/mandatory/system_preferences_full_optimal_for_install_only.sql
33     ru-RU/mandatory/system_preferences_full_optimal_for_install_only.sql
34     pl-PL/mandatory/sysprefs.sql
35 );
36
37 ok(
38     open( my $ref_fh, "<$root_dir/$base_syspref_file" ),
39     "Open reference syspref file $root_dir/$base_syspref_file" );
40 my $ref_syspref = get_syspref_from_file( $ref_fh );
41 my @ref_sysprefs = sort { lc $a cmp lc $b } keys %$ref_syspref;
42 cmp_ok(
43     $#ref_sysprefs, '>=', 0,
44     "Found " . ($#ref_sysprefs + 1) . " sysprefs" );
45
46 foreach my $file_name ( @trans_syspref_files ) {
47     compare_syspref( $file_name );
48 }
49
50
51 #
52 # Get sysprefs from SQL file populating sysprefs table with INSERT statement.
53 #
54 # Exemple:
55 # INSERT INTO `systempreferences` (variable,value,explanation,options,type) 
56 # VALUES('AmazonLocale','US','Use to set the Locale of your Amazon.com Web Services',
57 # 'US|CA|DE|FR|JP|UK','Choice')
58 #
59 sub get_syspref_from_file {
60     my $fh = shift;
61     my %syspref;
62     while ( <$fh> ) {
63         next if /^--/; # Comment line
64         #/VALUES.*\(\'([\w\-:]+)\'/;
65         /\(\'([\w\-:]+)\'/;
66         my $variable = $1;
67         next unless $variable;
68         # lowercase syspref - at present, systempreference lookup is not case-sensitive
69         $variable = lc $variable;
70         $syspref{$variable} = 1;
71     }
72     return \%syspref;
73 }
74
75
76 sub compare_syspref {
77     my $trans_file = shift;
78     ok(
79        open( my $trans_fh, "<$root_dir/$trans_file" ),
80        "Open translated sysprefs file $root_dir/$trans_file" );
81     my $trans_syspref = get_syspref_from_file( $trans_fh );
82     my @trans_sysprefs = sort { lc $a cmp lc $b } keys %$trans_syspref;
83     cmp_ok(
84         $#trans_sysprefs, '>=', 0,
85         "Found " . ($#trans_sysprefs + 1) . " sysprefs" );
86
87     my @to_add_sysprefs;
88     foreach ( @ref_sysprefs ) {
89        push @to_add_sysprefs, $_ if ! $trans_syspref->{$_};
90     }
91     if ( $#to_add_sysprefs >= 0 ) {
92         fail( 'No syspref to add') or diag( "Sysprefs to add in $trans_file: " . join(', ', @to_add_sysprefs ) );
93     }
94     else {
95         pass( 'No syspref to add' );
96     }
97
98     my @to_delete_sysprefs;
99     foreach ( @trans_sysprefs ) {
100        push @to_delete_sysprefs, $_ if ! $ref_syspref->{$_};
101     }
102     if ( $#to_delete_sysprefs >= 0 ) {
103         fail( 'No syspref to delete' );
104         diag( "Sysprefs to delete in $trans_file: " . join(', ', @to_delete_sysprefs ) );
105         diag( 'Warning: Some of those sysprefs may rather have to be added to English sysprefs' );
106     }
107     else {
108         pass( 'No syspref to delete' );
109     }
110 }
111
112
113 =head1 NAME
114
115 syspref.t
116
117 =head1 DESCRIPTION
118
119 This test identifies incoherences between translated sysprefs files
120 and the reference file.
121
122 Koha sysprefs are loaded to sypref table from a text SQL file during
123 Koha installation by web installer. The reference file is the one
124 provided for English (en) installation :
125
126   <koha_root>/installer/data/mysql/en/mandatory/sysprefs.sql
127
128 Alternatives files are provided for other languages. Those files
129 are difficult to keep syncrhonized with reference file.
130
131 =head1 USAGE
132
133 prove -v syspref.t
134
135 =cut
136