ed56fbc2b29796aac6c9fbfbd33a92524b159309
[bse.git] / t / 060-generate / 020-catalog.t
1 #!perl -w
2 use strict;
3 use BSE::Test ();
4 use Test::More tests=>83;
5 use File::Spec;
6 use FindBin;
7 BEGIN {
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 }
12 use BSE::API qw(bse_init bse_cfg bse_make_catalog bse_make_product);
13
14 bse_init(".");
15
16 my $cfg = bse_cfg();
17 require BSE::Util::SQL;
18 use DevHelp::Date qw(dh_strftime_sql_datetime);
19
20 BSE::DB->init($cfg);
21
22 BSE::Util::SQL->import(qw/sql_datetime/);
23 sub template_test($$$$);
24
25 my $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   );
33 ok($parent, "create parent catalog");
34 my @kids;
35 for 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
42 my $stepkid = add_catalog(title=>'step kid', parentid=>3);
43 ok($stepkid, "adding step catalog");
44 my $stepprod = add_product(title=>'Delta', parentid=>$stepkid->{id},
45                            retailPrice=>400);
46 ok($stepprod, "adding step product");
47
48 my %prices = ( Alpha => 100, Beta => 200, Gamma => 300 );
49 my @prods;
50 for 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
57 require BSE::Admin::StepParents;
58 BSE::Admin::StepParents->add($parent, $stepkid);
59 sleep(2); # make sure they get a new displayOrder
60 BSE::Admin::StepParents->add($parent, $stepprod);
61
62 my $top = BSE::TB::Articles->getByPkey(1);
63 ok($top, "grabbing Home page");
64
65 template_test "children_of", $top, <<TEMPLATE, <<EXPECTED;
66 <:iterator begin children_of $parent->{id}:><:
67 ofchild title:>
68 <:iterator end children_of:>
69 TEMPLATE
70 Gamma
71 Beta
72 Alpha
73 Three
74 Two
75 One
76
77 EXPECTED
78
79 template_test "allkids_of", $top, <<TEMPLATE, <<EXPECTED;
80 <:iterator begin allkids_of $parent->{id}:><:
81 ofallkid title:>
82 <:iterator end allkids_of:>
83 TEMPLATE
84 Delta
85 step kid
86 Gamma
87 Beta
88 Alpha
89 Three
90 Two
91 One
92
93 EXPECTED
94
95 my @kidids = map $_->{id}, @kids;
96 template_test "inlines", $top, <<TEMPLATE, <<EXPECTED;
97 <:iterator begin inlines @kidids:><:
98 inline title:><:iterator end inlines:>
99 TEMPLATE
100 OneTwoThree
101 EXPECTED
102
103 template_test "ifancestor positive", $kids[0], <<TEMPLATE, <<EXPECTED;
104 <:ifAncestor $parent->{id}:>Yes<:or:>No<:eif:>
105 TEMPLATE
106 Yes
107 EXPECTED
108
109 template_test "ifancestor equal", $kids[0], <<TEMPLATE, <<EXPECTED;
110 <:ifAncestor $kids[0]{id}:>Yes<:or:>No<:eif:>
111 TEMPLATE
112 Yes
113 EXPECTED
114
115 template_test "ifancestor negative", $kids[0], <<TEMPLATE, <<EXPECTED;
116 <:ifAncestor $kids[1]{id}:>Yes<:or:>No<:eif:>
117 TEMPLATE
118 No
119 EXPECTED
120
121 template_test "children", $parent, <<TEMPLATE, <<EXPECTED;
122 <:iterator begin children:><:
123 child title:>
124 <:iterator end children:>
125 TEMPLATE
126 Gamma
127 Beta
128 Alpha
129 Three
130 Two
131 One
132
133 EXPECTED
134
135 template_test "embed children", $top, <<TEMPLATE, <<EXPECTED;
136 <:embed $parent->{id} test/children.tmpl:>
137 TEMPLATE
138 Gamma
139 Beta
140 Alpha
141 Three
142 Two
143 One
144
145
146 EXPECTED
147
148 # test some of the newer basic tags
149 template_test "add", $top, <<TEMPLATE, <<EXPECTED;
150 <:add 3 4:>
151 <:add 3 4 5:>
152 <:add 3 [add 4 5]:>
153 TEMPLATE
154 7
155 12
156 12
157 EXPECTED
158
159 template_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"]]:>
164 TEMPLATE
165 onetwo
166 onetwo three
167 onetwo three
168 onetwothree
169 EXPECTED
170
171 template_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":>
176 TEMPLATE
177 123
178 123abc
179
180 default
181 EXPECTED
182
183 template_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
189 123
190 xyz" "\n" "\\n" g:>
191 TEMPLATE
192 abcXXX
193 !!123abc!!
194 XXX XXX
195 abc 123
196 abc\\n123\\nxyz
197 EXPECTED
198
199 template_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":>
205 TEMPLATE
206 abc123 xyz
207 ABC123 XYZ
208 abC123 XYZ
209 ABc123 xyz
210 Alpha Beta Gamma
211 EXPECTED
212
213 template_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]:>
218 TEMPLATE
219 4
220 4
221 2.78
222 <:arithmetic 2+[add 1 2]+[undefinedtag x]+[add 1 1]+[undefinedtag2]:>
223 EXPECTED
224
225 template_test "nobodytext", $kids[0], <<'TEMPLATE', <<EXPECTED;
226 <:nobodytext article body:>
227 TEMPLATE
228 One
229 EXPECTED
230
231 {
232   my $mod = dh_strftime_sql_datetime("%a %d/%m/%Y", $parent->lastModified);
233   template_test "date", $parent, <<'TEMPLATE', <<EXPECTED;
234 <:date "%a %d/%m/%Y" article lastModified:>
235 TEMPLATE
236 $mod
237 EXPECTED
238 }
239
240 template_test "strepeats", $parent, <<'TEMPLATE', <<EXPECTED;
241 <:iterator begin strepeats [arithmetic 1+1]:><:strepeat index:> <:strepeat value:>
242 <:iterator end strepeats:>
243 TEMPLATE
244 0 1
245 1 2
246
247 EXPECTED
248
249 template_test "strepeats2", $parent, <<'TEMPLATE', <<EXPECTED;
250 <:iterator begin strepeats [arithmetic 1+1] 5:><:strepeat index:> <:strepeat value:>
251 <:iterator end strepeats:>
252 TEMPLATE
253 0 2
254 1 3
255 2 4
256 3 5
257
258 EXPECTED
259
260 template_test "ifUnderThreshold parent allcats", $parent, <<TEMPLATE, <<EXPECTED;
261 <:ifUnderThreshold allcats:>1<:or:>0<:eif:>
262 TEMPLATE
263 0
264 EXPECTED
265
266 template_test "ifUnderThreshold parent allprods", $parent, <<TEMPLATE, <<EXPECTED;
267 <:ifUnderThreshold allprods:>1<:or:>0<:eif:>
268 TEMPLATE
269 0
270 EXPECTED
271
272 template_test "variables", $parent, <<TEMPLATE, <<EXPECTED;
273 <:= article.title :>
274 TEMPLATE
275 Test catalog
276 EXPECTED
277
278 BSE::Admin::StepParents->del($parent, $stepkid);
279 BSE::Admin::StepParents->del($parent, $stepprod);
280 for my $kid (reverse @prods, $stepprod) {
281   my $name = $kid->{title};
282   $kid->remove($cfg);
283   ok(1, "removing product $name");
284 }
285 for my $kid (reverse @kids, $stepkid) {
286   my $name = $kid->{title};
287   $kid->remove($cfg);
288   ok(1, "removing kid $name");
289 }
290 $parent->remove($cfg);
291 ok(1, "removed parent");
292
293 sub add_article {
294   my (%parms) = @_;
295
296   my $article = bse_make_article(cfg => $cfg, parentid => -1, %parms);
297
298   $article;
299 }
300
301 sub add_catalog {
302   my (%parms) = @_;
303
304   # this won't put the catalogs in the shop area, but that isn't needed 
305   # for this case.
306   return bse_make_catalog(cfg => $cfg, body => "", %parms);
307 }
308
309 sub add_product {
310   my (%parms) = @_;
311
312   return bse_make_product(cfg => $cfg, %parms);
313 }
314
315 sub template_test($$$$) {
316   my ($tag, $article, $template, $expected) = @_;
317
318   #diag "Template >$template<";
319   my $gen = 
320     eval {
321       (my $filename = $article->{generator}) =~ s!::!/!g;
322       $filename .= ".pm";
323       require $filename;
324       $article->{generator}->new(cfg => $cfg, top => $article);
325     };
326   ok($gen, "$tag: created generator $article->{generator}");
327   diag $@ unless $gen;
328   my $content;
329  SKIP: {
330     skip "$tag: couldn't make generator", 1 unless $gen;
331     eval {
332       $content =
333         $gen->generate_low($template, $article, 'BSE::TB::Articles', 0);
334     };
335     ok($content, "$tag: generate content");
336     diag $@ unless $content;
337   }
338  SKIP: {
339      skip "$tag: couldn't gen content", 1 unless $content;
340      is($content, $expected, "$tag: comparing");
341    }
342 }