CVE-2022-23617: Missing Authorization (CWE-862)
value: XWiki Platform is a generic wiki platform offering runtime services for applications built on top of it. In affected versions any user with edit right can copy the content of a page it does not have access to by using it as template of a new page. This issue has been patched in XWiki 13.2CR1 and 12.10.6. Users are advised to update. There are no known workarounds for this issue.
CWE description: The product does not perform an authorization check when an actor attempts to access a resource or perform an action.
CVSS v3 base score: 6.5
Published: 2022-02-09T21:15Z
Repository: https://github.com/xwiki/xwiki-platform
Language: Java
Files changed: xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/AdminAction.java, xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/CreateAction.java, xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/EditAction.java, xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/InlineAction.java, xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/SaveAction.java, xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/XWikiAction.java, xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/web/CreateActionTest.java, xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/web/SaveActionTest.java
Commit date: 2021-03-12 17:05:01 +0100
Commit message: XWIKI-18430: Page content is revealed to users that don't have rights if used as a template for the creation of another page

Diff: diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/AdminAction.java b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/AdminAction.java
index 858eccffe7f..5336884aa88 100644
--- a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/AdminAction.java
+++ b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/AdminAction.java
@@ -127,11 +127,11 @@ public String render(XWikiContext context) throws XWikiException
             if (content != null && !content.isEmpty()) {
                 tdoc2.setContent(content);
             }
             context.put("tdoc", tdoc2);
             try {
-                tdoc2.readFromTemplate(peform, context);
+                readFromTemplate(tdoc2, peform.getTemplate(), context);
             } catch (XWikiException e) {
                 if (e.getCode() == XWikiException.ERROR_XWIKI_APP_DOCUMENT_NOT_EMPTY) {
                     context.put("exception", e);
                     return "docalreadyexists";
                 }
diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/CreateAction.java b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/CreateAction.java
index 5833e943844..38f65bfba0f 100644
--- a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/CreateAction.java
+++ b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/CreateAction.java
@@ -297,26 +297,24 @@ private DocumentReferenceResolver<String> getCurrentMixedDocumentReferenceResolv
      */
     private void initAndSaveDocument(XWikiContext context, XWikiDocument newDocument, String title, String template,
         String parent) throws XWikiException
     {
         XWiki xwiki = context.getWiki();
-        DocumentReferenceResolver<String> resolver = getCurrentMixedDocumentReferenceResolver();
 
         // Set the locale and default locale, considering that we're creating the original version of the document
         // (not a translation).
         newDocument.setLocale(Locale.ROOT);
         if (newDocument.getDefaultLocale() == Locale.ROOT) {
             newDocument.setDefaultLocale(xwiki.getLocalePreference(context));
         }
 
         // Copy the template.
-        DocumentReference templateReference = resolver.resolve(template);
-        newDocument.readFromTemplate(templateReference, context);
+        readFromTemplate(newDocument, template, context);
 
         // Set the parent field.
         if (!StringUtils.isEmpty(parent)) {
-            DocumentReference parentReference = resolver.resolve(parent);
+            DocumentReference parentReference = this.currentmixedReferenceResolver.resolve(parent);
             newDocument.setParentReference(parentReference);
         }
 
         // Set the document title
         if (title != null) {
diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/EditAction.java b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/EditAction.java
index 35cdaa5507e..229435e5e9c 100644
--- a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/EditAction.java
+++ b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/EditAction.java
@@ -100,11 +100,11 @@ protected XWikiDocument prepareEditedDocument(XWikiContext context) throws XWiki
         // Determine the edited document (translation).
         XWikiDocument editedDocument = getEditedDocument(context);
         EditForm editForm = (EditForm) context.getForm();
 
         // Update the edited document based on the template specified on the request.
-        editedDocument.readFromTemplate(editForm, context);
+        readFromTemplate(editedDocument, editForm.getTemplate(), context);
 
         // The default values from the template can be overwritten by additional request parameters.
         updateDocumentTitleAndContentFromRequest(editedDocument, context);
         editedDocument.readObjectsFromForm(editForm, context);
 
diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/InlineAction.java b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/InlineAction.java
index 2d6fa6fb662..0a526c870d4 100644
--- a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/InlineAction.java
+++ b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/InlineAction.java
@@ -80,22 +80,22 @@ public String render(XWikiContext context) throws XWikiException
             }
             if (doc2.getDefaultLanguage().equals("")) {
                 doc2.setDefaultLanguage(context.getWiki().getLanguagePreference(context));
             }
             try {
-                doc2.readFromTemplate(peform, context);
+                readFromTemplate(doc2, peform.getTemplate(), context);
             } catch (XWikiException e) {
                 if (e.getCode() == XWikiException.ERROR_XWIKI_APP_DOCUMENT_NOT_EMPTY) {
                     return "docalreadyexists";
                 }
             }
 
             if (doc == cdoc) {
                 context.put("cdoc", doc2);
             } else {
                 XWikiDocument cdoc2 = cdoc.clone();
-                cdoc2.readFromTemplate(peform, context);
+                readFromTemplate(cdoc2, peform.getTemplate(), context);
                 context.put("cdoc", cdoc2);
             }
 
             doc2.readFromForm((EditForm) form, context);
 
diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/SaveAction.java b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/SaveAction.java
index e5b3719a9af..1a9d7eb0c34 100644
--- a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/SaveAction.java
+++ b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/SaveAction.java
@@ -191,11 +191,11 @@ public boolean save(XWikiContext context) throws XWikiException
                 doc.setDefaultLocale(xwiki.getLocalePreference(context));
             }
         }
 
         try {
-            tdoc.readFromTemplate(form.getTemplate(), context);
+            readFromTemplate(tdoc, form.getTemplate(), context);
         } catch (XWikiException e) {
             if (e.getCode() == XWikiException.ERROR_XWIKI_APP_DOCUMENT_NOT_EMPTY) {
                 context.put("exception", e);
                 return true;
             }
@@ -567,11 +567,13 @@ private boolean isAsync(XWikiRequest request)
         return "true".equals(request.get(ASYNC_PARAM));
     }
 
     private Job startCreateJob(EntityReference entityReference, EditForm editForm) throws XWikiException
     {
-        if (StringUtils.isBlank(editForm.getTemplate())) {
+        DocumentReference templateReference = resolveTemplate(editForm.getTemplate());
+
+        if (templateReference == null) {
             // No template specified, nothing more to do.
             return null;
         }
 
         // If a template is set in the request, then this is a create action which needs to be handled by a create job,
@@ -583,13 +585,10 @@ private Job startCreateJob(EntityReference entityReference, EditForm editForm) t
         CreateRequest request = refactoring.getRequestFactory().createCreateRequest(Arrays.asList(entityReference));
         request.setCheckAuthorRights(false);
         // Set the target document.
         request.setEntityReferences(Arrays.asList(entityReference));
         // Set the template to use.
-        DocumentReferenceResolver<String> resolver =
-            Utils.getComponent(DocumentReferenceResolver.TYPE_STRING, "currentmixed");
-        EntityReference templateReference = resolver.resolve(editForm.getTemplate());
         request.setTemplateReference(templateReference);
         // We`ve already created and populated the fields of the target document, focus only on the remaining children
         // specified in the template.
         request.setSkippedEntities(Arrays.asList(entityReference));
 
diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/XWikiAction.java b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/XWikiAction.java
index c521c4b7030..538f6d58059 100644
--- a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/XWikiAction.java
+++ b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/web/XWikiAction.java
@@ -27,10 +27,11 @@
 import java.util.Locale;
 import java.util.Map;
 import java.util.Vector;
 
 import javax.inject.Inject;
+import javax.inject.Named;
 import javax.script.ScriptContext;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
@@ -78,10 +79,12 @@
 import org.xwiki.resource.ResourceReferenceManager;
 import org.xwiki.resource.ResourceType;
 import org.xwiki.resource.entity.EntityResourceReference;
 import org.xwiki.resource.internal.DefaultResourceReferenceHandlerChain;
 import org.xwiki.script.ScriptContextManager;
+import org.xwiki.security.authorization.ContextualAuthorizationManager;
+import org.xwiki.security.authorization.Right;
 import org.xwiki.stability.Unstable;
 import org.xwiki.template.TemplateManager;
 import org.xwiki.velocity.VelocityManager;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -148,10 +151,17 @@ public abstract class XWikiAction implements LegacyAction
     protected Container container;
 
     @Inject
     protected Execution execution;
 
+    @Inject
+    protected ContextualAuthorizationManager autorization;
+
+    @Inject
+    @Named("currentmixed")
+    protected DocumentReferenceResolver<String> currentmixedReferenceResolver;
+
     /**
      * Indicate if the action allow asynchronous display (among which the XWiki initialization).
      */
     protected boolean waitForXWikiInitialization = true;
 
@@ -1134,6 +1144,55 @@ protected void answerJSON(XWikiContext context, int status, Map<String, String>
     protected void setContentLength(XWikiResponse response, long length)
     {
         // Set the content length in the response
         response.setContentLengthLong(length);
     }
+
+    /**
+     * Helper used resolve the template passed to the action if the current user have access to it.
+     * 
+     * @param template the template to copy
+     * @return the reference of the template if not empty and the current user have access to it
+     * @since 12.10.6
+     * @since 13.2RC1
+     */
+    protected DocumentReference resolveTemplate(String template)
+    {
+        if (StringUtils.isNotBlank(template)) {
+            DocumentReference templateReference = this.currentmixedReferenceResolver.resolve(template);
+
+            // Make sure the current user have access to the template document before copying it
+            if (this.autorization.hasAccess(Right.VIEW, templateReference)) {
+                return templateReference;
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * Helper used by various actions to initialize a document by copying a template to it.
+     * 
+     * @param document the document to update
+     * @param template the template to copy
+     * @param context the XWiki context
+     * @return true if the document was updated, false otherwise (for example when the current user does not have view
+     *         right on the template document)
+     * @throws XWikiException when failing to copy the template
+     * @since 12.10.6
+     * @since 13.2RC1
+     */
+    @Unstable
+    protected boolean readFromTemplate(XWikiDocument document, String template, XWikiContext context)
+        throws XWikiException
+    {
+        DocumentReference templateReference = resolveTemplate(template);
+
+        if (templateReference != null) {
+            document.readFromTemplate(templateReference, context);
+
+            return true;
+        }
+
+        return false;
+    }
 }
diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/web/CreateActionTest.java b/xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/web/CreateActionTest.java
index 30857c67759..ca7d774cc28 100644
--- a/xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/web/CreateActionTest.java
+++ b/xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/web/CreateActionTest.java
@@ -26,33 +26,35 @@
 import java.util.List;
 import java.util.Locale;
 
 import javax.inject.Provider;
 
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
 import org.xwiki.model.reference.DocumentReference;
 import org.xwiki.model.reference.EntityReference;
 import org.xwiki.model.reference.SpaceReference;
 import org.xwiki.model.reference.WikiReference;
 import org.xwiki.observation.ObservationManager;
 import org.xwiki.query.Query;
 import org.xwiki.query.QueryManager;
 import org.xwiki.security.authorization.Right;
 import org.xwiki.test.annotation.ComponentList;
+import org.xwiki.test.junit5.mockito.InjectMockComponents;
 
 import com.xpn.xwiki.XWikiContext;
 import com.xpn.xwiki.XWikiException;
 import com.xpn.xwiki.doc.XWikiDocument;
 import com.xpn.xwiki.objects.BaseObject;
-import com.xpn.xwiki.test.MockitoOldcoreRule;
+import com.xpn.xwiki.test.MockitoOldcore;
+import com.xpn.xwiki.test.junit5.mockito.InjectMockitoOldcore;
+import com.xpn.xwiki.test.junit5.mockito.OldcoreTest;
 import com.xpn.xwiki.test.reference.ReferenceComponentList;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
@@ -63,67 +65,67 @@
  * @version $Id$
  * @since 7.2M1
  */
 @ComponentList
 @ReferenceComponentList
-public class CreateActionTest
+@OldcoreTest
+class CreateActionTest
 {
-    @Rule
-    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule();
+    @InjectMockitoOldcore
+    MockitoOldcore oldcore;
 
     XWikiURLFactory mockURLFactory;
 
+    @InjectMockComponents
     CreateAction action;
 
     XWikiContext context;
 
     XWikiRequest mockRequest;
 
     XWikiResponse mockResponse;
 
     Query mockTemplateProvidersQuery;
 
-    @Before
-    public void setUp() throws Exception
+    @BeforeEach
+    public void beforeEach() throws Exception
     {
-        context = oldcore.getXWikiContext();
+        this.context = this.oldcore.getXWikiContext();
 
-        Utils.setComponentManager(oldcore.getMocker());
+        Utils.setComponentManager(this.oldcore.getMocker());
 
         QueryManager mockSecureQueryManager =
-            oldcore.getMocker().registerMockComponent((Type) QueryManager.class, "secure");
+            this.oldcore.getMocker().registerMockComponent((Type) QueryManager.class, "secure");
 
-        mockTemplateProvidersQuery = mock(Query.class);
-        when(mockSecureQueryManager.createQuery(any(), any())).thenReturn(mockTemplateProvidersQuery);
-        when(mockTemplateProvidersQuery.execute()).thenReturn(Collections.emptyList());
+        this.mockTemplateProvidersQuery = mock(Query.class);
+        when(mockSecureQueryManager.createQuery(any(), any())).thenReturn(this.mockTemplateProvidersQuery);
+        when(this.mockTemplateProvidersQuery.execute()).thenReturn(Collections.emptyList());
 
-        when(oldcore.getMockContextualAuthorizationManager().hasAccess(any(Right.class), any(EntityReference.class)))
-            .thenReturn(true);
+        when(this.oldcore.getMockContextualAuthorizationManager().hasAccess(any(Right.class),
+            any(EntityReference.class))).thenReturn(true);
 
         Provider<DocumentReference> mockDocumentReferenceProvider =
-            oldcore.getMocker().registerMockComponent(DocumentReference.TYPE_PROVIDER);
+            this.oldcore.getMocker().registerMockComponent(DocumentReference.TYPE_PROVIDER);
         when(mockDocumentReferenceProvider.get())
             .thenReturn(new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome"));
 
-        mockURLFactory = mock(XWikiURLFactory.class);
-        context.setURLFactory(mockURLFactory);
+        this.mockURLFactory = mock(XWikiURLFactory.class);
+        this.context.setURLFactory(this.mockURLFactory);
 
-        action = new CreateAction();
+        this.mockRequest = mock(XWikiRequest.class);
+        this.context.setRequest(this.mockRequest);
 
-        mockRequest = mock(XWikiRequest.class);
-        context.setRequest(mockRequest);
+        this.mockResponse = mock(XWikiResponse.class);
+        this.context.setResponse(this.mockResponse);
 
-        mockResponse = mock(XWikiResponse.class);
-        context.setResponse(mockResponse);
-
-        when(mockRequest.get("type")).thenReturn("plain");
+        when(this.mockRequest.get("type")).thenReturn("plain");
 
         this.oldcore.getMocker().registerMockComponent(ObservationManager.class);
     }
 
     @Test
-    public void newDocumentFromURL() throws Exception
+    void newDocumentFromURL() throws Exception
     {
         // new document = xwiki:X.Y
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X"), "Y");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -142,11 +144,11 @@ public void newDocumentFromURL() throws Exception
         verify(mockURLFactory).createURL("X", "Y", "edit", "template=&parent=Main.WebHome&title=Y", null, "xwiki",
             context);
     }
 
     @Test
-    public void newDocumentButNonTerminalFromURL() throws Exception
+    void newDocumentButNonTerminalFromURL() throws Exception
     {
         // new document = xwiki:X.Y
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X"), "Y");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -168,11 +170,11 @@ public void newDocumentButNonTerminalFromURL() throws Exception
         verify(mockURLFactory).createURL("X.Y", "WebHome", "edit", "template=&parent=Main.WebHome&title=Y", null,
             "xwiki", context);
     }
 
     @Test
-    public void newDocumentFromURLWhenNoType() throws Exception
+    void newDocumentFromURLWhenNoType() throws Exception
     {
         // No type has been set by the user
         when(mockRequest.get("type")).thenReturn(null);
 
         // new document = xwiki:X.Y
@@ -191,11 +193,11 @@ public void newDocumentFromURLWhenNoType() throws Exception
         // Verify null is returned (this means the response has been returned)
         assertEquals("create", result);
     }
 
     @Test
-    public void newDocumentWebHomeTopLevelFromURL() throws Exception
+    void newDocumentWebHomeTopLevelFromURL() throws Exception
     {
         // new document = xwiki:X.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -215,11 +217,11 @@ public void newDocumentWebHomeTopLevelFromURL() throws Exception
         verify(mockURLFactory).createURL("X", "WebHome", "edit", "template=&parent=Main.WebHome&title=X", null, "xwiki",
             context);
     }
 
     @Test
-    public void newDocumentWebHomeFromURL() throws Exception
+    void newDocumentWebHomeFromURL() throws Exception
     {
         // new document = xwiki:X.Y.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X", "Y"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -240,11 +242,11 @@ public void newDocumentWebHomeFromURL() throws Exception
         verify(mockURLFactory).createURL("X.Y", "WebHome", "edit", "template=&parent=Main.WebHome&title=Y", null,
             "xwiki", context);
     }
 
     @Test
-    public void newDocumentWebHomeButTerminalFromURL() throws Exception
+    void newDocumentWebHomeButTerminalFromURL() throws Exception
     {
         // new document = xwiki:X.Y.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X", "Y"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -267,11 +269,11 @@ public void newDocumentWebHomeButTerminalFromURL() throws Exception
         verify(mockURLFactory).createURL("X", "Y", "edit", "template=&parent=Main.WebHome&title=Y", null, "xwiki",
             context);
     }
 
     @Test
-    public void newDocumentWebHomeTopLevelSpaceButTerminalFromURL() throws Exception
+    void newDocumentWebHomeTopLevelSpaceButTerminalFromURL() throws Exception
     {
         // new document = xwiki:X.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -292,16 +294,15 @@ public void newDocumentWebHomeTopLevelSpaceButTerminalFromURL() throws Exception
 
         // Note: We can not create the "X" terminal document, since it is already at the top level of the hierarchy and
         // none was able to be deducted from the given information. The user needs to specify more info in order to
         // continue.
         // We should not get this far so no redirect should be done, just the template will be rendered.
-        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
-            any(), any(XWikiContext.class));
+        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(), any(), any(XWikiContext.class));
     }
 
     @Test
-    public void existingDocumentFromUINoName() throws Exception
+    void existingDocumentFromUINoName() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -318,16 +319,15 @@ public void existingDocumentFromUINoName() throws Exception
 
         // Verify that the create template is rendered, so the UI is displayed for the user to enter the missing values.
         assertEquals("create", result);
 
         // We should not get this far so no redirect should be done, just the template will be rendered.
-        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
-            any(), any(XWikiContext.class));
+        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(), any(), any(XWikiContext.class));
     }
 
     @Test
-    public void existingDocumentFromUI() throws Exception
+    void existingDocumentFromUI() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -351,11 +351,11 @@ public void existingDocumentFromUI() throws Exception
         verify(mockURLFactory).createURL("X.Y", "WebHome", "edit", "template=&parent=Main.WebHome&title=Y", null,
             "xwiki", context);
     }
 
     @Test
-    public void existingDocumentFromUICheckEscaping() throws Exception
+    void existingDocumentFromUICheckEscaping() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -379,11 +379,11 @@ public void existingDocumentFromUICheckEscaping() throws Exception
         verify(mockURLFactory).createURL("X.Y.Z", "WebHome", "edit", "template=&parent=Main.WebHome&title=Z", null,
             "xwiki", context);
     }
 
     @Test
