Shifting the tests that need user input to their own directory, they cant be used...
[koha.git] / xt / permissions.t
blob377f4515173c7ae88013fef891b474ab3cbbdc0b
1 #!/usr/bin/perl
3 # Copyright (C) 2010 Tamil s.a.r.l.
5 # This file is part of Koha.
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.
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.
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.
20 use strict;
21 use warnings;
23 use Test::More qw(no_plan);
25 use C4::Context;
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 fr-FR/1-Obligatoire/userpermissions.sql
32 uk-UA/mandatory/permissions_and_user_flags.sql
33 ru-RU/mandatory/permissions_and_user_flags.sql
34 pl-PL/mandatory/userpermissions.sql
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" );
46 foreach my $file_name ( @trans_perms_files ) {
47 compare_perms( $file_name );
52 # Get user permissions from SQL file populating permissions table with INSERT
53 # statement.
55 # Exemple:
56 # INSERT INTO permissions (module_bit, code, description) VALUES
57 # ( 1, 'override_renewals', 'Override blocked renewals'),
59 sub get_perms_from_file {
60 my $fh = shift;
61 my %perm;
62 my $found_insert = 0;
63 while ( <$fh> ) {
64 next if /^--/; # Comment line
65 $found_insert = 1 if /insert\s+into/i and /permissions/i;
66 next unless $found_insert;
67 #/VALUES.*\(\'([\w\-:]+)\'/;
68 /,\s*\'(.*?)\'/;
69 my $variable = $1;
70 next unless $variable;
71 $perm{$variable} = 1;
73 return \%perm;
77 sub compare_perms {
78 my $trans_file = shift;
79 ok(
80 open( my $trans_fh, "<$root_dir/$trans_file" ),
81 "Open translated user permissions file $root_dir/$trans_file" );
82 my $trans_perm = get_perms_from_file( $trans_fh );
83 my @trans_perms = sort { lc $a cmp lc $b } keys %$trans_perm;
84 cmp_ok(
85 $#trans_perms, '>=', 0,
86 "Found " . ($#trans_perms + 1) . " perms" );
88 my @to_add_perms;
89 foreach ( @ref_perms ) {
90 push @to_add_perms, $_ if ! $trans_perm->{$_};
92 if ( $#to_add_perms >= 0 ) {
93 fail( 'No user permissions to add') or diag( "User permissions to add in $trans_file: " . join(', ', @to_add_perms ) );
95 else {
96 pass( 'No user permissions to add' );
99 my @to_delete_perms;
100 foreach ( @trans_perms ) {
101 push @to_delete_perms, $_ if ! $ref_perm->{$_};
103 if ( $#to_delete_perms >= 0 ) {
104 fail( 'No user permissions to delete' );
105 diag( "User permissions to delete in $trans_file: " . join(', ', @to_delete_perms ) );
106 diag( 'Warning: Some of those user permissions may rather have to be added to English permissions' );
108 else {
109 pass( 'No user permissions to delete' );
114 =head1 NAME
116 permissions.t
118 =head1 DESCRIPTION
120 This test identifies incoherences between translated user permissions files and
121 the 'en' reference file.
123 Koha user permissions are loaded to 'permissions' table from a text SQL file
124 during Koha installation by web installer. The reference file is the one
125 provided for English (en) installation :
127 <koha_root>/installer/data/mysql/en/mandatory/userpermissions.sql
129 Alternatives files are provided for other languages. Those files
130 are difficult to keep syncrhonized with reference file.
132 =head1 USAGE
134 prove -v permissions.t
135 prove permissions.t
137 =cut