|
1 |
# frozen_string_literal: true
|
|
2 |
|
|
3 |
# Redmine - project management software
|
|
4 |
# Copyright (C) 2006-2023 Jean-Philippe Lang
|
|
5 |
#
|
|
6 |
# This program is free software; you can redistribute it and/or
|
|
7 |
# modify it under the terms of the GNU General Public License
|
|
8 |
# as published by the Free Software Foundation; either version 2
|
|
9 |
# of the License, or (at your option) any later version.
|
|
10 |
#
|
|
11 |
# This program is distributed in the hope that it will be useful,
|
|
12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 |
# GNU General Public License for more details.
|
|
15 |
#
|
|
16 |
# You should have received a copy of the GNU General Public License
|
|
17 |
# along with this program; if not, write to the Free Software
|
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
19 |
|
|
20 |
require File.expand_path('../../../test_helper', __FILE__)
|
|
21 |
|
|
22 |
class RoutingPluginsTest < Redmine::RoutingTest
|
|
23 |
setup do
|
|
24 |
@setup_plugin_paths = []
|
|
25 |
@setup_plugin_paths << setup_plugin(
|
|
26 |
:redmine_test_plugin_foo,
|
|
27 |
"config/routes.rb" => <<~ROUTES_CONTENT,
|
|
28 |
resources :plugin_articles, only: %i[index]
|
|
29 |
ROUTES_CONTENT
|
|
30 |
"app/contrtollers/plugin_articles_controller.rb" => <<~CONTROLLER_CONTENT,
|
|
31 |
class PluginArticlesController < ApplicationController
|
|
32 |
def index
|
|
33 |
render plain: "foo PluginArticlesController#index"
|
|
34 |
end
|
|
35 |
end
|
|
36 |
CONTROLLER_CONTENT
|
|
37 |
)
|
|
38 |
@setup_plugin_paths << setup_plugin(
|
|
39 |
:redmine_test_plugin_bar,
|
|
40 |
"config/routes.rb" => <<~ROUTES_CONTENT,
|
|
41 |
# same path helper name with foo's
|
|
42 |
get '/bar_plugin_articles', as: :plugin_articles, to: 'bar_plugin_articles#index'
|
|
43 |
ROUTES_CONTENT
|
|
44 |
"app/contrtollers/bar_plugin_articles_controller.rb" => <<~CONTROLLER_CONTENT,
|
|
45 |
class BarPluginArticlesController < ApplicationController
|
|
46 |
def index
|
|
47 |
render plain: "bar BarPluginArticlesController#index"
|
|
48 |
end
|
|
49 |
end
|
|
50 |
CONTROLLER_CONTENT
|
|
51 |
)
|
|
52 |
Redmine::PluginLoader.load
|
|
53 |
Redmine::PluginLoader.directories.each(&:run_initializer) # to define relative controllers
|
|
54 |
RedmineApp::Application.instance.routes_reloader.reload!
|
|
55 |
end
|
|
56 |
|
|
57 |
teardown do
|
|
58 |
@setup_plugin_paths.each(&:rmtree)
|
|
59 |
Redmine::PluginLoader.load
|
|
60 |
RedmineApp::Application.instance.routes_reloader.reload!
|
|
61 |
end
|
|
62 |
|
|
63 |
def test_plugins
|
|
64 |
should_route 'GET /plugin_articles' => 'plugin_articles#index'
|
|
65 |
should_route 'GET /bar_plugin_articles' => 'bar_plugin_articles#index'
|
|
66 |
assert_equal("/bar_plugin_articles", plugin_articles_path)
|
|
67 |
end
|
|
68 |
|
|
69 |
private
|
|
70 |
|
|
71 |
def setup_plugin(plugin_name, **relative_path_to_content)
|
|
72 |
plugin_path = Redmine::Plugin.directory / plugin_name.to_s
|
|
73 |
plugin_path.mkpath
|
|
74 |
(plugin_path / "init.rb").write(<<~EOS)
|
|
75 |
Redmine::Plugin.register :#{plugin_name} do
|
|
76 |
name 'Test plugin #{plugin_name}'
|
|
77 |
author 'Author name'
|
|
78 |
description 'This is a plugin for Redmine test'
|
|
79 |
version '0.0.1'
|
|
80 |
end
|
|
81 |
|
|
82 |
Pathname(__dir__).glob("app/**/*.rb").sort.each do |path|
|
|
83 |
require path
|
|
84 |
end
|
|
85 |
EOS
|
|
86 |
|
|
87 |
relative_path_to_content.each do |relative_path, content|
|
|
88 |
path = plugin_path / relative_path
|
|
89 |
path.parent.mkpath
|
|
90 |
path.write(content)
|
|
91 |
end
|
|
92 |
|
|
93 |
return plugin_path
|
|
94 |
end
|
|
95 |
end
|