hook_test.rb
178 lines
| 5.9 KiB
| text/x-ruby
|
RubyLexer
|
r5969 | # Redmine - project management software | ||
|
r10939 | # Copyright (C) 2006-2013 Jean-Philippe Lang | ||
|
r1785 | # | ||
# This program is free software; you can redistribute it and/or | ||||
# modify it under the terms of the GNU General Public License | ||||
# as published by the Free Software Foundation; either version 2 | ||||
# of the License, or (at your option) any later version. | ||||
|
r5969 | # | ||
|
r1785 | # This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
|
r5969 | # | ||
|
r1785 | # You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
|
r4395 | require File.expand_path('../../../../test_helper', __FILE__) | ||
|
r1785 | |||
|
r9346 | class Redmine::Hook::ManagerTest < ActionView::TestCase | ||
|
r10073 | fixtures :projects, :users, :members, :member_roles, :roles, | ||
:groups_users, | ||||
:trackers, :projects_trackers, | ||||
:enabled_modules, | ||||
:versions, | ||||
:issue_statuses, :issue_categories, :issue_relations, :workflows, | ||||
:enumerations, | ||||
:issues | ||||
|
r5969 | |||
|
r1785 | # Some hooks that are manually registered in these tests | ||
|
r2368 | class TestHook < Redmine::Hook::ViewListener; end | ||
|
r5970 | |||
|
r1785 | class TestHook1 < TestHook | ||
def view_layouts_base_html_head(context) | ||||
'Test hook 1 listener.' | ||||
end | ||||
end | ||||
class TestHook2 < TestHook | ||||
def view_layouts_base_html_head(context) | ||||
'Test hook 2 listener.' | ||||
end | ||||
end | ||||
|
r5969 | |||
|
r1785 | class TestHook3 < TestHook | ||
def view_layouts_base_html_head(context) | ||||
"Context keys: #{context.keys.collect(&:to_s).sort.join(', ')}." | ||||
end | ||||
end | ||||
|
r5969 | |||
|
r2368 | class TestLinkToHook < TestHook | ||
def view_layouts_base_html_head(context) | ||||
link_to('Issues', :controller => 'issues') | ||||
end | ||||
end | ||||
|
r2427 | class TestHookHelperController < ActionController::Base | ||
include Redmine::Hook::Helper | ||||
end | ||||
|
r5970 | |||
|
r2428 | class TestHookHelperView < ActionView::Base | ||
include Redmine::Hook::Helper | ||||
end | ||||
|
r5970 | |||
|
r1785 | Redmine::Hook.clear_listeners | ||
|
r5970 | |||
|
r1785 | def setup | ||
@hook_module = Redmine::Hook | ||||
end | ||||
|
r5970 | |||
|
r1785 | def teardown | ||
@hook_module.clear_listeners | ||||
end | ||||
|
r5970 | |||
|
r1785 | def test_clear_listeners | ||
assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size | ||||
@hook_module.add_listener(TestHook1) | ||||
@hook_module.add_listener(TestHook2) | ||||
assert_equal 2, @hook_module.hook_listeners(:view_layouts_base_html_head).size | ||||
|
r5970 | |||
|
r1785 | @hook_module.clear_listeners | ||
assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size | ||||
end | ||||
|
r5970 | |||
|
r1785 | def test_add_listener | ||
assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size | ||||
@hook_module.add_listener(TestHook1) | ||||
assert_equal 1, @hook_module.hook_listeners(:view_layouts_base_html_head).size | ||||
end | ||||
|
r5970 | |||
|
r1785 | def test_call_hook | ||
@hook_module.add_listener(TestHook1) | ||||
|
r3083 | assert_equal ['Test hook 1 listener.'], hook_helper.call_hook(:view_layouts_base_html_head) | ||
|
r1785 | end | ||
|
r5970 | |||
|
r1785 | def test_call_hook_with_context | ||
@hook_module.add_listener(TestHook3) | ||||
|
r9602 | assert_equal ['Context keys: bar, controller, foo, hook_caller, project, request.'], | ||
|
r3083 | hook_helper.call_hook(:view_layouts_base_html_head, :foo => 1, :bar => 'a') | ||
|
r1785 | end | ||
|
r5970 | |||
|
r1785 | def test_call_hook_with_multiple_listeners | ||
@hook_module.add_listener(TestHook1) | ||||
@hook_module.add_listener(TestHook2) | ||||
|
r3083 | assert_equal ['Test hook 1 listener.', 'Test hook 2 listener.'], hook_helper.call_hook(:view_layouts_base_html_head) | ||
|
r2368 | end | ||
|
r5970 | |||
|
r2429 | # Context: Redmine::Hook::Helper.call_hook default_url | ||
def test_call_hook_default_url_options | ||||
|
r2368 | @hook_module.add_listener(TestLinkToHook) | ||
|
r3083 | assert_equal ['<a href="/issues">Issues</a>'], hook_helper.call_hook(:view_layouts_base_html_head) | ||
|
r2368 | end | ||
# Context: Redmine::Hook::Helper.call_hook | ||||
def test_call_hook_with_project_added_to_context | ||||
|
r2428 | @hook_module.add_listener(TestHook3) | ||
|
r3083 | assert_match /project/i, hook_helper.call_hook(:view_layouts_base_html_head)[0] | ||
|
r2368 | end | ||
|
r5970 | |||
|
r2368 | def test_call_hook_from_controller_with_controller_added_to_context | ||
|
r2428 | @hook_module.add_listener(TestHook3) | ||
|
r3083 | assert_match /controller/i, hook_helper.call_hook(:view_layouts_base_html_head)[0] | ||
|
r2368 | end | ||
|
r5970 | |||
|
r2368 | def test_call_hook_from_controller_with_request_added_to_context | ||
|
r2428 | @hook_module.add_listener(TestHook3) | ||
|
r3083 | assert_match /request/i, hook_helper.call_hook(:view_layouts_base_html_head)[0] | ||
|
r2368 | end | ||
|
r5970 | |||
|
r2368 | def test_call_hook_from_view_with_project_added_to_context | ||
|
r2428 | @hook_module.add_listener(TestHook3) | ||
|
r3083 | assert_match /project/i, view_hook_helper.call_hook(:view_layouts_base_html_head) | ||
|
r2368 | end | ||
|
r5970 | |||
|
r2368 | def test_call_hook_from_view_with_controller_added_to_context | ||
|
r2428 | @hook_module.add_listener(TestHook3) | ||
|
r3083 | assert_match /controller/i, view_hook_helper.call_hook(:view_layouts_base_html_head) | ||
|
r2368 | end | ||
|
r5970 | |||
|
r2368 | def test_call_hook_from_view_with_request_added_to_context | ||
|
r2428 | @hook_module.add_listener(TestHook3) | ||
|
r3083 | assert_match /request/i, view_hook_helper.call_hook(:view_layouts_base_html_head) | ||
|
r2368 | end | ||
def test_call_hook_from_view_should_join_responses_with_a_space | ||||
|
r2428 | @hook_module.add_listener(TestHook1) | ||
@hook_module.add_listener(TestHook2) | ||||
assert_equal 'Test hook 1 listener. Test hook 2 listener.', | ||||
|
r3083 | view_hook_helper.call_hook(:view_layouts_base_html_head) | ||
|
r1785 | end | ||
|
r2458 | |||
def test_call_hook_should_not_change_the_default_url_for_email_notifications | ||||
issue = Issue.find(1) | ||||
|
r5970 | |||
|
r2458 | ActionMailer::Base.deliveries.clear | ||
|
r9455 | Mailer.issue_add(issue).deliver | ||
|
r2458 | mail = ActionMailer::Base.deliveries.last | ||
|
r5970 | |||
|
r2458 | @hook_module.add_listener(TestLinkToHook) | ||
|
r3083 | hook_helper.call_hook(:view_layouts_base_html_head) | ||
|
r5970 | |||
|
r2458 | ActionMailer::Base.deliveries.clear | ||
|
r9455 | Mailer.issue_add(issue).deliver | ||
|
r2458 | mail2 = ActionMailer::Base.deliveries.last | ||
|
r5970 | |||
|
r8972 | assert_equal mail_body(mail), mail_body(mail2) | ||
|
r3083 | end | ||
|
r5970 | |||
|
r3083 | def hook_helper | ||
@hook_helper ||= TestHookHelperController.new | ||||
end | ||||
def view_hook_helper | ||||
|
r5971 | @view_hook_helper ||= TestHookHelperView.new(Rails.root.to_s + '/app/views') | ||
|
r3083 | end | ||
|
r1785 | end | ||
|
r2368 | |||