summaryrefslogtreecommitdiffstats
path: root/test/unit/lib/redmine/hook_test.rb
blob: 5ebe1e7573041dcbcb5889f7c9d5e93047d800bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# redMine - project management software
# Copyright (C) 2006-2008  Jean-Philippe Lang
#
# 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.
# 
# 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.
# 
# 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.

require File.dirname(__FILE__) + '/../../../test_helper'

class Redmine::Hook::ManagerTest < Test::Unit::TestCase

  # Some hooks that are manually registered in these tests
  class TestHook < Redmine::Hook::ViewListener; end
  
  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
  
  class TestHook3 < TestHook
    def view_layouts_base_html_head(context)
      "Context keys: #{context.keys.collect(&:to_s).sort.join(', ')}."
    end
  end
  
  class TestLinkToHook < TestHook
    def view_layouts_base_html_head(context)
      link_to('Issues', :controller => 'issues')
    end
  end

  class TestHookHelperController < ActionController::Base
    include Redmine::Hook::Helper
  end
  
  class TestHookHelperView < ActionView::Base
    include Redmine::Hook::Helper
  end
  
  Redmine::Hook.clear_listeners
  
  def setup
    @hook_module = Redmine::Hook
    @hook_helper = TestHookHelperController.new
    @view_hook_helper = TestHookHelperView.new(RAILS_ROOT + '/app/views')
  end
  
  def teardown
    @hook_module.clear_listeners
    @hook_module.default_url_options = { }
  end
  
  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
    
    @hook_module.clear_listeners
    assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size
  end
  
  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
  
  def test_call_hook
    @hook_module.add_listener(TestHook1)
    assert_equal ['Test hook 1 listener.'], @hook_helper.call_hook(:view_layouts_base_html_head)
  end
  
  def test_call_hook_with_context
    @hook_module.add_listener(TestHook3)
    assert_equal ['Context keys: bar, controller, foo, project, request.'],
                 @hook_helper.call_hook(:view_layouts_base_html_head, :foo => 1, :bar => 'a')
  end
  
  def test_call_hook_with_multiple_listeners
    @hook_module.add_listener(TestHook1)
    @hook_module.add_listener(TestHook2)
    assert_equal ['Test hook 1 listener.', 'Test hook 2 listener.'], @hook_helper.call_hook(:view_layouts_base_html_head)
  end
  
  # Context: Redmine::Hook::call_hook
  def test_call_hook_default_url_options_set
    request = ActionController::TestRequest.new
    request.env = { "SERVER_NAME" => 'example.com'}
    @hook_module.add_listener(TestLinkToHook)

    assert_equal ['<a href="http://example.com/issues">Issues</a>'],
      @hook_helper.call_hook(:view_layouts_base_html_head, :request => request)
  end

  def test_call_hook_default_url_options_set_with_no_standard_request_port
    request = ActionController::TestRequest.new
    request.env = { "SERVER_NAME" => 'example.com', "SERVER_PORT" => 3000}
    @hook_module.add_listener(TestLinkToHook)
    
    assert_equal ['<a href="http://example.com:3000/issues">Issues</a>'],
      @hook_helper.call_hook(:view_layouts_base_html_head, :request => request)
  end

  def test_call_hook_default_url_options_set_with_ssl
    request = ActionController::TestRequest.new
    request.env = { "SERVER_NAME" => 'example.com', "HTTPS" => 'on'}
    @hook_module.add_listener(TestLinkToHook)

    assert_equal ['<a href="https://example.com/issues">Issues</a>'],
      @hook_helper.call_hook(:view_layouts_base_html_head, :request => request)
  end

  def test_call_hook_default_url_options_set_with_forwarded_ssl
    request = ActionController::TestRequest.new
    request.env = { "SERVER_NAME" => 'example.com', "HTTP_X_FORWARDED_PROTO" => "https"}
    @hook_module.add_listener(TestLinkToHook)

    assert_equal ['<a href="https://example.com/issues">Issues</a>'],
      @hook_helper.call_hook(:view_layouts_base_html_head, :request => request)
  end

  # Context: Redmine::Hook::Helper.call_hook
  def test_call_hook_with_project_added_to_context
    @hook_module.add_listener(TestHook3)
    assert_match /project/i, @hook_helper.call_hook(:view_layouts_base_html_head)[0]
  end
  
  def test_call_hook_from_controller_with_controller_added_to_context
    @hook_module.add_listener(TestHook3)
    assert_match /controller/i, @hook_helper.call_hook(:view_layouts_base_html_head)[0]
  end
    
  def test_call_hook_from_controller_with_request_added_to_context
    @hook_module.add_listener(TestHook3)
    assert_match /request/i, @hook_helper.call_hook(:view_layouts_base_html_head)[0]
  end
    
  def test_call_hook_from_view_with_project_added_to_context
    @hook_module.add_listener(TestHook3)
    assert_match /project/i, @view_hook_helper.call_hook(:view_layouts_base_html_head)
  end
    
  def test_call_hook_from_view_with_controller_added_to_context
    @hook_module.add_listener(TestHook3)
    assert_match /controller/i, @view_hook_helper.call_hook(:view_layouts_base_html_head)
  end
    
  def test_call_hook_from_view_with_request_added_to_context
    @hook_module.add_listener(TestHook3)
    assert_match /request/i, @view_hook_helper.call_hook(:view_layouts_base_html_head)
  end

  def test_call_hook_from_view_should_join_responses_with_a_space
    @hook_module.add_listener(TestHook1)
    @hook_module.add_listener(TestHook2)
    assert_equal 'Test hook 1 listener. Test hook 2 listener.',
                 @view_hook_helper.call_hook(:view_layouts_base_html_head)
  end
end