1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.wiki.script

File WikiManagerScriptServiceTest.java

 

Code metrics

0
219
38
1
599
432
38
0.17
5.76
38
1

Classes

Class Line # Actions
WikiManagerScriptServiceTest 66 219 0% 38 0
1.0100%
 

Contributing tests

This file is covered by 33 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.wiki.script;
21   
22    import static org.junit.Assert.assertEquals;
23    import static org.junit.Assert.assertFalse;
24    import static org.junit.Assert.assertNull;
25    import static org.junit.Assert.assertTrue;
26    import static org.mockito.ArgumentMatchers.eq;
27    import static org.mockito.Mockito.doThrow;
28    import static org.mockito.Mockito.mock;
29    import static org.mockito.Mockito.never;
30    import static org.mockito.Mockito.times;
31    import static org.mockito.Mockito.verify;
32    import static org.mockito.Mockito.when;
33   
34    import java.util.ArrayList;
35    import java.util.Arrays;
36    import java.util.Collection;
37   
38    import javax.inject.Provider;
39   
40    import org.apache.commons.lang3.StringUtils;
41    import org.junit.Before;
42    import org.junit.Rule;
43    import org.junit.Test;
44    import org.xwiki.component.util.DefaultParameterizedType;
45    import org.xwiki.context.Execution;
46    import org.xwiki.context.ExecutionContext;
47    import org.xwiki.model.reference.DocumentReference;
48    import org.xwiki.model.reference.EntityReferenceSerializer;
49    import org.xwiki.model.reference.WikiReference;
50    import org.xwiki.script.service.ScriptServiceManager;
51    import org.xwiki.security.authorization.AccessDeniedException;
52    import org.xwiki.security.authorization.AuthorizationManager;
53    import org.xwiki.security.authorization.Right;
54    import org.xwiki.test.mockito.MockitoComponentMockingRule;
55    import org.xwiki.url.internal.standard.StandardURLConfiguration;
56    import org.xwiki.wiki.configuration.WikiConfiguration;
57    import org.xwiki.wiki.descriptor.WikiDescriptor;
58    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
59    import org.xwiki.wiki.internal.descriptor.document.WikiDescriptorDocumentHelper;
60    import org.xwiki.wiki.manager.WikiManager;
61    import org.xwiki.wiki.manager.WikiManagerException;
62   
63    import com.xpn.xwiki.XWikiContext;
64    import com.xpn.xwiki.doc.XWikiDocument;
65   
 
