fix generator.vembed()
[bse.git] / t / 060-generate / 020-catalog.t
CommitLineData
a5e3fc4b
TC
1#!perl -w
2use strict;
3use BSE::Test ();
63dc4fe2 4use Test::More tests=>86;
a5e3fc4b
TC
5use File::Spec;
6use FindBin;
810ce080
TC
7BEGIN {
8 my $cgidir = File::Spec->catdir(BSE::Test::base_dir, 'cgi-bin');
9 ok(chdir $cgidir, "switch to CGI directory");
10 push @INC, 'modules';
11}
12use BSE::API qw(bse_init bse_cfg bse_make_catalog bse_make_product);
13
14bse_init(".");
15
16my $cfg = bse_cfg();
a5e3fc4b 17require BSE::Util::SQL;
810ce080 18use DevHelp::Date qw(dh_strftime_sql_datetime);
3dd50c7c
TC
19
20BSE::DB->init($cfg);
21
a5e3fc4b
TC
22BSE::Util::SQL->import(qw/sql_datetime/);
23sub template_test($$$$);
24
fd1b40b1
TC
25my $parent = add_catalog
26 (
27 title=>'Test catalog',
28 body=>'test catalog',
29 parentid => 3,
30 lastModified => '2004-09-23 06:00:00',
31 threshold => 2,
32 );
a5e3fc4b
TC
33ok($parent, "create parent catalog");
34my @kids;
35for my $name ('One', 'Two', 'Three') {
36 my $kid = add_catalog(title => $name, parentid => $parent->{id},
37 body => "b[$name]");
38 ok($kid, "creating kid catalog $name");
39 push(@kids, $kid);
40}
41
42my $stepkid = add_catalog(title=>'step kid', parentid=>3);
43ok($stepkid, "adding step catalog");
44my $stepprod = add_product(title=>'Delta', parentid=>$stepkid->{id},
45 retailPrice=>400);
46ok($stepprod, "adding step product");
47
48my %prices = ( Alpha => 100, Beta => 200, Gamma => 300 );
49my @prods;
50for my $name (qw(Alpha Beta Gamma)) {
51 my $prod = add_product(title=>$name, retailPrice => $prices{$name},
52 parentid => $parent->{id});
53 ok($prod, "creating kid product $name");
54 push @prods, $prod;
55}
56
57require BSE::Admin::StepParents;
58BSE::Admin::StepParents->add($parent, $stepkid);
0da5d199 59sleep(2); # make sure they get a new displayOrder
a5e3fc4b
TC
60BSE::Admin::StepParents->add($parent, $stepprod);
61
e0ed81d7 62my $top = BSE::TB::Articles->getByPkey(1);
a5e3fc4b
TC
63ok($top, "grabbing Home page");
64
65template_test "children_of", $top, <<TEMPLATE, <<EXPECTED;
66<:iterator begin children_of $parent->{id}:><:
67ofchild title:>
68<:iterator end children_of:>
69TEMPLATE
70Gamma
71Beta
72Alpha
73Three
74Two
75One
76
77EXPECTED
78
79template_test "allkids_of", $top, <<TEMPLATE, <<EXPECTED;
80<:iterator begin allkids_of $parent->{id}:><:
81ofallkid title:>
82<:iterator end allkids_of:>
83TEMPLATE
a5e3fc4b 84Delta
0da5d199 85step kid
a5e3fc4b
TC
86Gamma
87Beta
88Alpha
89Three
90Two
91One
92
93EXPECTED
94
95my @kidids = map $_->{id}, @kids;
96template_test "inlines", $top, <<TEMPLATE, <<EXPECTED;
97<:iterator begin inlines @kidids:><:
98inline title:><:iterator end inlines:>
99TEMPLATE
100OneTwoThree
101EXPECTED
102
103template_test "ifancestor positive", $kids[0], <<TEMPLATE, <<EXPECTED;
104<:ifAncestor $parent->{id}:>Yes<:or:>No<:eif:>
105TEMPLATE
106Yes
107EXPECTED
108
109template_test "ifancestor equal", $kids[0], <<TEMPLATE, <<EXPECTED;
110<:ifAncestor $kids[0]{id}:>Yes<:or:>No<:eif:>
111TEMPLATE
112Yes
113EXPECTED
114
115template_test "ifancestor negative", $kids[0], <<TEMPLATE, <<EXPECTED;
116<:ifAncestor $kids[1]{id}:>Yes<:or:>No<:eif:>
117TEMPLATE
118No
119EXPECTED
120
121template_test "children", $parent, <<TEMPLATE, <<EXPECTED;
122<:iterator begin children:><:
123child title:>
124<:iterator end children:>
125TEMPLATE
126Gamma
127Beta
128Alpha
129Three
130Two
131One
132
133EXPECTED
134
135template_test "embed children", $top, <<TEMPLATE, <<EXPECTED;
136<:embed $parent->{id} test/children.tmpl:>
137TEMPLATE
138Gamma
139Beta
140Alpha
141Three
142Two
143One
144
145
146EXPECTED
147
148# test some of the newer basic tags
149template_test "add", $top, <<TEMPLATE, <<EXPECTED;
150<:add 3 4:>
151<:add 3 4 5:>
152<:add 3 [add 4 5]:>
153TEMPLATE
1547
15512
15612
157EXPECTED
158
159template_test "concatenate", $top, <<TEMPLATE, <<EXPECTED;
160<:concatenate one two:>
161<:concatenate one "two " three:>
162<:concatenate one [concatenate "two " three]:>
163<:concatenate [concatenate "one" [concatenate "two" "three"]]:>
164TEMPLATE
165onetwo
166onetwo three
167onetwo three
168onetwothree
169EXPECTED
170
171template_test "match", $top, <<'TEMPLATE', <<EXPECTED;
172<:match "abc123" "(\d+)":>
173<:match "abc 123" "(\w+)\s+(\w+)" "$2$1":>
174<:match "abc 123" "(\w+)X(\w+)" "$2$1":>
175<:match "abc 123" "(\w+)X(\w+)" "$2$1" "default":>
176TEMPLATE
177123
178123abc
179
180default
181EXPECTED
182
183template_test "replace", $top, <<'TEMPLATE', <<EXPECTED;
184<:replace "abc123" "(\d+)" "XXX" :>
185<:replace "!!abc 123!!" "(\w+)\s+(\w+)" "$2$1":>
186<:replace "abc 123" "(\w+)" "XXX" g:>
187<:replace "abc 123" "X" "$1" :>
188<:replace "abc
189123
190xyz" "\n" "\\n" g:>
191TEMPLATE
192abcXXX
193!!123abc!!
194XXX XXX
195abc 123
196abc\\n123\\nxyz
197EXPECTED
198
199template_test "cases", $top, <<'TEMPLATE', <<EXPECTED;
200<:lc "AbC123 XYZ":>
201<:uc "aBc123 xyz":>
202<:lcfirst "AbC123 XYZ":>
203<:ucfirst "aBc123 xyz":>
204<:capitalize "alpha beta gamma":>
205TEMPLATE
206abc123 xyz
207ABC123 XYZ
208abC123 XYZ
209ABc123 xyz
210Alpha Beta Gamma
211EXPECTED
212
213template_test "arithmetic", $top, <<'TEMPLATE', <<EXPECTED;
214<:arithmetic 2+2:>
215<:arithmetic 2+[add 1 1]:>
216<:arithmetic d2:1.234+1.542:>
217<:arithmetic 2+[add 1 2]+[undefinedtag x]+[add 1 1]+[undefinedtag2]:>
218TEMPLATE
2194
2204
2212.78
b7af1823 222<:arithmetic 2+[add 1 2]+[undefinedtag x]+[add 1 1]+[undefinedtag2]:>
a5e3fc4b
TC
223EXPECTED
224
225template_test "nobodytext", $kids[0], <<'TEMPLATE', <<EXPECTED;
226<:nobodytext article body:>
227TEMPLATE
228One
229EXPECTED
230
810ce080
TC
231{
232 my $mod = dh_strftime_sql_datetime("%a %d/%m/%Y", $parent->lastModified);
233 template_test "date", $parent, <<'TEMPLATE', <<EXPECTED;
a5e3fc4b
TC
234<:date "%a %d/%m/%Y" article lastModified:>
235TEMPLATE
810ce080 236$mod
a5e3fc4b 237EXPECTED
810ce080 238}
a5e3fc4b
TC
239
240template_test "strepeats", $parent, <<'TEMPLATE', <<EXPECTED;
241<:iterator begin strepeats [arithmetic 1+1]:><:strepeat index:> <:strepeat value:>
242<:iterator end strepeats:>
243TEMPLATE
2440 1
2451 2
246
247EXPECTED
248
249template_test "strepeats2", $parent, <<'TEMPLATE', <<EXPECTED;
250<:iterator begin strepeats [arithmetic 1+1] 5:><:strepeat index:> <:strepeat value:>
251<:iterator end strepeats:>
252TEMPLATE
2530 2
2541 3
2552 4
2563 5
257
258EXPECTED
259
fd1b40b1
TC
260template_test "ifUnderThreshold parent allcats", $parent, <<TEMPLATE, <<EXPECTED;
261<:ifUnderThreshold allcats:>1<:or:>0<:eif:>
262TEMPLATE
2630
264EXPECTED
265
266template_test "ifUnderThreshold parent allprods", $parent, <<TEMPLATE, <<EXPECTED;
267<:ifUnderThreshold allprods:>1<:or:>0<:eif:>
268TEMPLATE
2690
270EXPECTED
271
eff5731a
TC
272template_test "variables", $parent, <<TEMPLATE, <<EXPECTED;
273<:= article.title :>
274TEMPLATE
275Test catalog
276EXPECTED
277
63dc4fe2
TC
278template_test "vembed", $prods[0], <<TEMPLATE, <<EXPECTED;
279<:= generator.vembed(article.parent, "test/title.tmpl") |raw:>
280TEMPLATE
281Test catalog
282EXPECTED
283
a5e3fc4b
TC
284BSE::Admin::StepParents->del($parent, $stepkid);
285BSE::Admin::StepParents->del($parent, $stepprod);
57d988af
TC
286for my $kid (reverse @prods, $stepprod) {
287 my $name = $kid->{title};
16901a2a 288 $kid->remove($cfg);
57d988af
TC
289 ok(1, "removing product $name");
290}
291for my $kid (reverse @kids, $stepkid) {
a5e3fc4b 292 my $name = $kid->{title};
16901a2a 293 $kid->remove($cfg);
a5e3fc4b
TC
294 ok(1, "removing kid $name");
295}
16901a2a 296$parent->remove($cfg);
a5e3fc4b
TC
297ok(1, "removed parent");
298
a5e3fc4b
TC
299sub add_article {
300 my (%parms) = @_;
a5e3fc4b 301
810ce080 302 my $article = bse_make_article(cfg => $cfg, parentid => -1, %parms);
a5e3fc4b
TC
303
304 $article;
305}
306
307sub add_catalog {
308 my (%parms) = @_;
309
310 # this won't put the catalogs in the shop area, but that isn't needed
311 # for this case.
810ce080 312 return bse_make_catalog(cfg => $cfg, body => "", %parms);
a5e3fc4b
TC
313}
314
315sub add_product {
316 my (%parms) = @_;
317
810ce080 318 return bse_make_product(cfg => $cfg, %parms);
a5e3fc4b
TC
319}
320
321sub template_test($$$$) {
322 my ($tag, $article, $template, $expected) = @_;
323
324 #diag "Template >$template<";
325 my $gen =
326 eval {
327 (my $filename = $article->{generator}) =~ s!::!/!g;
328 $filename .= ".pm";
329 require $filename;
330 $article->{generator}->new(cfg => $cfg, top => $article);
331 };
332 ok($gen, "$tag: created generator $article->{generator}");
333 diag $@ unless $gen;
334 my $content;
335 SKIP: {
336 skip "$tag: couldn't make generator", 1 unless $gen;
337 eval {
338 $content =
ab004bbb 339 $gen->generate_low($template, $article, 'BSE::TB::Articles', 0);
a5e3fc4b
TC
340 };
341 ok($content, "$tag: generate content");
342 diag $@ unless $content;
343 }
344 SKIP: {
345 skip "$tag: couldn't gen content", 1 unless $content;
346 is($content, $expected, "$tag: comparing");
347 }
348}