diff --git a/.gitchangelog.rc b/.gitchangelog.rc index e4ff6bda6..0a3cbdad6 100644 --- a/.gitchangelog.rc +++ b/.gitchangelog.rc @@ -88,10 +88,12 @@ section_regexps = [ r'^[aA]ggiornat[aeio]\s+([^\n]*)$', r'^[cC]ambiat[aeio]\s+([^\n]*)$', r'^[mM]odificat[aeio]\s+([^\n]*)$', + r'^[uU]pdates.*$', ]), ('Fix', [ r'^[fF]ix\s*:\s*((dev|use?r|pkg|test|doc)\s*:\s*)?([^\n]*)$', r'^[cC]orrett[aeio]\s+([^\n]*)$', + r'^[fF]ixes\s.*$', r'^Load\s\'loading', ]), diff --git a/.gitignore b/.gitignore index a94f8f278..020f78ad0 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,6 @@ bower.json # Ignore Byebug command history file. .byebug_history .directory + +# Ignore vscode history +.history \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index ec953fa2a..89f127bf4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,27 @@ # Changelog +## development (unreleased) + +### Changes + +* Updates.gitignore. [Liane Hampe] + + Adds .history what is a vs code extension storing the history of all + changes. + +### Fix + +* Fixes usage of drawio_svg_enabled setting. [Liane Hampe] + + If svg is disabled the corresponding checkbox + in _marcro_dialog.html.erb won't be displayed anymore. + + Adds tests for this fix and a check for the settings table. + The table check is necessary for running tests since the table is not + available when loading the code. + + ## v1.2.1 (2022-01-05) ### Fix diff --git a/app/views/redmine_drawio/_macro_dialog.html.erb b/app/views/redmine_drawio/_macro_dialog.html.erb index 8f9f5909d..0396e4c07 100644 --- a/app/views/redmine_drawio/_macro_dialog.html.erb +++ b/app/views/redmine_drawio/_macro_dialog.html.erb @@ -9,6 +9,7 @@

