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