1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.web

File CreateActionTest.java

 

Code metrics

6
490
44
1
1,434
796
47
0.1
11.14
44
1.07

Classes

Class Line # Actions
CreateActionTest 67 490 0% 47 0
1.0100%
 

Contributing tests

This file is covered by 39 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.web;
21   
22    import java.lang.reflect.Type;
23    import java.util.ArrayList;
24    import java.util.Arrays;
25    import java.util.Collections;
26    import java.util.List;
27    import java.util.Locale;
28   
29    import javax.inject.Provider;
30   
31    import org.junit.Before;
32    import org.junit.Rule;
33    import org.junit.Test;
34    import org.xwiki.model.reference.DocumentReference;
35    import org.xwiki.model.reference.EntityReference;
36    import org.xwiki.model.reference.SpaceReference;
37    import org.xwiki.model.reference.WikiReference;
38    import org.xwiki.query.Query;
39    import org.xwiki.query.QueryManager;
40    import org.xwiki.security.authorization.Right;
41    import org.xwiki.test.annotation.ComponentList;
42   
43    import com.xpn.xwiki.XWikiContext;
44    import com.xpn.xwiki.XWikiException;
45    import com.xpn.xwiki.doc.XWikiDocument;
46    import com.xpn.xwiki.objects.BaseObject;
47    import com.xpn.xwiki.test.MockitoOldcoreRule;
48    import com.xpn.xwiki.test.reference.ReferenceComponentList;
49   
50    import static org.junit.Assert.assertEquals;
51    import static org.junit.Assert.assertNotNull;
52    import static org.junit.Assert.assertNull;
53    import static org.mockito.ArgumentMatchers.any;
54    import static org.mockito.Mockito.mock;
55    import static org.mockito.Mockito.never;
56    import static org.mockito.Mockito.verify;
57    import static org.mockito.Mockito.when;
58   
59    /**
60    * Unit tests for {@link com.xpn.xwiki.web.CreateAction}.
61    *
62    * @version $Id: 959da5354f14cb2b8f7807396618eb3bbc4eeef4 $
63    * @since 7.2M1
64    */
65    @ComponentList
66    @ReferenceComponentList
 
67    public class CreateActionTest
68    {
69    @Rule
70    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule();
71   
72    XWikiURLFactory mockURLFactory;
73   
74    CreateAction action;
75   
76    XWikiContext context;
77   
78    XWikiRequest mockRequest;
79   
80    XWikiResponse mockResponse;
81   
82    Query mockTemplateProvidersQuery;
83   
 
84  39 toggle @Before
85    public void setUp() throws Exception
86    {
87  39 context = oldcore.getXWikiContext();
88   
89  39 Utils.setComponentManager(oldcore.getMocker());
90   
91  39 QueryManager mockSecureQueryManager =
92    oldcore.getMocker().registerMockComponent((Type) QueryManager.class, "secure");
93   
94  39 mockTemplateProvidersQuery = mock(Query.class);
95  39 when(mockSecureQueryManager.createQuery(any(), any())).thenReturn(mockTemplateProvidersQuery);
96  39 when(mockTemplateProvidersQuery.execute()).thenReturn(Collections.emptyList());
97   
98  39 when(oldcore.getMockContextualAuthorizationManager().hasAccess(any(Right.class), any(EntityReference.class)))
99    .thenReturn(true);
100   
101  39 Provider<DocumentReference> mockDocumentReferenceProvider =
102    oldcore.getMocker().registerMockComponent(DocumentReference.TYPE_PROVIDER);
103  39 when(mockDocumentReferenceProvider.get())
104    .thenReturn(new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome"));
105   
106  39 mockURLFactory = mock(XWikiURLFactory.class);
107  39 context.setURLFactory(mockURLFactory);
108   
109  39 action = new CreateAction();
110   
111  39 mockRequest = mock(XWikiRequest.class);
112  39 context.setRequest(mockRequest);
113   
114  39 mockResponse = mock(XWikiResponse.class);
115  39 context.setResponse(mockResponse);
116   
117  39 when(mockRequest.get("type")).thenReturn("plain");
118    }
119   
 
120  1 toggle @Test
121    public void newDocumentFromURL() throws Exception
122    {
123    // new document = xwiki:X.Y
124  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X"), "Y");
125  1 XWikiDocument document = mock(XWikiDocument.class);
126  1 when(document.getDocumentReference()).thenReturn(documentReference);
127  1 when(document.isNew()).thenReturn(true);
128   
129  1 context.setDoc(document);
130   
131    // Run the action
132  1 String result = action.render(context);
133   
134    // The tests are below this line!
135   
136    // Verify null is returned (this means the response has been returned)
137  1 assertNull(result);
138   
139  1 verify(mockURLFactory).createURL("X", "Y", "edit", "template=&parent=Main.WebHome&title=Y", null, "xwiki",
140    context);
141    }
142   
 
143  1 toggle @Test
144    public void newDocumentButNonTerminalFromURL() throws Exception
145    {
146    // new document = xwiki:X.Y
147  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X"), "Y");
148  1 XWikiDocument document = mock(XWikiDocument.class);
149  1 when(document.getDocumentReference()).thenReturn(documentReference);
150  1 when(document.isNew()).thenReturn(true);
151   
152  1 context.setDoc(document);
153   
154    // Pass the tocreate=nonterminal request parameter
155  1 when(mockRequest.getParameter("tocreate")).thenReturn("nonterminal");
156   
157    // Run the action
158  1 String result = action.render(context);
159   
160    // The tests are below this line!
161   
162    // Verify null is returned (this means the response has been returned)
163  1 assertNull(result);
164   
165  1 verify(mockURLFactory).createURL("X.Y", "WebHome", "edit", "template=&parent=Main.WebHome&title=Y", null,
166    "xwiki", context);
167    }
168   
 
169  1 toggle @Test
170    public void newDocumentFromURLWhenNoType() throws Exception
171    {
172    // No type has been set by the user
173  1 when(mockRequest.get("type")).thenReturn(null);
174   
175    // new document = xwiki:X.Y
176  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X"), "Y");
177  1 XWikiDocument document = mock(XWikiDocument.class);
178  1 when(document.getDocumentReference()).thenReturn(documentReference);
179  1 when(document.isNew()).thenReturn(true);
180   
181  1 context.setDoc(document);
182   
183    // Run the action
184  1 String result = action.render(context);
185   
186    // The tests are below this line!
187   
188    // Verify null is returned (this means the response has been returned)
189  1 assertEquals("create", result);
190    }
191   
 
192  1 toggle @Test
193    public void newDocumentWebHomeTopLevelFromURL() throws Exception
194    {
195    // new document = xwiki:X.WebHome
196  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X"), "WebHome");
197  1 XWikiDocument document = mock(XWikiDocument.class);
198  1 when(document.getDocumentReference()).thenReturn(documentReference);
199  1 when(document.isNew()).thenReturn(true);
200   
201  1 context.setDoc(document);
202   
203    // Run the action
204  1 String result = action.render(context);
205   
206    // The tests are below this line!
207   
208    // Verify null is returned (this means the response has been returned)
209  1 assertNull(result);
210   
211    // Note: The title is not "WebHome", but "X" (the space's name) to avoid exposing "WebHome" in the UI.
212  1 verify(mockURLFactory).createURL("X", "WebHome", "edit", "template=&parent=Main.WebHome&title=X", null, "xwiki",
213    context);
214    }
215   
 
216  1 toggle @Test
217    public void newDocumentWebHomeFromURL() throws Exception
218    {
219    // new document = xwiki:X.Y.WebHome
220  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X", "Y"), "WebHome");
221  1 XWikiDocument document = mock(XWikiDocument.class);
222  1 when(document.getDocumentReference()).thenReturn(documentReference);
223  1 when(document.isNew()).thenReturn(true);
224   
225  1 context.setDoc(document);
226   
227    // Run the action
228  1 String result = action.render(context);
229   
230    // The tests are below this line!
231   
232    // Verify null is returned (this means the response has been returned)
233  1 assertNull(result);
234   
235    // Note1: The bebavior is the same for both a top level space and a child space WebHome.
236    // Note2: The title is not "WebHome", but "Y" (the space's name) to avoid exposing "WebHome" in the UI.
237  1 verify(mockURLFactory).createURL("X.Y", "WebHome", "edit", "template=&parent=Main.WebHome&title=Y", null,
238    "xwiki", context);
239    }
240   
 
241  1 toggle @Test
242    public void newDocumentWebHomeButTerminalFromURL() throws Exception
243    {
244    // new document = xwiki:X.Y.WebHome
245  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X", "Y"), "WebHome");
246  1 XWikiDocument document = mock(XWikiDocument.class);
247  1 when(document.getDocumentReference()).thenReturn(documentReference);
248  1 when(document.isNew()).thenReturn(true);
249   
250  1 context.setDoc(document);
251   
252    // Pass the tocreate=terminal request parameter
253  1 when(mockRequest.getParameter("tocreate")).thenReturn("terminal");
254   
255    // Run the action
256  1 String result = action.render(context);
257   
258    // The tests are below this line!
259   
260    // Verify null is returned (this means the response has been returned)
261  1 assertNull(result);
262   
263    // Note: We are creating X.Y instead of X.Y.WebHome because the tocreate parameter says "terminal".
264  1 verify(mockURLFactory).createURL("X", "Y", "edit", "template=&parent=Main.WebHome&title=Y", null, "xwiki",
265    context);
266    }
267   
 
268  1 toggle @Test
269    public void newDocumentWebHomeTopLevelSpaceButTerminalFromURL() throws Exception
270    {
271    // new document = xwiki:X.WebHome
272  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X"), "WebHome");
273  1 XWikiDocument document = mock(XWikiDocument.class);
274  1 when(document.getDocumentReference()).thenReturn(documentReference);
275  1 when(document.isNew()).thenReturn(true);
276   
277  1 context.setDoc(document);
278   
279    // Pass the tocreate=terminal request parameter
280  1 when(mockRequest.getParameter("tocreate")).thenReturn("terminal");
281   
282    // Run the action
283  1 String result = action.render(context);
284   
285    // The tests are below this line!
286   
287    // Verify that the create template is rendered, so the UI is displayed for the user to enter the missing values.
288  1 assertEquals("create", result);
289   
290    // Note: We can not create the "X" terminal document, since it is already at the top level of the hierarchy and
291    // none was able to be deducted from the given information. The user needs to specify more info in order to
292    // continue.
293    // We should not get this far so no redirect should be done, just the template will be rendered.
294  1 verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
295    any(), any(XWikiContext.class));
296    }
297   
 
298  1 toggle @Test
299    public void existingDocumentFromUINoName() throws Exception
300    {
301    // current document = xwiki:Main.WebHome
302  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
303  1 XWikiDocument document = mock(XWikiDocument.class);
304  1 when(document.getDocumentReference()).thenReturn(documentReference);
305  1 when(document.isNew()).thenReturn(false);
306  1 context.setDoc(document);
307   
308    // Just landed on the create page or submitted with no values (no name) specified.
309   
310    // Run the action
311  1 String result = action.render(context);
312   
313    // The tests are below this line!
314   
315    // Verify that the create template is rendered, so the UI is displayed for the user to enter the missing values.
316  1 assertEquals("create", result);
317   
318    // We should not get this far so no redirect should be done, just the template will be rendered.
319  1 verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
320    any(), any(XWikiContext.class));
321    }
322   
 
