diff --git a/app/views/admin/plugins.html.erb b/app/views/admin/plugins.html.erb index f98ed90..5ca6965 100644 --- a/app/views/admin/plugins.html.erb +++ b/app/views/admin/plugins.html.erb @@ -1,4 +1,5 @@ <%= title l(:label_plugins) %> +<%= call_hook(:view_admin_plugins_top, :plugins => @plugins) %> <% if @plugins.any? %>
@@ -7,6 +8,7 @@ <%= l(:field_name) %> / <%= l(:field_description) %> <%= l(:field_author) %> <%= l(:label_version) %> + <%= call_hook(:view_admin_plugins_table_header) %> @@ -18,6 +20,7 @@ <%= plugin.author_url.blank? ? plugin.author : link_to(plugin.author, plugin.author_url) %> <%= plugin.version %> + <%= call_hook(:view_admin_plugins_table_row, :plugin => plugin) %> <%= link_to(l(:button_configure), plugin_settings_path(plugin)) if plugin.configurable? %> <% end %> diff --git a/app/views/custom_fields/_index.html.erb b/app/views/custom_fields/_index.html.erb index fe40745..5390c77 100644 --- a/app/views/custom_fields/_index.html.erb +++ b/app/views/custom_fields/_index.html.erb @@ -7,6 +7,7 @@ <%=l(:field_is_for_all)%> <%=l(:label_used_by)%> <% end %> + <%= call_hook(:view_custom_fields_index_table_header, :custom_field_type => tab[:name]) %> @@ -20,6 +21,7 @@ <%= checked_image custom_field.is_for_all? %> <%= l(:label_x_projects, :count => @custom_fields_projects_count[custom_field.id] || 0) if custom_field.is_a? IssueCustomField and !custom_field.is_for_all? %> <% end %> + <%= call_hook(:view_custom_fields_index_table_row, :custom_field => custom_field) %> <%= reorder_handle(custom_field, :url => custom_field_path(custom_field), :param => 'custom_field') %> <%= link_to_function sprite_icon('copy', l(:button_copy)), "location.href = '#{new_custom_field_path(:copy => custom_field)}&type=' + encodeURIComponent(($('.tabs a.selected').attr('id')||'').split('tab-').pop())", :class => 'icon icon-copy' %> diff --git a/app/views/enumerations/_form.html.erb b/app/views/enumerations/_form.html.erb index 8ff774d..a6097b3 100644 --- a/app/views/enumerations/_form.html.erb +++ b/app/views/enumerations/_form.html.erb @@ -1,6 +1,7 @@ <%= error_messages_for 'enumeration' %>
+ <%= call_hook(:view_enumerations_form_top, :enumeration => @enumeration, :form => f) %>

<%= f.text_field :name %>

<%= f.check_box :active %>

<%= f.check_box :is_default %>

@@ -8,4 +9,5 @@ <% @enumeration.custom_field_values.each do |value| %>

<%= custom_field_tag_with_label :enumeration, value %>

<% end %> + <%= call_hook(:view_enumerations_form_bottom, :enumeration => @enumeration, :form => f) %>
diff --git a/app/views/enumerations/index.html.erb b/app/views/enumerations/index.html.erb index fa47ba8..0c9703e 100644 --- a/app/views/enumerations/index.html.erb +++ b/app/views/enumerations/index.html.erb @@ -13,6 +13,7 @@ <%= l(:field_name) %> <%= l(:field_is_default) %> <%= l(:field_active) %> + <%= call_hook(:view_enumerations_index_table_header, :enumeration_class => klass) %> <% enumerations.each do |enumeration| %> @@ -20,6 +21,7 @@ <%= link_to enumeration, edit_enumeration_path(enumeration) %> <%= checked_image enumeration.is_default? %> <%= checked_image enumeration.active? %> + <%= call_hook(:view_enumerations_index_table_row, :enumeration => enumeration) %> <%= reorder_handle(enumeration, :url => enumeration_path(enumeration), :param => 'enumeration') %> <%= delete_link enumeration_path(enumeration) %> diff --git a/app/views/groups/index.html.erb b/app/views/groups/index.html.erb index 88c6d9d..7c1ae71 100644 --- a/app/views/groups/index.html.erb +++ b/app/views/groups/index.html.erb @@ -20,6 +20,7 @@ <%=l(:label_group)%> <%=l(:label_user_plural)%> + <%= call_hook(:view_groups_index_table_header) %> @@ -27,6 +28,7 @@ "> <%= link_to group, edit_group_path(group) %> <%= (@user_count_by_group_id[group.id] || 0) unless group.builtin? %> + <%= call_hook(:view_groups_index_table_row, :group => group) %> <%= delete_link group unless group.builtin? %> <% end %> diff --git a/app/views/issue_statuses/_form.html.erb b/app/views/issue_statuses/_form.html.erb index ba58f03..47dcac2 100644 --- a/app/views/issue_statuses/_form.html.erb +++ b/app/views/issue_statuses/_form.html.erb @@ -1,6 +1,7 @@ <%= error_messages_for 'issue_status' %>
+<%= call_hook(:view_issue_statuses_form_top, :issue_status => @issue_status, :form => f) %>

