Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 10
- Log:
Checking in HEAD from RForum's SVN of 22-Jul-2006, 8pm (revision 906).
- Author:
- adh
- Date:
- Sat Jul 22 20:02:44 +0100 2006
- Size:
- 11180 Bytes
- Properties:
- Property svn:executable is set
1 | #!/bin/env ruby |
2 | |
3 | require File.dirname(__FILE__) + '/../test_helper' |
4 | |
5 | class PostTest < Test::Unit::TestCase |
6 | fixtures 'users', 'forums', 'topics', 'posts', 'attachments' |
7 | |
8 | |
9 | def teardown |
10 | Dir[RAILS_ROOT + RForum::CONFIG[:attachment_path] + '/*'].each do |d| |
11 | Dir[d + '/*'].each do |f| |
12 | File.delete f |
13 | end |
14 | Dir.delete d |
15 | end |
16 | end |
17 | |
18 | def test_create |
19 | p = time { Post.create(prototype_params(Post)) } |
20 | |
21 | post_from_db = Post.find(p.id) |
22 | assert post_from_db |
23 | |
24 | assert_has_attributes post_from_db, |
25 | %w( id topic_id l r parent_id user_id guest_name guest_email subject nntpid |
26 | messageid created_at text) |
27 | |
28 | assert_equal_sysdate post_from_db.created_at |
29 | assert !(post_from_db.messageid.empty?) |
30 | assert !(post_from_db.nntpid.nil?) |
31 | |
32 | assert_equal_sysdate post_from_db.topic.last_post_created_at |
33 | assert_match /^.*#{RForum::CONFIG[:hostname]}/, p.messageid |
34 | assert_equal 0, p.deleted |
35 | end |
36 | |
37 | def test_create_spam_from_web |
38 | assert_raise(RForum::ValidationError, "Spam is accepted") { |
39 | Post.create(prototype_params(Post, 'text' => 'xanax')) |
40 | } |
41 | end |
42 | |
43 | def test_create_spam_from_mail |
44 | # incoming mails are not affected by the spam filter |
45 | assert Post.create(prototype_params(Post, 'post_method'=> 'mail', 'text' => 'xanax')) |
46 | end |
47 | |
48 | def test_create_with_empty_messageid |
49 | |
50 | p = Post.create(prototype_params(Post, 'messageid' => nil)) |
51 | |
52 | assert_match /^.*#{RForum::CONFIG[:hostname]}/, p.messageid |
53 | |
54 | # Should also work on an empty database (with no posts yet) |
55 | db.execute 'DELETE FROM posts' |
56 | |
57 | p = Post.create(prototype_params(Post, 'messageid' => nil)) |
58 | |
59 | assert_match /^.*#{RForum::CONFIG[:hostname]}/, p.messageid |
60 | end |
61 | |
62 | def test_create_populates_created_at |
63 | time = Time.at(Time.now - 600) |
64 | post = prototype(Post, 'created_at' => time) |
65 | post.save |
66 | assert_equal time, post.created_at |
67 | end |
68 | |
69 | def test_validate_mandatory_attributes |
70 | assert_mandatory_attributes_enforced(%w(topic_id), Post) |
71 | end |
72 | |
73 | def test_validate_empty_text |
74 | [nil, '', '1', "1 "].each { |empty_text_value| |
75 | assert_raise(RForum::ValidationError, "Text #{empty_text_value.inspect} is accepted") { |
76 | Post.create(prototype_params(Post, 'text' => empty_text_value)) |
77 | } |
78 | } |
79 | end |
80 | |
81 | def test_validate_empty_subject |
82 | [nil, '', '1'].each { |empty_subject_value| |
83 | assert_raise(RForum::ValidationError, "Subject #{empty_subject_value.inspect} is accepted") { |
84 | Post.create(prototype_params(Post, 'subject' => empty_subject_value)) |
85 | } |
86 | } |
87 | assert_nothing_raised { prototype_params(Post, 'subject' => '123') } |
88 | end |
89 | |
90 | def test_validate_parent_is_from_wrong_topic |
91 | new_post = prototype_params(Post, 'parent_id' => 102, 'topic_id' => 1) |
92 | assert_raise(ArgumentError) { Post.create(new_post) } |
93 | end |
94 | |
95 | def test_get_author_user |
96 | p = prototype(Post, 'user_id' => 1) |
97 | assert_equal 1, p.author.id |
98 | assert p.author.is_a?(User) |
99 | end |
100 | |
101 | def test_get_author_guest |
102 | p = prototype(Post, 'guest_name' => 'Alex', 'guest_email' => 'a@b.c') |
103 | assert p.author.is_a?(Guest) |
104 | assert_equal 'Alex', p.author.guest_name |
105 | assert_equal 'a@b.c', p.author.guest_email |
106 | end |
107 | |
108 | def test_set_author_user |
109 | u = User.find 1 |
110 | p = prototype(Post, 'author' => u) |
111 | assert_equal 1, p.user_id |
112 | end |
113 | |
114 | def test_set_author_guest |
115 | u = Guest.new('Alex', 'a@b.c') |
116 | p = prototype(Post, 'author' => u) |
117 | assert_equal 'Alex', p.guest_name |
118 | assert_equal 'a@b.c', p.guest_email |
119 | assert_nil p.user_id |
120 | end |
121 | |
122 | def test_save_discards_guest_attributes_if_user_given |
123 | p = prototype(Post, 'user_id' => 1, 'guest_name' => 'Alex', 'guest_email' => 'a@b.c') |
124 | |
125 | p.save |
126 | |
127 | assert_nil p.guest_name |
128 | assert_nil p.guest_email |
129 | end |
130 | |
131 | def test_guest_aggregation |
132 | p = prototype(Post, 'user_id' => nil, 'guest_name' => 'Alex', 'guest_email' => 'a@b.c') |
133 | assert p.guest.is_a?(Guest) |
134 | assert_equal 'Alex', p.guest_name |
135 | assert_equal 'a@b.c', p.guest_email |
136 | assert_nil p.user |
137 | end |
138 | |
139 | def test_create_notifies_subscribers |
140 | # TODO implement me |
141 | end |
142 | |
143 | def test_find_with_all_parents |
144 | posts = Post.find_with_all_parents(2) |
145 | assert_equal 1, posts[0].id |
146 | assert_equal 2, posts[1].id |
147 | |
148 | posts = Post.find_with_all_parents(10) |
149 | assert_equal 1, posts[0].id |
150 | assert_equal 8, posts[1].id |
151 | assert_equal 9, posts[2].id |
152 | assert_equal 10, posts[3].id |
153 | end |
154 | |
155 | def test_find_with_user_data |
156 | p = Post.find_with_user_data(2) |
157 | |
158 | assert_equal 'sergej', p.user_name |
159 | assert_equal 'Sergej', p.user_firstname |
160 | assert_equal 'Fährlich', p.user_surname |
161 | end |
162 | |
163 | def test_find_with_user_data_nonexistant_post_id |
164 | assert_raises(ActiveRecord::RecordNotFound) { Post.find_with_user_data(654231) } |
165 | end |
166 | |
167 | def test_find_latest |
168 | p = Post.create(prototype_params(Post)) |
169 | assert_equal [p], Post.find_latest(1) |
170 | end |
171 | |
172 | def test_add_reply |
173 | p = Post.find(2) |
174 | replies_before = p.topic.posts.count |
175 | assert_equal replies_before, p.topic.post_counter |
176 | reply = prototype(Post) |
177 | |
178 | p.add_reply(reply) |
179 | |
180 | assert_equal p.topic_id, reply.topic_id |
181 | assert !reply.root? |
182 | assert_equal p.id, reply.parent_id |
183 | assert_equal replies_before + 1, p.topic.posts(:reload).size |
184 | assert_equal replies_before + 1, p.topic(:reload).post_counter |
185 | assert_nested_set_well_formed(p.topic_id) |
186 | end |
187 | |
188 | def test_destroy |
189 | # destroy a subtree |
190 | Post.find(2).destroy |
191 | |
192 | assert_nested_set_well_formed(1) |
193 | remaining_ids = db.select_all('SELECT id FROM posts WHERE topic_id = 1').map { |row| row['id'] } |
194 | assert_equal %w(1 10 11 5 6 7 8 9), remaining_ids.sort |
195 | |
196 | # destroy a leaf |
197 | Post.find(6).destroy |
198 | |
199 | assert_nested_set_well_formed(1) |
200 | remaining_ids = db.select_all('SELECT id FROM posts WHERE topic_id = 1').map { |row| row['id'] } |
201 | assert_equal %w(1 10 11 5 7 8 9), remaining_ids.sort |
202 | |
203 | # destroy a root message |
204 | Post.find(1).destroy |
205 | |
206 | remaining_ids = db.select_all('SELECT id FROM posts WHERE topic_id = 1').map { |row| row['id'] } |
207 | assert remaining_ids.empty? |
208 | # destroying a root must also delete the topic |
209 | assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1) } |
210 | end |
211 | |
212 | def test_get_display_name |
213 | # Post identified by user_name attribute |
214 | p = Post.find_with_user_data(2) |
215 | assert_equal 'Sergej Fährlich', p.get_display_name |
216 | |
217 | # Post identified by querying user |
218 | p.user_name = nil |
219 | assert_equal 'Sergej Fährlich', p.get_display_name |
220 | |
221 | # Post identified by guest_user |
222 | p = Post.find(6) |
223 | assert_equal 'A guest', p.get_display_name |
224 | |
225 | # Unidentified post |
226 | p = Post.find(7) |
227 | assert_equal '(unknown)', p.get_display_name |
228 | end |
229 | |
230 | def test_get_references |
231 | assert_equal '<root@xy.com>', Post.find(2).get_references |
232 | end |
233 | |
234 | def test_get_references_root_post |
235 | assert_equal '', Post.find(1).get_references |
236 | end |
237 | |
238 | def test_get_references_multiple_parents |
239 | assert_equal '<root@xy.com> <a@xy.com>', Post.find(3).get_references |
240 | end |
241 | |
242 | def test_root? |
243 | p = prototype(Post) |
244 | assert p.root? |
245 | p.parent_id = 1 |
246 | assert !p.root? |
247 | end |
248 | |
249 | def test_find_in_nntp_range |
250 | forum_id = 1 |
251 | first = 1 |
252 | last = 4 |
253 | |
254 | posts = Post.find_in_nntp_range(forum_id, first, last) |
255 | |
256 | assert_equal 4, posts.size |
257 | assert_equal 1, posts[0].nntpid |
258 | assert_equal 2, posts[1].nntpid |
259 | assert_equal 3, posts[2].nntpid |
260 | assert_equal 4, posts[3].nntpid |
261 | assert_equal nil, posts[0]['references'] |
262 | assert_equal '<root@xy.com>', posts[1]['references'] |
263 | assert_equal '<root@xy.com> <a@xy.com>', posts[2]['references'] |
264 | assert_equal '<root@xy.com> <a@xy.com> <a1@xy.com>', posts[3]['references'] |
265 | end |
266 | |
267 | def test_reply_subject |
268 | assert_equal 'Re: blah blah', prototype(Post, 'subject' => 'blah blah').reply_subject |
269 | assert_equal 'Re: blah blah', prototype(Post, 'subject' => 'Re: blah blah').reply_subject |
270 | assert_equal 're: blah blah', prototype(Post, 'subject' => 're: blah blah').reply_subject |
271 | assert_equal 'Re:blah blah', prototype(Post, 'subject' => 'Re:blah blah').reply_subject |
272 | end |
273 | |
274 | def test_hide |
275 | p = Post.find(102) |
276 | assert !p.hidden? |
277 | |
278 | p.hide |
279 | |
280 | assert p.hidden? |
281 | p_from_db = Post.find(102) |
282 | assert p_from_db.hidden? |
283 | end |
284 | |
285 | def test_hide_recursive_with_one_child |
286 | p = Post.find(101) |
287 | p.hide(:recursive) |
288 | |
289 | assert p.hidden? |
290 | assert Post.find(102).hidden? |
291 | end |
292 | |
293 | def test_hide_with_one_child |
294 | p = Post.find(101) |
295 | p.hide |
296 | |
297 | assert p.hidden? |
298 | assert !Post.find(102).hidden? |
299 | end |
300 | |
301 | def test_hide_big_topic |
302 | p = Post.find(1) |
303 | p.hide(:recursive) |
304 | |
305 | assert_equal '0', db.select_one( |
306 | "SELECT COUNT(*) AS c FROM posts WHERE topic_id = 1 AND deleted = 0")['c'] |
307 | end |
308 | |
309 | def test_unhide |
310 | p = Post.find(102) |
311 | p.hide |
312 | p.unhide |
313 | |
314 | assert !p.hidden? |
315 | p_from_db = Post.find(102) |
316 | assert !p_from_db.hidden? |
317 | end |
318 | |
319 | def test_unhide_with_one_child |
320 | p = Post.find(101) |
321 | p.hide(:recursive) |
322 | p.unhide |
323 | |
324 | assert !p.hidden? |
325 | assert Post.find(102).hidden? |
326 | end |
327 | |
328 | def test_unhide_recursive_with_one_child |
329 | p = Post.find(101) |
330 | p.hide(:recursive) |
331 | p.unhide(:recursive) |
332 | |
333 | assert !p.hidden? |
334 | assert !Post.find(102).hidden? |
335 | end |
336 | |
337 | def test_has_undeleted_children? |
338 | # post with only hidden child |
339 | assert !(Post.find(21).has_undeleted_children?) |
340 | # post with one active and one hidden child |
341 | assert Post.find(20).has_undeleted_children? |
342 | # root post of a big topic |
343 | assert Post.find(1).has_undeleted_children? |
344 | # post with no children at all |
345 | assert !(Post.find(103).has_undeleted_children?) |
346 | end |
347 | |
348 | def test_attach_file |
349 | p = Post.create(prototype_params(Post)) |
350 | a = p.attach_file('test.txt', 'testdata') |
351 | assert_equal 'testdata', File.read(a.file_path) |
352 | assert_equal 'testdata', a.get_file_content |
353 | end |
354 | |
355 | def test_attach_file_directly |
356 | RForum::CONFIG[:max_attachment_size] = 100 |
357 | p = Post.new(prototype_params(Post)) |
358 | upload = Struct.new(:read, :original_filename, :size).new |
359 | upload.read = 'testdata' |
360 | upload.size = 8 |
361 | upload.original_filename = 'test.txt' |
362 | p.new_attachment = upload |
363 | p.save |
364 | p.reload |
365 | assert_equal 'testdata', p.attachments[0].get_file_content |
366 | assert_equal 'test.txt', p.attachments[0].filename |
367 | end |
368 | |
369 | def test_attach_large_file_directly |
370 | RForum::CONFIG[:max_attachment_size] = 5 |
371 | p = Post.new(prototype_params(Post)) |
372 | upload = Struct.new(:read, :original_filename, :size).new |
373 | upload.read = 'testdata' |
374 | upload.size = 8 |
375 | upload.original_filename = 'test.txt' |
376 | p.new_attachment = upload |
377 | assert_raise(RForum::ValidationError) { p.save } |
378 | end |
379 | |
380 | def test_author_is_guest_without_name |
381 | p = Post.create( :guest_name => '', :subject => 'subject foo', :topic_id => '1', :text => 'content' ) |
382 | assert p.author.is_a?(Guest) |
383 | assert_equal 'Guest', p.author.guest_name |
384 | end |
385 | |
386 | def test_deliver_mail |
387 | RForum::CONFIG[:deliver_mail] = true |
388 | post = Post.new(:guest_name => 'foobar', :subject => 'foobar', :text => 'content') |
389 | Topic.find(3).add_reply(post) |
390 | assert_equal 1, ActionMailer::Base.deliveries.size |
391 | end |
392 | end |