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