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