annotate test/unit/lib/redmine/hook_test.rb @ 1466:834828a14f2b bug_598

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