PNG + <% if svg_enabled %> SVG <% end %> diff --git a/app/views/settings/_drawio_settings.html.erb b/app/views/settings/_drawio_settings.html.erb index 5de887b4e..5e00784c8 100644 --- a/app/views/settings/_drawio_settings.html.erb +++ b/app/views/settings/_drawio_settings.html.erb @@ -3,7 +3,7 @@ <%= text_field_tag 'settings[drawio_service_url]', @settings['drawio_service_url'], size: 60 %>
<%= l(:drawio_service_url_hint) %>
- + <%= check_box_tag 'settings[drawio_svg_enabled]', true, @settings['drawio_svg_enabled'] %>
<%= l(:drawio_svg_enabled_hint) %>
diff --git a/init.rb b/init.rb index bda098006..aa45fc1e6 100644 --- a/init.rb +++ b/init.rb @@ -4,7 +4,7 @@ name 'Redmine Drawio plugin' author 'Michele Tessaro' description 'Wiki macro plugin for inserting drawio diagrams into Wiki pages and Issues' - version '1.2.1' + version '1.3.0' url 'https://github.com/mikitex70/redmine_drawio' author_url 'https://github.com/mikitex70' diff --git a/lib/redmine_drawio.rb b/lib/redmine_drawio.rb index 41f7c0c9b..b1e591c3c 100644 --- a/lib/redmine_drawio.rb +++ b/lib/redmine_drawio.rb @@ -7,6 +7,7 @@ # Helpers require 'redmine_drawio/helpers/drawio_dmsf_helper' +require 'redmine_drawio/helpers/drawio_settings_helper' require 'redmine_drawio/helpers/textile_helper' require 'redmine_drawio/helpers/markdown_helper' diff --git a/lib/redmine_drawio/helpers/drawio_settings_helper.rb b/lib/redmine_drawio/helpers/drawio_settings_helper.rb new file mode 100644 index 000000000..0a4246da6 --- /dev/null +++ b/lib/redmine_drawio/helpers/drawio_settings_helper.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +# Copyright (C) 2022 Liane Hampe , xmera. + +## +# Helps to use the plugin settings throughout the code. +# +# @note The table check is necessary for running tests since the table is not +# available when loading this code first. +# +module DrawioSettingsHelper + def self.svg_enabled? + return false unless ActiveRecord::Base.connection.data_source_exists? 'settings' + + Setting[:plugin_redmine_drawio]['drawio_svg_enabled'].present? ? true : false + end +end diff --git a/lib/redmine_drawio/hooks/macro_dialog.rb b/lib/redmine_drawio/hooks/macro_dialog.rb index 40be9b0db..0aaaf2ca4 100644 --- a/lib/redmine_drawio/hooks/macro_dialog.rb +++ b/lib/redmine_drawio/hooks/macro_dialog.rb @@ -1,10 +1,12 @@ # encoding: UTF-8 class RedmineDrawioHookListener < Redmine::Hook::ViewListener - svg_enabled = Setting[:plugin_redmine_drawio]['drawio_svg_enabled'] - svg_enabled = true if svg_enabled.nil? - - render_on :view_layouts_base_body_bottom, :partial => "redmine_drawio/macro_dialog", :locals => { - svg_enabled: svg_enabled - } + include DrawioSettingsHelper + + def view_layouts_base_body_bottom(context = {}) + html = context[:controller].send(:render_to_string, + { partial: 'redmine_drawio/macro_dialog', + locals: { svg_enabled: svg_enabled? } }) + html.html_safe + end end diff --git a/lib/redmine_drawio/macros.rb b/lib/redmine_drawio/macros.rb index e2324c98a..b23d604aa 100644 --- a/lib/redmine_drawio/macros.rb +++ b/lib/redmine_drawio/macros.rb @@ -444,7 +444,5 @@ def js_safe(string) end def svg_enabled? - enabled = Setting.plugin_redmine_drawio['drawio_svg_enabled'] - enabled = true if enabled.nil? - enabled + DrawioSettingsHelper.svg_enabled? end diff --git a/test/authenticate_user.rb b/test/authenticate_user.rb new file mode 100755 index 000000000..8eb0fb008 --- /dev/null +++ b/test/authenticate_user.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +# Copyright (C) 2022 Liane Hampe , xmera. + +module RedmineDrawio + ## + # Provide user login test + # + module AuthenticateUser + module_function + + def log_user(login, password) + login_page + log_user_in(login, password) + assert_equal login, User.find(user_session_id).login + end + + private + + def login_page + User.anonymous + get '/login' + assert_nil user_session_id + assert_response :success + end + + def user_session_id + session[:user_id] + end + + def log_user_in(login, password) + post '/login', params: { + username: login, + password: password + } + end + end +end diff --git a/test/integration/macro_dialog_test.rb b/test/integration/macro_dialog_test.rb new file mode 100644 index 000000000..5efa8eace --- /dev/null +++ b/test/integration/macro_dialog_test.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +# Copyright (C) 2022 Liane Hampe , xmera. + +require File.expand_path('test_helper', File.dirname(__dir__)) +require File.expand_path('authenticate_user', File.dirname(__dir__)) +require File.expand_path('load_fixtures', File.dirname(__dir__)) +require File.expand_path('with_drawio_settings', File.dirname(__dir__)) + +class MacroDialogTest < ActionDispatch::IntegrationTest + include RedmineDrawio::AuthenticateUser + include RedmineDrawio::LoadFixtures + include RedmineDrawio::WithDrawioSettings + + fixtures :users, :email_addresses, :roles + + def teardown + Setting.plugin_redmine_drawio = { drawio_svg_enabled: nil } + end + + test 'render macro dialog' do + render_marcro_dialog + assert_select '#dlg_redmine_drawio' + end + + test 'render macro diaglog without svg' do + with_settings(redmine_drawio({ drawio_svg_enabled: false })) do + render_marcro_dialog + assert_select 'input[value=?]', 'svg', 0 + end + end + + test 'render macro diaglog with svg' do + with_settings(redmine_drawio({ drawio_svg_enabled: true })) do + render_marcro_dialog + assert_select 'input[value=?]', 'svg' + end + end + + private + + def render_marcro_dialog + log_user('admin', 'admin') + get '/settings/plugin/redmine_drawio' + assert_response :success + end +end diff --git a/test/load_fixtures.rb b/test/load_fixtures.rb new file mode 100755 index 000000000..681a9562e --- /dev/null +++ b/test/load_fixtures.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +# Copyright (C) 2022 Liane Hampe , xmera. + +module RedmineDrawio + ## + # Redmine won't load plugin fixtures out-of-the-box. + # This module loads first the plugin fixtures and then Redmine fixtures + # if the listed file does not exist in the plugin's fixture directory. + # + module LoadFixtures + class << self + def fixtures(*table_names) + dir = File.join(File.dirname(__FILE__), '/fixtures') + table_names.each do |file| + create_fixtures(dir, file) if File.exist?("#{dir}/#{file}.yml") + end + super(table_names) + end + + private + + def create_fixtures(dir, file) + ActiveRecord::FixtureSet.create_fixtures(dir, file) + end + end + end +end diff --git a/test/unit/drawio_settings_helper_test.rb b/test/unit/drawio_settings_helper_test.rb new file mode 100644 index 000000000..e3fc6779f --- /dev/null +++ b/test/unit/drawio_settings_helper_test.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +# Copyright (C) 2022 Liane Hampe , xmera. + +require File.expand_path('test_helper', File.dirname(__dir__)) +require File.expand_path('with_drawio_settings', File.dirname(__dir__)) + +class DrawioSettingsHelperTest < ActiveSupport::TestCase + include DrawioSettingsHelper + include RedmineDrawio::WithDrawioSettings + + def teardown + Setting.plugin_redmine_drawio = { drawio_svg_enabled: nil } + end + + def test_svg_disabled + with_settings(redmine_drawio({ drawio_svg_enabled: false })) do + assert_not svg_enabled? + end + end + + def test_svg_enabled + with_settings(redmine_drawio({ drawio_svg_enabled: true })) do + assert svg_enabled? + end + end +end diff --git a/test/with_drawio_settings.rb b/test/with_drawio_settings.rb new file mode 100644 index 000000000..68b6ccc6a --- /dev/null +++ b/test/with_drawio_settings.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +# Copyright (C) 2022 Liane Hampe , xmera. + +module RedmineDrawio + module WithDrawioSettings + def redmine_drawio(**attrs) + { plugin_redmine_drawio: attrs }.with_indifferent_access + end + end +end