323  1 toggle @Test
324    public void existingDocumentFromUI() throws Exception
325    {
326    // current document = xwiki:Main.WebHome
327  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
328  1 XWikiDocument document = mock(XWikiDocument.class);
329  1 when(document.getDocumentReference()).thenReturn(documentReference);
330  1 when(document.isNew()).thenReturn(false);
331  1 context.setDoc(document);
332   
333    // Submit from the UI spaceReference=X&name=Y
334  1 when(mockRequest.getParameter("spaceReference")).thenReturn("X");
335  1 when(mockRequest.getParameter("name")).thenReturn("Y");
336   
337    // Run the action
338  1 String result = action.render(context);
339   
340    // The tests are below this line!
341   
342    // Verify null is returned (this means the response has been returned)
343  1 assertNull(result);
344   
345    // Note: We are creating X.Y.WebHome since we default to non-terminal documents.
346  1 verify(mockURLFactory).createURL("X.Y", "WebHome", "edit", "template=&parent=Main.WebHome&title=Y", null,
347    "xwiki", context);
348    }
349   
 
350  1 toggle @Test
351    public void existingDocumentFromUICheckEscaping() throws Exception
352    {
353    // current document = xwiki:Main.WebHome
354  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
355  1 XWikiDocument document = mock(XWikiDocument.class);
356  1 when(document.getDocumentReference()).thenReturn(documentReference);
357  1 when(document.isNew()).thenReturn(false);
358  1 context.setDoc(document);
359   
360    // Submit from the UI spaceReference=X.Y&name=Z
361  1 when(mockRequest.getParameter("spaceReference")).thenReturn("X.Y");
362  1 when(mockRequest.getParameter("name")).thenReturn("Z");
363   
364    // Run the action
365  1 String result = action.render(context);
366   
367    // The tests are below this line!
368   
369    // Verify null is returned (this means the response has been returned)
370  1 assertNull(result);
371   
372    // Note: We are creating X.Y.Z.WebHome since we default to non-terminal documents.
373  1 verify(mockURLFactory).createURL("X.Y.Z", "WebHome", "edit", "template=&parent=Main.WebHome&title=Z", null,
374    "xwiki", context);
375    }
376   
 
377  1 toggle @Test
378    public void existingDocumentTerminalFromUI() throws Exception
379    {
380    // current document = xwiki:Main.WebHome
381  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
382  1 XWikiDocument document = mock(XWikiDocument.class);
383  1 when(document.getDocumentReference()).thenReturn(documentReference);
384  1 when(document.isNew()).thenReturn(false);
385  1 context.setDoc(document);
386   
387    // Submit from the UI spaceReference=X&name=Y&tocreate=terminal
388  1 when(mockRequest.getParameter("spaceReference")).thenReturn("X");
389  1 when(mockRequest.getParameter("name")).thenReturn("Y");
390  1 when(mockRequest.getParameter("tocreate")).thenReturn("terminal");
391   
392    // Run the action
393  1 String result = action.render(context);
394   
395    // The tests are below this line!
396   
397    // Verify null is returned (this means the response has been returned)
398  1 assertNull(result);
399   
400    // Note: We are creating X.Y instead of X.Y.WebHome because the tocreate parameter says "terminal".
401  1 verify(mockURLFactory).createURL("X", "Y", "edit", "template=&parent=Main.WebHome&title=Y", null, "xwiki",
402    context);
403    }
404   
 
