redmine-hooks-svn-1709.patch

Patch to add a Plugin Hook API with unit tests - Eric Davis, 2008-07-29 02:36

Download (12.9 KB)

View differences:

test/unit/lib/redmine/plugin_test.rb (revision 0)
1
require File.dirname(__FILE__) + '/../../../test_helper'
2

  
3
class Redmine::PluginTest < Test::Unit::TestCase
4
  def test_sanity
5
    assert true
6
  end
7
  
8
  def test_add_hook
9
    assert_equal false, Redmine::Plugin::Hook::Manager.hook_registered?(:issue_show)
10
    Redmine::Plugin.add_hook(:issue_show, Proc.new { })
11
    assert Redmine::Plugin::Hook::Manager.hook_registered?(:issue_show)
12
  end
13
  
14
  def test_add_hook_invalid
15
    assert_equal false, Redmine::Plugin::Hook::Manager.hook_registered?(:invalid)
16
    Redmine::Plugin.add_hook(:invalid, Proc.new { })
17
    assert Redmine::Plugin::Hook::Manager.hook_registered?(:invalid)
18
  end
19
end
20

  
test/unit/lib/redmine/plugin_hook_test.rb (revision 0)
1
require File.dirname(__FILE__) + '/../../../test_helper'
2

  
3
class Redmine::Plugin::Hook::ManagerTest < Test::Unit::TestCase
4
  def setup
5
    @manager =  Redmine::Plugin::Hook::Manager
6
  end
7
  
8
  def teardown
9
    @manager.clear_listeners
10
  end
11
  
12
  def test_sanity
13
    assert true
14
  end
15
  
16
  def test_hook_format
17
    assert_kind_of Hash, @manager::hooks
18
    @manager::hooks.each do |hook, registrations|
19
      assert_kind_of Symbol, hook
20
      assert_kind_of Array, registrations
21
      assert_equal 0, registrations.length
22
    end
23
  end
24
  
25
  def test_valid_hook
26
    assert @manager::valid_hook?(:issue_show)
27
  end
28
  
29
  def test_invalid_hook
30
    assert_equal false, @manager::valid_hook?(:an_invalid_hook_name)
31
  end
32
  
33
  def test_clear_listeners
34
    assert_equal 0, @manager::hooks[:issue_show].length
35
    @manager.add_listener(:issue_show, Proc.new { } )
36
    @manager.add_listener(:issue_show, Proc.new { } )
37
    @manager.add_listener(:issue_show, Proc.new { } )
38
    @manager.add_listener(:issue_show, Proc.new { } )
39
    assert_equal 4, @manager::hooks[:issue_show].length
40
    
41
    @manager.clear_listeners
42
    assert_equal 0, @manager::hooks[:issue_show].length
43
  end
44
  
45
  def test_add_listener
46
    assert_equal 0, @manager::hooks[:issue_show].length
47
    @manager.add_listener(:issue_show, Proc.new { } )
48
    assert_equal 1, @manager::hooks[:issue_show].length
49
  end
50
  
51
  def test_add_invalid_listener
52
    hooks = @manager::hooks
53
    @manager.add_listener(:invalid, Proc.new { } )
54
    assert_equal hooks, @manager::hooks
55
  end
56
  
57
  def test_call_hook_with_response
58
    function = Proc.new { return 'response' }
59
    
60
    @manager.add_listener(:issue_show, function)
61
    
62
    assert_equal 'response', @manager.call_hook(:issue_show)
63
  end
64

  
65
  def test_call_multiple_hooks_with_response
66
    function1 = Proc.new { return 'First Call.' }
67
    function2 = Proc.new { return 'Second Call.' }
68
    function3 = Proc.new { return 'Third Call.' }
69
    
70
    @manager.add_listener(:issue_show, function1)
71
    @manager.add_listener(:issue_show, function2)
72
    @manager.add_listener(:issue_show, function3)
73
    
74
    assert_equal 'First Call.Second Call.Third Call.', @manager.call_hook(:issue_show)
75
  end
76

  
77
  def test_call_hook_without_response
78
    function = Proc.new { }
79
    
80
    @manager.add_listener(:issue_show, function)
81
    
82
    assert_equal '', @manager.call_hook(:issue_show)
83
  end
84

  
85
   def test_call_multiple_hooks_without_responses
86
     function1 = Proc.new { }
87
     function2 = Proc.new { }
88
     function3 = Proc.new { }
89
    
90
     @manager.add_listener(:issue_show, function1)
91
     @manager.add_listener(:issue_show, function2)
92
     @manager.add_listener(:issue_show, function3)
