From 58c643b420d919ffae07e72dc6f8f11c6de58aa9 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Mon, 18 Feb 2013 17:29:31 +0000 Subject: [PATCH] Code cleanup. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11438 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- test/functional/projects_controller_test.rb | 485 ++++++++++---------- 1 file changed, 239 insertions(+), 246 deletions(-) diff --git a/test/functional/projects_controller_test.rb b/test/functional/projects_controller_test.rb index a93211caa..29de3df3c 100644 --- a/test/functional/projects_controller_test.rb +++ b/test/functional/projects_controller_test.rb @@ -27,22 +27,23 @@ class ProjectsControllerTest < ActionController::TestCase Setting.default_language = 'en' end - def test_index + def test_index_by_anonymous_should_not_show_private_projects get :index assert_response :success assert_template 'index' - assert_not_nil assigns(:projects) - - assert_tag :ul, :child => {:tag => 'li', - :descendant => {:tag => 'a', :content => 'eCookbook'}, - :child => { :tag => 'ul', - :descendant => { :tag => 'a', - :content => 'Child of private child' - } - } - } - - assert_no_tag :a, :content => /Private child of eCookbook/ + projects = assigns(:projects) + assert_not_nil projects + assert projects.all?(&:is_public?) + + assert_select 'ul' do + assert_select 'li' do + assert_select 'a', :text => 'eCookbook' + assert_select 'ul' do + assert_select 'a', :text => 'Child of private child' + end + end + end + assert_select 'a', :text => /Private child of eCookbook/, :count => 0 end def test_index_atom @@ -53,239 +54,218 @@ class ProjectsControllerTest < ActionController::TestCase assert_select 'feed>entry', :count => Project.count(:conditions => Project.visible_condition(User.current)) end - context "#index" do - context "by non-admin user with view_time_entries permission" do - setup do - @request.session[:user_id] = 3 - end - should "show overall spent time link" do - get :index - assert_template 'index' - assert_tag :a, :attributes => {:href => '/time_entries'} - end - end + test "#index by non-admin user with view_time_entries permission should show overall spent time link" do + @request.session[:user_id] = 3 + get :index + assert_template 'index' + assert_select 'a[href=?]', '/time_entries' + end - context "by non-admin user without view_time_entries permission" do - setup do - Role.find(2).remove_permission! :view_time_entries - Role.non_member.remove_permission! :view_time_entries - Role.anonymous.remove_permission! :view_time_entries - @request.session[:user_id] = 3 - end - should "not show overall spent time link" do - get :index - assert_template 'index' - assert_no_tag :a, :attributes => {:href => '/time_entries'} - end - end + test "#index by non-admin user without view_time_entries permission should not show overall spent time link" do + Role.find(2).remove_permission! :view_time_entries + Role.non_member.remove_permission! :view_time_entries + Role.anonymous.remove_permission! :view_time_entries + @request.session[:user_id] = 3 + + get :index + assert_template 'index' + assert_select 'a[href=?]', '/time_entries', 0 end - context "#new" do - context "by admin user" do - setup do - @request.session[:user_id] = 1 - end + test "#new by admin user should accept get" do + @request.session[:user_id] = 1 - should "accept get" do - get :new - assert_response :success - assert_template 'new' - end + get :new + assert_response :success + assert_template 'new' + end - end + test "#new by non-admin user with add_project permission should accept get" do + Role.non_member.add_permission! :add_project + @request.session[:user_id] = 9 - context "by non-admin user with add_project permission" do - setup do - Role.non_member.add_permission! :add_project - @request.session[:user_id] = 9 - end + get :new + assert_response :success + assert_template 'new' + assert_select 'select[name=?]', 'project[parent_id]', 0 + end - should "accept get" do - get :new - assert_response :success - assert_template 'new' - assert_no_tag :select, :attributes => {:name => 'project[parent_id]'} - end - end + test "#new by non-admin user with add_subprojects permission should accept get" do + Role.find(1).remove_permission! :add_project + Role.find(1).add_permission! :add_subprojects + @request.session[:user_id] = 2 - context "by non-admin user with add_subprojects permission" do - setup do - Role.find(1).remove_permission! :add_project - Role.find(1).add_permission! :add_subprojects - @request.session[:user_id] = 2 - end + get :new, :parent_id => 'ecookbook' + assert_response :success + assert_template 'new' - should "accept get" do - get :new, :parent_id => 'ecookbook' - assert_response :success - assert_template 'new' - # parent project selected - assert_tag :select, :attributes => {:name => 'project[parent_id]'}, - :child => {:tag => 'option', :attributes => {:value => '1', :selected => 'selected'}} - # no empty value - assert_no_tag :select, :attributes => {:name => 'project[parent_id]'}, - :child => {:tag => 'option', :attributes => {:value => ''}} - end + assert_select 'select[name=?]', 'project[parent_id]' do + # parent project selected + assert_select 'option[value=1][selected=selected]' + # no empty value + assert_select 'option[value=]', 0 end - end - context "POST :create" do - context "by admin user" do - setup do - @request.session[:user_id] = 1 - end - - should "create a new project" do - post :create, - :project => { - :name => "blog", - :description => "weblog", - :homepage => 'http://weblog', - :identifier => "blog", - :is_public => 1, - :custom_field_values => { '3' => 'Beta' }, - :tracker_ids => ['1', '3'], - # an issue custom field that is not for all project - :issue_custom_field_ids => ['9'], - :enabled_module_names => ['issue_tracking', 'news', 'repository'] - } - assert_redirected_to '/projects/blog/settings' - - project = Project.find_by_name('blog') - assert_kind_of Project, project - assert project.active? - assert_equal 'weblog', project.description - assert_equal 'http://weblog', project.homepage - assert_equal true, project.is_public? - assert_nil project.parent - assert_equal 'Beta', project.custom_value_for(3).value - assert_equal [1, 3], project.trackers.map(&:id).sort - assert_equal ['issue_tracking', 'news', 'repository'], project.enabled_module_names.sort - assert project.issue_custom_fields.include?(IssueCustomField.find(9)) - end + test "#create by admin user should create a new project" do + @request.session[:user_id] = 1 - should "create a new subproject" do - post :create, :project => { :name => "blog", - :description => "weblog", - :identifier => "blog", - :is_public => 1, - :custom_field_values => { '3' => 'Beta' }, - :parent_id => 1 - } - assert_redirected_to '/projects/blog/settings' - - project = Project.find_by_name('blog') - assert_kind_of Project, project - assert_equal Project.find(1), project.parent - end + post :create, + :project => { + :name => "blog", + :description => "weblog", + :homepage => 'http://weblog', + :identifier => "blog", + :is_public => 1, + :custom_field_values => { '3' => 'Beta' }, + :tracker_ids => ['1', '3'], + # an issue custom field that is not for all project + :issue_custom_field_ids => ['9'], + :enabled_module_names => ['issue_tracking', 'news', 'repository'] + } + assert_redirected_to '/projects/blog/settings' + + project = Project.find_by_name('blog') + assert_kind_of Project, project + assert project.active? + assert_equal 'weblog', project.description + assert_equal 'http://weblog', project.homepage + assert_equal true, project.is_public? + assert_nil project.parent + assert_equal 'Beta', project.custom_value_for(3).value + assert_equal [1, 3], project.trackers.map(&:id).sort + assert_equal ['issue_tracking', 'news', 'repository'], project.enabled_module_names.sort + assert project.issue_custom_fields.include?(IssueCustomField.find(9)) + end + + test "#create by admin user should create a new subproject" do + @request.session[:user_id] = 1 - should "continue" do - assert_difference 'Project.count' do - post :create, :project => {:name => "blog", :identifier => "blog"}, :continue => 'Create and continue' - end - assert_redirected_to '/projects/new' - end + assert_difference 'Project.count' do + post :create, :project => { :name => "blog", + :description => "weblog", + :identifier => "blog", + :is_public => 1, + :custom_field_values => { '3' => 'Beta' }, + :parent_id => 1 + } + assert_redirected_to '/projects/blog/settings' end - context "by non-admin user with add_project permission" do - setup do - Role.non_member.add_permission! :add_project - @request.session[:user_id] = 9 - end + project = Project.find_by_name('blog') + assert_kind_of Project, project + assert_equal Project.find(1), project.parent + end - should "accept create a Project" do - post :create, :project => { :name => "blog", - :description => "weblog", - :identifier => "blog", - :is_public => 1, - :custom_field_values => { '3' => 'Beta' }, - :tracker_ids => ['1', '3'], - :enabled_module_names => ['issue_tracking', 'news', 'repository'] - } - - assert_redirected_to '/projects/blog/settings' - - project = Project.find_by_name('blog') - assert_kind_of Project, project - assert_equal 'weblog', project.description - assert_equal true, project.is_public? - assert_equal [1, 3], project.trackers.map(&:id).sort - assert_equal ['issue_tracking', 'news', 'repository'], project.enabled_module_names.sort - - # User should be added as a project member - assert User.find(9).member_of?(project) - assert_equal 1, project.members.size - end + test "#create by admin user should continue" do + @request.session[:user_id] = 1 - should "fail with parent_id" do - assert_no_difference 'Project.count' do - post :create, :project => { :name => "blog", - :description => "weblog", - :identifier => "blog", - :is_public => 1, - :custom_field_values => { '3' => 'Beta' }, - :parent_id => 1 - } - end - assert_response :success - project = assigns(:project) - assert_kind_of Project, project - assert_not_nil project.errors[:parent_id] - end + assert_difference 'Project.count' do + post :create, :project => {:name => "blog", :identifier => "blog"}, :continue => 'Create and continue' + end + assert_redirected_to '/projects/new' + end + + test "#create by non-admin user with add_project permission should create a new project" do + Role.non_member.add_permission! :add_project + @request.session[:user_id] = 9 + + post :create, :project => { :name => "blog", + :description => "weblog", + :identifier => "blog", + :is_public => 1, + :custom_field_values => { '3' => 'Beta' }, + :tracker_ids => ['1', '3'], + :enabled_module_names => ['issue_tracking', 'news', 'repository'] + } + + assert_redirected_to '/projects/blog/settings' + + project = Project.find_by_name('blog') + assert_kind_of Project, project + assert_equal 'weblog', project.description + assert_equal true, project.is_public? + assert_equal [1, 3], project.trackers.map(&:id).sort + assert_equal ['issue_tracking', 'news', 'repository'], project.enabled_module_names.sort + + # User should be added as a project member + assert User.find(9).member_of?(project) + assert_equal 1, project.members.size + end + + test "#create by non-admin user with add_project permission should fail with parent_id" do + Role.non_member.add_permission! :add_project + @request.session[:user_id] = 9 + + assert_no_difference 'Project.count' do + post :create, :project => { :name => "blog", + :description => "weblog", + :identifier => "blog", + :is_public => 1, + :custom_field_values => { '3' => 'Beta' }, + :parent_id => 1 + } end + assert_response :success + project = assigns(:project) + assert_kind_of Project, project + assert_not_nil project.errors[:parent_id] + end - context "by non-admin user with add_subprojects permission" do - setup do - Role.find(1).remove_permission! :add_project - Role.find(1).add_permission! :add_subprojects - @request.session[:user_id] = 2 - end + test "#create by non-admin user with add_subprojects permission should create a project with a parent_id" do + Role.find(1).remove_permission! :add_project + Role.find(1).add_permission! :add_subprojects + @request.session[:user_id] = 2 - should "create a project with a parent_id" do - post :create, :project => { :name => "blog", - :description => "weblog", - :identifier => "blog", - :is_public => 1, - :custom_field_values => { '3' => 'Beta' }, - :parent_id => 1 - } - assert_redirected_to '/projects/blog/settings' - project = Project.find_by_name('blog') - end + post :create, :project => { :name => "blog", + :description => "weblog", + :identifier => "blog", + :is_public => 1, + :custom_field_values => { '3' => 'Beta' }, + :parent_id => 1 + } + assert_redirected_to '/projects/blog/settings' + project = Project.find_by_name('blog') + end - should "fail without parent_id" do - assert_no_difference 'Project.count' do - post :create, :project => { :name => "blog", - :description => "weblog", - :identifier => "blog", - :is_public => 1, - :custom_field_values => { '3' => 'Beta' } - } - end - assert_response :success - project = assigns(:project) - assert_kind_of Project, project - assert_not_nil project.errors[:parent_id] - end + test "#create by non-admin user with add_subprojects permission should fail without parent_id" do + Role.find(1).remove_permission! :add_project + Role.find(1).add_permission! :add_subprojects + @request.session[:user_id] = 2 - should "fail with unauthorized parent_id" do - assert !User.find(2).member_of?(Project.find(6)) - assert_no_difference 'Project.count' do - post :create, :project => { :name => "blog", - :description => "weblog", - :identifier => "blog", - :is_public => 1, - :custom_field_values => { '3' => 'Beta' }, - :parent_id => 6 - } - end - assert_response :success - project = assigns(:project) - assert_kind_of Project, project - assert_not_nil project.errors[:parent_id] - end + assert_no_difference 'Project.count' do + post :create, :project => { :name => "blog", + :description => "weblog", + :identifier => "blog", + :is_public => 1, + :custom_field_values => { '3' => 'Beta' } + } end + assert_response :success + project = assigns(:project) + assert_kind_of Project, project + assert_not_nil project.errors[:parent_id] + end + + test "#create by non-admin user with add_subprojects permission should fail with unauthorized parent_id" do + Role.find(1).remove_permission! :add_project + Role.find(1).add_permission! :add_subprojects + @request.session[:user_id] = 2 + + assert !User.find(2).member_of?(Project.find(6)) + assert_no_difference 'Project.count' do + post :create, :project => { :name => "blog", + :description => "weblog", + :identifier => "blog", + :is_public => 1, + :custom_field_values => { '3' => 'Beta' }, + :parent_id => 6 + } + end + assert_response :success + project = assigns(:project) + assert_kind_of Project, project + assert_not_nil project.errors[:parent_id] end def test_create_subproject_with_inherit_members_should_inherit_members @@ -337,7 +317,7 @@ class ProjectsControllerTest < ActionController::TestCase assert_not_nil assigns(:project) assert_equal Project.find_by_identifier('ecookbook'), assigns(:project) - assert_tag 'li', :content => /Development status/ + assert_select 'li', :text => /Development status/ end def test_show_should_not_display_hidden_custom_fields @@ -347,7 +327,7 @@ class ProjectsControllerTest < ActionController::TestCase assert_template 'show' assert_not_nil assigns(:project) - assert_no_tag 'li', :content => /Development status/ + assert_select 'li', :text => /Development status/, :count => 0 end def test_show_should_not_fail_when_custom_values_are_nil @@ -367,22 +347,22 @@ class ProjectsControllerTest < ActionController::TestCase get :show, :id => 'ecookbook' assert_response 403 assert_nil assigns(:project) - assert_tag :tag => 'p', :content => /archived/ + assert_select 'p', :text => /archived/ end - def test_private_subprojects_hidden + def test_show_should_not_show_private_subprojects_that_are_not_visible get :show, :id => 'ecookbook' assert_response :success assert_template 'show' - assert_no_tag :tag => 'a', :content => /Private child/ + assert_select 'a', :text => /Private child/, :count => 0 end - def test_private_subprojects_visible + def test_show_should_show_private_subprojects_that_are_visible @request.session[:user_id] = 2 # manager who is a member of the private subproject get :show, :id => 'ecookbook' assert_response :success assert_template 'show' - assert_tag :tag => 'a', :content => /Private child/ + assert_select 'a', :text => /Private child/ end def test_settings @@ -459,22 +439,37 @@ class ProjectsControllerTest < ActionController::TestCase assert_equal ['documents', 'issue_tracking', 'repository'], Project.find(1).enabled_module_names.sort end - def test_destroy_without_confirmation + def test_destroy_leaf_project_without_confirmation_should_show_confirmation @request.session[:user_id] = 1 # admin - delete :destroy, :id => 1 - assert_response :success - assert_template 'destroy' - assert_not_nil Project.find_by_id(1) - assert_tag :tag => 'strong', - :content => ['Private child of eCookbook', + + assert_no_difference 'Project.count' do + delete :destroy, :id => 2 + assert_response :success + assert_template 'destroy' + end + end + + def test_destroy_without_confirmation_should_show_confirmation_with_subprojects + @request.session[:user_id] = 1 # admin + + assert_no_difference 'Project.count' do + delete :destroy, :id => 1 + assert_response :success + assert_template 'destroy' + end + assert_select 'strong', + :text => ['Private child of eCookbook', 'Child of private child, eCookbook Subproject 1', 'eCookbook Subproject 2'].join(', ') end - def test_destroy + def test_destroy_with_confirmation_should_destroy_the_project_and_subprojects @request.session[:user_id] = 1 # admin - delete :destroy, :id => 1, :confirm => 1 - assert_redirected_to '/admin/projects' + + assert_difference 'Project.count', -5 do + delete :destroy, :id => 1, :confirm => 1 + assert_redirected_to '/admin/projects' + end assert_nil Project.find_by_id(1) end @@ -520,12 +515,11 @@ class ProjectsControllerTest < ActionController::TestCase CustomField.delete_all parent = nil 6.times do |i| - p = Project.create!(:name => "Breadcrumbs #{i}", :identifier => "breadcrumbs-#{i}") - p.set_parent!(parent) + p = Project.generate_with_parent!(parent) get :show, :id => p - assert_tag :h1, :parent => { :attributes => {:id => 'header'}}, - :children => { :count => [i, 3].min, - :only => { :tag => 'a' } } + assert_select '#header h1' do + assert_select 'a', :count => [i, 3].min + end parent = p end @@ -540,8 +534,7 @@ class ProjectsControllerTest < ActionController::TestCase assert_equal Project.find(1).description, assigns(:project).description assert_nil assigns(:project).id - assert_tag :tag => 'input', - :attributes => {:name => 'project[enabled_module_names][]', :value => 'issue_tracking'} + assert_select 'input[name=?][value=?]', 'project[enabled_module_names][]', 'issue_tracking', 1 end def test_get_copy_with_invalid_source_should_respond_with_404 -- 2.39.5