1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.extension.xar.internal.handler

File XarExtensionHandlerTest.java

 

Code metrics

16
441
28
1
1,177
748
36
0.08
15.75
28
1.29

Classes

Class Line # Actions
XarExtensionHandlerTest 84 441 0% 36 8
0.9835051398.4%
 

Contributing tests

This file is covered by 21 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 org.xwiki.extension.xar.internal.handler;
21   
22    import java.nio.charset.StandardCharsets;
23    import java.util.Arrays;
24    import java.util.HashMap;
25    import java.util.List;
26    import java.util.Locale;
27    import java.util.Map;
28   
29    import org.apache.commons.io.IOUtils;
30    import org.junit.Assert;
31    import org.junit.Before;
32    import org.junit.Rule;
33    import org.junit.Test;
34    import org.xwiki.bridge.event.WikiCreatedEvent;
35    import org.xwiki.bridge.event.WikiCreatingEvent;
36    import org.xwiki.extension.ExtensionId;
37    import org.xwiki.extension.InstallException;
38    import org.xwiki.extension.UninstallException;
39    import org.xwiki.extension.job.InstallRequest;
40    import org.xwiki.extension.job.UninstallRequest;
41    import org.xwiki.extension.job.internal.InstallJob;
42    import org.xwiki.extension.job.internal.UninstallJob;
43    import org.xwiki.extension.repository.InstalledExtensionRepository;
44    import org.xwiki.extension.test.MockitoRepositoryUtilsRule;
45    import org.xwiki.extension.xar.XarExtensionConfiguration.DocumentProtection;
46    import org.xwiki.extension.xar.internal.repository.XarInstalledExtension;
47    import org.xwiki.extension.xar.internal.repository.XarInstalledExtensionRepository;
48    import org.xwiki.job.Job;
49    import org.xwiki.job.JobExecutor;
50    import org.xwiki.logging.LogLevel;
51    import org.xwiki.logging.event.LogEvent;
52    import org.xwiki.model.reference.DocumentReference;
53    import org.xwiki.model.reference.LocalDocumentReference;
54    import org.xwiki.observation.ObservationManager;
55    import org.xwiki.rendering.syntax.Syntax;
56    import org.xwiki.security.authorization.AccessDeniedException;
57    import org.xwiki.security.authorization.Right;
58    import org.xwiki.test.annotation.AllComponents;
59    import org.xwiki.test.mockito.MockitoComponentManager;
60   
61    import com.xpn.xwiki.CoreConfiguration;
62    import com.xpn.xwiki.XWikiContext;
63    import com.xpn.xwiki.doc.MandatoryDocumentInitializer;
64    import com.xpn.xwiki.doc.XWikiAttachment;
65    import com.xpn.xwiki.doc.XWikiDocument;
66    import com.xpn.xwiki.objects.BaseObject;
67    import com.xpn.xwiki.objects.classes.BaseClass;
68    import com.xpn.xwiki.objects.classes.NumberClass;
69    import com.xpn.xwiki.test.MockitoOldcoreRule;
70    import com.xpn.xwiki.util.XWikiStubContextProvider;
71   
72    import static org.junit.Assert.assertEquals;
73    import static org.junit.Assert.assertFalse;
74    import static org.junit.jupiter.api.Assertions.assertTrue;
75    import static org.mockito.ArgumentMatchers.any;
76    import static org.mockito.ArgumentMatchers.eq;
77    import static org.mockito.Mockito.doReturn;
78    import static org.mockito.Mockito.doThrow;
79    import static org.mockito.Mockito.times;
80    import static org.mockito.Mockito.verify;
81    import static org.mockito.Mockito.when;
82   
83    @AllComponents
 
