annotate .svn/pristine/ca/ca6a83d87a789a365f28655b1d414ca917f8787b.svn-base @ 1384:b51b5ae3734c luisf

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