405  1 toggle @Test
406    public void existingDocumentTerminalFromUICheckEscaping() throws Exception
407    {
408    // current document = xwiki:Main.WebHome
409  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
410  1 XWikiDocument document = mock(XWikiDocument.class);
411  1 when(document.getDocumentReference()).thenReturn(documentReference);
412  1 when(document.isNew()).thenReturn(false);
413  1 context.setDoc(document);
414   
415    // Submit from the UI spaceReference=X.Y&name=Z&tocreate=termina
416  1 when(mockRequest.getParameter("spaceReference")).thenReturn("X.Y");
417  1 when(mockRequest.getParameter("name")).thenReturn("Z");
418  1 when(mockRequest.getParameter("tocreate")).thenReturn("terminal");
419   
420    // Run the action
421  1 String result = action.render(context);
422   
423    // The tests are below this line!
424   
425    // Verify null is returned (this means the response has been returned)
426  1 assertNull(result);
427   
428    // Note: We are creating X.Y.Z instead of X.Y.Z.WebHome because the tocreate parameter says "terminal".
429  1 verify(mockURLFactory).createURL("X.Y", "Z", "edit", "template=&parent=Main.WebHome&title=Z", null, "xwiki",
430    context);
431    }
432   
 
433  1 toggle @Test
434    public void existingDocumentTerminalFromUIButAlreadyExisting() throws Exception
435    {
436    // current document = xwiki:Main.WebHome
437  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
438  1 XWikiDocument document = mock(XWikiDocument.class);
439  1 when(document.getDocumentReference()).thenReturn(documentReference);
440  1 when(document.isNew()).thenReturn(false);
441  1 context.setDoc(document);
442    // Mock it as existing in the DB as well with non-empty content
443  1 oldcore.getDocuments().put(new DocumentReference(documentReference, Locale.ROOT), document);
444  1 when(document.getContent()).thenReturn("Some non-empty content");
445   
446    // Submit from the UI spaceReference=X&name=Y&tocreate=terminal
447    // No diference if it was a non-terminal document, just easier to mock since we already have Main.WebHome set
448    // up.
449  1 when(mockRequest.getParameter("spaceReference")).thenReturn("Main");
450  1 when(mockRequest.getParameter("name")).thenReturn("WebHome");
451  1 when(mockRequest.getParameter("tocreate")).thenReturn("terminal");
452   
453    // Run the action
454  1 String result = action.render(context);
455   
456    // The tests are below this line!
457   
458    // Verify that the create template is rendered, so the UI is displayed for the user to see the error.
459  1 assertEquals("create", result);
460   
461    // Check that the exception is properly set in the context for the UI to display.
462  1 XWikiException exception = (XWikiException) this.oldcore.getScriptContext().getAttribute("createException");
463  1 assertNotNull(exception);
464  1 assertEquals(XWikiException.ERROR_XWIKI_APP_DOCUMENT_NOT_EMPTY, exception.getCode());
465   
466    // We should not get this far so no redirect should be done, just the template will be rendered.
467  1 verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
468    any(), any(XWikiContext.class));
469    }
470   
 
471  1 toggle @Test
472    public void existingDocumentFromUITopLevelDocument() throws Exception
473    {
474    // current document = xwiki:Main.WebHome
475  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
476  1 XWikiDocument document = mock(XWikiDocument.class);
477  1 when(document.getDocumentReference()).thenReturn(documentReference);
478  1 when(document.isNew()).thenReturn(false);
479  1 context.setDoc(document);
480   
481    // Submit from the UI name=Y
482  1 when(mockRequest.getParameter("name")).thenReturn("Y");
483   
484    // Run the action
485  1 String result = action.render(context);
486   
487    // The tests are below this line!
488   
489    // Verify null is returned (this means the response has been returned)
490  1 assertNull(result);
491   
492    // Note: We are creating X.Y.WebHome since we default to non-terminal documents.
493  1 verify(mockURLFactory).createURL("Y", "WebHome", "edit", "template=&parent=Main.WebHome&title=Y", null, "xwiki",
494    context);
495    }
496   
497    /*
498    * Deprecated parameters
499    */
500   
 
501  1 toggle @Test
502    public void existingDocumentFromUIDeprecated() throws Exception
503    {
504    // current document = xwiki:Main.WebHome
505  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
506  1 XWikiDocument document = mock(XWikiDocument.class);
507  1 when(document.getDocumentReference()).thenReturn(documentReference);
508  1 when(document.isNew()).thenReturn(false);
509  1 context.setDoc(document);
510   
511    // Submit from the UI space=X&page=Y
512  1 when(mockRequest.getParameter("space")).thenReturn("X");
513  1 when(mockRequest.getParameter("page")).thenReturn("Y");
514   
515    // Run the action
516  1 String result = action.render(context);
517   
518    // The tests are below this line!
519   
520    // Verify null is returned (this means the response has been returned)
521  1 assertNull(result);
522   
523    // Note: We are creating X.Y since the deprecated parameters were creating terminal documents by default.
524  1 verify(mockURLFactory).createURL("X", "Y", "edit", "template=&parent=Main.WebHome&title=Y", null, "xwiki",
525    context);
526    }
527   
 
528  1 toggle @Test
529    public void existingDocumentFromUIDeprecatedCheckEscaping() throws Exception
530    {
531    // current document = xwiki:Main.WebHome
532  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
533  1 XWikiDocument document = mock(XWikiDocument.class);
534  1 when(document.getDocumentReference()).thenReturn(documentReference);
535  1 when(document.isNew()).thenReturn(false);
536  1 context.setDoc(document);
537   
538    // Submit from the UI space=X.Y&page=Z
539  1 when(mockRequest.getParameter("space")).thenReturn("X.Y");
540  1 when(mockRequest.getParameter("page")).thenReturn("Z");
541   
542    // Run the action
543  1 String result = action.render(context);
544   
545    // The tests are below this line!
546   
547    // Verify null is returned (this means the response has been returned)
548  1 assertNull(result);
549   
550    // Note1: The space parameter was previously considered as space name, not space reference, so it is escaped.
551    // Note2: We are creating X\.Y.Z since the deprecated parameters were creating terminal documents by default.
552  1 verify(mockURLFactory).createURL("X\\.Y", "Z", "edit", "template=&parent=Main.WebHome&title=Z", null, "xwiki",
553    context);
554    }
555   
 
556  1 toggle @Test
557    public void existingDocumentNonTerminalFromUIDeprecated() throws Exception
558    {
559    // current document = xwiki:Main.WebHome
560  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
561  1 XWikiDocument document = mock(XWikiDocument.class);
562  1 when(document.getDocumentReference()).thenReturn(documentReference);
563  1 when(document.isNew()).thenReturn(false);
564  1 context.setDoc(document);
565   
566    // Submit from the UI space=X&tocreate=space
567  1 when(mockRequest.getParameter("space")).thenReturn("X");
568  1 when(mockRequest.getParameter("tocreate")).thenReturn("space");
569   
570    // Run the action
571  1 String result = action.render(context);
572   
573    // The tests are below this line!
574   
575    // Verify null is returned (this means the response has been returned)
576  1 assertNull(result);
577   
578    // Note: We are creating X.WebHome because the tocreate parameter says "space".
579  1 verify(mockURLFactory).createURL("X", "WebHome", "edit", "template=&parent=Main.WebHome&title=X", null, "xwiki",
580    context);
581    }
582   
 