84    public class XarExtensionHandlerTest
85    {
86    private MockitoOldcoreRule oldcore = new MockitoOldcoreRule();
87   
88    private MockitoComponentManager componentManager = this.oldcore.getMocker();
89   
90    @Rule
91    public MockitoRepositoryUtilsRule repositoryUtil =
92    new MockitoRepositoryUtilsRule(this.componentManager, this.oldcore);
93   
94    private XarInstalledExtensionRepository installedExtensionRepository;
95   
96    private ExtensionId localXarExtensiontId1;
97   
98    private ExtensionId localXarExtensiontId2;
99   
100    private ExtensionId collisionextension1;
101   
102    private ExtensionId collisionextension2;
103   
104    private JobExecutor jobExecutor;
105   
106    private InstalledExtensionRepository xarExtensionRepository;
107   
108    private ObservationManager observation;
109   
110    private Map<String, BaseClass> classes = new HashMap<String, BaseClass>();
111   
112    private DocumentReference contextUser;
113   
 
114  21 toggle @Before
115    public void setUp() throws Exception
116    {
117    // mock
118   
119  21 this.contextUser = new DocumentReference(getXWikiContext().getWikiId(), "XWiki", "ExtensionUser");
120   
121  21 this.localXarExtensiontId1 = new ExtensionId("test", "1.0");
122  21 this.localXarExtensiontId2 = new ExtensionId("test", "2.0");
123  21 this.collisionextension1 = new ExtensionId("collisionextension1", "version");
124  21 this.collisionextension2 = new ExtensionId("collisionextension2", "version");
125   
126    // classes
127   
128  21 BaseClass styleSheetClass = new BaseClass();
129  21 this.classes.put("StyleSheetExtension", styleSheetClass);
130   
131    // checking
132   
133  21 doReturn(true).when(this.oldcore.getSpyXWiki()).hasAttachmentRecycleBin(any(XWikiContext.class));
134   
135  21 getXWikiContext().setUserReference(this.contextUser);
136   
137  21 ((XWikiStubContextProvider) this.componentManager.getInstance(XWikiStubContextProvider.class))
138    .initialize(getXWikiContext());
139   
140  21 CoreConfiguration coreConfiguration = this.componentManager.getInstance(CoreConfiguration.class);
141  21 doReturn(Syntax.PLAIN_1_0).when(coreConfiguration).getDefaultDocumentSyntax();
142   
143    // lookup
144   
145  21 this.jobExecutor = this.componentManager.getInstance(JobExecutor.class);
146  21 this.xarExtensionRepository =
147    this.componentManager.getInstance(InstalledExtensionRepository.class, XarExtensionHandler.TYPE);
148  21 this.observation = this.repositoryUtil.getComponentManager().getInstance(ObservationManager.class);
149   
150    // Get rid of wiki macro listener
151  21 this.componentManager.<ObservationManager>getInstance(ObservationManager.class)
152    .removeListener("RegisterMacrosOnImportListener");
153   
154  21 this.installedExtensionRepository =
155    this.componentManager.getInstance(InstalledExtensionRepository.class, "xar");
156   
157    // Programming right is not required for XAR extensions
158  21 doThrow(AccessDeniedException.class).when(this.oldcore.getMockAuthorizationManager())
159    .checkAccess(eq(Right.PROGRAM), any(), any());
160    }
161   
 
162  4 toggle private void setHasNoAdminRight() throws AccessDeniedException
163    {
164  4 doThrow(AccessDeniedException.class).when(this.oldcore.getMockAuthorizationManager())
165    .checkAccess(eq(Right.ADMIN), eq(this.contextUser), any());
166    }
167   
 
168  15 toggle private void verifyHasAdminRight(int times) throws AccessDeniedException
169    {
170  15 verify(this.oldcore.getMockAuthorizationManager(), times(times)).checkAccess(eq(Right.ADMIN),
171    eq(this.contextUser), any());
172    }
173   
 
174  184 toggle private XWikiContext getXWikiContext()
175    {
176  184 return this.oldcore.getXWikiContext();
177    }
178   
 
179  29 toggle private XarInstalledExtension install(ExtensionId extensionId, String wiki, DocumentReference user) throws Throwable
180    {
181  29 return installOnNamespace(extensionId, wiki != null ? "wiki:" + wiki : null, user);
182    }
183   
 
184  30 toggle private XarInstalledExtension installOnNamespace(ExtensionId extensionId, String namespace, DocumentReference user)
185    throws Throwable
186    {
187  30 InstallRequest installRequest = new InstallRequest();
188  30 if (user != null) {
189  29 installRequest.setProperty("user.reference", getXWikiContext().getUserReference());
190  29 installRequest.setProperty("checkrights", true);
191    }
192  30 installRequest.addExtension(extensionId);
193  30 if (namespace != null) {
194  18 installRequest.addNamespace(namespace);
195    }
196  30 Job installJob = this.jobExecutor.execute(InstallJob.JOBTYPE, installRequest);
197  30 installJob.join();
198   
199  30 List<LogEvent> errors = installJob.getStatus().getLog().getLogsFrom(LogLevel.WARN);
200  30 if (!errors.isEmpty()) {
201  3 if (errors.get(0).getThrowable() != null) {
202  3 throw errors.get(0).getThrowable();
203    } else {
204  0 throw new Exception(errors.get(0).getFormattedMessage());
205    }
206    }
207   
208  27 return (XarInstalledExtension) this.xarExtensionRepository.resolve(extensionId);
209    }
210   
 
211  6 toggle private void uninstall(ExtensionId extensionId, String wiki) throws Throwable
212    {
213  6 UninstallRequest uninstallRequest = new UninstallRequest();
214  6 uninstallRequest.setProperty("user.reference", getXWikiContext().getUserReference());
215  6 uninstallRequest.setProperty("checkrights", true);
216  6 uninstallRequest.addExtension(extensionId);
217  6 if (wiki != null) {
218  4 uninstallRequest.addNamespace("wiki:" + wiki);
219    }
220  6 Job uninstallJob = this.jobExecutor.execute(UninstallJob.JOBTYPE, uninstallRequest);
221  6 uninstallJob.join();
222   
223  6 List<LogEvent> errors = uninstallJob.getStatus().getLog().getLogsFrom(LogLevel.WARN);
224  6 if (!errors.isEmpty()) {
225  2 if (errors.get(0).getThrowable() != null) {
226  2 throw errors.get(0).getThrowable();
227    } else {
228  0 throw new Exception(errors.get(0).getFormattedMessage());
229    }
230    }
231    }
232   
233    // Tests
234   
 
235  1 toggle @Test
236    public void testInstallOnWiki() throws Throwable
237    {
238  1 XWikiDocument existingDocument = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
239  1 BaseObject object = new BaseObject();
240  1 object.setXClassReference(new DocumentReference("wiki", "space", "class"));
241  1 existingDocument.addXObject(object);
242  1 existingDocument.setCreatorReference(new DocumentReference("wiki", "space", "existingcreator"));
243  1 this.oldcore.getSpyXWiki().saveDocument(existingDocument, "", true, getXWikiContext());
244   
245  1 MandatoryDocumentInitializer mandatoryInitializer =
246    this.componentManager.registerMockComponent(MandatoryDocumentInitializer.class, "space.mandatory");
247  1 when(mandatoryInitializer.updateDocument(any(XWikiDocument.class))).thenReturn(true);
248  1 XWikiDocument mandatoryDocument = new XWikiDocument(new DocumentReference("wiki", "space", "mandatory"));
249  1 mandatoryDocument.setCreatorReference(new DocumentReference("wiki", "space", "existingcreator"));
250  1 mandatoryDocument.setSyntax(Syntax.PLAIN_1_0);
251  1 mandatoryDocument.setContent("modified content");
252  1 this.oldcore.getSpyXWiki().saveDocument(mandatoryDocument, "", true, getXWikiContext());
253   
254  1 MandatoryDocumentInitializer mandatoryconfigurationInitializer = this.componentManager
255    .registerMockComponent(MandatoryDocumentInitializer.class, "space.mandatoryconfiguration");
256  1 when(mandatoryconfigurationInitializer.updateDocument(any(XWikiDocument.class))).thenReturn(true);
257  1 XWikiDocument mandatoryconfigurationDocument =
258    new XWikiDocument(new DocumentReference("wiki", "space", "mandatoryconfiguration"));
259  1 mandatoryconfigurationDocument.setCreatorReference(new DocumentReference("wiki", "space", "existingcreator"));
260  1 mandatoryconfigurationDocument.setSyntax(Syntax.PLAIN_1_0);
261  1 this.oldcore.getSpyXWiki().saveDocument(mandatoryconfigurationDocument, "", true, getXWikiContext());
262   
263    // install
264   
265  1 XarInstalledExtension xarInstalledExtension = install(this.localXarExtensiontId1, "wiki", this.contextUser);
266   
267  1 verifyHasAdminRight(2);
268   
269    // validate
270   
271    // space.page
272  1 XWikiDocument page =
273    this.oldcore.getSpyXWiki().getDocument(existingDocument.getDocumentReference(), getXWikiContext());
274   
275  1 Assert.assertFalse("Document wiki:space.page has not been saved in the database", page.isNew());
276   
277  1 Assert.assertNull(page.getXObject(object.getXClassReference()));
278   
279  1 Assert.assertEquals("Wrong content", "content", page.getContent());
280  1 Assert.assertEquals("Wrong creator", new DocumentReference("wiki", "space", "existingcreator"),
281    page.getCreatorReference());
282  1 Assert.assertEquals("Wrong author", this.contextUser, page.getAuthorReference());
283  1 Assert.assertEquals("Wrong content author", this.contextUser, page.getContentAuthorReference());
284  1 Assert.assertEquals("Wrong version", "2.1", page.getVersion());
285  1 Assert.assertEquals("Wrong version", Locale.ROOT, page.getLocale());
286  1 Assert.assertFalse("Document is hidden", page.isHidden());
287  1 Assert.assertFalse("Document is minor edit", page.isMinorEdit());
288   
289  1 BaseClass baseClass = page.getXClass();
290  1 Assert.assertNotNull(baseClass.getField("property"));
291  1 Assert.assertEquals("property", baseClass.getField("property").getName());
292  1 Assert.assertSame(NumberClass.class, baseClass.getField("property").getClass());
293   
294    // space.pagewithattachment
295   
296  1 XWikiDocument pagewithattachment = this.oldcore.getSpyXWiki()
297    .getDocument(new DocumentReference("wiki", "space", "pagewithattachment"), getXWikiContext());
298  1 Assert.assertFalse(pagewithattachment.isNew());
299  1 Assert.assertEquals("Wrong version", "1.1", pagewithattachment.getVersion());
300  1 Assert.assertEquals("Wrong creator", this.contextUser, pagewithattachment.getCreatorReference());
301  1 Assert.assertEquals("Wrong author", this.contextUser, pagewithattachment.getAuthorReference());
302  1 Assert.assertEquals("Wrong content author", this.contextUser, pagewithattachment.getContentAuthorReference());
303   
304  1 XWikiAttachment attachment = pagewithattachment.getAttachment("attachment.txt");
305  1 Assert.assertNotNull(attachment);
306  1 Assert.assertEquals("attachment.txt", attachment.getFilename());
307  1 Assert.assertEquals(18, attachment.getContentLongSize(getXWikiContext()));
308  1 Assert.assertEquals("attachment content",
309    IOUtils.toString(attachment.getContentInputStream(getXWikiContext()), StandardCharsets.UTF_8));
310  1 Assert.assertEquals(this.contextUser, attachment.getAuthorReference());
311   
312    // space1.page1
313   
314  1 XWikiDocument page1 =
315    this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space1", "page1"), getXWikiContext());
316   
317  1 Assert.assertFalse("Document wiki:space1.page1 has not been saved in the database", page1.isNew());
318   
319  1 assertEquals(Arrays.asList(xarInstalledExtension),
320    this.installedExtensionRepository.getXarInstalledExtensions(page1.getDocumentReference()));
321  1 assertEquals(Arrays.asList(xarInstalledExtension),
322    this.installedExtensionRepository.getXarInstalledExtensions(page1.getDocumentReferenceWithLocale()));
323  1 assertEquals(0, this.installedExtensionRepository
324    .getXarInstalledExtensions(new DocumentReference("wiki", "space1", "page1", Locale.ENGLISH)).size());
325  1 assertEquals(0, this.installedExtensionRepository
326    .getXarInstalledExtensions(new DocumentReference("otherwiki", "space1", "page1")).size());
327   
328    // translated.translated
329  1 DocumentReference translatedReference = new DocumentReference("wiki", "translated", "translated");
330  1 XWikiDocument defaultTranslated =
331    this.oldcore.getSpyXWiki().getDocument(translatedReference, getXWikiContext());
332   
333  1 Assert.assertNotNull("Document wiki:translated.translated has not been saved in the database",
334    defaultTranslated);
335  1 Assert.assertFalse("Document wiki:translated.translated has not been saved in the database",
336    defaultTranslated.isNew());
337   
338  1 Assert.assertEquals("Wrong content", "default content", defaultTranslated.getContent());
339  1 Assert.assertEquals("Wrong creator", this.contextUser, defaultTranslated.getCreatorReference());
340  1 Assert.assertEquals("Wrong author", this.contextUser, defaultTranslated.getAuthorReference());
341  1 Assert.assertEquals("Wrong content author", this.contextUser, defaultTranslated.getContentAuthorReference());
342  1 Assert.assertEquals("Wrong version", "1.1", defaultTranslated.getVersion());
343   
344  1 assertEquals(Arrays.asList(xarInstalledExtension), this.installedExtensionRepository
345    .getXarInstalledExtensions(defaultTranslated.getDocumentReferenceWithLocale()));
346   
347    // translated.translated.tr
348  1 XWikiDocument translated =
349    this.oldcore.getDocuments().get(new DocumentReference(translatedReference, new Locale("tr")));
350   
351  1 Assert.assertNotNull("Document wiki:translated.translated in langauge tr has not been saved in the database",
352    translated);
353  1 Assert.assertFalse("Document wiki:translated.translated in langauge tr has not been saved in the database",
354    translated.isNew());
355   
356  1 Assert.assertEquals("Wrong content", "tr content", translated.getContent());
357  1 Assert.assertEquals("Wrong creator", this.contextUser, translated.getCreatorReference());
358  1 Assert.assertEquals("Wrong author", this.contextUser, translated.getAuthorReference());
359  1 Assert.assertEquals("Wrong content author", this.contextUser, translated.getContentAuthorReference());
360  1 Assert.assertEquals("Wrong version", "1.1", translated.getVersion());
361   
362  1 assertEquals(Arrays.asList(xarInstalledExtension),
363    this.installedExtensionRepository.getXarInstalledExtensions(translated.getDocumentReferenceWithLocale()));
364   
365    // translated.translated.fr
366  1 XWikiDocument translated2 =
367    this.oldcore.getDocuments().get(new DocumentReference(translatedReference, new Locale("fr")));
368   
369  1 Assert.assertNotNull("Document wiki:translated.translated in language fr has not been saved in the database",
370    translated2);
371  1 Assert.assertFalse("Document wiki:translated.translated in langauge fr has not been saved in the database",
372    translated2.isNew());
373   
374  1 Assert.assertEquals("Wrong content", "fr content", translated2.getContent());
375  1 Assert.assertEquals("Wrong creator", this.contextUser, translated2.getCreatorReference());
376  1 Assert.assertEquals("Wrong author", this.contextUser, translated2.getAuthorReference());
377  1 Assert.assertEquals("Wrong content author", this.contextUser, translated2.getContentAuthorReference());
378  1 Assert.assertEquals("Wrong version", "1.1", translated2.getVersion());
379   
380  1 assertEquals(Arrays.asList(xarInstalledExtension),
381    this.installedExtensionRepository.getXarInstalledExtensions(translated2.getDocumentReferenceWithLocale()));
382   
383    // space.hiddenpage
384   
385  1 XWikiDocument hiddenpage = this.oldcore.getSpyXWiki()
386    .getDocument(new DocumentReference("wiki", "space", "hiddenpage"), getXWikiContext());
387   
388  1 Assert.assertFalse("Document wiki:space.hiddenpage has not been saved in the database", hiddenpage.isNew());
389   
390  1 Assert.assertTrue("Document is not hidden", hiddenpage.isHidden());
391   
392    // space.mandatory
393   
394  1 XWikiDocument mandatorypage = this.oldcore.getSpyXWiki()
395    .getDocument(new DocumentReference("wiki", "space", "mandatory"), getXWikiContext());
396   
397  1 Assert.assertEquals("Document wiki:space.mandatory has been overwritten", "1.1", mandatorypage.getVersion());
398   
399    // space.mandatoryconfiguration
400   
401  1 XWikiDocument mandatoryconfigurationpage = this.oldcore.getSpyXWiki()
402    .getDocument(new DocumentReference("wiki", "space", "mandatoryconfiguration"), getXWikiContext());
403   
404  1 assertEquals("Document wiki:space.mandatoryconfiguration has not been overwritten", "2.1",
405    mandatoryconfigurationpage.getVersion());
406   
407  1 assertTrue(
408    this.installedExtensionRepository.isAllowed(mandatoryconfigurationpage.getDocumentReference(), Right.EDIT));
409  1 assertFalse(this.installedExtensionRepository.isAllowed(mandatoryconfigurationpage.getDocumentReference(),
410    Right.DELETE));
411    }
412   
 
413  1 toggle @Test
414    public void testInstallOnWikiWithoutAuthor() throws Throwable
415    {
416  1 XWikiDocument existingDocument = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
417  1 BaseObject object = new BaseObject();
418  1 object.setXClassReference(new DocumentReference("wiki", "space", "class"));
419  1 existingDocument.addXObject(object);
420  1 existingDocument.setCreatorReference(new DocumentReference("wiki", "space", "existingcreator"));
421  1 this.oldcore.getSpyXWiki().saveDocument(existingDocument, "", true, getXWikiContext());
422   
423    // install
424   
425  1 install(this.localXarExtensiontId1, "wiki", null);
426   
427    // validate
428   
429  1 DocumentReference xarAuthorReference = new DocumentReference("wiki", "XWiki", "author");
430  1 DocumentReference xarCreatorReference = new DocumentReference("wiki", "XWiki", "creator");
431  1 DocumentReference xarContentAuthorReference = new DocumentReference("wiki", "XWiki", "contentAuthor");
432   
433    // space.page
434  1 XWikiDocument page =
435    this.oldcore.getSpyXWiki().getDocument(existingDocument.getDocumentReference(), getXWikiContext());
436   
437  1 Assert.assertFalse("Document wiki:space.page has not been saved in the database", page.isNew());
438   
439  1 Assert.assertNull(page.getXObject(object.getXClassReference()));
440   
441  1 Assert.assertEquals("Wrong content", "content", page.getContent());
442  1 Assert.assertEquals("Wrong creator", new DocumentReference("wiki", "space", "existingcreator"),
443    page.getCreatorReference());
444  1 Assert.assertEquals("Wrong author", xarAuthorReference, page.getAuthorReference());
445  1 Assert.assertEquals("Wrong content author", xarContentAuthorReference, page.getContentAuthorReference());
446  1 Assert.assertEquals("Wrong version", "2.1", page.getVersion());
447  1 Assert.assertEquals("Wrong version", Locale.ROOT, page.getLocale());
448  1 Assert.assertFalse("Document is hidden", page.isHidden());
449   
450  1 BaseClass baseClass = page.getXClass();
451  1 Assert.assertNotNull(baseClass.getField("property"));
452  1 Assert.assertEquals("property", baseClass.getField("property").getName());
453  1 Assert.assertSame(NumberClass.class, baseClass.getField("property").getClass());
454   
455    // space.pagewithattachment
456   
457  1 XWikiDocument pagewithattachment = this.oldcore.getSpyXWiki()
458    .getDocument(new DocumentReference("wiki", "space", "pagewithattachment"), getXWikiContext());
459  1 Assert.assertFalse(pagewithattachment.isNew());
460  1 Assert.assertEquals("Wrong version", "1.1", pagewithattachment.getVersion());
461  1 Assert.assertEquals("Wrong creator", xarCreatorReference, pagewithattachment.getCreatorReference());
462  1 Assert.assertEquals("Wrong author", xarAuthorReference, pagewithattachment.getAuthorReference());
463  1 Assert.assertEquals("Wrong content author", xarContentAuthorReference,
464    pagewithattachment.getContentAuthorReference());
465   
466  1 XWikiAttachment attachment = pagewithattachment.getAttachment("attachment.txt");
467  1 Assert.assertNotNull(attachment);
468  1 Assert.assertEquals("attachment.txt", attachment.getFilename());
469  1 Assert.assertEquals(18, attachment.getContentLongSize(getXWikiContext()));
470  1 Assert.assertEquals("attachment content",
471    IOUtils.toString(attachment.getContentInputStream(getXWikiContext()), StandardCharsets.UTF_8));
472  1 Assert.assertEquals(new DocumentReference("wiki", "XWiki", "attachmentauthor"),
473    attachment.getAuthorReference());
474   
475    // space1.page1
476   
477  1 XWikiDocument page1 =
478    this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space1", "page1"), getXWikiContext());
479   
480  1 Assert.assertFalse("Document wiki:space1.page1 has not been saved in the database", page1.isNew());
481   
482    // translated.translated
483  1 DocumentReference translatedReference = new DocumentReference("wiki", "translated", "translated");
484  1 XWikiDocument defaultTranslated =
485    this.oldcore.getSpyXWiki().getDocument(translatedReference, getXWikiContext());
486   
487  1 Assert.assertNotNull("Document wiki:translated.translated has not been saved in the database",
488    defaultTranslated);
489  1 Assert.assertFalse("Document wiki:translated.translated has not been saved in the database",
490    defaultTranslated.isNew());
491   
492  1 Assert.assertEquals("Wrong content", "default content", defaultTranslated.getContent());
493  1 Assert.assertEquals("Wrong creator", xarCreatorReference, defaultTranslated.getCreatorReference());
494  1 Assert.assertEquals("Wrong author", xarAuthorReference, defaultTranslated.getAuthorReference());
495  1 Assert.assertEquals("Wrong content author", xarContentAuthorReference,
496    defaultTranslated.getContentAuthorReference());
497  1 Assert.assertEquals("Wrong version", "1.1", defaultTranslated.getVersion());
498   
499    // translated.translated.tr
500  1 XWikiDocument translated =
501    this.oldcore.getDocuments().get(new DocumentReference(translatedReference, new Locale("tr")));
502   
503  1 Assert.assertNotNull("Document wiki:translated.translated in langauge tr has not been saved in the database",
504    translated);
505  1 Assert.assertFalse("Document wiki:translated.translated in langauge tr has not been saved in the database",
506    translated.isNew());
507   
508  1 Assert.assertEquals("Wrong content", "tr content", translated.getContent());
509  1 Assert.assertEquals("Wrong creator", xarCreatorReference, translated.getCreatorReference());
510  1 Assert.assertEquals("Wrong author", xarAuthorReference, translated.getAuthorReference());
511  1 Assert.assertEquals("Wrong content author", xarContentAuthorReference, translated.getContentAuthorReference());
512  1 Assert.assertEquals("Wrong version", "1.1", translated.getVersion());
513   
514    // translated.translated.fr
515  1 XWikiDocument translated2 =
516    this.oldcore.getDocuments().get(new DocumentReference(translatedReference, new Locale("fr")));
517   
518  1 Assert.assertNotNull("Document wiki:translated.translated in language fr has not been saved in the database",
519    translated2);
520  1 Assert.assertFalse("Document wiki:translated.translated in langauge fr has not been saved in the database",
521    translated2.isNew());
522   
523  1 Assert.assertEquals("Wrong content", "fr content", translated2.getContent());
524  1 Assert.assertEquals("Wrong creator", xarCreatorReference, translated2.getCreatorReference());
525  1 Assert.assertEquals("Wrong author", xarAuthorReference, translated2.getAuthorReference());
526  1 Assert.assertEquals("Wrong content author", xarContentAuthorReference, translated2.getContentAuthorReference());
527  1 Assert.assertEquals("Wrong version", "1.1", translated2.getVersion());
528   
529    // space.hiddenpage
530   
531  1 XWikiDocument hiddenpage = this.oldcore.getSpyXWiki()
532    .getDocument(new DocumentReference("wiki", "space", "hiddenpage"), getXWikiContext());
533   
534  1 Assert.assertNotNull("Document wiki:space.hiddenpage has not been saved in the database", hiddenpage);
535  1 Assert.assertFalse("Document wiki:space.hiddenpage has not been saved in the database", hiddenpage.isNew());
536   
537  1 Assert.assertTrue("Document is not hidden", hiddenpage.isHidden());
538    }
539   
 
540  1 toggle @Test
541    public void testUpgradeOnWiki() throws Throwable
542    {
543  1 install(this.localXarExtensiontId1, "wiki", this.contextUser);
544   
545  1 verifyHasAdminRight(2);
546   
547    // Do some local modifications
548   
549  1 XWikiDocument deletedpage = this.oldcore.getSpyXWiki()
550    .getDocument(new DocumentReference("wiki", "space", "deletedpage"), getXWikiContext());
551  1 this.oldcore.getSpyXWiki().deleteDocument(deletedpage, getXWikiContext());
552   
553  1 XWikiDocument modifieddeletedpage = this.oldcore.getSpyXWiki()
554    .getDocument(new DocumentReference("wiki", "space", "modifieddeletedpage"), getXWikiContext());
555  1 this.oldcore.getSpyXWiki().deleteDocument(modifieddeletedpage, getXWikiContext());
556   
557  1 XWikiDocument pagewithobject = this.oldcore.getSpyXWiki()
558    .getDocument(new DocumentReference("wiki", "space", "pagewithobject"), getXWikiContext());
559  1 pagewithobject.removeXObjects(new LocalDocumentReference("XWiki", "XWikiGroups"));
560  1 this.oldcore.getSpyXWiki().saveDocument(pagewithobject, getXWikiContext());
561   
562  1 XWikiDocument deletedpagewithmodifications = this.oldcore.getSpyXWiki()
563    .getDocument(new DocumentReference("wiki", "space1", "modified"), getXWikiContext());
564  1 deletedpagewithmodifications.setContent("modified content");
565   
566    // upgrade
567   
568  1 install(this.localXarExtensiontId2, "wiki", this.contextUser);
569   
570  1 verifyHasAdminRight(3);
571   
572    // validate
573   
574    // samespace.samepage
575   
576  1 XWikiDocument samepage = this.oldcore.getSpyXWiki()
577    .getDocument(new DocumentReference("wiki", "samespace", "samepage"), getXWikiContext());
578   
579  1 Assert.assertFalse("Document samespace has been removed from the database", samepage.isNew());
580  1 Assert.assertEquals("Wrong versions", "1.1", samepage.getVersion());
581   
582    // space.page
583   
584  1 XWikiDocument modifiedpage =
585    this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"), getXWikiContext());
586   
587  1 Assert.assertFalse("Document wiki.space.page has not been saved in the database", modifiedpage.isNew());
588   
589  1 Assert.assertEquals("Wrong content", "content 2", modifiedpage.getContent());
590  1 Assert.assertEquals("Wrong author", this.contextUser, modifiedpage.getAuthorReference());
591  1 Assert.assertEquals("Wrong versions", "2.1", modifiedpage.getVersion());
592  1 Assert.assertEquals("Wrong version", Locale.ROOT, modifiedpage.getLocale());
593   
594  1 Assert.assertEquals("Wrong customclass", "customclass2", modifiedpage.getCustomClass());
595  1 Assert.assertEquals("Wrong defaultTemplate", "defaultTemplate2", modifiedpage.getDefaultTemplate());
596  1 Assert.assertEquals("Wrong hidden", true, modifiedpage.isHidden());
597  1 Assert.assertEquals("Wrong ValidationScript", "validationScript2", modifiedpage.getValidationScript());
598   
599  1 BaseClass baseClass = modifiedpage.getXClass();
600  1 Assert.assertNotNull(baseClass.getField("property"));
601  1 Assert.assertEquals("property", baseClass.getField("property").getName());
602  1 Assert.assertSame(NumberClass.class, baseClass.getField("property").getClass());
603   
604  1 XWikiAttachment attachment = modifiedpage.getAttachment("attachment.txt");
605  1 Assert.assertNotNull(attachment);
606  1 Assert.assertEquals("attachment.txt", attachment.getFilename());
607  1 Assert.assertEquals(18, attachment.getContentLongSize(getXWikiContext()));
608  1 Assert.assertEquals("attachment content",
609    IOUtils.toString(attachment.getContentInputStream(getXWikiContext()), StandardCharsets.UTF_8));
610   
611    // space2.page2
612   
613  1 XWikiDocument newPage =
614    this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space2", "page2"), getXWikiContext());
615   
616  1 Assert.assertFalse("Document wiki:space2.page2 has not been saved in the database", newPage.isNew());
617   
618    // space1.page1
619   
620  1 XWikiDocument removedPage =
621    this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space1", "page1"), getXWikiContext());
622   
623  1 Assert.assertTrue("Document wiki:space1.page1 has not been removed from the database", removedPage.isNew());
624   
625    // space.deletedpage
626   
627  1 deletedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "deletedpage"),
628    getXWikiContext());
629   
630  1 Assert.assertTrue("Document wiki:space.deleted has been restored", deletedpage.isNew());
631   
632    // space.modifieddeletedpage
633   
634  1 modifieddeletedpage = this.oldcore.getSpyXWiki()
635    .getDocument(new DocumentReference("wiki", "space", "modifieddeletedpage"), getXWikiContext());
636   
637  1 Assert.assertTrue("Document wiki:space.modifieddeletedpage has been restored", modifieddeletedpage.isNew());
638   
639    // space.pagewithobject
640   
641  1 pagewithobject = this.oldcore.getSpyXWiki()
642    .getDocument(new DocumentReference("wiki", "space", "pagewithobject"), getXWikiContext());
643   
644  1 Assert.assertNull("Document wiki:space.pagewithobject does not contain an XWiki.XWikiGroups object",
645    pagewithobject.getXObject(new LocalDocumentReference("XWiki", "XWikiGroups")));
646   
647    // space1.modified
648   
649  1 XWikiDocument space1modified = this.oldcore.getSpyXWiki()
650    .getDocument(new DocumentReference("wiki", "space1", "modified"), getXWikiContext());
651   
652  1 Assert.assertFalse("Document wiki:space.modified has been removed from the database", space1modified.isNew());
653    }
654   
 
