@@ -1,179 +1,180 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2015 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 |
|
19 | |||
20 | class Redmine::Hook::ManagerTest < ActionView::TestCase |
|
20 | class Redmine::Hook::ManagerTest < ActionView::TestCase | |
21 | fixtures :projects, :users, :members, :member_roles, :roles, |
|
21 | fixtures :projects, :users, :members, :member_roles, :roles, | |
22 | :groups_users, |
|
22 | :groups_users, | |
|
23 | :email_addresses, | |||
23 | :trackers, :projects_trackers, |
|
24 | :trackers, :projects_trackers, | |
24 | :enabled_modules, |
|
25 | :enabled_modules, | |
25 | :versions, |
|
26 | :versions, | |
26 | :issue_statuses, :issue_categories, :issue_relations, |
|
27 | :issue_statuses, :issue_categories, :issue_relations, | |
27 | :enumerations, |
|
28 | :enumerations, | |
28 | :issues |
|
29 | :issues | |
29 |
|
30 | |||
30 | # Some hooks that are manually registered in these tests |
|
31 | # Some hooks that are manually registered in these tests | |
31 | class TestHook < Redmine::Hook::ViewListener; end |
|
32 | class TestHook < Redmine::Hook::ViewListener; end | |
32 |
|
33 | |||
33 | class TestHook1 < TestHook |
|
34 | class TestHook1 < TestHook | |
34 | def view_layouts_base_html_head(context) |
|
35 | def view_layouts_base_html_head(context) | |
35 | 'Test hook 1 listener.' |
|
36 | 'Test hook 1 listener.' | |
36 | end |
|
37 | end | |
37 | end |
|
38 | end | |
38 |
|
39 | |||
39 | class TestHook2 < TestHook |
|
40 | class TestHook2 < TestHook | |
40 | def view_layouts_base_html_head(context) |
|
41 | def view_layouts_base_html_head(context) | |
41 | 'Test hook 2 listener.' |
|
42 | 'Test hook 2 listener.' | |
42 | end |
|
43 | end | |
43 | end |
|
44 | end | |
44 |
|
45 | |||
45 | class TestHook3 < TestHook |
|
46 | class TestHook3 < TestHook | |
46 | def view_layouts_base_html_head(context) |
|
47 | def view_layouts_base_html_head(context) | |
47 | "Context keys: #{context.keys.collect(&:to_s).sort.join(', ')}." |
|
48 | "Context keys: #{context.keys.collect(&:to_s).sort.join(', ')}." | |
48 | end |
|
49 | end | |
49 | end |
|
50 | end | |
50 |
|
51 | |||
51 | class TestLinkToHook < TestHook |
|
52 | class TestLinkToHook < TestHook | |
52 | def view_layouts_base_html_head(context) |
|
53 | def view_layouts_base_html_head(context) | |
53 | link_to('Issues', :controller => 'issues') |
|
54 | link_to('Issues', :controller => 'issues') | |
54 | end |
|
55 | end | |
55 | end |
|
56 | end | |
56 |
|
57 | |||
57 | class TestHookHelperController < ActionController::Base |
|
58 | class TestHookHelperController < ActionController::Base | |
58 | include Redmine::Hook::Helper |
|
59 | include Redmine::Hook::Helper | |
59 | end |
|
60 | end | |
60 |
|
61 | |||
61 | class TestHookHelperView < ActionView::Base |
|
62 | class TestHookHelperView < ActionView::Base | |
62 | include Redmine::Hook::Helper |
|
63 | include Redmine::Hook::Helper | |
63 | end |
|
64 | end | |
64 |
|
65 | |||
65 | Redmine::Hook.clear_listeners |
|
66 | Redmine::Hook.clear_listeners | |
66 |
|
67 | |||
67 | def setup |
|
68 | def setup | |
68 | @hook_module = Redmine::Hook |
|
69 | @hook_module = Redmine::Hook | |
69 | @hook_module.clear_listeners |
|
70 | @hook_module.clear_listeners | |
70 | end |
|
71 | end | |
71 |
|
72 | |||
72 | def teardown |
|
73 | def teardown | |
73 | @hook_module.clear_listeners |
|
74 | @hook_module.clear_listeners | |
74 | end |
|
75 | end | |
75 |
|
76 | |||
76 | def test_clear_listeners |
|
77 | def test_clear_listeners | |
77 | assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size |
|
78 | assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size | |
78 | @hook_module.add_listener(TestHook1) |
|
79 | @hook_module.add_listener(TestHook1) | |
79 | @hook_module.add_listener(TestHook2) |
|
80 | @hook_module.add_listener(TestHook2) | |
80 | assert_equal 2, @hook_module.hook_listeners(:view_layouts_base_html_head).size |
|
81 | assert_equal 2, @hook_module.hook_listeners(:view_layouts_base_html_head).size | |
81 |
|
82 | |||
82 | @hook_module.clear_listeners |
|
83 | @hook_module.clear_listeners | |
83 | assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size |
|
84 | assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size | |
84 | end |
|
85 | end | |
85 |
|
86 | |||
86 | def test_add_listener |
|
87 | def test_add_listener | |
87 | assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size |
|
88 | assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size | |
88 | @hook_module.add_listener(TestHook1) |
|
89 | @hook_module.add_listener(TestHook1) | |
89 | assert_equal 1, @hook_module.hook_listeners(:view_layouts_base_html_head).size |
|
90 | assert_equal 1, @hook_module.hook_listeners(:view_layouts_base_html_head).size | |
90 | end |
|
91 | end | |
91 |
|
92 | |||
92 | def test_call_hook |
|
93 | def test_call_hook | |
93 | @hook_module.add_listener(TestHook1) |
|
94 | @hook_module.add_listener(TestHook1) | |
94 | assert_equal ['Test hook 1 listener.'], hook_helper.call_hook(:view_layouts_base_html_head) |
|
95 | assert_equal ['Test hook 1 listener.'], hook_helper.call_hook(:view_layouts_base_html_head) | |
95 | end |
|
96 | end | |
96 |
|
97 | |||
97 | def test_call_hook_with_context |
|
98 | def test_call_hook_with_context | |
98 | @hook_module.add_listener(TestHook3) |
|
99 | @hook_module.add_listener(TestHook3) | |
99 | assert_equal ['Context keys: bar, controller, foo, hook_caller, project, request.'], |
|
100 | assert_equal ['Context keys: bar, controller, foo, hook_caller, project, request.'], | |
100 | hook_helper.call_hook(:view_layouts_base_html_head, :foo => 1, :bar => 'a') |
|
101 | hook_helper.call_hook(:view_layouts_base_html_head, :foo => 1, :bar => 'a') | |
101 | end |
|
102 | end | |
102 |
|
103 | |||
103 | def test_call_hook_with_multiple_listeners |
|
104 | def test_call_hook_with_multiple_listeners | |
104 | @hook_module.add_listener(TestHook1) |
|
105 | @hook_module.add_listener(TestHook1) | |
105 | @hook_module.add_listener(TestHook2) |
|
106 | @hook_module.add_listener(TestHook2) | |
106 | assert_equal ['Test hook 1 listener.', 'Test hook 2 listener.'], hook_helper.call_hook(:view_layouts_base_html_head) |
|
107 | assert_equal ['Test hook 1 listener.', 'Test hook 2 listener.'], hook_helper.call_hook(:view_layouts_base_html_head) | |
107 | end |
|
108 | end | |
108 |
|
109 | |||
109 | # Context: Redmine::Hook::Helper.call_hook default_url |
|
110 | # Context: Redmine::Hook::Helper.call_hook default_url | |
110 | def test_call_hook_default_url_options |
|
111 | def test_call_hook_default_url_options | |
111 | @hook_module.add_listener(TestLinkToHook) |
|
112 | @hook_module.add_listener(TestLinkToHook) | |
112 |
|
113 | |||
113 | assert_equal ['<a href="/issues">Issues</a>'], hook_helper.call_hook(:view_layouts_base_html_head) |
|
114 | assert_equal ['<a href="/issues">Issues</a>'], hook_helper.call_hook(:view_layouts_base_html_head) | |
114 | end |
|
115 | end | |
115 |
|
116 | |||
116 | # Context: Redmine::Hook::Helper.call_hook |
|
117 | # Context: Redmine::Hook::Helper.call_hook | |
117 | def test_call_hook_with_project_added_to_context |
|
118 | def test_call_hook_with_project_added_to_context | |
118 | @hook_module.add_listener(TestHook3) |
|
119 | @hook_module.add_listener(TestHook3) | |
119 | assert_match /project/i, hook_helper.call_hook(:view_layouts_base_html_head)[0] |
|
120 | assert_match /project/i, hook_helper.call_hook(:view_layouts_base_html_head)[0] | |
120 | end |
|
121 | end | |
121 |
|
122 | |||
122 | def test_call_hook_from_controller_with_controller_added_to_context |
|
123 | def test_call_hook_from_controller_with_controller_added_to_context | |
123 | @hook_module.add_listener(TestHook3) |
|
124 | @hook_module.add_listener(TestHook3) | |
124 | assert_match /controller/i, hook_helper.call_hook(:view_layouts_base_html_head)[0] |
|
125 | assert_match /controller/i, hook_helper.call_hook(:view_layouts_base_html_head)[0] | |
125 | end |
|
126 | end | |
126 |
|
127 | |||
127 | def test_call_hook_from_controller_with_request_added_to_context |
|
128 | def test_call_hook_from_controller_with_request_added_to_context | |
128 | @hook_module.add_listener(TestHook3) |
|
129 | @hook_module.add_listener(TestHook3) | |
129 | assert_match /request/i, hook_helper.call_hook(:view_layouts_base_html_head)[0] |
|
130 | assert_match /request/i, hook_helper.call_hook(:view_layouts_base_html_head)[0] | |
130 | end |
|
131 | end | |
131 |
|
132 | |||
132 | def test_call_hook_from_view_with_project_added_to_context |
|
133 | def test_call_hook_from_view_with_project_added_to_context | |
133 | @hook_module.add_listener(TestHook3) |
|
134 | @hook_module.add_listener(TestHook3) | |
134 | assert_match /project/i, view_hook_helper.call_hook(:view_layouts_base_html_head) |
|
135 | assert_match /project/i, view_hook_helper.call_hook(:view_layouts_base_html_head) | |
135 | end |
|
136 | end | |
136 |
|
137 | |||
137 | def test_call_hook_from_view_with_controller_added_to_context |
|
138 | def test_call_hook_from_view_with_controller_added_to_context | |
138 | @hook_module.add_listener(TestHook3) |
|
139 | @hook_module.add_listener(TestHook3) | |
139 | assert_match /controller/i, view_hook_helper.call_hook(:view_layouts_base_html_head) |
|
140 | assert_match /controller/i, view_hook_helper.call_hook(:view_layouts_base_html_head) | |
140 | end |
|
141 | end | |
141 |
|
142 | |||
142 | def test_call_hook_from_view_with_request_added_to_context |
|
143 | def test_call_hook_from_view_with_request_added_to_context | |
143 | @hook_module.add_listener(TestHook3) |
|
144 | @hook_module.add_listener(TestHook3) | |
144 | assert_match /request/i, view_hook_helper.call_hook(:view_layouts_base_html_head) |
|
145 | assert_match /request/i, view_hook_helper.call_hook(:view_layouts_base_html_head) | |
145 | end |
|
146 | end | |
146 |
|
147 | |||
147 | def test_call_hook_from_view_should_join_responses_with_a_space |
|
148 | def test_call_hook_from_view_should_join_responses_with_a_space | |
148 | @hook_module.add_listener(TestHook1) |
|
149 | @hook_module.add_listener(TestHook1) | |
149 | @hook_module.add_listener(TestHook2) |
|
150 | @hook_module.add_listener(TestHook2) | |
150 | assert_equal 'Test hook 1 listener. Test hook 2 listener.', |
|
151 | assert_equal 'Test hook 1 listener. Test hook 2 listener.', | |
151 | view_hook_helper.call_hook(:view_layouts_base_html_head) |
|
152 | view_hook_helper.call_hook(:view_layouts_base_html_head) | |
152 | end |
|
153 | end | |
153 |
|
154 | |||
154 | def test_call_hook_should_not_change_the_default_url_for_email_notifications |
|
155 | def test_call_hook_should_not_change_the_default_url_for_email_notifications | |
155 | issue = Issue.find(1) |
|
156 | issue = Issue.find(1) | |
156 |
|
157 | |||
157 | ActionMailer::Base.deliveries.clear |
|
158 | ActionMailer::Base.deliveries.clear | |
158 | Mailer.deliver_issue_add(issue) |
|
159 | Mailer.deliver_issue_add(issue) | |
159 | mail = ActionMailer::Base.deliveries.last |
|
160 | mail = ActionMailer::Base.deliveries.last | |
160 |
|
161 | |||
161 | @hook_module.add_listener(TestLinkToHook) |
|
162 | @hook_module.add_listener(TestLinkToHook) | |
162 | hook_helper.call_hook(:view_layouts_base_html_head) |
|
163 | hook_helper.call_hook(:view_layouts_base_html_head) | |
163 |
|
164 | |||
164 | ActionMailer::Base.deliveries.clear |
|
165 | ActionMailer::Base.deliveries.clear | |
165 | Mailer.deliver_issue_add(issue) |
|
166 | Mailer.deliver_issue_add(issue) | |
166 | mail2 = ActionMailer::Base.deliveries.last |
|
167 | mail2 = ActionMailer::Base.deliveries.last | |
167 |
|
168 | |||
168 | assert_equal mail_body(mail), mail_body(mail2) |
|
169 | assert_equal mail_body(mail), mail_body(mail2) | |
169 | end |
|
170 | end | |
170 |
|
171 | |||
171 | def hook_helper |
|
172 | def hook_helper | |
172 | @hook_helper ||= TestHookHelperController.new |
|
173 | @hook_helper ||= TestHookHelperController.new | |
173 | end |
|
174 | end | |
174 |
|
175 | |||
175 | def view_hook_helper |
|
176 | def view_hook_helper | |
176 | @view_hook_helper ||= TestHookHelperView.new(Rails.root.to_s + '/app/views') |
|
177 | @view_hook_helper ||= TestHookHelperView.new(Rails.root.to_s + '/app/views') | |
177 | end |
|
178 | end | |
178 | end |
|
179 | end | |
179 |
|
180 |
General Comments 0
You need to be logged in to leave comments.
Login now