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.

issue_relations_controller_test.rb 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../test_helper', __FILE__)
  19. class IssueRelationsControllerTest < Redmine::ControllerTest
  20. fixtures :projects,
  21. :users,
  22. :roles,
  23. :members,
  24. :member_roles,
  25. :issues,
  26. :issue_statuses,
  27. :issue_relations,
  28. :enabled_modules,
  29. :enumerations,
  30. :trackers,
  31. :projects_trackers
  32. def setup
  33. User.current = nil
  34. @request.session[:user_id] = 3
  35. end
  36. def test_create
  37. assert_difference 'IssueRelation.count' do
  38. post(
  39. :create,
  40. :params => {
  41. :issue_id => 1,
  42. :relation => {
  43. :issue_to_id => '2',
  44. :relation_type => 'relates',
  45. :delay => ''
  46. }
  47. }
  48. )
  49. end
  50. relation = IssueRelation.order('id DESC').first
  51. assert_equal 1, relation.issue_from_id
  52. assert_equal 2, relation.issue_to_id
  53. assert_equal 'relates', relation.relation_type
  54. end
  55. def test_create_on_invalid_issue
  56. assert_no_difference 'IssueRelation.count' do
  57. post(
  58. :create,
  59. :params => {
  60. :issue_id => 999,
  61. :relation => {
  62. :issue_to_id => '2',
  63. :relation_type => 'relates',
  64. :delay => ''
  65. }
  66. }
  67. )
  68. assert_response 404
  69. end
  70. end
  71. def test_create_xhr
  72. assert_difference 'IssueRelation.count' do
  73. post(
  74. :create,
  75. :params => {
  76. :issue_id => 3,
  77. :relation => {
  78. :issue_to_id => '1',
  79. :relation_type => 'relates',
  80. :delay => ''
  81. }
  82. },
  83. :xhr => true
  84. )
  85. assert_response :success
  86. assert_equal 'text/javascript', response.media_type
  87. end
  88. relation = IssueRelation.order('id DESC').first
  89. assert_equal 1, relation.issue_from_id
  90. assert_equal 3, relation.issue_to_id
  91. assert_include 'Bug #1', response.body
  92. end
  93. def test_create_should_accept_id_with_hash
  94. assert_difference 'IssueRelation.count' do
  95. post(
  96. :create,
  97. :params => {
  98. :issue_id => 1,
  99. :relation => {
  100. :issue_to_id => '#2',
  101. :relation_type => 'relates',
  102. :delay => ''
  103. }
  104. }
  105. )
  106. end
  107. relation = IssueRelation.order('id DESC').first
  108. assert_equal 2, relation.issue_to_id
  109. end
  110. def test_create_should_strip_id
  111. assert_difference 'IssueRelation.count' do
  112. post(
  113. :create,
  114. :params => {
  115. :issue_id => 1,
  116. :relation => {
  117. :issue_to_id => ' 2 ',
  118. :relation_type => 'relates',
  119. :delay => ''
  120. }
  121. }
  122. )
  123. end
  124. relation = IssueRelation.order('id DESC').first
  125. assert_equal 2, relation.issue_to_id
  126. end
  127. def test_create_should_not_break_with_non_numerical_id
  128. assert_no_difference 'IssueRelation.count' do
  129. assert_nothing_raised do
  130. post(
  131. :create,
  132. :params => {
  133. :issue_id => 1,
  134. :relation => {
  135. :issue_to_id => 'foo',
  136. :relation_type => 'relates',
  137. :delay => ''
  138. }
  139. }
  140. )
  141. end
  142. end
  143. end
  144. def test_create_follows_relation_should_update_relations_list
  145. issue1 = Issue.generate!(:subject => 'Followed issue',
  146. :start_date => Date.yesterday,
  147. :due_date => Date.today)
  148. issue2 = Issue.generate!
  149. assert_difference 'IssueRelation.count' do
  150. post(
  151. :create,
  152. :params => {
  153. :issue_id => issue2.id,
  154. :relation => {
  155. :issue_to_id => issue1.id,
  156. :relation_type => 'follows',
  157. :delay => ''
  158. }
  159. },
  160. :xhr => true
  161. )
  162. end
  163. assert_include 'Followed issue', response.body
  164. end
  165. def test_should_create_relations_with_visible_issues_only
  166. with_settings :cross_project_issue_relations => '1' do
  167. assert_nil Issue.visible(User.find(3)).find_by_id(4)
  168. assert_no_difference 'IssueRelation.count' do
  169. post(
  170. :create,
  171. :params => {
  172. :issue_id => 1,
  173. :relation => {
  174. :issue_to_id => '4',
  175. :relation_type => 'relates',
  176. :delay => ''
  177. }
  178. }
  179. )
  180. end
  181. end
  182. end
  183. def test_create_xhr_with_failure
  184. assert_no_difference 'IssueRelation.count' do
  185. post(
  186. :create,
  187. :params => {
  188. :issue_id => 3,
  189. :relation => {
  190. :issue_to_id => '999',
  191. :relation_type => 'relates',
  192. :delay => ''
  193. }
  194. },
  195. :xhr => true
  196. )
  197. assert_response :success
  198. assert_equal 'text/javascript', response.media_type
  199. end
  200. assert_include 'Related issue cannot be blank', response.body
  201. end
  202. def test_create_duplicated_follows_relations_should_not_raise_exception
  203. IssueRelation.create(
  204. :issue_from => Issue.find(1), :issue_to => Issue.find(2),
  205. :relation_type => IssueRelation::TYPE_PRECEDES
  206. )
  207. assert_no_difference 'IssueRelation.count' do
  208. post(
  209. :create,
  210. :params => {
  211. :issue_id => 2,
  212. :relation => {
  213. :issue_to_id => 1,
  214. :relation_type => 'follows',
  215. :delay => ''
  216. }
  217. },
  218. :xhr => true
  219. )
  220. end
  221. assert_response :success
  222. assert_include 'has already been taken', response.body
  223. end
  224. def test_bulk_create_with_multiple_issue_to_id_issues
  225. assert_difference 'IssueRelation.count', +3 do
  226. post(
  227. :create,
  228. :params => {
  229. :issue_id => 1,
  230. :relation => {
  231. # js autocomplete adds a comma at the end
  232. # issue to id should accept both id and hash with id
  233. :issue_to_id => '2,3,#7, ',
  234. :relation_type => 'relates',
  235. :delay => ''
  236. }
  237. },
  238. :xhr => true
  239. )
  240. end
  241. assert_response :success
  242. relations = IssueRelation.where(:issue_from_id => 1, :issue_to_id => [2, 3, 7])
  243. assert_equal 3, relations.count
  244. # all relations types should be 'relates'
  245. relations.map {|r| assert_equal 'relates', r.relation_type}
  246. # no error messages should be returned in the response
  247. assert_not_include 'id=\"errorExplanation\"', response.body
  248. end
  249. def test_bulk_create_should_show_errors
  250. with_settings :cross_project_issue_relations => '0' do
  251. assert_difference 'IssueRelation.count', +3 do
  252. post(
  253. :create,
  254. :params => {
  255. :issue_id => 1,
  256. :relation => {
  257. :issue_to_id => '1,2,3,4,5,7',
  258. :relation_type => 'relates',
  259. :delay => ''
  260. }
  261. },
  262. :xhr => true
  263. )
  264. end
  265. end
  266. assert_response :success
  267. assert_equal 'text/javascript', response.media_type
  268. # issue #1 is invalid
  269. assert_include 'Related issue is invalid: #1', response.body
  270. # issues #4 and #5 can't be related by default
  271. assert_include 'Related issue cannot be blank', response.body
  272. assert_include 'Related issue doesn&#39;t belong to the same project', response.body
  273. end
  274. def test_destroy
  275. assert_difference 'IssueRelation.count', -1 do
  276. delete(:destroy, :params => {:id => '2'})
  277. end
  278. end
  279. def test_destroy_invalid_relation
  280. assert_no_difference 'IssueRelation.count' do
  281. delete(:destroy, :params => {:id => '999'})
  282. assert_response 404
  283. end
  284. end
  285. def test_destroy_xhr
  286. IssueRelation.create!(:relation_type => IssueRelation::TYPE_RELATES) do |r|
  287. r.issue_from_id = 3
  288. r.issue_to_id = 1
  289. end
  290. assert_difference 'IssueRelation.count', -1 do
  291. delete(:destroy, :params => {:id => '2'}, :xhr => true)
  292. assert_response :success
  293. assert_equal 'text/javascript', response.media_type
  294. assert_include 'relation-2', response.body
  295. end
  296. end
  297. end