From ecf232c08ad9694e07aeba114878590a25d48dc6 Mon Sep 17 00:00:00 2001 From: Philipp Date: Thu, 4 May 2023 16:52:45 +0200 Subject: [PATCH] fix(copy-paste): do not copy message flows without participant Closes #1902 --- lib/features/copy-paste/BpmnCopyPaste.js | 2 ++ lib/features/rules/BpmnRules.js | 22 +++++++--------- .../features/copy-paste/BpmnCopyPasteSpec.js | 25 ++++++++++++++++++ test/spec/features/rules/BpmnRulesSpec.js | 26 +++++++++++++++++++ 4 files changed, 63 insertions(+), 12 deletions(-) diff --git a/lib/features/copy-paste/BpmnCopyPaste.js b/lib/features/copy-paste/BpmnCopyPaste.js index e8789e28f9..f5aee3700b 100644 --- a/lib/features/copy-paste/BpmnCopyPaste.js +++ b/lib/features/copy-paste/BpmnCopyPaste.js @@ -14,6 +14,8 @@ import { import { isLabel } from '../../util/LabelUtil'; +import { getParent } from '../modeling/util/ModelingUtil'; + /** * @typedef {import('../modeling/BpmnFactory').default} BpmnFactory * @typedef {import('diagram-js/lib/core/EventBus').default} EventBus diff --git a/lib/features/rules/BpmnRules.js b/lib/features/rules/BpmnRules.js index 8e958c0c37..c2df36eb98 100644 --- a/lib/features/rules/BpmnRules.js +++ b/lib/features/rules/BpmnRules.js @@ -1143,16 +1143,6 @@ function canInsert(shape, connection, position) { canDrop(shape, connection.parent, position)); } -/** - * @param {Element[]} elements - * @param {Element} element - * - * @return {boolean} - */ -function includes(elements, element) { - return (elements && element) && elements.indexOf(element) !== -1; -} - /** * @param {Element[]} elements * @param {Element} element @@ -1164,10 +1154,18 @@ function canCopy(elements, element) { return true; } - if (is(element, 'bpmn:Lane') && !includes(elements, element.parent)) { + if (is(element, 'bpmn:Lane') && !elements.includes(element.parent)) { return false; } + if (is(element, 'bpmn:MessageFlow')) { + const source = element.source, + target = element.target; + + return elements.includes(is(source, 'bpmn:Participant') ? source : getParent(source, 'bpmn:Participant')) + && elements.includes(is(target, 'bpmn:Participant') ? target : getParent(target, 'bpmn:Participant')); + } + return true; } @@ -1178,4 +1176,4 @@ function canCopy(elements, element) { */ function getRootElement(element) { return getParent(element, 'bpmn:Process') || getParent(element, 'bpmn:Collaboration'); -} +} \ No newline at end of file diff --git a/test/spec/features/copy-paste/BpmnCopyPasteSpec.js b/test/spec/features/copy-paste/BpmnCopyPasteSpec.js index 165ce05278..daef3198eb 100644 --- a/test/spec/features/copy-paste/BpmnCopyPasteSpec.js +++ b/test/spec/features/copy-paste/BpmnCopyPasteSpec.js @@ -964,6 +964,31 @@ describe('features/copy-paste', function() { }); + + describe('rules', function() { + + beforeEach(bootstrapModeler(collaborationMultipleXML, { + modules: testModules, + moddleExtensions: { + camunda: camundaPackage + } + })); + + + it.only('should allow copying message flow with parent participants of source and target', inject(function(elementRegistry) { + + // when + var tree = copy([ 'IntermediateThrowEvent_1', 'Task_2', 'MessageFlow_1' ]); + + console.log(tree, keys(tree)); + + // then + expect(keys(tree)).to.have.length(1); + expect(getAllElementsInTree(tree, 0)).to.have.length(2); + })); + + }); + }); diff --git a/test/spec/features/rules/BpmnRulesSpec.js b/test/spec/features/rules/BpmnRulesSpec.js index 5290986132..e320f92334 100644 --- a/test/spec/features/rules/BpmnRulesSpec.js +++ b/test/spec/features/rules/BpmnRulesSpec.js @@ -220,6 +220,32 @@ describe('features/modeling/rules - BpmnRules', function() { expectCanCopy(boundaryEvent, [ boundaryEvent ], true); })); + + it('copy message flow with parent participants of source and target', inject(function(elementFactory) { + + // given + var sourceParticipant = elementFactory.createShape({ type: 'bpmn:Participant' }), + targetParticipant = elementFactory.createShape({ type: 'bpmn:Participant' }), + source = elementFactory.createShape({ type: 'bpmn:Task', parent: sourceParticipant }), + target = elementFactory.createShape({ type: 'bpmn:Task', parent: targetParticipant }), + messageFlow = elementFactory.createConnection({ type: 'bpmn:MessageFlow', source: source, target: target, waypoints: [] }); + + // then + expectCanCopy(messageFlow, [ sourceParticipant, targetParticipant, source, target, messageFlow ], true); + })); + + + it('copy message flow without parent participants of source and target', inject(function(elementFactory) { + + // given + var source = elementFactory.createShape({ type: 'bpmn:Task' }), + target = elementFactory.createShape({ type: 'bpmn:Task' }), + messageFlow = elementFactory.createConnection({ type: 'bpmn:MessageFlow', source: source, target: target, waypoints: [] }); + + // then + expectCanCopy(messageFlow, [ source, target, messageFlow ], false); + })); + });