-    public void existingDocumentTerminalFromUI() throws Exception
+    void existingDocumentTerminalFromUI() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -408,11 +408,11 @@ public void existingDocumentTerminalFromUI() throws Exception
         verify(mockURLFactory).createURL("X", "Y", "edit", "template=&parent=Main.WebHome&title=Y", null, "xwiki",
             context);
     }
 
     @Test
-    public void existingDocumentTerminalFromUICheckEscaping() throws Exception
+    void existingDocumentTerminalFromUICheckEscaping() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -437,11 +437,11 @@ public void existingDocumentTerminalFromUICheckEscaping() throws Exception
         verify(mockURLFactory).createURL("X.Y", "Z", "edit", "template=&parent=Main.WebHome&title=Z", null, "xwiki",
             context);
     }
 
     @Test
-    public void existingDocumentTerminalFromUIButAlreadyExisting() throws Exception
+    void existingDocumentTerminalFromUIButAlreadyExisting() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -471,16 +471,15 @@ public void existingDocumentTerminalFromUIButAlreadyExisting() throws Exception
         XWikiException exception = (XWikiException) this.oldcore.getScriptContext().getAttribute("createException");
         assertNotNull(exception);
         assertEquals(XWikiException.ERROR_XWIKI_APP_DOCUMENT_NOT_EMPTY, exception.getCode());
 
         // We should not get this far so no redirect should be done, just the template will be rendered.
