##// END OF EJS Templates
test: functional: wiki: add tests to show html and txt (#401)...
Toshi MARUYAMA -
r7619:0921042bc461
parent child
Show More
@@ -1,523 +1,543
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_show_page_with_name
50 50 get :show, :project_id => 1, :id => 'Another_page'
51 51 assert_response :success
52 52 assert_template 'show'
53 53 assert_tag :tag => 'h1', :content => /Another page/
54 54 # Included page with an inline image
55 55 assert_tag :tag => 'p', :content => /This is an inline image/
56 56 assert_tag :tag => 'img', :attributes => { :src => '/attachments/download/3',
57 57 :alt => 'This is a logo' }
58 58 end
59 59
60 60 def test_show_redirected_page
61 61 WikiRedirect.create!(:wiki_id => 1, :title => 'Old_title', :redirects_to => 'Another_page')
62 62
63 63 get :show, :project_id => 'ecookbook', :id => 'Old_title'
64 64 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
65 65 end
66 66
67 67 def test_show_with_sidebar
68 68 page = Project.find(1).wiki.pages.new(:title => 'Sidebar')
69 69 page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar')
70 70 page.save!
71 71
72 72 get :show, :project_id => 1, :id => 'Another_page'
73 73 assert_response :success
74 74 assert_tag :tag => 'div', :attributes => {:id => 'sidebar'},
75 75 :content => /Side bar content for test_show_with_sidebar/
76 76 end
77 77
78 78 def test_show_unexistent_page_without_edit_right
79 79 get :show, :project_id => 1, :id => 'Unexistent page'
80 80 assert_response 404
81 81 end
82 82
83 83 def test_show_unexistent_page_with_edit_right
84 84 @request.session[:user_id] = 2
85 85 get :show, :project_id => 1, :id => 'Unexistent page'
86 86 assert_response :success
87 87 assert_template 'edit'
88 88 end
89 89
90 90 def test_create_page
91 91 @request.session[:user_id] = 2
92 92 put :update, :project_id => 1,
93 93 :id => 'New page',
94 94 :content => {:comments => 'Created the page',
95 95 :text => "h1. New page\n\nThis is a new page",
96 96 :version => 0}
97 97 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'New_page'
98 98 page = Project.find(1).wiki.find_page('New page')
99 99 assert !page.new_record?
100 100 assert_not_nil page.content
101 101 assert_equal 'Created the page', page.content.comments
102 102 end
103 103
104 104 def test_create_page_with_attachments
105 105 @request.session[:user_id] = 2
106 106 assert_difference 'WikiPage.count' do
107 107 assert_difference 'Attachment.count' do
108 108 put :update, :project_id => 1,
109 109 :id => 'New page',
110 110 :content => {:comments => 'Created the page',
111 111 :text => "h1. New page\n\nThis is a new page",
112 112 :version => 0},
113 113 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
114 114 end
115 115 end
116 116 page = Project.find(1).wiki.find_page('New page')
117 117 assert_equal 1, page.attachments.count
118 118 assert_equal 'testfile.txt', page.attachments.first.filename
119 119 end
120 120
121 121 def test_update_page
122 122 @request.session[:user_id] = 2
123 123 assert_no_difference 'WikiPage.count' do
124 124 assert_no_difference 'WikiContent.count' do
125 125 assert_difference 'WikiContent::Version.count' do
126 126 put :update, :project_id => 1,
127 127 :id => 'Another_page',
128 128 :content => {
129 129 :comments => "my comments",
130 130 :text => "edited",
131 131 :version => 1
132 132 }
133 133 end
134 134 end
135 135 end
136 136 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
137 137
138 138 page = Wiki.find(1).pages.find_by_title('Another_page')
139 139 assert_equal "edited", page.content.text
140 140 assert_equal 2, page.content.version
141 141 assert_equal "my comments", page.content.comments
142 142 end
143 143
144 144 def test_update_page_with_failure
145 145 @request.session[:user_id] = 2
146 146 assert_no_difference 'WikiPage.count' do
147 147 assert_no_difference 'WikiContent.count' do
148 148 assert_no_difference 'WikiContent::Version.count' do
149 149 put :update, :project_id => 1,
150 150 :id => 'Another_page',
151 151 :content => {
152 152 :comments => 'a' * 300, # failure here, comment is too long
153 153 :text => 'edited',
154 154 :version => 1
155 155 }
156 156 end
157 157 end
158 158 end
159 159 assert_response :success
160 160 assert_template 'edit'
161 161
162 162 assert_error_tag :descendant => {:content => /Comment is too long/}
163 163 assert_tag :tag => 'textarea', :attributes => {:id => 'content_text'}, :content => 'edited'
164 164 assert_tag :tag => 'input', :attributes => {:id => 'content_version', :value => '1'}
165 165 end
166 166
167 167 def test_update_stale_page_should_not_raise_an_error
168 168 @request.session[:user_id] = 2
169 169 c = Wiki.find(1).find_page('Another_page').content
170 170 c.text = 'Previous text'
171 171 c.save!
172 172 assert_equal 2, c.version
173 173
174 174 assert_no_difference 'WikiPage.count' do
175 175 assert_no_difference 'WikiContent.count' do
176 176 assert_no_difference 'WikiContent::Version.count' do
177 177 put :update, :project_id => 1,
178 178 :id => 'Another_page',
179 179 :content => {
180 180 :comments => 'My comments',
181 181 :text => 'Text should not be lost',
182 182 :version => 1
183 183 }
184 184 end
185 185 end
186 186 end
187 187 assert_response :success
188 188 assert_template 'edit'
189 189 assert_tag :div,
190 190 :attributes => { :class => /error/ },
191 191 :content => /Data has been updated by another user/
192 192 assert_tag 'textarea',
193 193 :attributes => { :name => 'content[text]' },
194 194 :content => /Text should not be lost/
195 195 assert_tag 'input',
196 196 :attributes => { :name => 'content[comments]', :value => 'My comments' }
197 197
198 198 c.reload
199 199 assert_equal 'Previous text', c.text
200 200 assert_equal 2, c.version
201 201 end
202 202
203 203 def test_preview
204 204 @request.session[:user_id] = 2
205 205 xhr :post, :preview, :project_id => 1, :id => 'CookBook_documentation',
206 206 :content => { :comments => '',
207 207 :text => 'this is a *previewed text*',
208 208 :version => 3 }
209 209 assert_response :success
210 210 assert_template 'common/_preview'
211 211 assert_tag :tag => 'strong', :content => /previewed text/
212 212 end
213 213
214 214 def test_preview_new_page
215 215 @request.session[:user_id] = 2
216 216 xhr :post, :preview, :project_id => 1, :id => 'New page',
217 217 :content => { :text => 'h1. New page',
218 218 :comments => '',
219 219 :version => 0 }
220 220 assert_response :success
221 221 assert_template 'common/_preview'
222 222 assert_tag :tag => 'h1', :content => /New page/
223 223 end
224 224
225 225 def test_history
226 226 get :history, :project_id => 1, :id => 'CookBook_documentation'
227 227 assert_response :success
228 228 assert_template 'history'
229 229 assert_not_nil assigns(:versions)
230 230 assert_equal 3, assigns(:versions).size
231 231 assert_select "input[type=submit][name=commit]"
232 232 end
233 233
234 234 def test_history_with_one_version
235 235 get :history, :project_id => 1, :id => 'Another_page'
236 236 assert_response :success
237 237 assert_template 'history'
238 238 assert_not_nil assigns(:versions)
239 239 assert_equal 1, assigns(:versions).size
240 240 assert_select "input[type=submit][name=commit]", false
241 241 end
242 242
243 243 def test_diff
244 244 get :diff, :project_id => 1, :id => 'CookBook_documentation', :version => 2, :version_from => 1
245 245 assert_response :success
246 246 assert_template 'diff'
247 247 assert_tag :tag => 'span', :attributes => { :class => 'diff_in'},
248 248 :content => /updated/
249 249 end
250 250
251 251 def test_annotate
252 252 get :annotate, :project_id => 1, :id => 'CookBook_documentation', :version => 2
253 253 assert_response :success
254 254 assert_template 'annotate'
255 255
256 256 # Line 1
257 257 assert_tag :tag => 'tr', :child => {
258 258 :tag => 'th', :attributes => {:class => 'line-num'}, :content => '1', :sibling => {
259 259 :tag => 'td', :attributes => {:class => 'author'}, :content => /John Smith/, :sibling => {
260 260 :tag => 'td', :content => /h1\. CookBook documentation/
261 261 }
262 262 }
263 263 }
264 264
265 265 # Line 5
266 266 assert_tag :tag => 'tr', :child => {
267 267 :tag => 'th', :attributes => {:class => 'line-num'}, :content => '5', :sibling => {
268 268 :tag => 'td', :attributes => {:class => 'author'}, :content => /redMine Admin/, :sibling => {
269 269 :tag => 'td', :content => /Some updated \[\[documentation\]\] here/
270 270 }
271 271 }
272 272 }
273 273 end
274 274
275 275 def test_get_rename
276 276 @request.session[:user_id] = 2
277 277 get :rename, :project_id => 1, :id => 'Another_page'
278 278 assert_response :success
279 279 assert_template 'rename'
280 280 assert_tag 'option',
281 281 :attributes => {:value => ''},
282 282 :content => '',
283 283 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
284 284 assert_no_tag 'option',
285 285 :attributes => {:selected => 'selected'},
286 286 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
287 287 end
288 288
289 289 def test_get_rename_child_page
290 290 @request.session[:user_id] = 2
291 291 get :rename, :project_id => 1, :id => 'Child_1'
292 292 assert_response :success
293 293 assert_template 'rename'
294 294 assert_tag 'option',
295 295 :attributes => {:value => ''},
296 296 :content => '',
297 297 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
298 298 assert_tag 'option',
299 299 :attributes => {:value => '2', :selected => 'selected'},
300 300 :content => /Another page/,
301 301 :parent => {
302 302 :tag => 'select',
303 303 :attributes => {:name => 'wiki_page[parent_id]'}
304 304 }
305 305 end
306 306
307 307 def test_rename_with_redirect
308 308 @request.session[:user_id] = 2
309 309 post :rename, :project_id => 1, :id => 'Another_page',
310 310 :wiki_page => { :title => 'Another renamed page',
311 311 :redirect_existing_links => 1 }
312 312 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
313 313 wiki = Project.find(1).wiki
314 314 # Check redirects
315 315 assert_not_nil wiki.find_page('Another page')
316 316 assert_nil wiki.find_page('Another page', :with_redirect => false)
317 317 end
318 318
319 319 def test_rename_without_redirect
320 320 @request.session[:user_id] = 2
321 321 post :rename, :project_id => 1, :id => 'Another_page',
322 322 :wiki_page => { :title => 'Another renamed page',
323 323 :redirect_existing_links => "0" }
324 324 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
325 325 wiki = Project.find(1).wiki
326 326 # Check that there's no redirects
327 327 assert_nil wiki.find_page('Another page')
328 328 end
329 329
330 330 def test_rename_with_parent_assignment
331 331 @request.session[:user_id] = 2
332 332 post :rename, :project_id => 1, :id => 'Another_page',
333 333 :wiki_page => { :title => 'Another page', :redirect_existing_links => "0", :parent_id => '4' }
334 334 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
335 335 assert_equal WikiPage.find(4), WikiPage.find_by_title('Another_page').parent
336 336 end
337 337
338 338 def test_rename_with_parent_unassignment
339 339 @request.session[:user_id] = 2
340 340 post :rename, :project_id => 1, :id => 'Child_1',
341 341 :wiki_page => { :title => 'Child 1', :redirect_existing_links => "0", :parent_id => '' }
342 342 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Child_1'
343 343 assert_nil WikiPage.find_by_title('Child_1').parent
344 344 end
345 345
346 346 def test_destroy_child
347 347 @request.session[:user_id] = 2
348 348 delete :destroy, :project_id => 1, :id => 'Child_1'
349 349 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
350 350 end
351 351
352 352 def test_destroy_parent
353 353 @request.session[:user_id] = 2
354 354 assert_no_difference('WikiPage.count') do
355 355 delete :destroy, :project_id => 1, :id => 'Another_page'
356 356 end
357 357 assert_response :success
358 358 assert_template 'destroy'
359 359 end
360 360
361 361 def test_destroy_parent_with_nullify
362 362 @request.session[:user_id] = 2
363 363 assert_difference('WikiPage.count', -1) do
364 364 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'nullify'
365 365 end
366 366 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
367 367 assert_nil WikiPage.find_by_id(2)
368 368 end
369 369
370 370 def test_destroy_parent_with_cascade
371 371 @request.session[:user_id] = 2
372 372 assert_difference('WikiPage.count', -3) do
373 373 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'destroy'
374 374 end
375 375 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
376 376 assert_nil WikiPage.find_by_id(2)
377 377 assert_nil WikiPage.find_by_id(5)
378 378 end
379 379
380 380 def test_destroy_parent_with_reassign
381 381 @request.session[:user_id] = 2
382 382 assert_difference('WikiPage.count', -1) do
383 383 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'reassign', :reassign_to_id => 1
384 384 end
385 385 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
386 386 assert_nil WikiPage.find_by_id(2)
387 387 assert_equal WikiPage.find(1), WikiPage.find_by_id(5).parent
388 388 end
389 389
390 390 def test_index
391 391 get :index, :project_id => 'ecookbook'
392 392 assert_response :success
393 393 assert_template 'index'
394 394 pages = assigns(:pages)
395 395 assert_not_nil pages
396 396 assert_equal Project.find(1).wiki.pages.size, pages.size
397 397 assert_equal pages.first.content.updated_on, pages.first.updated_on
398 398
399 399 assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
400 400 :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/CookBook_documentation' },
401 401 :content => 'CookBook documentation' },
402 402 :child => { :tag => 'ul',
403 403 :child => { :tag => 'li',
404 404 :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
405 405 :content => 'Page with an inline image' } } } },
406 406 :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Another_page' },
407 407 :content => 'Another page' } }
408 408 end
409 409
410 410 def test_index_should_include_atom_link
411 411 get :index, :project_id => 'ecookbook'
412 412 assert_tag 'a', :attributes => { :href => '/projects/ecookbook/activity.atom?show_wiki_edits=1'}
413 413 end
414 414
415 415 context "GET :export" do
416 416 context "with an authorized user to export the wiki" do
417 417 setup do
418 418 @request.session[:user_id] = 2
419 419 get :export, :project_id => 'ecookbook'
420 420 end
421 421
422 422 should_respond_with :success
423 423 should_assign_to :pages
424 424 should_respond_with_content_type "text/html"
425 425 should "export all of the wiki pages to a single html file" do
426 426 assert_select "a[name=?]", "CookBook_documentation"
427 427 assert_select "a[name=?]", "Another_page"
428 428 assert_select "a[name=?]", "Page_with_an_inline_image"
429 429 end
430 430
431 431 end
432 432
433 433 context "with an unauthorized user" do
434 434 setup do
435 435 get :export, :project_id => 'ecookbook'
436 436
437 437 should_respond_with :redirect
438 438 should_redirect_to('wiki index') { {:action => 'show', :project_id => @project, :id => nil} }
439 439 end
440 440 end
441 441 end
442 442
443 443 context "GET :date_index" do
444 444 setup do
445 445 get :date_index, :project_id => 'ecookbook'
446 446 end
447 447
448 448 should_respond_with :success
449 449 should_assign_to :pages
450 450 should_assign_to :pages_by_date
451 451 should_render_template 'wiki/date_index'
452 452
453 453 should "include atom link" do
454 454 assert_tag 'a', :attributes => { :href => '/projects/ecookbook/activity.atom?show_wiki_edits=1'}
455 455 end
456 456 end
457 457
458 458 def test_not_found
459 459 get :show, :project_id => 999
460 460 assert_response 404
461 461 end
462 462
463 463 def test_protect_page
464 464 page = WikiPage.find_by_wiki_id_and_title(1, 'Another_page')
465 465 assert !page.protected?
466 466 @request.session[:user_id] = 2
467 467 post :protect, :project_id => 1, :id => page.title, :protected => '1'
468 468 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
469 469 assert page.reload.protected?
470 470 end
471 471
472 472 def test_unprotect_page
473 473 page = WikiPage.find_by_wiki_id_and_title(1, 'CookBook_documentation')
474 474 assert page.protected?
475 475 @request.session[:user_id] = 2
476 476 post :protect, :project_id => 1, :id => page.title, :protected => '0'
477 477 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'CookBook_documentation'
478 478 assert !page.reload.protected?
479 479 end
480 480
481 481 def test_show_page_with_edit_link
482 482 @request.session[:user_id] = 2
483 483 get :show, :project_id => 1
484 484 assert_response :success
485 485 assert_template 'show'
486 486 assert_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
487 487 end
488 488
489 489 def test_show_page_without_edit_link
490 490 @request.session[:user_id] = 4
491 491 get :show, :project_id => 1
492 492 assert_response :success
493 493 assert_template 'show'
494 494 assert_no_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
495 495 end
496 496
497 def test_show_html
498 @request.session[:user_id] = 2
499 get :show, :project_id => 1, :format => 'html'
500 assert_response :success
501 assert_not_nil assigns(:page)
502 assert_equal 'text/html', @response.content_type
503 assert_equal 'attachment; filename="CookBook_documentation.html"',
504 @response.headers['Content-Disposition']
505 end
506
507 def test_show_txt
508 @request.session[:user_id] = 2
509 get :show, :project_id => 1, :format => 'txt'
510 assert_response :success
511 assert_not_nil assigns(:page)
512 assert_equal 'text/plain', @response.content_type
513 assert_equal 'attachment; filename="CookBook_documentation.txt"',
514 @response.headers['Content-Disposition']
515 end
516
497 517 def test_edit_unprotected_page
498 518 # Non members can edit unprotected wiki pages
499 519 @request.session[:user_id] = 4
500 520 get :edit, :project_id => 1, :id => 'Another_page'
501 521 assert_response :success
502 522 assert_template 'edit'
503 523 end
504 524
505 525 def test_edit_protected_page_by_nonmember
506 526 # Non members can't edit protected wiki pages
507 527 @request.session[:user_id] = 4
508 528 get :edit, :project_id => 1, :id => 'CookBook_documentation'
509 529 assert_response 403
510 530 end
511 531
512 532 def test_edit_protected_page_by_member
513 533 @request.session[:user_id] = 2
514 534 get :edit, :project_id => 1, :id => 'CookBook_documentation'
515 535 assert_response :success
516 536 assert_template 'edit'
517 537 end
518 538
519 539 def test_history_of_non_existing_page_should_return_404
520 540 get :history, :project_id => 1, :id => 'Unknown_page'
521 541 assert_response 404
522 542 end
523 543 end
General Comments 0
You need to be logged in to leave comments. Login now