You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hook_test.rb 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require_relative '../../../test_helper'
  19. class HookTest < Redmine::IntegrationTest
  20. fixtures :users, :roles, :projects, :members, :member_roles
  21. # Hooks that are manually registered later
  22. class ProjectBasedTemplate < Redmine::Hook::ViewListener
  23. def view_layouts_base_html_head(context)
  24. # Adds a project stylesheet
  25. stylesheet_link_tag(context[:project].identifier) if context[:project]
  26. end
  27. end
  28. class SidebarContent < Redmine::Hook::ViewListener
  29. def view_layouts_base_sidebar(context)
  30. content_tag('p', 'Sidebar hook')
  31. end
  32. end
  33. Redmine::Hook.clear_listeners
  34. class ContentForInsideHook < Redmine::Hook::ViewListener
  35. render_on :view_welcome_index_left, :inline => <<~VIEW
  36. <% content_for :header_tags do %>
  37. <%= javascript_include_tag 'test_plugin.js', :plugin => 'test_plugin' %>
  38. <%= stylesheet_link_tag 'test_plugin.css', :plugin => 'test_plugin' %>
  39. <% end %>
  40. <p>ContentForInsideHook content</p>
  41. VIEW
  42. end
  43. class SingleRenderOn < Redmine::Hook::ViewListener
  44. render_on :view_welcome_index_left, :inline => 'SingleRenderOn 1'
  45. end
  46. class MultipleRenderOn < Redmine::Hook::ViewListener
  47. render_on :view_welcome_index_left, {:inline => 'MultipleRenderOn 1'}, {:inline => 'MultipleRenderOn 2'}
  48. end
  49. # Hooks that stores the call context
  50. class ContextTestHook < Redmine::Hook::ViewListener
  51. cattr_accessor :context
  52. def controller_account_success_authentication_after(context)
  53. self.class.context = context
  54. end
  55. end
  56. def setup
  57. Redmine::Hook.clear_listeners
  58. end
  59. def teardown
  60. Redmine::Hook.clear_listeners
  61. end
  62. def test_html_head_hook_response
  63. Redmine::Hook.add_listener(ProjectBasedTemplate)
  64. get '/projects/ecookbook'
  65. assert_select 'head link[href=?]', '/stylesheets/ecookbook.css'
  66. end
  67. def test_empty_sidebar_should_be_hidden
  68. get '/'
  69. assert_select 'div#main.nosidebar'
  70. end
  71. def test_sidebar_with_hook_content_should_not_be_hidden
  72. Redmine::Hook.add_listener(SidebarContent)
  73. get '/'
  74. assert_select 'div#sidebar p', :text => 'Sidebar hook'
  75. assert_select 'div#main'
  76. assert_select 'div#main.nosidebar', 0
  77. end
  78. def test_hook_with_content_for_should_append_content
  79. Redmine::Hook.add_listener(ContentForInsideHook)
  80. get '/'
  81. assert_response :success
  82. assert_select 'p', :text => 'ContentForInsideHook content'
  83. assert_select 'head' do
  84. assert_select 'script[src="/assets/plugin_assets/test_plugin/test_plugin.js"]'
  85. assert_select 'link[href="/assets/plugin_assets/test_plugin/test_plugin.css"]'
  86. end
  87. end
  88. def test_controller_hook_context_should_include_request
  89. Redmine::Hook.add_listener(ContextTestHook)
  90. post '/login', :params => {:username => 'admin', :password => 'admin'}
  91. assert_not_nil ContextTestHook.context
  92. context = ContextTestHook.context
  93. assert_kind_of ActionDispatch::Request, context[:request]
  94. assert_kind_of Hash, context[:request].params
  95. assert_kind_of AccountController, context[:hook_caller]
  96. end
  97. def test_multiple_hooks
  98. Redmine::Hook.add_listener(SingleRenderOn)
  99. Redmine::Hook.add_listener(MultipleRenderOn)
  100. get '/'
  101. assert_equal 1, response.body.scan("SingleRenderOn 1 MultipleRenderOn 1 MultipleRenderOn 2").size
  102. end
  103. end