<%= f.text_field :name, :required => true %>

<%= f.textarea :description, :rows => 4 %>

<% if Issue.use_status_for_done_ratio? %> @@ -9,4 +10,5 @@

<%= f.check_box :is_closed %>

<%= call_hook(:view_issue_statuses_form, :issue_status => @issue_status) %> +<%= call_hook(:view_issue_statuses_form_bottom, :issue_status => @issue_status, :form => f) %>
diff --git a/app/views/issue_statuses/index.html.erb b/app/views/issue_statuses/index.html.erb index c38e3b8..7a25f32 100644 --- a/app/views/issue_statuses/index.html.erb +++ b/app/views/issue_statuses/index.html.erb @@ -14,6 +14,7 @@ <%=l(:field_is_closed)%> <%=l(:field_description)%> + <%= call_hook(:view_issue_statuses_index_table_header) %> @@ -32,6 +33,7 @@ <% end %> + <%= call_hook(:view_issue_statuses_index_table_row, :issue_status => status) %> <%= reorder_handle(status) %> <%= delete_link issue_status_path(status) %> diff --git a/app/views/roles/_form.html.erb b/app/views/roles/_form.html.erb index 44bc54c..60be67d 100644 --- a/app/views/roles/_form.html.erb +++ b/app/views/roles/_form.html.erb @@ -1,6 +1,7 @@ <%= error_messages_for 'role' %>
+ <%= call_hook(:view_roles_form_top, :role => @role, :form => f) %> <% unless @role.builtin? %>

<%= f.text_field :name, :required => true %>

<%= f.check_box :assignable %>

@@ -47,6 +48,7 @@

<%= select_tag(:copy_workflow_from, content_tag("option") + options_from_collection_for_select(@roles, :id, :name, params[:copy_workflow_from] || @copy_from.try(:id))) %>

<% end %> + <%= call_hook(:view_roles_form_bottom, :role => @role, :form => f) %>

<%= l(:label_permissions) %>