655  1 toggle @Test
656    public void testUpgradeOnRoot() throws Throwable
657    {
658  1 doReturn(Arrays.asList("wiki1", "wiki2")).when(this.oldcore.getWikiDescriptorManager()).getAllIds();
659   
660  1 install(this.localXarExtensiontId1, null, this.contextUser);
661   
662  1 verifyHasAdminRight(2);
663   
664    // Do some local modifications
665   
666  1 XWikiDocument deletedpage = this.oldcore.getSpyXWiki()
667    .getDocument(new DocumentReference("wiki1", "space", "deletedpage"), getXWikiContext());
668  1 this.oldcore.getSpyXWiki().deleteDocument(deletedpage, getXWikiContext());
669   
670  1 XWikiDocument modifieddeletedpage = this.oldcore.getSpyXWiki()
671    .getDocument(new DocumentReference("wiki1", "space", "modifieddeletedpage"), getXWikiContext());
672  1 this.oldcore.getSpyXWiki().deleteDocument(modifieddeletedpage, getXWikiContext());
673   
674  1 XWikiDocument pagewithobject = this.oldcore.getSpyXWiki()
675    .getDocument(new DocumentReference("wiki1", "space", "pagewithobject"), getXWikiContext());
676  1 pagewithobject.removeXObjects(new LocalDocumentReference("XWiki", "XWikiGroups"));
677  1 this.oldcore.getSpyXWiki().saveDocument(pagewithobject, getXWikiContext());
678   
679  1 XWikiDocument deletedpagewithmodifications = this.oldcore.getSpyXWiki()
680    .getDocument(new DocumentReference("wiki1", "space1", "modified"), getXWikiContext());
681  1 deletedpagewithmodifications.setContent("modified content");
682   
683    // upgrade
684   
685  1 install(this.localXarExtensiontId2, null, this.contextUser);
686   
687  1 verifyHasAdminRight(3);
688   
689    // validate
690   
691    // samespace.samepage
692   
693  1 XWikiDocument samepage = this.oldcore.getSpyXWiki()
694    .getDocument(new DocumentReference("wiki1", "samespace", "samepage"), getXWikiContext());
695   
696  1 Assert.assertEquals("Wrong versions", "1.1", samepage.getVersion());
697   
698    // space.page
699   
700  1 XWikiDocument modifiedpage =
701    this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "page"), getXWikiContext());
702   
703  1 Assert.assertFalse("Document wiki.space.page has not been saved in the database", modifiedpage.isNew());
704   
705  1 Assert.assertEquals("Wrong content", "content 2", modifiedpage.getContent());
706  1 Assert.assertEquals("Wrong author", this.contextUser, modifiedpage.getAuthorReference());
707  1 Assert.assertEquals("Wrong versions", "2.1", modifiedpage.getVersion());
708  1 Assert.assertEquals("Wrong version", Locale.ROOT, modifiedpage.getLocale());
709   
710  1 Assert.assertEquals("Wrong customclass", "customclass2", modifiedpage.getCustomClass());
711  1 Assert.assertEquals("Wrong defaultTemplate", "defaultTemplate2", modifiedpage.getDefaultTemplate());
712  1 Assert.assertEquals("Wrong hidden", true, modifiedpage.isHidden());
713  1 Assert.assertEquals("Wrong ValidationScript", "validationScript2", modifiedpage.getValidationScript());
714   
715  1 BaseClass baseClass = modifiedpage.getXClass();
716  1 Assert.assertNotNull(baseClass.getField("property"));
717  1 Assert.assertEquals("property", baseClass.getField("property").getName());
718  1 Assert.assertSame(NumberClass.class, baseClass.getField("property").getClass());
719   
720  1 XWikiAttachment attachment = modifiedpage.getAttachment("attachment.txt");
721  1 Assert.assertNotNull(attachment);
722  1 Assert.assertEquals("attachment.txt", attachment.getFilename());
723  1 Assert.assertEquals(18, attachment.getContentLongSize(getXWikiContext()));
724  1 Assert.assertEquals("attachment content",
725    IOUtils.toString(attachment.getContentInputStream(getXWikiContext()), StandardCharsets.UTF_8));
726   
727    // space2.page2
728   
729  1 XWikiDocument newPage = this.oldcore.getSpyXWiki()
730    .getDocument(new DocumentReference("wiki1", "space2", "page2"), getXWikiContext());
731   
732  1 Assert.assertFalse("Document wiki:space2.page2 has not been saved in the database", newPage.isNew());
733   
734    // space1.page1
735   
736  1 XWikiDocument removedPage = this.oldcore.getSpyXWiki()
737    .getDocument(new DocumentReference("wiki1", "space1", "page1"), getXWikiContext());
738   
739  1 Assert.assertTrue("Document wiki:space1.page1 has not been removed from the database", removedPage.isNew());
740   
741    // space.deletedpage
742   
743  1 deletedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "deletedpage"),
744    getXWikiContext());
745   
746  1 Assert.assertTrue("Document wiki:space.deleted has been restored", deletedpage.isNew());
747   
748    // space.modifieddeletedpage
749   
750  1 modifieddeletedpage = this.oldcore.getSpyXWiki()
751    .getDocument(new DocumentReference("wiki1", "space", "modifieddeletedpage"), getXWikiContext());
752   
753  1 Assert.assertTrue("Document wiki:space.modifieddeletedpage has been restored", modifieddeletedpage.isNew());
754   
755    // space.pagewithobject
756   
757  1 pagewithobject = this.oldcore.getSpyXWiki()
758    .getDocument(new DocumentReference("wiki1", "space", "pagewithobject"), getXWikiContext());
759   
760  1 Assert.assertNull("Document wiki:space.pagewithobject does not contain an XWiki.XWikiGroups object",
761    pagewithobject.getXObject(new LocalDocumentReference("XWiki", "XWikiGroups")));
762   
763    // space1.modified
764   
765  1 XWikiDocument space1modified = this.oldcore.getSpyXWiki()
766    .getDocument(new DocumentReference("wiki1", "space1", "modified"), getXWikiContext());
767   
768  1 Assert.assertFalse("Document wiki:space1.modified has been removed from the database", space1modified.isNew());
769    }
770   
 
