-
Notifications
You must be signed in to change notification settings - Fork 16
/
zafu_compiler_test.rb
414 lines (370 loc) · 12.3 KB
/
zafu_compiler_test.rb
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
require 'test_helper'
class ZafuCompilerTest < Zena::Controller::TestCase
include Zena::Use::Urls::Common
OK = [
'action',
'asset',
'alias_site',
'conditional',
'calendar',
'dates',
'display',
'errors',
'i18n',
'idx_scope',
'query',
'recursion',
'relations',
'roles',
'rubyless',
'safe_definitions',
'search',
'site',
'tags', # bricks/tags/zena/test/zafu
'spreadsheet', # bricks/spreadsheet/zena/test/spreadsheet
'worker', # bricks/worker/zena/test/zafu
'urls',
'user',
'version',
'workflow',
'zafu_attributes',
'zazen',
'security',
'ajax',
'complex_ok',
'forms',
'meta',
'context',
'comments',
'eval',
]
BUG = [
'apphelper',
'basic',
'complex',
'data',
'complex',
]
LATER = %w{later}
yamltest :directories => ([:default] + Bricks.zafu_tests), :files => OK
Section # make sure we load Section links before trying relations
class ZafuDummy
include RubyLess
safe_method [:hello, {:lang => String}] => String
def hello(opts)
case opts[:lang]
when 'en'
'Hi there!'
when 'fr'
'Salut poilu!'
when 'de'
'Grützi!'
else
"Sorry, I don't speak #{opts[:lang]}..."
end
end
end
class ::Node
def dummy
ZafuDummy.new
end
end
RubyLess::SafeClass.safe_method_for Node, [:dummy] => ZafuDummy
def setup
@controller = Zena::TestController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
super
end
def yt_do_test(file, test)
# Disable defined tests without loaded files
return unless test_data = (@@test_strings[file] || {})[test]
if test_data.keys.include?('src')
# we do not want src built from title
src = yt_get('src', file, test)
elsif src = yt_get('eval', file, test)
src = test_data['src'] = "<r:eval>#{src}</r:eval>"
end
context = yt_get('context', file, test)
site = Site.setup_master(Site.find_by_host(context.delete('host') || 'test.host'))
# This is used to find fixtures: returns master name (not alias)
$_test_site = site.name
@request.host = site.host
# set context
params = {}
#params[:user_id] = users_id(context.delete('visitor').to_sym)
params['user'] = context.delete 'visitor'
params['node_id'] = nodes_id(context.delete('node').to_sym)
params['prefix'] = context.delete('lang')
params['date'] = context['ref_date'] ? context.delete('ref_date').to_s : nil
params['url'] = "#{file}/#{test.to_s.gsub('_', '/')}"
params.merge!(context) # merge the rest of the context as query parameters
compiled_files = {}
test_data.each do |k,v|
next if ['src','tem','res','context','eval','js'].include?(k) || k =~ /^old/
compiled_files[k] = v
end
tem = yt_get('tem', file, test)
res = yt_get('res', file, test)
js = yt_get('js', file, test)
Zena::TestController.templates = @@test_strings[file]
if src
post 'test_compile', params
template = @response.body
if tem
yt_assert tem, template
end
else
template = tem
end
compiled_files.each do |path,value|
fullpath = File.join(SITES_ROOT,'test.host','zafu',path)
assert File.exist?(fullpath), "Saved template #{path} should exist."
yt_assert value, File.read(fullpath)
end
if res
params['text'] = template
post 'test_render', params
result = @response.body
yt_assert res, result
if js
yt_assert js, @controller.send(:render_js)
end
elsif js
params['text'] = template
post 'test_render', params
yt_assert js, @controller.send(:render_js)
end
end
alias o_yt_assert yt_assert
def yt_assert(test_val, result)
test_val.gsub!(/_ID\(([^\)]+)\)/) do
Zena::FoxyParser::id($_test_site, $1)
end
o_yt_assert test_val, result
end
# turned part caching off
# def test_basic_cache_part
# test_site('zena')
# with_caching do
# Node.connection.execute "UPDATE nodes SET node_name = 'first' WHERE id = #{nodes_id(:status)}"
# caches = Cache.find(:all)
# assert_equal [], caches
# yt_do_test('basic', 'cache_part')
#
# cont = {
# :user_id => users_id(:anon),
# :user => 'anon',
# :node_id => nodes_id(:status),
# :prefix => 'en',
# :url => '/cache/part',
# :text => @response.body
# }.freeze
#
# post 'test_render', cont
# assert_equal 'first', @response.body
#
# cache = Cache.find(:first)
# assert_kind_of Cache, cache
# assert_equal "first", cache.content
# Node.connection.execute "UPDATE nodes SET node_name = 'second' WHERE id = #{nodes_id(:status)}"
#
# post 'test_render', cont
# assert_equal 'first', @response.body
#
# Node.connection.execute "DELETE FROM #{Cache.table_name};"
#
# post 'test_render', cont
# assert_equal 'second', @response.body
# end
# end
def test_relations_updated_today
test_site('zena')
Node.connection.execute "UPDATE nodes SET updated_at = #{Zena::Db::NOW} WHERE id IN (#{nodes_id(:status)}, #{nodes_id(:art)});"
yt_do_test('relations', 'updated_today')
end
def test_relations_upcoming_events
set_date(:people, :days => 7)
yt_do_test('relations', 'upcoming_events')
end
def test_relations_in_7_days
set_date(:art)
set_date([:projects, :cleanWater], :days => 6)
set_date([:people], :days => 10)
yt_do_test('relations', 'in_7_days')
end
def test_relations_logged_7_days_ago
set_date([:art, :status], :minutes => 2)
set_date([:projects, :cleanWater], :days => -6)
set_date([:people], :days => -10)
yt_do_test('relations', 'logged_7_days_ago')
end
def test_relations_around_7_days
set_date(:status)
set_date(:art, :days => 5)
set_date([:projects, :cleanWater], :days => -6)
set_date([:people], :days => -10)
yt_do_test('relations', 'around_7_days')
end
def test_relations_in_37_hours
set_date(:art, :minutes => 2)
set_date(:cleanWater, :hours => 36)
set_date([:projects, :people], :hours => 38)
yt_do_test('relations', 'in_37_hours')
end
def test_relations_this_week
if Time.now.strftime('%u').to_i < 3
# not in this week
set_date(:people, :days => -5, :fld => 'event_at')
# in this week
set_date(:art, :days => 2, :fld => 'event_at')
set_date(:projects, :days => 1, :fld => 'event_at')
else
# not in this week
set_date(:people, :days => 5, :fld => 'event_at')
# in this week
set_date(:art, :days => -2, :fld => 'event_at')
set_date(:projects, :days => -1, :fld => 'event_at')
end
yt_do_test('relations', 'this_week')
end
def test_relations_this_month
if Time.now.strftime('%d').to_i < 15
# not in this month
set_date(:people, :days => -20, :fld => 'event_at')
# in this month
set_date(:art, :days => 12, :fld => 'event_at')
set_date(:projects, :days => 5, :fld => 'event_at')
else
# not in this month
set_date(:people, :days => 20, :fld => 'event_at')
# in this month
set_date(:art, :days => -12, :fld => 'event_at')
set_date(:projects, :days => -5, :fld => 'event_at')
end
yt_do_test('relations', 'this_month')
end
def test_relations_this_year
if Time.now.strftime('%m').to_i < 6
# not in this year
set_date(:people, :months => -8, :fld => 'event_at')
# in this year
set_date(:art, :months => 2, :fld => 'event_at')
set_date(:projects, :months => 1, :fld => 'event_at')
else
# not in this year
set_date(:people, :months => 8, :fld => 'event_at')
# in this year
set_date(:art, :months => -2, :fld => 'event_at')
set_date(:projects, :months => -1, :fld => 'event_at')
end
yt_do_test('relations', 'this_year')
end
def test_relations_direction_both
test_site('zena')
art, projects, status = nodes_id(:art), nodes_id(:projects), nodes_id(:status)
values = [
[art, status, relations_id(:node_has_references)],
[status, projects, relations_id(:node_has_references)]
]
Zena::Db.insert_many('links', %W{source_id target_id relation_id}, values)
yt_do_test('relations', 'direction_both')
end
def test_relations_direction_both_self_auto_ref
test_site('zena')
art, projects, status = nodes_id(:art), nodes_id(:projects), nodes_id(:status)
values = [
[art, status, relations_id(:node_has_references)],
[status, status, relations_id(:node_has_references)],
[status, projects, relations_id(:node_has_references)]
]
Zena::Db.insert_many('links', %W{source_id target_id relation_id}, values)
yt_do_test('relations', 'direction_both_self_auto_ref')
end
def test_recursion_in_each
login(:tiger)
node = secure!(Node) { nodes(:status) }
node.unpublish
yt_do_test('recursion', 'in_each')
end
def test_zazen_swf_button_player
login(:tiger)
node = secure!(Node) { nodes(:water_pdf) }
node.prop['ext'] = 'mp3'
node.send(:dump_properties)
Zena::Db.execute "UPDATE versions SET properties = #{Zena::Db.quote(node.version[:properties])}"
yt_do_test('zazen', 'swf_button_player')
end
def test_dates_uses_datebox_missing_lang
login(:ant)
visitor.lang = 'io'
yt_do_test('dates', 'uses_datebox_missing_lang')
end
def test_display_defined_icon
return unless test = (@@test_strings['display'] || {})['defined_icon']
login(:tiger)
# define flower as icon
node = secure!(Node) { nodes(test['context']['node'].to_sym)}
assert node.update_attributes(:icon_id => nodes_id(:flower_jpg))
yt_do_test('display', 'defined_icon')
end
def test_relations_same_name_as_class
login(:lion)
rel = Relation.create(
:source_kpath => 'NP',
:source_role => '',
:target_kpath => 'NNL',
:target_role => 'live_letter'
)
assert !rel.new_record?
yt_do_test('relations', 'same_name_as_class')
end
def test_dates_time_year
login(:lion)
node = secure(Node) { nodes(:opening) }
node.update_attributes!(:date => Time.utc(2012,1,1,0,0))
yt_do_test('dates', 'time_year')
end
def test_display_with_a_custom_img_tag_field
login(:lion)
node = secure(Node) { nodes(:status) }
node.prop['img_tag_std'] = "<foobar>PATH=PATH_std.html uuid=UUID</foobar>"
node.send(:dump_properties)
Zena::Db.execute "UPDATE versions SET properties = #{Zena::Db.quote(node.version[:properties])}"
yt_do_test('display', 'with_a_custom_img_tag_field')
end
def test_display_with_a_custom_img_tag_field_with_JS
login(:lion)
node = secure(Node) { nodes(:status) }
node.prop['img_tag_std'] = "blah blah [JS]some js[/JS]blah blah [JS]more JS[/JS]"
node.send(:dump_properties)
Zena::Db.execute "UPDATE versions SET properties = #{Zena::Db.quote(node.version[:properties])}"
yt_do_test('display', 'with_a_custom_img_tag_field_with_JS')
end
def test_alias_site_from_home
login(:lion)
node = secure(Page) { Page.create(:title => 'one', :parent_id => nodes_id(:wiki))}
sub = secure(Page) { Page.create(:title => 'two', :parent_id => node.id)}
secure(Page) { Page.create(:title => 'three', :parent_id => sub.id)}
yt_do_test('alias_site', 'from_home')
end
def test_alias_site_in_home
login(:lion)
node = secure(Page) { Page.create(:title => 'one', :parent_id => nodes_id(:wiki))}
sub = secure(Page) { Page.create(:title => 'two', :parent_id => node.id)}
secure(Page) { Page.create(:title => 'three', :parent_id => sub.id)}
yt_do_test('alias_site', 'in_home')
end
def test_alias_site_link_with_custom_base
login(:lion)
secure(Node) { nodes(:wiki) }.tap do |w|
w.update_attributes(:custom_base => true)
err w
end
node = secure(Page) { Page.create(:title => 'one', :parent_id => nodes_id(:wiki), :custom_base => true)}
sub = secure(Page) { Page.create(:title => 'two', :parent_id => node.id)}
secure(Page) { Page.create(:title => 'three', :parent_id => sub.id)}
yt_do_test('alias_site', 'link_with_custom_base')
end
yt_make
end