Bug 27726: DBRev 20.12.00.029
[koha.git] / xt / sample_notices.t
1 #!/usr/bin/perl
2
3 # Copyright (C) 2014 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
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 use Test::More qw(no_plan);
22
23
24 my $root_dir = 'installer/data/mysql';
25 my $base_notices_file = "en/mandatory/sample_notices.yml";
26 my @trans_notices_files = qw(
27     fr-FR/1-Obligatoire/sample_notices.sql
28     fr-CA/obligatoire/sample_notices.sql
29     it-IT/necessari/notices.sql
30     nb-NO/1-Obligatorisk/sample_notices.sql
31     pl-PL/mandatory/sample_notices.sql
32     ru-RU/mandatory/sample_notices.sql
33     uk-UA/mandatory/sample_notices.sql
34 );
35
36 ok(
37     open( my $ref_fh, "<", "$root_dir/$base_notices_file" ),
38     "Open reference sample notices file $root_dir/$base_notices_file" );
39 my $ref_notice = get_notices_from_yml_file( $ref_fh );
40 my @ref_notices = sort { lc $a cmp lc $b } keys %$ref_notice;
41 cmp_ok(
42     $#ref_notices, '>=', 0,
43     "Found " . ($#ref_notices + 1) . " sample notices" );
44
45 foreach my $file_name ( @trans_notices_files ) {
46     compare_notices( $file_name );
47 }
48
49
50 #
51 # Get sample notices from SQL file populating letters table with INSERT
52 # statement.
53 #
54 sub get_notices_from_sql_file {
55     my $fh = shift;
56     my %notice;
57     while ( <$fh> ) {
58         next unless /, *'([\_A-Z_]*)'/;
59         $notice{$1} = 1;
60     }
61     return \%notice;
62 }
63 sub get_notices_from_yml_file {
64     my $fh = shift;
65     my %notice;
66     while ( <$fh> ) {
67         next unless /^\s+code:\s([\_A-Z_]*)$/;
68         $notice{$1} = 1;
69     }
70     return \%notice;
71 }
72
73
74
75 sub compare_notices {
76     my $trans_file = shift;
77     ok(
78        open( my $trans_fh,"<", "$root_dir/$trans_file" ),
79        "Open translated sample notices file $root_dir/$trans_file" );
80     my $trans_notice = get_notices_from_sql_file( $trans_fh );
81     my @trans_notices = sort { lc $a cmp lc $b } keys %$trans_notice;
82     cmp_ok(
83         $#trans_notices, '>=', 0,
84         "Found " . ($#trans_notices + 1) . " notices" );
85     my @to_add_notices;
86     foreach ( @ref_notices ) {
87        push @to_add_notices, $_ if ! $trans_notice->{$_};
88     }
89     if ( $#to_add_notices >= 0 ) {
90         fail( 'No sample notice to add') or diag( "Sample notices to add in $trans_file: " . join(', ', @to_add_notices ) );
91     }
92     else {
93         pass( 'No sample notice to add' );
94     }
95
96     my @to_delete_notices;
97     foreach ( @trans_notices ) {
98        push @to_delete_notices, $_ if ! $ref_notice->{$_};
99     }
100     if ( $#to_delete_notices >= 0 ) {
101         fail( 'No sample notice to delete' );
102         diag( "Sample notices to delete in $trans_file: " . join(', ', @to_delete_notices ) );
103         diag( 'Warning: Some of those sample notices may rather have to be added to English notice' );
104     }
105     else {
106         pass( 'No sample notices to delete' );
107     }
108 }
109
110
111 =head1 NAME
112
113 sample_notices.t
114
115 =head1 DESCRIPTION
116
117 This test identifies incoherences between translated sample notices and the
118 'en' reference file.
119
120 Koha sample notices are loaded to 'letter' table from a text SQL file
121 during Koha installation by web installer. The reference file is the one
122 provided for English (en) installation :
123
124   <koha_root>/installer/data/mysql/en/mandatory/sample_notices.sql
125
126 Alternatives files are provided for other languages. Those files are difficult
127 to keep synchronized with reference file. This could be an functional issue
128 since some Koha operation depend on notice existence, for example Print Slip in
129 Circulation.
130
131 =head1 USAGE
132
133  prove -v sample_notices.t
134  prove sample_notices.t
135
136 =cut