You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

context_menus_controller_test.rb 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2015 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../test_helper', __FILE__)
  18. class ContextMenusControllerTest < ActionController::TestCase
  19. fixtures :projects,
  20. :trackers,
  21. :projects_trackers,
  22. :roles,
  23. :member_roles,
  24. :members,
  25. :enabled_modules,
  26. :workflows,
  27. :journals, :journal_details,
  28. :versions,
  29. :issues, :issue_statuses, :issue_categories,
  30. :users,
  31. :enumerations,
  32. :time_entries
  33. def test_context_menu_one_issue
  34. @request.session[:user_id] = 2
  35. get :issues, :ids => [1]
  36. assert_response :success
  37. assert_template 'context_menus/issues'
  38. assert_select 'a.icon-edit[href=?]', '/issues/1/edit', :text => 'Edit'
  39. assert_select 'a.icon-copy[href=?]', '/projects/ecookbook/issues/1/copy', :text => 'Copy'
  40. assert_select 'a.icon-del[href=?]', '/issues?ids%5B%5D=1', :text => 'Delete'
  41. # Statuses
  42. assert_select 'a[href=?]', '/issues/bulk_update?ids%5B%5D=1&issue%5Bstatus_id%5D=5', :text => 'Closed'
  43. assert_select 'a[href=?]', '/issues/bulk_update?ids%5B%5D=1&issue%5Bpriority_id%5D=8', :text => 'Immediate'
  44. # No inactive priorities
  45. assert_select 'a', :text => /Inactive Priority/, :count => 0
  46. # Versions
  47. assert_select 'a[href=?]', '/issues/bulk_update?ids%5B%5D=1&issue%5Bfixed_version_id%5D=3', :text => '2.0'
  48. assert_select 'a[href=?]', '/issues/bulk_update?ids%5B%5D=1&issue%5Bfixed_version_id%5D=4', :text => 'eCookbook Subproject 1 - 2.0'
  49. # Assignees
  50. assert_select 'a[href=?]', '/issues/bulk_update?ids%5B%5D=1&issue%5Bassigned_to_id%5D=3', :text => 'Dave Lopper'
  51. end
  52. def test_context_menu_one_issue_by_anonymous
  53. with_settings :default_language => 'en' do
  54. get :issues, :ids => [1]
  55. assert_response :success
  56. assert_template 'context_menus/issues'
  57. assert_select 'a.icon-del.disabled[href="#"]', :text => 'Delete'
  58. end
  59. end
  60. def test_context_menu_multiple_issues_of_same_project
  61. @request.session[:user_id] = 2
  62. get :issues, :ids => [1, 2]
  63. assert_response :success
  64. assert_template 'context_menus/issues'
  65. assert_not_nil assigns(:issues)
  66. assert_equal [1, 2], assigns(:issues).map(&:id).sort
  67. ids = assigns(:issues).map(&:id).sort.map {|i| "ids%5B%5D=#{i}"}.join('&')
  68. assert_select 'a.icon-edit[href=?]', "/issues/bulk_edit?#{ids}", :text => 'Edit'
  69. assert_select 'a.icon-copy[href=?]', "/issues/bulk_edit?copy=1&#{ids}", :text => 'Copy'
  70. assert_select 'a.icon-del[href=?]', "/issues?#{ids}", :text => 'Delete'
  71. assert_select 'a[href=?]', "/issues/bulk_update?#{ids}&issue%5Bstatus_id%5D=5", :text => 'Closed'
  72. assert_select 'a[href=?]', "/issues/bulk_update?#{ids}&issue%5Bpriority_id%5D=8", :text => 'Immediate'
  73. assert_select 'a[href=?]', "/issues/bulk_update?#{ids}&issue%5Bassigned_to_id%5D=3", :text => 'Dave Lopper'
  74. end
  75. def test_context_menu_multiple_issues_of_different_projects
  76. @request.session[:user_id] = 2
  77. get :issues, :ids => [1, 2, 6]
  78. assert_response :success
  79. assert_template 'context_menus/issues'
  80. assert_not_nil assigns(:issues)
  81. assert_equal [1, 2, 6], assigns(:issues).map(&:id).sort
  82. ids = assigns(:issues).map(&:id).sort.map {|i| "ids%5B%5D=#{i}"}.join('&')
  83. assert_select 'a.icon-edit[href=?]', "/issues/bulk_edit?#{ids}", :text => 'Edit'
  84. assert_select 'a.icon-del[href=?]', "/issues?#{ids}", :text => 'Delete'
  85. assert_select 'a[href=?]', "/issues/bulk_update?#{ids}&issue%5Bstatus_id%5D=5", :text => 'Closed'
  86. assert_select 'a[href=?]', "/issues/bulk_update?#{ids}&issue%5Bpriority_id%5D=8", :text => 'Immediate'
  87. assert_select 'a[href=?]', "/issues/bulk_update?#{ids}&issue%5Bassigned_to_id%5D=2", :text => 'John Smith'
  88. end
  89. def test_context_menu_should_include_list_custom_fields
  90. field = IssueCustomField.create!(:name => 'List', :field_format => 'list',
  91. :possible_values => ['Foo', 'Bar'], :is_for_all => true, :tracker_ids => [1, 2, 3])
  92. @request.session[:user_id] = 2
  93. get :issues, :ids => [1]
  94. assert_select "li.cf_#{field.id}" do
  95. assert_select 'a[href="#"]', :text => 'List'
  96. assert_select 'ul' do
  97. assert_select 'a', 3
  98. assert_select 'a[href=?]', "/issues/bulk_update?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=Foo", :text => 'Foo'
  99. assert_select 'a[href=?]', "/issues/bulk_update?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=__none__", :text => 'none'
  100. end
  101. end
  102. end
  103. def test_context_menu_should_not_include_null_value_for_required_custom_fields
  104. field = IssueCustomField.create!(:name => 'List', :is_required => true, :field_format => 'list',
  105. :possible_values => ['Foo', 'Bar'], :is_for_all => true, :tracker_ids => [1, 2, 3])
  106. @request.session[:user_id] = 2
  107. get :issues, :ids => [1, 2]
  108. assert_select "li.cf_#{field.id}" do
  109. assert_select 'a[href="#"]', :text => 'List'
  110. assert_select 'ul' do
  111. assert_select 'a', 2
  112. assert_select 'a', :text => 'none', :count => 0
  113. end
  114. end
  115. end
  116. def test_context_menu_on_single_issue_should_select_current_custom_field_value
  117. field = IssueCustomField.create!(:name => 'List', :field_format => 'list',
  118. :possible_values => ['Foo', 'Bar'], :is_for_all => true, :tracker_ids => [1, 2, 3])
  119. issue = Issue.find(1)
  120. issue.custom_field_values = {field.id => 'Bar'}
  121. issue.save!
  122. @request.session[:user_id] = 2
  123. get :issues, :ids => [1]
  124. assert_select "li.cf_#{field.id}" do
  125. assert_select 'a[href="#"]', :text => 'List'
  126. assert_select 'ul' do
  127. assert_select 'a', 3
  128. assert_select 'a.icon-checked', :text => 'Bar'
  129. end
  130. end
  131. end
  132. def test_context_menu_should_include_bool_custom_fields
  133. field = IssueCustomField.create!(:name => 'Bool', :field_format => 'bool',
  134. :is_for_all => true, :tracker_ids => [1, 2, 3])
  135. @request.session[:user_id] = 2
  136. get :issues, :ids => [1]
  137. assert_select "li.cf_#{field.id}" do
  138. assert_select 'a[href="#"]', :text => 'Bool'
  139. assert_select 'ul' do
  140. assert_select 'a', 3
  141. assert_select 'a[href=?]', "/issues/bulk_update?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=0", :text => 'No'
  142. assert_select 'a[href=?]', "/issues/bulk_update?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=1", :text => 'Yes'
  143. assert_select 'a[href=?]', "/issues/bulk_update?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=__none__", :text => 'none'
  144. end
  145. end
  146. end
  147. def test_context_menu_should_include_user_custom_fields
  148. field = IssueCustomField.create!(:name => 'User', :field_format => 'user',
  149. :is_for_all => true, :tracker_ids => [1, 2, 3])
  150. @request.session[:user_id] = 2
  151. get :issues, :ids => [1]
  152. assert_select "li.cf_#{field.id}" do
  153. assert_select 'a[href="#"]', :text => 'User'
  154. assert_select 'ul' do
  155. assert_select 'a', Project.find(1).members.count + 1
  156. assert_select 'a[href=?]', "/issues/bulk_update?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=2", :text => 'John Smith'
  157. assert_select 'a[href=?]', "/issues/bulk_update?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=__none__", :text => 'none'
  158. end
  159. end
  160. end
  161. def test_context_menu_should_include_version_custom_fields
  162. field = IssueCustomField.create!(:name => 'Version', :field_format => 'version', :is_for_all => true, :tracker_ids => [1, 2, 3])
  163. @request.session[:user_id] = 2
  164. get :issues, :ids => [1]
  165. assert_select "li.cf_#{field.id}" do
  166. assert_select 'a[href="#"]', :text => 'Version'
  167. assert_select 'ul' do
  168. assert_select 'a', Project.find(1).shared_versions.count + 1
  169. assert_select 'a[href=?]', "/issues/bulk_update?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=3", :text => '2.0'
  170. assert_select 'a[href=?]', "/issues/bulk_update?ids%5B%5D=1&issue%5Bcustom_field_values%5D%5B#{field.id}%5D=__none__", :text => 'none'
  171. end
  172. end
  173. end
  174. def test_context_menu_should_show_enabled_custom_fields_for_the_role_only
  175. enabled_cf = IssueCustomField.generate!(:field_format => 'bool', :is_for_all => true, :tracker_ids => [1], :visible => false, :role_ids => [1,2])
  176. disabled_cf = IssueCustomField.generate!(:field_format => 'bool', :is_for_all => true, :tracker_ids => [1], :visible => false, :role_ids => [2])
  177. issue = Issue.generate!(:project_id => 1, :tracker_id => 1)
  178. @request.session[:user_id] = 2
  179. get :issues, :ids => [issue.id]
  180. assert_select "li.cf_#{enabled_cf.id}"
  181. assert_select "li.cf_#{disabled_cf.id}", 0
  182. end
  183. def test_context_menu_by_assignable_user_should_include_assigned_to_me_link
  184. @request.session[:user_id] = 2
  185. get :issues, :ids => [1]
  186. assert_response :success
  187. assert_template 'context_menus/issues'
  188. assert_select 'a[href=?]', '/issues/bulk_update?ids%5B%5D=1&issue%5Bassigned_to_id%5D=2', :text => / me /
  189. end
  190. def test_context_menu_should_propose_shared_versions_for_issues_from_different_projects
  191. @request.session[:user_id] = 2
  192. version = Version.create!(:name => 'Shared', :sharing => 'system', :project_id => 1)
  193. get :issues, :ids => [1, 4]
  194. assert_response :success
  195. assert_template 'context_menus/issues'
  196. assert_include version, assigns(:versions)
  197. assert_select 'a', :text => 'eCookbook - Shared'
  198. end
  199. def test_context_menu_with_issue_that_is_not_visible_should_fail
  200. get :issues, :ids => [1, 4] # issue 4 is not visible
  201. assert_response 302
  202. end
  203. def test_should_respond_with_404_without_ids
  204. get :issues
  205. assert_response 404
  206. end
  207. def test_time_entries_context_menu
  208. @request.session[:user_id] = 2
  209. get :time_entries, :ids => [1, 2]
  210. assert_response :success
  211. assert_template 'context_menus/time_entries'
  212. assert_select 'a:not(.disabled)', :text => 'Edit'
  213. end
  214. def test_context_menu_for_one_time_entry
  215. @request.session[:user_id] = 2
  216. get :time_entries, :ids => [1]
  217. assert_response :success
  218. assert_template 'context_menus/time_entries'
  219. assert_select 'a:not(.disabled)', :text => 'Edit'
  220. end
  221. def test_time_entries_context_menu_should_include_custom_fields
  222. field = TimeEntryCustomField.generate!(:name => "Field", :field_format => "list", :possible_values => ["foo", "bar"])
  223. @request.session[:user_id] = 2
  224. get :time_entries, :ids => [1, 2]
  225. assert_response :success
  226. assert_select "li.cf_#{field.id}" do
  227. assert_select 'a[href="#"]', :text => "Field"
  228. assert_select 'ul' do
  229. assert_select 'a', 3
  230. assert_select 'a[href=?]', "/time_entries/bulk_update?ids%5B%5D=1&ids%5B%5D=2&time_entry%5Bcustom_field_values%5D%5B#{field.id}%5D=foo", :text => 'foo'
  231. assert_select 'a[href=?]', "/time_entries/bulk_update?ids%5B%5D=1&ids%5B%5D=2&time_entry%5Bcustom_field_values%5D%5B#{field.id}%5D=bar", :text => 'bar'
  232. assert_select 'a[href=?]', "/time_entries/bulk_update?ids%5B%5D=1&ids%5B%5D=2&time_entry%5Bcustom_field_values%5D%5B#{field.id}%5D=__none__", :text => 'none'
  233. end
  234. end
  235. end
  236. def test_time_entries_context_menu_with_edit_own_time_entries_permission
  237. @request.session[:user_id] = 2
  238. Role.find_by_name('Manager').remove_permission! :edit_time_entries
  239. Role.find_by_name('Manager').add_permission! :edit_own_time_entries
  240. ids = (0..1).map {TimeEntry.generate!(:user => User.find(2)).id}
  241. get :time_entries, :ids => ids
  242. assert_response :success
  243. assert_template 'context_menus/time_entries'
  244. assert_select 'a:not(.disabled)', :text => 'Edit'
  245. end
  246. def test_time_entries_context_menu_without_edit_permission
  247. @request.session[:user_id] = 2
  248. Role.find_by_name('Manager').remove_permission! :edit_time_entries
  249. get :time_entries, :ids => [1, 2]
  250. assert_response :success
  251. assert_template 'context_menus/time_entries'
  252. assert_select 'a.disabled', :text => 'Edit'
  253. end
  254. end