poll vote (Accept both)
[ikiwiki.git] / t / po.t
blob51321fa96c73d3a325064fb59bda6aa6b10fbdea
1 #!/usr/bin/perl
2 # -*- cperl-indent-level: 8; -*-
3 use warnings;
4 use strict;
5 use File::Temp qw{tempdir};
7 BEGIN {
8 unless (eval { require Locale::Po4a::Chooser }) {
9 eval q{
10 use Test::More skip_all => "Locale::Po4a::Chooser::new is not available"
13 unless (eval { require Locale::Po4a::Po }) {
14 eval q{
15 use Test::More skip_all => "Locale::Po4a::Po::new is not available"
20 use Test::More tests => 91;
22 BEGIN { use_ok("IkiWiki"); }
24 my $msgprefix;
26 my $dir = tempdir("ikiwiki-test-po.XXXXXXXXXX",
27 DIR => File::Spec->tmpdir,
28 CLEANUP => 1);
30 ### Init
31 %config=IkiWiki::defaultconfig();
32 $config{srcdir} = "$dir/src";
33 $config{destdir} = "$dir/dst";
34 $config{destdir} = "$dir/dst";
35 $config{underlaydirbase} = "/dev/null";
36 $config{underlaydir} = "/dev/null";
37 $config{discussion} = 0;
38 $config{po_master_language} = { code => 'en',
39 name => 'English'
41 $config{po_slave_languages} = {
42 es => 'Castellano',
43 fr => "Français"
45 $config{po_translatable_pages}='index or test1 or test2 or translatable';
46 $config{po_link_to}='negotiated';
47 IkiWiki::loadplugins();
48 ok(IkiWiki::loadplugin('meta'), "meta plugin loaded");
49 ok(IkiWiki::loadplugin('po'), "po plugin loaded");
50 IkiWiki::checkconfig();
52 ### seed %pagesources and %pagecase
53 $pagesources{'index'}='index.mdwn';
54 $pagesources{'index.fr'}='index.fr.po';
55 $pagesources{'index.es'}='index.es.po';
56 $pagesources{'test1'}='test1.mdwn';
57 $pagesources{'test1.es'}='test1.es.po';
58 $pagesources{'test1.fr'}='test1.fr.po';
59 $pagesources{'test2'}='test2.mdwn';
60 $pagesources{'test2.es'}='test2.es.po';
61 $pagesources{'test2.fr'}='test2.fr.po';
62 $pagesources{'test3'}='test3.mdwn';
63 $pagesources{'test3.es'}='test3.es.mdwn';
64 $pagesources{'translatable'}='translatable.mdwn';
65 $pagesources{'translatable.fr'}='translatable.fr.po';
66 $pagesources{'translatable.es'}='translatable.es.po';
67 $pagesources{'nontranslatable'}='nontranslatable.mdwn';
68 foreach my $page (keys %pagesources) {
69 $IkiWiki::pagecase{lc $page}=$page;
72 ### populate srcdir
73 writefile('index.mdwn', $config{srcdir},
74 "[[!meta title=\"index title\"]]\n[[translatable]] [[nontranslatable]]");
75 writefile('test1.mdwn', $config{srcdir},
76 "[[!meta title=\"test1 title\"]]\ntest1 content");
77 writefile('test2.mdwn', $config{srcdir}, 'test2 content');
78 writefile('test3.mdwn', $config{srcdir}, 'test3 content');
79 writefile('translatable.mdwn', $config{srcdir}, '[[nontranslatable]]');
80 writefile('nontranslatable.mdwn', $config{srcdir}, '[[/]] [[translatable]]');
82 ### istranslatable/istranslation
83 # we run these tests twice because memoization attempts made them
84 # succeed once every two tries...
85 foreach (1, 2) {
86 ok(IkiWiki::Plugin::po::istranslatable('index'), "index is translatable");
87 ok(IkiWiki::Plugin::po::istranslatable('/index'), "/index is translatable");
88 ok(! IkiWiki::Plugin::po::istranslatable('index.fr'), "index.fr is not translatable");
89 ok(! IkiWiki::Plugin::po::istranslatable('index.es'), "index.es is not translatable");
90 ok(! IkiWiki::Plugin::po::istranslatable('/index.fr'), "/index.fr is not translatable");
91 ok(! IkiWiki::Plugin::po::istranslation('index'), "index is not a translation");
92 ok(IkiWiki::Plugin::po::istranslation('index.fr'), "index.fr is a translation");
93 ok(IkiWiki::Plugin::po::istranslation('index.es'), "index.es is a translation");
94 ok(IkiWiki::Plugin::po::istranslation('/index.fr'), "/index.fr is a translation");
95 ok(IkiWiki::Plugin::po::istranslatable('test1'), "test1 is translatable");
96 ok(IkiWiki::Plugin::po::istranslation('test1.es'), "test1.es is a translation");
97 ok(IkiWiki::Plugin::po::istranslation('test1.fr'), "test1.fr is a translation");
98 ok(IkiWiki::Plugin::po::istranslatable('test2'), "test2 is translatable");
99 ok(! IkiWiki::Plugin::po::istranslation('test2'), "test2 is not a translation");
100 ok(! IkiWiki::Plugin::po::istranslatable('test3'), "test3 is not translatable");
101 ok(! IkiWiki::Plugin::po::istranslation('test3'), "test3 is not a translation");
104 ### pofiles
106 my @pofiles = IkiWiki::Plugin::po::pofiles(srcfile("index.mdwn"));
107 ok( @pofiles, "pofiles is defined");
108 ok( @pofiles == 2, "pofiles has correct size");
109 is_deeply(\@pofiles, ["$config{srcdir}/index.es.po", "$config{srcdir}/index.fr.po"], "pofiles content is correct");
111 ### links
112 require IkiWiki::Render;
114 sub refresh_n_scan(@) {
115 my @masterfiles_rel=@_;
116 foreach my $masterfile_rel (@masterfiles_rel) {
117 my $masterfile=srcfile($masterfile_rel);
118 IkiWiki::scan($masterfile_rel);
119 next unless IkiWiki::Plugin::po::istranslatable(pagename($masterfile_rel));
120 my @pofiles=IkiWiki::Plugin::po::pofiles($masterfile);
121 IkiWiki::Plugin::po::refreshpot($masterfile);
122 IkiWiki::Plugin::po::refreshpofiles($masterfile, @pofiles);
123 map IkiWiki::scan(IkiWiki::abs2rel($_, $config{srcdir})), @pofiles;
127 $config{po_link_to}='negotiated';
128 $msgprefix="links (po_link_to=negotiated)";
129 refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
130 is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
131 is_deeply(\@{$links{'index.es'}}, ['translatable.es', 'nontranslatable'], "$msgprefix index.es");
132 is_deeply(\@{$links{'index.fr'}}, ['translatable.fr', 'nontranslatable'], "$msgprefix index.fr");
133 is_deeply(\@{$links{'translatable'}}, ['nontranslatable'], "$msgprefix translatable");
134 is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
135 is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
136 is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable");
138 $config{po_link_to}='current';
139 $msgprefix="links (po_link_to=current)";
140 refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
141 is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
142 is_deeply(\@{$links{'index.es'}}, [ (map bestlink('index.es', $_), ('translatable.es', 'nontranslatable'))], "$msgprefix index.es");
143 is_deeply(\@{$links{'index.fr'}}, [ (map bestlink('index.fr', $_), ('translatable.fr', 'nontranslatable'))], "$msgprefix index.fr");
144 is_deeply(\@{$links{'translatable'}}, [bestlink('translatable', 'nontranslatable')], "$msgprefix translatable");
145 is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
146 is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
147 is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable");
149 ### targetpage
150 $config{usedirs}=0;
151 $msgprefix="targetpage (usedirs=0)";
152 is(targetpage('test1', 'html'), 'test1.en.html', "$msgprefix test1");
153 is(targetpage('test1.fr', 'html'), 'test1.fr.html', "$msgprefix test1.fr");
154 $config{usedirs}=1;
155 $msgprefix="targetpage (usedirs=1)";
156 is(targetpage('index', 'html'), 'index.en.html', "$msgprefix index");
157 is(targetpage('index.fr', 'html'), 'index.fr.html', "$msgprefix index.fr");
158 is(targetpage('test1', 'html'), 'test1/index.en.html', "$msgprefix test1");
159 is(targetpage('test1.fr', 'html'), 'test1/index.fr.html', "$msgprefix test1.fr");
160 is(targetpage('test3', 'html'), 'test3/index.html', "$msgprefix test3 (non-translatable page)");
161 is(targetpage('test3.es', 'html'), 'test3.es/index.html', "$msgprefix test3.es (non-translatable page)");
163 ### urlto -> index
164 $config{po_link_to}='current';
165 $msgprefix="urlto (po_link_to=current)";
166 is(urlto('', 'index'), './index.en.html', "$msgprefix index -> ''");
167 is(urlto('', 'nontranslatable'), '../index.en.html', "$msgprefix nontranslatable -> ''");
168 is(urlto('', 'translatable.fr'), '../index.fr.html', "$msgprefix translatable.fr -> ''");
169 $config{po_link_to}='negotiated';
170 $msgprefix="urlto (po_link_to=negotiated)";
171 is(urlto('', 'index'), './', "$msgprefix index -> ''");
172 is(urlto('', 'nontranslatable'), '../', "$msgprefix nontranslatable -> ''");
173 is(urlto('', 'translatable.fr'), '../', "$msgprefix translatable.fr -> ''");
175 ### bestlink
176 $config{po_link_to}='current';
177 $msgprefix="bestlink (po_link_to=current)";
178 is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
179 is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
180 $config{po_link_to}='negotiated';
181 $msgprefix="bestlink (po_link_to=negotiated)";
182 is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
183 is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
185 ### beautify_urlpath
186 $config{po_link_to}='default';
187 $msgprefix="beautify_urlpath (po_link_to=default)";
188 is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/index.en.html', "$msgprefix test1/index.en.html");
189 is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/index.fr.html', "$msgprefix test1/index.fr.html");
190 $config{po_link_to}='negotiated';
191 $msgprefix="beautify_urlpath (po_link_to=negotiated)";
192 is(IkiWiki::beautify_urlpath('test1/index.html'), './test1/', "$msgprefix test1/index.html");
193 is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/', "$msgprefix test1/index.en.html");
194 is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/', "$msgprefix test1/index.fr.html");
196 ### re-scan
197 refresh_n_scan('index.mdwn');
198 is($pagestate{'index'}{meta}{title}, 'index title');
199 is($pagestate{'index.es'}{meta}{title}, 'index title');
200 is($pagestate{'index.fr'}{meta}{title}, 'index title');
201 refresh_n_scan('test1.mdwn');
202 is($pagestate{'test1'}{meta}{title}, 'test1 title');
203 is($pagestate{'test1.es'}{meta}{title}, 'test1 title');
204 is($pagestate{'test1.fr'}{meta}{title}, 'test1 title');
206 ### istranslatedto
207 ok(IkiWiki::Plugin::po::istranslatedto('index', 'es'));
208 ok(IkiWiki::Plugin::po::istranslatedto('index', 'fr'));
209 ok(! IkiWiki::Plugin::po::istranslatedto('index', 'cz'));
210 ok(IkiWiki::Plugin::po::istranslatedto('test1', 'es'));
211 ok(IkiWiki::Plugin::po::istranslatedto('test1', 'fr'));
212 ok(! IkiWiki::Plugin::po::istranslatedto('test1', 'cz'));
213 ok(! IkiWiki::Plugin::po::istranslatedto('nontranslatable', 'es'));
214 ok(! IkiWiki::Plugin::po::istranslatedto('nontranslatable', 'cz'));
215 ok(! IkiWiki::Plugin::po::istranslatedto('test1.es', 'fr'));
216 ok(! IkiWiki::Plugin::po::istranslatedto('test1.fr', 'es'));