2011-05-30 00:11:52 +04:00
|
|
|
#-- copyright
|
|
|
|
# ChiliProject is a project management system.
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +04:00
|
|
|
# Copyright (C) 2010-2011 the ChiliProject Team
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2008-09-05 14:31:06 +04:00
|
|
|
# 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.
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +04:00
|
|
|
# See doc/COPYRIGHT.rdoc for more details.
|
|
|
|
#++
|
2010-12-13 02:24:34 +03:00
|
|
|
require File.expand_path('../../../../test_helper', __FILE__)
|
2008-09-05 14:31:06 +04:00
|
|
|
|
2009-09-13 21:14:35 +04:00
|
|
|
class Redmine::Hook::ManagerTest < ActiveSupport::TestCase
|
2008-09-05 14:31:06 +04:00
|
|
|
|
2009-02-25 10:25:01 +03:00
|
|
|
fixtures :issues
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2008-09-05 14:31:06 +04:00
|
|
|
# Some hooks that are manually registered in these tests
|
2009-02-10 06:12:40 +03:00
|
|
|
class TestHook < Redmine::Hook::ViewListener; end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2008-09-05 14:31:06 +04:00
|
|
|
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
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2008-09-05 14:31:06 +04:00
|
|
|
class TestHook3 < TestHook
|
|
|
|
def view_layouts_base_html_head(context)
|
|
|
|
"Context keys: #{context.keys.collect(&:to_s).sort.join(', ')}."
|
|
|
|
end
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-02-10 06:12:40 +03:00
|
|
|
class TestLinkToHook < TestHook
|
|
|
|
def view_layouts_base_html_head(context)
|
|
|
|
link_to('Issues', :controller => 'issues')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-21 03:23:16 +03:00
|
|
|
class TestHookHelperController < ActionController::Base
|
|
|
|
include Redmine::Hook::Helper
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-02-21 03:23:22 +03:00
|
|
|
class TestHookHelperView < ActionView::Base
|
|
|
|
include Redmine::Hook::Helper
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2008-09-05 14:31:06 +04:00
|
|
|
Redmine::Hook.clear_listeners
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2008-09-05 14:31:06 +04:00
|
|
|
def setup
|
|
|
|
@hook_module = Redmine::Hook
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2008-09-05 14:31:06 +04:00
|
|
|
def teardown
|
|
|
|
@hook_module.clear_listeners
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2008-09-05 14:31:06 +04:00
|
|
|
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
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2008-09-05 14:31:06 +04:00
|
|
|
@hook_module.clear_listeners
|
|
|
|
assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2008-09-05 14:31:06 +04:00
|
|
|
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
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2008-09-05 14:31:06 +04:00
|
|
|
def test_call_hook
|
|
|
|
@hook_module.add_listener(TestHook1)
|
2009-12-19 23:06:32 +03:00
|
|
|
assert_equal ['Test hook 1 listener.'], hook_helper.call_hook(:view_layouts_base_html_head)
|
2008-09-05 14:31:06 +04:00
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2008-09-05 14:31:06 +04:00
|
|
|
def test_call_hook_with_context
|
|
|
|
@hook_module.add_listener(TestHook3)
|
2009-02-21 03:23:16 +03:00
|
|
|
assert_equal ['Context keys: bar, controller, foo, project, request.'],
|
2009-12-19 23:06:32 +03:00
|
|
|
hook_helper.call_hook(:view_layouts_base_html_head, :foo => 1, :bar => 'a')
|
2008-09-05 14:31:06 +04:00
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2008-09-05 14:31:06 +04:00
|
|
|
def test_call_hook_with_multiple_listeners
|
|
|
|
@hook_module.add_listener(TestHook1)
|
|
|
|
@hook_module.add_listener(TestHook2)
|
2009-12-19 23:06:32 +03:00
|
|
|
assert_equal ['Test hook 1 listener.', 'Test hook 2 listener.'], hook_helper.call_hook(:view_layouts_base_html_head)
|
2009-02-10 06:12:40 +03:00
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-02-21 03:23:28 +03:00
|
|
|
# Context: Redmine::Hook::Helper.call_hook default_url
|
|
|
|
def test_call_hook_default_url_options
|
2009-02-10 06:12:40 +03:00
|
|
|
@hook_module.add_listener(TestLinkToHook)
|
|
|
|
|
2009-12-19 23:06:32 +03:00
|
|
|
assert_equal ['<a href="/issues">Issues</a>'], hook_helper.call_hook(:view_layouts_base_html_head)
|
2009-02-10 06:12:40 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
# Context: Redmine::Hook::Helper.call_hook
|
|
|
|
def test_call_hook_with_project_added_to_context
|
2009-02-21 03:23:22 +03:00
|
|
|
@hook_module.add_listener(TestHook3)
|
2009-12-19 23:06:32 +03:00
|
|
|
assert_match /project/i, hook_helper.call_hook(:view_layouts_base_html_head)[0]
|
2009-02-10 06:12:40 +03:00
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-02-10 06:12:40 +03:00
|
|
|
def test_call_hook_from_controller_with_controller_added_to_context
|
2009-02-21 03:23:22 +03:00
|
|
|
@hook_module.add_listener(TestHook3)
|
2009-12-19 23:06:32 +03:00
|
|
|
assert_match /controller/i, hook_helper.call_hook(:view_layouts_base_html_head)[0]
|
2009-02-10 06:12:40 +03:00
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-02-10 06:12:40 +03:00
|
|
|
def test_call_hook_from_controller_with_request_added_to_context
|
2009-02-21 03:23:22 +03:00
|
|
|
@hook_module.add_listener(TestHook3)
|
2009-12-19 23:06:32 +03:00
|
|
|
assert_match /request/i, hook_helper.call_hook(:view_layouts_base_html_head)[0]
|
2009-02-10 06:12:40 +03:00
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-02-10 06:12:40 +03:00
|
|
|
def test_call_hook_from_view_with_project_added_to_context
|
2009-02-21 03:23:22 +03:00
|
|
|
@hook_module.add_listener(TestHook3)
|
2009-12-19 23:06:32 +03:00
|
|
|
assert_match /project/i, view_hook_helper.call_hook(:view_layouts_base_html_head)
|
2009-02-10 06:12:40 +03:00
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-02-10 06:12:40 +03:00
|
|
|
def test_call_hook_from_view_with_controller_added_to_context
|
2009-02-21 03:23:22 +03:00
|
|
|
@hook_module.add_listener(TestHook3)
|
2009-12-19 23:06:32 +03:00
|
|
|
assert_match /controller/i, view_hook_helper.call_hook(:view_layouts_base_html_head)
|
2009-02-10 06:12:40 +03:00
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-02-10 06:12:40 +03:00
|
|
|
def test_call_hook_from_view_with_request_added_to_context
|
2009-02-21 03:23:22 +03:00
|
|
|
@hook_module.add_listener(TestHook3)
|
2009-12-19 23:06:32 +03:00
|
|
|
assert_match /request/i, view_hook_helper.call_hook(:view_layouts_base_html_head)
|
2009-02-10 06:12:40 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_call_hook_from_view_should_join_responses_with_a_space
|
2009-02-21 03:23:22 +03:00
|
|
|
@hook_module.add_listener(TestHook1)
|
|
|
|
@hook_module.add_listener(TestHook2)
|
|
|
|
assert_equal 'Test hook 1 listener. Test hook 2 listener.',
|
2009-12-19 23:06:32 +03:00
|
|
|
view_hook_helper.call_hook(:view_layouts_base_html_head)
|
2008-09-05 14:31:06 +04:00
|
|
|
end
|
2009-02-25 10:25:01 +03:00
|
|
|
|
|
|
|
def test_call_hook_should_not_change_the_default_url_for_email_notifications
|
|
|
|
issue = Issue.find(1)
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-02-25 10:25:01 +03:00
|
|
|
ActionMailer::Base.deliveries.clear
|
|
|
|
Mailer.deliver_issue_add(issue)
|
|
|
|
mail = ActionMailer::Base.deliveries.last
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-02-25 10:25:01 +03:00
|
|
|
@hook_module.add_listener(TestLinkToHook)
|
2009-12-19 23:06:32 +03:00
|
|
|
hook_helper.call_hook(:view_layouts_base_html_head)
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-02-25 10:25:01 +03:00
|
|
|
ActionMailer::Base.deliveries.clear
|
|
|
|
Mailer.deliver_issue_add(issue)
|
|
|
|
mail2 = ActionMailer::Base.deliveries.last
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-02-25 10:25:01 +03:00
|
|
|
assert_equal mail.body, mail2.body
|
2009-12-19 23:06:32 +03:00
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2009-12-19 23:06:32 +03:00
|
|
|
def hook_helper
|
|
|
|
@hook_helper ||= TestHookHelperController.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def view_hook_helper
|
2009-12-21 05:24:56 +03:00
|
|
|
@view_hook_helper ||= TestHookHelperView.new(RAILS_ROOT + '/app/views')
|
2009-12-19 23:06:32 +03:00
|
|
|
end
|
2008-09-05 14:31:06 +04:00
|
|
|
end
|
2009-02-10 06:12:40 +03:00
|
|
|
|