annotate test/unit/lib/redmine/.svn/text-base/hook_test.rb.svn-base @ 850:e9e53db0c93a bug_213

Close obsolete branch bug_213
author Chris Cannam
date Sat, 13 Aug 2011 14:51:48 +0100
parents af80e5618e9b
children
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006-2008 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@0 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@0 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@117 18 require File.expand_path('../../../../test_helper', __FILE__)
Chris@0 19
Chris@0 20 class Redmine::Hook::ManagerTest < ActiveSupport::TestCase
Chris@0 21
Chris@0 22 fixtures :issues
Chris@0 23
Chris@0 24 # Some hooks that are manually registered in these tests
Chris@0 25 class TestHook < Redmine::Hook::ViewListener; end
Chris@0 26
Chris@0 27 class TestHook1 < TestHook
Chris@0 28 def view_layouts_base_html_head(context)
Chris@0 29 'Test hook 1 listener.'
Chris@0 30 end
Chris@0 31 end
Chris@0 32
Chris@0 33 class TestHook2 < TestHook
Chris@0 34 def view_layouts_base_html_head(context)
Chris@0 35 'Test hook 2 listener.'
Chris@0 36 end
Chris@0 37 end
Chris@0 38
Chris@0 39 class TestHook3 < TestHook
Chris@0 40 def view_layouts_base_html_head(context)
Chris@0 41 "Context keys: #{context.keys.collect(&:to_s).sort.join(', ')}."
Chris@0 42 end
Chris@0 43 end
Chris@0 44
Chris@0 45 class TestLinkToHook < TestHook
Chris@0 46 def view_layouts_base_html_head(context)
Chris@0 47 link_to('Issues', :controller => 'issues')
Chris@0 48 end
Chris@0 49 end
Chris@0 50
Chris@0 51 class TestHookHelperController < ActionController::Base
Chris@0 52 include Redmine::Hook::Helper
Chris@0 53 end
Chris@0 54
Chris@0 55 class TestHookHelperView < ActionView::Base
Chris@0 56 include Redmine::Hook::Helper
Chris@0 57 end
Chris@0 58
Chris@0 59 Redmine::Hook.clear_listeners
Chris@0 60
Chris@0 61 def setup
Chris@0 62 @hook_module = Redmine::Hook
Chris@0 63 end
Chris@0 64
Chris@0 65 def teardown
Chris@0 66 @hook_module.clear_listeners
Chris@0 67 end
Chris@0 68
Chris@0 69 def test_clear_listeners
Chris@0 70 assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size
Chris@0 71 @hook_module.add_listener(TestHook1)
Chris@0 72 @hook_module.add_listener(TestHook2)
Chris@0 73 assert_equal 2, @hook_module.hook_listeners(:view_layouts_base_html_head).size
Chris@0 74
Chris@0 75 @hook_module.clear_listeners
Chris@0 76 assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size
Chris@0 77 end
Chris@0 78
Chris@0 79 def test_add_listener
Chris@0 80 assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size
Chris@0 81 @hook_module.add_listener(TestHook1)
Chris@0 82 assert_equal 1, @hook_module.hook_listeners(:view_layouts_base_html_head).size
Chris@0 83 end
Chris@0 84
Chris@0 85 def test_call_hook
Chris@0 86 @hook_module.add_listener(TestHook1)
Chris@0 87 assert_equal ['Test hook 1 listener.'], hook_helper.call_hook(:view_layouts_base_html_head)
Chris@0 88 end
Chris@0 89
Chris@0 90 def test_call_hook_with_context
Chris@0 91 @hook_module.add_listener(TestHook3)
Chris@0 92 assert_equal ['Context keys: bar, controller, foo, project, request.'],
Chris@0 93 hook_helper.call_hook(:view_layouts_base_html_head, :foo => 1, :bar => 'a')
Chris@0 94 end
Chris@0 95
Chris@0 96 def test_call_hook_with_multiple_listeners
Chris@0 97 @hook_module.add_listener(TestHook1)
Chris@0 98 @hook_module.add_listener(TestHook2)
Chris@0 99 assert_equal ['Test hook 1 listener.', 'Test hook 2 listener.'], hook_helper.call_hook(:view_layouts_base_html_head)
Chris@0 100 end
Chris@0 101
Chris@0 102 # Context: Redmine::Hook::Helper.call_hook default_url
Chris@0 103 def test_call_hook_default_url_options
Chris@0 104 @hook_module.add_listener(TestLinkToHook)
Chris@0 105
Chris@0 106 assert_equal ['<a href="/issues">Issues</a>'], hook_helper.call_hook(:view_layouts_base_html_head)
Chris@0 107 end
Chris@0 108
Chris@0 109 # Context: Redmine::Hook::Helper.call_hook
Chris@0 110 def test_call_hook_with_project_added_to_context
Chris@0 111 @hook_module.add_listener(TestHook3)
Chris@0 112 assert_match /project/i, hook_helper.call_hook(:view_layouts_base_html_head)[0]
Chris@0 113 end
Chris@0 114
Chris@0 115 def test_call_hook_from_controller_with_controller_added_to_context
Chris@0 116 @hook_module.add_listener(TestHook3)
Chris@0 117 assert_match /controller/i, hook_helper.call_hook(:view_layouts_base_html_head)[0]
Chris@0 118 end
Chris@0 119
Chris@0 120 def test_call_hook_from_controller_with_request_added_to_context
Chris@0 121 @hook_module.add_listener(TestHook3)
Chris@0 122 assert_match /request/i, hook_helper.call_hook(:view_layouts_base_html_head)[0]
Chris@0 123 end
Chris@0 124
Chris@0 125 def test_call_hook_from_view_with_project_added_to_context
Chris@0 126 @hook_module.add_listener(TestHook3)
Chris@0 127 assert_match /project/i, view_hook_helper.call_hook(:view_layouts_base_html_head)
Chris@0 128 end
Chris@0 129
Chris@0 130 def test_call_hook_from_view_with_controller_added_to_context
Chris@0 131 @hook_module.add_listener(TestHook3)
Chris@0 132 assert_match /controller/i, view_hook_helper.call_hook(:view_layouts_base_html_head)
Chris@0 133 end
Chris@0 134
Chris@0 135 def test_call_hook_from_view_with_request_added_to_context
Chris@0 136 @hook_module.add_listener(TestHook3)
Chris@0 137 assert_match /request/i, view_hook_helper.call_hook(:view_layouts_base_html_head)
Chris@0 138 end
Chris@0 139
Chris@0 140 def test_call_hook_from_view_should_join_responses_with_a_space
Chris@0 141 @hook_module.add_listener(TestHook1)
Chris@0 142 @hook_module.add_listener(TestHook2)
Chris@0 143 assert_equal 'Test hook 1 listener. Test hook 2 listener.',
Chris@0 144 view_hook_helper.call_hook(:view_layouts_base_html_head)
Chris@0 145 end
Chris@0 146
Chris@0 147 def test_call_hook_should_not_change_the_default_url_for_email_notifications
Chris@0 148 issue = Issue.find(1)
Chris@0 149
Chris@0 150 ActionMailer::Base.deliveries.clear
Chris@0 151 Mailer.deliver_issue_add(issue)
Chris@0 152 mail = ActionMailer::Base.deliveries.last
Chris@0 153
Chris@0 154 @hook_module.add_listener(TestLinkToHook)
Chris@0 155 hook_helper.call_hook(:view_layouts_base_html_head)
Chris@0 156
Chris@0 157 ActionMailer::Base.deliveries.clear
Chris@0 158 Mailer.deliver_issue_add(issue)
Chris@0 159 mail2 = ActionMailer::Base.deliveries.last
Chris@0 160
Chris@0 161 assert_equal mail.body, mail2.body
Chris@0 162 end
Chris@0 163
Chris@0 164 def hook_helper
Chris@0 165 @hook_helper ||= TestHookHelperController.new
Chris@0 166 end
Chris@0 167
Chris@0 168 def view_hook_helper
Chris@0 169 @view_hook_helper ||= TestHookHelperView.new(RAILS_ROOT + '/app/views')
Chris@0 170 end
Chris@0 171 end
Chris@0 172