add a branches page
[ikiwiki.git] / t / po.t
blobda0bd68a7d89e6177ddc811e503444d75c59c264
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 => 109;
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{url} = "http://example.com";
38 $config{cgiurl} = "http://example.com/ikiwiki.cgi";
39 $config{discussion} = 0;
40 $config{po_master_language} = { code => 'en',
41 name => 'English'
43 $config{po_slave_languages} = {
44 es => 'Castellano',
45 fr => "Français"
47 $config{po_translatable_pages}='index or test1 or test2 or translatable';
48 $config{po_link_to}='negotiated';
49 IkiWiki::loadplugins();
50 ok(IkiWiki::loadplugin('meta'), "meta plugin loaded");
51 ok(IkiWiki::loadplugin('po'), "po plugin loaded");
52 IkiWiki::checkconfig();
54 ### seed %pagesources and %pagecase
55 $pagesources{'index'}='index.mdwn';
56 $pagesources{'index.fr'}='index.fr.po';
57 $pagesources{'index.es'}='index.es.po';
58 $pagesources{'test1'}='test1.mdwn';
59 $pagesources{'test1.es'}='test1.es.po';
60 $pagesources{'test1.fr'}='test1.fr.po';
61 $pagesources{'test2'}='test2.mdwn';
62 $pagesources{'test2.es'}='test2.es.po';
63 $pagesources{'test2.fr'}='test2.fr.po';
64 $pagesources{'test3'}='test3.mdwn';
65 $pagesources{'test3.es'}='test3.es.mdwn';
66 $pagesources{'translatable'}='translatable.mdwn';
67 $pagesources{'translatable.fr'}='translatable.fr.po';
68 $pagesources{'translatable.es'}='translatable.es.po';
69 $pagesources{'nontranslatable'}='nontranslatable.mdwn';
70 foreach my $page (keys %pagesources) {
71 $IkiWiki::pagecase{lc $page}=$page;
74 ### populate srcdir
75 writefile('index.mdwn', $config{srcdir},
76 "[[!meta title=\"index title\"]]\n[[translatable]] [[nontranslatable]]");
77 writefile('test1.mdwn', $config{srcdir},
78 "[[!meta title=\"test1 title\"]]\ntest1 content");
79 writefile('test2.mdwn', $config{srcdir}, 'test2 content');
80 writefile('test3.mdwn', $config{srcdir}, 'test3 content');
81 writefile('translatable.mdwn', $config{srcdir}, '[[nontranslatable]]');
82 writefile('nontranslatable.mdwn', $config{srcdir}, '[[/]] [[translatable]]');
84 ### istranslatable/istranslation
85 # we run these tests twice because memoization attempts made them
86 # succeed once every two tries...
87 foreach (1, 2) {
88 ok(IkiWiki::Plugin::po::istranslatable('index'), "index is translatable");
89 ok(IkiWiki::Plugin::po::istranslatable('/index'), "/index is translatable");
90 ok(! IkiWiki::Plugin::po::istranslatable('index.fr'), "index.fr is not translatable");
91 ok(! IkiWiki::Plugin::po::istranslatable('index.es'), "index.es is not translatable");
92 ok(! IkiWiki::Plugin::po::istranslatable('/index.fr'), "/index.fr is not translatable");
93 ok(! IkiWiki::Plugin::po::istranslation('index'), "index is not a translation");
94 ok(IkiWiki::Plugin::po::istranslation('index.fr'), "index.fr is a translation");
95 ok(IkiWiki::Plugin::po::istranslation('index.es'), "index.es is a translation");
96 ok(IkiWiki::Plugin::po::istranslation('/index.fr'), "/index.fr is a translation");
97 ok(IkiWiki::Plugin::po::istranslatable('test1'), "test1 is translatable");
98 ok(IkiWiki::Plugin::po::istranslation('test1.es'), "test1.es is a translation");
99 ok(IkiWiki::Plugin::po::istranslation('test1.fr'), "test1.fr is a translation");
100 ok(IkiWiki::Plugin::po::istranslatable('test2'), "test2 is translatable");
101 ok(! IkiWiki::Plugin::po::istranslation('test2'), "test2 is not a translation");
102 ok(! IkiWiki::Plugin::po::istranslatable('test3'), "test3 is not translatable");
103 ok(! IkiWiki::Plugin::po::istranslation('test3'), "test3 is not a translation");
106 ### pofiles
108 my @pofiles = IkiWiki::Plugin::po::pofiles(srcfile("index.mdwn"));
109 ok( @pofiles, "pofiles is defined");
110 ok( @pofiles == 2, "pofiles has correct size");
111 is_deeply(\@pofiles, ["$config{srcdir}/index.es.po", "$config{srcdir}/index.fr.po"], "pofiles content is correct");
113 ### links
114 require IkiWiki::Render;
116 sub refresh_n_scan(@) {
117 my @masterfiles_rel=@_;
118 foreach my $masterfile_rel (@masterfiles_rel) {
119 my $masterfile=srcfile($masterfile_rel);
120 IkiWiki::scan($masterfile_rel);
121 next unless IkiWiki::Plugin::po::istranslatable(pagename($masterfile_rel));
122 my @pofiles=IkiWiki::Plugin::po::pofiles($masterfile);
123 IkiWiki::Plugin::po::refreshpot($masterfile);
124 IkiWiki::Plugin::po::refreshpofiles($masterfile, @pofiles);
125 map IkiWiki::scan(IkiWiki::abs2rel($_, $config{srcdir})), @pofiles;
129 $config{po_link_to}='negotiated';
130 $msgprefix="links (po_link_to=negotiated)";
131 refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
132 is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
133 is_deeply(\@{$links{'index.es'}}, ['translatable.es', 'nontranslatable'], "$msgprefix index.es");
134 is_deeply(\@{$links{'index.fr'}}, ['translatable.fr', 'nontranslatable'], "$msgprefix index.fr");
135 is_deeply(\@{$links{'translatable'}}, ['nontranslatable'], "$msgprefix translatable");
136 is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
137 is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
138 is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable");
140 $config{po_link_to}='current';
141 $msgprefix="links (po_link_to=current)";
142 refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
143 is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
144 is_deeply(\@{$links{'index.es'}}, [ (map bestlink('index.es', $_), ('translatable.es', 'nontranslatable'))], "$msgprefix index.es");
145 is_deeply(\@{$links{'index.fr'}}, [ (map bestlink('index.fr', $_), ('translatable.fr', 'nontranslatable'))], "$msgprefix index.fr");
146 is_deeply(\@{$links{'translatable'}}, [bestlink('translatable', 'nontranslatable')], "$msgprefix translatable");
147 is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
148 is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
149 is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable");
151 ### targetpage
152 $config{usedirs}=0;
153 $msgprefix="targetpage (usedirs=0)";
154 is(targetpage('test1', 'html'), 'test1.en.html', "$msgprefix test1");
155 is(targetpage('test1.fr', 'html'), 'test1.fr.html', "$msgprefix test1.fr");
156 $config{usedirs}=1;
157 $msgprefix="targetpage (usedirs=1)";
158 is(targetpage('index', 'html'), 'index.en.html', "$msgprefix index");
159 is(targetpage('index.fr', 'html'), 'index.fr.html', "$msgprefix index.fr");
160 is(targetpage('test1', 'html'), 'test1/index.en.html', "$msgprefix test1");
161 is(targetpage('test1.fr', 'html'), 'test1/index.fr.html', "$msgprefix test1.fr");
162 is(targetpage('test3', 'html'), 'test3/index.html', "$msgprefix test3 (non-translatable page)");
163 is(targetpage('test3.es', 'html'), 'test3.es/index.html', "$msgprefix test3.es (non-translatable page)");
165 ### urlto -> index
166 $config{po_link_to}='current';
167 $msgprefix="urlto (po_link_to=current)";
168 is(urlto('', 'index'), './index.en.html', "$msgprefix index -> ''");
169 is(urlto('', 'nontranslatable'), '../index.en.html', "$msgprefix nontranslatable -> ''");
170 is(urlto('', 'translatable.fr'), '../index.fr.html', "$msgprefix translatable.fr -> ''");
171 # when asking for a semi-absolute or absolute URL, we can't know what the
172 # current language is, so for translatable pages we use the master language
173 is(urlto('nontranslatable'), '/nontranslatable/', "$msgprefix 1-arg -> nontranslatable");
174 is(urlto('translatable'), '/translatable/index.en.html', "$msgprefix 1-arg -> translatable");
175 is(urlto('nontranslatable', undef, 1), 'http://example.com/nontranslatable/', "$msgprefix 1-arg -> nontranslatable");
176 is(urlto('index', undef, 1), 'http://example.com/index.en.html', "$msgprefix 1-arg -> index");
177 is(urlto('', undef, 1), 'http://example.com/index.en.html', "$msgprefix 1-arg -> ''");
178 # FIXME: should these three produce the negotiatable URL instead of the master
179 # language?
180 is(urlto(''), '/index.en.html', "$msgprefix 1-arg -> ''");
181 is(urlto('index'), '/index.en.html', "$msgprefix 1-arg -> index");
182 is(urlto('translatable', undef, 1), 'http://example.com/translatable/index.en.html', "$msgprefix 1-arg -> translatable");
184 $config{po_link_to}='negotiated';
185 $msgprefix="urlto (po_link_to=negotiated)";
186 is(urlto('', 'index'), './', "$msgprefix index -> ''");
187 is(urlto('', 'nontranslatable'), '../', "$msgprefix nontranslatable -> ''");
188 is(urlto('', 'translatable.fr'), '../', "$msgprefix translatable.fr -> ''");
189 is(urlto('nontranslatable'), '/nontranslatable/', "$msgprefix 1-arg -> nontranslatable");
190 is(urlto('translatable'), '/translatable/', "$msgprefix 1-arg -> translatable");
191 is(urlto(''), '/', "$msgprefix 1-arg -> ''");
192 is(urlto('index'), '/', "$msgprefix 1-arg -> index");
193 is(urlto('nontranslatable', undef, 1), 'http://example.com/nontranslatable/', "$msgprefix 1-arg -> nontranslatable");
194 is(urlto('translatable', undef, 1), 'http://example.com/translatable/', "$msgprefix 1-arg -> translatable");
195 is(urlto('index', undef, 1), 'http://example.com/', "$msgprefix 1-arg -> index");
196 is(urlto('', undef, 1), 'http://example.com/', "$msgprefix 1-arg -> ''");
198 ### bestlink
199 $config{po_link_to}='current';
200 $msgprefix="bestlink (po_link_to=current)";
201 is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
202 is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
203 $config{po_link_to}='negotiated';
204 $msgprefix="bestlink (po_link_to=negotiated)";
205 is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
206 is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
208 ### beautify_urlpath
209 $config{po_link_to}='default';
210 $msgprefix="beautify_urlpath (po_link_to=default)";
211 is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/index.en.html', "$msgprefix test1/index.en.html");
212 is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/index.fr.html', "$msgprefix test1/index.fr.html");
213 $config{po_link_to}='negotiated';
214 $msgprefix="beautify_urlpath (po_link_to=negotiated)";
215 is(IkiWiki::beautify_urlpath('test1/index.html'), './test1/', "$msgprefix test1/index.html");
216 is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/', "$msgprefix test1/index.en.html");
217 is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/', "$msgprefix test1/index.fr.html");
218 $config{po_link_to}='current';
219 $msgprefix="beautify_urlpath (po_link_to=current)";
220 is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/index.en.html', "$msgprefix test1/index.en.html");
221 is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/index.fr.html', "$msgprefix test1/index.fr.html");
223 ### re-scan
224 refresh_n_scan('index.mdwn');
225 is($pagestate{'index'}{meta}{title}, 'index title');
226 is($pagestate{'index.es'}{meta}{title}, 'index title');
227 is($pagestate{'index.fr'}{meta}{title}, 'index title');
228 refresh_n_scan('test1.mdwn');
229 is($pagestate{'test1'}{meta}{title}, 'test1 title');
230 is($pagestate{'test1.es'}{meta}{title}, 'test1 title');
231 is($pagestate{'test1.fr'}{meta}{title}, 'test1 title');
233 ### istranslatedto
234 ok(IkiWiki::Plugin::po::istranslatedto('index', 'es'));
235 ok(IkiWiki::Plugin::po::istranslatedto('index', 'fr'));
236 ok(! IkiWiki::Plugin::po::istranslatedto('index', 'cz'));
237 ok(IkiWiki::Plugin::po::istranslatedto('test1', 'es'));
238 ok(IkiWiki::Plugin::po::istranslatedto('test1', 'fr'));
239 ok(! IkiWiki::Plugin::po::istranslatedto('test1', 'cz'));
240 ok(! IkiWiki::Plugin::po::istranslatedto('nontranslatable', 'es'));
241 ok(! IkiWiki::Plugin::po::istranslatedto('nontranslatable', 'cz'));
242 ok(! IkiWiki::Plugin::po::istranslatedto('test1.es', 'fr'));
243 ok(! IkiWiki::Plugin::po::istranslatedto('test1.fr', 'es'));