From ce91eb474b1d95a05d60946851f20110f9386d64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Pi=C4=8Dman?= Date: Wed, 6 Jun 2018 14:19:36 +0200 Subject: [PATCH] An error when moving issue with attached documents to a different project --- .../controllers/issues_controller_hooks.rb | 57 +++++++++----- lib/redmine_dmsf/patches/issue_patch.rb | 8 +- test/fixtures/dmsf_files.yml | 9 +++ test/fixtures/dmsf_folders.yml | 8 ++ test/functional/issues_controller_test.rb | 74 +++++++++++++++++++ test/unit/dmsf_folder_test.rb | 2 +- test/unit/project_patch_test.rb | 4 +- 7 files changed, 138 insertions(+), 24 deletions(-) create mode 100644 test/functional/issues_controller_test.rb diff --git a/lib/redmine_dmsf/hooks/controllers/issues_controller_hooks.rb b/lib/redmine_dmsf/hooks/controllers/issues_controller_hooks.rb index 62a4daf1..feeb060c 100644 --- a/lib/redmine_dmsf/hooks/controllers/issues_controller_hooks.rb +++ b/lib/redmine_dmsf/hooks/controllers/issues_controller_hooks.rb @@ -72,30 +72,47 @@ module RedmineDmsf if context.is_a?(Hash) issue = context[:issue] params = context[:params] - # Move existing attached documents if needed + controller = context[:controller] if edit && params[:issue] project_id = params[:issue][:project_id].to_i old_project_id = context[:project].id - system_folder = issue.system_folder(false, old_project_id) - if system_folder - # Change the title if the issue's subject changed - system_folder.title = "#{issue.id} - #{DmsfFolder::get_valid_title(issue.subject)}" - # Move system folders if needed - if system_folder.dmsf_folder - system_folder.dmsf_folder.project_id = project_id - system_folder.dmsf_folder.save! + # Sync the title with the issue's subject + old_system_folder = issue.system_folder(false, old_project_id) + if old_system_folder + old_system_folder.title = "#{issue.id} - #{DmsfFolder::get_valid_title(issue.subject)}" + unless old_system_folder.save + controller.flash[:error] = old_system_folder.errors.full_messages.to_sentence + Rails.logger.error old_system_folder.errors.full_messages.to_sentence end - system_folder.project_id = project_id - system_folder.save! - # Move documents - issue.dmsf_files.each do |dmsf_file| - dmsf_file.project_id = project_id - dmsf_file.save! - end - # Move links - issue.dmsf_links.each do | dmsf_link| - dmsf_link.project_id = project_id - dmsf_link.save! + else + Rails.error "No system folder found for issue ##{issue.id}." + end + # Move documents, links and folders if needed + if project_id != old_project_id + if old_system_folder + new_main_system_folder = issue.main_system_folder(true) + if new_main_system_folder + old_system_folder.dmsf_folder_id = new_main_system_folder.id + old_system_folder.project_id = project_id + unless old_system_folder.save + controller.flash[:error] = old_system_folder.errors.full_messages.to_sentence + Rails.logger.error old_system_folder.errors.full_messages.to_sentence + end + issue.dmsf_files.each do |dmsf_file| + dmsf_file.project_id = project_id + unless dmsf_file.save + controller.flash[:error] = dmsf_file.errors.full_messages.to_sentence + Rails.logger.error dmsf_file.errors.full_messages.to_sentence + end + end + issue.dmsf_links.each do | dmsf_link| + dmsf_link.project_id = project_id + unless dmsf_link.save + controller.flash[:error] = dmsf_link.errors.full_messages.to_sentence + Rails.logger.error dmsf_link.errors.full_messages.to_sentence + end + end + end end end end diff --git a/lib/redmine_dmsf/patches/issue_patch.rb b/lib/redmine_dmsf/patches/issue_patch.rb index 80cc1568..00dd0b2e 100644 --- a/lib/redmine_dmsf/patches/issue_patch.rb +++ b/lib/redmine_dmsf/patches/issue_patch.rb @@ -93,7 +93,7 @@ module RedmineDmsf @saved_dmsf_links_wfs || {} end - def system_folder(create = false, prj_id = nil) + def main_system_folder(create = false, prj_id = nil) prj_id ||= self.project_id parent = DmsfFolder.system.where(:project_id => prj_id, :title => '.Issues').first if create && !parent @@ -105,6 +105,12 @@ module RedmineDmsf parent.system = true parent.save end + parent + end + + def system_folder(create = false, prj_id = nil) + prj_id ||= self.project_id + parent = main_system_folder(create, prj_id) if parent folder = DmsfFolder.system.where(["project_id = ? AND dmsf_folder_id = ? AND title LIKE '? - %'", prj_id, parent.id, self.id]).first diff --git a/test/fixtures/dmsf_files.yml b/test/fixtures/dmsf_files.yml index fe99f71d..d72da3ad 100644 --- a/test/fixtures/dmsf_files.yml +++ b/test/fixtures/dmsf_files.yml @@ -90,3 +90,12 @@ dmsf_files_010: notification: false deleted: 0 deleted_by_user_id: NULL + +dmsf_files_011: + id: 11 + project_id: 1 + dmsf_folder_id: 9 + name: 'zero.txt' + notification: false + deleted: 0 + deleted_by_user_id: NULL diff --git a/test/fixtures/dmsf_folders.yml b/test/fixtures/dmsf_folders.yml index 09576cc9..27d86c43 100644 --- a/test/fixtures/dmsf_folders.yml +++ b/test/fixtures/dmsf_folders.yml @@ -54,4 +54,12 @@ dmsf_folders_008: project_id: 1 dmsf_folder_id: NULL user_id: 1 + system: true + +dmsf_folders_009: + id: 9 + title: 1 - Cannot print recipes + project_id: 1 + dmsf_folder_id: 8 + user_id: 1 system: true \ No newline at end of file diff --git a/test/functional/issues_controller_test.rb b/test/functional/issues_controller_test.rb new file mode 100644 index 00000000..97d71a6a --- /dev/null +++ b/test/functional/issues_controller_test.rb @@ -0,0 +1,74 @@ +# encoding: utf-8 +# +# Redmine plugin for Document Management System "Features" +# +# Copyright © 2011-18 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__) + +class IssuesControllerTest < RedmineDmsf::Test::TestCase + + fixtures :users, :email_addresses, :user_preferences, :projects, :dmsf_file_revisions, :dmsf_folders, + :dmsf_files, :projects, :issues, :versions, :trackers, :projects_trackers, :roles, :members, :member_roles, + :enabled_modules, :enumerations + + def setup + @user_manager = User.find_by_id 2 + assert_not_nil @user_manager + @project1 = Project.find_by_id 1 + assert_not_nil @project1 + @project1.enable_module! :dmsf + @project1.enable_module! :issue_tracking + @project2 = Project.find_by_id 2 + assert_not_nil @project2 + @project2.enable_module! :dmsf + @project2.enable_module! :issue_tracking + @issue1 = Issue.find_by_id 1 + Setting.plugin_redmine_dmsf['dmsf_storage_directory'] = File.expand_path '../../fixtures/files', __FILE__ + User.current = nil + @request.session[:user_id] = @user_manager.id + end + + def test_truth + assert_kind_of Project, @project1 + assert_kind_of Project, @project2 + assert_kind_of Issue, @issue1 + end + + def test_put_update_with_project_change + # If we move an issue to a different project, attached documents and their system folders must be moved too + assert_equal @issue1.project, @project1 + system_folder = @issue1.system_folder + assert system_folder + assert_equal @project1.id, system_folder.project_id + main_system_folder = @issue1.main_system_folder + assert main_system_folder + assert_equal @project1.id, main_system_folder.project_id + put :update, :id => @issue1.id, :issue => { :project_id => @project2.id, :tracker_id => '1', :priority_id => '6', + :category_id => '3' } + assert_redirected_to :action => 'show', :id => @issue1.id + @issue1.reload + assert_equal @project2.id, @issue1.project.id + system_folder = @issue1.system_folder + assert system_folder + assert_equal @project2.id, system_folder.project_id + main_system_folder = @issue1.main_system_folder + assert main_system_folder + assert_equal @project2.id, main_system_folder.project_id + end + +end \ No newline at end of file diff --git a/test/unit/dmsf_folder_test.rb b/test/unit/dmsf_folder_test.rb index d784858d..320ed4f1 100644 --- a/test/unit/dmsf_folder_test.rb +++ b/test/unit/dmsf_folder_test.rb @@ -58,7 +58,7 @@ class DmsfFolderTest < RedmineDmsf::Test::UnitTest def test_visiblity # The role has got permissions User.current = @manager - assert_equal 6, DmsfFolder.where(:project_id => 1).count + assert_equal 7, DmsfFolder.where(:project_id => 1).count assert_equal 5, DmsfFolder.visible.where(:project_id => 1).count # The user has got permissions User.current = @developer diff --git a/test/unit/project_patch_test.rb b/test/unit/project_patch_test.rb index c02c295e..d6306663 100644 --- a/test/unit/project_patch_test.rb +++ b/test/unit/project_patch_test.rb @@ -67,8 +67,8 @@ class ProjectPatchTest < RedmineDmsf::Test::UnitTest def test_dmsf_count hash = @project1.dmsf_count - assert_equal 9, hash[:files] - assert_equal 7, hash[:folders] + assert_equal 10, hash[:files] + assert_equal 8, hash[:folders] end def test_copy_approval_workflows