##// END OF EJS Templates
add missing fixtures...
Toshi MARUYAMA -
r13551:3ba5415d5622
parent child
Show More
@@ -1,551 +1,552
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2015 Jean-Philippe Lang
5 5 #
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; either version 2
9 9 # of the License, or (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 19
20 20 require File.expand_path('../../test_helper', __FILE__)
21 21
22 22 class ChangesetTest < ActiveSupport::TestCase
23 23 fixtures :projects, :repositories,
24 24 :issues, :issue_statuses, :issue_categories,
25 25 :changesets, :changes,
26 26 :enumerations,
27 27 :custom_fields, :custom_values,
28 28 :users, :members, :member_roles,
29 :email_addresses,
29 30 :trackers, :projects_trackers,
30 31 :enabled_modules, :roles
31 32
32 33 def test_ref_keywords_any
33 34 ActionMailer::Base.deliveries.clear
34 35 Setting.commit_ref_keywords = '*'
35 36 Setting.commit_update_keywords = [{'keywords' => 'fixes , closes', 'status_id' => '5', 'done_ratio' => '90'}]
36 37
37 38 c = Changeset.new(:repository => Project.find(1).repository,
38 39 :committed_on => Time.now,
39 40 :comments => 'New commit (#2). Fixes #1',
40 41 :revision => '12345')
41 42 assert c.save
42 43 assert_equal [1, 2], c.issue_ids.sort
43 44 fixed = Issue.find(1)
44 45 assert fixed.closed?
45 46 assert_equal 90, fixed.done_ratio
46 47 assert_equal 1, ActionMailer::Base.deliveries.size
47 48 end
48 49
49 50 def test_ref_keywords
50 51 Setting.commit_ref_keywords = 'refs'
51 52 Setting.commit_update_keywords = ''
52 53 c = Changeset.new(:repository => Project.find(1).repository,
53 54 :committed_on => Time.now,
54 55 :comments => 'Ignores #2. Refs #1',
55 56 :revision => '12345')
56 57 assert c.save
57 58 assert_equal [1], c.issue_ids.sort
58 59 end
59 60
60 61 def test_ref_keywords_any_only
61 62 Setting.commit_ref_keywords = '*'
62 63 Setting.commit_update_keywords = ''
63 64 c = Changeset.new(:repository => Project.find(1).repository,
64 65 :committed_on => Time.now,
65 66 :comments => 'Ignores #2. Refs #1',
66 67 :revision => '12345')
67 68 assert c.save
68 69 assert_equal [1, 2], c.issue_ids.sort
69 70 end
70 71
71 72 def test_ref_keywords_any_with_timelog
72 73 Setting.commit_ref_keywords = '*'
73 74 Setting.commit_logtime_enabled = '1'
74 75
75 76 {
76 77 '2' => 2.0,
77 78 '2h' => 2.0,
78 79 '2hours' => 2.0,
79 80 '15m' => 0.25,
80 81 '15min' => 0.25,
81 82 '3h15' => 3.25,
82 83 '3h15m' => 3.25,
83 84 '3h15min' => 3.25,
84 85 '3:15' => 3.25,
85 86 '3.25' => 3.25,
86 87 '3.25h' => 3.25,
87 88 '3,25' => 3.25,
88 89 '3,25h' => 3.25,
89 90 }.each do |syntax, expected_hours|
90 91 c = Changeset.new(:repository => Project.find(1).repository,
91 92 :committed_on => 24.hours.ago,
92 93 :comments => "Worked on this issue #1 @#{syntax}",
93 94 :revision => '520',
94 95 :user => User.find(2))
95 96 assert_difference 'TimeEntry.count' do
96 97 c.scan_comment_for_issue_ids
97 98 end
98 99 assert_equal [1], c.issue_ids.sort
99 100
100 101 time = TimeEntry.order('id desc').first
101 102 assert_equal 1, time.issue_id
102 103 assert_equal 1, time.project_id
103 104 assert_equal 2, time.user_id
104 105 assert_equal expected_hours, time.hours,
105 106 "@#{syntax} should be logged as #{expected_hours} hours but was #{time.hours}"
106 107 assert_equal Date.yesterday, time.spent_on
107 108 assert time.activity.is_default?
108 109 assert time.comments.include?('r520'),
109 110 "r520 was expected in time_entry comments: #{time.comments}"
110 111 end
111 112 end
112 113
113 114 def test_ref_keywords_closing_with_timelog
114 115 Setting.commit_ref_keywords = '*'
115 116 Setting.commit_update_keywords = [{'keywords' => 'fixes , closes',
116 117 'status_id' => IssueStatus.where(:is_closed => true).first.id.to_s}]
117 118 Setting.commit_logtime_enabled = '1'
118 119
119 120 c = Changeset.new(:repository => Project.find(1).repository,
120 121 :committed_on => Time.now,
121 122 :comments => 'This is a comment. Fixes #1 @4.5, #2 @1',
122 123 :user => User.find(2))
123 124 assert_difference 'TimeEntry.count', 2 do
124 125 c.scan_comment_for_issue_ids
125 126 end
126 127
127 128 assert_equal [1, 2], c.issue_ids.sort
128 129 assert Issue.find(1).closed?
129 130 assert Issue.find(2).closed?
130 131
131 132 times = TimeEntry.order('id desc').limit(2)
132 133 assert_equal [1, 2], times.collect(&:issue_id).sort
133 134 end
134 135
135 136 def test_ref_keywords_any_line_start
136 137 Setting.commit_ref_keywords = '*'
137 138 c = Changeset.new(:repository => Project.find(1).repository,
138 139 :committed_on => Time.now,
139 140 :comments => '#1 is the reason of this commit',
140 141 :revision => '12345')
141 142 assert c.save
142 143 assert_equal [1], c.issue_ids.sort
143 144 end
144 145
145 146 def test_ref_keywords_allow_brackets_around_a_issue_number
146 147 Setting.commit_ref_keywords = '*'
147 148 c = Changeset.new(:repository => Project.find(1).repository,
148 149 :committed_on => Time.now,
149 150 :comments => '[#1] Worked on this issue',
150 151 :revision => '12345')
151 152 assert c.save
152 153 assert_equal [1], c.issue_ids.sort
153 154 end
154 155
155 156 def test_ref_keywords_allow_brackets_around_multiple_issue_numbers
156 157 Setting.commit_ref_keywords = '*'
157 158 c = Changeset.new(:repository => Project.find(1).repository,
158 159 :committed_on => Time.now,
159 160 :comments => '[#1 #2, #3] Worked on these',
160 161 :revision => '12345')
161 162 assert c.save
162 163 assert_equal [1,2,3], c.issue_ids.sort
163 164 end
164 165
165 166 def test_update_keywords_with_multiple_rules
166 167 with_settings :commit_update_keywords => [
167 168 {'keywords' => 'fixes, closes', 'status_id' => '5'},
168 169 {'keywords' => 'resolves', 'status_id' => '3'}
169 170 ] do
170 171
171 172 issue1 = Issue.generate!
172 173 issue2 = Issue.generate!
173 174 Changeset.generate!(:comments => "Closes ##{issue1.id}\nResolves ##{issue2.id}")
174 175 assert_equal 5, issue1.reload.status_id
175 176 assert_equal 3, issue2.reload.status_id
176 177 end
177 178 end
178 179
179 180 def test_update_keywords_with_multiple_rules_should_match_tracker
180 181 with_settings :commit_update_keywords => [
181 182 {'keywords' => 'fixes', 'status_id' => '5', 'if_tracker_id' => '2'},
182 183 {'keywords' => 'fixes', 'status_id' => '3', 'if_tracker_id' => ''}
183 184 ] do
184 185
185 186 issue1 = Issue.generate!(:tracker_id => 2)
186 187 issue2 = Issue.generate!
187 188 Changeset.generate!(:comments => "Fixes ##{issue1.id}, ##{issue2.id}")
188 189 assert_equal 5, issue1.reload.status_id
189 190 assert_equal 3, issue2.reload.status_id
190 191 end
191 192 end
192 193
193 194 def test_update_keywords_with_multiple_rules_and_no_match
194 195 with_settings :commit_update_keywords => [
195 196 {'keywords' => 'fixes', 'status_id' => '5', 'if_tracker_id' => '2'},
196 197 {'keywords' => 'fixes', 'status_id' => '3', 'if_tracker_id' => '3'}
197 198 ] do
198 199
199 200 issue1 = Issue.generate!(:tracker_id => 2)
200 201 issue2 = Issue.generate!
201 202 Changeset.generate!(:comments => "Fixes ##{issue1.id}, ##{issue2.id}")
202 203 assert_equal 5, issue1.reload.status_id
203 204 assert_equal 1, issue2.reload.status_id # no updates
204 205 end
205 206 end
206 207
207 208 def test_commit_referencing_a_subproject_issue
208 209 c = Changeset.new(:repository => Project.find(1).repository,
209 210 :committed_on => Time.now,
210 211 :comments => 'refs #5, a subproject issue',
211 212 :revision => '12345')
212 213 assert c.save
213 214 assert_equal [5], c.issue_ids.sort
214 215 assert c.issues.first.project != c.project
215 216 end
216 217
217 218 def test_commit_closing_a_subproject_issue
218 219 with_settings :commit_update_keywords => [{'keywords' => 'closes', 'status_id' => '5'}],
219 220 :default_language => 'en' do
220 221 issue = Issue.find(5)
221 222 assert !issue.closed?
222 223 assert_difference 'Journal.count' do
223 224 c = Changeset.new(:repository => Project.find(1).repository,
224 225 :committed_on => Time.now,
225 226 :comments => 'closes #5, a subproject issue',
226 227 :revision => '12345')
227 228 assert c.save
228 229 end
229 230 assert issue.reload.closed?
230 231 journal = Journal.order('id DESC').first
231 232 assert_equal issue, journal.issue
232 233 assert_include "Applied in changeset ecookbook:r12345.", journal.notes
233 234 end
234 235 end
235 236
236 237 def test_commit_referencing_a_parent_project_issue
237 238 # repository of child project
238 239 r = Repository::Subversion.create!(
239 240 :project => Project.find(3),
240 241 :url => 'svn://localhost/test')
241 242 c = Changeset.new(:repository => r,
242 243 :committed_on => Time.now,
243 244 :comments => 'refs #2, an issue of a parent project',
244 245 :revision => '12345')
245 246 assert c.save
246 247 assert_equal [2], c.issue_ids.sort
247 248 assert c.issues.first.project != c.project
248 249 end
249 250
250 251 def test_commit_referencing_a_project_with_commit_cross_project_ref_disabled
251 252 r = Repository::Subversion.create!(
252 253 :project => Project.find(3),
253 254 :url => 'svn://localhost/test')
254 255 with_settings :commit_cross_project_ref => '0' do
255 256 c = Changeset.new(:repository => r,
256 257 :committed_on => Time.now,
257 258 :comments => 'refs #4, an issue of a different project',
258 259 :revision => '12345')
259 260 assert c.save
260 261 assert_equal [], c.issue_ids
261 262 end
262 263 end
263 264
264 265 def test_commit_referencing_a_project_with_commit_cross_project_ref_enabled
265 266 r = Repository::Subversion.create!(
266 267 :project => Project.find(3),
267 268 :url => 'svn://localhost/test')
268 269 with_settings :commit_cross_project_ref => '1' do
269 270 c = Changeset.new(:repository => r,
270 271 :committed_on => Time.now,
271 272 :comments => 'refs #4, an issue of a different project',
272 273 :revision => '12345')
273 274 assert c.save
274 275 assert_equal [4], c.issue_ids
275 276 end
276 277 end
277 278
278 279 def test_old_commits_should_not_update_issues_nor_log_time
279 280 Setting.commit_ref_keywords = '*'
280 281 Setting.commit_update_keywords = {'fixes , closes' => {'status_id' => '5', 'done_ratio' => '90'}}
281 282 Setting.commit_logtime_enabled = '1'
282 283
283 284 repository = Project.find(1).repository
284 285 repository.created_on = Time.now
285 286 repository.save!
286 287
287 288 c = Changeset.new(:repository => repository,
288 289 :committed_on => 1.month.ago,
289 290 :comments => 'New commit (#2). Fixes #1 @1h',
290 291 :revision => '12345')
291 292 assert_no_difference 'TimeEntry.count' do
292 293 assert c.save
293 294 end
294 295 assert_equal [1, 2], c.issue_ids.sort
295 296 issue = Issue.find(1)
296 297 assert_equal 1, issue.status_id
297 298 assert_equal 0, issue.done_ratio
298 299 end
299 300
300 301 def test_2_repositories_with_same_backend_should_not_link_issue_multiple_times
301 302 Setting.commit_ref_keywords = '*'
302 303 r1 = Repository::Subversion.create!(:project_id => 1, :identifier => 'svn1', :url => 'file:///svn1')
303 304 r2 = Repository::Subversion.create!(:project_id => 1, :identifier => 'svn2', :url => 'file:///svn1')
304 305 now = Time.now
305 306 assert_difference 'Issue.find(1).changesets.count' do
306 307 c1 = Changeset.create!(:repository => r1, :committed_on => now, :comments => 'Fixes #1', :revision => '12345')
307 308 c1 = Changeset.create!(:repository => r2, :committed_on => now, :comments => 'Fixes #1', :revision => '12345')
308 309 end
309 310 end
310 311
311 312 def test_text_tag_revision
312 313 c = Changeset.new(:revision => '520')
313 314 assert_equal 'r520', c.text_tag
314 315 end
315 316
316 317 def test_text_tag_revision_with_same_project
317 318 c = Changeset.new(:revision => '520', :repository => Project.find(1).repository)
318 319 assert_equal 'r520', c.text_tag(Project.find(1))
319 320 end
320 321
321 322 def test_text_tag_revision_with_different_project
322 323 c = Changeset.new(:revision => '520', :repository => Project.find(1).repository)
323 324 assert_equal 'ecookbook:r520', c.text_tag(Project.find(2))
324 325 end
325 326
326 327 def test_text_tag_revision_with_repository_identifier
327 328 r = Repository::Subversion.create!(
328 329 :project_id => 1,
329 330 :url => 'svn://localhost/test',
330 331 :identifier => 'documents')
331 332 c = Changeset.new(:revision => '520', :repository => r)
332 333 assert_equal 'documents|r520', c.text_tag
333 334 assert_equal 'ecookbook:documents|r520', c.text_tag(Project.find(2))
334 335 end
335 336
336 337 def test_text_tag_hash
337 338 c = Changeset.new(
338 339 :scmid => '7234cb2750b63f47bff735edc50a1c0a433c2518',
339 340 :revision => '7234cb2750b63f47bff735edc50a1c0a433c2518')
340 341 assert_equal 'commit:7234cb2750b63f47bff735edc50a1c0a433c2518', c.text_tag
341 342 end
342 343
343 344 def test_text_tag_hash_with_same_project
344 345 c = Changeset.new(:revision => '7234cb27', :scmid => '7234cb27', :repository => Project.find(1).repository)
345 346 assert_equal 'commit:7234cb27', c.text_tag(Project.find(1))
346 347 end
347 348
348 349 def test_text_tag_hash_with_different_project
349 350 c = Changeset.new(:revision => '7234cb27', :scmid => '7234cb27', :repository => Project.find(1).repository)
350 351 assert_equal 'ecookbook:commit:7234cb27', c.text_tag(Project.find(2))
351 352 end
352 353
353 354 def test_text_tag_hash_all_number
354 355 c = Changeset.new(:scmid => '0123456789', :revision => '0123456789')
355 356 assert_equal 'commit:0123456789', c.text_tag
356 357 end
357 358
358 359 def test_text_tag_hash_with_repository_identifier
359 360 r = Repository::Subversion.new(
360 361 :project_id => 1,
361 362 :url => 'svn://localhost/test',
362 363 :identifier => 'documents')
363 364 c = Changeset.new(:revision => '7234cb27', :scmid => '7234cb27', :repository => r)
364 365 assert_equal 'commit:documents|7234cb27', c.text_tag
365 366 assert_equal 'ecookbook:commit:documents|7234cb27', c.text_tag(Project.find(2))
366 367 end
367 368
368 369 def test_previous
369 370 changeset = Changeset.find_by_revision('3')
370 371 assert_equal Changeset.find_by_revision('2'), changeset.previous
371 372 end
372 373
373 374 def test_previous_nil
374 375 changeset = Changeset.find_by_revision('1')
375 376 assert_nil changeset.previous
376 377 end
377 378
378 379 def test_next
379 380 changeset = Changeset.find_by_revision('2')
380 381 assert_equal Changeset.find_by_revision('3'), changeset.next
381 382 end
382 383
383 384 def test_next_nil
384 385 changeset = Changeset.find_by_revision('10')
385 386 assert_nil changeset.next
386 387 end
387 388
388 389 def test_comments_should_be_converted_to_utf8
389 390 proj = Project.find(3)
390 391 # str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
391 392 str = "Texte encod\xe9 en ISO-8859-1.".force_encoding("ASCII-8BIT")
392 393 r = Repository::Bazaar.create!(
393 394 :project => proj,
394 395 :url => '/tmp/test/bazaar',
395 396 :log_encoding => 'ISO-8859-1' )
396 397 assert r
397 398 c = Changeset.new(:repository => r,
398 399 :committed_on => Time.now,
399 400 :revision => '123',
400 401 :scmid => '12345',
401 402 :comments => str)
402 403 assert( c.save )
403 404 str_utf8 = "Texte encod\xc3\xa9 en ISO-8859-1.".force_encoding("UTF-8")
404 405 assert_equal str_utf8, c.comments
405 406 end
406 407
407 408 def test_invalid_utf8_sequences_in_comments_should_be_replaced_latin1
408 409 proj = Project.find(3)
409 410 # str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
410 411 str1 = "Texte encod\xe9 en ISO-8859-1.".force_encoding("UTF-8")
411 412 str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test".force_encoding("ASCII-8BIT")
412 413 r = Repository::Bazaar.create!(
413 414 :project => proj,
414 415 :url => '/tmp/test/bazaar',
415 416 :log_encoding => 'UTF-8' )
416 417 assert r
417 418 c = Changeset.new(:repository => r,
418 419 :committed_on => Time.now,
419 420 :revision => '123',
420 421 :scmid => '12345',
421 422 :comments => str1,
422 423 :committer => str2)
423 424 assert( c.save )
424 425 assert_equal "Texte encod? en ISO-8859-1.", c.comments
425 426 assert_equal "?a?b?c?d?e test", c.committer
426 427 end
427 428
428 429 def test_invalid_utf8_sequences_in_comments_should_be_replaced_ja_jis
429 430 proj = Project.find(3)
430 431 str = "test\xb5\xfetest\xb5\xfe".force_encoding('ASCII-8BIT')
431 432 r = Repository::Bazaar.create!(
432 433 :project => proj,
433 434 :url => '/tmp/test/bazaar',
434 435 :log_encoding => 'ISO-2022-JP' )
435 436 assert r
436 437 c = Changeset.new(:repository => r,
437 438 :committed_on => Time.now,
438 439 :revision => '123',
439 440 :scmid => '12345',
440 441 :comments => str)
441 442 assert( c.save )
442 443 assert_equal "test??test??", c.comments
443 444 end
444 445
445 446 def test_comments_should_be_converted_all_latin1_to_utf8
446 447 s1 = "\xC2\x80"
447 448 s2 = "\xc3\x82\xc2\x80"
448 449 s4 = s2.dup
449 450 s3 = s1.dup
450 451 s1.force_encoding('ASCII-8BIT')
451 452 s2.force_encoding('ASCII-8BIT')
452 453 s3.force_encoding('ISO-8859-1')
453 454 s4.force_encoding('UTF-8')
454 455 assert_equal s3.encode('UTF-8'), s4
455 456 proj = Project.find(3)
456 457 r = Repository::Bazaar.create!(
457 458 :project => proj,
458 459 :url => '/tmp/test/bazaar',
459 460 :log_encoding => 'ISO-8859-1' )
460 461 assert r
461 462 c = Changeset.new(:repository => r,
462 463 :committed_on => Time.now,
463 464 :revision => '123',
464 465 :scmid => '12345',
465 466 :comments => s1)
466 467 assert( c.save )
467 468 assert_equal s4, c.comments
468 469 end
469 470
470 471 def test_invalid_utf8_sequences_in_paths_should_be_replaced
471 472 proj = Project.find(3)
472 473 str1 = "Texte encod\xe9 en ISO-8859-1".force_encoding("UTF-8")
473 474 str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test".force_encoding("ASCII-8BIT")
474 475 r = Repository::Bazaar.create!(
475 476 :project => proj,
476 477 :url => '/tmp/test/bazaar',
477 478 :log_encoding => 'UTF-8' )
478 479 assert r
479 480 cs = Changeset.new(
480 481 :repository => r,
481 482 :committed_on => Time.now,
482 483 :revision => '123',
483 484 :scmid => '12345',
484 485 :comments => "test")
485 486 assert(cs.save)
486 487 ch = Change.new(
487 488 :changeset => cs,
488 489 :action => "A",
489 490 :path => str1,
490 491 :from_path => str2,
491 492 :from_revision => "345")
492 493 assert(ch.save)
493 494 assert_equal "Texte encod? en ISO-8859-1", ch.path
494 495 assert_equal "?a?b?c?d?e test", ch.from_path
495 496 end
496 497
497 498 def test_comments_nil
498 499 proj = Project.find(3)
499 500 r = Repository::Bazaar.create!(
500 501 :project => proj,
501 502 :url => '/tmp/test/bazaar',
502 503 :log_encoding => 'ISO-8859-1' )
503 504 assert r
504 505 c = Changeset.new(:repository => r,
505 506 :committed_on => Time.now,
506 507 :revision => '123',
507 508 :scmid => '12345',
508 509 :comments => nil,
509 510 :committer => nil)
510 511 assert( c.save )
511 512 assert_equal "", c.comments
512 513 assert_equal nil, c.committer
513 514 assert_equal "UTF-8", c.comments.encoding.to_s
514 515 end
515 516
516 517 def test_comments_empty
517 518 proj = Project.find(3)
518 519 r = Repository::Bazaar.create!(
519 520 :project => proj,
520 521 :url => '/tmp/test/bazaar',
521 522 :log_encoding => 'ISO-8859-1' )
522 523 assert r
523 524 c = Changeset.new(:repository => r,
524 525 :committed_on => Time.now,
525 526 :revision => '123',
526 527 :scmid => '12345',
527 528 :comments => "",
528 529 :committer => "")
529 530 assert( c.save )
530 531 assert_equal "", c.comments
531 532 assert_equal "", c.committer
532 533 assert_equal "UTF-8", c.comments.encoding.to_s
533 534 assert_equal "UTF-8", c.committer.encoding.to_s
534 535 end
535 536
536 537 def test_comments_should_accept_more_than_64k
537 538 c = Changeset.new(:repository => Repository.first,
538 539 :committed_on => Time.now,
539 540 :revision => '123',
540 541 :scmid => '12345',
541 542 :comments => "a" * 500.kilobyte)
542 543 assert c.save
543 544 c.reload
544 545 assert_equal 500.kilobyte, c.comments.size
545 546 end
546 547
547 548 def test_identifier
548 549 c = Changeset.find_by_revision('1')
549 550 assert_equal c.revision, c.identifier
550 551 end
551 552 end
@@ -1,165 +1,166
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class WikiContentTest < ActiveSupport::TestCase
21 21 fixtures :projects, :enabled_modules,
22 22 :users, :members, :member_roles, :roles,
23 :email_addresses,
23 24 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions
24 25
25 26 def setup
26 27 @wiki = Wiki.find(1)
27 28 @page = @wiki.pages.first
28 29 end
29 30
30 31 def test_create
31 32 page = WikiPage.new(:wiki => @wiki, :title => "Page")
32 33 page.content = WikiContent.new(:text => "Content text", :author => User.find(1), :comments => "My comment")
33 34 assert page.save
34 35 page.reload
35 36
36 37 content = page.content
37 38 assert_kind_of WikiContent, content
38 39 assert_equal 1, content.version
39 40 assert_equal 1, content.versions.length
40 41 assert_equal "Content text", content.text
41 42 assert_equal "My comment", content.comments
42 43 assert_equal User.find(1), content.author
43 44 assert_equal content.text, content.versions.last.text
44 45 end
45 46
46 47 def test_create_should_send_email_notification
47 48 ActionMailer::Base.deliveries.clear
48 49 page = WikiPage.new(:wiki => @wiki, :title => "A new page")
49 50 page.content = WikiContent.new(:text => "Content text", :author => User.find(1), :comments => "My comment")
50 51
51 52 with_settings :default_language => 'en', :notified_events => %w(wiki_content_added) do
52 53 assert page.save
53 54 end
54 55
55 56 assert_equal 1, ActionMailer::Base.deliveries.size
56 57 assert_include 'wiki page has been added', mail_body(ActionMailer::Base.deliveries.last)
57 58 end
58 59
59 60 def test_update_should_be_versioned
60 61 content = @page.content
61 62 version_count = content.version
62 63 content.text = "My new content"
63 64 assert_difference 'WikiContent::Version.count' do
64 65 assert content.save
65 66 end
66 67 content.reload
67 68 assert_equal version_count+1, content.version
68 69 assert_equal version_count+1, content.versions.length
69 70
70 71 version = WikiContent::Version.order('id DESC').first
71 72 assert_equal @page.id, version.page_id
72 73 assert_equal '', version.compression
73 74 assert_equal "My new content", version.data
74 75 assert_equal "My new content", version.text
75 76 end
76 77
77 78 def test_update_with_gzipped_history
78 79 with_settings :wiki_compression => 'gzip' do
79 80 content = @page.content
80 81 content.text = "My new content"
81 82 assert_difference 'WikiContent::Version.count' do
82 83 assert content.save
83 84 end
84 85 end
85 86
86 87 version = WikiContent::Version.order('id DESC').first
87 88 assert_equal @page.id, version.page_id
88 89 assert_equal 'gzip', version.compression
89 90 assert_not_equal "My new content", version.data
90 91 assert_equal "My new content", version.text
91 92 end
92 93
93 94 def test_update_should_send_email_notification
94 95 ActionMailer::Base.deliveries.clear
95 96 content = @page.content
96 97 content.text = "My new content"
97 98
98 99 with_settings :notified_events => %w(wiki_content_updated), :default_language => 'en' do
99 100 assert content.save
100 101 end
101 102
102 103 assert_equal 1, ActionMailer::Base.deliveries.size
103 104 assert_include 'wiki page has been updated', mail_body(ActionMailer::Base.deliveries.last)
104 105 end
105 106
106 107 def test_fetch_history
107 108 assert !@page.content.versions.empty?
108 109 @page.content.versions.each do |version|
109 110 assert_kind_of String, version.text
110 111 end
111 112 end
112 113
113 114 def test_large_text_should_not_be_truncated_to_64k
114 115 page = WikiPage.new(:wiki => @wiki, :title => "Big page")
115 116 page.content = WikiContent.new(:text => "a" * 500.kilobyte, :author => User.find(1))
116 117 assert page.save
117 118 page.reload
118 119 assert_equal 500.kilobyte, page.content.text.size
119 120 end
120 121
121 122 def test_current_version
122 123 content = WikiContent.find(11)
123 124 assert_equal true, content.current_version?
124 125 assert_equal true, content.versions.order('version DESC').first.current_version?
125 126 assert_equal false, content.versions.order('version ASC').first.current_version?
126 127 end
127 128
128 129 def test_previous_for_first_version_should_return_nil
129 130 content = WikiContent::Version.find_by_page_id_and_version(1, 1)
130 131 assert_nil content.previous
131 132 end
132 133
133 134 def test_previous_for_version_should_return_previous_version
134 135 content = WikiContent::Version.find_by_page_id_and_version(1, 3)
135 136 assert_not_nil content.previous
136 137 assert_equal 2, content.previous.version
137 138 end
138 139
139 140 def test_previous_for_version_with_gap_should_return_previous_available_version
140 141 WikiContent::Version.find_by_page_id_and_version(1, 2).destroy
141 142
142 143 content = WikiContent::Version.find_by_page_id_and_version(1, 3)
143 144 assert_not_nil content.previous
144 145 assert_equal 1, content.previous.version
145 146 end
146 147
147 148 def test_next_for_last_version_should_return_nil
148 149 content = WikiContent::Version.find_by_page_id_and_version(1, 3)
149 150 assert_nil content.next
150 151 end
151 152
152 153 def test_next_for_version_should_return_next_version
153 154 content = WikiContent::Version.find_by_page_id_and_version(1, 1)
154 155 assert_not_nil content.next
155 156 assert_equal 2, content.next.version
156 157 end
157 158
158 159 def test_next_for_version_with_gap_should_return_next_available_version
159 160 WikiContent::Version.find_by_page_id_and_version(1, 2).destroy
160 161
161 162 content = WikiContent::Version.find_by_page_id_and_version(1, 1)
162 163 assert_not_nil content.next
163 164 assert_equal 3, content.next.version
164 165 end
165 166 end
General Comments 0
You need to be logged in to leave comments. Login now