##// END OF EJS Templates
Strip issue id when adding a relation....
Jean-Philippe Lang -
r9250:12b71ebc8e9d
parent child
Show More
@@ -1,95 +1,95
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class IssueRelationsController < ApplicationController
18 class IssueRelationsController < ApplicationController
19 before_filter :find_issue, :find_project_from_association, :authorize, :only => [:index, :create]
19 before_filter :find_issue, :find_project_from_association, :authorize, :only => [:index, :create]
20 before_filter :find_relation, :except => [:index, :create]
20 before_filter :find_relation, :except => [:index, :create]
21
21
22 accept_api_auth :index, :show, :create, :destroy
22 accept_api_auth :index, :show, :create, :destroy
23
23
24 def index
24 def index
25 @relations = @issue.relations
25 @relations = @issue.relations
26
26
27 respond_to do |format|
27 respond_to do |format|
28 format.html { render :nothing => true }
28 format.html { render :nothing => true }
29 format.api
29 format.api
30 end
30 end
31 end
31 end
32
32
33 def show
33 def show
34 raise Unauthorized unless @relation.visible?
34 raise Unauthorized unless @relation.visible?
35
35
36 respond_to do |format|
36 respond_to do |format|
37 format.html { render :nothing => true }
37 format.html { render :nothing => true }
38 format.api
38 format.api
39 end
39 end
40 end
40 end
41
41
42 def create
42 def create
43 @relation = IssueRelation.new(params[:relation])
43 @relation = IssueRelation.new(params[:relation])
44 @relation.issue_from = @issue
44 @relation.issue_from = @issue
45 if params[:relation] && m = params[:relation][:issue_to_id].to_s.match(/^#?(\d+)$/)
45 if params[:relation] && m = params[:relation][:issue_to_id].to_s.strip.match(/^#?(\d+)$/)
46 @relation.issue_to = Issue.visible.find_by_id(m[1].to_i)
46 @relation.issue_to = Issue.visible.find_by_id(m[1].to_i)
47 end
47 end
48 saved = @relation.save
48 saved = @relation.save
49
49
50 respond_to do |format|
50 respond_to do |format|
51 format.html { redirect_to :controller => 'issues', :action => 'show', :id => @issue }
51 format.html { redirect_to :controller => 'issues', :action => 'show', :id => @issue }
52 format.js do
52 format.js do
53 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
53 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
54 render :update do |page|
54 render :update do |page|
55 page.replace_html "relations", :partial => 'issues/relations'
55 page.replace_html "relations", :partial => 'issues/relations'
56 if @relation.errors.empty?
56 if @relation.errors.empty?
57 page << "$('relation_delay').value = ''"
57 page << "$('relation_delay').value = ''"
58 page << "$('relation_issue_to_id').value = ''"
58 page << "$('relation_issue_to_id').value = ''"
59 end
59 end
60 end
60 end
61 end
61 end
62 format.api {
62 format.api {
63 if saved
63 if saved
64 render :action => 'show', :status => :created, :location => relation_url(@relation)
64 render :action => 'show', :status => :created, :location => relation_url(@relation)
65 else
65 else
66 render_validation_errors(@relation)
66 render_validation_errors(@relation)
67 end
67 end
68 }
68 }
69 end
69 end
70 end
70 end
71
71
72 def destroy
72 def destroy
73 raise Unauthorized unless @relation.deletable?
73 raise Unauthorized unless @relation.deletable?
74 @relation.destroy
74 @relation.destroy
75
75
76 respond_to do |format|
76 respond_to do |format|
77 format.html { redirect_to issue_path } # TODO : does this really work since @issue is always nil? What is it useful to?
77 format.html { redirect_to issue_path } # TODO : does this really work since @issue is always nil? What is it useful to?
78 format.js { render(:update) {|page| page.remove "relation-#{@relation.id}"} }
78 format.js { render(:update) {|page| page.remove "relation-#{@relation.id}"} }
79 format.api { head :ok }
79 format.api { head :ok }
80 end
80 end
81 end
81 end
82
82
83 private
83 private
84 def find_issue
84 def find_issue
85 @issue = @object = Issue.find(params[:issue_id])
85 @issue = @object = Issue.find(params[:issue_id])
86 rescue ActiveRecord::RecordNotFound
86 rescue ActiveRecord::RecordNotFound
87 render_404
87 render_404
88 end
88 end
89
89
90 def find_relation
90 def find_relation
91 @relation = IssueRelation.find(params[:id])
91 @relation = IssueRelation.find(params[:id])
92 rescue ActiveRecord::RecordNotFound
92 rescue ActiveRecord::RecordNotFound
93 render_404
93 render_404
94 end
94 end
95 end
95 end
@@ -1,116 +1,126
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19 require 'issue_relations_controller'
19 require 'issue_relations_controller'
20
20
21 # Re-raise errors caught by the controller.
21 # Re-raise errors caught by the controller.
22 class IssueRelationsController; def rescue_action(e) raise e end; end
22 class IssueRelationsController; def rescue_action(e) raise e end; end
23
23
24
24
25 class IssueRelationsControllerTest < ActionController::TestCase
25 class IssueRelationsControllerTest < ActionController::TestCase
26 fixtures :projects,
26 fixtures :projects,
27 :users,
27 :users,
28 :roles,
28 :roles,
29 :members,
29 :members,
30 :member_roles,
30 :member_roles,
31 :issues,
31 :issues,
32 :issue_statuses,
32 :issue_statuses,
33 :issue_relations,
33 :issue_relations,
34 :enabled_modules,
34 :enabled_modules,
35 :enumerations,
35 :enumerations,
36 :trackers
36 :trackers
37
37
38 def setup
38 def setup
39 @controller = IssueRelationsController.new
39 @controller = IssueRelationsController.new
40 @request = ActionController::TestRequest.new
40 @request = ActionController::TestRequest.new
41 @response = ActionController::TestResponse.new
41 @response = ActionController::TestResponse.new
42 User.current = nil
42 User.current = nil
43 end
43 end
44
44
45 def test_create
45 def test_create
46 assert_difference 'IssueRelation.count' do
46 assert_difference 'IssueRelation.count' do
47 @request.session[:user_id] = 3
47 @request.session[:user_id] = 3
48 post :create, :issue_id => 1,
48 post :create, :issue_id => 1,
49 :relation => {:issue_to_id => '2', :relation_type => 'relates', :delay => ''}
49 :relation => {:issue_to_id => '2', :relation_type => 'relates', :delay => ''}
50 end
50 end
51 end
51 end
52
52
53 def test_create_xhr
53 def test_create_xhr
54 assert_difference 'IssueRelation.count' do
54 assert_difference 'IssueRelation.count' do
55 @request.session[:user_id] = 3
55 @request.session[:user_id] = 3
56 xhr :post, :create,
56 xhr :post, :create,
57 :issue_id => 3,
57 :issue_id => 3,
58 :relation => {:issue_to_id => '1', :relation_type => 'relates', :delay => ''}
58 :relation => {:issue_to_id => '1', :relation_type => 'relates', :delay => ''}
59 assert_select_rjs 'relations' do
59 assert_select_rjs 'relations' do
60 assert_select 'table', 1
60 assert_select 'table', 1
61 assert_select 'tr', 2 # relations
61 assert_select 'tr', 2 # relations
62 end
62 end
63 end
63 end
64 end
64 end
65
65
66 def test_create_should_accept_id_with_hash
66 def test_create_should_accept_id_with_hash
67 assert_difference 'IssueRelation.count' do
67 assert_difference 'IssueRelation.count' do
68 @request.session[:user_id] = 3
68 @request.session[:user_id] = 3
69 post :create, :issue_id => 1,
69 post :create, :issue_id => 1,
70 :relation => {:issue_to_id => '#2', :relation_type => 'relates', :delay => ''}
70 :relation => {:issue_to_id => '#2', :relation_type => 'relates', :delay => ''}
71 end
71 end
72 end
72 end
73
73
74 def test_create_should_strip_id
75 assert_difference 'IssueRelation.count' do
76 @request.session[:user_id] = 3
77 post :create, :issue_id => 1,
78 :relation => {:issue_to_id => ' 2 ', :relation_type => 'relates', :delay => ''}
79 end
80 relation = IssueRelation.first(:order => 'id DESC')
81 assert_equal 2, relation.issue_to_id
82 end
83
74 def test_create_should_not_break_with_non_numerical_id
84 def test_create_should_not_break_with_non_numerical_id
75 assert_no_difference 'IssueRelation.count' do
85 assert_no_difference 'IssueRelation.count' do
76 assert_nothing_raised do
86 assert_nothing_raised do
77 @request.session[:user_id] = 3
87 @request.session[:user_id] = 3
78 post :create, :issue_id => 1,
88 post :create, :issue_id => 1,
79 :relation => {:issue_to_id => 'foo', :relation_type => 'relates', :delay => ''}
89 :relation => {:issue_to_id => 'foo', :relation_type => 'relates', :delay => ''}
80 end
90 end
81 end
91 end
82 end
92 end
83
93
84 def test_should_create_relations_with_visible_issues_only
94 def test_should_create_relations_with_visible_issues_only
85 Setting.cross_project_issue_relations = '1'
95 Setting.cross_project_issue_relations = '1'
86 assert_nil Issue.visible(User.find(3)).find_by_id(4)
96 assert_nil Issue.visible(User.find(3)).find_by_id(4)
87
97
88 assert_no_difference 'IssueRelation.count' do
98 assert_no_difference 'IssueRelation.count' do
89 @request.session[:user_id] = 3
99 @request.session[:user_id] = 3
90 post :create, :issue_id => 1,
100 post :create, :issue_id => 1,
91 :relation => {:issue_to_id => '4', :relation_type => 'relates', :delay => ''}
101 :relation => {:issue_to_id => '4', :relation_type => 'relates', :delay => ''}
92 end
102 end
93 end
103 end
94
104
95 should "prevent relation creation when there's a circular dependency"
105 should "prevent relation creation when there's a circular dependency"
96
106
97 def test_destroy
107 def test_destroy
98 assert_difference 'IssueRelation.count', -1 do
108 assert_difference 'IssueRelation.count', -1 do
99 @request.session[:user_id] = 3
109 @request.session[:user_id] = 3
100 delete :destroy, :id => '2'
110 delete :destroy, :id => '2'
101 end
111 end
102 end
112 end
103
113
104 def test_destroy_xhr
114 def test_destroy_xhr
105 IssueRelation.create!(:relation_type => IssueRelation::TYPE_RELATES) do |r|
115 IssueRelation.create!(:relation_type => IssueRelation::TYPE_RELATES) do |r|
106 r.issue_from_id = 3
116 r.issue_from_id = 3
107 r.issue_to_id = 1
117 r.issue_to_id = 1
108 end
118 end
109
119
110 assert_difference 'IssueRelation.count', -1 do
120 assert_difference 'IssueRelation.count', -1 do
111 @request.session[:user_id] = 3
121 @request.session[:user_id] = 3
112 xhr :delete, :destroy, :id => '2'
122 xhr :delete, :destroy, :id => '2'
113 assert_select_rjs :remove, 'relation-2'
123 assert_select_rjs :remove, 'relation-2'
114 end
124 end
115 end
125 end
116 end
126 end
General Comments 0
You need to be logged in to leave comments. Login now