@Test void dontUpdateParentWhenLegacyMode(MockitoComponentManager componentManager) throws Exception { DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("Path", "To"), "Page"); XWikiDocument document = mock(XWikiDocument.class); when(this.xcontext.getWiki().getDocument(documentReference, this.xcontext)).thenReturn(document); when(document.getParentReference()).thenReturn(new DocumentReference("wiki", "What", "Ever")); ParentChildConfiguration parentChildConfiguration = componentManager.getInstance(ParentChildConfiguration.class); when(parentChildConfiguration.isParentChildMechanismEnabled()).thenReturn(true); this.modelBridge.update(documentReference, Collections.emptyMap()); verify(document, never()).setParentReference(any(DocumentReference.class)); verify(this.xcontext.getWiki(), never()).saveDocument(any(XWikiDocument.class), anyString(), anyBoolean(), any(XWikiContext.class)); }
@Override public void update(DocumentReference documentReference, Map<String, String> parameters) { try { XWikiContext context = xcontextProvider.get(); XWiki wiki = context.getWiki(); XWikiDocument document = wiki.getDocument(documentReference, context); boolean save = false; String title = parameters.get("title"); if (title != null && !title.equals(document.getTitle())) { document.setTitle(title); save = true; } if (!parentChildConfiguration.isParentChildMechanismEnabled()) { DocumentReference hierarchicalParent = getHierarchicalParent(documentReference); String hierarchicalParentSerialized = this.compactEntityReferenceSerializer.serialize(hierarchicalParent, documentReference); EntityReference relativeHierarchicalReference = this.relativeStringEntityReferenceResolver .resolve(hierarchicalParentSerialized, EntityType.DOCUMENT); EntityReference newDocumentParentRef = document.getRelativeParentReference(); if (!relativeHierarchicalReference.equals(newDocumentParentRef)) { document.setParentReference(relativeHierarchicalReference); save = true; } } if (save) { wiki.saveDocument(document, "Update document after refactoring.", true, context); this.logger.info("Document [{}] has been updated.", documentReference); } } catch (Exception e) { this.logger.error("Failed to update the document [{}] after refactoring.", documentReference, e); } }
DefaultModelBridge implements ModelBridge { @Override public void update(DocumentReference documentReference, Map<String, String> parameters) { try { XWikiContext context = xcontextProvider.get(); XWiki wiki = context.getWiki(); XWikiDocument document = wiki.getDocument(documentReference, context); boolean save = false; String title = parameters.get("title"); if (title != null && !title.equals(document.getTitle())) { document.setTitle(title); save = true; } if (!parentChildConfiguration.isParentChildMechanismEnabled()) { DocumentReference hierarchicalParent = getHierarchicalParent(documentReference); String hierarchicalParentSerialized = this.compactEntityReferenceSerializer.serialize(hierarchicalParent, documentReference); EntityReference relativeHierarchicalReference = this.relativeStringEntityReferenceResolver .resolve(hierarchicalParentSerialized, EntityType.DOCUMENT); EntityReference newDocumentParentRef = document.getRelativeParentReference(); if (!relativeHierarchicalReference.equals(newDocumentParentRef)) { document.setParentReference(relativeHierarchicalReference); save = true; } } if (save) { wiki.saveDocument(document, "Update document after refactoring.", true, context); this.logger.info("Document [{}] has been updated.", documentReference); } } catch (Exception e) { this.logger.error("Failed to update the document [{}] after refactoring.", documentReference, e); } } }
DefaultModelBridge implements ModelBridge { @Override public void update(DocumentReference documentReference, Map<String, String> parameters) { try { XWikiContext context = xcontextProvider.get(); XWiki wiki = context.getWiki(); XWikiDocument document = wiki.getDocument(documentReference, context); boolean save = false; String title = parameters.get("title"); if (title != null && !title.equals(document.getTitle())) { document.setTitle(title); save = true; } if (!parentChildConfiguration.isParentChildMechanismEnabled()) { DocumentReference hierarchicalParent = getHierarchicalParent(documentReference); String hierarchicalParentSerialized = this.compactEntityReferenceSerializer.serialize(hierarchicalParent, documentReference); EntityReference relativeHierarchicalReference = this.relativeStringEntityReferenceResolver .resolve(hierarchicalParentSerialized, EntityType.DOCUMENT); EntityReference newDocumentParentRef = document.getRelativeParentReference(); if (!relativeHierarchicalReference.equals(newDocumentParentRef)) { document.setParentReference(relativeHierarchicalReference); save = true; } } if (save) { wiki.saveDocument(document, "Update document after refactoring.", true, context); this.logger.info("Document [{}] has been updated.", documentReference); } } catch (Exception e) { this.logger.error("Failed to update the document [{}] after refactoring.", documentReference, e); } }  }
DefaultModelBridge implements ModelBridge { @Override public void update(DocumentReference documentReference, Map<String, String> parameters) { try { XWikiContext context = xcontextProvider.get(); XWiki wiki = context.getWiki(); XWikiDocument document = wiki.getDocument(documentReference, context); boolean save = false; String title = parameters.get("title"); if (title != null && !title.equals(document.getTitle())) { document.setTitle(title); save = true; } if (!parentChildConfiguration.isParentChildMechanismEnabled()) { DocumentReference hierarchicalParent = getHierarchicalParent(documentReference); String hierarchicalParentSerialized = this.compactEntityReferenceSerializer.serialize(hierarchicalParent, documentReference); EntityReference relativeHierarchicalReference = this.relativeStringEntityReferenceResolver .resolve(hierarchicalParentSerialized, EntityType.DOCUMENT); EntityReference newDocumentParentRef = document.getRelativeParentReference(); if (!relativeHierarchicalReference.equals(newDocumentParentRef)) { document.setParentReference(relativeHierarchicalReference); save = true; } } if (save) { wiki.saveDocument(document, "Update document after refactoring.", true, context); this.logger.info("Document [{}] has been updated.", documentReference); } } catch (Exception e) { this.logger.error("Failed to update the document [{}] after refactoring.", documentReference, e); } }  @Override boolean create(DocumentReference documentReference); @Override boolean copy(DocumentReference source, DocumentReference destination); @Override boolean delete(DocumentReference reference); @Override boolean delete(DocumentReference reference, boolean skipRecycleBin); @Override boolean removeLock(DocumentReference reference); @Override void createRedirect(DocumentReference oldReference, DocumentReference newReference); @Override boolean canOverwriteSilently(DocumentReference documentReference); @Override boolean exists(DocumentReference reference); @Override List<DocumentReference> getBackLinkedReferences(DocumentReference documentReference, String wikiId); @Override List<DocumentReference> getDocumentReferences(SpaceReference spaceReference); @Override boolean updateParentField(final DocumentReference oldParentReference,
        final DocumentReference newParentReference); @Override DocumentReference setContextUserReference(DocumentReference userReference); @Override void update(DocumentReference documentReference, Map<String, String> parameters); @Override boolean restoreDeletedDocument(long deletedDocumentId, AbstractCheckRightsRequest request); @Override List<Long> getDeletedDocumentIds(String batchId); @Override boolean permanentlyDeleteDocument(long deletedDocumentId, AbstractCheckRightsRequest request); @Override boolean permanentlyDeleteAllDocuments(PermanentlyDeleteJob deleteJob, AbstractCheckRightsRequest request); @Override boolean rename(DocumentReference source, DocumentReference destination); }