583  1 toggle @Test
584    public void existingDocumentNonTerminalFromUIDeprecatedIgnoringPage() throws Exception
585    {
586    // current document = xwiki:Main.WebHome
587  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
588  1 XWikiDocument document = mock(XWikiDocument.class);
589  1 when(document.getDocumentReference()).thenReturn(documentReference);
590  1 when(document.isNew()).thenReturn(false);
591  1 context.setDoc(document);
592   
593    // Submit from the UI space=X&page=Y&tocreate=space
594  1 when(mockRequest.getParameter("space")).thenReturn("X");
595  1 when(mockRequest.getParameter("page")).thenReturn("Y");
596  1 when(mockRequest.getParameter("tocreate")).thenReturn("space");
597   
598    // Run the action
599  1 String result = action.render(context);
600   
601    // The tests are below this line!
602   
603    // Verify null is returned (this means the response has been returned)
604  1 assertNull(result);
605   
606    // Note: We are creating X.WebHome instead of X.Y because the tocreate parameter says "space" and the page
607    // parameter is ignored.
608  1 verify(mockURLFactory).createURL("X", "WebHome", "edit", "template=&parent=Main.WebHome&title=X", null, "xwiki",
609    context);
610    }
611   
 
612  1 toggle @Test
613    public void existingDocumentNonTerminalFromUIDeprecatedCheckEscaping() throws Exception
614    {
615    // current document = xwiki:Main.WebHome
616  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
617  1 XWikiDocument document = mock(XWikiDocument.class);
618  1 when(document.getDocumentReference()).thenReturn(documentReference);
619  1 when(document.isNew()).thenReturn(false);
620  1 context.setDoc(document);
621   
622    // Submit from the UI space=X.Y&tocreate=space
623  1 when(mockRequest.getParameter("space")).thenReturn("X.Y");
624  1 when(mockRequest.getParameter("tocreate")).thenReturn("space");
625   
626    // Run the action
627  1 String result = action.render(context);
628   
629    // The tests are below this line!
630   
631    // Verify null is returned (this means the response has been returned)
632  1 assertNull(result);
633   
634    // Note1: The space parameter was previously considered as space name, not space reference, so it is escaped.
635    // Note2: We are creating X\.Y.WebHome because the tocreate parameter says "space".
636  1 verify(mockURLFactory).createURL("X\\.Y", "WebHome", "edit", "template=&parent=Main.WebHome&title=X.Y", null,
637    "xwiki", context);
638    }
639   
640    /*
641    * Template providers
642    */
643   
 
644  1 toggle @Test
645    public void existingDocumentFromUITemplateProviderExistingButNoneSelected() throws Exception
646    {
647    // current document = xwiki:Main.WebHome
648  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
649  1 XWikiDocument document = mock(XWikiDocument.class);
650  1 when(document.getDocumentReference()).thenReturn(documentReference);
651  1 when(document.isNew()).thenReturn(false);
652  1 context.setDoc(document);
653   
654    // Submit from the UI spaceReference=X&name=Y
655  1 when(mockRequest.getParameter("spaceReference")).thenReturn("X");
656  1 when(mockRequest.getParameter("name")).thenReturn("Y");
657   
658    // Mock 1 existing template provider
659  1 mockExistingTemplateProviders("XWiki.MyTemplateProvider",
660    new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST);
661   
662    // Run the action
663  1 String result = action.render(context);
664   
665    // The tests are below this line!
666   
667    // Verify that the create template is rendered, so the UI is displayed for the user to enter the missing values.
668  1 assertEquals("create", result);
669   
670    // We should not get this far so no redirect should be done, just the template will be rendered.
671  1 verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
672    any(), any(XWikiContext.class));
673    }
674   
675    /**
676    * Mocks 1 existing template provider.
677    * <p>
678    * Note: Calling it multiple times does not add multiple providers.
679    */
 
680  7 toggle private void mockExistingTemplateProviders(String fullName, DocumentReference resolvedDocumentReference,
681    List<String> allowedSpaces) throws Exception
682    {
683  7 mockExistingTemplateProviders(fullName, resolvedDocumentReference, allowedSpaces, false);
684    }
685   
686    /**
687    * Mocks 1 existing template provider.
688    * <p>
689    * Note: Calling it multiple times does not add multiple providers.
690    */
 
691  15 toggle private void mockExistingTemplateProviders(String fullName, DocumentReference resolvedDocumentReference,
692    List<String> allowedSpaces, Boolean terminal) throws Exception
693    {
694  15 mockExistingTemplateProviders(fullName, resolvedDocumentReference, allowedSpaces, terminal, null);
695    }
696   
697    /**
698    * Mocks 1 existing template provider.
699    * <p>
700    * Note: Calling it multiple times does not add multiple providers.
701    */
 
702  19 toggle private void mockExistingTemplateProviders(String fullName, DocumentReference resolvedDocumentReference,
703    List<String> allowedSpaces, Boolean terminal, String type) throws Exception
704    {
705  19 DocumentReference templateProviderClassReference =
706    new DocumentReference("xwiki", Arrays.asList("XWiki"), "TemplateProviderClass");
707   
708    // Mock to return at least 1 existing template provider
709  19 when(mockTemplateProvidersQuery.execute()).thenReturn(new ArrayList<Object>(Arrays.asList(fullName)));
710   
711    // Mock the template document as existing.
712  19 XWikiDocument templateProviderDocument = mock(XWikiDocument.class);
713  19 when(templateProviderDocument.getDocumentReference()).thenReturn(resolvedDocumentReference);
714  19 oldcore.getDocuments().put(new DocumentReference(resolvedDocumentReference, Locale.ROOT),
715    templateProviderDocument);
716    // Mock the provider object (template + spaces properties)
717  19 BaseObject templateProviderObject = mock(BaseObject.class);
718  19 when(templateProviderObject.getListValue("creationRestrictions")).thenReturn(allowedSpaces);
719  19 String templateDocumentFullName = fullName.substring(0, fullName.indexOf("Provider"));
720  19 when(templateProviderObject.getStringValue("template")).thenReturn(templateDocumentFullName);
721  19 if (terminal != null) {
722  15 when(templateProviderObject.getIntValue("terminal", -1)).thenReturn(terminal ? 1 : 0);
723    } else {
724  4 when(templateProviderObject.getIntValue("terminal", -1)).thenReturn(-1);
725    }
726  19 if (type != null) {
727  4 when(templateProviderObject.getStringValue("type")).thenReturn(type);
728    }
729  19 when(templateProviderDocument.getXObject(templateProviderClassReference)).thenReturn(templateProviderObject);
730   
731    // Mock the template document as existing
732  19 String templateDocumentName =
733    resolvedDocumentReference.getName().substring(0, resolvedDocumentReference.getName().indexOf("Provider"));
734  19 DocumentReference templateDocumentReference =
735    new DocumentReference(templateDocumentName, new SpaceReference(resolvedDocumentReference.getParent()));
736  19 mockTemplateDocumentExisting(templateDocumentFullName, templateDocumentReference);
737    }
738   
739    /**
740    * @param templateDocumentFullName
741    * @param templateDocumentReference
742    * @throws XWikiException
743    */
 
744  20 toggle private void mockTemplateDocumentExisting(String templateDocumentFullName,
745    DocumentReference templateDocumentReference) throws XWikiException
746    {
747  20 XWikiDocument templateDocument = mock(XWikiDocument.class);
748  20 when(templateDocument.getDocumentReference()).thenReturn(templateDocumentReference);
749  20 when(templateDocument.getDefaultEditMode(context)).thenReturn("edit");
750  20 oldcore.getDocuments().put(new DocumentReference(templateDocumentReference, Locale.ROOT), templateDocument);
751    }
752   
 
