##// END OF EJS Templates
Additional tests for WikiController....
Jean-Philippe Lang -
r8142:849463558da4
parent child
Show More
@@ -1,739 +1,769
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 require 'wiki_controller'
20 20
21 21 # Re-raise errors caught by the controller.
22 22 class WikiController; def rescue_action(e) raise e end; end
23 23
24 24 class WikiControllerTest < ActionController::TestCase
25 25 fixtures :projects, :users, :roles, :members, :member_roles,
26 26 :enabled_modules, :wikis, :wiki_pages, :wiki_contents,
27 27 :wiki_content_versions, :attachments
28 28
29 29 def setup
30 30 @controller = WikiController.new
31 31 @request = ActionController::TestRequest.new
32 32 @response = ActionController::TestResponse.new
33 33 User.current = nil
34 34 end
35 35
36 36 def test_show_start_page
37 37 get :show, :project_id => 'ecookbook'
38 38 assert_response :success
39 39 assert_template 'show'
40 40 assert_tag :tag => 'h1', :content => /CookBook documentation/
41 41
42 42 # child_pages macro
43 43 assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
44 44 :child => { :tag => 'li',
45 45 :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
46 46 :content => 'Page with an inline image' } }
47 47 end
48 48
49 49 def test_export_link
50 50 Role.anonymous.add_permission! :export_wiki_pages
51 51 get :show, :project_id => 'ecookbook'
52 52 assert_response :success
53 53 assert_tag 'a', :attributes => {:href => '/projects/ecookbook/wiki/CookBook_documentation.txt'}
54 54 end
55 55
56 56 def test_show_page_with_name
57 57 get :show, :project_id => 1, :id => 'Another_page'
58 58 assert_response :success
59 59 assert_template 'show'
60 60 assert_tag :tag => 'h1', :content => /Another page/
61 61 # Included page with an inline image
62 62 assert_tag :tag => 'p', :content => /This is an inline image/
63 63 assert_tag :tag => 'img', :attributes => { :src => '/attachments/download/3',
64 64 :alt => 'This is a logo' }
65 65 end
66 66
67 67 def test_show_redirected_page
68 68 WikiRedirect.create!(:wiki_id => 1, :title => 'Old_title', :redirects_to => 'Another_page')
69 69
70 70 get :show, :project_id => 'ecookbook', :id => 'Old_title'
71 71 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
72 72 end
73 73
74 74 def test_show_with_sidebar
75 75 page = Project.find(1).wiki.pages.new(:title => 'Sidebar')
76 76 page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar')
77 77 page.save!
78 78
79 79 get :show, :project_id => 1, :id => 'Another_page'
80 80 assert_response :success
81 81 assert_tag :tag => 'div', :attributes => {:id => 'sidebar'},
82 82 :content => /Side bar content for test_show_with_sidebar/
83 83 end
84 84
85 85 def test_show_should_display_section_edit_links
86 86 @request.session[:user_id] = 2
87 87 get :show, :project_id => 1, :id => 'Page with sections'
88 88 assert_no_tag 'a', :attributes => {
89 89 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=1'
90 90 }
91 91 assert_tag 'a', :attributes => {
92 92 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
93 93 }
94 94 assert_tag 'a', :attributes => {
95 95 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=3'
96 96 }
97 97 end
98 98
99 99 def test_show_current_version_should_display_section_edit_links
100 100 @request.session[:user_id] = 2
101 101 get :show, :project_id => 1, :id => 'Page with sections', :version => 3
102 102
103 103 assert_tag 'a', :attributes => {
104 104 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
105 105 }
106 106 end
107 107
108 108 def test_show_old_version_should_not_display_section_edit_links
109 109 @request.session[:user_id] = 2
110 110 get :show, :project_id => 1, :id => 'Page with sections', :version => 2
111 111
112 112 assert_no_tag 'a', :attributes => {
113 113 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
114 114 }
115 115 end
116 116
117 117 def test_show_unexistent_page_without_edit_right
118 118 get :show, :project_id => 1, :id => 'Unexistent page'
119 119 assert_response 404
120 120 end
121 121
122 122 def test_show_unexistent_page_with_edit_right
123 123 @request.session[:user_id] = 2
124 124 get :show, :project_id => 1, :id => 'Unexistent page'
125 125 assert_response :success
126 126 assert_template 'edit'
127 127 assert_no_tag 'input', :attributes => {:name => 'page[parent_id]'}
128 128 end
129 129
130 130 def test_show_unexistent_page_with_parent
131 131 @request.session[:user_id] = 2
132 132 get :show, :project_id => 1, :id => 'Unexistent page', :parent => 'Another_page'
133 133 assert_response :success
134 134 assert_template 'edit'
135 135 assert_tag 'input', :attributes => {:name => 'page[parent_id]', :value => '2'}
136 136 end
137 137
138 138 def test_show_should_not_show_history_without_permission
139 139 Role.anonymous.remove_permission! :view_wiki_edits
140 140 get :show, :project_id => 1, :id => 'Page with sections', :version => 2
141 141
142 142 assert_response 302
143 143 end
144 144
145 145 def test_create_page
146 146 @request.session[:user_id] = 2
147 147 assert_difference 'WikiPage.count' do
148 148 assert_difference 'WikiContent.count' do
149 149 put :update, :project_id => 1,
150 150 :id => 'New page',
151 151 :content => {:comments => 'Created the page',
152 152 :text => "h1. New page\n\nThis is a new page",
153 153 :version => 0}
154 154 end
155 155 end
156 156 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'New_page'
157 157 page = Project.find(1).wiki.find_page('New page')
158 158 assert !page.new_record?
159 159 assert_not_nil page.content
160 160 assert_nil page.parent
161 161 assert_equal 'Created the page', page.content.comments
162 162 end
163 163
164 164 def test_create_page_with_attachments
165 165 @request.session[:user_id] = 2
166 166 assert_difference 'WikiPage.count' do
167 167 assert_difference 'Attachment.count' do
168 168 put :update, :project_id => 1,
169 169 :id => 'New page',
170 170 :content => {:comments => 'Created the page',
171 171 :text => "h1. New page\n\nThis is a new page",
172 172 :version => 0},
173 173 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
174 174 end
175 175 end
176 176 page = Project.find(1).wiki.find_page('New page')
177 177 assert_equal 1, page.attachments.count
178 178 assert_equal 'testfile.txt', page.attachments.first.filename
179 179 end
180 180
181 181 def test_create_page_with_parent
182 182 @request.session[:user_id] = 2
183 183 assert_difference 'WikiPage.count' do
184 184 put :update, :project_id => 1, :id => 'New page',
185 185 :content => {:text => "h1. New page\n\nThis is a new page", :version => 0},
186 186 :page => {:parent_id => 2}
187 187 end
188 188 page = Project.find(1).wiki.find_page('New page')
189 189 assert_equal WikiPage.find(2), page.parent
190 190 end
191 191
192 192 def test_edit_page
193 193 @request.session[:user_id] = 2
194 194 get :edit, :project_id => 'ecookbook', :id => 'Another_page'
195 195
196 196 assert_response :success
197 197 assert_template 'edit'
198 198
199 199 assert_tag 'textarea',
200 200 :attributes => { :name => 'content[text]' },
201 201 :content => WikiPage.find_by_title('Another_page').content.text
202 202 end
203 203
204 204 def test_edit_section
205 205 @request.session[:user_id] = 2
206 206 get :edit, :project_id => 'ecookbook', :id => 'Page_with_sections', :section => 2
207 207
208 208 assert_response :success
209 209 assert_template 'edit'
210 210
211 211 page = WikiPage.find_by_title('Page_with_sections')
212 212 section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
213 213
214 214 assert_tag 'textarea',
215 215 :attributes => { :name => 'content[text]' },
216 216 :content => section
217 217 assert_tag 'input',
218 218 :attributes => { :name => 'section', :type => 'hidden', :value => '2' }
219 219 assert_tag 'input',
220 220 :attributes => { :name => 'section_hash', :type => 'hidden', :value => hash }
221 221 end
222 222
223 223 def test_edit_invalid_section_should_respond_with_404
224 224 @request.session[:user_id] = 2
225 225 get :edit, :project_id => 'ecookbook', :id => 'Page_with_sections', :section => 10
226 226
227 227 assert_response 404
228 228 end
229 229
230 230 def test_update_page
231 231 @request.session[:user_id] = 2
232 232 assert_no_difference 'WikiPage.count' do
233 233 assert_no_difference 'WikiContent.count' do
234 234 assert_difference 'WikiContent::Version.count' do
235 235 put :update, :project_id => 1,
236 236 :id => 'Another_page',
237 237 :content => {
238 238 :comments => "my comments",
239 239 :text => "edited",
240 240 :version => 1
241 241 }
242 242 end
243 243 end
244 244 end
245 245 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
246 246
247 247 page = Wiki.find(1).pages.find_by_title('Another_page')
248 248 assert_equal "edited", page.content.text
249 249 assert_equal 2, page.content.version
250 250 assert_equal "my comments", page.content.comments
251 251 end
252 252
253 253 def test_update_page_with_failure
254 254 @request.session[:user_id] = 2
255 255 assert_no_difference 'WikiPage.count' do
256 256 assert_no_difference 'WikiContent.count' do
257 257 assert_no_difference 'WikiContent::Version.count' do
258 258 put :update, :project_id => 1,
259 259 :id => 'Another_page',
260 260 :content => {
261 261 :comments => 'a' * 300, # failure here, comment is too long
262 262 :text => 'edited',
263 263 :version => 1
264 264 }
265 265 end
266 266 end
267 267 end
268 268 assert_response :success
269 269 assert_template 'edit'
270 270
271 271 assert_error_tag :descendant => {:content => /Comment is too long/}
272 272 assert_tag :tag => 'textarea', :attributes => {:id => 'content_text'}, :content => 'edited'
273 273 assert_tag :tag => 'input', :attributes => {:id => 'content_version', :value => '1'}
274 274 end
275 275
276 def test_update_page_with_attachments_only_should_not_create_content_version
277 @request.session[:user_id] = 2
278 assert_no_difference 'WikiPage.count' do
279 assert_no_difference 'WikiContent.count' do
280 assert_no_difference 'WikiContent::Version.count' do
281 assert_difference 'Attachment.count' do
282 put :update, :project_id => 1,
283 :id => 'Another_page',
284 :content => {
285 :comments => '',
286 :text => Wiki.find(1).find_page('Another_page').content.text,
287 :version => 1
288 },
289 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
290 end
291 end
292 end
293 end
294 end
295
276 296 def test_update_stale_page_should_not_raise_an_error
277 297 @request.session[:user_id] = 2
278 298 c = Wiki.find(1).find_page('Another_page').content
279 299 c.text = 'Previous text'
280 300 c.save!
281 301 assert_equal 2, c.version
282 302
283 303 assert_no_difference 'WikiPage.count' do
284 304 assert_no_difference 'WikiContent.count' do
285 305 assert_no_difference 'WikiContent::Version.count' do
286 306 put :update, :project_id => 1,
287 307 :id => 'Another_page',
288 308 :content => {
289 309 :comments => 'My comments',
290 310 :text => 'Text should not be lost',
291 311 :version => 1
292 312 }
293 313 end
294 314 end
295 315 end
296 316 assert_response :success
297 317 assert_template 'edit'
298 318 assert_tag :div,
299 319 :attributes => { :class => /error/ },
300 320 :content => /Data has been updated by another user/
301 321 assert_tag 'textarea',
302 322 :attributes => { :name => 'content[text]' },
303 323 :content => /Text should not be lost/
304 324 assert_tag 'input',
305 325 :attributes => { :name => 'content[comments]', :value => 'My comments' }
306 326
307 327 c.reload
308 328 assert_equal 'Previous text', c.text
309 329 assert_equal 2, c.version
310 330 end
311 331
312 332 def test_update_section
313 333 @request.session[:user_id] = 2
314 334 page = WikiPage.find_by_title('Page_with_sections')
315 335 section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
316 336 text = page.content.text
317 337
318 338 assert_no_difference 'WikiPage.count' do
319 339 assert_no_difference 'WikiContent.count' do
320 340 assert_difference 'WikiContent::Version.count' do
321 341 put :update, :project_id => 1, :id => 'Page_with_sections',
322 342 :content => {
323 343 :text => "New section content",
324 344 :version => 3
325 345 },
326 346 :section => 2,
327 347 :section_hash => hash
328 348 end
329 349 end
330 350 end
331 351 assert_redirected_to '/projects/ecookbook/wiki/Page_with_sections'
332 352 assert_equal Redmine::WikiFormatting::Textile::Formatter.new(text).update_section(2, "New section content"), page.reload.content.text
333 353 end
334 354
335 355 def test_update_section_should_allow_stale_page_update
336 356 @request.session[:user_id] = 2
337 357 page = WikiPage.find_by_title('Page_with_sections')
338 358 section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
339 359 text = page.content.text
340 360
341 361 assert_no_difference 'WikiPage.count' do
342 362 assert_no_difference 'WikiContent.count' do
343 363 assert_difference 'WikiContent::Version.count' do
344 364 put :update, :project_id => 1, :id => 'Page_with_sections',
345 365 :content => {
346 366 :text => "New section content",
347 367 :version => 2 # Current version is 3
348 368 },
349 369 :section => 2,
350 370 :section_hash => hash
351 371 end
352 372 end
353 373 end
354 374 assert_redirected_to '/projects/ecookbook/wiki/Page_with_sections'
355 375 page.reload
356 376 assert_equal Redmine::WikiFormatting::Textile::Formatter.new(text).update_section(2, "New section content"), page.content.text
357 377 assert_equal 4, page.content.version
358 378 end
359 379
360 380 def test_update_section_should_not_allow_stale_section_update
361 381 @request.session[:user_id] = 2
362 382
363 383 assert_no_difference 'WikiPage.count' do
364 384 assert_no_difference 'WikiContent.count' do
365 385 assert_no_difference 'WikiContent::Version.count' do
366 386 put :update, :project_id => 1, :id => 'Page_with_sections',
367 387 :content => {
368 388 :comments => 'My comments',
369 389 :text => "Text should not be lost",
370 390 :version => 3
371 391 },
372 392 :section => 2,
373 393 :section_hash => Digest::MD5.hexdigest("wrong hash")
374 394 end
375 395 end
376 396 end
377 397 assert_response :success
378 398 assert_template 'edit'
379 399 assert_tag :div,
380 400 :attributes => { :class => /error/ },
381 401 :content => /Data has been updated by another user/
382 402 assert_tag 'textarea',
383 403 :attributes => { :name => 'content[text]' },
384 404 :content => /Text should not be lost/
385 405 assert_tag 'input',
386 406 :attributes => { :name => 'content[comments]', :value => 'My comments' }
387 407 end
388 408
389 409 def test_preview
390 410 @request.session[:user_id] = 2
391 411 xhr :post, :preview, :project_id => 1, :id => 'CookBook_documentation',
392 412 :content => { :comments => '',
393 413 :text => 'this is a *previewed text*',
394 414 :version => 3 }
395 415 assert_response :success
396 416 assert_template 'common/_preview'
397 417 assert_tag :tag => 'strong', :content => /previewed text/
398 418 end
399 419
400 420 def test_preview_new_page
401 421 @request.session[:user_id] = 2
402 422 xhr :post, :preview, :project_id => 1, :id => 'New page',
403 423 :content => { :text => 'h1. New page',
404 424 :comments => '',
405 425 :version => 0 }
406 426 assert_response :success
407 427 assert_template 'common/_preview'
408 428 assert_tag :tag => 'h1', :content => /New page/
409 429 end
410 430
411 431 def test_history
412 432 get :history, :project_id => 1, :id => 'CookBook_documentation'
413 433 assert_response :success
414 434 assert_template 'history'
415 435 assert_not_nil assigns(:versions)
416 436 assert_equal 3, assigns(:versions).size
417 437 assert_select "input[type=submit][name=commit]"
418 438 end
419 439
420 440 def test_history_with_one_version
421 441 get :history, :project_id => 1, :id => 'Another_page'
422 442 assert_response :success
423 443 assert_template 'history'
424 444 assert_not_nil assigns(:versions)
425 445 assert_equal 1, assigns(:versions).size
426 446 assert_select "input[type=submit][name=commit]", false
427 447 end
428 448
429 449 def test_diff
430 450 get :diff, :project_id => 1, :id => 'CookBook_documentation', :version => 2, :version_from => 1
431 451 assert_response :success
432 452 assert_template 'diff'
433 453 assert_tag :tag => 'span', :attributes => { :class => 'diff_in'},
434 454 :content => /updated/
435 455 end
436 456
437 457 def test_annotate
438 458 get :annotate, :project_id => 1, :id => 'CookBook_documentation', :version => 2
439 459 assert_response :success
440 460 assert_template 'annotate'
441 461
442 462 # Line 1
443 463 assert_tag :tag => 'tr', :child => {
444 464 :tag => 'th', :attributes => {:class => 'line-num'}, :content => '1', :sibling => {
445 465 :tag => 'td', :attributes => {:class => 'author'}, :content => /John Smith/, :sibling => {
446 466 :tag => 'td', :content => /h1\. CookBook documentation/
447 467 }
448 468 }
449 469 }
450 470
451 471 # Line 5
452 472 assert_tag :tag => 'tr', :child => {
453 473 :tag => 'th', :attributes => {:class => 'line-num'}, :content => '5', :sibling => {
454 474 :tag => 'td', :attributes => {:class => 'author'}, :content => /redMine Admin/, :sibling => {
455 475 :tag => 'td', :content => /Some updated \[\[documentation\]\] here/
456 476 }
457 477 }
458 478 }
459 479 end
460 480
461 481 def test_get_rename
462 482 @request.session[:user_id] = 2
463 483 get :rename, :project_id => 1, :id => 'Another_page'
464 484 assert_response :success
465 485 assert_template 'rename'
466 486 assert_tag 'option',
467 487 :attributes => {:value => ''},
468 488 :content => '',
469 489 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
470 490 assert_no_tag 'option',
471 491 :attributes => {:selected => 'selected'},
472 492 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
473 493 end
474 494
475 495 def test_get_rename_child_page
476 496 @request.session[:user_id] = 2
477 497 get :rename, :project_id => 1, :id => 'Child_1'
478 498 assert_response :success
479 499 assert_template 'rename'
480 500 assert_tag 'option',
481 501 :attributes => {:value => ''},
482 502 :content => '',
483 503 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
484 504 assert_tag 'option',
485 505 :attributes => {:value => '2', :selected => 'selected'},
486 506 :content => /Another page/,
487 507 :parent => {
488 508 :tag => 'select',
489 509 :attributes => {:name => 'wiki_page[parent_id]'}
490 510 }
491 511 end
492 512
493 513 def test_rename_with_redirect
494 514 @request.session[:user_id] = 2
495 515 post :rename, :project_id => 1, :id => 'Another_page',
496 516 :wiki_page => { :title => 'Another renamed page',
497 517 :redirect_existing_links => 1 }
498 518 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
499 519 wiki = Project.find(1).wiki
500 520 # Check redirects
501 521 assert_not_nil wiki.find_page('Another page')
502 522 assert_nil wiki.find_page('Another page', :with_redirect => false)
503 523 end
504 524
505 525 def test_rename_without_redirect
506 526 @request.session[:user_id] = 2
507 527 post :rename, :project_id => 1, :id => 'Another_page',
508 528 :wiki_page => { :title => 'Another renamed page',
509 529 :redirect_existing_links => "0" }
510 530 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
511 531 wiki = Project.find(1).wiki
512 532 # Check that there's no redirects
513 533 assert_nil wiki.find_page('Another page')
514 534 end
515 535
516 536 def test_rename_with_parent_assignment
517 537 @request.session[:user_id] = 2
518 538 post :rename, :project_id => 1, :id => 'Another_page',
519 539 :wiki_page => { :title => 'Another page', :redirect_existing_links => "0", :parent_id => '4' }
520 540 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
521 541 assert_equal WikiPage.find(4), WikiPage.find_by_title('Another_page').parent
522 542 end
523 543
524 544 def test_rename_with_parent_unassignment
525 545 @request.session[:user_id] = 2
526 546 post :rename, :project_id => 1, :id => 'Child_1',
527 547 :wiki_page => { :title => 'Child 1', :redirect_existing_links => "0", :parent_id => '' }
528 548 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Child_1'
529 549 assert_nil WikiPage.find_by_title('Child_1').parent
530 550 end
531 551
532 552 def test_destroy_child
533 553 @request.session[:user_id] = 2
534 554 delete :destroy, :project_id => 1, :id => 'Child_1'
535 555 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
536 556 end
537 557
538 558 def test_destroy_parent
539 559 @request.session[:user_id] = 2
540 560 assert_no_difference('WikiPage.count') do
541 561 delete :destroy, :project_id => 1, :id => 'Another_page'
542 562 end
543 563 assert_response :success
544 564 assert_template 'destroy'
545 565 end
546 566
547 567 def test_destroy_parent_with_nullify
548 568 @request.session[:user_id] = 2
549 569 assert_difference('WikiPage.count', -1) do
550 570 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'nullify'
551 571 end
552 572 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
553 573 assert_nil WikiPage.find_by_id(2)
554 574 end
555 575
556 576 def test_destroy_parent_with_cascade
557 577 @request.session[:user_id] = 2
558 578 assert_difference('WikiPage.count', -3) do
559 579 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'destroy'
560 580 end
561 581 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
562 582 assert_nil WikiPage.find_by_id(2)
563 583 assert_nil WikiPage.find_by_id(5)
564 584 end
565 585
566 586 def test_destroy_parent_with_reassign
567 587 @request.session[:user_id] = 2
568 588 assert_difference('WikiPage.count', -1) do
569 589 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'reassign', :reassign_to_id => 1
570 590 end
571 591 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
572 592 assert_nil WikiPage.find_by_id(2)
573 593 assert_equal WikiPage.find(1), WikiPage.find_by_id(5).parent
574 594 end
575 595
576 596 def test_index
577 597 get :index, :project_id => 'ecookbook'
578 598 assert_response :success
579 599 assert_template 'index'
580 600 pages = assigns(:pages)
581 601 assert_not_nil pages
582 602 assert_equal Project.find(1).wiki.pages.size, pages.size
583 603 assert_equal pages.first.content.updated_on, pages.first.updated_on
584 604
585 605 assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
586 606 :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/CookBook_documentation' },
587 607 :content => 'CookBook documentation' },
588 608 :child => { :tag => 'ul',
589 609 :child => { :tag => 'li',
590 610 :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
591 611 :content => 'Page with an inline image' } } } },
592 612 :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Another_page' },
593 613 :content => 'Another page' } }
594 614 end
595 615
596 616 def test_index_should_include_atom_link
597 617 get :index, :project_id => 'ecookbook'
598 618 assert_tag 'a', :attributes => { :href => '/projects/ecookbook/activity.atom?show_wiki_edits=1'}
599 619 end
600 620
601 621 context "GET :export" do
602 622 context "with an authorized user to export the wiki" do
603 623 setup do
604 624 @request.session[:user_id] = 2
605 625 get :export, :project_id => 'ecookbook'
606 626 end
607 627
608 628 should_respond_with :success
609 629 should_assign_to :pages
610 630 should_respond_with_content_type "text/html"
611 631 should "export all of the wiki pages to a single html file" do
612 632 assert_select "a[name=?]", "CookBook_documentation"
613 633 assert_select "a[name=?]", "Another_page"
614 634 assert_select "a[name=?]", "Page_with_an_inline_image"
615 635 end
616 636
617 637 end
618 638
619 639 context "with an unauthorized user" do
620 640 setup do
621 641 get :export, :project_id => 'ecookbook'
622 642
623 643 should_respond_with :redirect
624 644 should_redirect_to('wiki index') { {:action => 'show', :project_id => @project, :id => nil} }
625 645 end
626 646 end
627 647 end
628 648
629 649 context "GET :date_index" do
630 650 setup do
631 651 get :date_index, :project_id => 'ecookbook'
632 652 end
633 653
634 654 should_respond_with :success
635 655 should_assign_to :pages
636 656 should_assign_to :pages_by_date
637 657 should_render_template 'wiki/date_index'
638 658
639 659 should "include atom link" do
640 660 assert_tag 'a', :attributes => { :href => '/projects/ecookbook/activity.atom?show_wiki_edits=1'}
641 661 end
642 662 end
643 663
644 664 def test_not_found
645 665 get :show, :project_id => 999
646 666 assert_response 404
647 667 end
648 668
649 669 def test_protect_page
650 670 page = WikiPage.find_by_wiki_id_and_title(1, 'Another_page')
651 671 assert !page.protected?
652 672 @request.session[:user_id] = 2
653 673 post :protect, :project_id => 1, :id => page.title, :protected => '1'
654 674 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
655 675 assert page.reload.protected?
656 676 end
657 677
658 678 def test_unprotect_page
659 679 page = WikiPage.find_by_wiki_id_and_title(1, 'CookBook_documentation')
660 680 assert page.protected?
661 681 @request.session[:user_id] = 2
662 682 post :protect, :project_id => 1, :id => page.title, :protected => '0'
663 683 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'CookBook_documentation'
664 684 assert !page.reload.protected?
665 685 end
666 686
667 687 def test_show_page_with_edit_link
668 688 @request.session[:user_id] = 2
669 689 get :show, :project_id => 1
670 690 assert_response :success
671 691 assert_template 'show'
672 692 assert_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
673 693 end
674 694
675 695 def test_show_page_without_edit_link
676 696 @request.session[:user_id] = 4
677 697 get :show, :project_id => 1
678 698 assert_response :success
679 699 assert_template 'show'
680 700 assert_no_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
681 701 end
682 702
683 703 def test_show_pdf
684 704 @request.session[:user_id] = 2
685 705 get :show, :project_id => 1, :format => 'pdf'
686 706 assert_response :success
687 707 assert_not_nil assigns(:page)
688 708 assert_equal 'application/pdf', @response.content_type
689 709 assert_equal 'attachment; filename="CookBook_documentation.pdf"',
690 710 @response.headers['Content-Disposition']
691 711 end
692 712
693 713 def test_show_html
694 714 @request.session[:user_id] = 2
695 715 get :show, :project_id => 1, :format => 'html'
696 716 assert_response :success
697 717 assert_not_nil assigns(:page)
698 718 assert_equal 'text/html', @response.content_type
699 719 assert_equal 'attachment; filename="CookBook_documentation.html"',
700 720 @response.headers['Content-Disposition']
701 721 end
702 722
703 723 def test_show_txt
704 724 @request.session[:user_id] = 2
705 725 get :show, :project_id => 1, :format => 'txt'
706 726 assert_response :success
707 727 assert_not_nil assigns(:page)
708 728 assert_equal 'text/plain', @response.content_type
709 729 assert_equal 'attachment; filename="CookBook_documentation.txt"',
710 730 @response.headers['Content-Disposition']
711 731 end
712 732
713 733 def test_edit_unprotected_page
714 734 # Non members can edit unprotected wiki pages
715 735 @request.session[:user_id] = 4
716 736 get :edit, :project_id => 1, :id => 'Another_page'
717 737 assert_response :success
718 738 assert_template 'edit'
719 739 end
720 740
721 741 def test_edit_protected_page_by_nonmember
722 742 # Non members can't edit protected wiki pages
723 743 @request.session[:user_id] = 4
724 744 get :edit, :project_id => 1, :id => 'CookBook_documentation'
725 745 assert_response 403
726 746 end
727 747
728 748 def test_edit_protected_page_by_member
729 749 @request.session[:user_id] = 2
730 750 get :edit, :project_id => 1, :id => 'CookBook_documentation'
731 751 assert_response :success
732 752 assert_template 'edit'
733 753 end
734 754
735 755 def test_history_of_non_existing_page_should_return_404
736 756 get :history, :project_id => 1, :id => 'Unknown_page'
737 757 assert_response 404
738 758 end
759
760 def test_add_attachment
761 @request.session[:user_id] = 2
762 assert_difference 'Attachment.count' do
763 post :add_attachment, :project_id => 1, :id => 'CookBook_documentation',
764 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
765 end
766 attachment = Attachment.first(:order => 'id DESC')
767 assert_equal Wiki.find(1).find_page('CookBook_documentation'), attachment.container
768 end
739 769 end
General Comments 0
You need to be logged in to leave comments. Login now