Bug 36190: To not assume op is invalid if it is a TT variable
[koha.git] / xt / find-missing-op-in-forms.t
1 #!/usr/bin/perl
2
3 # Copyright 2024 Koha development team
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 tests => 1;
22 use File::Slurp;
23 use Data::Dumper;
24
25 my @files;
26
27 # OPAC
28 push @files, `git ls-files 'koha-tmpl/opac-tmpl/bootstrap/en/*.tt'`;
29 push @files, `git ls-files 'koha-tmpl/opac-tmpl/bootstrap/en/*.inc'`;
30
31 # Staff
32 push @files, `git ls-files 'koha-tmpl/intranet-tmpl/prog/en/*.tt'`;
33 push @files, `git ls-files 'koha-tmpl/intranet-tmpl/prog/en/*.inc'`;
34
35 my @errors;
36 for my $file ( @files ) {
37     chomp $file;
38     my @e = catch_missing_op($file);
39     push @errors, sprintf "%s:%s", $file, join (",", @e) if @e;
40 }
41
42 is( @errors, 0, "The <form> in the following files are missing it's corresponding op parameter, or op does not start with 'cud-' (see bug 34478)" )
43     or diag( Dumper @errors );
44
45 sub catch_missing_op {
46     my ($file) = @_;
47
48     my @lines = read_file($file);
49     my @errors;
50     return unless grep { $_ =~ m|<form| } @lines;
51     my ( $in_form, $closed_form, $line_open_form, $has_op, $op_value );
52     my $line_number = 0;
53     for my $line (@lines) {
54         $line_number++;
55         if ( $line =~ m{^(\s*)<form.*method=('|")post('|")}i ) {
56             $in_form        = 1;
57             $line_open_form = $line_number;
58         }
59         if ( $in_form && $line =~ m{name="op"} ) {
60             $has_op = 1;
61             if ( $line =~ m{value="(.*)"} ) {
62                 $op_value = $1
63             }
64         }
65         if ( $in_form && $line =~ m{</form} ) {
66             $closed_form = 0;
67             if ($has_op) {
68                 if ( $op_value !~ m{^cud-} && $op_value !~ m{^\[%} ) {
69                     push @errors, $line_open_form;
70                 }
71             } else {
72                 push @errors, $line_open_form;
73             }
74             $in_form = 0;
75         }
76     }
77     return @errors;
78 }