-        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
-            any(), any(XWikiContext.class));
+        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(), any(), any(XWikiContext.class));
     }
 
     @Test
-    public void notExistingDocumentFromUIButNameTooLong() throws Exception
+    void notExistingDocumentFromUIButNameTooLong() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -503,16 +502,15 @@ public void notExistingDocumentFromUIButNameTooLong() throws Exception
         XWikiException exception = (XWikiException) this.oldcore.getScriptContext().getAttribute("createException");
         assertNotNull(exception);
         assertEquals(XWikiException.ERROR_XWIKI_APP_DOCUMENT_PATH_TOO_LONG, exception.getCode());
 
         // We should not get this far so no redirect should be done, just the template will be rendered.
-        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
-            any(), any(XWikiContext.class));
+        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(), any(), any(XWikiContext.class));
     }
 
     @Test
-    public void notExistingDocumentFromUIButSpaceTooLong() throws Exception
+    void notExistingDocumentFromUIButSpaceTooLong() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -535,16 +533,15 @@ public void notExistingDocumentFromUIButSpaceTooLong() throws Exception
         XWikiException exception = (XWikiException) this.oldcore.getScriptContext().getAttribute("createException");
         assertNotNull(exception);
         assertEquals(XWikiException.ERROR_XWIKI_APP_DOCUMENT_PATH_TOO_LONG, exception.getCode());
 
         // We should not get this far so no redirect should be done, just the template will be rendered.
-        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
-            any(), any(XWikiContext.class));
+        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(), any(), any(XWikiContext.class));
     }
 
     @Test
-    public void existingDocumentFromUITopLevelDocument() throws Exception
+    void existingDocumentFromUITopLevelDocument() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -571,11 +568,11 @@ public void existingDocumentFromUITopLevelDocument() throws Exception
     /*
      * Deprecated parameters
      */
 
     @Test
-    public void existingDocumentFromUIDeprecated() throws Exception
+    void existingDocumentFromUIDeprecated() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -599,11 +596,11 @@ public void existingDocumentFromUIDeprecated() throws Exception
         verify(mockURLFactory).createURL("X", "Y", "edit", "template=&parent=Main.WebHome&title=Y", null, "xwiki",
             context);
     }
 
     @Test
-    public void existingDocumentFromUIDeprecatedCheckEscaping() throws Exception
+    void existingDocumentFromUIDeprecatedCheckEscaping() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -628,11 +625,11 @@ public void existingDocumentFromUIDeprecatedCheckEscaping() throws Exception
         verify(mockURLFactory).createURL("X\\.Y", "Z", "edit", "template=&parent=Main.WebHome&title=Z", null, "xwiki",
             context);
     }
 
     @Test
-    public void existingDocumentNonTerminalFromUIDeprecated() throws Exception
+    void existingDocumentNonTerminalFromUIDeprecated() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -656,11 +653,11 @@ public void existingDocumentNonTerminalFromUIDeprecated() throws Exception
         verify(mockURLFactory).createURL("X", "WebHome", "edit", "template=&parent=Main.WebHome&title=X", null, "xwiki",
             context);
     }
 
     @Test
-    public void existingDocumentNonTerminalFromUIDeprecatedIgnoringPage() throws Exception
+    void existingDocumentNonTerminalFromUIDeprecatedIgnoringPage() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -686,11 +683,11 @@ public void existingDocumentNonTerminalFromUIDeprecatedIgnoringPage() throws Exc
         verify(mockURLFactory).createURL("X", "WebHome", "edit", "template=&parent=Main.WebHome&title=X", null, "xwiki",
             context);
     }
 
     @Test
-    public void existingDocumentNonTerminalFromUIDeprecatedCheckEscaping() throws Exception
+    void existingDocumentNonTerminalFromUIDeprecatedCheckEscaping() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -719,11 +716,11 @@ public void existingDocumentNonTerminalFromUIDeprecatedCheckEscaping() throws Ex
     /*
      * Template providers
      */
 
     @Test
-    public void existingDocumentFromUITemplateProviderExistingButNoneSelected() throws Exception
+    void existingDocumentFromUITemplateProviderExistingButNoneSelected() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -735,23 +732,22 @@ public void existingDocumentFromUITemplateProviderExistingButNoneSelected() thro
         when(mockRequest.getParameter("spaceReference")).thenReturn("X");
         when(mockRequest.getParameter("name")).thenReturn("Y");
 
         // Mock 1 existing template provider
         mockExistingTemplateProviders("XWiki.MyTemplateProvider",
-            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST);
+            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList());
 
         // Run the action
         String result = action.render(context);
 
         // The tests are below this line!
 
         // Verify that the create template is rendered, so the UI is displayed for the user to enter the missing values.
         assertEquals("create", result);
 
         // We should not get this far so no redirect should be done, just the template will be rendered.