753  1 toggle @Test
754    public void existingDocumentFromUITemplateProviderSpecified() throws Exception
755    {
756    // current document = xwiki:Main.WebHome
757  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
758  1 XWikiDocument document = mock(XWikiDocument.class);
759  1 when(document.getDocumentReference()).thenReturn(documentReference);
760  1 when(document.isNew()).thenReturn(false);
761  1 context.setDoc(document);
762   
763    // Submit from the UI spaceReference=X&name=Y&templateProvider=XWiki.MyTemplateProvider
764  1 String templateProviderFullName = "XWiki.MyTemplateProvider";
765  1 when(mockRequest.getParameter("spaceReference")).thenReturn("X");
766  1 when(mockRequest.getParameter("name")).thenReturn("Y");
767  1 when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
768   
769    // Mock 1 existing template provider
770  1 mockExistingTemplateProviders(templateProviderFullName,
771    new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST);
772   
773    // Run the action
774  1 String result = action.render(context);
775   
776    // The tests are below this line!
777   
778    // Verify null is returned (this means the response has been returned)
779  1 assertNull(result);
780   
781    // Note: We are creating X.Y and using the template extracted from the template provider.
782  1 verify(mockURLFactory).createURL("X.Y", "WebHome", "edit",
783    "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y", null, "xwiki", context);
784    }
785   
 
786  1 toggle @Test
787    public void existingDocumentFromUITemplateProviderSpecifiedRestrictionExists() throws Exception
788    {
789    // current document = xwiki:Main.WebHome
790  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
791  1 XWikiDocument document = mock(XWikiDocument.class);
792  1 when(document.getDocumentReference()).thenReturn(documentReference);
793  1 when(document.isNew()).thenReturn(false);
794  1 context.setDoc(document);
795   
796    // Submit from the UI spaceReference=X&name=Y&templateProvider=XWiki.MyTemplateProvider
797  1 String templateProviderFullName = "XWiki.MyTemplateProvider";
798  1 String spaceReferenceString = "X";
799  1 when(mockRequest.getParameter("spaceReference")).thenReturn(spaceReferenceString);
800  1 when(mockRequest.getParameter("name")).thenReturn("Y");
801  1 when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
802   
803    // Mock 1 existing template provider that allows usage in target space.
804  1 mockExistingTemplateProviders(templateProviderFullName,
805    new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Arrays.asList("X"));
806   
807    // Run the action
808  1 String result = action.render(context);
809   
810    // The tests are below this line!
811   
812    // Verify null is returned (this means the response has been returned)
813  1 assertNull(result);
814   
815    // Note1: We are allowed to create anything under space X, be it a terminal or a non-terminal document.
816    // Note2: We are creating X.Y and using the template extracted from the template provider.
817  1 verify(mockURLFactory).createURL("X.Y", "WebHome", "edit",
818    "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y", null, "xwiki", context);
819    }
820   
 
821  1 toggle @Test
822    public void existingDocumentFromUITemplateProviderSpecifiedRestrictionExistsOnParentSpace() throws Exception
823    {
824    // current document = xwiki:Main.WebHome
825  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
826  1 XWikiDocument document = mock(XWikiDocument.class);
827  1 when(document.getDocumentReference()).thenReturn(documentReference);
828  1 when(document.isNew()).thenReturn(false);
829  1 context.setDoc(document);
830   
831    // Submit from the UI spaceReference=X.Y.Z&name=W&templateProvider=XWiki.MyTemplateProvider
832  1 String templateProviderFullName = "XWiki.MyTemplateProvider";
833  1 String spaceReferenceString = "X.Y.Z";
834  1 when(mockRequest.getParameter("spaceReference")).thenReturn(spaceReferenceString);
835  1 when(mockRequest.getParameter("name")).thenReturn("W");
836  1 when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
837   
838    // Mock 1 existing template provider that allows usage in one of the target space's parents (top level in this
839    // case).
840  1 mockExistingTemplateProviders(templateProviderFullName,
841    new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Arrays.asList("X"));
842   
843    // Run the action
844  1 String result = action.render(context);
845   
846    // The tests are below this line!
847   
848    // Verify null is returned (this means the response has been returned)
849  1 assertNull(result);
850   
851    // Note1: We are allowed to create anything under space X or its children, be it a terminal or a non-terminal
852    // document
853    // Note2: We are creating X.Y.Z.W and using the template extracted from the template provider.
854  1 verify(mockURLFactory).createURL("X.Y.Z.W", "WebHome", "edit",
855    "template=XWiki.MyTemplate&parent=Main.WebHome&title=W", null, "xwiki", context);
856    }
857   
 
858  1 toggle @Test
859    public void existingDocumentFromUITemplateProviderSpecifiedButNotAllowed() throws Exception
860    {
861    // current document = xwiki:Main.WebHome
862  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
863  1 XWikiDocument document = mock(XWikiDocument.class);
864  1 when(document.getDocumentReference()).thenReturn(documentReference);
865  1 when(document.isNew()).thenReturn(false);
866  1 context.setDoc(document);
867   
868    // Submit from the UI spaceReference=X&name=Y&templateProvider=XWiki.MyTemplateProvider
869  1 String templateProviderFullName = "XWiki.MyTemplateProvider";
870  1 when(mockRequest.getParameter("spaceReference")).thenReturn("X");
871  1 when(mockRequest.getParameter("name")).thenReturn("Y");
872  1 when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
873   
874    // Mock 1 existing template provider
875  1 mockExistingTemplateProviders(templateProviderFullName,
876    new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"),
877    Arrays.asList("AnythingButX"));
878   
879    // Run the action
880  1 String result = action.render(context);
881   
882    // The tests are below this line!
883   
884    // Verify that the create template is rendered, so the UI is displayed for the user to see the error.
885  1 assertEquals("create", result);
886   
887    // Check that the exception is properly set in the context for the UI to display.
888  1 XWikiException exception = (XWikiException) this.oldcore.getScriptContext().getAttribute("createException");
889  1 assertNotNull(exception);
890  1 assertEquals(XWikiException.ERROR_XWIKI_APP_TEMPLATE_NOT_AVAILABLE, exception.getCode());
891   
892    // We should not get this far so no redirect should be done, just the template will be rendered.
893  1 verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
894    any(), any(XWikiContext.class));
895    }
896   
 
897  1 toggle @Test
898    public void newDocumentFromURLTemplateProviderSpecifiedButNotAllowed() throws Exception
899    {
900    // new document = xwiki:X.Y
901  1 DocumentReference documentReference =
902    new DocumentReference("Y", new SpaceReference("X", new WikiReference("xwiki")));
903  1 XWikiDocument document = mock(XWikiDocument.class);
904  1 when(document.getDocumentReference()).thenReturn(documentReference);
905  1 when(document.isNew()).thenReturn(true);
906   
907  1 context.setDoc(document);
908   
909    // Specifying a template provider in the URL: templateprovider=XWiki.MyTemplateProvider
910  1 String templateProviderFullName = "XWiki.MyTemplateProvider";
911  1 when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
912   
913    // Mock 1 existing template provider
914  1 mockExistingTemplateProviders(templateProviderFullName,
915    new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"),
916    Arrays.asList("AnythingButX"));
917   
918    // Run the action
919  1 String result = action.render(context);
920   
921    // The tests are below this line!
922   
923    // Verify that the create template is rendered, so the UI is displayed for the user to see the error.
924  1 assertEquals("create", result);
925   
926    // Check that the exception is properly set in the context for the UI to display.
927  1 XWikiException exception = (XWikiException) this.oldcore.getScriptContext().getAttribute("createException");
928  1 assertNotNull(exception);
929  1 assertEquals(XWikiException.ERROR_XWIKI_APP_TEMPLATE_NOT_AVAILABLE, exception.getCode());
930   
931    // We should not get this far so no redirect should be done, just the template will be rendered.
932  1 verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
933    any(), any(XWikiContext.class));
934    }
935   
 