771  1 toggle @Test
772    public void testDowngradeOnWiki() throws Throwable
773    {
774  1 install(this.localXarExtensiontId2, "wiki", this.contextUser);
775   
776  1 verifyHasAdminRight(1);
777   
778    // upgrade
779   
780  1 install(this.localXarExtensiontId1, "wiki", this.contextUser);
781   
782  1 verifyHasAdminRight(3);
783   
784    // validate
785   
786    // samespace.samepage
787   
788  1 XWikiDocument samepage = this.oldcore.getSpyXWiki()
789    .getDocument(new DocumentReference("wiki", "samespace", "samepage"), getXWikiContext());
790   
791  1 Assert.assertEquals("Wrong versions", "1.1", samepage.getVersion());
792   
793    // space.page
794   
795  1 XWikiDocument modifiedpage =
796    this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"), getXWikiContext());
797   
798  1 Assert.assertFalse("Document wiki.space.page has not been saved in the database", modifiedpage.isNew());
799   
800  1 Assert.assertEquals("Wrong content", "content", modifiedpage.getContent());
801  1 Assert.assertEquals("Wrong author", this.contextUser, modifiedpage.getAuthorReference());
802  1 Assert.assertEquals("Wrong versions", "2.1", modifiedpage.getVersion());
803  1 Assert.assertEquals("Wrong version", Locale.ROOT, modifiedpage.getLocale());
804   
805  1 BaseClass baseClass = modifiedpage.getXClass();
806  1 Assert.assertNotNull(baseClass.getField("property"));
807  1 Assert.assertEquals("property", baseClass.getField("property").getName());
808  1 Assert.assertSame(NumberClass.class, baseClass.getField("property").getClass());
809   
810    // The attachment does not exist in version 1.0
811  1 Assert.assertNull(modifiedpage.getAttachment("attachment.txt"));
812   
813    // space2.page2
814   
815  1 XWikiDocument newPage =
816    this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space2", "page2"), getXWikiContext());
817   
818  1 Assert.assertTrue("Document wiki.space2.page2 has not been removed from the database", newPage.isNew());
819   
820    // space1.page1
821   
822  1 XWikiDocument removedPage =
823    this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space1", "page1"), getXWikiContext());
824   
825  1 Assert.assertFalse("Document wiki.space1.page1 has not been saved in the database", removedPage.isNew());
826    }
827   
 