93
    
94
     assert_equal '', @manager.call_hook(:issue_show)
95
   end
96

  
97
   def test_hook_registered_yes
98
     @manager.add_listener(:issue_show, Proc.new { })
99
     assert @manager.hook_registered?(:issue_show)
100
   end
101

  
102
    def test_hook_registered_no
103
     assert_equal false, @manager.hook_registered?(:issue_show)
104
   end
105
end
106

  
107
class Redmine::Plugin::Hook::BaseTest < Test::Unit::TestCase
108
  def test_sanity
109
    assert true
110
  end
111
  
112
  def test_help_should_be_a_singleton
113
    assert Redmine::Plugin::Hook::Base::Helper.include?(Singleton)
114
  end
115
  
116
  def test_helper_should_include_actionview_helpers
117
    [ActionView::Helpers::TagHelper,
118
     ActionView::Helpers::FormHelper,
119
     ActionView::Helpers::FormTagHelper,
120
     ActionView::Helpers::FormOptionsHelper,
121
     ActionView::Helpers::JavaScriptHelper, 
122
     ActionView::Helpers::PrototypeHelper,
123
     ActionView::Helpers::NumberHelper,
124
     ActionView::Helpers::UrlHelper].each do |helper|
125
      assert Redmine::Plugin::Hook::Base::Helper.include?(helper), "#{helper} wasn't included."
126
    end
127
  end
128
end
app/helpers/issues_helper.rb (working copy)
86 86
    when 'attachment'
87 87
      label = l(:label_attachment)
88 88
    end
89
       
89

  
90
    Redmine::Plugin::Hook::Manager.call_hook(:issues_helper_show_details, {:detail => detail, :label => label, :value => value, :old_value => old_value })
91
    
90 92
    label ||= detail.prop_key
91 93
    value ||= detail.value
92 94
    old_value ||= detail.old_value
app/controllers/issues_controller.rb (working copy)
223 223
      assigned_to = (params[:assigned_to_id].blank? || params[:assigned_to_id] == 'none') ? nil : User.find_by_id(params[:assigned_to_id])
224 224
      category = (params[:category_id].blank? || params[:category_id] == 'none') ? nil : @project.issue_categories.find_by_id(params[:category_id])
225 225
      fixed_version = (params[:fixed_version_id].blank? || params[:fixed_version_id] == 'none') ? nil : @project.versions.find_by_id(params[:fixed_version_id])
226
      
227 226
      unsaved_issue_ids = []      
228 227
      @issues.each do |issue|
229 228
        journal = issue.init_journal(User.current, params[:notes])
......
234 233
        issue.start_date = params[:start_date] unless params[:start_date].blank?
235 234
        issue.due_date = params[:due_date] unless params[:due_date].blank?
236 235
        issue.done_ratio = params[:done_ratio] unless params[:done_ratio].blank?
236

  
237
        Redmine::Plugin::Hook::Manager.call_hook(:issue_bulk_edit_save, {:params => params, :issue => issue })
238

  
237 239
        # Don't save any change to the issue if the user is not authorized to apply the requested status
238 240
        if (status.nil? || (issue.status.new_status_allowed_to?(status, current_role, issue.tracker) && issue.status = status)) && issue.save
239 241
          # Send notification for each issue (if changed)
app/views/projects/settings/_members.rhtml (working copy)
9 9
	<thead>
10 10
	  <th><%= l(:label_user) %></th>
11 11
	  <th><%= l(:label_role) %></th>
12
          <%= Redmine::Plugin::Hook::Manager.call_hook(:project_member_list_header, {:project => @project }) %>
12 13
	  <th style="width:15%"></th>
13 14
	</thead>
14 15
	<tbody>
......
24 25
      <% end %>
25 26
    <% end %>
26 27
    </td>
28
    <%= Redmine::Plugin::Hook::Manager.call_hook(:project_member_list_column_three, {:project => @project, :member => member }) %>
29

  
27 30
    <td align="center">
