Bug 15395: Add unit tests for extract_messages_from_templates
[koha.git] / t / LangInstaller.t
1 use Modern::Perl;
2
3 use FindBin '$Bin';
4 use lib "$Bin/../misc/translator";
5
6 use Test::More tests => 39;
7 use File::Temp qw(tempdir);
8 use File::Slurp;
9 use Locale::PO;
10
11 use t::lib::Mocks;
12
13 use_ok('LangInstaller');
14
15 my $installer = LangInstaller->new();
16
17 my $tempdir = tempdir(CLEANUP => 0);
18 t::lib::Mocks::mock_config('intranetdir', "$Bin/LangInstaller/templates");
19 my @files = ('simple.tt');
20 $installer->extract_messages_from_templates($tempdir, @files);
21
22 ok(-e "$tempdir/simple.tt", 'it has created a temporary file simple.tt');
23 SKIP: {
24     skip "simple.tt does not exist", 37 unless -e "$tempdir/simple.tt";
25
26     my $output = read_file("$tempdir/simple.tt");
27     my $expected_output = <<'EOF';
28 __('hello');
29 __x('hello {name}');
30 __n('item', 'items');
31 __nx('{count} item', '{count} items');
32 __p('context', 'hello');
33 __px('context', 'hello {name}');
34 __np('context', 'item', 'items');
35 __npx('context', '{count} item', '{count} items');
36 __npx('context', '{count} item', '{count} items');
37 __x('status is {status}');
38 __('active');
39 __('inactive');
40 __('Inside block');
41 EOF
42
43     is($output, $expected_output, "Output of extract_messages_from_templates is as expected");
44
45     my $xgettext_cmd = "xgettext -L Perl --from-code=UTF-8 "
46         . "--package-name=Koha --package-version='' "
47         . "-k -k__ -k__x -k__n:1,2 -k__nx:1,2 -k__xn:1,2 -k__p:1c,2 "
48         . "-k__px:1c,2 -k__np:1c,2,3 -k__npx:1c,2,3 "
49         . "-o $tempdir/Koha.pot -D $tempdir simple.tt";
50
51     system($xgettext_cmd);
52     my $pot = Locale::PO->load_file_asarray("$tempdir/Koha.pot");
53
54     my @expected = (
55         {
56             msgid => '"hello"',
57         },
58         {
59             msgid => '"hello {name}"',
60         },
61         {
62             msgid => '"item"',
63             msgid_plural => '"items"',
64         },
65         {
66             msgid => '"{count} item"',
67             msgid_plural => '"{count} items"',
68         },
69         {
70             msgid => '"hello"',
71             msgctxt => '"context"',
72         },
73         {
74             msgid => '"hello {name}"',
75             msgctxt => '"context"',
76         },
77         {
78             msgid => '"item"',
79             msgid_plural => '"items"',
80             msgctxt => '"context"',
81         },
82         {
83             msgid => '"{count} item"',
84             msgid_plural => '"{count} items"',
85             msgctxt => '"context"',
86         },
87         {
88             msgid => '"status is {status}"',
89         },
90         {
91             msgid => '"active"',
92         },
93         {
94             msgid => '"inactive"',
95         },
96         {
97             msgid => '"Inside block"',
98         },
99     );
100
101     for (my $i = 0; $i < @expected; $i++) {
102         for my $key (qw(msgid msgid_plural msgctxt)) {
103             my $expected = $expected[$i]->{$key};
104             my $expected_str = defined $expected ? $expected : 'not defined';
105             is($pot->[$i + 1]->$key, $expected, "$i: $key is $expected_str");
106         }
107     }
108 }