-        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
-            any(), any(XWikiContext.class));
+        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(), any(), any(XWikiContext.class));
     }
 
     /**
      * Mocks 1 existing template provider.
      * <p>
@@ -843,11 +839,11 @@ private void mockTemplateDocumentExisting(String templateDocumentFullName,
         when(templateDocument.getDefaultEditMode(context)).thenReturn("edit");
         oldcore.getDocuments().put(new DocumentReference(templateDocumentReference, Locale.ROOT), templateDocument);
     }
 
     @Test
-    public void existingDocumentFromUITemplateProviderSpecified() throws Exception
+    void existingDocumentFromUITemplateProviderSpecified() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -861,11 +857,11 @@ public void existingDocumentFromUITemplateProviderSpecified() throws Exception
         when(mockRequest.getParameter("name")).thenReturn("Y");
         when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
 
         // Mock 1 existing template provider
         mockExistingTemplateProviders(templateProviderFullName,
-            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST);
+            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList());
 
         // Run the action
         String result = action.render(context);
 
         // The tests are below this line!
@@ -877,11 +873,11 @@ public void existingDocumentFromUITemplateProviderSpecified() throws Exception
         verify(mockURLFactory).createURL("X.Y", "WebHome", "edit",
             "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y", null, "xwiki", context);
     }
 
     @Test
-    public void existingDocumentFromUITemplateProviderSpecifiedRestrictionExists() throws Exception
+    void existingDocumentFromUITemplateProviderSpecifiedRestrictionExists() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -913,11 +909,11 @@ public void existingDocumentFromUITemplateProviderSpecifiedRestrictionExists() t
         verify(mockURLFactory).createURL("X.Y", "WebHome", "edit",
             "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y", null, "xwiki", context);
     }
 
     @Test
-    public void existingDocumentFromUITemplateProviderSpecifiedRestrictionExistsOnParentSpace() throws Exception
+    void existingDocumentFromUITemplateProviderSpecifiedRestrictionExistsOnParentSpace() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -951,11 +947,11 @@ public void existingDocumentFromUITemplateProviderSpecifiedRestrictionExistsOnPa
         verify(mockURLFactory).createURL("X.Y.Z.W", "WebHome", "edit",
             "template=XWiki.MyTemplate&parent=Main.WebHome&title=W", null, "xwiki", context);
     }
 
     @Test
-    public void existingDocumentFromUITemplateProviderSpecifiedButNotAllowed() throws Exception
+    void existingDocumentFromUITemplateProviderSpecifiedButNotAllowed() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -986,16 +982,15 @@ public void existingDocumentFromUITemplateProviderSpecifiedButNotAllowed() throw
         XWikiException exception = (XWikiException) this.oldcore.getScriptContext().getAttribute("createException");
         assertNotNull(exception);
         assertEquals(XWikiException.ERROR_XWIKI_APP_TEMPLATE_NOT_AVAILABLE, exception.getCode());
 
         // We should not get this far so no redirect should be done, just the template will be rendered.
-        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
-            any(), any(XWikiContext.class));
+        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(), any(), any(XWikiContext.class));
     }
 
     @Test
-    public void newDocumentFromURLTemplateProviderSpecifiedButNotAllowed() throws Exception
+    void newDocumentFromURLTemplateProviderSpecifiedButNotAllowed() throws Exception
     {
         // new document = xwiki:X.Y
         DocumentReference documentReference =
             new DocumentReference("Y", new SpaceReference("X", new WikiReference("xwiki")));
         XWikiDocument document = mock(XWikiDocument.class);
@@ -1025,16 +1020,15 @@ public void newDocumentFromURLTemplateProviderSpecifiedButNotAllowed() throws Ex
         XWikiException exception = (XWikiException) this.oldcore.getScriptContext().getAttribute("createException");
         assertNotNull(exception);
         assertEquals(XWikiException.ERROR_XWIKI_APP_TEMPLATE_NOT_AVAILABLE, exception.getCode());
 
         // We should not get this far so no redirect should be done, just the template will be rendered.
-        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
-            any(), any(XWikiContext.class));
+        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(), any(), any(XWikiContext.class));
     }
 
     @Test
-    public void newDocumentWebHomeFromURLTemplateProviderSpecifiedButNotAllowed() throws Exception
+    void newDocumentWebHomeFromURLTemplateProviderSpecifiedButNotAllowed() throws Exception
     {
         // new document = xwiki:X.Y.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X", "Y"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -1063,16 +1057,15 @@ public void newDocumentWebHomeFromURLTemplateProviderSpecifiedButNotAllowed() th
         XWikiException exception = (XWikiException) this.oldcore.getScriptContext().getAttribute("createException");
         assertNotNull(exception);
         assertEquals(XWikiException.ERROR_XWIKI_APP_TEMPLATE_NOT_AVAILABLE, exception.getCode());
 
         // We should not get this far so no redirect should be done, just the template will be rendered.
-        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
-            any(), any(XWikiContext.class));
+        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(), any(), any(XWikiContext.class));
     }
 
     @Test
-    public void newDocumentWebHomeFromURLTemplateProviderSpecifiedTerminal() throws Exception
+    void newDocumentWebHomeFromURLTemplateProviderSpecifiedTerminal() throws Exception
     {
         // new document = xwiki:X.Y.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X", "Y"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -1084,11 +1077,12 @@ public void newDocumentWebHomeFromURLTemplateProviderSpecifiedTerminal() throws
         String templateProviderFullName = "XWiki.MyTemplateProvider";
         when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
 
         // Mock 1 existing template provider
         mockExistingTemplateProviders(templateProviderFullName,
-            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, true);
+            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(),
+            true);
 
         // Run the action
         String result = action.render(context);
 
         // The tests are below this line!
@@ -1101,12 +1095,11 @@ public void newDocumentWebHomeFromURLTemplateProviderSpecifiedTerminal() throws
         verify(mockURLFactory).createURL("X", "Y", "edit", "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y",
             null, "xwiki", context);
     }
 
     @Test
-    public void newDocumentWebHomeFromURLTemplateProviderSpecifiedTerminalOverriddenFromUIToNonTerminal()
-        throws Exception
+    void newDocumentWebHomeFromURLTemplateProviderSpecifiedTerminalOverriddenFromUIToNonTerminal() throws Exception
     {
         // new document = xwiki:X.Y.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X", "Y"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -1119,11 +1112,12 @@ public void newDocumentWebHomeFromURLTemplateProviderSpecifiedTerminalOverridden
         when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
         when(mockRequest.getParameter("tocreate")).thenReturn("nonterminal");
 
         // Mock 1 existing template provider
         mockExistingTemplateProviders(templateProviderFullName,
-            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, true);
+            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(),
+            true);
 
         // Run the action
         String result = action.render(context);
 
         // The tests are below this line!
@@ -1136,11 +1130,11 @@ public void newDocumentWebHomeFromURLTemplateProviderSpecifiedTerminalOverridden
         verify(mockURLFactory).createURL("X.Y", "WebHome", "edit",
             "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y", null, "xwiki", context);
     }
 
     @Test
-    public void newDocumentFromURLTemplateProviderSpecifiedNonTerminal() throws Exception
+    void newDocumentFromURLTemplateProviderSpecifiedNonTerminal() throws Exception
     {
         // new document = xwiki:X.Y
         DocumentReference documentReference = new DocumentReference("xwiki", "X", "Y");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -1152,11 +1146,11 @@ public void newDocumentFromURLTemplateProviderSpecifiedNonTerminal() throws Exce
         String templateProviderFullName = "XWiki.MyTemplateProvider";
         when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
 
         // Mock 1 existing template provider
         mockExistingTemplateProviders(templateProviderFullName,
-            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST,
+            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(),
             false);
 
         // Run the action
         String result = action.render(context);
 
@@ -1170,11 +1164,11 @@ public void newDocumentFromURLTemplateProviderSpecifiedNonTerminal() throws Exce
         verify(mockURLFactory).createURL("X.Y", "WebHome", "edit",
             "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y", null, "xwiki", context);
     }
 
     @Test
-    public void newDocumentFromURLTemplateProviderSpecifiedNonTerminalButOverriddenFromUITerminal() throws Exception
+    void newDocumentFromURLTemplateProviderSpecifiedNonTerminalButOverriddenFromUITerminal() throws Exception
     {
         // new document = xwiki:X.Y
         DocumentReference documentReference = new DocumentReference("xwiki", "X", "Y");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -1187,11 +1181,11 @@ public void newDocumentFromURLTemplateProviderSpecifiedNonTerminalButOverriddenF
         when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
         when(mockRequest.getParameter("tocreate")).thenReturn("terminal");
 
         // Mock 1 existing template provider
         mockExistingTemplateProviders(templateProviderFullName,
-            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST,
+            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(),
             false);
 
         // Run the action
         String result = action.render(context);
 
@@ -1205,11 +1199,11 @@ public void newDocumentFromURLTemplateProviderSpecifiedNonTerminalButOverriddenF
         verify(mockURLFactory).createURL("X", "Y", "edit", "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y",
             null, "xwiki", context);
     }
 
     @Test
-    public void existingDocumentFromUITemplateSpecified() throws Exception
+    void existingDocumentFromUITemplateSpecified() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -1241,11 +1235,11 @@ public void existingDocumentFromUITemplateSpecified() throws Exception
         verify(mockURLFactory).createURL("X.Y", "WebHome", "edit",
             "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y", null, "xwiki", context);
     }
 
     @Test
-    public void existingDocumentFromUITemplateProviderSpecifiedTerminal() throws Exception
+    void existingDocumentFromUITemplateProviderSpecifiedTerminal() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -1260,11 +1254,12 @@ public void existingDocumentFromUITemplateProviderSpecifiedTerminal() throws Exc
         when(mockRequest.getParameter("name")).thenReturn("Y");
         when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
 
         // Mock 1 existing template provider that creates terminal documents.
         mockExistingTemplateProviders(templateProviderFullName,
-            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, true);
+            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(),
+            true);
 
         // Run the action
         String result = action.render(context);
 
         // The tests are below this line!
@@ -1277,11 +1272,11 @@ public void existingDocumentFromUITemplateProviderSpecifiedTerminal() throws Exc
         verify(mockURLFactory).createURL("X", "Y", "edit", "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y",
             null, "xwiki", context);
     }
 
     @Test
-    public void existingDocumentFromUITemplateProviderSpecifiedTerminalOverridenFromUIToNonTerminal() throws Exception
+    void existingDocumentFromUITemplateProviderSpecifiedTerminalOverridenFromUIToNonTerminal() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -1297,11 +1292,12 @@ public void existingDocumentFromUITemplateProviderSpecifiedTerminalOverridenFrom
         when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
         when(mockRequest.getParameter("tocreate")).thenReturn("nonterminal");
 
         // Mock 1 existing template provider that creates terminal documents.
         mockExistingTemplateProviders(templateProviderFullName,
-            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, true);
+            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(),
+            true);
 
         // Run the action
         String result = action.render(context);
 
         // The tests are below this line!
@@ -1314,11 +1310,11 @@ public void existingDocumentFromUITemplateProviderSpecifiedTerminalOverridenFrom
         verify(mockURLFactory).createURL("X.Y", "WebHome", "edit",
             "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y", null, "xwiki", context);
     }
 
     @Test
-    public void existingDocumentFromUITemplateProviderSpecifiedNonTerminal() throws Exception
+    void existingDocumentFromUITemplateProviderSpecifiedNonTerminal() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -1333,11 +1329,11 @@ public void existingDocumentFromUITemplateProviderSpecifiedNonTerminal() throws
         when(mockRequest.getParameter("name")).thenReturn("Y");
         when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
 
         // Mock 1 existing template provider that creates terminal documents.
         mockExistingTemplateProviders(templateProviderFullName,
-            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST,
+            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(),
             false);
 
         // Run the action
         String result = action.render(context);
 
@@ -1351,11 +1347,11 @@ public void existingDocumentFromUITemplateProviderSpecifiedNonTerminal() throws
         verify(mockURLFactory).createURL("X.Y", "WebHome", "edit",
             "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y", null, "xwiki", context);
     }
 
     @Test
-    public void existingDocumentFromUITemplateProviderSpecifiedNonTerminalOverridenFromUIToTerminal() throws Exception
+    void existingDocumentFromUITemplateProviderSpecifiedNonTerminalOverridenFromUIToTerminal() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -1371,11 +1367,11 @@ public void existingDocumentFromUITemplateProviderSpecifiedNonTerminalOverridenF
         when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
         when(mockRequest.getParameter("tocreate")).thenReturn("terminal");
 
         // Mock 1 existing template provider that creates terminal documents.
         mockExistingTemplateProviders(templateProviderFullName,
-            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST,
+            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(),
             false);
 
         // Run the action
         String result = action.render(context);
 
@@ -1389,11 +1385,11 @@ public void existingDocumentFromUITemplateProviderSpecifiedNonTerminalOverridenF
         verify(mockURLFactory).createURL("X", "Y", "edit", "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y",
             null, "xwiki", context);
     }
 
     @Test
-    public void newDocumentWebHomeFromURLTemplateProviderSpecifiedButOldPageType() throws Exception
+    void newDocumentWebHomeFromURLTemplateProviderSpecifiedButOldPageType() throws Exception
     {
         // new document = xwiki:X.Y.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X", "Y"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -1405,11 +1401,11 @@ public void newDocumentWebHomeFromURLTemplateProviderSpecifiedButOldPageType() t
         String templateProviderFullName = "XWiki.MyTemplateProvider";
         when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
 
         // Mock 1 existing template provider
         mockExistingTemplateProviders(templateProviderFullName,
-            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, null,
+            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(), null,
             "page");
 
         // Run the action
         String result = action.render(context);
 
@@ -1424,11 +1420,11 @@ public void newDocumentWebHomeFromURLTemplateProviderSpecifiedButOldPageType() t
         verify(mockURLFactory).createURL("X", "Y", "edit", "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y",
             null, "xwiki", context);
     }
 
     @Test
-    public void newDocumentWebHomeFromURLTemplateProviderSpecifiedButOldPageTypeButOverriddenFromUIToNonTerminal()
+    void newDocumentWebHomeFromURLTemplateProviderSpecifiedButOldPageTypeButOverriddenFromUIToNonTerminal()
         throws Exception
     {
         // new document = xwiki:X.Y.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X", "Y"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
@@ -1442,11 +1438,11 @@ public void newDocumentWebHomeFromURLTemplateProviderSpecifiedButOldPageTypeButO
         when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
         when(mockRequest.getParameter("tocreate")).thenReturn("nonterminal");
 
         // Mock 1 existing template provider
         mockExistingTemplateProviders(templateProviderFullName,
-            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, null,
+            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(), null,
             "page");
 
         // Run the action
         String result = action.render(context);
 
@@ -1461,11 +1457,11 @@ public void newDocumentWebHomeFromURLTemplateProviderSpecifiedButOldPageTypeButO
         verify(mockURLFactory).createURL("X.Y", "WebHome", "edit",
             "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y", null, "xwiki", context);
     }
 
     @Test
-    public void existingDocumentFromUITemplateProviderSpecifiedButOldSpaceType() throws Exception
+    void existingDocumentFromUITemplateProviderSpecifiedButOldSpaceType() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -1479,11 +1475,11 @@ public void existingDocumentFromUITemplateProviderSpecifiedButOldSpaceType() thr
         when(mockRequest.getParameter("name")).thenReturn("Y");
         when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
 
         // Mock 1 existing template provider
         mockExistingTemplateProviders(templateProviderFullName,
-            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, null,
+            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(), null,
             "space");
 
         // Run the action
         String result = action.render(context);
 
@@ -1498,12 +1494,11 @@ public void existingDocumentFromUITemplateProviderSpecifiedButOldSpaceType() thr
         verify(mockURLFactory).createURL("X.Y", "WebHome", "edit",
             "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y", null, "xwiki", context);
     }
 
     @Test
-    public void existingDocumentFromUITemplateProviderSpecifiedButOldSpaceTypeButOverridenFromUIToTerminal()
-        throws Exception
+    void existingDocumentFromUITemplateProviderSpecifiedButOldSpaceTypeButOverridenFromUIToTerminal() throws Exception
     {
         // current document = xwiki:Main.WebHome
         DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -1518,11 +1513,11 @@ public void existingDocumentFromUITemplateProviderSpecifiedButOldSpaceTypeButOve
         when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
         when(mockRequest.getParameter("tocreate")).thenReturn("terminal");
 
         // Mock 1 existing template provider
         mockExistingTemplateProviders(templateProviderFullName,
-            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, null,
+            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(), null,
             "space");
 
         // Run the action
         String result = action.render(context);
 
@@ -1536,11 +1531,11 @@ public void existingDocumentFromUITemplateProviderSpecifiedButOldSpaceTypeButOve
         verify(mockURLFactory).createURL("X", "Y", "edit", "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y",
             null, "xwiki", context);
     }
 
     @Test
-    public void newDocumentWebHomeFromURLTemplateProviderSpecifiedWithSaveAndEdit() throws Exception
+    void newDocumentWebHomeFromURLTemplateProviderSpecifiedWithSaveAndEdit() throws Exception
     {
         // Mock the document to create.
         DocumentReference documentReference = new DocumentReference("xwiki", "X", "Y");
         XWikiDocument document = mock(XWikiDocument.class);
         when(document.getDocumentReference()).thenReturn(documentReference);
@@ -1557,15 +1552,15 @@ public void newDocumentWebHomeFromURLTemplateProviderSpecifiedWithSaveAndEdit()
         this.context.setDoc(document);
         DocumentReference userReference = new DocumentReference("xwiki", "Users", "Alice");
         this.context.setUserReference(userReference);
 
         // Mock the creation request.
-        when(mockRequest.getParameter("spaceReference")).thenReturn("X");
-        when(mockRequest.getParameter("name")).thenReturn("Y");
-        when(mockRequest.getParameter("title")).thenReturn("Yippee");
+        when(this.mockRequest.getParameter("spaceReference")).thenReturn("X");
+        when(this.mockRequest.getParameter("name")).thenReturn("Y");
+        when(this.mockRequest.getParameter("title")).thenReturn("Yippee");
         String templateProviderFullName = "XWiki.MyTemplateProvider";
-        when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
+        when(this.mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
 
         // Mock the template provider.
         mockExistingTemplateProviders(templateProviderFullName,
             new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(), null,
             "page", "saveandedit");
diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/web/SaveActionTest.java b/xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/web/SaveActionTest.java
index e262a4af02c..3dc4af6af06 100644
--- a/xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/web/SaveActionTest.java
+++ b/xwiki-platform-core/xwiki-platform-oldcore/src/test/java/com/xpn/xwiki/web/SaveActionTest.java
@@ -22,22 +22,31 @@
 import java.util.Collections;
 import java.util.Date;
 
 import javax.inject.Named;
 
-import org.apache.commons.lang.time.DateUtils;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.suigeneris.jrcs.rcs.Version;
 import org.xwiki.configuration.ConfigurationSource;
 import org.xwiki.context.Execution;
+import org.xwiki.job.Job;
 import org.xwiki.model.reference.DocumentReference;
 import org.xwiki.model.validation.EntityNameValidation;
 import org.xwiki.model.validation.EntityNameValidationConfiguration;
 import org.xwiki.model.validation.EntityNameValidationManager;
+import org.xwiki.refactoring.job.CreateRequest;
+import org.xwiki.refactoring.script.RefactoringScriptService;
+import org.xwiki.refactoring.script.RequestFactory;
+import org.xwiki.script.service.ScriptService;
+import org.xwiki.security.authorization.ContextualAuthorizationManager;
+import org.xwiki.security.authorization.Right;
 import org.xwiki.test.annotation.ComponentList;
+import org.xwiki.test.junit5.mockito.InjectComponentManager;
+import org.xwiki.test.junit5.mockito.InjectMockComponents;
 import org.xwiki.test.junit5.mockito.MockComponent;
+import org.xwiki.test.mockito.MockitoComponentManager;
 
 import com.xpn.xwiki.XWiki;
 import com.xpn.xwiki.XWikiContext;
 import com.xpn.xwiki.doc.DocumentRevisionProvider;
 import com.xpn.xwiki.doc.XWikiDocument;
@@ -52,10 +61,11 @@
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 /**
  * Tests for {@link SaveAction}.
@@ -63,17 +73,20 @@
  * @version $Id$
  */
 @ComponentList
 @ReferenceComponentList
 @OldcoreTest(mockXWiki = false)
