Chris@0: # Redmine - project management software Chris@0: # Copyright (C) 2006-2008 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@117: require File.expand_path('../../test_helper', __FILE__) Chris@0: require 'projects_controller' Chris@0: Chris@0: # Re-raise errors caught by the controller. Chris@0: class ProjectsController; def rescue_action(e) raise e end; end Chris@0: Chris@0: class ProjectsControllerTest < ActionController::TestCase Chris@0: fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details, Chris@0: :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages, Chris@0: :attachments, :custom_fields, :custom_values, :time_entries Chris@0: Chris@0: def setup Chris@0: @controller = ProjectsController.new Chris@0: @request = ActionController::TestRequest.new Chris@0: @response = ActionController::TestResponse.new Chris@0: @request.session[:user_id] = nil Chris@0: Setting.default_language = 'en' Chris@0: end Chris@0: Chris@0: def test_index Chris@0: get :index Chris@0: assert_response :success Chris@0: assert_template 'index' Chris@0: assert_not_nil assigns(:projects) Chris@0: Chris@0: assert_tag :ul, :child => {:tag => 'li', Chris@0: :descendant => {:tag => 'a', :content => 'eCookbook'}, Chris@0: :child => { :tag => 'ul', Chris@0: :descendant => { :tag => 'a', Chris@0: :content => 'Child of private child' Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: assert_no_tag :a, :content => /Private child of eCookbook/ Chris@0: end Chris@0: Chris@0: def test_index_atom Chris@0: get :index, :format => 'atom' Chris@0: assert_response :success Chris@0: assert_template 'common/feed.atom.rxml' Chris@0: assert_select 'feed>title', :text => 'Redmine: Latest projects' Chris@0: assert_select 'feed>entry', :count => Project.count(:conditions => Project.visible_by(User.current)) Chris@0: end Chris@0: Chris@0: context "#index" do Chris@0: context "by non-admin user with view_time_entries permission" do Chris@0: setup do Chris@0: @request.session[:user_id] = 3 Chris@0: end Chris@0: should "show overall spent time link" do Chris@0: get :index Chris@0: assert_template 'index' Chris@0: assert_tag :a, :attributes => {:href => '/time_entries'} Chris@0: end Chris@0: end Chris@0: Chris@0: context "by non-admin user without view_time_entries permission" do Chris@0: setup do Chris@0: Role.find(2).remove_permission! :view_time_entries Chris@0: Role.non_member.remove_permission! :view_time_entries Chris@0: Role.anonymous.remove_permission! :view_time_entries Chris@0: @request.session[:user_id] = 3 Chris@0: end Chris@0: should "not show overall spent time link" do Chris@0: get :index Chris@0: assert_template 'index' Chris@0: assert_no_tag :a, :attributes => {:href => '/time_entries'} Chris@0: end Chris@0: end Chris@0: end Chris@0: chris@22: context "#new" do Chris@0: context "by admin user" do Chris@0: setup do Chris@0: @request.session[:user_id] = 1 Chris@0: end Chris@0: Chris@0: should "accept get" do chris@22: get :new Chris@0: assert_response :success chris@22: assert_template 'new' chris@22: end chris@22: chris@22: end chris@22: chris@22: context "by non-admin user with add_project permission" do chris@22: setup do chris@22: Role.non_member.add_permission! :add_project chris@22: @request.session[:user_id] = 9 chris@22: end chris@22: chris@22: should "accept get" do chris@22: get :new chris@22: assert_response :success chris@22: assert_template 'new' chris@22: assert_no_tag :select, :attributes => {:name => 'project[parent_id]'} chris@22: end chris@22: end chris@22: chris@22: context "by non-admin user with add_subprojects permission" do chris@22: setup do chris@22: Role.find(1).remove_permission! :add_project chris@22: Role.find(1).add_permission! :add_subprojects chris@22: @request.session[:user_id] = 2 Chris@0: end Chris@0: chris@22: should "accept get" do chris@22: get :new, :parent_id => 'ecookbook' chris@22: assert_response :success chris@22: assert_template 'new' chris@22: # parent project selected chris@22: assert_tag :select, :attributes => {:name => 'project[parent_id]'}, chris@22: :child => {:tag => 'option', :attributes => {:value => '1', :selected => 'selected'}} chris@22: # no empty value chris@22: assert_no_tag :select, :attributes => {:name => 'project[parent_id]'}, chris@22: :child => {:tag => 'option', :attributes => {:value => ''}} chris@22: end chris@22: end chris@22: chris@22: end chris@22: chris@22: context "POST :create" do chris@22: context "by admin user" do chris@22: setup do chris@22: @request.session[:user_id] = 1 chris@22: end chris@22: chris@22: should "create a new project" do Chris@117: post :create, Chris@117: :project => { Chris@117: :name => "blog", Chris@117: :description => "weblog", Chris@117: :homepage => 'http://weblog', Chris@117: :identifier => "blog", Chris@117: :is_public => 1, Chris@117: :custom_field_values => { '3' => 'Beta' }, Chris@117: :tracker_ids => ['1', '3'], Chris@117: # an issue custom field that is not for all project Chris@117: :issue_custom_field_ids => ['9'], Chris@117: :enabled_module_names => ['issue_tracking', 'news', 'repository'] Chris@117: } Chris@0: assert_redirected_to '/projects/blog/settings' Chris@0: Chris@0: project = Project.find_by_name('blog') Chris@0: assert_kind_of Project, project Chris@117: assert project.active? Chris@0: assert_equal 'weblog', project.description Chris@117: assert_equal 'http://weblog', project.homepage Chris@0: assert_equal true, project.is_public? Chris@0: assert_nil project.parent Chris@117: assert_equal 'Beta', project.custom_value_for(3).value Chris@117: assert_equal [1, 3], project.trackers.map(&:id).sort Chris@117: assert_equal ['issue_tracking', 'news', 'repository'], project.enabled_module_names.sort Chris@117: assert project.issue_custom_fields.include?(IssueCustomField.find(9)) Chris@0: end Chris@0: chris@22: should "create a new subproject" do chris@22: post :create, :project => { :name => "blog", Chris@0: :description => "weblog", Chris@0: :identifier => "blog", Chris@0: :is_public => 1, Chris@0: :custom_field_values => { '3' => 'Beta' }, Chris@0: :parent_id => 1 Chris@0: } Chris@0: assert_redirected_to '/projects/blog/settings' Chris@0: Chris@0: project = Project.find_by_name('blog') Chris@0: assert_kind_of Project, project Chris@0: assert_equal Project.find(1), project.parent Chris@0: end Chris@0: end Chris@0: Chris@0: context "by non-admin user with add_project permission" do Chris@0: setup do Chris@0: Role.non_member.add_permission! :add_project Chris@0: @request.session[:user_id] = 9 Chris@0: end Chris@0: chris@22: should "accept create a Project" do chris@22: post :create, :project => { :name => "blog", Chris@0: :description => "weblog", Chris@0: :identifier => "blog", Chris@0: :is_public => 1, Chris@117: :custom_field_values => { '3' => 'Beta' }, Chris@117: :tracker_ids => ['1', '3'], Chris@117: :enabled_module_names => ['issue_tracking', 'news', 'repository'] Chris@0: } Chris@0: Chris@0: assert_redirected_to '/projects/blog/settings' Chris@0: Chris@0: project = Project.find_by_name('blog') Chris@0: assert_kind_of Project, project Chris@0: assert_equal 'weblog', project.description Chris@0: assert_equal true, project.is_public? Chris@117: assert_equal [1, 3], project.trackers.map(&:id).sort Chris@117: assert_equal ['issue_tracking', 'news', 'repository'], project.enabled_module_names.sort Chris@0: Chris@0: # User should be added as a project member Chris@0: assert User.find(9).member_of?(project) Chris@0: assert_equal 1, project.members.size Chris@0: end Chris@0: Chris@0: should "fail with parent_id" do Chris@0: assert_no_difference 'Project.count' do chris@22: post :create, :project => { :name => "blog", Chris@0: :description => "weblog", Chris@0: :identifier => "blog", Chris@0: :is_public => 1, Chris@0: :custom_field_values => { '3' => 'Beta' }, Chris@0: :parent_id => 1 Chris@0: } Chris@0: end Chris@0: assert_response :success Chris@0: project = assigns(:project) Chris@0: assert_kind_of Project, project Chris@0: assert_not_nil project.errors.on(:parent_id) Chris@0: end Chris@0: end Chris@0: Chris@0: context "by non-admin user with add_subprojects permission" do Chris@0: setup do Chris@0: Role.find(1).remove_permission! :add_project Chris@0: Role.find(1).add_permission! :add_subprojects Chris@0: @request.session[:user_id] = 2 Chris@0: end Chris@0: chris@22: should "create a project with a parent_id" do chris@22: post :create, :project => { :name => "blog", Chris@0: :description => "weblog", Chris@0: :identifier => "blog", Chris@0: :is_public => 1, Chris@0: :custom_field_values => { '3' => 'Beta' }, Chris@0: :parent_id => 1 Chris@0: } Chris@0: assert_redirected_to '/projects/blog/settings' Chris@0: project = Project.find_by_name('blog') Chris@0: end Chris@0: Chris@0: should "fail without parent_id" do Chris@0: assert_no_difference 'Project.count' do chris@22: post :create, :project => { :name => "blog", Chris@0: :description => "weblog", Chris@0: :identifier => "blog", Chris@0: :is_public => 1, Chris@0: :custom_field_values => { '3' => 'Beta' } Chris@0: } Chris@0: end Chris@0: assert_response :success Chris@0: project = assigns(:project) Chris@0: assert_kind_of Project, project Chris@0: assert_not_nil project.errors.on(:parent_id) Chris@0: end Chris@0: Chris@0: should "fail with unauthorized parent_id" do Chris@0: assert !User.find(2).member_of?(Project.find(6)) Chris@0: assert_no_difference 'Project.count' do chris@22: post :create, :project => { :name => "blog", Chris@0: :description => "weblog", Chris@0: :identifier => "blog", Chris@0: :is_public => 1, Chris@0: :custom_field_values => { '3' => 'Beta' }, Chris@0: :parent_id => 6 Chris@0: } Chris@0: end Chris@0: assert_response :success Chris@0: project = assigns(:project) Chris@0: assert_kind_of Project, project Chris@0: assert_not_nil project.errors.on(:parent_id) Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@117: def test_create_should_not_accept_get Chris@117: @request.session[:user_id] = 1 Chris@117: get :create Chris@117: assert_response :method_not_allowed Chris@117: end Chris@117: Chris@0: def test_show_by_id Chris@0: get :show, :id => 1 Chris@0: assert_response :success Chris@0: assert_template 'show' Chris@0: assert_not_nil assigns(:project) Chris@0: end Chris@0: Chris@0: def test_show_by_identifier Chris@0: get :show, :id => 'ecookbook' Chris@0: assert_response :success Chris@0: assert_template 'show' Chris@0: assert_not_nil assigns(:project) Chris@0: assert_equal Project.find_by_identifier('ecookbook'), assigns(:project) chris@37: chris@37: assert_tag 'li', :content => /Development status/ chris@37: end chris@37: chris@37: def test_show_should_not_display_hidden_custom_fields chris@37: ProjectCustomField.find_by_name('Development status').update_attribute :visible, false chris@37: get :show, :id => 'ecookbook' chris@37: assert_response :success chris@37: assert_template 'show' chris@37: assert_not_nil assigns(:project) chris@37: chris@37: assert_no_tag 'li', :content => /Development status/ Chris@0: end Chris@0: Chris@0: def test_show_should_not_fail_when_custom_values_are_nil Chris@0: project = Project.find_by_identifier('ecookbook') Chris@0: project.custom_values.first.update_attribute(:value, nil) Chris@0: get :show, :id => 'ecookbook' Chris@0: assert_response :success Chris@0: assert_template 'show' Chris@0: assert_not_nil assigns(:project) Chris@0: assert_equal Project.find_by_identifier('ecookbook'), assigns(:project) Chris@0: end Chris@0: chris@37: def show_archived_project_should_be_denied chris@37: project = Project.find_by_identifier('ecookbook') chris@37: project.archive! chris@37: chris@37: get :show, :id => 'ecookbook' chris@37: assert_response 403 chris@37: assert_nil assigns(:project) chris@37: assert_tag :tag => 'p', :content => /archived/ chris@37: end chris@37: Chris@0: def test_private_subprojects_hidden Chris@0: get :show, :id => 'ecookbook' Chris@0: assert_response :success Chris@0: assert_template 'show' Chris@0: assert_no_tag :tag => 'a', :content => /Private child/ Chris@0: end Chris@0: Chris@0: def test_private_subprojects_visible Chris@0: @request.session[:user_id] = 2 # manager who is a member of the private subproject Chris@0: get :show, :id => 'ecookbook' Chris@0: assert_response :success Chris@0: assert_template 'show' Chris@0: assert_tag :tag => 'a', :content => /Private child/ Chris@0: end Chris@0: Chris@0: def test_settings Chris@0: @request.session[:user_id] = 2 # manager Chris@0: get :settings, :id => 1 Chris@0: assert_response :success Chris@0: assert_template 'settings' Chris@0: end Chris@0: chris@22: def test_update Chris@0: @request.session[:user_id] = 2 # manager chris@22: post :update, :id => 1, :project => {:name => 'Test changed name', Chris@0: :issue_custom_field_ids => ['']} chris@37: assert_redirected_to '/projects/ecookbook/settings' Chris@0: project = Project.find(1) Chris@0: assert_equal 'Test changed name', project.name Chris@0: end Chris@117: Chris@117: def test_modules Chris@117: @request.session[:user_id] = 2 Chris@117: Project.find(1).enabled_module_names = ['issue_tracking', 'news'] Chris@117: Chris@117: post :modules, :id => 1, :enabled_module_names => ['issue_tracking', 'repository', 'documents'] Chris@117: assert_redirected_to '/projects/ecookbook/settings/modules' Chris@117: assert_equal ['documents', 'issue_tracking', 'repository'], Project.find(1).enabled_module_names.sort Chris@117: end Chris@117: Chris@117: def test_modules_should_not_allow_get Chris@117: @request.session[:user_id] = 1 Chris@117: get :modules, :id => 1 Chris@117: assert_response :method_not_allowed Chris@117: end Chris@0: Chris@0: def test_get_destroy Chris@0: @request.session[:user_id] = 1 # admin Chris@0: get :destroy, :id => 1 Chris@0: assert_response :success Chris@0: assert_template 'destroy' Chris@0: assert_not_nil Project.find_by_id(1) Chris@0: end Chris@0: Chris@0: def test_post_destroy Chris@0: @request.session[:user_id] = 1 # admin Chris@0: post :destroy, :id => 1, :confirm => 1 chris@37: assert_redirected_to '/admin/projects' Chris@0: assert_nil Project.find_by_id(1) Chris@0: end Chris@0: Chris@0: def test_archive Chris@0: @request.session[:user_id] = 1 # admin Chris@0: post :archive, :id => 1 chris@37: assert_redirected_to '/admin/projects' Chris@0: assert !Project.find(1).active? Chris@0: end Chris@0: Chris@0: def test_unarchive Chris@0: @request.session[:user_id] = 1 # admin Chris@0: Project.find(1).archive Chris@0: post :unarchive, :id => 1 chris@37: assert_redirected_to '/admin/projects' Chris@0: assert Project.find(1).active? Chris@0: end Chris@0: Chris@0: def test_project_breadcrumbs_should_be_limited_to_3_ancestors Chris@0: CustomField.delete_all Chris@0: parent = nil Chris@0: 6.times do |i| Chris@0: p = Project.create!(:name => "Breadcrumbs #{i}", :identifier => "breadcrumbs-#{i}") Chris@0: p.set_parent!(parent) Chris@0: get :show, :id => p Chris@0: assert_tag :h1, :parent => { :attributes => {:id => 'header'}}, Chris@0: :children => { :count => [i, 3].min, Chris@0: :only => { :tag => 'a' } } Chris@0: Chris@0: parent = p Chris@0: end Chris@0: end Chris@0: Chris@0: def test_copy_with_project Chris@0: @request.session[:user_id] = 1 # admin Chris@0: get :copy, :id => 1 Chris@0: assert_response :success Chris@0: assert_template 'copy' Chris@0: assert assigns(:project) Chris@0: assert_equal Project.find(1).description, assigns(:project).description Chris@0: assert_nil assigns(:project).id Chris@0: end Chris@0: Chris@0: def test_copy_without_project Chris@0: @request.session[:user_id] = 1 # admin Chris@0: get :copy Chris@0: assert_response :redirect Chris@0: assert_redirected_to :controller => 'admin', :action => 'projects' Chris@0: end Chris@0: chris@37: context "POST :copy" do chris@37: should "TODO: test the rest of the method" chris@37: chris@37: should "redirect to the project settings when successful" do chris@37: @request.session[:user_id] = 1 # admin chris@37: post :copy, :id => 1, :project => {:name => 'Copy', :identifier => 'unique-copy'} chris@37: assert_response :redirect Chris@117: assert_redirected_to :controller => 'projects', :action => 'settings', :id => 'unique-copy' chris@37: end chris@37: end chris@37: Chris@0: def test_jump_should_redirect_to_active_tab Chris@0: get :show, :id => 1, :jump => 'issues' chris@37: assert_redirected_to '/projects/ecookbook/issues' Chris@0: end Chris@0: Chris@0: def test_jump_should_not_redirect_to_inactive_tab Chris@0: get :show, :id => 3, :jump => 'documents' Chris@0: assert_response :success Chris@0: assert_template 'show' Chris@0: end Chris@0: Chris@0: def test_jump_should_not_redirect_to_unknown_tab Chris@0: get :show, :id => 3, :jump => 'foobar' Chris@0: assert_response :success Chris@0: assert_template 'show' Chris@0: end Chris@0: Chris@0: # A hook that is manually registered later Chris@0: class ProjectBasedTemplate < Redmine::Hook::ViewListener Chris@0: def view_layouts_base_html_head(context) Chris@0: # Adds a project stylesheet Chris@0: stylesheet_link_tag(context[:project].identifier) if context[:project] Chris@0: end Chris@0: end Chris@0: # Don't use this hook now Chris@0: Redmine::Hook.clear_listeners Chris@0: Chris@0: def test_hook_response Chris@0: Redmine::Hook.add_listener(ProjectBasedTemplate) Chris@0: get :show, :id => 1 Chris@0: assert_tag :tag => 'link', :attributes => {:href => '/stylesheets/ecookbook.css'}, Chris@0: :parent => {:tag => 'head'} Chris@0: Chris@0: Redmine::Hook.clear_listeners Chris@0: end Chris@0: end