DefaultModelBridge implements ModelBridge { @Override public void update(DocumentReference documentReference, Map<String, String> parameters) { try { XWikiContext context = xcontextProvider.get(); XWiki wiki = context.getWiki(); XWikiDocument document = wiki.getDocument(documentReference, context); boolean save = false; String title = parameters.get("title"); if (title != null && !title.equals(document.getTitle())) { document.setTitle(title); save = true; } if (!parentChildConfiguration.isParentChildMechanismEnabled()) { DocumentReference hierarchicalParent = getHierarchicalParent(documentReference); String hierarchicalParentSerialized = this.compactEntityReferenceSerializer.serialize(hierarchicalParent, documentReference); EntityReference relativeHierarchicalReference = this.relativeStringEntityReferenceResolver .resolve(hierarchicalParentSerialized, EntityType.DOCUMENT); EntityReference newDocumentParentRef = document.getRelativeParentReference(); if (!relativeHierarchicalReference.equals(newDocumentParentRef)) { document.setParentReference(relativeHierarchicalReference); save = true; } } if (save) { wiki.saveDocument(document, "Update document after refactoring.", true, context); this.logger.info("Document [{}] has been updated.", documentReference); } } catch (Exception e) { this.logger.error("Failed to update the document [{}] after refactoring.", documentReference, e); } }  @Override boolean create(DocumentReference documentReference); @Override boolean copy(DocumentReference source, DocumentReference destination); @Override boolean delete(DocumentReference reference); @Override boolean delete(DocumentReference reference, boolean skipRecycleBin); @Override boolean removeLock(DocumentReference reference); @Override void createRedirect(DocumentReference oldReference, DocumentReference newReference); @Override boolean canOverwriteSilently(DocumentReference documentReference); @Override boolean exists(DocumentReference reference); @Override List<DocumentReference> getBackLinkedReferences(DocumentReference documentReference, String wikiId); @Override List<DocumentReference> getDocumentReferences(SpaceReference spaceReference); @Override boolean updateParentField(final DocumentReference oldParentReference,
        final DocumentReference newParentReference); @Override DocumentReference setContextUserReference(DocumentReference userReference); @Override void update(DocumentReference documentReference, Map<String, String> parameters); @Override boolean restoreDeletedDocument(long deletedDocumentId, AbstractCheckRightsRequest request); @Override List<Long> getDeletedDocumentIds(String batchId); @Override boolean permanentlyDeleteDocument(long deletedDocumentId, AbstractCheckRightsRequest request); @Override boolean permanentlyDeleteAllDocuments(PermanentlyDeleteJob deleteJob, AbstractCheckRightsRequest request); @Override boolean rename(DocumentReference source, DocumentReference destination);  }