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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 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 File.expand_path('../../../../test_helper', __FILE__)
  19. class Redmine::Hook::ManagerTest < ActionView::TestCase
  20. fixtures :projects, :users, :members, :member_roles, :roles,
  21. :groups_users,
  22. :email_addresses,
  23. :trackers, :projects_trackers,
  24. :enabled_modules,
  25. :versions,
  26. :issue_statuses, :issue_categories, :issue_relations,
  27. :enumerations,
  28. :issues
  29. # Some hooks that are manually registered in these tests
  30. class TestHook < Redmine::Hook::ViewListener; end
  31. class TestHook1 < TestHook
  32. def view_layouts_base_html_head(context)
  33. 'Test hook 1 listener.'
  34. end
  35. end
  36. class TestHook2 < TestHook
  37. def view_layouts_base_html_head(context)
  38. 'Test hook 2 listener.'
  39. end
  40. end
  41. class TestHook3 < TestHook
  42. def view_layouts_base_html_head(context)
  43. "Context keys: #{context.keys.collect(&:to_s).sort.join(', ')}."
  44. end
  45. end
  46. class TestLinkToHook < TestHook
  47. def view_layouts_base_html_head(context)
  48. link_to('Issues', :controller => 'issues')
  49. end
  50. end
  51. class TestHookHelperController < ActionController::Base
  52. include Redmine::Hook::Helper
  53. end
  54. class TestHookHelperView < ActionView::Base
  55. include Redmine::Hook::Helper
  56. end
  57. Redmine::Hook.clear_listeners
  58. def setup
  59. @hook_module = Redmine::Hook
  60. @hook_module.clear_listeners
  61. end
  62. def teardown
  63. @hook_module.clear_listeners
  64. end
  65. def test_clear_listeners
  66. assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size
  67. @hook_module.add_listener(TestHook1)
  68. @hook_module.add_listener(TestHook2)
  69. assert_equal 2, @hook_module.hook_listeners(:view_layouts_base_html_head).size
  70. @hook_module.clear_listeners
  71. assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size
  72. end
  73. def test_add_listener
  74. assert_equal 0, @hook_module.hook_listeners(:view_layouts_base_html_head).size
  75. @hook_module.add_listener(TestHook1)
  76. assert_equal 1, @hook_module.hook_listeners(:view_layouts_base_html_head).size
  77. end
  78. def test_call_hook
  79. @hook_module.add_listener(TestHook1)
  80. assert_equal ['Test hook 1 listener.'], hook_helper.call_hook(:view_layouts_base_html_head)
  81. end
  82. def test_call_hook_with_context
  83. @hook_module.add_listener(TestHook3)
  84. assert_equal ['Context keys: bar, controller, foo, hook_caller, project, request.'],
  85. hook_helper.call_hook(:view_layouts_base_html_head, :foo => 1, :bar => 'a')
  86. end
  87. def test_call_hook_with_multiple_listeners
  88. @hook_module.add_listener(TestHook1)
  89. @hook_module.add_listener(TestHook2)
  90. assert_equal ['Test hook 1 listener.', 'Test hook 2 listener.'], hook_helper.call_hook(:view_layouts_base_html_head)
  91. end
  92. # Context: Redmine::Hook::Helper.call_hook default_url
  93. def test_call_hook_default_url_options
  94. @hook_module.add_listener(TestLinkToHook)
  95. assert_equal ['<a href="/issues">Issues</a>'], hook_helper.call_hook(:view_layouts_base_html_head)
  96. end
  97. def test_view_hook_should_generate_links_with_relative_url_root
  98. Redmine::Utils.relative_url_root = '/foo'
  99. @hook_module.add_listener(TestLinkToHook)
  100. assert_equal ['<a href="/foo/issues">Issues</a>'], hook_helper.call_hook(:view_layouts_base_html_head)
  101. ensure
  102. Redmine::Utils.relative_url_root = ''
  103. end
  104. # Context: Redmine::Hook::Helper.call_hook
  105. def test_call_hook_with_project_added_to_context
  106. @hook_module.add_listener(TestHook3)
  107. assert_match /project/i, hook_helper.call_hook(:view_layouts_base_html_head)[0]
  108. end
  109. def test_call_hook_from_controller_with_controller_added_to_context
  110. @hook_module.add_listener(TestHook3)
  111. assert_match /controller/i, hook_helper.call_hook(:view_layouts_base_html_head)[0]
  112. end
  113. def test_call_hook_from_controller_with_request_added_to_context
  114. @hook_module.add_listener(TestHook3)
  115. assert_match /request/i, hook_helper.call_hook(:view_layouts_base_html_head)[0]
  116. end
  117. def test_call_hook_from_view_with_project_added_to_context
  118. @hook_module.add_listener(TestHook3)
  119. assert_match /project/i, view_hook_helper.call_hook(:view_layouts_base_html_head)
  120. end
  121. def test_call_hook_from_view_with_controller_added_to_context
  122. @hook_module.add_listener(TestHook3)
  123. assert_match /controller/i, view_hook_helper.call_hook(:view_layouts_base_html_head)
  124. end
  125. def test_call_hook_from_view_with_request_added_to_context
  126. @hook_module.add_listener(TestHook3)
  127. assert_match /request/i, view_hook_helper.call_hook(:view_layouts_base_html_head)
  128. end
  129. def test_call_hook_from_view_should_join_responses_with_a_space
  130. @hook_module.add_listener(TestHook1)
  131. @hook_module.add_listener(TestHook2)
  132. assert_equal 'Test hook 1 listener. Test hook 2 listener.',
  133. view_hook_helper.call_hook(:view_layouts_base_html_head)
  134. end
  135. def test_call_hook_should_not_change_the_default_url_for_email_notifications
  136. issue = Issue.find(1)
  137. ActionMailer::Base.deliveries.clear
  138. Mailer.deliver_issue_add(issue)
  139. mail = ActionMailer::Base.deliveries.last
  140. @hook_module.add_listener(TestLinkToHook)
  141. hook_helper.call_hook(:view_layouts_base_html_head)
  142. ActionMailer::Base.deliveries.clear
  143. Mailer.deliver_issue_add(issue)
  144. mail2 = ActionMailer::Base.deliveries.last
  145. assert_equal mail_body(mail), mail_body(mail2)
  146. end
  147. def hook_helper
  148. @hook_helper ||= TestHookHelperController.new
  149. end
  150. def view_hook_helper
  151. @view_hook_helper ||= TestHookHelperView.new(Rails.root.to_s + '/app/views')
  152. end
  153. end