828  1 toggle @Test
829    public void testUninstallFromWiki() throws Throwable
830    {
831  1 install(this.localXarExtensiontId1, "wiki", this.contextUser);
832   
833  1 verifyHasAdminRight(2);
834   
835    // uninstall
836   
837  1 uninstall(this.localXarExtensiontId1, "wiki");
838   
839  1 verifyHasAdminRight(3);
840   
841    // validate
842   
843    // space.page belong to several extensions
844  1 XWikiDocument page =
845    this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"), getXWikiContext());
846   
847  1 Assert.assertFalse("Document wiki.space.page has been removed from the database", page.isNew());
848   
849    // space1.page1 only belong to the uninstalled extension
850  1 XWikiDocument page1 =
851    this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space1", "page1"), getXWikiContext());
852   
853  1 Assert.assertTrue("Document wiki.space1.page1 has not been removed from the database", page1.isNew());
854    }
855   
 
856  1 toggle @Test
857    public void testUninstallMandatory() throws Throwable
858    {
859    // register a mandatory document initializer
860  1 MandatoryDocumentInitializer mandatoryInitializer =
861    this.componentManager.registerMockComponent(MandatoryDocumentInitializer.class, "space.page");
862   
863  1 when(mandatoryInitializer.updateDocument(any(XWikiDocument.class))).thenReturn(true);
864   
865  1 install(this.localXarExtensiontId1, "wiki", this.contextUser);
866   
867  1 verifyHasAdminRight(2);
868   
869    // uninstall
870   
871  1 uninstall(this.localXarExtensiontId1, "wiki");
872   
873  1 verifyHasAdminRight(3);
874   
875    // validate
876   
877  1 XWikiDocument page =
878    this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"), getXWikiContext());
879   
880  1 Assert.assertFalse("Document wiki.space.page has been removed from the database", page.isNew());
881    }
882   
 
