annotate test/functional/wiki_controller_test.rb @ 1082:997f6d7738f7 bug_531

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