936  1 toggle @Test
937    public void newDocumentWebHomeFromURLTemplateProviderSpecifiedButNotAllowed() throws Exception
938    {
939    // new document = xwiki:X.Y.WebHome
940  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X", "Y"), "WebHome");
941  1 XWikiDocument document = mock(XWikiDocument.class);
942  1 when(document.getDocumentReference()).thenReturn(documentReference);
943  1 when(document.isNew()).thenReturn(true);
944   
945  1 context.setDoc(document);
946   
947    // Specifying a template provider in the URL: templateprovider=XWiki.MyTemplateProvider
948  1 String templateProviderFullName = "XWiki.MyTemplateProvider";
949  1 when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
950   
951    // Mock 1 existing template provider
952  1 mockExistingTemplateProviders(templateProviderFullName,
953    new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"),
954    Arrays.asList("AnythingButX"));
955   
956    // Run the action
957  1 String result = action.render(context);
958   
959    // The tests are below this line!
960   
961    // Verify that the create template is rendered, so the UI is displayed for the user to see the error.
962  1 assertEquals("create", result);
963   
964    // Check that the exception is properly set in the context for the UI to display.
965  1 XWikiException exception = (XWikiException) this.oldcore.getScriptContext().getAttribute("createException");
966  1 assertNotNull(exception);
967  1 assertEquals(XWikiException.ERROR_XWIKI_APP_TEMPLATE_NOT_AVAILABLE, exception.getCode());
968   
969    // We should not get this far so no redirect should be done, just the template will be rendered.
970  1 verify(mockURLFactory, never()).createURL(any(), any(), any(), any(), any(),
971    any(), any(XWikiContext.class));
972    }
973   
 
974  1 toggle @Test
975    public void newDocumentWebHomeFromURLTemplateProviderSpecifiedTerminal() throws Exception
976    {
977    // new document = xwiki:X.Y.WebHome
978  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X", "Y"), "WebHome");
979  1 XWikiDocument document = mock(XWikiDocument.class);
980  1 when(document.getDocumentReference()).thenReturn(documentReference);
981  1 when(document.isNew()).thenReturn(true);
982   
983  1 context.setDoc(document);
984   
985    // Specifying a template provider in the URL: templateprovider=XWiki.MyTemplateProvider
986  1 String templateProviderFullName = "XWiki.MyTemplateProvider";
987  1 when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
988   
989    // Mock 1 existing template provider
990  1 mockExistingTemplateProviders(templateProviderFullName,
991    new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, true);
992   
993    // Run the action
994  1 String result = action.render(context);
995   
996    // The tests are below this line!
997   
998    // Verify null is returned (this means the response has been returned)
999  1 assertNull(result);
1000   
1001    // Note: We are creating the document X.Y as terminal and using a template, as specified in the template
1002    // provider.
1003  1 verify(mockURLFactory).createURL("X", "Y", "edit", "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y",
1004    null, "xwiki", context);
1005    }
1006   
 
1007  1 toggle @Test
1008    public void newDocumentWebHomeFromURLTemplateProviderSpecifiedTerminalOverriddenFromUIToNonTerminal()
1009    throws Exception
1010    {
1011    // new document = xwiki:X.Y.WebHome
1012  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X", "Y"), "WebHome");
1013  1 XWikiDocument document = mock(XWikiDocument.class);
1014  1 when(document.getDocumentReference()).thenReturn(documentReference);
1015  1 when(document.isNew()).thenReturn(true);
1016   
1017  1 context.setDoc(document);
1018   
1019    // Specifying a template provider in the URL: templateprovider=XWiki.MyTemplateProvider
1020  1 String templateProviderFullName = "XWiki.MyTemplateProvider";
1021  1 when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
1022  1 when(mockRequest.getParameter("tocreate")).thenReturn("nonterminal");
1023   
1024    // Mock 1 existing template provider
1025  1 mockExistingTemplateProviders(templateProviderFullName,
1026    new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, true);
1027   
1028    // Run the action
1029  1 String result = action.render(context);
1030   
1031    // The tests are below this line!
1032   
1033    // Verify null is returned (this means the response has been returned)
1034  1 assertNull(result);
1035   
1036    // Note: We are creating the document X.Y.WebHome as non-terminal even if the template provider says otherwise.
1037    // Also using a template, as specified in the template provider.
1038  1 verify(mockURLFactory).createURL("X.Y", "WebHome", "edit",
1039    "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y", null, "xwiki", context);
1040    }
1041   
 
1042  1 toggle @Test
1043    public void newDocumentFromURLTemplateProviderSpecifiedNonTerminal() throws Exception
1044    {
1045    // new document = xwiki:X.Y
1046  1 DocumentReference documentReference = new DocumentReference("xwiki", "X", "Y");
1047  1 XWikiDocument document = mock(XWikiDocument.class);
1048  1 when(document.getDocumentReference()).thenReturn(documentReference);
1049  1 when(document.isNew()).thenReturn(true);
1050   
1051  1 context.setDoc(document);
1052   
1053    // Specifying a template provider in the URL: templateprovider=XWiki.MyTemplateProvider
1054  1 String templateProviderFullName = "XWiki.MyTemplateProvider";
1055  1 when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
1056   
1057    // Mock 1 existing template provider
1058  1 mockExistingTemplateProviders(templateProviderFullName,
1059    new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST,
1060    false);
1061   
1062    // Run the action
1063  1 String result = action.render(context);
1064   
1065    // The tests are below this line!
1066   
1067    // Verify null is returned (this means the response has been returned)
1068  1 assertNull(result);
1069   
1070    // Note: We are creating the document X.Y as terminal and using a template, as specified in the template
1071    // provider.
1072  1 verify(mockURLFactory).createURL("X.Y", "WebHome", "edit",
1073    "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y", null, "xwiki", context);
1074    }
1075   
 
1076  1 toggle @Test
1077    public void newDocumentFromURLTemplateProviderSpecifiedNonTerminalButOverriddenFromUITerminal() throws Exception
1078    {
1079    // new document = xwiki:X.Y
1080  1 DocumentReference documentReference = new DocumentReference("xwiki", "X", "Y");
1081  1 XWikiDocument document = mock(XWikiDocument.class);
1082  1 when(document.getDocumentReference()).thenReturn(documentReference);
1083  1 when(document.isNew()).thenReturn(true);
1084   
1085  1 context.setDoc(document);
1086   
1087    // Specifying a template provider in the URL: templateprovider=XWiki.MyTemplateProvider
1088  1 String templateProviderFullName = "XWiki.MyTemplateProvider";
1089  1 when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
1090  1 when(mockRequest.getParameter("tocreate")).thenReturn("terminal");
1091   
1092    // Mock 1 existing template provider
1093  1 mockExistingTemplateProviders(templateProviderFullName,
1094    new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST,
1095    false);
1096   
1097    // Run the action
1098  1 String result = action.render(context);
1099   
1100    // The tests are below this line!
1101   
1102    // Verify null is returned (this means the response has been returned)
1103  1 assertNull(result);
1104   
1105    // Note: We are creating the document X.Y as terminal and using a template, as specified in the template
1106    // provider.
1107  1 verify(mockURLFactory).createURL("X", "Y", "edit", "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y",
1108    null, "xwiki", context);
1109    }
1110   
 
