@@ -1,159 +1,166 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2012 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 | module Redmine |
|
18 | module Redmine | |
19 | module Hook |
|
19 | module Hook | |
20 | @@listener_classes = [] |
|
20 | @@listener_classes = [] | |
21 | @@listeners = nil |
|
21 | @@listeners = nil | |
22 | @@hook_listeners = {} |
|
22 | @@hook_listeners = {} | |
23 |
|
23 | |||
24 | class << self |
|
24 | class << self | |
25 | # Adds a listener class. |
|
25 | # Adds a listener class. | |
26 | # Automatically called when a class inherits from Redmine::Hook::Listener. |
|
26 | # Automatically called when a class inherits from Redmine::Hook::Listener. | |
27 | def add_listener(klass) |
|
27 | def add_listener(klass) | |
28 | raise "Hooks must include Singleton module." unless klass.included_modules.include?(Singleton) |
|
28 | raise "Hooks must include Singleton module." unless klass.included_modules.include?(Singleton) | |
29 | @@listener_classes << klass |
|
29 | @@listener_classes << klass | |
30 | clear_listeners_instances |
|
30 | clear_listeners_instances | |
31 | end |
|
31 | end | |
32 |
|
32 | |||
33 | # Returns all the listerners instances. |
|
33 | # Returns all the listerners instances. | |
34 | def listeners |
|
34 | def listeners | |
35 | @@listeners ||= @@listener_classes.collect {|listener| listener.instance} |
|
35 | @@listeners ||= @@listener_classes.collect {|listener| listener.instance} | |
36 | end |
|
36 | end | |
37 |
|
37 | |||
38 | # Returns the listeners instances for the given hook. |
|
38 | # Returns the listeners instances for the given hook. | |
39 | def hook_listeners(hook) |
|
39 | def hook_listeners(hook) | |
40 | @@hook_listeners[hook] ||= listeners.select {|listener| listener.respond_to?(hook)} |
|
40 | @@hook_listeners[hook] ||= listeners.select {|listener| listener.respond_to?(hook)} | |
41 | end |
|
41 | end | |
42 |
|
42 | |||
43 | # Clears all the listeners. |
|
43 | # Clears all the listeners. | |
44 | def clear_listeners |
|
44 | def clear_listeners | |
45 | @@listener_classes = [] |
|
45 | @@listener_classes = [] | |
46 | clear_listeners_instances |
|
46 | clear_listeners_instances | |
47 | end |
|
47 | end | |
48 |
|
48 | |||
49 | # Clears all the listeners instances. |
|
49 | # Clears all the listeners instances. | |
50 | def clear_listeners_instances |
|
50 | def clear_listeners_instances | |
51 | @@listeners = nil |
|
51 | @@listeners = nil | |
52 | @@hook_listeners = {} |
|
52 | @@hook_listeners = {} | |
53 | end |
|
53 | end | |
54 |
|
54 | |||
55 | # Calls a hook. |
|
55 | # Calls a hook. | |
56 | # Returns the listeners response. |
|
56 | # Returns the listeners response. | |
57 | def call_hook(hook, context={}) |
|
57 | def call_hook(hook, context={}) | |
58 | [].tap do |response| |
|
58 | [].tap do |response| | |
59 | hls = hook_listeners(hook) |
|
59 | hls = hook_listeners(hook) | |
60 | if hls.any? |
|
60 | if hls.any? | |
61 | hls.each {|listener| response << listener.send(hook, context)} |
|
61 | hls.each {|listener| response << listener.send(hook, context)} | |
62 | end |
|
62 | end | |
63 | end |
|
63 | end | |
64 | end |
|
64 | end | |
65 | end |
|
65 | end | |
66 |
|
66 | |||
67 | # Base class for hook listeners. |
|
67 | # Base class for hook listeners. | |
68 | class Listener |
|
68 | class Listener | |
69 | include Singleton |
|
69 | include Singleton | |
70 | include Redmine::I18n |
|
70 | include Redmine::I18n | |
71 |
|
71 | |||
72 | # Registers the listener |
|
72 | # Registers the listener | |
73 | def self.inherited(child) |
|
73 | def self.inherited(child) | |
74 | Redmine::Hook.add_listener(child) |
|
74 | Redmine::Hook.add_listener(child) | |
75 | super |
|
75 | super | |
76 | end |
|
76 | end | |
77 |
|
77 | |||
78 | end |
|
78 | end | |
79 |
|
79 | |||
80 | # Listener class used for views hooks. |
|
80 | # Listener class used for views hooks. | |
81 | # Listeners that inherit this class will include various helpers by default. |
|
81 | # Listeners that inherit this class will include various helpers by default. | |
82 | class ViewListener < Listener |
|
82 | class ViewListener < Listener | |
83 | include ERB::Util |
|
83 | include ERB::Util | |
84 | include ActionView::Helpers::TagHelper |
|
84 | include ActionView::Helpers::TagHelper | |
85 | include ActionView::Helpers::FormHelper |
|
85 | include ActionView::Helpers::FormHelper | |
86 | include ActionView::Helpers::FormTagHelper |
|
86 | include ActionView::Helpers::FormTagHelper | |
87 | include ActionView::Helpers::FormOptionsHelper |
|
87 | include ActionView::Helpers::FormOptionsHelper | |
88 | include ActionView::Helpers::JavaScriptHelper |
|
88 | include ActionView::Helpers::JavaScriptHelper | |
89 | include ActionView::Helpers::NumberHelper |
|
89 | include ActionView::Helpers::NumberHelper | |
90 | include ActionView::Helpers::UrlHelper |
|
90 | include ActionView::Helpers::UrlHelper | |
91 | include ActionView::Helpers::AssetTagHelper |
|
91 | include ActionView::Helpers::AssetTagHelper | |
92 | include ActionView::Helpers::TextHelper |
|
92 | include ActionView::Helpers::TextHelper | |
93 | include Rails.application.routes.url_helpers |
|
93 | include Rails.application.routes.url_helpers | |
94 | include ApplicationHelper |
|
94 | include ApplicationHelper | |
95 |
|
95 | |||
96 | # Default to creating links using only the path. Subclasses can |
|
96 | # Default to creating links using only the path. Subclasses can | |
97 | # change this default as needed |
|
97 | # change this default as needed | |
98 | def self.default_url_options |
|
98 | def self.default_url_options | |
99 | {:only_path => true } |
|
99 | {:only_path => true } | |
100 | end |
|
100 | end | |
101 |
|
101 | |||
102 | # Helper method to directly render a partial using the context: |
|
102 | # Helper method to directly render a partial using the context: | |
103 | # |
|
103 | # | |
104 | # class MyHook < Redmine::Hook::ViewListener |
|
104 | # class MyHook < Redmine::Hook::ViewListener | |
105 | # render_on :view_issues_show_details_bottom, :partial => "show_more_data" |
|
105 | # render_on :view_issues_show_details_bottom, :partial => "show_more_data" | |
106 | # end |
|
106 | # end | |
107 | # |
|
107 | # | |
108 | def self.render_on(hook, options={}) |
|
108 | def self.render_on(hook, options={}) | |
109 | define_method hook do |context| |
|
109 | define_method hook do |context| | |
110 | context[:controller].send(:render_to_string, {:locals => context}.merge(options)) |
|
110 | if context[:hook_caller].respond_to?(:render) | |
|
111 | context[:hook_caller].send(:render, {:locals => context}.merge(options)) | |||
|
112 | elsif context[:controller].is_a?(ActionController::Base) | |||
|
113 | context[:controller].send(:render_to_string, {:locals => context}.merge(options)) | |||
|
114 | else | |||
|
115 | raise "Cannot render #{self.name} hook from #{context[:hook_caller].class.name}" | |||
|
116 | end | |||
111 | end |
|
117 | end | |
112 | end |
|
118 | end | |
113 |
|
119 | |||
114 | def controller |
|
120 | def controller | |
115 | nil |
|
121 | nil | |
116 | end |
|
122 | end | |
117 |
|
123 | |||
118 | def config |
|
124 | def config | |
119 | ActionController::Base.config |
|
125 | ActionController::Base.config | |
120 | end |
|
126 | end | |
121 | end |
|
127 | end | |
122 |
|
128 | |||
123 | # Helper module included in ApplicationHelper and ActionController so that |
|
129 | # Helper module included in ApplicationHelper and ActionController so that | |
124 | # hooks can be called in views like this: |
|
130 | # hooks can be called in views like this: | |
125 | # |
|
131 | # | |
126 | # <%= call_hook(:some_hook) %> |
|
132 | # <%= call_hook(:some_hook) %> | |
127 | # <%= call_hook(:another_hook, :foo => 'bar') %> |
|
133 | # <%= call_hook(:another_hook, :foo => 'bar') %> | |
128 | # |
|
134 | # | |
129 | # Or in controllers like: |
|
135 | # Or in controllers like: | |
130 | # call_hook(:some_hook) |
|
136 | # call_hook(:some_hook) | |
131 | # call_hook(:another_hook, :foo => 'bar') |
|
137 | # call_hook(:another_hook, :foo => 'bar') | |
132 | # |
|
138 | # | |
133 | # Hooks added to views will be concatenated into a string. Hooks added to |
|
139 | # Hooks added to views will be concatenated into a string. Hooks added to | |
134 | # controllers will return an array of results. |
|
140 | # controllers will return an array of results. | |
135 | # |
|
141 | # | |
136 | # Several objects are automatically added to the call context: |
|
142 | # Several objects are automatically added to the call context: | |
137 | # |
|
143 | # | |
138 | # * project => current project |
|
144 | # * project => current project | |
139 | # * request => Request instance |
|
145 | # * request => Request instance | |
140 | # * controller => current Controller instance |
|
146 | # * controller => current Controller instance | |
|
147 | # * hook_caller => object that called the hook | |||
141 | # |
|
148 | # | |
142 | module Helper |
|
149 | module Helper | |
143 | def call_hook(hook, context={}) |
|
150 | def call_hook(hook, context={}) | |
144 | if is_a?(ActionController::Base) |
|
151 | if is_a?(ActionController::Base) | |
145 | default_context = {:controller => self, :project => @project, :request => request} |
|
152 | default_context = {:controller => self, :project => @project, :request => request, :hook_caller => self} | |
146 | Redmine::Hook.call_hook(hook, default_context.merge(context)) |
|
153 | Redmine::Hook.call_hook(hook, default_context.merge(context)) | |
147 | else |
|
154 | else | |
148 | default_context = { :project => @project } |
|
155 | default_context = { :project => @project, :hook_caller => self } | |
149 | default_context[:controller] = controller if respond_to?(:controller) |
|
156 | default_context[:controller] = controller if respond_to?(:controller) | |
150 | default_context[:request] = request if respond_to?(:request) |
|
157 | default_context[:request] = request if respond_to?(:request) | |
151 | Redmine::Hook.call_hook(hook, default_context.merge(context)).join(' ').html_safe |
|
158 | Redmine::Hook.call_hook(hook, default_context.merge(context)).join(' ').html_safe | |
152 | end |
|
159 | end | |
153 | end |
|
160 | end | |
154 | end |
|
161 | end | |
155 | end |
|
162 | end | |
156 | end |
|
163 | end | |
157 |
|
164 | |||
158 | ApplicationHelper.send(:include, Redmine::Hook::Helper) |
|
165 | ApplicationHelper.send(:include, Redmine::Hook::Helper) | |
159 | ActionController::Base.send(:include, Redmine::Hook::Helper) |
|
166 | ActionController::Base.send(:include, Redmine::Hook::Helper) |
@@ -1,67 +1,90 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2012 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 MenuManagerTest < ActionController::IntegrationTest |
|
20 | class MenuManagerTest < ActionController::IntegrationTest | |
21 |
|
21 | |||
22 | fixtures :users, :roles, :projects, :members, :member_roles |
|
22 | fixtures :users, :roles, :projects, :members, :member_roles | |
23 |
|
23 | |||
24 | # Hooks that are manually registered later |
|
24 | # Hooks that are manually registered later | |
25 | class ProjectBasedTemplate < Redmine::Hook::ViewListener |
|
25 | class ProjectBasedTemplate < Redmine::Hook::ViewListener | |
26 | def view_layouts_base_html_head(context) |
|
26 | def view_layouts_base_html_head(context) | |
27 | # Adds a project stylesheet |
|
27 | # Adds a project stylesheet | |
28 | stylesheet_link_tag(context[:project].identifier) if context[:project] |
|
28 | stylesheet_link_tag(context[:project].identifier) if context[:project] | |
29 | end |
|
29 | end | |
30 | end |
|
30 | end | |
31 |
|
31 | |||
32 | class SidebarContent < Redmine::Hook::ViewListener |
|
32 | class SidebarContent < Redmine::Hook::ViewListener | |
33 | def view_layouts_base_sidebar(context) |
|
33 | def view_layouts_base_sidebar(context) | |
34 | content_tag('p', 'Sidebar hook') |
|
34 | content_tag('p', 'Sidebar hook') | |
35 | end |
|
35 | end | |
36 | end |
|
36 | end | |
37 |
|
37 | |||
|
38 | class ContentForInsideHook < Redmine::Hook::ViewListener | |||
|
39 | render_on :view_welcome_index_left, :inline => <<-VIEW | |||
|
40 | <% content_for :header_tags do %> | |||
|
41 | <%= javascript_include_tag 'test_plugin.js', :plugin => 'test_plugin' %> | |||
|
42 | <%= stylesheet_link_tag 'test_plugin.css', :plugin => 'test_plugin' %> | |||
|
43 | <% end %> | |||
|
44 | ||||
|
45 | <p>ContentForInsideHook content</p> | |||
|
46 | VIEW | |||
|
47 | end | |||
|
48 | ||||
38 | def setup |
|
49 | def setup | |
39 | Redmine::Hook.clear_listeners |
|
50 | Redmine::Hook.clear_listeners | |
40 | end |
|
51 | end | |
41 |
|
52 | |||
42 | def teardown |
|
53 | def teardown | |
43 | Redmine::Hook.clear_listeners |
|
54 | Redmine::Hook.clear_listeners | |
44 | end |
|
55 | end | |
45 |
|
56 | |||
46 | def test_html_head_hook_response |
|
57 | def test_html_head_hook_response | |
47 | Redmine::Hook.add_listener(ProjectBasedTemplate) |
|
58 | Redmine::Hook.add_listener(ProjectBasedTemplate) | |
48 |
|
59 | |||
49 | get '/projects/ecookbook' |
|
60 | get '/projects/ecookbook' | |
50 | assert_tag :tag => 'link', :attributes => {:href => '/stylesheets/ecookbook.css'}, |
|
61 | assert_tag :tag => 'link', :attributes => {:href => '/stylesheets/ecookbook.css'}, | |
51 | :parent => {:tag => 'head'} |
|
62 | :parent => {:tag => 'head'} | |
52 | end |
|
63 | end | |
53 |
|
64 | |||
54 | def test_empty_sidebar_should_be_hidden |
|
65 | def test_empty_sidebar_should_be_hidden | |
55 | get '/' |
|
66 | get '/' | |
56 | assert_select 'div#main.nosidebar' |
|
67 | assert_select 'div#main.nosidebar' | |
57 | end |
|
68 | end | |
58 |
|
69 | |||
59 | def test_sidebar_with_hook_content_should_not_be_hidden |
|
70 | def test_sidebar_with_hook_content_should_not_be_hidden | |
60 | Redmine::Hook.add_listener(SidebarContent) |
|
71 | Redmine::Hook.add_listener(SidebarContent) | |
61 |
|
72 | |||
62 | get '/' |
|
73 | get '/' | |
63 | assert_select 'div#sidebar p', :text => 'Sidebar hook' |
|
74 | assert_select 'div#sidebar p', :text => 'Sidebar hook' | |
64 | assert_select 'div#main' |
|
75 | assert_select 'div#main' | |
65 | assert_select 'div#main.nosidebar', 0 |
|
76 | assert_select 'div#main.nosidebar', 0 | |
66 | end |
|
77 | end | |
|
78 | ||||
|
79 | def test_hook_with_content_for_should_append_content | |||
|
80 | Redmine::Hook.add_listener(ContentForInsideHook) | |||
|
81 | ||||
|
82 | get '/' | |||
|
83 | assert_response :success | |||
|
84 | assert_select 'p', :text => 'ContentForInsideHook content' | |||
|
85 | assert_select 'head' do | |||
|
86 | assert_select 'script[src=/plugin_assets/test_plugin/javascripts/test_plugin.js]' | |||
|
87 | assert_select 'link[href=/plugin_assets/test_plugin/stylesheets/test_plugin.css]' | |||
|
88 | end | |||
|
89 | end | |||
67 | end |
|
90 | end |
@@ -1,172 +1,172 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2012 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 |
|
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 | end |
|
63 | end | |
64 |
|
64 | |||
65 | def teardown |
|
65 | def teardown | |
66 | @hook_module.clear_listeners |
|
66 | @hook_module.clear_listeners | |
67 | end |
|
67 | end | |
68 |
|
68 | |||
69 | def test_clear_listeners |
|
69 | def test_clear_listeners | |
70 | 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 | |
71 | @hook_module.add_listener(TestHook1) |
|
71 | @hook_module.add_listener(TestHook1) | |
72 | @hook_module.add_listener(TestHook2) |
|
72 | @hook_module.add_listener(TestHook2) | |
73 | 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 | |
74 |
|
74 | |||
75 | @hook_module.clear_listeners |
|
75 | @hook_module.clear_listeners | |
76 | 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 | |
77 | end |
|
77 | end | |
78 |
|
78 | |||
79 | def test_add_listener |
|
79 | def test_add_listener | |
80 | 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 | |
81 | @hook_module.add_listener(TestHook1) |
|
81 | @hook_module.add_listener(TestHook1) | |
82 | 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 | |
83 | end |
|
83 | end | |
84 |
|
84 | |||
85 | def test_call_hook |
|
85 | def test_call_hook | |
86 | @hook_module.add_listener(TestHook1) |
|
86 | @hook_module.add_listener(TestHook1) | |
87 | assert_equal ['Test hook 1 listener.'], hook_helper.call_hook(:view_layouts_base_html_head) |
|
87 | assert_equal ['Test hook 1 listener.'], hook_helper.call_hook(:view_layouts_base_html_head) | |
88 | end |
|
88 | end | |
89 |
|
89 | |||
90 | def test_call_hook_with_context |
|
90 | def test_call_hook_with_context | |
91 | @hook_module.add_listener(TestHook3) |
|
91 | @hook_module.add_listener(TestHook3) | |
92 | assert_equal ['Context keys: bar, controller, foo, project, request.'], |
|
92 | assert_equal ['Context keys: bar, controller, foo, hook_caller, project, request.'], | |
93 | hook_helper.call_hook(:view_layouts_base_html_head, :foo => 1, :bar => 'a') |
|
93 | hook_helper.call_hook(:view_layouts_base_html_head, :foo => 1, :bar => 'a') | |
94 | end |
|
94 | end | |
95 |
|
95 | |||
96 | def test_call_hook_with_multiple_listeners |
|
96 | def test_call_hook_with_multiple_listeners | |
97 | @hook_module.add_listener(TestHook1) |
|
97 | @hook_module.add_listener(TestHook1) | |
98 | @hook_module.add_listener(TestHook2) |
|
98 | @hook_module.add_listener(TestHook2) | |
99 | assert_equal ['Test hook 1 listener.', 'Test hook 2 listener.'], hook_helper.call_hook(:view_layouts_base_html_head) |
|
99 | assert_equal ['Test hook 1 listener.', 'Test hook 2 listener.'], hook_helper.call_hook(:view_layouts_base_html_head) | |
100 | end |
|
100 | end | |
101 |
|
101 | |||
102 | # Context: Redmine::Hook::Helper.call_hook default_url |
|
102 | # Context: Redmine::Hook::Helper.call_hook default_url | |
103 | def test_call_hook_default_url_options |
|
103 | def test_call_hook_default_url_options | |
104 | @hook_module.add_listener(TestLinkToHook) |
|
104 | @hook_module.add_listener(TestLinkToHook) | |
105 |
|
105 | |||
106 | assert_equal ['<a href="/issues">Issues</a>'], hook_helper.call_hook(:view_layouts_base_html_head) |
|
106 | assert_equal ['<a href="/issues">Issues</a>'], hook_helper.call_hook(:view_layouts_base_html_head) | |
107 | end |
|
107 | end | |
108 |
|
108 | |||
109 | # Context: Redmine::Hook::Helper.call_hook |
|
109 | # Context: Redmine::Hook::Helper.call_hook | |
110 | def test_call_hook_with_project_added_to_context |
|
110 | def test_call_hook_with_project_added_to_context | |
111 | @hook_module.add_listener(TestHook3) |
|
111 | @hook_module.add_listener(TestHook3) | |
112 | assert_match /project/i, hook_helper.call_hook(:view_layouts_base_html_head)[0] |
|
112 | assert_match /project/i, hook_helper.call_hook(:view_layouts_base_html_head)[0] | |
113 | end |
|
113 | end | |
114 |
|
114 | |||
115 | def test_call_hook_from_controller_with_controller_added_to_context |
|
115 | def test_call_hook_from_controller_with_controller_added_to_context | |
116 | @hook_module.add_listener(TestHook3) |
|
116 | @hook_module.add_listener(TestHook3) | |
117 | assert_match /controller/i, hook_helper.call_hook(:view_layouts_base_html_head)[0] |
|
117 | assert_match /controller/i, hook_helper.call_hook(:view_layouts_base_html_head)[0] | |
118 | end |
|
118 | end | |
119 |
|
119 | |||
120 | def test_call_hook_from_controller_with_request_added_to_context |
|
120 | def test_call_hook_from_controller_with_request_added_to_context | |
121 | @hook_module.add_listener(TestHook3) |
|
121 | @hook_module.add_listener(TestHook3) | |
122 | assert_match /request/i, hook_helper.call_hook(:view_layouts_base_html_head)[0] |
|
122 | assert_match /request/i, hook_helper.call_hook(:view_layouts_base_html_head)[0] | |
123 | end |
|
123 | end | |
124 |
|
124 | |||
125 | def test_call_hook_from_view_with_project_added_to_context |
|
125 | def test_call_hook_from_view_with_project_added_to_context | |
126 | @hook_module.add_listener(TestHook3) |
|
126 | @hook_module.add_listener(TestHook3) | |
127 | assert_match /project/i, view_hook_helper.call_hook(:view_layouts_base_html_head) |
|
127 | assert_match /project/i, view_hook_helper.call_hook(:view_layouts_base_html_head) | |
128 | end |
|
128 | end | |
129 |
|
129 | |||
130 | def test_call_hook_from_view_with_controller_added_to_context |
|
130 | def test_call_hook_from_view_with_controller_added_to_context | |
131 | @hook_module.add_listener(TestHook3) |
|
131 | @hook_module.add_listener(TestHook3) | |
132 | assert_match /controller/i, view_hook_helper.call_hook(:view_layouts_base_html_head) |
|
132 | assert_match /controller/i, view_hook_helper.call_hook(:view_layouts_base_html_head) | |
133 | end |
|
133 | end | |
134 |
|
134 | |||
135 | def test_call_hook_from_view_with_request_added_to_context |
|
135 | def test_call_hook_from_view_with_request_added_to_context | |
136 | @hook_module.add_listener(TestHook3) |
|
136 | @hook_module.add_listener(TestHook3) | |
137 | assert_match /request/i, view_hook_helper.call_hook(:view_layouts_base_html_head) |
|
137 | assert_match /request/i, view_hook_helper.call_hook(:view_layouts_base_html_head) | |
138 | end |
|
138 | end | |
139 |
|
139 | |||
140 | 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 | |
141 | @hook_module.add_listener(TestHook1) |
|
141 | @hook_module.add_listener(TestHook1) | |
142 | @hook_module.add_listener(TestHook2) |
|
142 | @hook_module.add_listener(TestHook2) | |
143 | assert_equal 'Test hook 1 listener. Test hook 2 listener.', |
|
143 | assert_equal 'Test hook 1 listener. Test hook 2 listener.', | |
144 | view_hook_helper.call_hook(:view_layouts_base_html_head) |
|
144 | view_hook_helper.call_hook(:view_layouts_base_html_head) | |
145 | end |
|
145 | end | |
146 |
|
146 | |||
147 | 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 | |
148 | issue = Issue.find(1) |
|
148 | issue = Issue.find(1) | |
149 |
|
149 | |||
150 | ActionMailer::Base.deliveries.clear |
|
150 | ActionMailer::Base.deliveries.clear | |
151 | Mailer.issue_add(issue).deliver |
|
151 | Mailer.issue_add(issue).deliver | |
152 | mail = ActionMailer::Base.deliveries.last |
|
152 | mail = ActionMailer::Base.deliveries.last | |
153 |
|
153 | |||
154 | @hook_module.add_listener(TestLinkToHook) |
|
154 | @hook_module.add_listener(TestLinkToHook) | |
155 | hook_helper.call_hook(:view_layouts_base_html_head) |
|
155 | hook_helper.call_hook(:view_layouts_base_html_head) | |
156 |
|
156 | |||
157 | ActionMailer::Base.deliveries.clear |
|
157 | ActionMailer::Base.deliveries.clear | |
158 | Mailer.issue_add(issue).deliver |
|
158 | Mailer.issue_add(issue).deliver | |
159 | mail2 = ActionMailer::Base.deliveries.last |
|
159 | mail2 = ActionMailer::Base.deliveries.last | |
160 |
|
160 | |||
161 | assert_equal mail_body(mail), mail_body(mail2) |
|
161 | assert_equal mail_body(mail), mail_body(mail2) | |
162 | end |
|
162 | end | |
163 |
|
163 | |||
164 | def hook_helper |
|
164 | def hook_helper | |
165 | @hook_helper ||= TestHookHelperController.new |
|
165 | @hook_helper ||= TestHookHelperController.new | |
166 | end |
|
166 | end | |
167 |
|
167 | |||
168 | def view_hook_helper |
|
168 | def view_hook_helper | |
169 | @view_hook_helper ||= TestHookHelperView.new(Rails.root.to_s + '/app/views') |
|
169 | @view_hook_helper ||= TestHookHelperView.new(Rails.root.to_s + '/app/views') | |
170 | end |
|
170 | end | |
171 | end |
|
171 | end | |
172 |
|
172 |
General Comments 0
You need to be logged in to leave comments.
Login now