diff --git a/app/views/roles/index.html.erb b/app/views/roles/index.html.erb index 9705fdd..8180dac 100644 --- a/app/views/roles/index.html.erb +++ b/app/views/roles/index.html.erb @@ -9,6 +9,7 @@ <%=l(:label_role)%> + <%= call_hook(:view_roles_index_table_header) %> @@ -22,6 +23,7 @@ <% end %> + <%= call_hook(:view_roles_index_table_row, :role => role) %> <%= reorder_handle(role) unless role.builtin? %> <%= link_to sprite_icon('copy', l(:button_copy)), new_role_path(:copy => role), :class => 'icon icon-copy' %> diff --git a/app/views/trackers/index.html.erb b/app/views/trackers/index.html.erb index 12dc0aa..aed8a96 100644 --- a/app/views/trackers/index.html.erb +++ b/app/views/trackers/index.html.erb @@ -11,6 +11,7 @@ <%=l(:field_default_status)%> <%=l(:field_description)%> + <%= call_hook(:view_trackers_index_table_header) %> @@ -26,6 +27,7 @@ <% end %> + <%= call_hook(:view_trackers_index_table_row, :tracker => tracker) %> <%= reorder_handle(tracker) %> <%= link_to sprite_icon('copy', l(:button_copy)), new_tracker_path(:copy => tracker), :class => 'icon icon-copy' %> diff --git a/doc/HOOKS b/doc/HOOKS index ca232ff..480ad02 100644 --- a/doc/HOOKS +++ b/doc/HOOKS @@ -151,6 +151,21 @@ view_account_right_bottom Context: :user Called in: app/views/users/show.html.erb +view_admin_plugins_table_header + Since: 7.1.0 + Context: - + Called in: app/views/admin/plugins.html.erb + +view_admin_plugins_table_row + Since: 7.1.0 + Context: :plugin + Called in: app/views/admin/plugins.html.erb + +view_admin_plugins_top + Since: 7.1.0 + Context: :plugins + Called in: app/views/admin/plugins.html.erb + view_admin_projects_sidebar_queries_bottom Since: - Context: - @@ -171,6 +186,16 @@ view_custom_fields_form_upper_box Context: :custom_field, :form Called in: app/views/custom_fields/_form.html.erb +view_custom_fields_index_table_header + Since: 7.1.0 + Context: :custom_field_type + Called in: app/views/custom_fields/_index.html.erb + +view_custom_fields_index_table_row + Since: 7.1.0 + Context: :custom_field + Called in: app/views/custom_fields/_index.html.erb + view_documents_index_contextual Since: 7.1.0 Context: :project @@ -181,6 +206,26 @@ view_documents_show_contextual Context: :document, :project Called in: app/views/documents/show.html.erb +view_enumerations_form_bottom + Since: 7.1.0 + Context: :enumeration, :form + Called in: app/views/enumerations/_form.html.erb + +view_enumerations_form_top + Since: 7.1.0 + Context: :enumeration, :form + Called in: app/views/enumerations/_form.html.erb + +view_enumerations_index_table_header + Since: 7.1.0 + Context: :enumeration_class + Called in: app/views/enumerations/index.html.erb + +view_enumerations_index_table_row + Since: 7.1.0 + Context: :enumeration + Called in: app/views/enumerations/index.html.erb + view_groups_form_bottom Since: 7.1.0 Context: :group, :form @@ -191,6 +236,16 @@ view_groups_form_top Context: :group, :form Called in: app/views/groups/_form.html.erb +view_groups_index_table_header + Since: 7.1.0 + Context: - + Called in: app/views/groups/index.html.erb + +view_groups_index_table_row + Since: 7.1.0 + Context: :group + Called in: app/views/groups/index.html.erb + view_groups_memberships_table_header Since: - Context: :group @@ -211,6 +266,26 @@ view_issue_statuses_form Context: :issue_status Called in: app/views/issue_statuses/_form.html.erb +view_issue_statuses_form_bottom + Since: 7.1.0 + Context: :issue_status, :form + Called in: app/views/issue_statuses/_form.html.erb + +view_issue_statuses_form_top + Since: 7.1.0 + Context: :issue_status, :form + Called in: app/views/issue_statuses/_form.html.erb + +view_issue_statuses_index_table_header + Since: 7.1.0 + Context: - + Called in: app/views/issue_statuses/index.html.erb + +view_issue_statuses_index_table_row + Since: 7.1.0 + Context: :issue_status + Called in: app/views/issue_statuses/index.html.erb + view_issues_bulk_edit_details_bottom Since: - Context: :issues @@ -579,6 +654,26 @@ view_repositories_show_contextual Called in: app/views/repositories/entry.html.erb Called in: app/views/repositories/show.html.erb +view_roles_form_bottom + Since: 7.1.0 + Context: :role, :form + Called in: app/views/roles/_form.html.erb + +view_roles_form_top + Since: 7.1.0 + Context: :role, :form + Called in: app/views/roles/_form.html.erb + +view_roles_index_table_header + Since: 7.1.0 + Context: - + Called in: app/views/roles/index.html.erb + +view_roles_index_table_row + Since: 7.1.0 + Context: :role + Called in: app/views/roles/index.html.erb + view_search_index_options_content_bottom Since: - Context: - @@ -629,6 +724,16 @@ view_trackers_form_top Context: :tracker, :form Called in: app/views/trackers/_form.html.erb +view_trackers_index_table_header + Since: 7.1.0 + Context: - + Called in: app/views/trackers/index.html.erb + +view_trackers_index_table_row + Since: 7.1.0 + Context: :tracker + Called in: app/views/trackers/index.html.erb + view_users_form Since: - Context: :user, :form diff --git a/test/integration/lib/redmine/admin_list_hooks_test.rb b/test/integration/lib/redmine/admin_list_hooks_test.rb new file mode 100644 index 0000000..cf38559 --- /dev/null +++ b/test/integration/lib/redmine/admin_list_hooks_test.rb @@ -0,0 +1,183 @@ +# frozen_string_literal: true + +# Redmine - project management software +# Copyright (C) 2006- 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_relative '../../../test_helper' + +# The hooks that let plugins add columns to the administration's lists and to +# the plugin list, and fields to the forms of statuses, roles and enumerations: +# each one is called on its page, the added cells come before the buttons and +# match the added headers, and what a plugin adds leaves the page's HTML as +# valid as it was. +class AdminListHooksTest < Redmine::IntegrationTest + # The list pages, the table on each one, its header and row hooks and the + # class of the last cell of a row (buttons by default) + LISTS = { + '/trackers' => ['table.list.trackers', 'view_trackers_index_table'], + '/issue_statuses' => ['table.list.issue_statuses', 'view_issue_statuses_index_table'], + '/roles' => ['table.list.roles', 'view_roles_index_table'], + '/enumerations' => ['table.list.enumerations', 'view_enumerations_index_table'], + '/groups' => ['table.list.groups', 'view_groups_index_table'], + '/custom_fields' => ['table.list.custom_fields', 'view_custom_fields_index_table'], + '/admin/plugins' => ['table.list.plugins', 'view_admin_plugins_table', 'configure'] + } + # The forms and their top and bottom hooks + FORMS = { + '/issue_statuses/1/edit' => 'view_issue_statuses_form', + '/issue_statuses/new' => 'view_issue_statuses_form', + '/roles/1/edit' => 'view_roles_form', + '/roles/new' => 'view_roles_form', + '/enumerations/new?type=IssuePriority' => 'view_enumerations_form' + } + + # Answers every hook with a marker that is valid where the hook is called, + # and remembers the context it was given + class MarkerListener < Redmine::Hook::ViewListener + cattr_accessor :contexts + self.contexts = Hash.new {|h, k| h[k] = []} + + LISTS.each_value do |_table, prefix, _last| + define_method(:"#{prefix}_header") do |context| + contexts["#{prefix}_header"] << context + content_tag('th', 'marker', :class => "hook-#{prefix}_header") + end + define_method(:"#{prefix}_row") do |context| + contexts["#{prefix}_row"] << context + content_tag('td', 'marker', :class => "hook-#{prefix}_row") + end + end + FORMS.each_value.uniq.each do |prefix| + %w(top bottom).each do |place| + define_method(:"#{prefix}_#{place}") do |context| + contexts["#{prefix}_#{place}"] << context + content_tag('p', 'marker', :class => "hook-#{prefix}_#{place}") + end + end + end + define_method(:view_admin_plugins_top) do |context| + contexts['view_admin_plugins_top'] << context + content_tag('div', 'marker', :class => 'hook-view_admin_plugins_top') + end + end + + def setup + Redmine::Hook.clear_listeners + MarkerListener.contexts.clear + # The plugin list has a table only when a plugin is registered + Redmine::Plugin.register :admin_list_hooks_test_plugin do + name 'Test plugin' + directory 'test/fixtures/plugins/foo_plugin' + end + log_user('admin', 'admin') + end + + def teardown + Redmine::Hook.clear_listeners + Redmine::Plugin.unregister(:admin_list_hooks_test_plugin) + end + + def html_errors + Nokogiri::HTML5(response.body, :max_errors => 100).errors.map(&:to_s) + end + + def test_list_hooks_should_add_a_header_and_a_cell_before_the_buttons + Redmine::Hook.add_listener(MarkerListener) + LISTS.each do |path, (table, prefix, last)| + get path + assert_response :success, path + assert_select "#{table} thead th.hook-#{prefix}_header", {:minimum => 1}, "#{prefix}_header not called on #{path}" + assert_select "#{table} thead th.hook-#{prefix}_header + th:last-child", {:minimum => 1}, + "#{prefix}_header not before the buttons' column on #{path}" + assert_select "#{table} td.hook-#{prefix}_row + td.#{last || 'buttons'}", {:minimum => 1}, + "#{prefix}_row not before the buttons on #{path}" + end + end + + def test_every_row_should_have_as_many_cells_as_the_header + Redmine::Hook.add_listener(MarkerListener) + LISTS.each do |path, (table, _prefix)| + get path + css_select(table).each do |t| + headers = t.css('thead th').size + t.css('tr').reject {|tr| tr.ancestors('thead').any?}.each do |tr| + assert_equal headers, tr.css('td').size, "A row of #{table} on #{path} has another number of cells than its header" + end + end + end + end + + def test_list_hooks_should_get_the_object_of_their_row + Redmine::Hook.add_listener(MarkerListener) + get '/trackers' + assert_equal Tracker.sorted.to_a, MarkerListener.contexts['view_trackers_index_table_row'].pluck(:tracker) + get '/issue_statuses' + assert_equal IssueStatus.sorted.to_a, MarkerListener.contexts['view_issue_statuses_index_table_row'].pluck(:issue_status) + get '/roles' + assert_equal Role.sorted.to_a, MarkerListener.contexts['view_roles_index_table_row'].pluck(:role) + get '/enumerations' + assert_includes MarkerListener.contexts['view_enumerations_index_table_header'].pluck(:enumeration_class), IssuePriority + assert_includes MarkerListener.contexts['view_enumerations_index_table_row'].pluck(:enumeration), IssuePriority.shared.first + get '/groups' + assert_includes MarkerListener.contexts['view_groups_index_table_row'].pluck(:group), Group.find(10) + get '/custom_fields' + assert_includes MarkerListener.contexts['view_custom_fields_index_table_header'].pluck(:custom_field_type), 'IssueCustomField' + assert_includes MarkerListener.contexts['view_custom_fields_index_table_row'].pluck(:custom_field), CustomField.find(1) + get '/admin/plugins' + assert_includes MarkerListener.contexts['view_admin_plugins_table_row'].pluck(:plugin), + Redmine::Plugin.find(:admin_list_hooks_test_plugin) + end + + def test_plugins_top_hook_should_be_under_the_title + Redmine::Hook.add_listener(MarkerListener) + get '/admin/plugins' + assert_select 'h2 + div.hook-view_admin_plugins_top + div.autoscroll table.list.plugins' + assert_includes MarkerListener.contexts['view_admin_plugins_top'].last[:plugins], + Redmine::Plugin.find(:admin_list_hooks_test_plugin) + end + + def test_form_hooks_should_be_first_and_last_in_the_box + Redmine::Hook.add_listener(MarkerListener) + FORMS.each do |path, prefix| + get path + assert_response :success, path + assert_select "div.box.tabular > p.hook-#{prefix}_top:first-child", {:count => 1}, "#{prefix}_top not first on #{path}" + assert_select "div.box.tabular > p.hook-#{prefix}_bottom:last-child", {:count => 1}, "#{prefix}_bottom not last on #{path}" + assert MarkerListener.contexts["#{prefix}_top"].last[:form], "#{prefix}_top has no form on #{path}" + end + end + + def test_the_existing_issue_status_form_hook_should_still_be_called + listener = Class.new(Redmine::Hook::ViewListener) do + define_method(:view_issue_statuses_form) {|context| content_tag('p', context[:issue_status].name, :class => 'old-hook')} + end + Redmine::Hook.add_listener(listener) + get '/issue_statuses/1/edit' + assert_select 'p.old-hook', :text => IssueStatus.find(1).name + end + + def test_hook_output_should_not_make_the_html_invalid + (LISTS.keys + FORMS.keys).each do |path| + Redmine::Hook.clear_listeners + get path + without = html_errors + Redmine::Hook.add_listener(MarkerListener) + get path + assert_equal without, html_errors, "The hooks made the HTML of #{path} invalid" + end + end +end