"Fossies" - the Fresh Open Source Software Archive

Member "redmine-4.1.1/test/functional/projects_controller_test.rb" (6 Apr 2020, 39134 Bytes) of package /linux/www/redmine-4.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "projects_controller_test.rb": 4.1.0_vs_4.1.1.

    1 # frozen_string_literal: true
    2 
    3 # Redmine - project management software
    4 # Copyright (C) 2006-2019  Jean-Philippe Lang
    5 #
    6 # This program is free software; you can redistribute it and/or
    7 # modify it under the terms of the GNU General Public License
    8 # as published by the Free Software Foundation; either version 2
    9 # of the License, or (at your option) any later version.
   10 #
   11 # This program is distributed in the hope that it will be useful,
   12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 # GNU General Public License for more details.
   15 #
   16 # You should have received a copy of the GNU General Public License
   17 # along with this program; if not, write to the Free Software
   18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   19 
   20 require File.expand_path('../../test_helper', __FILE__)
   21 
   22 class ProjectsControllerTest < Redmine::ControllerTest
   23   fixtures :projects, :versions, :users, :email_addresses, :roles, :members,
   24            :member_roles, :issues, :journals, :journal_details,
   25            :trackers, :projects_trackers, :issue_statuses,
   26            :enabled_modules, :enumerations, :boards, :messages,
   27            :attachments, :custom_fields, :custom_values, :time_entries,
   28            :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions
   29 
   30   include Redmine::I18n
   31 
   32   def setup
   33     @request.session[:user_id] = nil
   34     Setting.default_language = 'en'
   35   end
   36 
   37   def test_index_by_anonymous_should_not_show_private_projects
   38     get :index
   39     assert_response :success
   40 
   41     assert_select 'ul' do
   42       assert_select 'li' do
   43         assert_select 'a', :text => 'eCookbook'
   44         assert_select 'ul' do
   45           assert_select 'a', :text => 'Child of private child'
   46         end
   47       end
   48     end
   49     assert_select 'a', :text => /Private child of eCookbook/, :count => 0
   50   end
   51 
   52   def test_index_atom
   53     get :index, :params => {
   54         :format => 'atom'
   55       }
   56     assert_response :success
   57     assert_select 'feed>title', :text => 'Redmine: Latest projects'
   58     assert_select 'feed>entry', :count => Project.visible(User.current).count
   59   end
   60 
   61   def test_index_with_project_filter_is_my_projects
   62     @request.session[:user_id] = 2
   63 
   64     get :index, :params => {
   65       :f => ['id'],
   66       :op => {'id' => '='},
   67       :v => {'id' => ['mine']}
   68     }
   69 
   70     assert_response :success
   71 
   72     assert_select 'div#projects-index ul' do
   73       assert_select 'a.project',  3
   74       assert_select 'a', :text => 'eCookbook'
   75       assert_select 'a', :text => 'OnlineStore'
   76       assert_select 'a', :text => 'Private child of eCookbook'
   77     end
   78   end
   79 
   80   def test_index_with_subproject_filter
   81     @request.session[:user_id] = 1
   82 
   83     get :index, :params => {
   84       :f => ['parent_id'],
   85       :op => {'parent_id' => '='},
   86       :v => {'parent_id' => ['1']}
   87     }
   88 
   89     assert_response :success
   90 
   91     assert_select 'div#projects-index ul' do
   92       assert_select 'a.project',  3
   93       assert_select 'a', :text => 'eCookbook Subproject 1'
   94       assert_select 'a', :text => 'eCookbook Subproject 2'
   95       assert_select 'a', :text => 'Private child of eCookbook'
   96     end
   97   end
   98 
   99   def test_index_as_list_should_format_column_value
  100     get :index, :params => {
  101       :c => ['name', 'status', 'short_description', 'homepage', 'parent_id', 'identifier', 'is_public', 'created_on', 'cf_3'],
  102       :display_type => 'list'
  103     }
  104     assert_response :success
  105 
  106     project = Project.find(1)
  107     assert_select 'table.projects' do
  108       assert_select 'tr[id=?]', 'project-1' do
  109         assert_select 'td.name a[href=?]', '/projects/ecookbook', :text => 'eCookbook'
  110         assert_select 'td.status', :text => 'active'
  111         assert_select 'td.short_description', :text => 'Recipes management application'
  112         assert_select 'td.homepage a.external', :text => 'http://ecookbook.somenet.foo/'
  113         assert_select 'td.identifier', :text => 'ecookbook'
  114         assert_select 'td.is_public', :text => 'Yes'
  115         assert_select 'td.created_on', :text => format_time(project.created_on)
  116         assert_select 'td.cf_3.list', :text => 'Stable'
  117       end
  118       assert_select 'tr[id=?]', 'project-4' do
  119         assert_select 'td.parent_id a[href=?]', '/projects/ecookbook', :text => 'eCookbook'
  120       end
  121     end
  122   end
  123 
  124   def test_index_as_list_should_show_my_favourite_projects
  125     @request.session[:user_id] = 1
  126     get :index, :params => {
  127       :display_type => 'list'
  128     }
  129 
  130     assert_response :success
  131     assert_select 'tr[id=?] td.name span[class=?]', 'project-5', 'icon icon-user my-project'
  132   end
  133 
  134   def test_index_as_list_should_indent_projects
  135     @request.session[:user_id] = 1
  136     get :index, :params => {
  137       :c => ['name', 'short_description'],
  138       :sort => 'parent_id:desc,lft:desc',
  139       :display_type => 'list'
  140     }
  141     assert_response :success
  142 
  143     child_level1 = css_select('tr#project-5').map {|e| e.attr('class')}.first.split(' ')
  144     child_level2 = css_select('tr#project-6').map {|e| e.attr('class')}.first.split(' ')
  145 
  146     assert_include 'idnt', child_level1
  147     assert_include 'idnt-1', child_level1
  148 
  149     assert_include 'idnt', child_level2
  150     assert_include 'idnt-2', child_level2
  151   end
  152 
  153   def test_index_with_default_query_setting
  154     with_settings :project_list_defaults => {'column_names' => %w(name short_description status)} do
  155       get :index, :params => {
  156         :display_type => 'list'
  157       }
  158       assert_response :success
  159     end
  160     assert_equal ['Name', 'Description', 'Status'], columns_in_list
  161   end
  162 
  163   def test_index_as_board_should_not_include_csv_export
  164     @request.session[:user_id] = 1
  165 
  166     get :index
  167 
  168     assert_response :success
  169     assert_select 'p.other-formats a.csv', 0
  170     assert_select '#csv-export-options', 0
  171   end
  172 
  173   def test_index_as_list_should_include_csv_export
  174     @request.session[:user_id] = 1
  175 
  176     get :index, :params => {
  177       :display_type => 'list',
  178       :f => ['parent_id'],
  179       :op => {'parent_id' => '='},
  180       :v => {'parent_id' => ['1']}
  181     }
  182     assert_response :success
  183 
  184     # Assert CSV export link
  185     assert_select 'p.other-formats a.csv'
  186 
  187     # Assert export modal
  188     assert_select '#csv-export-options' do
  189       assert_select 'form[action=?][method=get]', '/projects.csv' do
  190         # filter
  191         assert_select 'input[name=?][value=?]', 'f[]', 'parent_id'
  192         assert_select 'input[name=?][value=?]', 'op[parent_id]', '='
  193         assert_select 'input[name=?][value=?]', 'v[parent_id][]', '1'
  194         # columns
  195         assert_select 'input[name=?][type=hidden][value=?]', 'c[]', 'name'
  196         assert_select 'input[name=?][type=hidden][value=?]', 'c[]', 'identifier'
  197         assert_select 'input[name=?][type=hidden][value=?]', 'c[]', 'short_description'
  198         assert_select 'input[name=?][type=hidden]', 'c[]', 3
  199         assert_select 'input[name=?][value=?]', 'c[]', 'all_inline'
  200       end
  201     end
  202   end
  203 
  204   def test_index_csv
  205     with_settings :date_format => '%m/%d/%Y' do
  206       get :index, :params => {:format => 'csv'}
  207       assert_response :success
  208       assert_equal 'text/csv', response.media_type
  209     end
  210   end
  211 
  212   def test_index_sort_by_custom_field
  213     @request.session[:user_id] = 1
  214 
  215     cf = ProjectCustomField.find(3)
  216     CustomValue.create!(:custom_field => cf, :customized => Project.find(2), :value => 'Beta')
  217 
  218     get(
  219       :index,
  220       :params => {
  221         :display_type => 'list',
  222         :c => ['name', 'identifier', 'cf_3'],
  223         :set_filter => 1,
  224         :sort => "cf_#{cf.id}:asc"
  225       }
  226     )
  227     assert_response :success
  228 
  229     assert_equal(
  230       ['Beta', 'Stable'],
  231       columns_values_in_list('cf_3').reject {|p| p.empty?}
  232     )
  233   end
  234 
  235   def test_index_with_int_custom_field_total
  236     @request.session[:user_id] = 1
  237 
  238     field = ProjectCustomField.generate!(:field_format => 'int')
  239     CustomValue.create!(:customized => Project.find(1), :custom_field => field, :value => '2')
  240     CustomValue.create!(:customized => Project.find(2), :custom_field => field, :value => '7')
  241     get(
  242       :index,
  243       :params => {
  244         :display_type => 'list',
  245         :t => ["cf_#{field.id}"]
  246       }
  247     )
  248     assert_response :success
  249     assert_select '.query-totals'
  250     assert_select ".total-for-cf-#{field.id} span.value", :text => '9'
  251   end
  252 
  253   def test_autocomplete_js
  254     get :autocomplete, :params => {
  255         :format => 'js',
  256         :q => 'coo'
  257       },
  258       :xhr => true
  259     assert_response :success
  260     assert_equal 'text/javascript', response.content_type
  261   end
  262 
  263   def test_autocomplete_js_with_blank_search_term
  264     get :autocomplete, :params => {
  265         :format => 'js',
  266         :q => ''
  267       },
  268       :xhr => true
  269     assert_response :success
  270     assert_equal 'text/javascript', response.content_type
  271   end
  272 
  273   test "#index by non-admin user with view_time_entries permission should show overall spent time link" do
  274     @request.session[:user_id] = 3
  275     get :index
  276     assert_select 'a[href=?]', '/time_entries'
  277   end
  278 
  279   test "#index by non-admin user without view_time_entries permission should not show overall spent time link" do
  280     Role.find(2).remove_permission! :view_time_entries
  281     Role.non_member.remove_permission! :view_time_entries
  282     Role.anonymous.remove_permission! :view_time_entries
  283     @request.session[:user_id] = 3
  284 
  285     get :index
  286     assert_select 'a[href=?]', '/time_entries', 0
  287   end
  288 
  289   test "#index by non-admin user with permission should show add project link" do
  290     Role.find(1).add_permission! :add_project
  291     @request.session[:user_id] = 2
  292 
  293     get :index
  294     assert_select 'a[href=?]', '/projects/new'
  295   end
  296 
  297   test "#new by admin user should accept get" do
  298     @request.session[:user_id] = 1
  299 
  300     get :new
  301     assert_response :success
  302     assert_select 'input[name=?]', 'project[name]'
  303     assert_select 'select[name=?]', 'project[parent_id]'
  304   end
  305 
  306   test "#new by non-admin user with add_project permission should accept get" do
  307     Role.non_member.add_permission! :add_project
  308     @request.session[:user_id] = 9
  309 
  310     get :new
  311     assert_response :success
  312     assert_select 'input[name=?]', 'project[name]'
  313     assert_select 'select[name=?]', 'project[parent_id]', 0
  314   end
  315 
  316   test "#new by non-admin user with add_subprojects permission should accept get" do
  317     Role.find(1).remove_permission! :add_project
  318     Role.find(1).add_permission! :add_subprojects
  319     @request.session[:user_id] = 2
  320 
  321     get :new, :params => {
  322         :parent_id => 'ecookbook'
  323       }
  324     assert_response :success
  325 
  326     assert_select 'select[name=?]', 'project[parent_id]' do
  327       # parent project selected
  328       assert_select 'option[value="1"][selected=selected]'
  329       # no empty value
  330       assert_select 'option[value=""]', 0
  331     end
  332   end
  333 
  334   def test_new_by_non_admin_should_display_modules_if_default_role_is_allowed_to_select_modules
  335     Role.non_member.add_permission!(:add_project)
  336     default_role = Role.generate!(:permissions => [:view_issues])
  337     user = User.generate!
  338     @request.session[:user_id] = user.id
  339 
  340     with_settings :new_project_user_role_id => default_role.id.to_s do
  341       get :new
  342       assert_select 'input[name=?]', 'project[enabled_module_names][]', 0
  343 
  344       default_role.add_permission!(:select_project_modules)
  345       get :new
  346       assert_select 'input[name=?]', 'project[enabled_module_names][]'
  347     end
  348   end
  349 
  350   def test_new_should_not_display_invalid_search_link
  351     @request.session[:user_id] = 1
  352 
  353     get :new
  354     assert_response :success
  355     assert_select '#quick-search form[action=?]', '/search'
  356     assert_select '#quick-search a[href=?]', '/search'
  357   end
  358 
  359   test "#create by admin user should create a new project" do
  360     @request.session[:user_id] = 1
  361 
  362     post :create, :params => {
  363         :project => {
  364           :name => "blog",
  365           :description => "weblog",
  366           :homepage => 'http://weblog',
  367           :identifier => "blog",
  368           :is_public => 1,
  369           :custom_field_values => {
  370             '3' => 'Beta'
  371           },
  372           :tracker_ids => ['1', '3'],
  373           # an issue custom field that is not for all project
  374           :issue_custom_field_ids => ['9'],
  375           :enabled_module_names => ['issue_tracking', 'news', 'repository']
  376         }
  377       }
  378     assert_redirected_to '/projects/blog/settings'
  379 
  380     project = Project.find_by_name('blog')
  381     assert_kind_of Project, project
  382     assert project.active?
  383     assert_equal 'weblog', project.description
  384     assert_equal 'http://weblog', project.homepage
  385     assert_equal true, project.is_public?
  386     assert_nil project.parent
  387     assert_equal 'Beta', project.custom_value_for(3).value
  388     assert_equal [1, 3], project.trackers.map(&:id).sort
  389     assert_equal ['issue_tracking', 'news', 'repository'], project.enabled_module_names.sort
  390     assert project.issue_custom_fields.include?(IssueCustomField.find(9))
  391   end
  392 
  393   test "#create by admin user should create a new subproject" do
  394     @request.session[:user_id] = 1
  395 
  396     assert_difference 'Project.count' do
  397       post :create, :params => {
  398           :project => {
  399             :name => "blog",
  400             :description => "weblog",
  401             :identifier => "blog",
  402             :is_public => 1,
  403             :custom_field_values => {
  404               '3' => 'Beta'
  405             },
  406             :parent_id => 1
  407 
  408           }
  409         }
  410       assert_redirected_to '/projects/blog/settings'
  411     end
  412 
  413     project = Project.find_by_name('blog')
  414     assert_kind_of Project, project
  415     assert_equal Project.find(1), project.parent
  416   end
  417 
  418   test "#create by admin user should continue" do
  419     @request.session[:user_id] = 1
  420 
  421     assert_difference 'Project.count' do
  422       post :create, :params => {
  423           :project => {
  424             :name => "blog",
  425             :identifier => "blog"
  426           },
  427           :continue => 'Create and continue'
  428         }
  429     end
  430     assert_redirected_to '/projects/new'
  431   end
  432 
  433   test "#create by non-admin user with add_project permission should create a new project" do
  434     Role.non_member.add_permission! :add_project
  435     @request.session[:user_id] = 9
  436 
  437     post :create, :params => {
  438         :project => {
  439           :name => "blog",
  440           :description => "weblog",
  441           :identifier => "blog",
  442           :is_public => 1,
  443           :custom_field_values => {
  444             '3' => 'Beta'
  445           },
  446           :tracker_ids => ['1', '3'],
  447           :enabled_module_names => ['issue_tracking', 'news', 'repository']
  448 
  449         }
  450       }
  451 
  452     assert_redirected_to '/projects/blog/settings'
  453 
  454     project = Project.find_by_name('blog')
  455     assert_kind_of Project, project
  456     assert_equal 'weblog', project.description
  457     assert_equal true, project.is_public?
  458     assert_equal [1, 3], project.trackers.map(&:id).sort
  459     assert_equal ['issue_tracking', 'news', 'repository'], project.enabled_module_names.sort
  460 
  461     # User should be added as a project member
  462     assert User.find(9).member_of?(project)
  463     assert_equal 1, project.members.size
  464   end
  465 
  466   test "#create by non-admin user with add_project permission should fail with parent_id" do
  467     Role.non_member.add_permission! :add_project
  468     User.find(9).update! :language => 'en'
  469     @request.session[:user_id] = 9
  470 
  471     assert_no_difference 'Project.count' do
  472       post :create, :params => {
  473           :project => {
  474             :name => "blog",
  475             :description => "weblog",
  476             :identifier => "blog",
  477             :is_public => 1,
  478             :custom_field_values => {
  479               '3' => 'Beta'
  480             },
  481             :parent_id => 1
  482 
  483           }
  484         }
  485     end
  486     assert_response :success
  487     assert_select_error /Subproject of is invalid/
  488   end
  489 
  490   test "#create by non-admin user with add_subprojects permission should create a project with a parent_id" do
  491     Role.find(1).remove_permission! :add_project
  492     Role.find(1).add_permission! :add_subprojects
  493     @request.session[:user_id] = 2
  494 
  495     post :create, :params => {
  496         :project => {
  497           :name => "blog",
  498           :description => "weblog",
  499           :identifier => "blog",
  500           :is_public => 1,
  501           :custom_field_values => {
  502             '3' => 'Beta'
  503           },
  504           :parent_id => 1
  505 
  506         }
  507       }
  508     assert_redirected_to '/projects/blog/settings'
  509     project = Project.find_by_name('blog')
  510     assert_equal 1, project.parent_id
  511   end
  512 
  513   test "#create by non-admin user with add_subprojects permission should fail without parent_id" do
  514     Role.find(1).remove_permission! :add_project
  515     Role.find(1).add_permission! :add_subprojects
  516     @request.session[:user_id] = 2
  517 
  518     assert_no_difference 'Project.count' do
  519       post :create, :params => {
  520           :project => {
  521             :name => "blog",
  522             :description => "weblog",
  523             :identifier => "blog",
  524             :is_public => 1,
  525             :custom_field_values => {
  526               '3' => 'Beta'
  527             }
  528 
  529           }
  530         }
  531     end
  532     assert_response :success
  533     assert_select_error /Subproject of is invalid/
  534   end
  535 
  536   test "#create by non-admin user with add_subprojects permission should fail with unauthorized parent_id" do
  537     Role.find(1).remove_permission! :add_project
  538     Role.find(1).add_permission! :add_subprojects
  539     @request.session[:user_id] = 2
  540 
  541     assert !User.find(2).member_of?(Project.find(6))
  542     assert_no_difference 'Project.count' do
  543       post :create, :params => {
  544           :project => {
  545             :name => "blog",
  546             :description => "weblog",
  547             :identifier => "blog",
  548             :is_public => 1,
  549             :custom_field_values => {
  550               '3' => 'Beta'
  551             },
  552             :parent_id => 6
  553 
  554           }
  555         }
  556     end
  557     assert_response :success
  558     assert_select_error /Subproject of is invalid/
  559   end
  560 
  561   def test_create_by_non_admin_should_accept_modules_if_default_role_is_allowed_to_select_modules
  562     Role.non_member.add_permission!(:add_project)
  563     default_role = Role.generate!(:permissions => [:view_issues, :add_project])
  564     user = User.generate!
  565     @request.session[:user_id] = user.id
  566 
  567     with_settings :new_project_user_role_id => default_role.id.to_s, :default_projects_modules => %w(news files) do
  568       project = new_record(Project) do
  569         post :create, :params => {
  570             :project => {
  571               :name => "blog1",
  572               :identifier => "blog1",
  573               :enabled_module_names => ["issue_tracking", "repository"]
  574 
  575             }
  576           }
  577       end
  578       assert_equal %w(files news), project.enabled_module_names.sort
  579 
  580       default_role.add_permission!(:select_project_modules)
  581       project = new_record(Project) do
  582         post :create, :params => {
  583             :project => {
  584               :name => "blog2",
  585               :identifier => "blog2",
  586               :enabled_module_names => ["issue_tracking", "repository"]
  587 
  588             }
  589           }
  590       end
  591       assert_equal %w(issue_tracking repository), project.enabled_module_names.sort
  592     end
  593   end
  594 
  595   def test_create_subproject_with_inherit_members_should_inherit_members
  596     Role.find_by_name('Manager').add_permission! :add_subprojects
  597     parent = Project.find(1)
  598     @request.session[:user_id] = 2
  599 
  600     assert_difference 'Project.count' do
  601       post :create, :params => {
  602           :project => {
  603             :name => 'inherited',
  604             :identifier => 'inherited',
  605             :parent_id => parent.id,
  606             :inherit_members => '1'
  607 
  608           }
  609         }
  610       assert_response 302
  611     end
  612 
  613     project = Project.order('id desc').first
  614     assert_equal 'inherited', project.name
  615     assert_equal parent, project.parent
  616     assert project.memberships.count > 0
  617     assert_equal parent.memberships.count, project.memberships.count
  618   end
  619 
  620   def test_create_should_preserve_modules_on_validation_failure
  621     with_settings :default_projects_modules => ['issue_tracking', 'repository'] do
  622       @request.session[:user_id] = 1
  623       assert_no_difference 'Project.count' do
  624         post :create, :params => {
  625             :project => {
  626               :name => "blog",
  627               :identifier => "",
  628               :enabled_module_names => %w(issue_tracking news)
  629 
  630             }
  631           }
  632       end
  633       assert_response :success
  634       %w(issue_tracking news).each do |mod|
  635         assert_select 'input[name=?][value=?][checked=checked]', 'project[enabled_module_names][]', mod
  636       end
  637       assert_select 'input[name=?][checked=checked]', 'project[enabled_module_names][]', :count => 2
  638     end
  639   end
  640 
  641   def test_show_by_id
  642     get :show, :params => {
  643         :id => 1
  644       }
  645     assert_response :success
  646     assert_select '#header h1', :text => "eCookbook"
  647   end
  648 
  649   def test_show_by_identifier
  650     get :show, :params => {
  651         :id => 'ecookbook'
  652       }
  653     assert_response :success
  654     assert_select '#header h1', :text => "eCookbook"
  655   end
  656 
  657   def test_show_should_not_display_empty_sidebar
  658     p = Project.find(1)
  659     p.enabled_module_names = []
  660     p.save!
  661 
  662     get :show, :params => {
  663         :id => 'ecookbook'
  664       }
  665     assert_response :success
  666     assert_select '#main.nosidebar'
  667   end
  668 
  669   def test_show_should_display_visible_custom_fields
  670     ProjectCustomField.find_by_name('Development status').update_attribute :visible, true
  671     get :show, :params => {
  672         :id => 'ecookbook'
  673       }
  674     assert_response :success
  675 
  676     assert_select 'li[class=?]', 'cf_3', :text => /Development status/
  677   end
  678 
  679   def test_show_should_not_display_hidden_custom_fields
  680     ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
  681     get :show, :params => {
  682         :id => 'ecookbook'
  683       }
  684     assert_response :success
  685 
  686     assert_select 'li', :text => /Development status/, :count => 0
  687   end
  688 
  689   def test_show_should_not_display_blank_custom_fields_with_multiple_values
  690     f1 = ProjectCustomField.generate! :field_format => 'list', :possible_values => %w(Foo Bar), :multiple => true
  691     f2 = ProjectCustomField.generate! :field_format => 'list', :possible_values => %w(Baz Qux), :multiple => true
  692     project = Project.generate!(:custom_field_values => {f2.id.to_s => %w(Qux)})
  693 
  694     get :show, :params => {
  695         :id => project.id
  696       }
  697     assert_response :success
  698 
  699     assert_select 'li', :text => /#{f1.name}/, :count => 0
  700     assert_select 'li', :text => /#{f2.name}/
  701   end
  702 
  703   def test_show_should_not_display_blank_text_custom_fields
  704     f1 = ProjectCustomField.generate! :field_format => 'text'
  705 
  706     get :show, :params => {
  707         :id => 1
  708       }
  709     assert_response :success
  710 
  711     assert_select 'li', :text => /#{f1.name}/, :count => 0
  712   end
  713 
  714   def test_show_should_not_fail_when_custom_values_are_nil
  715     project = Project.find_by_identifier('ecookbook')
  716     project.custom_values.first.update_attribute(:value, nil)
  717     get :show, :params => {
  718         :id => 'ecookbook'
  719       }
  720     assert_response :success
  721   end
  722 
  723   def test_show_archived_project_should_be_denied
  724     project = Project.find_by_identifier('ecookbook')
  725     project.archive
  726 
  727     get :show, :params => {
  728         :id => 'ecookbook'
  729       }
  730     assert_response 403
  731     assert_select 'p', :text => /archived/
  732     assert_not_include project.name, response.body
  733   end
  734 
  735   def test_show_archived_project_should_show_unarchive_link_to_admins
  736     @request.session[:user_id] = 1
  737     project = Project.find_by_identifier('ecookbook')
  738     project.archive
  739 
  740     get :show, :params => {
  741         :id => 'ecookbook'
  742       }
  743     assert_response 403
  744     assert_select 'a', :text => "Unarchive"
  745   end
  746 
  747   def test_show_should_not_show_private_subprojects_that_are_not_visible
  748     get :show, :params => {
  749         :id => 'ecookbook'
  750       }
  751     assert_response :success
  752     assert_select 'a', :text => /Private child/, :count => 0
  753   end
  754 
  755   def test_show_should_show_private_subprojects_that_are_visible
  756     @request.session[:user_id] = 2 # manager who is a member of the private subproject
  757     get :show, :params => {
  758         :id => 'ecookbook'
  759       }
  760     assert_response :success
  761     assert_select 'a', :text => /Private child/
  762   end
  763 
  764   def test_show_by_member_on_leaf_project_should_display_issue_counts
  765     @request.session[:user_id] = 2
  766     get :show, :params => {
  767         :id => 'onlinestore'
  768       }
  769     assert_response :success
  770     # Make sure there's a > 0 issue count
  771     assert_select 'table.issue-report td.total a', :text => %r{\A[1-9]\d*\z}
  772   end
  773 
  774   def test_show_should_spent_and_estimated_time
  775     @request.session[:user_id] = 1
  776     get :show, :params => {
  777         :id => 'ecookbook'
  778       }
  779 
  780     assert_select 'div.spent_time.box>ul' do
  781       assert_select '>li:nth-child(1)', :text => 'Estimated time: 203.50 hours'
  782       assert_select '>li:nth-child(2)', :text => 'Spent time: 162.90 hours'
  783     end
  784   end
  785 
  786   def test_settings
  787     @request.session[:user_id] = 2 # manager
  788     get :settings, :params => {
  789         :id => 1
  790       }
  791     assert_response :success
  792 
  793     assert_select 'input[name=?]', 'project[name]'
  794   end
  795 
  796   def test_settings_of_subproject
  797     @request.session[:user_id] = 2
  798     get :settings, :params => {
  799         :id => 'private-child'
  800       }
  801     assert_response :success
  802 
  803     assert_select 'input[type=checkbox][name=?]', 'project[inherit_members]'
  804   end
  805 
  806   def test_settings_should_be_denied_for_member_on_closed_project
  807     Project.find(1).close
  808     @request.session[:user_id] = 2 # manager
  809 
  810     get :settings, :params => {
  811         :id => 1
  812       }
  813     assert_response 403
  814   end
  815 
  816   def test_settings_should_be_denied_for_anonymous_on_closed_project
  817     Project.find(1).close
  818 
  819     get :settings, :params => {
  820         :id => 1
  821       }
  822     assert_response 403
  823   end
  824 
  825   def test_settings_should_accept_version_status_filter
  826     @request.session[:user_id] = 2
  827 
  828     get :settings, :params => {
  829         :id => 'ecookbook',
  830         :tab => 'versions',
  831         :version_status => 'locked'
  832       }
  833     assert_response :success
  834 
  835     assert_select 'select[name=version_status]' do
  836       assert_select 'option[value=locked][selected=selected]'
  837     end
  838     assert_select 'table.versions tbody' do
  839       assert_select 'tr', 1
  840       assert_select 'td.name', :text => '1.0'
  841     end
  842     assert_select 'a#tab-versions[href=?]', '/projects/ecookbook/settings/versions?version_status=locked'
  843   end
  844 
  845   def test_settings_should_accept_version_name_filter
  846     @request.session[:user_id] = 2
  847 
  848     get :settings, :params => {
  849         :id => 'ecookbook',
  850         :tab => 'versions',
  851         :version_status => '',
  852         :version_name => '.1'
  853       }
  854     assert_response :success
  855 
  856     assert_select 'input[name=version_name][value=?]', '.1'
  857     assert_select 'table.versions tbody' do
  858       assert_select 'tr', 1
  859       assert_select 'td.name', :text => '0.1'
  860     end
  861     assert_select 'a#tab-versions[href=?]', '/projects/ecookbook/settings/versions?version_name=.1&version_status='
  862   end
  863 
  864   def test_settings_should_show_default_version_in_versions_tab
  865     project = Project.find(1)
  866     project.default_version_id = 3
  867     project.save!
  868 
  869     @request.session[:user_id] = 2
  870 
  871     get :settings, :params => {
  872         :id => 'ecookbook',
  873         :tab => 'versions',
  874       }
  875     assert_response :success
  876 
  877     assert_select 'table.versions tbody' do
  878       # asserts that only one version is marked as default
  879       assert_select 'td.tick span.icon-checked', 1
  880       # asserts which version is marked as default
  881       assert_select 'tr:first-child td.tick span.icon-checked', 1
  882     end
  883   end
  884 
  885   def test_settings_should_show_locked_members
  886     user = User.generate!
  887     member = User.add_to_project(user, Project.find(1))
  888     user.lock!
  889     assert user.reload.locked?
  890     @request.session[:user_id] = 2
  891 
  892     get :settings, :params => {
  893         :id => 'ecookbook',
  894         :tab => 'members'
  895       }
  896     assert_response :success
  897     assert_select "tr#member-#{member.id}"
  898   end
  899 
  900   def test_settings_should_show_tabs_depending_on_permission
  901     @request.session[:user_id] = 3
  902     project = Project.find(1)
  903     role = User.find(3).roles_for_project(project).first
  904 
  905     role.permissions = []
  906     role.save
  907     get :settings, :params => {
  908       :id => project.id
  909     }
  910     assert_response 403
  911 
  912     role.add_permission! :manage_repository, :manage_boards, :manage_project_activities
  913     get :settings, :params => {
  914       :id => project.id
  915     }
  916     assert_response :success
  917     assert_select 'a[id^=tab-]', 3
  918     assert_select 'a#tab-repositories'
  919     assert_select 'a#tab-boards'
  920     assert_select 'a#tab-activities'
  921   end
  922 
  923   def test_settings_should_not_display_custom_fields_not_visible_for_user
  924     @request.session[:user_id] = 2
  925 
  926     ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
  927     get :settings, :params => {
  928         :id => 'ecookbook'
  929       }
  930     assert_response :success
  931 
  932     assert_select 'select#project_custom_field_values_3', :count => 0
  933   end
  934 
  935   def test_update
  936     @request.session[:user_id] = 2 # manager
  937     post :update, :params => {
  938         :id => 1,
  939         :project => {
  940           :name => 'Test changed name',
  941           :issue_custom_field_ids => ['']
  942         }
  943       }
  944     assert_redirected_to '/projects/ecookbook/settings'
  945     project = Project.find(1)
  946     assert_equal 'Test changed name', project.name
  947   end
  948 
  949   def test_update_with_failure
  950     @request.session[:user_id] = 2 # manager
  951     post :update, :params => {
  952         :id => 1,
  953         :project => {
  954           :name => ''
  955         }
  956       }
  957     assert_response :success
  958     assert_select_error /name cannot be blank/i
  959   end
  960 
  961   def test_update_should_be_denied_for_member_on_closed_project
  962     Project.find(1).close
  963     @request.session[:user_id] = 2 # manager
  964 
  965     post :update, :params => {
  966         :id => 1,
  967         :project => {
  968           :name => 'Closed'
  969         }
  970       }
  971     assert_response 403
  972     assert_equal 'eCookbook', Project.find(1).name
  973   end
  974 
  975   def test_update_should_be_denied_for_anonymous_on_closed_project
  976     Project.find(1).close
  977 
  978     post :update, :params => {
  979         :id => 1,
  980         :project => {
  981           :name => 'Closed'
  982         }
  983       }
  984     assert_response 403
  985     assert_equal 'eCookbook', Project.find(1).name
  986   end
  987 
  988   def test_update_child_project_without_parent_permission_should_not_show_validation_error
  989     child = Project.generate_with_parent!
  990     user = User.generate!
  991     User.add_to_project(user, child, Role.generate!(:permissions => [:edit_project]))
  992     @request.session[:user_id] = user.id
  993 
  994     post :update, :params => {
  995         :id => child.id,
  996         :project => {
  997           :name => 'Updated'
  998         }
  999       }
 1000     assert_response 302
 1001     assert_match /Successful update/, flash[:notice]
 1002   end
 1003 
 1004   def test_update_modules
 1005     @request.session[:user_id] = 2
 1006     Project.find(1).enabled_module_names = ['issue_tracking', 'news']
 1007 
 1008     post :update, :params => {
 1009         :id => 1,
 1010         :project => {
 1011           :enabled_module_names => ['issue_tracking', 'repository', 'documents']
 1012         }
 1013       }
 1014     assert_redirected_to '/projects/ecookbook/settings'
 1015     assert_equal ['documents', 'issue_tracking', 'repository'], Project.find(1).enabled_module_names.sort
 1016   end
 1017 
 1018   def test_destroy_leaf_project_without_confirmation_should_show_confirmation
 1019     @request.session[:user_id] = 1 # admin
 1020 
 1021     assert_no_difference 'Project.count' do
 1022       delete :destroy, :params => {
 1023           :id => 2
 1024         }
 1025       assert_response :success
 1026     end
 1027     assert_select '.warning', :text => /Are you sure you want to delete this project/
 1028   end
 1029 
 1030   def test_destroy_without_confirmation_should_show_confirmation_with_subprojects
 1031     set_tmp_attachments_directory
 1032     @request.session[:user_id] = 1 # admin
 1033 
 1034     assert_no_difference 'Project.count' do
 1035       delete :destroy, :params => {
 1036           :id => 1
 1037         }
 1038       assert_response :success
 1039     end
 1040     assert_select 'strong',
 1041                   :text => ['Private child of eCookbook',
 1042                             'Child of private child, eCookbook Subproject 1',
 1043                             'eCookbook Subproject 2'].join(', ')
 1044   end
 1045 
 1046   def test_destroy_with_confirmation_should_destroy_the_project_and_subprojects
 1047     set_tmp_attachments_directory
 1048     @request.session[:user_id] = 1 # admin
 1049 
 1050     assert_difference 'Project.count', -5 do
 1051       delete :destroy, :params => {
 1052           :id => 1,
 1053           :confirm => 1
 1054         }
 1055       assert_redirected_to '/admin/projects'
 1056     end
 1057     assert_nil Project.find_by_id(1)
 1058   end
 1059 
 1060   def test_archive
 1061     @request.session[:user_id] = 1 # admin
 1062     post :archive, :params => {
 1063         :id => 1
 1064       }
 1065     assert_redirected_to '/admin/projects'
 1066     assert !Project.find(1).active?
 1067   end
 1068 
 1069   def test_archive_with_failure
 1070     @request.session[:user_id] = 1
 1071     Project.any_instance.stubs(:archive).returns(false)
 1072     post :archive, :params => {
 1073         :id => 1
 1074       }
 1075     assert_redirected_to '/admin/projects'
 1076     assert_match /project cannot be archived/i, flash[:error]
 1077   end
 1078 
 1079   def test_unarchive
 1080     @request.session[:user_id] = 1 # admin
 1081     Project.find(1).archive
 1082     post :unarchive, :params => {
 1083         :id => 1
 1084       }
 1085     assert_redirected_to '/admin/projects'
 1086     assert Project.find(1).active?
 1087   end
 1088 
 1089   def test_close
 1090     @request.session[:user_id] = 2
 1091     post :close, :params => {
 1092         :id => 1
 1093       }
 1094     assert_redirected_to '/projects/ecookbook'
 1095     assert_equal Project::STATUS_CLOSED, Project.find(1).status
 1096   end
 1097 
 1098   def test_reopen
 1099     Project.find(1).close
 1100     @request.session[:user_id] = 2
 1101     post :reopen, :params => {
 1102         :id => 1
 1103       }
 1104     assert_redirected_to '/projects/ecookbook'
 1105     assert Project.find(1).active?
 1106   end
 1107 
 1108   def test_project_breadcrumbs_should_be_limited_to_3_ancestors
 1109     CustomField.delete_all
 1110     parent = nil
 1111     6.times do |i|
 1112       p = Project.generate_with_parent!(parent)
 1113       get :show, :params => {
 1114           :id => p
 1115         }
 1116       assert_select '#header h1' do
 1117         assert_select 'a', :count => [i, 3].min
 1118       end
 1119 
 1120       parent = p
 1121     end
 1122   end
 1123 
 1124   def test_get_copy
 1125     @request.session[:user_id] = 1 # admin
 1126     orig = Project.find(1)
 1127 
 1128     get :copy, :params => {
 1129         :id => orig.id
 1130       }
 1131     assert_response :success
 1132 
 1133     assert_select 'textarea[name=?]', 'project[description]', :text => orig.description
 1134     assert_select 'input[name=?][value=?]', 'project[enabled_module_names][]', 'issue_tracking', 1
 1135   end
 1136 
 1137   def test_get_copy_with_invalid_source_should_respond_with_404
 1138     @request.session[:user_id] = 1
 1139     get :copy, :params => {
 1140         :id => 99
 1141       }
 1142     assert_response 404
 1143   end
 1144 
 1145   def test_get_copy_should_preselect_custom_fields
 1146     field1 = IssueCustomField.generate!(:is_for_all => false)
 1147     field2 = IssueCustomField.generate!(:is_for_all => false)
 1148     source = Project.generate!(:issue_custom_fields => [field1])
 1149     @request.session[:user_id] = 1
 1150 
 1151     get :copy, :params => {
 1152         :id => source.id
 1153       }
 1154     assert_response :success
 1155     assert_select 'input[type=hidden][name=?][value=?]', 'project[issue_custom_field_ids][]', field1.id.to_s
 1156     assert_select 'input[type=hidden][name=?][value=?]', 'project[issue_custom_field_ids][]', field2.id.to_s, 0
 1157   end
 1158 
 1159   def test_post_copy_should_copy_requested_items
 1160     @request.session[:user_id] = 1 # admin
 1161     CustomField.delete_all
 1162 
 1163     assert_difference 'Project.count' do
 1164       post :copy, :params => {
 1165           :id => 1,
 1166           :project => {
 1167             :name => 'Copy',
 1168             :identifier => 'unique-copy',
 1169             :tracker_ids => ['1', '2', '3', ''],
 1170             :enabled_module_names => %w(issue_tracking time_tracking)
 1171 
 1172           },
 1173           :only => %w(issues versions)
 1174         }
 1175     end
 1176     project = Project.find('unique-copy')
 1177     source = Project.find(1)
 1178     assert_equal %w(issue_tracking time_tracking), project.enabled_module_names.sort
 1179 
 1180     assert_equal source.versions.count, project.versions.count, "All versions were not copied"
 1181     assert_equal source.issues.count, project.issues.count, "All issues were not copied"
 1182     assert_equal 0, project.members.count
 1183   end
 1184 
 1185   def test_post_copy_should_redirect_to_settings_when_successful
 1186     @request.session[:user_id] = 1 # admin
 1187     post :copy, :params => {
 1188         :id => 1,
 1189         :project => {
 1190           :name => 'Copy',
 1191           :identifier => 'unique-copy'
 1192         }
 1193       }
 1194     assert_response :redirect
 1195     assert_redirected_to :controller => 'projects', :action => 'settings', :id => 'unique-copy'
 1196   end
 1197 
 1198   def test_post_copy_with_failure
 1199     @request.session[:user_id] = 1
 1200     post :copy, :params => {
 1201         :id => 1,
 1202         :project => {
 1203           :name => 'Copy',
 1204           :identifier => ''
 1205         }
 1206       }
 1207     assert_response :success
 1208     assert_select_error /Identifier cannot be blank/
 1209   end
 1210 
 1211   def test_bookmark_should_create_bookmark
 1212     @request.session[:user_id] = 3
 1213     post :bookmark, params: { id: 'ecookbook' }
 1214     assert_redirected_to controller: 'projects', action: 'show', id: 'ecookbook'
 1215     jb = Redmine::ProjectJumpBox.new(User.find(3))
 1216     assert jb.bookmark?(Project.find('ecookbook'))
 1217     refute jb.bookmark?(Project.find('onlinestore'))
 1218   end
 1219 
 1220   def test_bookmark_should_delete_bookmark
 1221     @request.session[:user_id] = 3
 1222     jb = Redmine::ProjectJumpBox.new(User.find(3))
 1223     project = Project.find('ecookbook')
 1224     jb.bookmark_project project
 1225     delete :bookmark, params: { id: 'ecookbook' }
 1226     assert_redirected_to controller: 'projects', action: 'show', id: 'ecookbook'
 1227 
 1228     jb = Redmine::ProjectJumpBox.new(User.find(3))
 1229     refute jb.bookmark?(Project.find('ecookbook'))
 1230   end
 1231 
 1232   def test_index_jump_without_project_id_should_redirect_to_active_tab
 1233     get :index, :params => {
 1234         :jump => 'issues'
 1235       }
 1236     assert_redirected_to '/issues'
 1237   end
 1238 
 1239   def test_index_jump_should_not_redirect_to_unknown_tab
 1240     get :index, :params => {
 1241         :jump => 'foobar'
 1242       }
 1243     assert_response :success
 1244   end
 1245 
 1246   def test_show_jump_should_redirect_to_active_tab
 1247     get :show, :params => {
 1248         :id => 1,
 1249         :jump => 'issues'
 1250       }
 1251     assert_redirected_to '/projects/ecookbook/issues'
 1252   end
 1253 
 1254   def test_show_jump_should_not_redirect_to_inactive_tab
 1255     get :show, :params => {
 1256         :id => 3,
 1257         :jump => 'documents'
 1258       }
 1259     assert_response :success
 1260   end
 1261 
 1262   def test_show_jump_should_not_redirect_to_unknown_tab
 1263     get :show, :params => {
 1264         :id => 3,
 1265         :jump => 'foobar'
 1266       }
 1267     assert_response :success
 1268   end
 1269 
 1270   def test_body_should_have_project_css_class
 1271     get :show, :params => {
 1272         :id => 1
 1273       }
 1274     assert_select 'body.project-ecookbook'
 1275   end
 1276 
 1277   def test_default_search_scope_in_global_page
 1278     get :index
 1279 
 1280     assert_select 'div#quick-search form' do
 1281       assert_select 'input[name=scope][type=hidden]'
 1282       assert_select 'a[href=?]', '/search'
 1283     end
 1284   end
 1285 
 1286   def test_default_search_scope_for_project_without_subprojects
 1287     get :show, :params => {
 1288       :id => 4,
 1289     }
 1290 
 1291     assert_select 'div#quick-search form' do
 1292       assert_select 'input[name=scope][type=hidden]'
 1293       assert_select 'a[href=?]', '/projects/subproject2/search'
 1294     end
 1295   end
 1296 
 1297   def test_default_search_scope_for_project_with_subprojects
 1298     get :show, :params => {
 1299       :id => 1,
 1300     }
 1301 
 1302     assert_select 'div#quick-search form' do
 1303       assert_select 'input[name=scope][type=hidden][value=subprojects]'
 1304       assert_select 'a[href=?]', '/projects/ecookbook/search?scope=subprojects'
 1305     end
 1306   end
 1307 end