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