-public class SaveActionTest
+class SaveActionTest
 {
     private static final DocumentReference USER_REFERENCE = new DocumentReference("xwiki", "XWiki", "FooBar");
 
     @InjectMockitoOldcore
     private MockitoOldcore oldcore;
 
+    @InjectComponentManager
+    private MockitoComponentManager componentManager;
+
     @MockComponent
     private EntityNameValidationManager entityNameValidationManager;
 
     @MockComponent
     private EntityNameValidationConfiguration entityNameValidationConfiguration;
@@ -83,15 +96,19 @@
 
     @MockComponent
     @Named("xwikiproperties")
     private ConfigurationSource propertiesConf;
 
+    @MockComponent
+    private ContextualAuthorizationManager autorization;
+
     @MockComponent
     private DocumentRevisionProvider documentRevisionProvider;
 
     private XWikiContext context;
 
+    @InjectMockComponents
     private SaveAction saveAction;
 
     private XWikiRequest mockRequest;
 
     private XWikiResponse mockResponse;
@@ -105,34 +122,32 @@
     private XWiki xWiki;
 
     @BeforeEach
     void setup()
     {
-        this.saveAction = new SaveAction();
-
-        context = oldcore.getXWikiContext();
+        this.context = this.oldcore.getXWikiContext();
 
-        xWiki = mock(XWiki.class);
-        context.setWiki(this.xWiki);
+        this.xWiki = mock(XWiki.class);
+        this.context.setWiki(this.xWiki);
 
-        mockRequest = mock(XWikiRequest.class);
-        context.setRequest(mockRequest);
+        this.mockRequest = mock(XWikiRequest.class);
+        this.context.setRequest(this.mockRequest);
 
-        mockResponse = mock(XWikiResponse.class);
-        context.setResponse(mockResponse);
+        this.mockResponse = mock(XWikiResponse.class);
+        this.context.setResponse(this.mockResponse);
 
-        mockDocument = mock(XWikiDocument.class);
-        context.setDoc(mockDocument);
+        this.mockDocument = mock(XWikiDocument.class);
+        this.context.setDoc(this.mockDocument);
 
-        mockClonedDocument = mock(XWikiDocument.class);
-        when(mockDocument.clone()).thenReturn(mockClonedDocument);
+        this.mockClonedDocument = mock(XWikiDocument.class);
+        when(this.mockDocument.clone()).thenReturn(this.mockClonedDocument);
 
-        mockForm = mock(EditForm.class);
-        context.setForm(mockForm);
+        this.mockForm = mock(EditForm.class);
+        this.context.setForm(this.mockForm);
         when(this.entityNameValidationConfiguration.useValidation()).thenReturn(false);
 
-        context.setUserReference(USER_REFERENCE);
+        this.context.setUserReference(USER_REFERENCE);
     }
 
     @Test
     void newDocumentInvalidName() throws Exception
     {
@@ -144,11 +159,11 @@ void newDocumentInvalidName() throws Exception
         when(this.entityNameValidationManager.getEntityReferenceNameStrategy()).thenReturn(entityNameValidation);
         when(entityNameValidation.isValid(documentReference)).thenReturn(false);
 
         assertTrue(saveAction.save(this.context));
         assertEquals("entitynamevalidation.create.invalidname", context.get("message"));
-        assertArrayEquals(new Object[] { "Foo.Bar" }, (Object[]) context.get("messageParameters"));
+        assertArrayEquals(new Object[] {"Foo.Bar"}, (Object[]) context.get("messageParameters"));
     }
 
     @Test
     void validSave() throws Exception
     {
@@ -157,11 +172,10 @@ void validSave() throws Exception
         when(mockClonedDocument.getLock(this.context)).thenReturn(mock(XWikiLock.class));
         when(mockForm.getTemplate()).thenReturn("");
         assertFalse(saveAction.save(this.context));
         assertEquals(new Version("1.2"), this.context.get("SaveAction.savedObjectVersion"));
 
-        verify(mockClonedDocument).readFromTemplate("", this.context);
         verify(mockClonedDocument).setAuthor("XWiki.FooBar");
         verify(mockClonedDocument).setMetaDataDirty(true);
         verify(this.xWiki).checkSavingDocument(USER_REFERENCE, mockClonedDocument, "My Changes", false, this.context);
         verify(this.xWiki).saveDocument(mockClonedDocument, "My Changes", false, this.context);
         verify(mockClonedDocument).removeLock(this.context);
@@ -179,12 +193,12 @@ void validSaveNewTranslation() throws Exception
         when(xWiki.isMultiLingual(this.context)).thenReturn(true);
         when(mockRequest.getParameter("previousVersion")).thenReturn("1.1");
         when(mockRequest.getParameter("isNew")).thenReturn("true");
         assertFalse(saveAction.save(this.context));
         assertEquals(new Version("1.1"), this.context.get("SaveAction.savedObjectVersion"));
-        verify(this.xWiki).checkSavingDocument(eq(USER_REFERENCE), any(XWikiDocument.class), eq(""),
-            eq(false), eq(this.context));
+        verify(this.xWiki).checkSavingDocument(eq(USER_REFERENCE), any(XWikiDocument.class), eq(""), eq(false),
+            eq(this.context));
         verify(this.xWiki).saveDocument(any(XWikiDocument.class), eq(""), eq(false), eq(this.context));
     }
 
     @Test
     void validSaveOldTranslation() throws Exception
@@ -208,12 +222,12 @@ void validSaveOldTranslation() throws Exception
         verify(this.xWiki).checkSavingDocument(USER_REFERENCE, mockClonedDocument, "My Changes", false, this.context);
         verify(this.xWiki).saveDocument(mockClonedDocument, "My Changes", false, this.context);
     }
 
     /**
-     * This tests aims at checking the usecase when uploading an image in the WYSIWYG editor before the first save
-     * and saving afterwards.
+     * This tests aims at checking the usecase when uploading an image in the WYSIWYG editor before the first save and
+     * saving afterwards.
      */
     @Test
     void validSaveRequestImageUploadAndConflictCheck() throws Exception
     {
         when(mockDocument.getRCSVersion()).thenReturn(new Version("1.2"));
@@ -235,14 +249,40 @@ void validSaveRequestImageUploadAndConflictCheck() throws Exception
         when(mockDocument.getObjectDiff("1.1", "1.2", context)).thenReturn(Collections.emptyList());
 
         assertFalse(saveAction.save(this.context));
         assertEquals(new Version("1.2"), this.context.get("SaveAction.savedObjectVersion"));
 
-        verify(mockClonedDocument).readFromTemplate("", this.context);
         verify(mockClonedDocument).setAuthor("XWiki.FooBar");
         verify(mockClonedDocument).setMetaDataDirty(true);
         verify(this.xWiki).checkSavingDocument(USER_REFERENCE, mockClonedDocument, "My Changes", false, this.context);
         verify(this.xWiki).saveDocument(mockClonedDocument, "My Changes", false, this.context);
         verify(mockClonedDocument).removeLock(this.context);
+    }
+
+    @Test
+    void saveFromTemplate() throws Exception
+    {
+        when(this.mockForm.getTemplate()).thenReturn("TemplateSpace.TemplateDocument");
+        DocumentReference templateReference =
+            new DocumentReference(context.getWikiId(), "TemplateSpace", "TemplateDocument");
+
+        when(this.autorization.hasAccess(Right.VIEW, templateReference)).thenReturn(false);
+
+        assertFalse(this.saveAction.save(this.context));
+
+        verify(this.mockClonedDocument, never()).readFromTemplate(templateReference, this.context);
+
+        when(this.autorization.hasAccess(Right.VIEW, templateReference)).thenReturn(true);
+        RefactoringScriptService refactoring = mock(RefactoringScriptService.class);
+        RequestFactory requestFactory = mock(RequestFactory.class);
+        when(refactoring.getRequestFactory()).thenReturn(requestFactory);
+        CreateRequest request = mock(CreateRequest.class);
+        when(requestFactory.createCreateRequest(any())).thenReturn(request);
+        Job job = mock(Job.class);
+        when(refactoring.create(request)).thenReturn(job);
+        this.componentManager.registerComponent(ScriptService.class, "refactoring", refactoring);
+
+        assertFalse(this.saveAction.save(this.context));
 
+        verify(this.mockClonedDocument).readFromTemplate(templateReference, this.context);
     }
 }

