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 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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 IssueRelationsControllerTest < Redmine::ControllerTest
  19. fixtures :projects,
  20. :users,
  21. :roles,
  22. :members,
  23. :member_roles,
  24. :issues,
  25. :issue_statuses,
  26. :issue_relations,
  27. :enabled_modules,
  28. :enumerations,
  29. :trackers,
  30. :projects_trackers
  31. def setup
  32. User.current = nil
  33. @request.session[:user_id] = 3
  34. end
  35. def test_create
  36. assert_difference 'IssueRelation.count' do
  37. post :create, :params => {
  38. :issue_id => 1,
  39. :relation => {
  40. :issue_to_id => '2',
  41. :relation_type => 'relates',
  42. :delay => ''
  43. }
  44. }
  45. end
  46. relation = IssueRelation.order('id DESC').first
  47. assert_equal 1, relation.issue_from_id
  48. assert_equal 2, relation.issue_to_id
  49. assert_equal 'relates', relation.relation_type
  50. end
  51. def test_create_on_invalid_issue
  52. assert_no_difference 'IssueRelation.count' do
  53. post :create, :params => {
  54. :issue_id => 999,
  55. :relation => {
  56. :issue_to_id => '2',
  57. :relation_type => 'relates',
  58. :delay => ''
  59. }
  60. }
  61. assert_response 404
  62. end
  63. end
  64. def test_create_xhr
  65. assert_difference 'IssueRelation.count' do
  66. post :create, :params => {
  67. :issue_id => 3,
  68. :relation => {
  69. :issue_to_id => '1',
  70. :relation_type => 'relates',
  71. :delay => ''
  72. }
  73. },
  74. :xhr => true
  75. assert_response :success
  76. assert_equal 'text/javascript', response.content_type
  77. end
  78. relation = IssueRelation.order('id DESC').first
  79. assert_equal 1, relation.issue_from_id
  80. assert_equal 3, relation.issue_to_id
  81. assert_include 'Bug #1', response.body
  82. end
  83. def test_create_should_accept_id_with_hash
  84. assert_difference 'IssueRelation.count' do
  85. post :create, :params => {
  86. :issue_id => 1,
  87. :relation => {
  88. :issue_to_id => '#2',
  89. :relation_type => 'relates',
  90. :delay => ''
  91. }
  92. }
  93. end
  94. relation = IssueRelation.order('id DESC').first
  95. assert_equal 2, relation.issue_to_id
  96. end
  97. def test_create_should_strip_id
  98. assert_difference 'IssueRelation.count' do
  99. post :create, :params => {
  100. :issue_id => 1,
  101. :relation => {
  102. :issue_to_id => ' 2 ',
  103. :relation_type => 'relates',
  104. :delay => ''
  105. }
  106. }
  107. end
  108. relation = IssueRelation.order('id DESC').first
  109. assert_equal 2, relation.issue_to_id
  110. end
  111. def test_create_should_not_break_with_non_numerical_id
  112. assert_no_difference 'IssueRelation.count' do
  113. assert_nothing_raised do
  114. post :create, :params => {
  115. :issue_id => 1,
  116. :relation => {
  117. :issue_to_id => 'foo',
  118. :relation_type => 'relates',
  119. :delay => ''
  120. }
  121. }
  122. end
  123. end
  124. end
  125. def test_create_follows_relation_should_update_relations_list
  126. issue1 = Issue.generate!(:subject => 'Followed issue', :start_date => Date.yesterday, :due_date => Date.today)
  127. issue2 = Issue.generate!
  128. assert_difference 'IssueRelation.count' do
  129. post :create, :params => {
  130. :issue_id => issue2.id,
  131. :relation => {
  132. :issue_to_id => issue1.id,
  133. :relation_type => 'follows',
  134. :delay => ''
  135. }
  136. },
  137. :xhr => true
  138. end
  139. assert_include 'Followed issue', response.body
  140. end
  141. def test_should_create_relations_with_visible_issues_only
  142. Setting.cross_project_issue_relations = '1'
  143. assert_nil Issue.visible(User.find(3)).find_by_id(4)
  144. assert_no_difference 'IssueRelation.count' do
  145. post :create, :params => {
  146. :issue_id => 1,
  147. :relation => {
  148. :issue_to_id => '4',
  149. :relation_type => 'relates',
  150. :delay => ''
  151. }
  152. }
  153. end
  154. end
  155. def test_create_xhr_with_failure
  156. assert_no_difference 'IssueRelation.count' do
  157. post :create, :params => {
  158. :issue_id => 3,
  159. :relation => {
  160. :issue_to_id => '999',
  161. :relation_type => 'relates',
  162. :delay => ''
  163. }
  164. },
  165. :xhr => true
  166. assert_response :success
  167. assert_equal 'text/javascript', response.content_type
  168. end
  169. assert_include 'Related issue cannot be blank', response.body
  170. end
  171. def test_destroy
  172. assert_difference 'IssueRelation.count', -1 do
  173. delete :destroy, :params => {
  174. :id => '2'
  175. }
  176. end
  177. end
  178. def test_destroy_invalid_relation
  179. assert_no_difference 'IssueRelation.count' do
  180. delete :destroy, :params => {
  181. :id => '999'
  182. }
  183. assert_response 404
  184. end
  185. end
  186. def test_destroy_xhr
  187. IssueRelation.create!(:relation_type => IssueRelation::TYPE_RELATES) do |r|
  188. r.issue_from_id = 3
  189. r.issue_to_id = 1
  190. end
  191. assert_difference 'IssueRelation.count', -1 do
  192. delete :destroy, :params => {
  193. :id => '2'
  194. },
  195. :xhr => true
  196. assert_response :success
  197. assert_equal 'text/javascript', response.content_type
  198. assert_include 'relation-2', response.body
  199. end
  200. end
  201. end