Merge branch 'new/bug_6479' into kcmaster
[koha.git] / xt / permissions.t
1 #!/usr/bin/perl 
2
3 # Copyright (C) 2010 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_perms_file = "en/mandatory/userpermissions.sql";
29 my @trans_perms_files = qw(
30     de-DE/mandatory/userpermissions.sql
31     it-IT/necessari/userpermissions.sql
32     fr-FR/1-Obligatoire/userpermissions.sql
33     uk-UA/mandatory/permissions_and_user_flags.sql
34     ru-RU/mandatory/permissions_and_user_flags.sql
35     pl-PL/mandatory/userpermissions.sql
36 );
37
38 ok(
39     open( my $ref_fh, "<$root_dir/$base_perms_file" ),
40     "Open reference user permissions file $root_dir/$base_perms_file" );
41 my $ref_perm = get_perms_from_file( $ref_fh );
42 my @ref_perms = sort { lc $a cmp lc $b } keys %$ref_perm;
43 cmp_ok(
44     $#ref_perms, '>=', 0,
45     "Found " . ($#ref_perms + 1) . " user permissions" );
46
47 foreach my $file_name ( @trans_perms_files ) {
48     compare_perms( $file_name );
49 }
50
51
52 #
53 # Get user permissions from SQL file populating permissions table with INSERT
54 # statement.
55 #
56 # Exemple:
57 #  INSERT INTO permissions (module_bit, code, description) VALUES
58 #  ( 1, 'override_renewals', 'Override blocked renewals'),
59 #
60 sub get_perms_from_file {
61     my $fh = shift;
62     my %perm;
63     my $found_insert = 0;
64     while ( <$fh> ) {
65         next if /^--/; # Comment line
66         $found_insert = 1 if /insert\s+into/i and /permissions/i;
67         next unless $found_insert;
68         #/VALUES.*\(\'([\w\-:]+)\'/;
69         /,\s*\'(.*?)\'/;
70         my $variable = $1;
71         next unless $variable;
72         $perm{$variable} = 1;
73     }
74     return \%perm;
75 }
76
77
78 sub compare_perms {
79     my $trans_file = shift;
80     ok(
81        open( my $trans_fh, "<$root_dir/$trans_file" ),
82        "Open translated user permissions file $root_dir/$trans_file" );
83     my $trans_perm = get_perms_from_file( $trans_fh );
84     my @trans_perms = sort { lc $a cmp lc $b } keys %$trans_perm;
85     cmp_ok(
86         $#trans_perms, '>=', 0,
87         "Found " . ($#trans_perms + 1) . " perms" );
88
89     my @to_add_perms;
90     foreach ( @ref_perms ) {
91        push @to_add_perms, $_ if ! $trans_perm->{$_};
92     }
93     if ( $#to_add_perms >= 0 ) {
94         fail( 'No user permissions to add') or diag( "User permissions to add in $trans_file: " . join(', ', @to_add_perms ) );
95     }
96     else {
97         pass( 'No user permissions to add' );
98     }
99
100     my @to_delete_perms;
101     foreach ( @trans_perms ) {
102        push @to_delete_perms, $_ if ! $ref_perm->{$_};
103     }
104     if ( $#to_delete_perms >= 0 ) {
105         fail( 'No user permissions to delete' );
106         diag( "User permissions to delete in $trans_file: " . join(', ', @to_delete_perms ) );
107         diag( 'Warning: Some of those user permissions may rather have to be added to English permissions' );
108     }
109     else {
110         pass( 'No user permissions to delete' );
111     }
112 }
113
114
115 =head1 NAME
116
117 permissions.t
118
119 =head1 DESCRIPTION
120
121 This test identifies incoherences between translated user permissions files and
122 the 'en' reference file.
123
124 Koha user permissions are loaded to 'permissions' table from a text SQL file
125 during Koha installation by web installer. The reference file is the one
126 provided for English (en) installation :
127
128   <koha_root>/installer/data/mysql/en/mandatory/userpermissions.sql
129
130 Alternatives files are provided for other languages. Those files
131 are difficult to keep syncrhonized with reference file.
132
133 =head1 USAGE
134
135  prove -v permissions.t
136  prove permissions.t
137
138 =cut
139