Vulnerable code:                 tdoc2.readFromTemplate(peform, context);
        DocumentReferenceResolver<String> resolver = getCurrentMixedDocumentReferenceResolver();
        DocumentReference templateReference = resolver.resolve(template);
        newDocument.readFromTemplate(templateReference, context);
            DocumentReference parentReference = resolver.resolve(parent);
        editedDocument.readFromTemplate(editForm, context);
                doc2.readFromTemplate(peform, context);
                cdoc2.readFromTemplate(peform, context);
            tdoc.readFromTemplate(form.getTemplate(), context);
        if (StringUtils.isBlank(editForm.getTemplate())) {
        DocumentReferenceResolver<String> resolver =
            Utils.getComponent(DocumentReferenceResolver.TYPE_STRING, "currentmixed");
        EntityReference templateReference = resolver.resolve(editForm.getTemplate());
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import com.xpn.xwiki.test.MockitoOldcoreRule;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
public class CreateActionTest
    @Rule
    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule();
    @Before
    public void setUp() throws Exception
        context = oldcore.getXWikiContext();
        Utils.setComponentManager(oldcore.getMocker());
            oldcore.getMocker().registerMockComponent((Type) QueryManager.class, "secure");
        mockTemplateProvidersQuery = mock(Query.class);
        when(mockSecureQueryManager.createQuery(any(), any())).thenReturn(mockTemplateProvidersQuery);
        when(mockTemplateProvidersQuery.execute()).thenReturn(Collections.emptyList());
        when(oldcore.getMockContextualAuthorizationManager().hasAccess(any(Right.class), any(EntityReference.class)))
            .thenReturn(true);
            oldcore.getMocker().registerMockComponent(DocumentReference.TYPE_PROVIDER);
        mockURLFactory = mock(XWikiURLFactory.class);
        context.setURLFactory(mockURLFactory);
        action = new CreateAction();
        mockRequest = mock(XWikiRequest.class);
        context.setRequest(mockRequest);
        mockResponse = mock(XWikiResponse.class);
        context.setResponse(mockResponse);

        when(mockRequest.get("type")).thenReturn("plain");
    public void newDocumentFromURL() throws Exception
    public void newDocumentButNonTerminalFromURL() throws Exception
    public void newDocumentFromURLWhenNoType() throws Exception
    public void newDocumentWebHomeTopLevelFromURL() throws Exception
    public void newDocumentWebHomeFromURL() throws Exception
    public void newDocumentWebHomeButTerminalFromURL() throws Exception
    public void newDocumentWebHomeTopLevelSpaceButTerminalFromURL() throws Exception
        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
            any(), any(XWikiContext.class));
    public void existingDocumentFromUINoName() throws Exception
        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
            any(), any(XWikiContext.class));
    public void existingDocumentFromUI() throws Exception
    public void existingDocumentFromUICheckEscaping() throws Exception
    public void existingDocumentTerminalFromUI() throws Exception
    public void existingDocumentTerminalFromUICheckEscaping() throws Exception
    public void existingDocumentTerminalFromUIButAlreadyExisting() throws Exception
        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
            any(), any(XWikiContext.class));
    public void notExistingDocumentFromUIButNameTooLong() throws Exception
        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
            any(), any(XWikiContext.class));
    public void notExistingDocumentFromUIButSpaceTooLong() throws Exception
        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
            any(), any(XWikiContext.class));
    public void existingDocumentFromUITopLevelDocument() throws Exception
    public void existingDocumentFromUIDeprecated() throws Exception
    public void existingDocumentFromUIDeprecatedCheckEscaping() throws Exception
    public void existingDocumentNonTerminalFromUIDeprecated() throws Exception
    public void existingDocumentNonTerminalFromUIDeprecatedIgnoringPage() throws Exception
    public void existingDocumentNonTerminalFromUIDeprecatedCheckEscaping() throws Exception
    public void existingDocumentFromUITemplateProviderExistingButNoneSelected() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST);
        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
            any(), any(XWikiContext.class));
    public void existingDocumentFromUITemplateProviderSpecified() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST);
    public void existingDocumentFromUITemplateProviderSpecifiedRestrictionExists() throws Exception
    public void existingDocumentFromUITemplateProviderSpecifiedRestrictionExistsOnParentSpace() throws Exception
    public void existingDocumentFromUITemplateProviderSpecifiedButNotAllowed() throws Exception
        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
            any(), any(XWikiContext.class));
    public void newDocumentFromURLTemplateProviderSpecifiedButNotAllowed() throws Exception
        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
            any(), any(XWikiContext.class));
    public void newDocumentWebHomeFromURLTemplateProviderSpecifiedButNotAllowed() throws Exception
        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
            any(), any(XWikiContext.class));
    public void newDocumentWebHomeFromURLTemplateProviderSpecifiedTerminal() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, true);
    public void newDocumentWebHomeFromURLTemplateProviderSpecifiedTerminalOverriddenFromUIToNonTerminal()
        throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, true);
    public void newDocumentFromURLTemplateProviderSpecifiedNonTerminal() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST,
    public void newDocumentFromURLTemplateProviderSpecifiedNonTerminalButOverriddenFromUITerminal() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST,
    public void existingDocumentFromUITemplateSpecified() throws Exception
    public void existingDocumentFromUITemplateProviderSpecifiedTerminal() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, true);
    public void existingDocumentFromUITemplateProviderSpecifiedTerminalOverridenFromUIToNonTerminal() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, true);
    public void existingDocumentFromUITemplateProviderSpecifiedNonTerminal() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST,
    public void existingDocumentFromUITemplateProviderSpecifiedNonTerminalOverridenFromUIToTerminal() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST,
    public void newDocumentWebHomeFromURLTemplateProviderSpecifiedButOldPageType() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, null,
    public void newDocumentWebHomeFromURLTemplateProviderSpecifiedButOldPageTypeButOverriddenFromUIToNonTerminal()
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, null,
    public void existingDocumentFromUITemplateProviderSpecifiedButOldSpaceType() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, null,
    public void existingDocumentFromUITemplateProviderSpecifiedButOldSpaceTypeButOverridenFromUIToTerminal()
        throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, null,
    public void newDocumentWebHomeFromURLTemplateProviderSpecifiedWithSaveAndEdit() throws Exception
        when(mockRequest.getParameter("spaceReference")).thenReturn("X");
        when(mockRequest.getParameter("name")).thenReturn("Y");
        when(mockRequest.getParameter("title")).thenReturn("Yippee");
        when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
