Debian Codegraph @

lemonldap-ng-manager/t/90-translations.t

raw
1 # Verify that languages translation cover all `trspan`
2
3 use warnings;
4 use Test::More;
5 use JSON;
6 use strict;
7
8 my $langDir = 'site/htdocs/static/languages';
9
10 my $count = 0;
11 use_ok('Lemonldap::NG::Manager::Build::Tree');
12 use_ok('Lemonldap::NG::Manager::Build::CTrees');
13 use_ok('Lemonldap::NG::Manager::Build::Attributes');
14 $count += 3;
15
16 ok( opendir( D, $langDir ), 'Found languages dir' );
17 my @langs = grep { /\.json$/ and $_ ne 'en.json' } readdir D;
18 close D;
19 ok( open( F, "$langDir/en.json" ), 'Found en.json' );
20 my $en;
21 ok( $en = join( '', <F> ), 'en.json is not empty' );
22 close F;
23 ok( @langs, 'Found other languages' );
24 my $keys;
25 ok( $keys = from_json($en), 'en.json contains JSON' );
26 $en = undef;
27 $count += 5;
28
29 foreach my $lang (@langs) {
30 ok( open( F, "$langDir/$lang" ), "Found $lang" );
31 my $j;
32 ok( $j = join( '', <F> ), "$lang is not empty" );
33 my $l;
34 ok( $l = from_json($j), "$lang contains JSON" . ( $@ ? " ($@)" : "" ) );
35 $lang =~ s/\.json$//;
36 my @l1 = sort keys %$keys;
37 my @l2 = sort keys %$l;
38 ok( $#l1 == $#l2,
39 "'$lang' and 'en' have the same count (" . @l2 . '/' . @l1 . ")" );
40
41 my @unTr;
42 while (@l1) {
43 if ( $l1[0] eq $l2[0] ) {
44 shift @l1;
45 shift @l2;
46 }
47 elsif ( $l1[0] eq $l2[1] ) {
48 push @unTr, shift(@l2);
49 }
50 elsif ( $l1[1] eq $l2[0] ) {
51 push @unTr, shift(@l1);
52 }
53 else {
54 die "To many errors in $lang.json (at $l1[0]/$l2[0])";
55 }
56 }
57 ok( @unTr == 0,
58 "All keys translated for '$lang'" . ( @unTr ? " (@unTr)" : "" ) );
59 $count += 5;
60 }
61
62 my $tree;
63 ok( $tree = Lemonldap::NG::Manager::Build::Tree::tree(), 'Get tree' );
64 my @nodes = @{ getNodes($tree) };
65
66 my @unTr;
67 foreach (@nodes) {
68 my $node = $_;
69 $node =~ s/^\*//;
70 push @unTr, $node unless ( $keys->{$node} );
71 }
72 ok( @unTr == 0,
73 'All attributes translated for Tree.pm' . ( @unTr ? " (@unTr)" : "" ) );
74 $count += 2;
75
76 my ( $type, $ctrees );
77 @unTr = ();
78 ok( $ctrees = Lemonldap::NG::Manager::Build::CTrees::cTrees(), 'Get cTrees' );
79 while ( ( $type, $tree ) = each %$ctrees ) {
80 @nodes = @{ getNodes($tree) };
81 foreach (@nodes) {
82 push @unTr, $_ unless ( $keys->{$_} );
83 }
84 }
85 ok( @unTr == 0,
86 'All attributes translated for Tree.pm' . ( @unTr ? " (@unTr)" : "" ) );
87 $count += 2;
88
89 ok(
90 open( F,
91 q#perl -ne 'print if(s/.*trspan="(\w+)".*/$1/g)' site/templates/manager.tpl site/htdocs/static/forms/*|sort -u|#
92 ),
93 'Find HTML docs'
94 );
95 my @trspan = ();
96 while (<F>) {
97 chomp;
98 push @trspan, $_;
99 }
100 ok( @trspan > 1, 'Found "trspan" attributes' );
101 @unTr = ();
102 foreach (@trspan) {
103 push @unTr, $_ unless ( $keys->{$_} );
104 }
105 ok( @unTr == 0,
106 'All "trspan" attribute translated' . ( @unTr ? " (@unTr)" : "" ) );
107 $count += 3;
108
109 # Check for flag icons
110 foreach my $lang (@langs) {
111 ok( -f "site/htdocs/static/logos/$lang.png", "Flag icon found for $lang" );
112 $count += 1;
113 }
114
115 done_testing($count);
116
117 sub getNodes {
118 my $tree = shift;
119 my @res;
120 foreach my $k (@$tree) {
121 if ( ref($k) ) {
122 push @res, $k->{title}, @{ getNodes( $k->{nodes} ) },
123 @{ getNodes( $k->{nodes_cond} ) };
124 }
125 else {
126 push @res, $k;
127 }
128 }
129 return \@res;
130 }