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