import org.apache.commons.lang.time.DateUtils;
public class SaveActionTest
        this.saveAction = new SaveAction();

        context = oldcore.getXWikiContext();
        xWiki = mock(XWiki.class);
        context.setWiki(this.xWiki);
        mockRequest = mock(XWikiRequest.class);
        context.setRequest(mockRequest);
        mockResponse = mock(XWikiResponse.class);
        context.setResponse(mockResponse);
        mockDocument = mock(XWikiDocument.class);
        context.setDoc(mockDocument);
        mockClonedDocument = mock(XWikiDocument.class);
        when(mockDocument.clone()).thenReturn(mockClonedDocument);
        mockForm = mock(EditForm.class);
        context.setForm(mockForm);
        context.setUserReference(USER_REFERENCE);
        assertArrayEquals(new Object[] { "Foo.Bar" }, (Object[]) context.get("messageParameters"));
        verify(mockClonedDocument).readFromTemplate("", this.context);
        verify(this.xWiki).checkSavingDocument(eq(USER_REFERENCE), any(XWikiDocument.class), eq(""),
            eq(false), eq(this.context));
     * This tests aims at checking the usecase when uploading an image in the WYSIWYG editor before the first save
     * and saving afterwards.
        verify(mockClonedDocument).readFromTemplate("", this.context);