1111  1 toggle @Test
1112    public void existingDocumentFromUITemplateSpecified() throws Exception
1113    {
1114    // current document = xwiki:Main.WebHome
1115  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
1116  1 XWikiDocument document = mock(XWikiDocument.class);
1117  1 when(document.getDocumentReference()).thenReturn(documentReference);
1118  1 when(document.isNew()).thenReturn(false);
1119   
1120  1 context.setDoc(document);
1121   
1122    // Submit from the UI spaceReference=X&name=Y&template=XWiki.MyTemplate
1123  1 String templateDocumentFullName = "XWiki.MyTemplate";
1124  1 DocumentReference templateDocumentReference =
1125    new DocumentReference("MyTemplate", Arrays.asList("XWiki"), "xwiki");
1126  1 when(mockRequest.getParameter("spaceReference")).thenReturn("X");
1127  1 when(mockRequest.getParameter("name")).thenReturn("Y");
1128  1 when(mockRequest.getParameter("template")).thenReturn("XWiki.MyTemplate");
1129   
1130    // Mock the passed template document as existing.
1131  1 mockTemplateDocumentExisting(templateDocumentFullName, templateDocumentReference);
1132   
1133    // Run the action
1134  1 String result = action.render(context);
1135   
1136    // The tests are below this line!
1137   
1138    // Verify null is returned (this means the response has been returned)
1139  1 assertNull(result);
1140   
1141    // Note: We are creating X.Y.WebHome and using the template specified in the request.
1142  1 verify(mockURLFactory).createURL("X.Y", "WebHome", "edit",
1143    "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y", null, "xwiki", context);
1144    }
1145   
 
1146  1 toggle @Test
1147    public void existingDocumentFromUITemplateProviderSpecifiedTerminal() throws Exception
1148    {
1149    // current document = xwiki:Main.WebHome
1150  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
1151  1 XWikiDocument document = mock(XWikiDocument.class);
1152  1 when(document.getDocumentReference()).thenReturn(documentReference);
1153  1 when(document.isNew()).thenReturn(false);
1154  1 context.setDoc(document);
1155   
1156    // Submit from the UI spaceReference=X&name=Y&templateProvider=XWiki.MyTemplateProvider
1157  1 String templateProviderFullName = "XWiki.MyTemplateProvider";
1158  1 String spaceReferenceString = "X";
1159  1 when(mockRequest.getParameter("spaceReference")).thenReturn(spaceReferenceString);
1160  1 when(mockRequest.getParameter("name")).thenReturn("Y");
1161  1 when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
1162   
1163    // Mock 1 existing template provider that creates terminal documents.
1164  1 mockExistingTemplateProviders(templateProviderFullName,
1165    new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, true);
1166   
1167    // Run the action
1168  1 String result = action.render(context);
1169   
1170    // The tests are below this line!
1171   
1172    // Verify null is returned (this means the response has been returned)
1173  1 assertNull(result);
1174   
1175    // Note: We are creating the document X.Y as terminal and using a template, as specified in the template
1176    // provider.
1177  1 verify(mockURLFactory).createURL("X", "Y", "edit", "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y",
1178    null, "xwiki", context);
1179    }
1180   
 
1181  1 toggle @Test
1182    public void existingDocumentFromUITemplateProviderSpecifiedTerminalOverridenFromUIToNonTerminal() throws Exception
1183    {
1184    // current document = xwiki:Main.WebHome
1185  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
1186  1 XWikiDocument document = mock(XWikiDocument.class);
1187  1 when(document.getDocumentReference()).thenReturn(documentReference);
1188  1 when(document.isNew()).thenReturn(false);
1189  1 context.setDoc(document);
1190   
1191    // Submit from the UI spaceReference=X&name=Y&templateProvider=XWiki.MyTemplateProvider
1192  1 String templateProviderFullName = "XWiki.MyTemplateProvider";
1193  1 String spaceReferenceString = "X";
1194  1 when(mockRequest.getParameter("spaceReference")).thenReturn(spaceReferenceString);
1195  1 when(mockRequest.getParameter("name")).thenReturn("Y");
1196  1 when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
1197  1 when(mockRequest.getParameter("tocreate")).thenReturn("nonterminal");
1198   
1199    // Mock 1 existing template provider that creates terminal documents.
1200  1 mockExistingTemplateProviders(templateProviderFullName,
1201    new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, true);
1202   
1203    // Run the action
1204  1 String result = action.render(context);
1205   
1206    // The tests are below this line!
1207   
1208    // Verify null is returned (this means the response has been returned)
1209  1 assertNull(result);
1210   
1211    // Note: We are creating the document X.Y.WebHome as non-terminal, even if the template provider says otherwise.
1212    // Also using a template, as specified in the template provider.
1213  1 verify(mockURLFactory).createURL("X.Y", "WebHome", "edit",
1214    "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y", null, "xwiki", context);
1215    }
1216   
 
1217  1 toggle @Test
1218    public void existingDocumentFromUITemplateProviderSpecifiedNonTerminal() throws Exception
1219    {
1220    // current document = xwiki:Main.WebHome
1221  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
1222  1 XWikiDocument document = mock(XWikiDocument.class);
1223  1 when(document.getDocumentReference()).thenReturn(documentReference);
1224  1 when(document.isNew()).thenReturn(false);
1225  1 context.setDoc(document);
1226   
1227    // Submit from the UI spaceReference=X&name=Y&templateProvider=XWiki.MyTemplateProvider
1228  1 String templateProviderFullName = "XWiki.MyTemplateProvider";
1229  1 String spaceReferenceString = "X";
1230  1 when(mockRequest.getParameter("spaceReference")).thenReturn(spaceReferenceString);
1231  1 when(mockRequest.getParameter("name")).thenReturn("Y");
1232  1 when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
1233   
1234    // Mock 1 existing template provider that creates terminal documents.
1235  1 mockExistingTemplateProviders(templateProviderFullName,
1236    new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST,
1237    false);
1238   
1239    // Run the action
1240  1 String result = action.render(context);
1241   
1242    // The tests are below this line!
1243   
1244    // Verify null is returned (this means the response has been returned)
1245  1 assertNull(result);
1246   
1247    // Note: We are creating the document X.Y.WebHome as non-terminal and using a template, as specified in the
1248    // template provider.
1249  1 verify(mockURLFactory).createURL("X.Y", "WebHome", "edit",
1250    "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y", null, "xwiki", context);
1251    }
1252   
 
1253  1 toggle @Test
1254    public void existingDocumentFromUITemplateProviderSpecifiedNonTerminalOverridenFromUIToTerminal() throws Exception
1255    {
1256    // current document = xwiki:Main.WebHome
1257  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
1258  1 XWikiDocument document = mock(XWikiDocument.class);
1259  1 when(document.getDocumentReference()).thenReturn(documentReference);
1260  1 when(document.isNew()).thenReturn(false);
1261  1 context.setDoc(document);
1262   
1263    // Submit from the UI spaceReference=X&name=Y&templateProvider=XWiki.MyTemplateProvider
1264  1 String templateProviderFullName = "XWiki.MyTemplateProvider";
1265  1 String spaceReferenceString = "X";
1266  1 when(mockRequest.getParameter("spaceReference")).thenReturn(spaceReferenceString);
1267  1 when(mockRequest.getParameter("name")).thenReturn("Y");
1268  1 when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
1269  1 when(mockRequest.getParameter("tocreate")).thenReturn("terminal");
1270   
1271    // Mock 1 existing template provider that creates terminal documents.
1272  1 mockExistingTemplateProviders(templateProviderFullName,
1273    new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST,
1274    false);
1275   
1276    // Run the action
1277  1 String result = action.render(context);
1278   
1279    // The tests are below this line!
1280   
1281    // Verify null is returned (this means the response has been returned)
1282  1 assertNull(result);
1283   
1284    // Note: We are creating the document X.Y as terminal, even if the template provider says otherwise.
1285    // Also using a template, as specified in the template provider.
1286  1 verify(mockURLFactory).createURL("X", "Y", "edit", "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y",
1287    null, "xwiki", context);
1288    }
1289   
 