883  1 toggle @Test
884    public void testUninstallExtensionWithCommonDocumentOnWiki() throws Throwable
885    {
886  1 install(this.collisionextension1, "wiki", this.contextUser);
887  1 install(this.collisionextension2, "wiki", this.contextUser);
888   
889    // uninstall
890   
891  1 uninstall(this.collisionextension1, "wiki");
892   
893  1 XWikiDocument page = this.oldcore.getSpyXWiki()
894    .getDocument(new DocumentReference("wiki", "samespace", "samepage"), getXWikiContext());
895   
896  1 assertFalse(page.isNew());
897    }
898   
 
899  1 toggle @Test
900    public void testUninstallExtensionWithCommonDocumentOnRoot() throws Throwable
901    {
902  1 install(this.collisionextension1, null, this.contextUser);
903  1 install(this.collisionextension2, null, this.contextUser);
904    }
905   
 
906  1 toggle @Test
907    public void testUninstallExtensionWithCommonDocumentOnRootAndWiki() throws Throwable
908    {
909  1 install(this.collisionextension1, "wiki", this.contextUser);
910   
911  1 install(this.collisionextension2, null, this.contextUser);
912    }
913   
 
914  1 toggle @Test
915    public void testUninstallExtensionWithCommonDocumentOnWikiAndRoot() throws Throwable
916    {
917  1 install(this.collisionextension1, null, this.contextUser);
918  1 install(this.collisionextension2, "wiki", this.contextUser);
919    }
920   
 