Fixed code:                 readFromTemplate(tdoc2, peform.getTemplate(), context);
        readFromTemplate(newDocument, template, context);
            DocumentReference parentReference = this.currentmixedReferenceResolver.resolve(parent);
        readFromTemplate(editedDocument, editForm.getTemplate(), context);
                readFromTemplate(doc2, peform.getTemplate(), context);
                readFromTemplate(cdoc2, peform.getTemplate(), context);
            readFromTemplate(tdoc, form.getTemplate(), context);
        DocumentReference templateReference = resolveTemplate(editForm.getTemplate());

        if (templateReference == null) {
import javax.inject.Named;
import org.xwiki.security.authorization.ContextualAuthorizationManager;
import org.xwiki.security.authorization.Right;
    @Inject
    protected ContextualAuthorizationManager autorization;

    @Inject
    @Named("currentmixed")
    protected DocumentReferenceResolver<String> currentmixedReferenceResolver;


    /**
     * Helper used resolve the template passed to the action if the current user have access to it.
     * 
     * @param template the template to copy
     * @return the reference of the template if not empty and the current user have access to it
     * @since 12.10.6
     * @since 13.2RC1
     */
    protected DocumentReference resolveTemplate(String template)
    {
        if (StringUtils.isNotBlank(template)) {
            DocumentReference templateReference = this.currentmixedReferenceResolver.resolve(template);

            // Make sure the current user have access to the template document before copying it
            if (this.autorization.hasAccess(Right.VIEW, templateReference)) {
                return templateReference;
            }
        }

        return null;
    }

    /**
     * Helper used by various actions to initialize a document by copying a template to it.
     * 
     * @param document the document to update
     * @param template the template to copy
     * @param context the XWiki context
     * @return true if the document was updated, false otherwise (for example when the current user does not have view
     *         right on the template document)
     * @throws XWikiException when failing to copy the template
     * @since 12.10.6
     * @since 13.2RC1
     */
    @Unstable
    protected boolean readFromTemplate(XWikiDocument document, String template, XWikiContext context)
        throws XWikiException
    {
        DocumentReference templateReference = resolveTemplate(template);

        if (templateReference != null) {
            document.readFromTemplate(templateReference, context);

            return true;
        }

        return false;
    }
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.xwiki.test.junit5.mockito.InjectMockComponents;
import com.xpn.xwiki.test.MockitoOldcore;
import com.xpn.xwiki.test.junit5.mockito.InjectMockitoOldcore;
import com.xpn.xwiki.test.junit5.mockito.OldcoreTest;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
@OldcoreTest
class CreateActionTest
    @InjectMockitoOldcore
    MockitoOldcore oldcore;
    @InjectMockComponents
    @BeforeEach
    public void beforeEach() throws Exception
        this.context = this.oldcore.getXWikiContext();
        Utils.setComponentManager(this.oldcore.getMocker());
            this.oldcore.getMocker().registerMockComponent((Type) QueryManager.class, "secure");
        this.mockTemplateProvidersQuery = mock(Query.class);
        when(mockSecureQueryManager.createQuery(any(), any())).thenReturn(this.mockTemplateProvidersQuery);
        when(this.mockTemplateProvidersQuery.execute()).thenReturn(Collections.emptyList());
        when(this.oldcore.getMockContextualAuthorizationManager().hasAccess(any(Right.class),
            any(EntityReference.class))).thenReturn(true);
            this.oldcore.getMocker().registerMockComponent(DocumentReference.TYPE_PROVIDER);
        this.mockURLFactory = mock(XWikiURLFactory.class);
        this.context.setURLFactory(this.mockURLFactory);
        this.mockRequest = mock(XWikiRequest.class);
        this.context.setRequest(this.mockRequest);
        this.mockResponse = mock(XWikiResponse.class);
        this.context.setResponse(this.mockResponse);
        when(this.mockRequest.get("type")).thenReturn("plain");
    void newDocumentFromURL() throws Exception
    void newDocumentButNonTerminalFromURL() throws Exception
    void newDocumentFromURLWhenNoType() throws Exception
    void newDocumentWebHomeTopLevelFromURL() throws Exception
    void newDocumentWebHomeFromURL() throws Exception
    void newDocumentWebHomeButTerminalFromURL() throws Exception
    void newDocumentWebHomeTopLevelSpaceButTerminalFromURL() throws Exception
        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(), any(), any(XWikiContext.class));
    void existingDocumentFromUINoName() throws Exception
        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(), any(), any(XWikiContext.class));
    void existingDocumentFromUI() throws Exception
    void existingDocumentFromUICheckEscaping() throws Exception
    void existingDocumentTerminalFromUI() throws Exception
    void existingDocumentTerminalFromUICheckEscaping() throws Exception
    void existingDocumentTerminalFromUIButAlreadyExisting() throws Exception
        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(), any(), any(XWikiContext.class));
    void notExistingDocumentFromUIButNameTooLong() throws Exception
        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(), any(), any(XWikiContext.class));
    void notExistingDocumentFromUIButSpaceTooLong() throws Exception
        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(), any(), any(XWikiContext.class));
    void existingDocumentFromUITopLevelDocument() throws Exception
    void existingDocumentFromUIDeprecated() throws Exception
    void existingDocumentFromUIDeprecatedCheckEscaping() throws Exception
    void existingDocumentNonTerminalFromUIDeprecated() throws Exception
    void existingDocumentNonTerminalFromUIDeprecatedIgnoringPage() throws Exception
    void existingDocumentNonTerminalFromUIDeprecatedCheckEscaping() throws Exception
    void existingDocumentFromUITemplateProviderExistingButNoneSelected() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList());
        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(), any(), any(XWikiContext.class));
    void existingDocumentFromUITemplateProviderSpecified() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList());
    void existingDocumentFromUITemplateProviderSpecifiedRestrictionExists() throws Exception
    void existingDocumentFromUITemplateProviderSpecifiedRestrictionExistsOnParentSpace() throws Exception
    void existingDocumentFromUITemplateProviderSpecifiedButNotAllowed() throws Exception
        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(), any(), any(XWikiContext.class));
    void newDocumentFromURLTemplateProviderSpecifiedButNotAllowed() throws Exception
        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(), any(), any(XWikiContext.class));
    void newDocumentWebHomeFromURLTemplateProviderSpecifiedButNotAllowed() throws Exception
        verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(), any(), any(XWikiContext.class));
    void newDocumentWebHomeFromURLTemplateProviderSpecifiedTerminal() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(),
            true);
    void newDocumentWebHomeFromURLTemplateProviderSpecifiedTerminalOverriddenFromUIToNonTerminal() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(),
            true);
    void newDocumentFromURLTemplateProviderSpecifiedNonTerminal() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(),
    void newDocumentFromURLTemplateProviderSpecifiedNonTerminalButOverriddenFromUITerminal() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(),
    void existingDocumentFromUITemplateSpecified() throws Exception
    void existingDocumentFromUITemplateProviderSpecifiedTerminal() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(),
            true);
    void existingDocumentFromUITemplateProviderSpecifiedTerminalOverridenFromUIToNonTerminal() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(),
            true);
    void existingDocumentFromUITemplateProviderSpecifiedNonTerminal() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(),
    void existingDocumentFromUITemplateProviderSpecifiedNonTerminalOverridenFromUIToTerminal() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(),
    void newDocumentWebHomeFromURLTemplateProviderSpecifiedButOldPageType() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(), null,
    void newDocumentWebHomeFromURLTemplateProviderSpecifiedButOldPageTypeButOverriddenFromUIToNonTerminal()
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(), null,
    void existingDocumentFromUITemplateProviderSpecifiedButOldSpaceType() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(), null,
    void existingDocumentFromUITemplateProviderSpecifiedButOldSpaceTypeButOverridenFromUIToTerminal() throws Exception
            new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.emptyList(), null,
    void newDocumentWebHomeFromURLTemplateProviderSpecifiedWithSaveAndEdit() throws Exception
        when(this.mockRequest.getParameter("spaceReference")).thenReturn("X");
        when(this.mockRequest.getParameter("name")).thenReturn("Y");
        when(this.mockRequest.getParameter("title")).thenReturn("Yippee");
        when(this.mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
import org.xwiki.job.Job;
import org.xwiki.refactoring.job.CreateRequest;
import org.xwiki.refactoring.script.RefactoringScriptService;
import org.xwiki.refactoring.script.RequestFactory;
import org.xwiki.script.service.ScriptService;
import org.xwiki.security.authorization.ContextualAuthorizationManager;
import org.xwiki.security.authorization.Right;
import org.xwiki.test.junit5.mockito.InjectComponentManager;
import org.xwiki.test.junit5.mockito.InjectMockComponents;
import org.xwiki.test.mockito.MockitoComponentManager;
import static org.mockito.Mockito.never;
class SaveActionTest
    @InjectComponentManager
    private MockitoComponentManager componentManager;

    @MockComponent
    private ContextualAuthorizationManager autorization;

    @InjectMockComponents
        this.context = this.oldcore.getXWikiContext();
        this.xWiki = mock(XWiki.class);
        this.context.setWiki(this.xWiki);
        this.mockRequest = mock(XWikiRequest.class);
        this.context.setRequest(this.mockRequest);
        this.mockResponse = mock(XWikiResponse.class);
        this.context.setResponse(this.mockResponse);
        this.mockDocument = mock(XWikiDocument.class);
        this.context.setDoc(this.mockDocument);
        this.mockClonedDocument = mock(XWikiDocument.class);
        when(this.mockDocument.clone()).thenReturn(this.mockClonedDocument);
        this.mockForm = mock(EditForm.class);
        this.context.setForm(this.mockForm);
        this.context.setUserReference(USER_REFERENCE);
        assertArrayEquals(new Object[] {"Foo.Bar"}, (Object[]) context.get("messageParameters"));
        verify(this.xWiki).checkSavingDocument(eq(USER_REFERENCE), any(XWikiDocument.class), eq(""), eq(false),
            eq(this.context));
     * This tests aims at checking the usecase when uploading an image in the WYSIWYG editor before the first save and
     * saving afterwards.
    }

    @Test
    void saveFromTemplate() throws Exception
    {
        when(this.mockForm.getTemplate()).thenReturn("TemplateSpace.TemplateDocument");
        DocumentReference templateReference =
            new DocumentReference(context.getWikiId(), "TemplateSpace", "TemplateDocument");

        when(this.autorization.hasAccess(Right.VIEW, templateReference)).thenReturn(false);

        assertFalse(this.saveAction.save(this.context));

        verify(this.mockClonedDocument, never()).readFromTemplate(templateReference, this.context);

        when(this.autorization.hasAccess(Right.VIEW, templateReference)).thenReturn(true);
        RefactoringScriptService refactoring = mock(RefactoringScriptService.class);
        RequestFactory requestFactory = mock(RequestFactory.class);
        when(refactoring.getRequestFactory()).thenReturn(requestFactory);
        CreateRequest request = mock(CreateRequest.class);
        when(requestFactory.createCreateRequest(any())).thenReturn(request);
        Job job = mock(Job.class);
        when(refactoring.create(request)).thenReturn(job);
        this.componentManager.registerComponent(ScriptService.class, "refactoring", refactoring);

        assertFalse(this.saveAction.save(this.context));
        verify(this.mockClonedDocument).readFromTemplate(templateReference, this.context);