annotate test/functional/wiki_controller_test.rb @ 8:0c83d98252d9 yuya

* Add custom repo prefix and proper auth realm, remove auth cache (seems like an unwise feature), pass DB handle around, various other bits of tidying
author Chris Cannam
date Thu, 12 Aug 2010 15:31:37 +0100
parents 513646585e45
children 94944d00e43c
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006-2007 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@0 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@0 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@0 18 require File.dirname(__FILE__) + '/../test_helper'
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@0 25 fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions, :attachments
Chris@0 26
Chris@0 27 def setup
Chris@0 28 @controller = WikiController.new
Chris@0 29 @request = ActionController::TestRequest.new
Chris@0 30 @response = ActionController::TestResponse.new
Chris@0 31 User.current = nil
Chris@0 32 end
Chris@0 33
Chris@0 34 def test_show_start_page
Chris@0 35 get :index, :id => 'ecookbook'
Chris@0 36 assert_response :success
Chris@0 37 assert_template 'show'
Chris@0 38 assert_tag :tag => 'h1', :content => /CookBook documentation/
Chris@0 39
Chris@0 40 # child_pages macro
Chris@0 41 assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
Chris@0 42 :child => { :tag => 'li',
Chris@0 43 :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
Chris@0 44 :content => 'Page with an inline image' } }
Chris@0 45 end
Chris@0 46
Chris@0 47 def test_show_page_with_name
Chris@0 48 get :index, :id => 1, :page => 'Another_page'
Chris@0 49 assert_response :success
Chris@0 50 assert_template 'show'
Chris@0 51 assert_tag :tag => 'h1', :content => /Another page/
Chris@0 52 # Included page with an inline image
Chris@0 53 assert_tag :tag => 'p', :content => /This is an inline image/
Chris@0 54 assert_tag :tag => 'img', :attributes => { :src => '/attachments/download/3',
Chris@0 55 :alt => 'This is a logo' }
Chris@0 56 end
Chris@0 57
Chris@0 58 def test_show_with_sidebar
Chris@0 59 page = Project.find(1).wiki.pages.new(:title => 'Sidebar')
Chris@0 60 page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar')
Chris@0 61 page.save!
Chris@0 62
Chris@0 63 get :index, :id => 1, :page => 'Another_page'
Chris@0 64 assert_response :success
Chris@0 65 assert_tag :tag => 'div', :attributes => {:id => 'sidebar'},
Chris@0 66 :content => /Side bar content for test_show_with_sidebar/
Chris@0 67 end
Chris@0 68
Chris@0 69 def test_show_unexistent_page_without_edit_right
Chris@0 70 get :index, :id => 1, :page => 'Unexistent page'
Chris@0 71 assert_response 404
Chris@0 72 end
Chris@0 73
Chris@0 74 def test_show_unexistent_page_with_edit_right
Chris@0 75 @request.session[:user_id] = 2
Chris@0 76 get :index, :id => 1, :page => 'Unexistent page'
Chris@0 77 assert_response :success
Chris@0 78 assert_template 'edit'
Chris@0 79 end
Chris@0 80
Chris@0 81 def test_create_page
Chris@0 82 @request.session[:user_id] = 2
Chris@0 83 post :edit, :id => 1,
Chris@0 84 :page => 'New page',
Chris@0 85 :content => {:comments => 'Created the page',
Chris@0 86 :text => "h1. New page\n\nThis is a new page",
Chris@0 87 :version => 0}
Chris@0 88 assert_redirected_to :action => 'index', :id => 'ecookbook', :page => 'New_page'
Chris@0 89 page = Project.find(1).wiki.find_page('New page')
Chris@0 90 assert !page.new_record?
Chris@0 91 assert_not_nil page.content
Chris@0 92 assert_equal 'Created the page', page.content.comments
Chris@0 93 end
Chris@0 94
Chris@0 95 def test_create_page_with_attachments
Chris@0 96 @request.session[:user_id] = 2
Chris@0 97 assert_difference 'WikiPage.count' do
Chris@0 98 assert_difference 'Attachment.count' do
Chris@0 99 post :edit, :id => 1,
Chris@0 100 :page => 'New page',
Chris@0 101 :content => {:comments => 'Created the page',
Chris@0 102 :text => "h1. New page\n\nThis is a new page",
Chris@0 103 :version => 0},
Chris@0 104 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
Chris@0 105 end
Chris@0 106 end
Chris@0 107 page = Project.find(1).wiki.find_page('New page')
Chris@0 108 assert_equal 1, page.attachments.count
Chris@0 109 assert_equal 'testfile.txt', page.attachments.first.filename
Chris@0 110 end
Chris@0 111
Chris@0 112 def test_preview
Chris@0 113 @request.session[:user_id] = 2
Chris@0 114 xhr :post, :preview, :id => 1, :page => 'CookBook_documentation',
Chris@0 115 :content => { :comments => '',
Chris@0 116 :text => 'this is a *previewed text*',
Chris@0 117 :version => 3 }
Chris@0 118 assert_response :success
Chris@0 119 assert_template 'common/_preview'
Chris@0 120 assert_tag :tag => 'strong', :content => /previewed text/
Chris@0 121 end
Chris@0 122
Chris@0 123 def test_preview_new_page
Chris@0 124 @request.session[:user_id] = 2
Chris@0 125 xhr :post, :preview, :id => 1, :page => 'New page',
Chris@0 126 :content => { :text => 'h1. New page',
Chris@0 127 :comments => '',
Chris@0 128 :version => 0 }
Chris@0 129 assert_response :success
Chris@0 130 assert_template 'common/_preview'
Chris@0 131 assert_tag :tag => 'h1', :content => /New page/
Chris@0 132 end
Chris@0 133
Chris@0 134 def test_history
Chris@0 135 get :history, :id => 1, :page => 'CookBook_documentation'
Chris@0 136 assert_response :success
Chris@0 137 assert_template 'history'
Chris@0 138 assert_not_nil assigns(:versions)
Chris@0 139 assert_equal 3, assigns(:versions).size
Chris@0 140 assert_select "input[type=submit][name=commit]"
Chris@0 141 end
Chris@0 142
Chris@0 143 def test_history_with_one_version
Chris@0 144 get :history, :id => 1, :page => 'Another_page'
Chris@0 145 assert_response :success
Chris@0 146 assert_template 'history'
Chris@0 147 assert_not_nil assigns(:versions)
Chris@0 148 assert_equal 1, assigns(:versions).size
Chris@0 149 assert_select "input[type=submit][name=commit]", false
Chris@0 150 end
Chris@0 151
Chris@0 152 def test_diff
Chris@0 153 get :diff, :id => 1, :page => 'CookBook_documentation', :version => 2, :version_from => 1
Chris@0 154 assert_response :success
Chris@0 155 assert_template 'diff'
Chris@0 156 assert_tag :tag => 'span', :attributes => { :class => 'diff_in'},
Chris@0 157 :content => /updated/
Chris@0 158 end
Chris@0 159
Chris@0 160 def test_annotate
Chris@0 161 get :annotate, :id => 1, :page => 'CookBook_documentation', :version => 2
Chris@0 162 assert_response :success
Chris@0 163 assert_template 'annotate'
Chris@0 164 # Line 1
Chris@0 165 assert_tag :tag => 'tr', :child => { :tag => 'th', :attributes => {:class => 'line-num'}, :content => '1' },
Chris@0 166 :child => { :tag => 'td', :attributes => {:class => 'author'}, :content => /John Smith/ },
Chris@0 167 :child => { :tag => 'td', :content => /h1\. CookBook documentation/ }
Chris@0 168 # Line 2
Chris@0 169 assert_tag :tag => 'tr', :child => { :tag => 'th', :attributes => {:class => 'line-num'}, :content => '2' },
Chris@0 170 :child => { :tag => 'td', :attributes => {:class => 'author'}, :content => /redMine Admin/ },
Chris@0 171 :child => { :tag => 'td', :content => /Some updated \[\[documentation\]\] here/ }
Chris@0 172 end
Chris@0 173
Chris@0 174 def test_rename_with_redirect
Chris@0 175 @request.session[:user_id] = 2
Chris@0 176 post :rename, :id => 1, :page => 'Another_page',
Chris@0 177 :wiki_page => { :title => 'Another renamed page',
Chris@0 178 :redirect_existing_links => 1 }
Chris@0 179 assert_redirected_to :action => 'index', :id => 'ecookbook', :page => 'Another_renamed_page'
Chris@0 180 wiki = Project.find(1).wiki
Chris@0 181 # Check redirects
Chris@0 182 assert_not_nil wiki.find_page('Another page')
Chris@0 183 assert_nil wiki.find_page('Another page', :with_redirect => false)
Chris@0 184 end
Chris@0 185
Chris@0 186 def test_rename_without_redirect
Chris@0 187 @request.session[:user_id] = 2
Chris@0 188 post :rename, :id => 1, :page => 'Another_page',
Chris@0 189 :wiki_page => { :title => 'Another renamed page',
Chris@0 190 :redirect_existing_links => "0" }
Chris@0 191 assert_redirected_to :action => 'index', :id => 'ecookbook', :page => 'Another_renamed_page'
Chris@0 192 wiki = Project.find(1).wiki
Chris@0 193 # Check that there's no redirects
Chris@0 194 assert_nil wiki.find_page('Another page')
Chris@0 195 end
Chris@0 196
Chris@0 197 def test_destroy_child
Chris@0 198 @request.session[:user_id] = 2
Chris@0 199 post :destroy, :id => 1, :page => 'Child_1'
Chris@0 200 assert_redirected_to :action => 'special', :id => 'ecookbook', :page => 'Page_index'
Chris@0 201 end
Chris@0 202
Chris@0 203 def test_destroy_parent
Chris@0 204 @request.session[:user_id] = 2
Chris@0 205 assert_no_difference('WikiPage.count') do
Chris@0 206 post :destroy, :id => 1, :page => 'Another_page'
Chris@0 207 end
Chris@0 208 assert_response :success
Chris@0 209 assert_template 'destroy'
Chris@0 210 end
Chris@0 211
Chris@0 212 def test_destroy_parent_with_nullify
Chris@0 213 @request.session[:user_id] = 2
Chris@0 214 assert_difference('WikiPage.count', -1) do
Chris@0 215 post :destroy, :id => 1, :page => 'Another_page', :todo => 'nullify'
Chris@0 216 end
Chris@0 217 assert_redirected_to :action => 'special', :id => 'ecookbook', :page => 'Page_index'
Chris@0 218 assert_nil WikiPage.find_by_id(2)
Chris@0 219 end
Chris@0 220
Chris@0 221 def test_destroy_parent_with_cascade
Chris@0 222 @request.session[:user_id] = 2
Chris@0 223 assert_difference('WikiPage.count', -3) do
Chris@0 224 post :destroy, :id => 1, :page => 'Another_page', :todo => 'destroy'
Chris@0 225 end
Chris@0 226 assert_redirected_to :action => 'special', :id => 'ecookbook', :page => 'Page_index'
Chris@0 227 assert_nil WikiPage.find_by_id(2)
Chris@0 228 assert_nil WikiPage.find_by_id(5)
Chris@0 229 end
Chris@0 230
Chris@0 231 def test_destroy_parent_with_reassign
Chris@0 232 @request.session[:user_id] = 2
Chris@0 233 assert_difference('WikiPage.count', -1) do
Chris@0 234 post :destroy, :id => 1, :page => 'Another_page', :todo => 'reassign', :reassign_to_id => 1
Chris@0 235 end
Chris@0 236 assert_redirected_to :action => 'special', :id => 'ecookbook', :page => 'Page_index'
Chris@0 237 assert_nil WikiPage.find_by_id(2)
Chris@0 238 assert_equal WikiPage.find(1), WikiPage.find_by_id(5).parent
Chris@0 239 end
Chris@0 240
Chris@0 241 def test_page_index
Chris@0 242 get :special, :id => 'ecookbook', :page => 'Page_index'
Chris@0 243 assert_response :success
Chris@0 244 assert_template 'special_page_index'
Chris@0 245 pages = assigns(:pages)
Chris@0 246 assert_not_nil pages
Chris@0 247 assert_equal Project.find(1).wiki.pages.size, pages.size
Chris@0 248
Chris@0 249 assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
Chris@0 250 :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/CookBook_documentation' },
Chris@0 251 :content => 'CookBook documentation' },
Chris@0 252 :child => { :tag => 'ul',
Chris@0 253 :child => { :tag => 'li',
Chris@0 254 :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
Chris@0 255 :content => 'Page with an inline image' } } } },
Chris@0 256 :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Another_page' },
Chris@0 257 :content => 'Another page' } }
Chris@0 258 end
Chris@0 259
Chris@0 260 def test_not_found
Chris@0 261 get :index, :id => 999
Chris@0 262 assert_response 404
Chris@0 263 end
Chris@0 264
Chris@0 265 def test_protect_page
Chris@0 266 page = WikiPage.find_by_wiki_id_and_title(1, 'Another_page')
Chris@0 267 assert !page.protected?
Chris@0 268 @request.session[:user_id] = 2
Chris@0 269 post :protect, :id => 1, :page => page.title, :protected => '1'
Chris@0 270 assert_redirected_to :action => 'index', :id => 'ecookbook', :page => 'Another_page'
Chris@0 271 assert page.reload.protected?
Chris@0 272 end
Chris@0 273
Chris@0 274 def test_unprotect_page
Chris@0 275 page = WikiPage.find_by_wiki_id_and_title(1, 'CookBook_documentation')
Chris@0 276 assert page.protected?
Chris@0 277 @request.session[:user_id] = 2
Chris@0 278 post :protect, :id => 1, :page => page.title, :protected => '0'
Chris@0 279 assert_redirected_to :action => 'index', :id => 'ecookbook', :page => 'CookBook_documentation'
Chris@0 280 assert !page.reload.protected?
Chris@0 281 end
Chris@0 282
Chris@0 283 def test_show_page_with_edit_link
Chris@0 284 @request.session[:user_id] = 2
Chris@0 285 get :index, :id => 1
Chris@0 286 assert_response :success
Chris@0 287 assert_template 'show'
Chris@0 288 assert_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
Chris@0 289 end
Chris@0 290
Chris@0 291 def test_show_page_without_edit_link
Chris@0 292 @request.session[:user_id] = 4
Chris@0 293 get :index, :id => 1
Chris@0 294 assert_response :success
Chris@0 295 assert_template 'show'
Chris@0 296 assert_no_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
Chris@0 297 end
Chris@0 298
Chris@0 299 def test_edit_unprotected_page
Chris@0 300 # Non members can edit unprotected wiki pages
Chris@0 301 @request.session[:user_id] = 4
Chris@0 302 get :edit, :id => 1, :page => 'Another_page'
Chris@0 303 assert_response :success
Chris@0 304 assert_template 'edit'
Chris@0 305 end
Chris@0 306
Chris@0 307 def test_edit_protected_page_by_nonmember
Chris@0 308 # Non members can't edit protected wiki pages
Chris@0 309 @request.session[:user_id] = 4
Chris@0 310 get :edit, :id => 1, :page => 'CookBook_documentation'
Chris@0 311 assert_response 403
Chris@0 312 end
Chris@0 313
Chris@0 314 def test_edit_protected_page_by_member
Chris@0 315 @request.session[:user_id] = 2
Chris@0 316 get :edit, :id => 1, :page => 'CookBook_documentation'
Chris@0 317 assert_response :success
Chris@0 318 assert_template 'edit'
Chris@0 319 end
Chris@0 320
Chris@0 321 def test_history_of_non_existing_page_should_return_404
Chris@0 322 get :history, :id => 1, :page => 'Unknown_page'
Chris@0 323 assert_response 404
Chris@0 324 end
Chris@0 325 end