/
TopicTemplatesTests.pm
300 lines (266 loc) · 9.75 KB
/
TopicTemplatesTests.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package TopicTemplatesTests;
use strict;
use warnings;
use FoswikiFnTestCase();
our @ISA = qw( FoswikiFnTestCase );
use Foswiki();
use Foswiki::UI::Save();
use Unit::Request();
use Error qw( :try );
my $UI_FN;
my $testform1 = <<'HERE';
| *Name* | *Type* | *Size* | *Values* | *Tooltip message* | *Attributes* |
| Select | select | 1 | Value_1, Value_2, *Value_3* | |
| Radio | radio | 3 | 1, 2, 3 | |
| Checkbox | checkbox | 3 | red,blue,green | |
| Checkbox and Buttons | checkbox+buttons | 3 | dog,cat,bird,hamster,goat,horse | |
| Textfield | text | 60 | test | |
HERE
my $testtext1 = <<'HERE';
A guest of this Foswiki web, not unlike yourself. You can leave your trace behind you, just add your name in %SYSTEMWEB%.UserRegistration and create your own page.
%META:PREFERENCE{name="VIEW_TEMPLATE" title="VIEW_TEMPLATE" value="UserTopic"}%
HERE
sub new {
my ( $class, @args ) = @_;
return $class->SUPER::new( 'TopicTemplates', @args );
}
# Set up the test fixture
sub set_up {
my $this = shift;
$this->SUPER::set_up();
$UI_FN ||= $this->getUIFn('save');
my ($topicObject) =
Foswiki::Func::readTopic( $this->{test_web}, 'TestForm1' );
$topicObject->text($testform1);
$topicObject->save();
$topicObject->finish();
($topicObject) =
Foswiki::Func::readTopic( $this->{test_web},
$Foswiki::cfg{WebPrefsTopicName} );
$topicObject->text(<<'CONTENT');
* Set WEBFORMS = TestForm1
CONTENT
$topicObject->save();
$topicObject->finish();
return;
}
sub test_templateTopicTextSave {
my $this = shift;
my $query = Unit::Request->new(
{
text => ['Template Topic'],
action => ['save'],
topic => [ $this->{test_web} . '.TemplateTopic' ]
}
);
$this->createNewFoswikiSession( $this->{test_user_login}, $query );
$this->captureWithKey( save => $UI_FN, $this->{session} );
$query = Unit::Request->new(
{
templatetopic => ['TemplateTopic'],
action => ['save'],
topic => [ $this->{test_web} . '.TemplateTopic' ]
}
);
$this->createNewFoswikiSession( $this->{test_user_login}, $query );
$this->captureWithKey( save => $UI_FN, $this->{session} );
my ($meta) = Foswiki::Func::readTopic( $this->{test_web}, 'TemplateTopic' );
my $text = $meta->text;
$this->assert_matches( qr/Template Topic/, $text );
$this->assert_null( $meta->get('FORM') );
$meta->finish();
return;
}
sub test_templateTopicFormSave {
my $this = shift;
my $query = Unit::Request->new(
{
text => ['Template Topic'],
formtemplate => ['TestForm1'],
'Select' => ['Value_1'],
'Textfield' => ['Fred'],
action => ['save'],
topic => [ $this->{test_web} . '.TemplateTopic' ]
}
);
$this->createNewFoswikiSession( $this->{test_user_login}, $query );
$this->captureWithKey( save => $UI_FN, $this->{session} );
my ($xmeta) =
Foswiki::Func::readTopic( $this->{test_web}, 'TemplateTopic' );
my $xtext = $xmeta->text;
$xmeta->finish();
$query = Unit::Request->new(
{
templatetopic => ['TemplateTopic'],
action => ['save'],
topic => [ $this->{test_web} . '.TemplateTopicAgain' ]
}
);
$this->createNewFoswikiSession( $this->{test_user_login}, $query );
$this->captureWithKey( save => $UI_FN, $this->{session} );
my ($meta) =
Foswiki::Func::readTopic( $this->{test_web}, 'TemplateTopicAgain' );
my $text = $meta->text;
$this->assert_matches( qr/Template Topic/, $text );
$this->assert_str_equals( 'TestForm1', $meta->get('FORM')->{name} );
$this->assert_str_equals( 'Value_1',
$meta->get( 'FIELD', 'Select' )->{value} );
$this->assert_str_equals( 'Fred',
$meta->get( 'FIELD', 'Textfield' )->{value} );
$meta->finish();
return;
}
# meta data (other than FORM, FIELD, TOPICPARENT, etc.) is inherited from
# templatetopic
sub test_templateTopicWithMeta {
my $this = shift;
Foswiki::Func::saveTopicText( $this->{test_web}, "TemplateTopic",
$testtext1 );
my $query = Unit::Request->new(
{
templatetopic => ['TemplateTopic'],
action => ['save'],
topic => [ $this->{test_web} . '.TemplateTopicWithMeta' ]
}
);
$this->createNewFoswikiSession( $this->{test_user_login}, $query );
$this->captureWithKey( save => $UI_FN, $this->{session} );
my ($meta) =
Foswiki::Func::readTopic( $this->{test_web}, 'TemplateTopicWithMeta' );
my $text = $meta->text;
my $pref = $meta->get( 'PREFERENCE', 'VIEW_TEMPLATE' );
$this->assert_not_null($pref);
$this->assert_str_equals( 'UserTopic', $pref->{value} );
$meta->finish();
return;
}
# attachments are copied over from templatetopic
sub test_templateTopicWithAttachments {
my $this = shift;
$this->assert(
open( my $FILE, ">", "$Foswiki::cfg{TempfileDir}/testfile.txt" ) );
print $FILE "one two three";
$this->assert( close($FILE) );
$this->assert(
open( $FILE, ">", "$Foswiki::cfg{TempfileDir}/testfile2.txt" ) );
print $FILE "four five six";
$this->assert( close($FILE) );
my $templateTopic = "TemplateTopic";
my $testTopic = "TemplateTopicWithAttachment";
Foswiki::Func::saveTopic( $this->{test_web}, $templateTopic, undef,
"test with an attachment" );
Foswiki::Func::saveAttachment(
$this->{test_web},
$templateTopic,
"testfile.txt",
{
file => "$Foswiki::cfg{TempfileDir}/testfile.txt",
comment => "a comment"
}
);
Foswiki::Func::saveAttachment(
$this->{test_web},
$templateTopic,
"testfile2.txt",
{
file => "$Foswiki::cfg{TempfileDir}/testfile2.txt",
comment => "a comment"
}
);
my $query = Unit::Request->new(
{
templatetopic => ['TemplateTopic'],
action => ['save'],
topic => ["$this->{test_web}.$testTopic"]
}
);
$this->createNewFoswikiSession( $this->{test_user_login}, $query );
$this->captureWithKey( save => $UI_FN, $this->{session} );
my ( $meta, $text ) =
Foswiki::Func::readTopic( $this->{test_web}, $testTopic );
$this->assert_matches( qr/test with an attachment/, $text );
$this->assert_not_null(
$meta->get( 'FILEATTACHMENT', 'testfile.txt' ),
"attachment meta copied for testfile.txt"
);
$this->assert_not_null(
$meta->get( 'FILEATTACHMENT', 'testfile2.txt' ),
"attachment meta copied for testfile2.txt"
);
$this->assert( $meta->testAttachment( "testfile.txt", 'e' ),
"testfile.txt copied" );
$this->assert( $meta->testAttachment( "testfile2.txt", 'e' ),
"testfile2.txt copied" );
$meta->finish();
return;
}
sub test_templateTopicWithMacros {
my $this = shift;
my $query = Unit::Request->new(
{
text => [<<'TEXT'],
* NOP: No%NOP%Link
* DATE: %DATE%
* TMPL:DATE: %TMPL:DATE%
* GMTIME: %GMTIME%
* SERVERTIME: %SERVERTIME%
* USERNAME: %USERNAME%
* TMPL:USERNAME: %TMPL:USERNAME%
* URLPARAM: %URLPARAM{"purple"}%
* WIKINAME: %WIKINAME%
* WIKIUSERNAME: %WIKIUSERNAME%
* COMMENT: #{ ... comment }#
* TMPL:IF: %TMPL:IF{"1=1" then="OK" else="BAD"}%
* TMPL:P:%TMPL:P{"sep"}%
%STARTSECTION{type="templateonly"}%
Mither me not
%ENDSECTION{type="templateonly"}%
%STARTSECTION{type="expandvariables"}%
%TOPIC%
%ENDSECTION{type="expandvariables"}%
%TOPIC%
TEXT
action => ['save'],
topic => [ $this->{test_web} . '.TemplateTopic' ]
}
);
$this->createNewFoswikiSession( $this->{test_user_login}, $query );
$this->captureWithKey( save => $UI_FN, $this->{session} );
$query = Unit::Request->new(
{
templatetopic => ['TemplateTopic'],
action => ['save'],
purple => ['ok'],
topic => [ $this->{test_web} . '.TemplatedTopic' ]
}
);
$this->createNewFoswikiSession( $this->{test_user_login}, $query );
my ( $responseText, $result, $stdout, $stderr ) =
$this->captureWithKey( save => $UI_FN, $this->{session} );
print STDERR $stderr;
my ($meta) =
Foswiki::Func::readTopic( $this->{test_web}, 'TemplatedTopic' );
my $text = $meta->text;
$this->assert( $text =~ s/^\s*\* DATE: \d+ \w+ \d+$//m, $text );
$this->assert( $text =~ s/^\s*\* TMPL:DATE: \d+ \w+ \d+$//m, $text );
$this->assert( $text =~ s/^\s*\* GMTIME: \d+ \w+ \d+ - \d+:\d+$//m, $text );
$this->assert( $text =~ s/^\s*\* SERVERTIME: \d+ \w+ \d+ - \d+:\d+$//m,
$text );
$this->assert( $text =~ s/^\s*\* USERNAME: scum$//m, $text );
$this->assert( $text =~ s/^\s*\* TMPL:USERNAME: scum$//m, $text );
$this->assert( $text =~ s/^\s*\* WIKINAME: ScumBag$//m, $text );
$this->assert( $text =~ s/^\s*\* WIKIUSERNAME: \w+\.ScumBag$//m, $text );
$this->assert( $text =~ s/^\s*\* URLPARAM: ok$//m, $text );
$this->assert( $text =~ s/^\s*\* NOP: NoLink$//m, $text );
$this->assert( $text =~ s/^\s*\* TMPL:IF: OK$//m, $text );
$this->assert( $text =~ s/^\s*\* TMPL:P: \| $//m, $text );
$this->assert( $text =~ s/^\s*\* COMMENT: $//m, $text );
$this->assert( $text =~ s/^TemplatedTopic$//m, $text );
$this->assert( $text =~ s/^%TOPIC%$//m, $text );
$this->assert( $text !~ /Mither me not/s, $text );
$text =~ s/\s+//gs;
$this->assert_equals( "", $text );
$meta->finish();
return;
}
1;