66    public class WikiManagerScriptServiceTest
67    {
68    @Rule
69    public MockitoComponentMockingRule<WikiManagerScriptService> mocker =
70    new MockitoComponentMockingRule<WikiManagerScriptService>(WikiManagerScriptService.class);
71   
72    private WikiManager wikiManager;
73   
74    private WikiDescriptorManager wikiDescriptorManager;
75   
76    private Provider<XWikiContext> xcontextProvider;
77   
78    private Execution execution;
79   
80    private AuthorizationManager authorizationManager;
81   
82    private EntityReferenceSerializer<String> entityReferenceSerializer;
83   
84    private ScriptServiceManager scriptServiceManager;
85   
86    private StandardURLConfiguration standardURLConfiguration;
87   
88    private WikiConfiguration wikiConfiguration;
89   
90    private XWikiContext xcontext;
91   
92    private ExecutionContext executionContext;
93   
94    private DocumentReference currentUserRef;
95   
96    private XWikiDocument currentDoc;
97   
98    private WikiDescriptorDocumentHelper wikiDescriptorDocumentHelper;
99   
 
100  33 toggle @Before
101    public void setUp() throws Exception
102    {
103  33 wikiManager = mocker.getInstance(WikiManager.class);
104  33 wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
105  33 authorizationManager = mocker.getInstance(AuthorizationManager.class);
106  33 scriptServiceManager = mocker.getInstance(ScriptServiceManager.class);
107  33 entityReferenceSerializer =
108    mocker.getInstance(new DefaultParameterizedType(null, EntityReferenceSerializer.class, String.class));
109  33 standardURLConfiguration = mocker.getInstance(StandardURLConfiguration.class);
110  33 wikiConfiguration = mocker.getInstance(WikiConfiguration.class);
111  33 xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
112  33 xcontext = mock(XWikiContext.class);
113  33 when(xcontextProvider.get()).thenReturn(xcontext);
114  33 execution = mocker.getInstance(Execution.class);
115  33 executionContext = new ExecutionContext();
116  33 when(execution.getContext()).thenReturn(executionContext);
117   
118  33 currentUserRef = new DocumentReference("mainWiki", "XWiki", "User");
119  33 when(xcontext.getUserReference()).thenReturn(currentUserRef);
120   
121  33 currentDoc = mock(XWikiDocument.class);
122  33 when(xcontext.getDoc()).thenReturn(currentDoc);
123   
124  33 when(wikiDescriptorManager.getMainWikiId()).thenReturn("mainWiki");
125   
126  33 when(entityReferenceSerializer.serialize(currentUserRef)).thenReturn("mainWiki:XWiki.User");
127   
128  33 wikiDescriptorDocumentHelper = mocker.getInstance(WikiDescriptorDocumentHelper.class);
129    }
130   
131    /**
132    * @return the exception expected when the current script has the not the programing right
133    */
 
134  1 toggle private Exception currentScriptHasNotProgrammingRight() throws AccessDeniedException
135    {
136  1 DocumentReference authorDocRef = new DocumentReference("mainWiki", "XWiki", "Admin");
137  1 when(currentDoc.getAuthorReference()).thenReturn(authorDocRef);
138  1 DocumentReference currentDocRef = new DocumentReference("subwiki", "Test", "test");
139  1 when(currentDoc.getDocumentReference()).thenReturn(currentDocRef);
140   
141  1 Exception exception = new AccessDeniedException(Right.PROGRAM, authorDocRef, currentDocRef);
142  1 doThrow(exception).when(authorizationManager).checkAccess(Right.PROGRAM, authorDocRef, currentDocRef);
143   
144  1 return exception;
145    }
146   
147    /**
148    * @return the exception expected when the current user has the not the admin right
149    */
 
150  1 toggle private Exception currentUserHasNotProgrammingRight() throws AccessDeniedException
151    {
152  1 WikiReference wiki = new WikiReference("mainWiki");
153  1 Exception exception = new AccessDeniedException(Right.PROGRAM, currentUserRef, wiki);
154  1 doThrow(exception).when(authorizationManager).checkAccess(eq(Right.PROGRAM), eq(currentUserRef), eq(wiki));
155   
156  1 return exception;
157    }
158   
159    /**
160    * @return the exception expected when the current user has the not the 'create wiki' right
161    */
 
162  1 toggle private Exception currentUserHasNotCreateWikiRight() throws AccessDeniedException
163    {
164  1 WikiReference wiki = new WikiReference("mainWiki");
165  1 Exception exception = new AccessDeniedException(Right.CREATE_WIKI, currentUserRef, wiki);
166  1 doThrow(exception).when(authorizationManager).checkAccess(eq(Right.CREATE_WIKI), eq(currentUserRef), eq(wiki));
167   
168  1 return exception;
169    }
170   
171    /**
172    * @param wikiId the id of the wiki for which to get the descriptor
173    * @return the wiki descriptor document for the wiki identified by the given wikiId
174    */
 
175  2 toggle private DocumentReference getAndSetupDescriptorDocument(String wikiId)
176    {
177  2 DocumentReference descriptorDocument =
178    new DocumentReference("mainWiki", "XWiki", "XWikiServer" + StringUtils.capitalize(wikiId));
179  2 when(wikiDescriptorDocumentHelper.getDocumentReferenceFromId(wikiId)).thenReturn(descriptorDocument);
180   
181  2 return descriptorDocument;
182    }
183   
 
184  1 toggle @Test
185    public void get() throws Exception
186    {
187    // Test
188  1 mocker.getComponentUnderTest().get("template");
189    // Verify
190  1 verify(scriptServiceManager).get("wiki.template");
191    }
192   
 
193  1 toggle @Test
194    public void createWiki() throws Exception
195    {
196  1 WikiDescriptor descriptor = new WikiDescriptor("newiki", "alias");
197  1 when(wikiManager.create("newwiki", "alias", true)).thenReturn(descriptor);
198   
199  1 WikiDescriptor result = mocker.getComponentUnderTest().createWiki("newwiki", "alias", "userA", true);
200  1 assertEquals(descriptor, result);
201  1 assertEquals("userA", result.getOwnerId());
202  1 verify(wikiDescriptorManager).saveDescriptor(result);
203    }
204   
 
205  1 toggle @Test
206    public void createWikiWithoutPR() throws Exception
207    {
208  1 Exception exception = currentScriptHasNotProgrammingRight();
209   
210  1 WikiDescriptor result = mocker.getComponentUnderTest().createWiki("newwiki", "alias", "userA", true);
211  1 assertNull(result);
212  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
213    }
214   
 
215  1 toggle @Test
216    public void createWikiWithoutCreateWikiRight() throws Exception
217    {
218  1 Exception exception = currentUserHasNotCreateWikiRight();
219   
220  1 WikiDescriptor result = mocker.getComponentUnderTest().createWiki("newwiki", "alias", "userA", true);
221  1 assertNull(result);
222  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
223    }
224   
 
225  1 toggle @Test
226    public void createWikiNoFailOnExistWithoutPR() throws Exception
227    {
228  1 Exception exception = currentUserHasNotProgrammingRight();
229   
230  1 WikiDescriptor result = mocker.getComponentUnderTest().createWiki("newwiki", "alias", "userA", false);
231  1 assertNull(result);
232  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
233    }
234   
 
235  1 toggle @Test
236    public void createWikiError() throws Exception
237    {
238  1 Exception exception = new WikiManagerException("error on create");
239  1 when(wikiManager.create("newwiki", "alias", true)).thenThrow(exception);
240  1 WikiDescriptor result = mocker.getComponentUnderTest().createWiki("newwiki", "alias", "userA", true);
241  1 assertNull(result);
242  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
243    }
244   
 
245  1 toggle @Test
246    public void getByAlias() throws Exception
247    {
248  1 WikiDescriptor descriptor = new WikiDescriptor("wikiId", "wikiAlias");
249  1 when(wikiDescriptorManager.getByAlias("wikiAlias")).thenReturn(descriptor);
250   
251  1 WikiDescriptor result = mocker.getComponentUnderTest().getByAlias("wikiAlias");
252  1 assertEquals(descriptor, result);
253    }
254   
 
255  1 toggle @Test
256    public void getByAliasError() throws Exception
257    {
258  1 Exception exception = new WikiManagerException("error in getByAlias");
259  1 when(wikiDescriptorManager.getByAlias("wikiAlias")).thenThrow(exception);
260   
261  1 WikiDescriptor result = mocker.getComponentUnderTest().getByAlias("wikiAlias");
262  1 assertNull(result);
263  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
264    }
265   
 
266  1 toggle @Test
267    public void getById() throws Exception
268    {
269  1 WikiDescriptor descriptor = new WikiDescriptor("wikiId", "wikiAlias");
270  1 when(wikiDescriptorManager.getById("wikiId")).thenReturn(descriptor);
271   
272  1 WikiDescriptor result = mocker.getComponentUnderTest().getById("wikiId");
273  1 assertEquals(descriptor, result);
274    }
275   
 
276  1 toggle @Test
277    public void getByIdError() throws Exception
278    {
279  1 Exception exception = new WikiManagerException("error in getById");
280  1 when(wikiDescriptorManager.getById("wikiId")).thenThrow(exception);
281   
282  1 WikiDescriptor result = mocker.getComponentUnderTest().getById("wikiId");
283  1 assertNull(result);
284  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
285    }
286   
 
287  1 toggle @Test
288    public void getAll() throws Exception
289    {
290  1 WikiDescriptor descriptor1 = new WikiDescriptor("wikiId1", "wikiAlias1");
291  1 WikiDescriptor descriptor2 = new WikiDescriptor("wikiId2", "wikiAlias2");
292  1 Collection<WikiDescriptor> descriptors = new ArrayList<WikiDescriptor>();
293  1 descriptors.add(descriptor1);
294  1 descriptors.add(descriptor2);
295  1 when(wikiDescriptorManager.getAll()).thenReturn(descriptors);
296   
297  1 Collection<WikiDescriptor> result = mocker.getComponentUnderTest().getAll();
298  1 assertEquals(descriptors, result);
299    }
300   
 
301  1 toggle @Test
302    public void getAllIds() throws Exception
303    {
304  1 Collection<String> wikiIds = Arrays.asList("wikiId1", "wikiId2");
305  1 when(wikiDescriptorManager.getAllIds()).thenReturn(wikiIds);
306   
307  1 Collection<String> result = mocker.getComponentUnderTest().getAllIds();
308  1 assertEquals(wikiIds, result);
309    }
310   
 
311  1 toggle @Test
312    public void getAllError() throws Exception
313    {
314  1 Exception exception = new WikiManagerException("error in getAll");
315  1 when(wikiDescriptorManager.getAll()).thenThrow(exception);
316   
317  1 Collection<WikiDescriptor> result = mocker.getComponentUnderTest().getAll();
318  1 assertTrue(result.isEmpty());
319  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
320    }
321   
 
322  1 toggle @Test
323    public void getAllIdsError() throws Exception
324    {
325  1 Exception exception = new WikiManagerException("error in getAllIds");
326  1 when(wikiDescriptorManager.getAllIds()).thenThrow(exception);
327   
328  1 Collection<String> result = mocker.getComponentUnderTest().getAllIds();
329  1 assertTrue(result.isEmpty());
330  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
331    }
332   
 
333  1 toggle @Test
334    public void exists() throws Exception
335    {
336  1 when(wikiDescriptorManager.exists("wikiId")).thenReturn(true);
337  1 when(wikiDescriptorManager.exists("no")).thenReturn(false);
338   
339  1 assertTrue(mocker.getComponentUnderTest().exists("wikiId"));
340  1 assertFalse(mocker.getComponentUnderTest().exists("no"));
341    }
342   
 
343  1 toggle @Test
344    public void existsError() throws Exception
345    {
346  1 Exception exception = new WikiManagerException("error in exists");
347  1 when(wikiDescriptorManager.exists("wikiId")).thenThrow(exception);
348   
349  1 Boolean result = mocker.getComponentUnderTest().exists("wikiId");
350  1 assertNull(result);
351  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
352    }
353   
 
354  1 toggle @Test
355    public void idAvailable() throws Exception
356    {
357  1 when(wikiManager.idAvailable("wikiId")).thenReturn(true);
358  1 when(wikiManager.idAvailable("no")).thenReturn(false);
359   
360  1 assertTrue(mocker.getComponentUnderTest().idAvailable("wikiId"));
361  1 assertFalse(mocker.getComponentUnderTest().idAvailable("no"));
362    }
363   
 
364  1 toggle @Test
365    public void idAvailableError() throws Exception
366    {
367  1 Exception exception = new WikiManagerException("error in idAvailable");
368  1 when(wikiManager.idAvailable("wikiId")).thenThrow(exception);
369   
370  1 Boolean result = mocker.getComponentUnderTest().idAvailable("wikiId");
371  1 assertNull(result);
372  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
373  1 assertEquals(exception, mocker.getComponentUnderTest().getLastException());
374    }
375   
 
376  1 toggle @Test
377    public void getMainWikiDescriptor() throws Exception
378    {
379  1 WikiDescriptor descriptor = new WikiDescriptor("mainWiki", "wikiAlias");
380  1 when(wikiDescriptorManager.getMainWikiDescriptor()).thenReturn(descriptor);
381   
382  1 WikiDescriptor result = mocker.getComponentUnderTest().getMainWikiDescriptor();
383  1 assertEquals(descriptor, result);
384    }
385   
 
386  1 toggle @Test
387    public void getMainWikiDescriptorError() throws Exception
388    {
389  1 Exception exception = new WikiManagerException("error in getMainWikiDescriptor");
390  1 when(wikiDescriptorManager.getMainWikiDescriptor()).thenThrow(exception);
391   
392  1 WikiDescriptor result = mocker.getComponentUnderTest().getMainWikiDescriptor();
393  1 assertNull(result);
394  1 assertEquals(exception, mocker.getComponentUnderTest().getLastError());
395    }
396   
 
397  1 toggle @Test
398    public void getMainWikiId() throws Exception
399    {
400  1 String result = mocker.getComponentUnderTest().getMainWikiId();
401  1 assertEquals("mainWiki", result);
402    }
403   
 
404  1 toggle @Test
405    public void getCurrentWikiId() throws Exception
406    {
407  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("currentWiki");
408  1 String result = mocker.getComponentUnderTest().getCurrentWikiId();
409  1 assertEquals("currentWiki", result);
410    }
411   
 
412  1 toggle @Test
413    public void saveDescriptorWhenICanEditDescriptorDocument() throws Exception
414    {
415  1 WikiDescriptor oldDescriptor = new WikiDescriptor("wikiId", "wikiAlias");
416  1 oldDescriptor.setOwnerId("SomeUser");
417  1 when(wikiDescriptorManager.getById(oldDescriptor.getId())).thenReturn(oldDescriptor);
418   
419  1 DocumentReference wikiDescriptorDocRef = getAndSetupDescriptorDocument(oldDescriptor.getId());
420  1 when(this.authorizationManager.hasAccess(Right.EDIT, currentUserRef, wikiDescriptorDocRef)).thenReturn(true);
421   
422    // Changing some value, not the owner.
423  1 WikiDescriptor descriptor = new WikiDescriptor(oldDescriptor.getId(), "wikiAlias");
424  1 descriptor.setOwnerId(oldDescriptor.getOwnerId());
425  1 boolean result = mocker.getComponentUnderTest().saveDescriptor(descriptor);
426  1 assertTrue(result);
427   
428    // The descriptor has been saved
429  1 verify(wikiDescriptorManager).saveDescriptor(descriptor);
430    }
431   
 
432  1 toggle @Test
433    public void saveDescriptorWhenIAmOwner() throws Exception
434    {
435  1 WikiDescriptor oldDescriptor = mock(WikiDescriptor.class);
436  1 when(oldDescriptor.getId()).thenReturn("wikiId");
437  1 when(oldDescriptor.getOwnerId()).thenReturn("mainWiki:XWiki.User");
438  1 when(wikiDescriptorManager.getById(oldDescriptor.getId())).thenReturn(oldDescriptor);
439   
440    // Changing some value, not the owner.
441  1 WikiDescriptor descriptor = new WikiDescriptor(oldDescriptor.getId(), "wikiAlias");
442  1 descriptor.setOwnerId(oldDescriptor.getOwnerId());
443  1 boolean result = mocker.getComponentUnderTest().saveDescriptor(descriptor);
444  1 assertTrue(result);
445   
446    // The owner of the old descriptor was verified (once by us, once by the call).
447  1 verify(oldDescriptor, times(2)).getOwnerId();
448   
449    // The descriptor has been saved
450  1 verify(wikiDescriptorManager).saveDescriptor(descriptor);
451    }
452   
 
453  1 toggle @Test
454    public void saveDescriptorWhenIAmLocalAdmin() throws Exception
455    {
456  1 WikiDescriptor oldDescriptor = new WikiDescriptor("wikiId", "wikiAlias");
457  1 oldDescriptor.setOwnerId("SomeUser");
458  1 when(wikiDescriptorManager.getById(oldDescriptor.getId())).thenReturn(oldDescriptor);
459   
460    // Local admin.
461  1 when(authorizationManager.hasAccess(eq(Right.ADMIN), eq(currentUserRef), eq(new WikiReference("wikiId"))))
462    .thenReturn(true);
463   
464    // Changing some value, not the owner.
465  1 WikiDescriptor descriptor = new WikiDescriptor(oldDescriptor.getId(), "wikiAlias");
466  1 descriptor.setOwnerId(oldDescriptor.getOwnerId());
467  1 boolean result = mocker.getComponentUnderTest().saveDescriptor(descriptor);
468  1 assertTrue(result);
469   
470    // The right has been checked
471  1 verify(authorizationManager).hasAccess(eq(Right.ADMIN), eq(currentUserRef), eq(new WikiReference("wikiId")));
472    // The descriptor has been saved
473  1 verify(wikiDescriptorManager).saveDescriptor(descriptor);
474    }
475   
 
476  1 toggle @Test
477    public void saveDescriptorWhenIAmNotOwnerNorLocalAdminNorGlobalAdmin() throws Exception
478    {
479  1 WikiDescriptor oldDescriptor = new WikiDescriptor("wikiId", "wikiAlias");
480  1 oldDescriptor.setOwnerId("SomeUser");
481  1 when(wikiDescriptorManager.getById(oldDescriptor.getId())).thenReturn(oldDescriptor);
482   
483  1 when(authorizationManager.hasAccess(eq(Right.ADMIN), eq(currentUserRef), eq(new WikiReference("wikiId"))))
484    .thenReturn(false);
485   
486    // Changing some value, not the owner.
487  1 WikiDescriptor descriptor = new WikiDescriptor(oldDescriptor.getId(), "wikiAlias");
488  1 oldDescriptor.setOwnerId(oldDescriptor.getOwnerId());
489  1 boolean result = mocker.getComponentUnderTest().saveDescriptor(descriptor);
490  1 assertFalse(result);
491   
492    // The descriptor has not been saved
493  1 verify(wikiDescriptorManager, never()).saveDescriptor(descriptor);
494   
495  1 Exception exception = new AccessDeniedException(currentUserRef, new WikiReference("wikiId"));
496  1 assertEquals(exception.getMessage(), mocker.getComponentUnderTest().getLastError().getMessage());
497  1 assertEquals(exception.getClass(), mocker.getComponentUnderTest().getLastError().getClass());
498    }
499   
 
500  1 toggle @Test
501    public void saveDescriptorWhenIAmLocalAdminAndChangeOwner() throws Exception
502    {
503  1 WikiDescriptor oldDescriptor = new WikiDescriptor("wikiId", "wikiAlias");
504  1 oldDescriptor.setOwnerId("SomeUser");
505  1 when(wikiDescriptorManager.getById(oldDescriptor.getId())).thenReturn(oldDescriptor);
506   
507    // Changing the owner.
508  1 WikiDescriptor descriptor = new WikiDescriptor(oldDescriptor.getId(), "wikiAlias");
509  1 descriptor.setOwnerId("SomeOtherUserOrMyself");
510  1 boolean result = mocker.getComponentUnderTest().saveDescriptor(descriptor);
511  1 assertFalse(result);
512   
513    // The right has been checked
514  1 verify(authorizationManager).hasAccess(eq(Right.ADMIN), eq(currentUserRef), eq(new WikiReference("wikiId")));
515   
516    // The descriptor has not been saved
517  1 verify(wikiDescriptorManager, never()).saveDescriptor(descriptor);
518   
519  1 Exception expectedException = new AccessDeniedException(currentUserRef, new WikiReference("wikiId"));
520  1 assertEquals(expectedException.getMessage(), mocker.getComponentUnderTest().getLastError().getMessage());
521  1 assertEquals(expectedException.getClass(), mocker.getComponentUnderTest().getLastError().getClass());
522    }
523   
 
524  1 toggle @Test
525    public void saveDescriptorWhenICanEditDescriptorDocumentAndChangeOwner() throws Exception
526    {
527  1 WikiDescriptor oldDescriptor = new WikiDescriptor("wikiId", "wikiAlias");
528  1 oldDescriptor.setOwnerId("SomeUser");
529  1 when(wikiDescriptorManager.getById(oldDescriptor.getId())).thenReturn(oldDescriptor);
530   
531  1 DocumentReference wikiDescriptorDocRef = getAndSetupDescriptorDocument(oldDescriptor.getId());
532  1 when(this.authorizationManager.hasAccess(Right.EDIT, currentUserRef, wikiDescriptorDocRef)).thenReturn(true);
533   
534    // Changing the owner is possible, since I can directly edit the wiki descriptor anyway.
535  1 WikiDescriptor descriptor = new WikiDescriptor(oldDescriptor.getId(), "wikiAlias");
536  1 descriptor.setOwnerId("SomeOtherUserOrMyself");
537  1 boolean result = mocker.getComponentUnderTest().saveDescriptor(descriptor);
538  1 assertTrue(result);
539   
540    // The descriptor has been saved
541  1 verify(wikiDescriptorManager).saveDescriptor(descriptor);
542    }
543   
 
544  1 toggle @Test
545    public void saveDescriptorWhenDescriptorDidNotExist() throws Exception
546    {
547  1 WikiDescriptor descriptor = new WikiDescriptor("wikiId", "wikiAlias");
548  1 boolean result = mocker.getComponentUnderTest().saveDescriptor(descriptor);
549  1 assertFalse(result);
550   
551    // Verify the rights have been checked
552  1 verify(authorizationManager).hasAccess(eq(Right.ADMIN), eq(currentUserRef), eq(new WikiReference("mainWiki")));
553   
554    // The descriptor has not been saved
555  1 verify(wikiDescriptorManager, never()).saveDescriptor(descriptor);
556    }
557   
 
558  1 toggle @Test
559    public void saveDescriptorWhenDescriptorDidNotExistAndIAmGlobalAdmin() throws Exception
560    {
561  1 when(this.authorizationManager.hasAccess(Right.ADMIN, currentUserRef, new WikiReference("mainWiki")))
562    .thenReturn(true);
563   
564  1 WikiDescriptor descriptor = new WikiDescriptor("wikiId", "wikiAlias");
565  1 boolean result = mocker.getComponentUnderTest().saveDescriptor(descriptor);
566  1 assertTrue(result);
567   
568    // Verify the rights have been checked
569  1 verify(authorizationManager).hasAccess(eq(Right.ADMIN), eq(currentUserRef), eq(new WikiReference("mainWiki")));
570   
571    // The descriptor has been saved
572  1 verify(wikiDescriptorManager).saveDescriptor(descriptor);
573    }
574   
 
575  1 toggle @Test
576    public void isPathMode() throws Exception
577    {
578  1 when(standardURLConfiguration.isPathBasedMultiWiki()).thenReturn(true);
579  1 assertTrue(mocker.getComponentUnderTest().isPathMode());
580   
581  1 when(standardURLConfiguration.isPathBasedMultiWiki()).thenReturn(false);
582  1 assertFalse(mocker.getComponentUnderTest().isPathMode());
583    }
584   
 
585  1 toggle @Test
586    public void getAliasSuffix() throws Exception
587    {
588  1 when(wikiConfiguration.getAliasSuffix()).thenReturn("mysuffix.org");
589  1 assertEquals(mocker.getComponentUnderTest().getAliasSuffix(), "mysuffix.org");
590    }
591   
 
592  1 toggle @Test
593    public void getCurrentDescriptor() throws Exception
594    {
595  1 mocker.getComponentUnderTest().getCurrentWikiDescriptor();
596   
597  1 verify(wikiDescriptorManager).getCurrentWikiDescriptor();
598    }
599    }