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