1290  1 toggle @Test
1291    public void newDocumentWebHomeFromURLTemplateProviderSpecifiedButOldPageType() throws Exception
1292    {
1293    // new document = xwiki:X.Y.WebHome
1294  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X", "Y"), "WebHome");
1295  1 XWikiDocument document = mock(XWikiDocument.class);
1296  1 when(document.getDocumentReference()).thenReturn(documentReference);
1297  1 when(document.isNew()).thenReturn(true);
1298   
1299  1 context.setDoc(document);
1300   
1301    // Specifying a template provider in the URL: templateprovider=XWiki.MyTemplateProvider
1302  1 String templateProviderFullName = "XWiki.MyTemplateProvider";
1303  1 when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
1304   
1305    // Mock 1 existing template provider
1306  1 mockExistingTemplateProviders(templateProviderFullName,
1307    new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, null,
1308    "page");
1309   
1310    // Run the action
1311  1 String result = action.render(context);
1312   
1313    // The tests are below this line!
1314   
1315    // Verify null is returned (this means the response has been returned)
1316  1 assertNull(result);
1317   
1318    // Note: We are creating the document X.Y as terminal, since the template provider did not specify a "terminal"
1319    // property and it used the old "page" type instead. Also using a template, as specified in the template
1320    // provider.
1321  1 verify(mockURLFactory).createURL("X", "Y", "edit", "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y",
1322    null, "xwiki", context);
1323    }
1324   
 
1325  1 toggle @Test
1326    public void newDocumentWebHomeFromURLTemplateProviderSpecifiedButOldPageTypeButOverriddenFromUIToNonTerminal()
1327    throws Exception
1328    {
1329    // new document = xwiki:X.Y.WebHome
1330  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("X", "Y"), "WebHome");
1331  1 XWikiDocument document = mock(XWikiDocument.class);
1332  1 when(document.getDocumentReference()).thenReturn(documentReference);
1333  1 when(document.isNew()).thenReturn(true);
1334   
1335  1 context.setDoc(document);
1336   
1337    // Specifying a template provider in the URL: templateprovider=XWiki.MyTemplateProvider
1338  1 String templateProviderFullName = "XWiki.MyTemplateProvider";
1339  1 when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
1340  1 when(mockRequest.getParameter("tocreate")).thenReturn("nonterminal");
1341   
1342    // Mock 1 existing template provider
1343  1 mockExistingTemplateProviders(templateProviderFullName,
1344    new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, null,
1345    "page");
1346   
1347    // Run the action
1348  1 String result = action.render(context);
1349   
1350    // The tests are below this line!
1351   
1352    // Verify null is returned (this means the response has been returned)
1353  1 assertNull(result);
1354   
1355    // Note: We are creating the document X.Y.WebHome as non-terminal, since even if the template provider did not
1356    // specify a "terminal" property and it used the old "page" type, the UI explicitly asked for a non-terminal
1357    // document. Also using a template, as specified in the template provider.
1358  1 verify(mockURLFactory).createURL("X.Y", "WebHome", "edit",
1359    "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y", null, "xwiki", context);
1360    }
1361   
 
1362  1 toggle @Test
1363    public void existingDocumentFromUITemplateProviderSpecifiedButOldSpaceType() throws Exception
1364    {
1365    // current document = xwiki:Main.WebHome
1366  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
1367  1 XWikiDocument document = mock(XWikiDocument.class);
1368  1 when(document.getDocumentReference()).thenReturn(documentReference);
1369  1 when(document.isNew()).thenReturn(false);
1370  1 context.setDoc(document);
1371   
1372    // Submit from the UI spaceReference=X&name=Y&templateProvider=XWiki.MyTemplateProvider
1373  1 String templateProviderFullName = "XWiki.MyTemplateProvider";
1374  1 when(mockRequest.getParameter("spaceReference")).thenReturn("X");
1375  1 when(mockRequest.getParameter("name")).thenReturn("Y");
1376  1 when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
1377   
1378    // Mock 1 existing template provider
1379  1 mockExistingTemplateProviders(templateProviderFullName,
1380    new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, null,
1381    "space");
1382   
1383    // Run the action
1384  1 String result = action.render(context);
1385   
1386    // The tests are below this line!
1387   
1388    // Verify null is returned (this means the response has been returned)
1389  1 assertNull(result);
1390   
1391    // Note: We are creating X.Y.WebHome as non-terminal, since the template provider does not specify a "terminal"
1392    // property and we fallback on the "type" property's value. Also using the template extracted from the template
1393    // provider.
1394  1 verify(mockURLFactory).createURL("X.Y", "WebHome", "edit",
1395    "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y", null, "xwiki", context);
1396    }
1397   
 
1398  1 toggle @Test
1399    public void existingDocumentFromUITemplateProviderSpecifiedButOldSpaceTypeButOverridenFromUIToTerminal()
1400    throws Exception
1401    {
1402    // current document = xwiki:Main.WebHome
1403  1 DocumentReference documentReference = new DocumentReference("xwiki", Arrays.asList("Main"), "WebHome");
1404  1 XWikiDocument document = mock(XWikiDocument.class);
1405  1 when(document.getDocumentReference()).thenReturn(documentReference);
1406  1 when(document.isNew()).thenReturn(false);
1407  1 context.setDoc(document);
1408   
1409    // Submit from the UI spaceReference=X&name=Y&templateProvider=XWiki.MyTemplateProvider
1410  1 String templateProviderFullName = "XWiki.MyTemplateProvider";
1411  1 when(mockRequest.getParameter("spaceReference")).thenReturn("X");
1412  1 when(mockRequest.getParameter("name")).thenReturn("Y");
1413  1 when(mockRequest.getParameter("templateprovider")).thenReturn(templateProviderFullName);
1414  1 when(mockRequest.getParameter("tocreate")).thenReturn("terminal");
1415   
1416    // Mock 1 existing template provider
1417  1 mockExistingTemplateProviders(templateProviderFullName,
1418    new DocumentReference("xwiki", Arrays.asList("XWiki"), "MyTemplateProvider"), Collections.EMPTY_LIST, null,
1419    "space");
1420   
1421    // Run the action
1422  1 String result = action.render(context);
1423   
1424    // The tests are below this line!
1425   
1426    // Verify null is returned (this means the response has been returned)
1427  1 assertNull(result);
1428   
1429    // Note: We are creating X.Y as terminal, since it is overriden from the UI, regardless of any backwards
1430    // compatibility resolutions. Also using the template extracted from the template provider.
1431  1 verify(mockURLFactory).createURL("X", "Y", "edit", "template=XWiki.MyTemplate&parent=Main.WebHome&title=Y",
1432    null, "xwiki", context);
1433    }
1434    }