921  1 toggle @Test
922    public void testInstallOnRoot() throws Throwable
923    {
924  1 doReturn(Arrays.asList("wiki1", "wiki2")).when(this.oldcore.getWikiDescriptorManager()).getAllIds();
925   
926    // install
927   
928  1 install(this.localXarExtensiontId1, null, this.contextUser);
929   
930  1 verifyHasAdminRight(2);
931   
932    // validate
933   
934  1 XWikiDocument pageWiki1 = this.oldcore.getSpyXWiki()
935    .getDocument(new DocumentReference("wiki1", "space1", "page1"), getXWikiContext());
936   
937  1 Assert.assertFalse(pageWiki1.isNew());
938   
939  1 XWikiDocument pageWiki2 = this.oldcore.getSpyXWiki()
940    .getDocument(new DocumentReference("wiki2", "space1", "page1"), getXWikiContext());
941   
942  1 Assert.assertFalse(pageWiki2.isNew());
943   
944  1 XWikiDocument overwrittenpage = this.oldcore.getSpyXWiki()
945    .getDocument(new DocumentReference("wiki1", "space", "overwrittenpage"), getXWikiContext());
946   
947  1 Assert.assertFalse(overwrittenpage.isNew());
948  1 Assert.assertEquals("1.1", overwrittenpage.getVersion());
949   
950    // uninstall
951   
952  1 uninstall(this.localXarExtensiontId1, null);
953   
954  1 verifyHasAdminRight(3);
955   
956    // validate
957   
958  1 pageWiki1 = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space1", "page1"),
959    getXWikiContext());
960   
961  1 Assert.assertTrue("Document wiki1:space1.page1 hasn't been removed from the database", pageWiki1.isNew());
962   
963  1 pageWiki2 = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki2", "space1", "page1"),
964    getXWikiContext());
965   
966  1 Assert.assertTrue(pageWiki2.isNew());
967    }
968   
969    // rights check
970   
971    // install
972   
 
973  1 toggle @Test(expected = InstallException.class)
974    public void testInstallOnRootWithoutAdminRights() throws Throwable
975    {
976  1 setHasNoAdminRight();
977   
978  1 install(this.localXarExtensiontId1, null, this.contextUser);
979   
980  0 verifyHasAdminRight(1);
981    }
982   
983    // uninstall
984   
 
985  1 toggle @Test(expected = InstallException.class)
986    public void testInstallOnWikiWithoutAdminRights() throws Throwable
987    {
988  1 setHasNoAdminRight();
989   
990  1 install(this.localXarExtensiontId1, "wiki", this.contextUser);
991   
992  0 verifyHasAdminRight(1);
993    }
994   
 
995  1 toggle @Test(expected = InstallException.class)
996    public void testInstallOnUnsupportedNamespace() throws Throwable
997    {
998  1 installOnNamespace(this.localXarExtensiontId1, "unsupportednamespace", this.contextUser);
999    }
1000   
1001    // uninstall
1002   
 
1003  1 toggle @Test(expected = UninstallException.class)
1004    public void testUninstallOnRootWithoutAdminRights() throws Throwable
1005    {
1006  1 doReturn(Arrays.asList("wiki1", "wiki2")).when(this.oldcore.getWikiDescriptorManager()).getAllIds();
1007   
1008  1 install(this.localXarExtensiontId1, null, this.contextUser);
1009   
1010  1 verifyHasAdminRight(2);
1011   
1012  1 setHasNoAdminRight();
1013   
1014  1 uninstall(this.localXarExtensiontId1, null);
1015   
1016  0 verifyHasAdminRight(3);
1017    }
1018   
 