28 31
      <%= link_to_remote l(:button_delete), { :url => {:controller => 'members', :action => 'destroy', :id => member},                                              
29 32
                                              :method => :post
app/views/issues/bulk_edit.rhtml (working copy)
38 38
<label><%= l(:field_done_ratio) %>: 
39 39
<%= select_tag 'done_ratio', options_for_select([[l(:label_no_change_option), '']] + (0..10).to_a.collect {|r| ["#{r*10} %", r*10] }) %></label>
40 40
</p>
41
<%= Redmine::Plugin::Hook::Manager.call_hook(:issue_bulk_edit, {:project => @project, :issue => @issues }) %>
41 42
</fieldset>
42 43

  
43 44
<fieldset><legend><%= l(:field_notes) %></legend>
app/views/issues/_form.rhtml (working copy)
48 48
<p><label><%=l(:label_attachment_plural)%></label><%= render :partial => 'attachments/form' %></p>
49 49
<% end %>
50 50

  
51
<%= Redmine::Plugin::Hook::Manager.call_hook(:issue_edit, {:project => @project, :issue => @issue, :form => f }) %>
52

  
51 53
<%= wikitoolbar_for 'issue_description' %>
app/views/issues/show.rhtml (working copy)
53 53
 <%end
54 54
end %>
55 55
</tr>
56
<%= Redmine::Plugin::Hook::Manager.call_hook(:issue_show, {:project => @project, :issue => @issue}) %>
57
       
56 58
</table>
57 59
<hr />
58 60

  
lib/redmine/plugin.rb (working copy)
117 117
      @project_module = nil
118 118
    end
119 119
    
120
    # Registers a +method+ to be called when Redmine runs a hook called
121
    # +hook_name+
122
    #
123
    #   # Run puts whenever the issue_show hook is called
124
    #   add_hook :issue_show, Proc.new { puts 'Hello' }
125
    #
126
    #   # Call the class method +my_method+ passing in all the context
127
    #   add_hook :issue_show, Proc.new {|context| MyPlugin.my_method(context)}
128
    def add_hook(hook_name, method)
129
      Redmine::Plugin::Hook::Manager.add_listener(hook_name, method)
130
    end
131
      
120 132
    # Registers an activity provider.
121 133
    #
122 134
    # Options:
......
147 159
    def configurable?
148 160
      settings && settings.is_a?(Hash) && !settings[:partial].blank?
149 161
    end
162
    
163
    # Hook is used to allow plugins to hook into Redmine at specific sections
164
    # to change it's behavior.  See +Redmine::Plugin.add_hook+ for details.
165
    class Hook
166
      class Manager
167
        # Hooks and the procs added
168
        @@hooks = {
169
          :issue_show => [],
170
          :issue_edit => [],
171
          :issue_bulk_edit => [],
172
          :issue_bulk_edit_save => [],
173
          :issue_update => [],
174
          :project_member_list_header => [],
175
          :project_member_list_column_three => [],
176
          :issues_helper_show_details => []
177
        }
178
        
179
        cattr_reader :hooks
180
      
181
        class << self
182
        
183
          def valid_hook?(hook_name)
184
            return @@hooks.has_key?(hook_name)
185
          end
186

  
187
          # Add +method+ to +hook_name+
188
          def add_listener(hook_name, method)
189
            if valid_hook?(hook_name)
190
              @@hooks[hook_name.to_sym] << method
191
              puts "Listener added for #{hook_name.to_s}"
192
            end
193
          end
194

  
195
          # Removes all listeners
196
          def clear_listeners()
197
            @@hooks.each do |hook, registrations|
198
              @@hooks[hook] = []
199
            end
200
          end
201
        
202
          # Run all the hooks for +hook_name+ passing in +context+
203
          def call_hook(hook_name, context = { })
204
            response = ''
205
            @@hooks[hook_name.to_sym].each do |method|
206
              method_response = method.call(context)
207
              response += method_response unless method_response.nil?
208
            end
209
            response
210
          end
211
        
212
          # Are hooks registered for +hook_name+
213
          def hook_registered?(hook_name)
214
            return @@hooks[hook_name.to_sym].size > 0
215
          end
216
        end
217
      end
218

  
219
      # Base class for Redmin Plugin hooks.
220
      class Base
221
        
222
        # Class level access to Rails' helper methods.
223
        def self.help
224
          Helper.instance
225
        end
226
        
227
        # Includes several Helper methods to be used in the class
228
        class Helper # :nodoc:
229
          include Singleton
230
          include ERB::Util
231
          include ActionView::Helpers::TagHelper
232
          include ActionView::Helpers::FormHelper
233
          include ActionView::Helpers::FormTagHelper
234
          include ActionView::Helpers::FormOptionsHelper
235
          include ActionView::Helpers::JavaScriptHelper 
236
          include ActionView::Helpers::PrototypeHelper
237
          include ActionView::Helpers::NumberHelper
238
          include ActionView::Helpers::UrlHelper
239
          
240
          include ActionController::UrlWriter 
241
          
242
          def protect_against_forgery? # :nodoc:
243
            false
244
          end
245
          
246
        end
247
      end
248
    end
150 249
  end
151 250
end