annotate test/functional/projects_controller_test.rb @ 866:2cd212a468b6 bug_152

Close obsolete branch bug_152
author Chris Cannam
date Wed, 11 May 2011 10:08:34 +0100
parents 94944d00e43c
children af80e5618e9b
rev   line source
Chris@0 1 # Redmine - project management software
Chris@0 2 # Copyright (C) 2006-2008 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 'projects_controller'
Chris@0 20
Chris@0 21 # Re-raise errors caught by the controller.
Chris@0 22 class ProjectsController; def rescue_action(e) raise e end; end
Chris@0 23
Chris@0 24 class ProjectsControllerTest < ActionController::TestCase
Chris@0 25 fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details,
Chris@0 26 :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages,
Chris@0 27 :attachments, :custom_fields, :custom_values, :time_entries
Chris@0 28
Chris@0 29 def setup
Chris@0 30 @controller = ProjectsController.new
Chris@0 31 @request = ActionController::TestRequest.new
Chris@0 32 @response = ActionController::TestResponse.new
Chris@0 33 @request.session[:user_id] = nil
Chris@0 34 Setting.default_language = 'en'
Chris@0 35 end
Chris@0 36
Chris@0 37 def test_index
Chris@0 38 get :index
Chris@0 39 assert_response :success
Chris@0 40 assert_template 'index'
Chris@0 41 assert_not_nil assigns(:projects)
Chris@0 42
Chris@0 43 assert_tag :ul, :child => {:tag => 'li',
Chris@0 44 :descendant => {:tag => 'a', :content => 'eCookbook'},
Chris@0 45 :child => { :tag => 'ul',
Chris@0 46 :descendant => { :tag => 'a',
Chris@0 47 :content => 'Child of private child'
Chris@0 48 }
Chris@0 49 }
Chris@0 50 }
Chris@0 51
Chris@0 52 assert_no_tag :a, :content => /Private child of eCookbook/
Chris@0 53 end
Chris@0 54
Chris@0 55 def test_index_atom
Chris@0 56 get :index, :format => 'atom'
Chris@0 57 assert_response :success
Chris@0 58 assert_template 'common/feed.atom.rxml'
Chris@0 59 assert_select 'feed>title', :text => 'Redmine: Latest projects'
Chris@0 60 assert_select 'feed>entry', :count => Project.count(:conditions => Project.visible_by(User.current))
Chris@0 61 end
Chris@0 62
Chris@0 63 context "#index" do
Chris@0 64 context "by non-admin user with view_time_entries permission" do
Chris@0 65 setup do
Chris@0 66 @request.session[:user_id] = 3
Chris@0 67 end
Chris@0 68 should "show overall spent time link" do
Chris@0 69 get :index
Chris@0 70 assert_template 'index'
Chris@0 71 assert_tag :a, :attributes => {:href => '/time_entries'}
Chris@0 72 end
Chris@0 73 end
Chris@0 74
Chris@0 75 context "by non-admin user without view_time_entries permission" do
Chris@0 76 setup do
Chris@0 77 Role.find(2).remove_permission! :view_time_entries
Chris@0 78 Role.non_member.remove_permission! :view_time_entries
Chris@0 79 Role.anonymous.remove_permission! :view_time_entries
Chris@0 80 @request.session[:user_id] = 3
Chris@0 81 end
Chris@0 82 should "not show overall spent time link" do
Chris@0 83 get :index
Chris@0 84 assert_template 'index'
Chris@0 85 assert_no_tag :a, :attributes => {:href => '/time_entries'}
Chris@0 86 end
Chris@0 87 end
Chris@0 88 end
Chris@0 89
chris@22 90 context "#new" do
Chris@0 91 context "by admin user" do
Chris@0 92 setup do
Chris@0 93 @request.session[:user_id] = 1
Chris@0 94 end
Chris@0 95
Chris@0 96 should "accept get" do
chris@22 97 get :new
Chris@0 98 assert_response :success
chris@22 99 assert_template 'new'
chris@22 100 end
chris@22 101
chris@22 102 end
chris@22 103
chris@22 104 context "by non-admin user with add_project permission" do
chris@22 105 setup do
chris@22 106 Role.non_member.add_permission! :add_project
chris@22 107 @request.session[:user_id] = 9
chris@22 108 end
chris@22 109
chris@22 110 should "accept get" do
chris@22 111 get :new
chris@22 112 assert_response :success
chris@22 113 assert_template 'new'
chris@22 114 assert_no_tag :select, :attributes => {:name => 'project[parent_id]'}
chris@22 115 end
chris@22 116 end
chris@22 117
chris@22 118 context "by non-admin user with add_subprojects permission" do
chris@22 119 setup do
chris@22 120 Role.find(1).remove_permission! :add_project
chris@22 121 Role.find(1).add_permission! :add_subprojects
chris@22 122 @request.session[:user_id] = 2
Chris@0 123 end
Chris@0 124
chris@22 125 should "accept get" do
chris@22 126 get :new, :parent_id => 'ecookbook'
chris@22 127 assert_response :success
chris@22 128 assert_template 'new'
chris@22 129 # parent project selected
chris@22 130 assert_tag :select, :attributes => {:name => 'project[parent_id]'},
chris@22 131 :child => {:tag => 'option', :attributes => {:value => '1', :selected => 'selected'}}
chris@22 132 # no empty value
chris@22 133 assert_no_tag :select, :attributes => {:name => 'project[parent_id]'},
chris@22 134 :child => {:tag => 'option', :attributes => {:value => ''}}
chris@22 135 end
chris@22 136 end
chris@22 137
chris@22 138 end
chris@22 139
chris@22 140 context "POST :create" do
chris@22 141 context "by admin user" do
chris@22 142 setup do
chris@22 143 @request.session[:user_id] = 1
chris@22 144 end
chris@22 145
chris@22 146 should "create a new project" do
chris@22 147 post :create, :project => { :name => "blog",
Chris@0 148 :description => "weblog",
Chris@0 149 :identifier => "blog",
Chris@0 150 :is_public => 1,
Chris@0 151 :custom_field_values => { '3' => 'Beta' }
Chris@0 152 }
Chris@0 153 assert_redirected_to '/projects/blog/settings'
Chris@0 154
Chris@0 155 project = Project.find_by_name('blog')
Chris@0 156 assert_kind_of Project, project
Chris@0 157 assert_equal 'weblog', project.description
Chris@0 158 assert_equal true, project.is_public?
Chris@0 159 assert_nil project.parent
Chris@0 160 end
Chris@0 161
chris@22 162 should "create a new subproject" do
chris@22 163 post :create, :project => { :name => "blog",
Chris@0 164 :description => "weblog",
Chris@0 165 :identifier => "blog",
Chris@0 166 :is_public => 1,
Chris@0 167 :custom_field_values => { '3' => 'Beta' },
Chris@0 168 :parent_id => 1
Chris@0 169 }
Chris@0 170 assert_redirected_to '/projects/blog/settings'
Chris@0 171
Chris@0 172 project = Project.find_by_name('blog')
Chris@0 173 assert_kind_of Project, project
Chris@0 174 assert_equal Project.find(1), project.parent
Chris@0 175 end
Chris@0 176 end
Chris@0 177
Chris@0 178 context "by non-admin user with add_project permission" do
Chris@0 179 setup do
Chris@0 180 Role.non_member.add_permission! :add_project
Chris@0 181 @request.session[:user_id] = 9
Chris@0 182 end
Chris@0 183
chris@22 184 should "accept create a Project" do
chris@22 185 post :create, :project => { :name => "blog",
Chris@0 186 :description => "weblog",
Chris@0 187 :identifier => "blog",
Chris@0 188 :is_public => 1,
Chris@0 189 :custom_field_values => { '3' => 'Beta' }
Chris@0 190 }
Chris@0 191
Chris@0 192 assert_redirected_to '/projects/blog/settings'
Chris@0 193
Chris@0 194 project = Project.find_by_name('blog')
Chris@0 195 assert_kind_of Project, project
Chris@0 196 assert_equal 'weblog', project.description
Chris@0 197 assert_equal true, project.is_public?
Chris@0 198
Chris@0 199 # User should be added as a project member
Chris@0 200 assert User.find(9).member_of?(project)
Chris@0 201 assert_equal 1, project.members.size
Chris@0 202 end
Chris@0 203
Chris@0 204 should "fail with parent_id" do
Chris@0 205 assert_no_difference 'Project.count' do
chris@22 206 post :create, :project => { :name => "blog",
Chris@0 207 :description => "weblog",
Chris@0 208 :identifier => "blog",
Chris@0 209 :is_public => 1,
Chris@0 210 :custom_field_values => { '3' => 'Beta' },
Chris@0 211 :parent_id => 1
Chris@0 212 }
Chris@0 213 end
Chris@0 214 assert_response :success
Chris@0 215 project = assigns(:project)
Chris@0 216 assert_kind_of Project, project
Chris@0 217 assert_not_nil project.errors.on(:parent_id)
Chris@0 218 end
Chris@0 219 end
Chris@0 220
Chris@0 221 context "by non-admin user with add_subprojects permission" do
Chris@0 222 setup do
Chris@0 223 Role.find(1).remove_permission! :add_project
Chris@0 224 Role.find(1).add_permission! :add_subprojects
Chris@0 225 @request.session[:user_id] = 2
Chris@0 226 end
Chris@0 227
chris@22 228 should "create a project with a parent_id" do
chris@22 229 post :create, :project => { :name => "blog",
Chris@0 230 :description => "weblog",
Chris@0 231 :identifier => "blog",
Chris@0 232 :is_public => 1,
Chris@0 233 :custom_field_values => { '3' => 'Beta' },
Chris@0 234 :parent_id => 1
Chris@0 235 }
Chris@0 236 assert_redirected_to '/projects/blog/settings'
Chris@0 237 project = Project.find_by_name('blog')
Chris@0 238 end
Chris@0 239
Chris@0 240 should "fail without parent_id" do
Chris@0 241 assert_no_difference 'Project.count' do
chris@22 242 post :create, :project => { :name => "blog",
Chris@0 243 :description => "weblog",
Chris@0 244 :identifier => "blog",
Chris@0 245 :is_public => 1,
Chris@0 246 :custom_field_values => { '3' => 'Beta' }
Chris@0 247 }
Chris@0 248 end
Chris@0 249 assert_response :success
Chris@0 250 project = assigns(:project)
Chris@0 251 assert_kind_of Project, project
Chris@0 252 assert_not_nil project.errors.on(:parent_id)
Chris@0 253 end
Chris@0 254
Chris@0 255 should "fail with unauthorized parent_id" do
Chris@0 256 assert !User.find(2).member_of?(Project.find(6))
Chris@0 257 assert_no_difference 'Project.count' do
chris@22 258 post :create, :project => { :name => "blog",
Chris@0 259 :description => "weblog",
Chris@0 260 :identifier => "blog",
Chris@0 261 :is_public => 1,
Chris@0 262 :custom_field_values => { '3' => 'Beta' },
Chris@0 263 :parent_id => 6
Chris@0 264 }
Chris@0 265 end
Chris@0 266 assert_response :success
Chris@0 267 project = assigns(:project)
Chris@0 268 assert_kind_of Project, project
Chris@0 269 assert_not_nil project.errors.on(:parent_id)
Chris@0 270 end
Chris@0 271 end
Chris@0 272 end
Chris@0 273
Chris@0 274 def test_show_by_id
Chris@0 275 get :show, :id => 1
Chris@0 276 assert_response :success
Chris@0 277 assert_template 'show'
Chris@0 278 assert_not_nil assigns(:project)
Chris@0 279 end
Chris@0 280
Chris@0 281 def test_show_by_identifier
Chris@0 282 get :show, :id => 'ecookbook'
Chris@0 283 assert_response :success
Chris@0 284 assert_template 'show'
Chris@0 285 assert_not_nil assigns(:project)
Chris@0 286 assert_equal Project.find_by_identifier('ecookbook'), assigns(:project)
chris@37 287
chris@37 288 assert_tag 'li', :content => /Development status/
chris@37 289 end
chris@37 290
chris@37 291 def test_show_should_not_display_hidden_custom_fields
chris@37 292 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
chris@37 293 get :show, :id => 'ecookbook'
chris@37 294 assert_response :success
chris@37 295 assert_template 'show'
chris@37 296 assert_not_nil assigns(:project)
chris@37 297
chris@37 298 assert_no_tag 'li', :content => /Development status/
Chris@0 299 end
Chris@0 300
Chris@0 301 def test_show_should_not_fail_when_custom_values_are_nil
Chris@0 302 project = Project.find_by_identifier('ecookbook')
Chris@0 303 project.custom_values.first.update_attribute(:value, nil)
Chris@0 304 get :show, :id => 'ecookbook'
Chris@0 305 assert_response :success
Chris@0 306 assert_template 'show'
Chris@0 307 assert_not_nil assigns(:project)
Chris@0 308 assert_equal Project.find_by_identifier('ecookbook'), assigns(:project)
Chris@0 309 end
Chris@0 310
chris@37 311 def show_archived_project_should_be_denied
chris@37 312 project = Project.find_by_identifier('ecookbook')
chris@37 313 project.archive!
chris@37 314
chris@37 315 get :show, :id => 'ecookbook'
chris@37 316 assert_response 403
chris@37 317 assert_nil assigns(:project)
chris@37 318 assert_tag :tag => 'p', :content => /archived/
chris@37 319 end
chris@37 320
Chris@0 321 def test_private_subprojects_hidden
Chris@0 322 get :show, :id => 'ecookbook'
Chris@0 323 assert_response :success
Chris@0 324 assert_template 'show'
Chris@0 325 assert_no_tag :tag => 'a', :content => /Private child/
Chris@0 326 end
Chris@0 327
Chris@0 328 def test_private_subprojects_visible
Chris@0 329 @request.session[:user_id] = 2 # manager who is a member of the private subproject
Chris@0 330 get :show, :id => 'ecookbook'
Chris@0 331 assert_response :success
Chris@0 332 assert_template 'show'
Chris@0 333 assert_tag :tag => 'a', :content => /Private child/
Chris@0 334 end
Chris@0 335
Chris@0 336 def test_settings
Chris@0 337 @request.session[:user_id] = 2 # manager
Chris@0 338 get :settings, :id => 1
Chris@0 339 assert_response :success
Chris@0 340 assert_template 'settings'
Chris@0 341 end
Chris@0 342
chris@22 343 def test_update
Chris@0 344 @request.session[:user_id] = 2 # manager
chris@22 345 post :update, :id => 1, :project => {:name => 'Test changed name',
Chris@0 346 :issue_custom_field_ids => ['']}
chris@37 347 assert_redirected_to '/projects/ecookbook/settings'
Chris@0 348 project = Project.find(1)
Chris@0 349 assert_equal 'Test changed name', project.name
Chris@0 350 end
Chris@0 351
Chris@0 352 def test_get_destroy
Chris@0 353 @request.session[:user_id] = 1 # admin
Chris@0 354 get :destroy, :id => 1
Chris@0 355 assert_response :success
Chris@0 356 assert_template 'destroy'
Chris@0 357 assert_not_nil Project.find_by_id(1)
Chris@0 358 end
Chris@0 359
Chris@0 360 def test_post_destroy
Chris@0 361 @request.session[:user_id] = 1 # admin
Chris@0 362 post :destroy, :id => 1, :confirm => 1
chris@37 363 assert_redirected_to '/admin/projects'
Chris@0 364 assert_nil Project.find_by_id(1)
Chris@0 365 end
Chris@0 366
Chris@0 367 def test_archive
Chris@0 368 @request.session[:user_id] = 1 # admin
Chris@0 369 post :archive, :id => 1
chris@37 370 assert_redirected_to '/admin/projects'
Chris@0 371 assert !Project.find(1).active?
Chris@0 372 end
Chris@0 373
Chris@0 374 def test_unarchive
Chris@0 375 @request.session[:user_id] = 1 # admin
Chris@0 376 Project.find(1).archive
Chris@0 377 post :unarchive, :id => 1
chris@37 378 assert_redirected_to '/admin/projects'
Chris@0 379 assert Project.find(1).active?
Chris@0 380 end
Chris@0 381
Chris@0 382 def test_project_breadcrumbs_should_be_limited_to_3_ancestors
Chris@0 383 CustomField.delete_all
Chris@0 384 parent = nil
Chris@0 385 6.times do |i|
Chris@0 386 p = Project.create!(:name => "Breadcrumbs #{i}", :identifier => "breadcrumbs-#{i}")
Chris@0 387 p.set_parent!(parent)
Chris@0 388 get :show, :id => p
Chris@0 389 assert_tag :h1, :parent => { :attributes => {:id => 'header'}},
Chris@0 390 :children => { :count => [i, 3].min,
Chris@0 391 :only => { :tag => 'a' } }
Chris@0 392
Chris@0 393 parent = p
Chris@0 394 end
Chris@0 395 end
Chris@0 396
Chris@0 397 def test_copy_with_project
Chris@0 398 @request.session[:user_id] = 1 # admin
Chris@0 399 get :copy, :id => 1
Chris@0 400 assert_response :success
Chris@0 401 assert_template 'copy'
Chris@0 402 assert assigns(:project)
Chris@0 403 assert_equal Project.find(1).description, assigns(:project).description
Chris@0 404 assert_nil assigns(:project).id
Chris@0 405 end
Chris@0 406
Chris@0 407 def test_copy_without_project
Chris@0 408 @request.session[:user_id] = 1 # admin
Chris@0 409 get :copy
Chris@0 410 assert_response :redirect
Chris@0 411 assert_redirected_to :controller => 'admin', :action => 'projects'
Chris@0 412 end
Chris@0 413
chris@37 414 context "POST :copy" do
chris@37 415 should "TODO: test the rest of the method"
chris@37 416
chris@37 417 should "redirect to the project settings when successful" do
chris@37 418 @request.session[:user_id] = 1 # admin
chris@37 419 post :copy, :id => 1, :project => {:name => 'Copy', :identifier => 'unique-copy'}
chris@37 420 assert_response :redirect
chris@37 421 assert_redirected_to :controller => 'projects', :action => 'settings'
chris@37 422 end
chris@37 423 end
chris@37 424
Chris@0 425 def test_jump_should_redirect_to_active_tab
Chris@0 426 get :show, :id => 1, :jump => 'issues'
chris@37 427 assert_redirected_to '/projects/ecookbook/issues'
Chris@0 428 end
Chris@0 429
Chris@0 430 def test_jump_should_not_redirect_to_inactive_tab
Chris@0 431 get :show, :id => 3, :jump => 'documents'
Chris@0 432 assert_response :success
Chris@0 433 assert_template 'show'
Chris@0 434 end
Chris@0 435
Chris@0 436 def test_jump_should_not_redirect_to_unknown_tab
Chris@0 437 get :show, :id => 3, :jump => 'foobar'
Chris@0 438 assert_response :success
Chris@0 439 assert_template 'show'
Chris@0 440 end
Chris@0 441
Chris@0 442 # A hook that is manually registered later
Chris@0 443 class ProjectBasedTemplate < Redmine::Hook::ViewListener
Chris@0 444 def view_layouts_base_html_head(context)
Chris@0 445 # Adds a project stylesheet
Chris@0 446 stylesheet_link_tag(context[:project].identifier) if context[:project]
Chris@0 447 end
Chris@0 448 end
Chris@0 449 # Don't use this hook now
Chris@0 450 Redmine::Hook.clear_listeners
Chris@0 451
Chris@0 452 def test_hook_response
Chris@0 453 Redmine::Hook.add_listener(ProjectBasedTemplate)
Chris@0 454 get :show, :id => 1
Chris@0 455 assert_tag :tag => 'link', :attributes => {:href => '/stylesheets/ecookbook.css'},
Chris@0 456 :parent => {:tag => 'head'}
Chris@0 457
Chris@0 458 Redmine::Hook.clear_listeners
Chris@0 459 end
Chris@0 460 end