03-HTML-forms.t 2.37 KB
Newer Older
Xavier Guimard's avatar
Xavier Guimard committed
1 2
# Check that HTML forms exists for all types of parameter and that no HTML
# forms are unused
3 4

use Test::More;
5
use Data::Dumper;
6 7
use strict;

8
my $formDir = 'site/htdocs/static/forms';
9 10

my $count = 0;
11 12 13
use_ok('Lemonldap::NG::Manager::Build::Tree');
use_ok('Lemonldap::NG::Manager::Build::CTrees');
use_ok('Lemonldap::NG::Manager::Build::Attributes');
14 15 16 17 18
$count += 3;

ok( opendir( D, $formDir ), 'Found forms dir' );
my %forms = map { s/\.html$// ? ( $_ => 1 ) : () } readdir D;
close D;
19

20
delete $forms{white};
Xavier Guimard's avatar
Xavier Guimard committed
21
delete $forms{mini};
22 23 24
delete $forms{restore};

my ( @types, $attr, $tree, $ctrees );
25 26
ok( $tree   = Lemonldap::NG::Manager::Build::Tree::tree(),     'Get tree' );
ok( $ctrees = Lemonldap::NG::Manager::Build::CTrees::cTrees(), 'Get cTrees' );
Xavier Guimard's avatar
Xavier Guimard committed
27
ok( $attr   = Lemonldap::NG::Manager::Build::Attributes::attributes(),
28
    'Get attributes' );
29
$count += 4;
30 31 32

my %types = %{ getTypes( $tree, values(%$ctrees), $attr ) };

33
foreach (qw(home menuCat menuApp authParamsTextContainer)) {
34 35
    ok( $forms{$_}, "Found $_ form" );
    $count++;
36 37 38 39 40
    delete $forms{$_};
}

foreach my $type ( keys %types ) {
    delete $types{$type};
41
    if ( $type =~
42
/^(?:array|authParamsText|url|PerlModule|hostname|pcre|lmAttrOrMacro|subContainer|RSAP(?:ublic|rivate)Key(?:OrCertificate)?)$/
43 44 45 46 47
      )
    {
        delete $forms{$type};
        next;
    }
48 49 50 51
    ok( $forms{$type}, "Found $type" );
    delete $forms{$type};
    $count++;
    if ( $type =~ s/Container$// ) {
52
        next if ( $type eq 'simpleInput' );
53 54 55 56 57
        ok( $forms{$type}, "Found $type" );
        delete $forms{$type};
        $count++;
    }
}
58
ok( !%forms, "No unused forms" ) or print "Found:\n" . Dumper( \%forms );
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
$count++;

done_testing($count);

sub getTypes {
    my @trees = @_;
    my $res = { 'text' => 1 };
    foreach my $t (@trees) {
        if ( ref($t) eq 'HASH' ) {
            foreach my $a ( values %$t ) {

                my $tmp = $a->{type};
                $res->{$tmp}++ if ($tmp);
            }
        }
        else {
            _getTypes( $t, $res );
        }
    }
    return $res;
}

sub _getTypes {
    my ( $tree, $res ) = splice @_;
Xavier Guimard's avatar
Xavier Guimard committed
83
    $tree = [$tree] unless ( ref($tree) );
84 85
    foreach my $k (@$tree) {
        if ( ref($k) ) {
Xavier Guimard's avatar
Xavier Guimard committed
86 87
            _getTypes( $k->{nodes},      $res ) if ( $k->{nodes} );
            _getTypes( $k->{nodes_cond}, $res ) if ( $k->{nodes_cond} );
88 89 90 91
            $res->{ $k->{form} }++ if ( $k->{form} );
        }
    }
}