1019  1 toggle @Test(expected = UninstallException.class)
1020    public void testUninstallOnWikiWithoutAdminRights() throws Throwable
1021    {
1022  1 install(this.localXarExtensiontId1, "wiki", this.contextUser);
1023   
1024  1 verifyHasAdminRight(2);
1025   
1026  1 setHasNoAdminRight();
1027   
1028  1 uninstall(this.localXarExtensiontId1, "wiki");
1029   
1030  0 verifyHasAdminRight(3);
1031    }
1032   
 
1033  1 toggle @Test
1034    public void testInstallOnNamespaceThenOnRoot() throws Throwable
1035    {
1036  1 doReturn(Arrays.asList("wiki1", "wiki2")).when(this.oldcore.getWikiDescriptorManager()).getAllIds();
1037   
1038    // install on wiki
1039   
1040  1 install(this.localXarExtensiontId1, "wiki1", this.contextUser);
1041   
1042    // validate
1043   
1044  1 XWikiDocument pageWiki1 =
1045    this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "page"), getXWikiContext());
1046   
1047  1 Assert.assertFalse(pageWiki1.isNew());
1048  1 Assert.assertEquals("1.1", pageWiki1.getVersion());
1049   
1050  1 pageWiki1.setContent("modified content");
1051  1 this.oldcore.getSpyXWiki().saveDocument(pageWiki1, getXWikiContext());
1052   
1053  1 pageWiki1 =
1054    this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "page"), getXWikiContext());
1055   
1056  1 Assert.assertFalse(pageWiki1.isNew());
1057  1 Assert.assertEquals("2.1", pageWiki1.getVersion());
1058   
1059    // install on root
1060   
1061  1 install(this.localXarExtensiontId1, null, this.contextUser);
1062   
1063    // validate
1064   
1065  1 pageWiki1 =
1066    this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "page"), getXWikiContext());
1067   
1068  1 Assert.assertFalse(pageWiki1.isNew());
1069  1 Assert.assertEquals("2.1", pageWiki1.getVersion());
1070    // Make sure modifications are kept
1071  1 Assert.assertEquals("modified content", pageWiki1.getContent());
1072    }
1073   
 
1074  1 toggle @Test
1075    public void testInstallOnNamespaceThenUpgradeOnRoot() throws Throwable
1076    {
1077  1 doReturn(Arrays.asList("wiki1", "wiki2")).when(this.oldcore.getWikiDescriptorManager()).getAllIds();
1078   
1079    // install on wiki
1080   
1081  1 install(this.localXarExtensiontId1, "wiki1", this.contextUser);
1082   
1083    // validate
1084   
1085  1 XWikiDocument pageWiki1 =
1086    this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "page"), getXWikiContext());
1087   
1088  1 Assert.assertFalse(pageWiki1.isNew());
1089  1 Assert.assertEquals("1.1", pageWiki1.getVersion());
1090   
1091  1 pageWiki1 = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "samespace", "samepage"),
1092    getXWikiContext());
1093   
1094  1 Assert.assertFalse(pageWiki1.isNew());
1095  1 Assert.assertEquals("1.1", pageWiki1.getVersion());
1096   
1097  1 XWikiDocument pageWiki2 =
1098    this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki2", "space", "page"), getXWikiContext());
1099   
1100  1 Assert.assertTrue(pageWiki2.isNew());
1101   
1102  1 pageWiki2 = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki2", "samespace", "samepage"),
1103    getXWikiContext());
1104   
1105  1 Assert.assertTrue(pageWiki2.isNew());
1106   
1107    // install on root
1108   
1109  1 install(this.localXarExtensiontId2, null, this.contextUser);
1110   
1111    // validate
1112   
1113  1 pageWiki1 =
1114    this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "page"), getXWikiContext());
1115   
1116  1 Assert.assertFalse(pageWiki1.isNew());
1117  1 Assert.assertEquals("2.1", pageWiki1.getVersion());
1118   
1119  1 pageWiki1 = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "samespace", "samepage"),
1120    getXWikiContext());
1121   
1122  1 Assert.assertFalse(pageWiki1.isNew());
1123  1 Assert.assertEquals("1.1", pageWiki1.getVersion());
1124   
1125  1 pageWiki2 =
1126    this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki2", "space", "page"), getXWikiContext());
1127   
1128  1 Assert.assertFalse(pageWiki2.isNew());
1129  1 Assert.assertEquals("1.1", pageWiki1.getVersion());
1130   
1131  1 pageWiki2 = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki2", "samespace", "samepage"),
1132    getXWikiContext());
1133   
1134  1 Assert.assertFalse(pageWiki2.isNew());
1135  1 Assert.assertEquals("1.1", pageWiki2.getVersion());
1136    }
1137   
 
1138  1 toggle @Test
1139    public void testInstallOnWikiWithOnlyAdminRight() throws Throwable
1140    {
1141  1 XWikiDocument existingDocument = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
1142  1 BaseObject object = new BaseObject();
1143  1 object.setXClassReference(new DocumentReference("wiki", "space", "class"));
1144  1 existingDocument.addXObject(object);
1145  1 existingDocument.setCreatorReference(new DocumentReference("wiki", "space", "existingcreator"));
1146  1 this.oldcore.getSpyXWiki().saveDocument(existingDocument, "", true, getXWikiContext());
1147   
1148    // install
1149   
1150  1 install(this.localXarExtensiontId1, "wiki", this.contextUser);
1151    }
1152   
 
1153  1 toggle @Test
1154    public void testCreateNewWiki() throws Throwable
1155    {
1156  1 doReturn(Arrays.asList("wiki1", "wiki2")).when(this.oldcore.getWikiDescriptorManager()).getAllIds();
1157   
1158  1 install(this.localXarExtensiontId1, null, this.contextUser);
1159   
1160  1 Assert.assertFalse(this.oldcore.getSpyXWiki()
1161    .getDocument(new DocumentReference("wiki1", "space1", "page1"), getXWikiContext()).isNew());
1162  1 Assert.assertFalse(this.oldcore.getSpyXWiki()
1163    .getDocument(new DocumentReference("wiki2", "space1", "page1"), getXWikiContext()).isNew());
1164  1 Assert.assertTrue(this.oldcore.getSpyXWiki()
1165    .getDocument(new DocumentReference("newwiki", "space1", "page1"), getXWikiContext()).isNew());
1166   
1167  1 this.observation.notify(new WikiCreatingEvent("newwiki"), null, this.oldcore.getXWikiContext());
1168  1 this.observation.notify(new WikiCreatedEvent("newwiki"), null, this.oldcore.getXWikiContext());
1169   
1170  1 Assert.assertFalse(this.oldcore.getSpyXWiki()
1171    .getDocument(new DocumentReference("wiki1", "space1", "page1"), getXWikiContext()).isNew());
1172  1 Assert.assertFalse(this.oldcore.getSpyXWiki()
1173    .getDocument(new DocumentReference("wiki2", "space1", "page1"), getXWikiContext()).isNew());
1174  1 Assert.assertFalse(this.oldcore.getSpyXWiki()
1175    .getDocument(new DocumentReference("newwiki", "space1", "page1"), getXWikiContext()).isNew());
1176    }
1177    }