@@ -1,172 +1,178 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class Redmine::Hook::ManagerTest < ActiveSupport::TestCase |
|
21 | ||
|
22 | fixtures :issues | |
|
21 | fixtures :projects, :users, :members, :member_roles, :roles, | |
|
22 | :groups_users, | |
|
23 | :trackers, :projects_trackers, | |
|
24 | :enabled_modules, | |
|
25 | :versions, | |
|
26 | :issue_statuses, :issue_categories, :issue_relations, :workflows, | |
|
27 | :enumerations, | |
|
28 | :issues | |
|
23 | 29 | |
|
24 | 30 | # Some hooks that are manually registered in these tests |
|
25 | 31 | class TestHook < Redmine::Hook::ViewListener; end |
|
26 | 32 | |
|
27 | 33 | class TestHook1 < TestHook |
|
28 | 34 | def view_layouts_base_html_head(context) |
|
29 | 35 | 'Test hook 1 listener.' |
|
30 | 36 | end |
|
31 | 37 | end |
|
32 | 38 | |
|
33 | 39 | class TestHook2 < TestHook |
|
34 | 40 | def view_layouts_base_html_head(context) |
|
35 | 41 | 'Test hook 2 listener.' |
|
36 | 42 | end |
|
37 | 43 | end |
|
38 | 44 | |
|
39 | 45 | class TestHook3 < TestHook |
|
40 | 46 | def view_layouts_base_html_head(context) |
|
41 | 47 | "Context keys: #{context.keys.collect(&:to_s).sort.join(', ')}." |
|
42 | 48 | end |
|
43 | 49 | end |
|
44 | 50 | |
|
45 | 51 | class TestLinkToHook < TestHook |
|
46 | 52 | def view_layouts_base_html_head(context) |
|
47 | 53 | link_to('Issues', :controller => 'issues') |
|
48 | 54 | end |
|
49 | 55 | end |
|
50 | 56 | |
|
51 | 57 | class TestHookHelperController < ActionController::Base |
|
52 | 58 | include Redmine::Hook::Helper |
|
53 | 59 | end |
|
54 | 60 | |
|
55 | 61 | class TestHookHelperView < ActionView::Base |
|
56 | 62 | include Redmine::Hook::Helper |
|
57 | 63 | end |
|
58 | 64 | |
|
59 | 65 | Redmine::Hook.clear_listeners |
|
60 | 66 | |
|
61 | 67 | def setup |
|
62 | 68 | @hook_module = Redmine::Hook |
|
63 | 69 | end |
|
64 | 70 | |
|
65 | 71 | def teardown |
|
66 | 72 | @hook_module.clear_listeners |
|
67 | 73 | end |
|
68 | 74 | |
|
69 | 75 | def test_clear_listeners |
|
70 | 76 | assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size |
|
71 | 77 | @hook_module.add_listener(TestHook1) |
|
72 | 78 | @hook_module.add_listener(TestHook2) |
|
73 | 79 | assert_equal 2, @hook_module.hook_listeners(:view_layouts_base_html_head).size |
|
74 | 80 | |
|
75 | 81 | @hook_module.clear_listeners |
|
76 | 82 | assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size |
|
77 | 83 | end |
|
78 | 84 | |
|
79 | 85 | def test_add_listener |
|
80 | 86 | assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size |
|
81 | 87 | @hook_module.add_listener(TestHook1) |
|
82 | 88 | assert_equal 1, @hook_module.hook_listeners(:view_layouts_base_html_head).size |
|
83 | 89 | end |
|
84 | 90 | |
|
85 | 91 | def test_call_hook |
|
86 | 92 | @hook_module.add_listener(TestHook1) |
|
87 | 93 | assert_equal ['Test hook 1 listener.'], hook_helper.call_hook(:view_layouts_base_html_head) |
|
88 | 94 | end |
|
89 | 95 | |
|
90 | 96 | def test_call_hook_with_context |
|
91 | 97 | @hook_module.add_listener(TestHook3) |
|
92 | 98 | assert_equal ['Context keys: bar, controller, foo, project, request.'], |
|
93 | 99 | hook_helper.call_hook(:view_layouts_base_html_head, :foo => 1, :bar => 'a') |
|
94 | 100 | end |
|
95 | 101 | |
|
96 | 102 | def test_call_hook_with_multiple_listeners |
|
97 | 103 | @hook_module.add_listener(TestHook1) |
|
98 | 104 | @hook_module.add_listener(TestHook2) |
|
99 | 105 | assert_equal ['Test hook 1 listener.', 'Test hook 2 listener.'], hook_helper.call_hook(:view_layouts_base_html_head) |
|
100 | 106 | end |
|
101 | 107 | |
|
102 | 108 | # Context: Redmine::Hook::Helper.call_hook default_url |
|
103 | 109 | def test_call_hook_default_url_options |
|
104 | 110 | @hook_module.add_listener(TestLinkToHook) |
|
105 | 111 | |
|
106 | 112 | assert_equal ['<a href="/issues">Issues</a>'], hook_helper.call_hook(:view_layouts_base_html_head) |
|
107 | 113 | end |
|
108 | 114 | |
|
109 | 115 | # Context: Redmine::Hook::Helper.call_hook |
|
110 | 116 | def test_call_hook_with_project_added_to_context |
|
111 | 117 | @hook_module.add_listener(TestHook3) |
|
112 | 118 | assert_match /project/i, hook_helper.call_hook(:view_layouts_base_html_head)[0] |
|
113 | 119 | end |
|
114 | 120 | |
|
115 | 121 | def test_call_hook_from_controller_with_controller_added_to_context |
|
116 | 122 | @hook_module.add_listener(TestHook3) |
|
117 | 123 | assert_match /controller/i, hook_helper.call_hook(:view_layouts_base_html_head)[0] |
|
118 | 124 | end |
|
119 | 125 | |
|
120 | 126 | def test_call_hook_from_controller_with_request_added_to_context |
|
121 | 127 | @hook_module.add_listener(TestHook3) |
|
122 | 128 | assert_match /request/i, hook_helper.call_hook(:view_layouts_base_html_head)[0] |
|
123 | 129 | end |
|
124 | 130 | |
|
125 | 131 | def test_call_hook_from_view_with_project_added_to_context |
|
126 | 132 | @hook_module.add_listener(TestHook3) |
|
127 | 133 | assert_match /project/i, view_hook_helper.call_hook(:view_layouts_base_html_head) |
|
128 | 134 | end |
|
129 | 135 | |
|
130 | 136 | def test_call_hook_from_view_with_controller_added_to_context |
|
131 | 137 | @hook_module.add_listener(TestHook3) |
|
132 | 138 | assert_match /controller/i, view_hook_helper.call_hook(:view_layouts_base_html_head) |
|
133 | 139 | end |
|
134 | 140 | |
|
135 | 141 | def test_call_hook_from_view_with_request_added_to_context |
|
136 | 142 | @hook_module.add_listener(TestHook3) |
|
137 | 143 | assert_match /request/i, view_hook_helper.call_hook(:view_layouts_base_html_head) |
|
138 | 144 | end |
|
139 | 145 | |
|
140 | 146 | def test_call_hook_from_view_should_join_responses_with_a_space |
|
141 | 147 | @hook_module.add_listener(TestHook1) |
|
142 | 148 | @hook_module.add_listener(TestHook2) |
|
143 | 149 | assert_equal 'Test hook 1 listener. Test hook 2 listener.', |
|
144 | 150 | view_hook_helper.call_hook(:view_layouts_base_html_head) |
|
145 | 151 | end |
|
146 | 152 | |
|
147 | 153 | def test_call_hook_should_not_change_the_default_url_for_email_notifications |
|
148 | 154 | issue = Issue.find(1) |
|
149 | 155 | |
|
150 | 156 | ActionMailer::Base.deliveries.clear |
|
151 | 157 | Mailer.deliver_issue_add(issue) |
|
152 | 158 | mail = ActionMailer::Base.deliveries.last |
|
153 | 159 | |
|
154 | 160 | @hook_module.add_listener(TestLinkToHook) |
|
155 | 161 | hook_helper.call_hook(:view_layouts_base_html_head) |
|
156 | 162 | |
|
157 | 163 | ActionMailer::Base.deliveries.clear |
|
158 | 164 | Mailer.deliver_issue_add(issue) |
|
159 | 165 | mail2 = ActionMailer::Base.deliveries.last |
|
160 | 166 | |
|
161 | 167 | assert_equal mail_body(mail), mail_body(mail2) |
|
162 | 168 | end |
|
163 | 169 | |
|
164 | 170 | def hook_helper |
|
165 | 171 | @hook_helper ||= TestHookHelperController.new |
|
166 | 172 | end |
|
167 | 173 | |
|
168 | 174 | def view_hook_helper |
|
169 | 175 | @view_hook_helper ||= TestHookHelperView.new(Rails.root.to_s + '/app/views') |
|
170 | 176 | end |
|
171 | 177 | end |
|
172 | 178 |
General Comments 0
You need to be logged in to leave comments.
Login now