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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2016 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, :issue_id => 1,
  38. :relation => {:issue_to_id => '2', :relation_type => 'relates', :delay => ''}
  39. end
  40. relation = IssueRelation.order('id DESC').first
  41. assert_equal 1, relation.issue_from_id
  42. assert_equal 2, relation.issue_to_id
  43. assert_equal 'relates', relation.relation_type
  44. end
  45. def test_create_on_invalid_issue
  46. assert_no_difference 'IssueRelation.count' do
  47. post :create, :issue_id => 999,
  48. :relation => {:issue_to_id => '2', :relation_type => 'relates', :delay => ''}
  49. assert_response 404
  50. end
  51. end
  52. def test_create_xhr
  53. assert_difference 'IssueRelation.count' do
  54. xhr :post, :create, :issue_id => 3, :relation => {:issue_to_id => '1', :relation_type => 'relates', :delay => ''}
  55. assert_response :success
  56. assert_equal 'text/javascript', response.content_type
  57. end
  58. relation = IssueRelation.order('id DESC').first
  59. assert_equal 3, relation.issue_from_id
  60. assert_equal 1, relation.issue_to_id
  61. assert_include 'Bug #1', response.body
  62. end
  63. def test_create_should_accept_id_with_hash
  64. assert_difference 'IssueRelation.count' do
  65. post :create, :issue_id => 1,
  66. :relation => {:issue_to_id => '#2', :relation_type => 'relates', :delay => ''}
  67. end
  68. relation = IssueRelation.order('id DESC').first
  69. assert_equal 2, relation.issue_to_id
  70. end
  71. def test_create_should_strip_id
  72. assert_difference 'IssueRelation.count' do
  73. post :create, :issue_id => 1,
  74. :relation => {:issue_to_id => ' 2 ', :relation_type => 'relates', :delay => ''}
  75. end
  76. relation = IssueRelation.order('id DESC').first
  77. assert_equal 2, relation.issue_to_id
  78. end
  79. def test_create_should_not_break_with_non_numerical_id
  80. assert_no_difference 'IssueRelation.count' do
  81. assert_nothing_raised do
  82. post :create, :issue_id => 1,
  83. :relation => {:issue_to_id => 'foo', :relation_type => 'relates', :delay => ''}
  84. end
  85. end
  86. end
  87. def test_create_follows_relation_should_update_relations_list
  88. issue1 = Issue.generate!(:subject => 'Followed issue', :start_date => Date.yesterday, :due_date => Date.today)
  89. issue2 = Issue.generate!
  90. assert_difference 'IssueRelation.count' do
  91. xhr :post, :create, :issue_id => issue2.id,
  92. :relation => {:issue_to_id => issue1.id, :relation_type => 'follows', :delay => ''}
  93. end
  94. assert_include 'Followed issue', response.body
  95. end
  96. def test_should_create_relations_with_visible_issues_only
  97. Setting.cross_project_issue_relations = '1'
  98. assert_nil Issue.visible(User.find(3)).find_by_id(4)
  99. assert_no_difference 'IssueRelation.count' do
  100. post :create, :issue_id => 1,
  101. :relation => {:issue_to_id => '4', :relation_type => 'relates', :delay => ''}
  102. end
  103. end
  104. def test_create_xhr_with_failure
  105. assert_no_difference 'IssueRelation.count' do
  106. xhr :post, :create, :issue_id => 3, :relation => {:issue_to_id => '999', :relation_type => 'relates', :delay => ''}
  107. assert_response :success
  108. assert_equal 'text/javascript', response.content_type
  109. end
  110. assert_include 'Related issue cannot be blank', response.body
  111. end
  112. def test_destroy
  113. assert_difference 'IssueRelation.count', -1 do
  114. delete :destroy, :id => '2'
  115. end
  116. end
  117. def test_destroy_invalid_relation
  118. assert_no_difference 'IssueRelation.count' do
  119. delete :destroy, :id => '999'
  120. assert_response 404
  121. end
  122. end
  123. def test_destroy_xhr
  124. IssueRelation.create!(:relation_type => IssueRelation::TYPE_RELATES) do |r|
  125. r.issue_from_id = 3
  126. r.issue_to_id = 1
  127. end
  128. assert_difference 'IssueRelation.count', -1 do
  129. xhr :delete, :destroy, :id => '2'
  130. assert_response :success
  131. assert_equal 'text/javascript', response.content_type
  132. assert_include 'relation-2', response.body
  133. end
  134. end
  135. end