From bf87b60215afadf440cb30606562f27c79581203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Pi=C4=8Dman?= Date: Fri, 6 Oct 2023 09:33:55 +0200 Subject: [PATCH] #324 Tests & Rubocop --- CHANGELOG.md | 3 +- README.md | 5 +- app/models/custom_workflow.rb | 9 ++- config/locales/cs.yml | 7 +- config/locales/es.yml | 5 +- config/locales/pt-BR.yml | 3 + init.rb | 2 +- lib/redmine_custom_workflows.rb | 2 + .../controllers/members_controller_patch.rb | 79 +++++++++++++++++++ .../patches/models/member_patch.rb | 2 +- test/fixtures/custom_workflows.yml | 24 +++++- test/fixtures/custom_workflows_projects.yml | 4 + .../members_controller_patch_test.rb | 45 +++++++++++ test/unit/member_patch_test.rb | 45 +++++++++++ test/unit/user_patch_test.rb | 2 +- 15 files changed, 223 insertions(+), 14 deletions(-) create mode 100644 lib/redmine_custom_workflows/patches/controllers/members_controller_patch.rb create mode 100644 test/functional/members_controller_patch_test.rb create mode 100644 test/unit/member_patch_test.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 67e99a7..d943090 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ Changelog for Custom Workflows ============================== -2.0.10 *????-??-??* +2.1.0 *????-??-??* ------------------- + Member as an observable object 2.0.9 *2023-06-06* ------------------ diff --git a/README.md b/README.md index 988d837..5785d17 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Custom Workflows plug-in 2.0.10 devel +Custom Workflows plug-in 2.1.0 devel ===================================== [![GitHub CI](https://github.com/anteo/redmine_custom_workflows/actions/workflows/rubyonrails.yml/badge.svg?branch=devel)](https://github.com/anteo/redmine_custom_workflows/actions/workflows/rubyonrails.yml) @@ -21,6 +21,7 @@ Supported observable objects: * Issue relations * Time entry * User +* Member * Version * Wiki * \ @@ -55,7 +56,7 @@ Installation From a ZIP file: * Download the latest version of the plugin. -* In case of an upgrade remove the original *plugins/redmine_custom_workflows* folder. +* In case of an upgrade, remove the original *plugins/redmine_custom_workflows* folder. * Unzip it to /plugins. From the Git repository: diff --git a/app/models/custom_workflow.rb b/app/models/custom_workflow.rb index fe6d3ed..6513395 100644 --- a/app/models/custom_workflow.rb +++ b/app/models/custom_workflow.rb @@ -21,12 +21,13 @@ # Custom workflow model class CustomWorkflow < ApplicationRecord - OBSERVABLES = %i[issue issue_relation issue_attachments user member attachment group group_users project project_attachments - wiki_content wiki_page_attachments time_entry version shared].freeze + OBSERVABLES = %i[issue issue_relation issue_attachments user member attachment group group_users project + project_attachments wiki_content wiki_page_attachments time_entry version shared].freeze PROJECT_OBSERVABLES = %i[issue issue_attachments project project_attachments wiki_content wiki_page_attachments - time_entry version].freeze + time_entry version member].freeze COLLECTION_OBSERVABLES = %i[group_users issue_attachments project_attachments wiki_page_attachments].freeze - SINGLE_OBSERVABLES = %i[issue issue_relation user member group attachment project wiki_content time_entry version].freeze + SINGLE_OBSERVABLES = %i[issue issue_relation user member group attachment project wiki_content time_entry version] + .freeze acts_as_list diff --git a/config/locales/cs.yml b/config/locales/cs.yml index b5a4b4b..d2a6a4a 100644 --- a/config/locales/cs.yml +++ b/config/locales/cs.yml @@ -76,17 +76,19 @@ cs: text_custom_workflow_issue_relation_code_note: Skripty jsou vykonávány v rámci objektu vazby mezi úkoly stejně jako volání `before_save` a `after_save` funkcí. Takže lze použít přímo vlastností vazby mezi úkoly. Proměnné (@variable) jsou rovněž dostupné a lze jich použít. - text_custom_workflow_shared_code_note: Tento kód bude spuštěn pře jakýmkoli jiným procesem a může obsahovat sdílený + text_custom_workflow_shared_code_note: Tento kód bude spuštěn před jakýmkoli jiným procesem a může obsahovat sdílený kód, např. funkce a třídy, které jsou potřeba v jiných procesech text_custom_workflow_user_code_note: Skripty jsou vykonávány v rámci objektu uživatel, když se objekt mění (maže). Můžete přímo použít metody a vlastnosti objektu. + text_custom_workflow_member_code_note: Skripty jsou vykonávány v rámci objektu člen, když se objekt mění (maže). + Můžete přímo použít metody a vlastnosti objektu. text_custom_workflow_group_code_note: Skripty jsou vykonávány v rámci objektu skupina, když se objekt mění (maže). Můžete přímo použít metody a vlastnosti objektu. text_custom_workflow_group_users_code_note: Tyto skripty jsou vykonávány, když je uživatel přidán nebo odebrán ze skupiny. Pro přístup k objektům použijte proměnné @user a @group. text_custom_workflow_attachment_code_note: Skripty jsou vykonávány v rámci objektu příloha, když se tento objekt mění (maže se). Můžete přímo použít metody a vlastnosti objektu. Mějte na paměti, že se to týká všech typů příloh - (u úkolů, dokumentů, wiki stránek atd.), takže byste měli zkontrolovat proměnnou 'container_type' pro určení, + (u úkolů, dokumentů, wiki stránek, atd.), takže byste měli zkontrolovat proměnnou 'container_type' pro určení, o jakou přílohu se jedná. text_custom_workflow_issue_attachments_code_note: Tento skripty se vykonávají když je příloha přidána nebo odebrába z úkolu. Můžete použít proměnné @issue a @attachment pro přístup k těmto objektům ve skriptu. @@ -115,6 +117,7 @@ cs: custom_workflow_observable_group: Skupina custom_workflow_observable_user: Uživatel custom_workflow_observable_attachment: Příloha + custom_workflow_observable_member: Člen custom_workflow_observable_project: Projekt custom_workflow_observable_project_attachments: Soubory custom_workflow_observable_wiki_content: Wiki diff --git a/config/locales/es.yml b/config/locales/es.yml index 8d6d88e..48010ea 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -79,7 +79,9 @@ es: text_custom_workflow_shared_code_note: Este código se ejecutará antes que cualquier otro workflow y puede contener código compartido, por ejemplo, funciones y clases necesarias para otros flujos de trabajo text_custom_workflow_user_code_note: Los scripts se ejecutan en el contexto del objeto User cuando el usuario cambia - (destruye). Utilice ​métodos y propiedades del usuario directamente (o a través de `self`) + (destruye). Utilice métodos y propiedades del usuario directamente (o a través de `self`) + text_custom_workflow_member_code_note: Scripts are executed in the context of Member object when member object changes + (destroys). Use methods and properties of the @member directly (or through `self`) text_custom_workflow_group_code_note: Los scripts se ejecutan en el contexto del objeto Group cuando el grupo cambia (destruye). Utilice métodos y propiedades del grupo directamente (o a través de `self`) text_custom_workflow_group_users_code_note: Estos scripts se ejecutan cuando el usuario se añade al grupo/se elimina del @@ -115,6 +117,7 @@ es: custom_workflow_observable_issue_attachments: Ficheros de petición custom_workflow_observable_group: Grupo custom_workflow_observable_user: Usuario + custom_workflow_observable_member: Miembro custom_workflow_observable_attachment: Ficheros custom_workflow_observable_project: Proyecto custom_workflow_observable_project_attachments: Adjuntos de proyecto / Ficheros diff --git a/config/locales/pt-BR.yml b/config/locales/pt-BR.yml index 02f8722..e63df14 100644 --- a/config/locales/pt-BR.yml +++ b/config/locales/pt-BR.yml @@ -80,6 +80,8 @@ pt-BR: e.g. functions and classes needed by other workflows. text_custom_workflow_user_code_note: Both scripts are executed in the context of the user object when user object changes. Use methods and properties of the user directly (or through `self`) + text_custom_workflow_member_code_note: Scripts are executed in the context of Member object when member object changes + (destroys). Use methods and properties of the @member directly (or through `self`) text_custom_workflow_group_code_note: Both scripts are executed in the context of the group object when group object changes. Use methods and properties of the group directly (or through `self`) text_custom_workflow_group_users_code_note: These scripts are executed when user being added to group/removed from @@ -115,6 +117,7 @@ pt-BR: custom_workflow_observable_group: Group custom_workflow_observable_user: User custom_workflow_observable_attachment: Attachment + custom_workflow_observable_member: Membro custom_workflow_observable_project: Project custom_workflow_observable_project_attachments: Project Attachments / Files custom_workflow_observable_wiki_content: Wiki Content diff --git a/init.rb b/init.rb index 9617271..fb5b639 100644 --- a/init.rb +++ b/init.rb @@ -25,7 +25,7 @@ Redmine::Plugin.register :redmine_custom_workflows do author_url 'https://github.com/anteo/redmine_custom_workflows/graphs/contributors' author 'Anton Argirov/Karel Pičman' description 'It allows to create custom workflows for objects, defined in a plain Ruby language' - version '2.0.10 devel' + version '2.1.0 devel' requires_redmine version_or_higher: '4.1.0' diff --git a/lib/redmine_custom_workflows.rb b/lib/redmine_custom_workflows.rb index 7311169..7063058 100644 --- a/lib/redmine_custom_workflows.rb +++ b/lib/redmine_custom_workflows.rb @@ -35,6 +35,7 @@ require "#{File.dirname(__FILE__)}/redmine_custom_workflows/patches/models/issue require "#{File.dirname(__FILE__)}/redmine_custom_workflows/patches/models/project_patch" require "#{File.dirname(__FILE__)}/redmine_custom_workflows/patches/models/time_entry_patch" require "#{File.dirname(__FILE__)}/redmine_custom_workflows/patches/models/user_patch" +require "#{File.dirname(__FILE__)}/redmine_custom_workflows/patches/models/member_patch" require "#{File.dirname(__FILE__)}/redmine_custom_workflows/patches/models/version_patch" require "#{File.dirname(__FILE__)}/redmine_custom_workflows/patches/models/wiki_content_patch" require "#{File.dirname(__FILE__)}/redmine_custom_workflows/patches/models/wiki_page_patch" @@ -47,6 +48,7 @@ require "#{File.dirname(__FILE__)}/redmine_custom_workflows/patches/controllers/ require "#{File.dirname(__FILE__)}/redmine_custom_workflows/patches/controllers/projects_controller_patch" require "#{File.dirname(__FILE__)}/redmine_custom_workflows/patches/controllers/timelog_controller_patch" require "#{File.dirname(__FILE__)}/redmine_custom_workflows/patches/controllers/users_controller_patch" +require "#{File.dirname(__FILE__)}/redmine_custom_workflows/patches/controllers/members_controller_patch" require "#{File.dirname(__FILE__)}/redmine_custom_workflows/patches/controllers/versions_controller_patch" require "#{File.dirname(__FILE__)}/redmine_custom_workflows/patches/controllers/wiki_controller_patch" diff --git a/lib/redmine_custom_workflows/patches/controllers/members_controller_patch.rb b/lib/redmine_custom_workflows/patches/controllers/members_controller_patch.rb new file mode 100644 index 0000000..73af62b --- /dev/null +++ b/lib/redmine_custom_workflows/patches/controllers/members_controller_patch.rb @@ -0,0 +1,79 @@ +# frozen_string_literal: true + +# Redmine plugin for Custom Workflows +# +# Copyright © 2015-19 Anton Argirov +# Copyright © 2019-23 Karel Pičman +# +# 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. + +module RedmineCustomWorkflows + module Patches + module Controllers + # Members controller patch + module MembersControllerPatch + ################################################################################################################ + # New methods + # + + def self.prepended(base) + base.class_eval do + before_action :set_env + after_action :display_custom_workflow_messages + end + end + + def set_env + objects = model_objects + return unless objects&.any? + + objects.each do |o| + o.custom_workflow_env[:remote_ip] = request.remote_ip + end + end + + def display_custom_workflow_messages + objects = model_objects + return unless objects&.any? + + objects.each do |o| + next if o&.custom_workflow_messages&.empty? + + o.custom_workflow_messages.each do |key, value| + if value&.present? + flash[key] = value + else + flash.delete key + end + end + o.custom_workflow_messages.clear + end + end + + private + + def model_objects + if @members + @members + elsif @member + [@member] + end + end + end + end + end +end + +MembersController.prepend RedmineCustomWorkflows::Patches::Controllers::MembersControllerPatch diff --git a/lib/redmine_custom_workflows/patches/models/member_patch.rb b/lib/redmine_custom_workflows/patches/models/member_patch.rb index e50e66c..011430e 100644 --- a/lib/redmine_custom_workflows/patches/models/member_patch.rb +++ b/lib/redmine_custom_workflows/patches/models/member_patch.rb @@ -74,5 +74,5 @@ end if Redmine::Plugin.installed?('easy_extensions') RedmineExtensions::PatchManager.register_model_patch 'Member', 'RedmineCustomWorkflows::Patches::Models::MemberPatch' else - User.prepend RedmineCustomWorkflows::Patches::Models::MemberPatch + Member.prepend RedmineCustomWorkflows::Patches::Models::MemberPatch end diff --git a/test/fixtures/custom_workflows.yml b/test/fixtures/custom_workflows.yml index 8a70ff1..5ad7de7 100644 --- a/test/fixtures/custom_workflows.yml +++ b/test/fixtures/custom_workflows.yml @@ -220,4 +220,26 @@ custom_workflows_010: after_remove: NULL before_destroy: "self.custom_workflow_messages[:warning] = self.custom_workflow_env[:remote_ip]\n Rails.logger.info \">>> #{self.class.name} #{self.id }#{self.custom_workflow_env[:remote_ip]}\"" - after_destroy: '' \ No newline at end of file + after_destroy: '' + +custom_workflows_011: + id: 11 + before_save: '' + after_save: '' + name: 'Member CW test' + description: 'Testing workflow' + position: 11 + is_for_all: 0 + author: 'karel.picman@kontron.com' + created_at: 2023-10-06 09:29:27 +02:00 + updated_at: 2023-10-06 09:29:27 +02:00 + active: 1 + observable: 'member' + shared_code: NULL + before_add: NULL + after_add: NULL + before_remove: NULL + after_remove: NULL + before_destroy: "self.custom_workflow_messages[:notice] = 'Custom workflow'\n + self.custom_workflow_messages[:warning] = self.custom_workflow_env[:remote_ip]" + after_destroy: '' \ No newline at end of file diff --git a/test/fixtures/custom_workflows_projects.yml b/test/fixtures/custom_workflows_projects.yml index 4cd1ef3..aa132cd 100644 --- a/test/fixtures/custom_workflows_projects.yml +++ b/test/fixtures/custom_workflows_projects.yml @@ -22,3 +22,7 @@ custom_workflows_projects_005: custom_workflows_projects_006: project_id: 1 custom_workflow_id: 10 # Wiki page + +custom_workflows_projects_007: + project_id: 1 + custom_workflow_id: 11 # Member diff --git a/test/functional/members_controller_patch_test.rb b/test/functional/members_controller_patch_test.rb new file mode 100644 index 0000000..8100115 --- /dev/null +++ b/test/functional/members_controller_patch_test.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +# Redmine plugin for Document Management System "Features" +# +# Copyright © 2019-23 Karel Pičman +# +# 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 File.expand_path('../../test_helper', __FILE__) + +# Users controller patch test +class MembersControllerPatchTest < RedmineCustomWorkflows::Test::TestCase + fixtures :user_preferences, :roles, :members, :member_roles, :custom_workflows, :custom_workflows_projects + + def setup + super + @member1 = Member.find 1 + @request.session[:user_id] = @jsmith.id + @controller = MembersController.new + end + + def test_delete_with_cw + delete :destroy, params: { id: @member1 } + assert_response :redirect + assert_equal 'Custom workflow', @controller.flash[:notice] + end + + def test_cw_env + delete :destroy, params: { id: @member1 } + assert_response :redirect + assert_equal request.remote_ip, @controller.flash[:warning] + end +end diff --git a/test/unit/member_patch_test.rb b/test/unit/member_patch_test.rb new file mode 100644 index 0000000..61173af --- /dev/null +++ b/test/unit/member_patch_test.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +# Redmine plugin for Custom Workflows +# +# Copyright © 2015-19 Anton Argirov +# Copyright © 2019-23 Karel Pičman +# +# 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 File.expand_path('../../test_helper', __FILE__) + +# Member patch test class +class MemberPatchTest < RedmineCustomWorkflows::Test::UnitTest + fixtures :members + + def setup + @member1 = Member.find 1 + end + + def test_truth + assert_kind_of Member, @member1 + end + + def test_custom_workflow_messages + @member1.custom_workflow_messages[:notice] = 'Okay' + assert_equal 'Okay', @member1.custom_workflow_messages[:notice] + end + + def test_custom_workflow_env + @member1.custom_workflow_env[:remote_ip] = '127.0.0.1' + assert_equal '127.0.0.1', @member1.custom_workflow_env[:remote_ip] + end +end diff --git a/test/unit/user_patch_test.rb b/test/unit/user_patch_test.rb index 1f45767..afd48ef 100644 --- a/test/unit/user_patch_test.rb +++ b/test/unit/user_patch_test.rb @@ -21,7 +21,7 @@ require File.expand_path('../../test_helper', __FILE__) -# user patch test class +# User patch test class class UserPatchTest < RedmineCustomWorkflows::Test::UnitTest fixtures :users