1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki

File XWikiTest.java

 

Code metrics

8
225
33
1
629
447
38
0.17
6.82
33
1.15

Classes

Class Line # Actions
XWikiTest 84 225 0% 38 4
0.984962498.5%
 

Contributing tests

This file is covered by 22 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;
21   
22    import java.io.ByteArrayInputStream;
23    import java.net.URL;
24    import java.util.ArrayList;
25    import java.util.Arrays;
26    import java.util.Collections;
27    import java.util.Date;
28    import java.util.Enumeration;
29    import java.util.HashMap;
30    import java.util.Locale;
31    import java.util.Map;
32   
33    import javax.servlet.http.Cookie;
34   
35    import org.apache.commons.collections4.IteratorUtils;
36    import org.jmock.Mock;
37    import org.jmock.core.Invocation;
38    import org.jmock.core.stub.CustomStub;
39    import org.junit.Assert;
40    import org.mockito.Mockito;
41    import org.xwiki.bridge.event.DocumentCreatedEvent;
42    import org.xwiki.bridge.event.DocumentCreatingEvent;
43    import org.xwiki.bridge.event.DocumentDeletedEvent;
44    import org.xwiki.bridge.event.DocumentDeletingEvent;
45    import org.xwiki.configuration.ConfigurationSource;
46    import org.xwiki.localization.LocalizationContext;
47    import org.xwiki.model.reference.DocumentReference;
48    import org.xwiki.model.reference.ObjectReference;
49    import org.xwiki.model.reference.WikiReference;
50    import org.xwiki.observation.EventListener;
51    import org.xwiki.observation.ObservationManager;
52    import org.xwiki.rendering.syntax.Syntax;
53    import org.xwiki.wiki.descriptor.WikiDescriptor;
54    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
55   
56    import com.xpn.xwiki.doc.XWikiAttachment;
57    import com.xpn.xwiki.doc.XWikiDocument;
58    import com.xpn.xwiki.objects.BaseObject;
59    import com.xpn.xwiki.objects.BaseProperty;
60    import com.xpn.xwiki.objects.StringProperty;
61    import com.xpn.xwiki.objects.classes.PropertyClass;
62    import com.xpn.xwiki.store.XWikiAttachmentStoreInterface;
63    import com.xpn.xwiki.store.XWikiHibernateStore;
64    import com.xpn.xwiki.store.XWikiHibernateVersioningStore;
65    import com.xpn.xwiki.store.XWikiStoreInterface;
66    import com.xpn.xwiki.store.XWikiVersioningStoreInterface;
67    import com.xpn.xwiki.test.AbstractBridgedXWikiComponentTestCase;
68    import com.xpn.xwiki.user.api.XWikiAuthService;
69    import com.xpn.xwiki.user.api.XWikiRightService;
70    import com.xpn.xwiki.user.impl.xwiki.XWikiGroupServiceImpl;
71    import com.xpn.xwiki.web.XWikiRequest;
72    import com.xpn.xwiki.web.XWikiServletRequest;
73    import com.xpn.xwiki.web.XWikiServletRequestStub;
74   
75    import static org.mockito.ArgumentMatchers.any;
76    import static org.mockito.ArgumentMatchers.anyString;
77    import static org.mockito.Mockito.times;
78   
79    /**
80    * Unit tests for {@link com.xpn.xwiki.XWiki}.
81    *
82    * @version $Id: ceb5f91356db8f40120e229248f5036a6e99ac17 $
83    */
 
84    public class XWikiTest extends AbstractBridgedXWikiComponentTestCase
85    {
86    private XWikiDocument document;
87   
88    private XWiki xwiki;
89   
90    private Mock mockXWikiStore;
91   
92    private Mock mockXWikiVersioningStore;
93   
94    private Mock mockWikiDescriptorManager;
95   
96    private Map<String, XWikiDocument> docs = new HashMap<String, XWikiDocument>();
97   
 
98  22 toggle @Override
99    protected void setUp() throws Exception
100    {
101  22 super.setUp();
102   
103  22 this.document = new XWikiDocument(new DocumentReference("Wiki", "MilkyWay", "Fidis"));
104  22 getContext().setRequest(new XWikiServletRequestStub());
105  22 getContext().setURL(new URL("http://localhost:8080/xwiki/bin/view/MilkyWay/Fidis"));
106   
107  22 Mock mockLocalizationContext = registerMockComponent(LocalizationContext.class);
108  22 mockLocalizationContext.stubs().method("getCurrentLocale").will(returnValue(Locale.ROOT));
109   
110  22 this.mockWikiDescriptorManager = registerMockComponent(WikiDescriptorManager.class);
111  22 this.mockWikiDescriptorManager.stubs().method("getCurrentWikiId").will(
112    new CustomStub("Implements WikiDescriptorManager.getCurrentWikiId")
113    {
 
114  660 toggle @Override
115    public Object invoke(Invocation invocation) throws Throwable
116    {
117  660 return getContext().getWikiId();
118    }
119    });
120   
121  22 this.xwiki = new XWiki(new XWikiConfig(), getContext())
122    {
123    // Avoid all the error at XWiki initialization
 
124  34 toggle @Override
125    public String getXWikiPreference(String prefname, String defaultValue, XWikiContext context)
126    {
127  34 if (prefname.equals("plugins") || prefname.startsWith("macros_")) {
128  22 return defaultValue;
129    } else {
130  12 return super.getXWikiPreference(prefname, defaultValue, context);
131    }
132    }
133    };
134  22 getContext().setWiki(this.xwiki);
135   
136    // Ensure that no Velocity Templates are going to be used when executing Velocity since otherwise
137    // the Velocity init would fail (since by default the macros.vm templates wouldn't be found as we're
138    // not providing it in our unit test resources).
139  22 getConfigurationSource().setProperty("xwiki.render.velocity.macrolist", "");
140   
141  22 this.mockXWikiStore =
142    mock(XWikiHibernateStore.class, new Class[] {XWiki.class, XWikiContext.class}, new Object[] {this.xwiki,
143    getContext()});
144  22 this.mockXWikiStore.stubs().method("loadXWikiDoc").will(
145    new CustomStub("Implements XWikiStoreInterface.loadXWikiDoc")
146    {
 
147  129 toggle @Override
148    public Object invoke(Invocation invocation) throws Throwable
149    {
150  129 XWikiDocument shallowDoc = (XWikiDocument) invocation.parameterValues.get(0);
151  129 if (XWikiTest.this.docs.containsKey(shallowDoc.getName())) {
152  82 return XWikiTest.this.docs.get(shallowDoc.getName());
153    } else {
154  47 return shallowDoc;
155    }
156    }
157    });
158  22 this.mockXWikiStore.stubs().method("saveXWikiDoc").will(
159    new CustomStub("Implements XWikiStoreInterface.saveXWikiDoc")
160    {
 
161  60 toggle @Override
162    public Object invoke(Invocation invocation) throws Throwable
163    {
164  60 XWikiDocument document = (XWikiDocument) invocation.parameterValues.get(0);
165  60 document.setNew(false);
166  60 document.setStore((XWikiStoreInterface) XWikiTest.this.mockXWikiStore.proxy());
167  60 XWikiTest.this.docs.put(document.getName(), document);
168  60 return null;
169    }
170    });
171  22 this.mockXWikiStore.stubs().method("deleteXWikiDoc").will(
172    new CustomStub("Implements XWikiStoreInterface.deleteXWikiDoc")
173    {
 
174  1 toggle @Override
175    public Object invoke(Invocation invocation) throws Throwable
176    {
177  1 XWikiDocument document = (XWikiDocument) invocation.parameterValues.get(0);
178  1 XWikiTest.this.docs.remove(document.getName());
179  1 return null;
180    }
181    });
182  22 this.mockXWikiStore.stubs().method("executeRead").will(
183    new CustomStub("Implements XWikiStoreInterface.executeRead")
184    {
 
185  26 toggle @Override
186    public Object invoke(Invocation invocation) throws Throwable
187    {
188  26 return Collections.emptyList();
189    }
190    });
191  22 this.mockXWikiStore.stubs().method("getTranslationList").will(returnValue(Collections.EMPTY_LIST));
192  22 this.mockXWikiStore.stubs().method("exists").will(returnValue(true));
193   
194  22 this.mockXWikiVersioningStore =
195    mock(XWikiHibernateVersioningStore.class, new Class[] {XWiki.class, XWikiContext.class}, new Object[] {
196    this.xwiki, getContext()});
197  22 this.mockXWikiVersioningStore.stubs().method("getXWikiDocumentArchive").will(returnValue(null));
198  22 this.mockXWikiVersioningStore.stubs().method("resetRCSArchive").will(returnValue(null));
199   
200  22 this.xwiki.setStore((XWikiStoreInterface) this.mockXWikiStore.proxy());
201  22 this.xwiki.setVersioningStore((XWikiVersioningStoreInterface) this.mockXWikiVersioningStore.proxy());
202  22 this.xwiki.saveDocument(this.document, getContext());
203   
204  22 this.document.setCreator("Condor");
205  22 this.document.setAuthor("Albatross");
206   
207  22 this.xwiki.saveDocument(this.document, getContext());
208    }
209   
 
210  1 toggle public void testUserNotAddedByDefaultToXWikiAllGroupWhenThisGroupImplicit() throws Exception
211    {
212    //given
213  1 getConfigurationSource().setProperty("xwiki.authentication.group.allgroupimplicit", "1");
214   
215  1 XWikiGroupServiceImpl xWikiGroupService = new XWikiGroupServiceImpl();
216  1 xwiki.setGroupService(xWikiGroupService);
217   
218  1 XWiki spyXWiki = Mockito.spy(xwiki);
219   
220    //when
221  1 spyXWiki.setUserDefaultGroup("XWiki.user1", getContext());
222   
223    //then
224  1 Mockito.verify(spyXWiki, times(0)).addUserToGroup(anyString(), anyString(), any(XWikiContext.class));
225    }
226   
 
227  1 toggle public void testUserAddedToXWikiAllGroupWhenItsSpecifiedByConfigurationRegardlessXWikiAllGroupIsImplicit() throws Exception
228    {
229    //given
230  1 getConfigurationSource().setProperty("xwiki.authentication.group.allgroupimplicit", "1");
231  1 getConfigurationSource().setProperty("xwiki.users.initialGroups", "XWiki.XWikiAllGroup");
232   
233  1 XWikiGroupServiceImpl xWikiGroupService = new XWikiGroupServiceImpl();
234  1 xwiki.setGroupService(xWikiGroupService);
235   
236  1 XWiki spyXWiki = Mockito.spy(xwiki);
237   
238    //when
239  1 spyXWiki.setUserDefaultGroup("XWiki.user1", getContext());
240   
241    //then
242  1 Mockito.verify(spyXWiki, times(1)).addUserToGroup("XWiki.user1", "XWiki.XWikiAllGroup", getContext());
243    }
244   
 
245  1 toggle public void testAuthorAfterDocumentCopy() throws XWikiException
246    {
247  1 DocumentReference copyReference =
248    new DocumentReference("Lyre", this.document.getDocumentReference().getLastSpaceReference());
249  1 DocumentReference author = this.document.getAuthorReference();
250  1 this.xwiki.copyDocument(this.document.getDocumentReference(), copyReference, getContext());
251  1 XWikiDocument copy = this.xwiki.getDocument(copyReference, getContext());
252   
253  1 assertEquals(author, copy.getAuthorReference());
254    }
255   
 
256  1 toggle public void testCreatorAfterDocumentCopy() throws XWikiException
257    {
258  1 DocumentReference copyReference =
259    new DocumentReference("Sirius", this.document.getDocumentReference().getLastSpaceReference());
260  1 DocumentReference creator = this.document.getCreatorReference();
261  1 this.xwiki.copyDocument(this.document.getDocumentReference(), copyReference, getContext());
262  1 XWikiDocument copy = this.xwiki.getDocument(copyReference, getContext());
263   
264  1 assertEquals(creator, copy.getCreatorReference());
265    }
266   
 
267  1 toggle public void testCreationDateAfterDocumentCopy() throws XWikiException, InterruptedException
268    {
269  1 Date sourceCreationDate = this.document.getCreationDate();
270  1 Thread.sleep(1000);
271  1 DocumentReference copyReference =
272    new DocumentReference(this.document.getDocumentReference().getName() + "Copy", this.document
273    .getDocumentReference().getLastSpaceReference());
274  1 this.xwiki.copyDocument(this.document.getDocumentReference(), copyReference, getContext());
275  1 XWikiDocument copy = this.xwiki.getDocument(copyReference, getContext());
276   
277  1 assertTrue(copy.getCreationDate().equals(sourceCreationDate));
278    }
279   
 
280  1 toggle public void testParseTemplateConsidersObjectField() throws XWikiException
281    {
282  1 DocumentReference skinReference = new DocumentReference("xwiki", "XWiki", "XWikiSkins");
283  1 XWikiDocument skinClass = new XWikiDocument(skinReference);
284  1 skinClass.getXClass().addTextAreaField("template.vm", "template", 80, 20);
285  1 this.xwiki.saveDocument(skinClass, getContext());
286   
287  1 DocumentReference mySkinReference = new DocumentReference("xwiki", "XWiki", "Skin");
288  1 XWikiDocument skinDocument = new XWikiDocument(mySkinReference);
289  1 BaseObject obj = skinDocument.newXObject(skinReference, getContext());
290  1 obj.setLargeStringValue("template.vm", "parsing a field");
291  1 this.xwiki.saveDocument(skinDocument, getContext());
292   
293  1 getContext().put("skin", "XWiki.Skin");
294  1 assertEquals("XWiki.Skin", this.xwiki.getSkin(getContext()));
295  1 assertFalse(this.xwiki.getDocument(mySkinReference, getContext()).isNew());
296  1 assertEquals(skinDocument, this.xwiki.getDocument(mySkinReference, getContext()));
297  1 assertEquals("parsing a field", this.xwiki.parseTemplate("template.vm", getContext()));
298    }
299   
300    /**
301    * See XWIKI-2096
302    */
 
303  1 toggle public void testParseTemplateConsidersAttachment() throws XWikiException
304    {
305  1 XWikiDocument skin = new XWikiDocument(new DocumentReference("Wiki", "XWiki", "Skin"));
306  1 XWikiAttachment attachment = new XWikiAttachment();
307  1 skin.getAttachmentList().add(attachment);
308  1 attachment.setContent("parsing an attachment".getBytes());
309  1 attachment.setFilename("template.vm");
310  1 attachment.setDoc(skin);
311  1 this.xwiki.saveDocument(skin, getContext());
312  1 getContext().put("skin", "XWiki.Skin");
313   
314  1 assertEquals("XWiki.Skin", this.xwiki.getSkin(getContext()));
315  1 assertFalse(this.xwiki.getDocument("XWiki.Skin", getContext()).isNew());
316  1 assertEquals(skin, this.xwiki.getDocument("XWiki.Skin", getContext()));
317  1 assertEquals("parsing an attachment", this.xwiki.parseTemplate("template.vm", getContext()));
318    }
319   
320    /**
321    * See XWIKI-2098
322    */
 
323  1 toggle public void testParseTemplateConsidersObjectFieldBeforeAttachment() throws Exception
324    {
325  1 DocumentReference skinReference = new DocumentReference("xwiki", "XWiki", "XWikiSkins");
326  1 XWikiDocument skinClass = new XWikiDocument(skinReference);
327  1 skinClass.getXClass().addTextAreaField("template.vm", "template", 80, 20);
328  1 this.xwiki.saveDocument(skinClass, getContext());
329   
330  1 DocumentReference mySkinReference = new DocumentReference("xwiki", "XWiki", "Skin");
331  1 XWikiDocument skinDocument = new XWikiDocument(mySkinReference);
332  1 BaseObject obj = skinDocument.newXObject(skinReference, getContext());
333  1 obj.setLargeStringValue("template.vm", "parsing a field");
334  1 XWikiAttachment attachment = new XWikiAttachment();
335  1 skinDocument.getAttachmentList().add(attachment);
336  1 attachment.setContent(new ByteArrayInputStream("parsing an attachment".getBytes()));
337  1 attachment.setFilename("template.vm");
338  1 attachment.setDoc(skinDocument);
339  1 this.xwiki.saveDocument(skinDocument, getContext());
340  1 getContext().put("skin", "XWiki.Skin");
341  1 assertEquals("XWiki.Skin", this.xwiki.getSkin(getContext()));
342  1 assertFalse(this.xwiki.getDocument(mySkinReference, getContext()).isNew());
343  1 assertEquals(skinDocument, this.xwiki.getDocument(mySkinReference, getContext()));
344  1 assertEquals("parsing a field", this.xwiki.parseTemplate("template.vm", getContext()));
345    }
346   
 
347  1 toggle public void testClearNameWithoutStripDotsWithoutAscii()
348    {
349  1 assertEquals("ee{&.txt", this.xwiki.clearName("\u00E9\u00EA{&.txt", false, false, getContext()));
350    }
351   
 
352  1 toggle public void testClearNameWithoutStripDotsWithAscii()
353    {
354  1 assertEquals("ee.txt", this.xwiki.clearName("\u00E9\u00EA{&.txt", false, true, getContext()));
355    }
356   
 
357  1 toggle public void testClearNameWithStripDotsWithoutAscii()
358    {
359  1 assertEquals("ee{&txt", this.xwiki.clearName("\u00E9\u00EA{&.txt", true, false, getContext()));
360    }
361   
 
362  1 toggle public void testClearNameWithStripDotsWithAscii()
363    {
364  1 assertEquals("eetxt", this.xwiki.clearName("\u00E9\u00EA{&.txt", true, true, getContext()));
365    }
366   
367    /**
368    * We only verify here that the saveDocument API calls the Observation component.
369    */
 
370  1 toggle public void testSaveDocumentSendsObservationEvents() throws Exception
371    {
372  1 Mock mockListener = mock(EventListener.class);
373  1 mockListener.stubs().method("getName").will(returnValue("testlistener"));
374  1 DocumentReference ref = new DocumentReference("xwikitest", "Some", "Document");
375  1 mockListener.expects(once()).method("getEvents")
376    .will(returnValue(Arrays.asList(new DocumentCreatedEvent(ref), new DocumentCreatingEvent(ref))));
377   
378  1 ObservationManager om = getComponentManager().getInstance(ObservationManager.class);
379  1 om.addListener((EventListener) mockListener.proxy());
380   
381  1 XWikiDocument document = new XWikiDocument(new DocumentReference("xwikitest", "Some", "Document"));
382  1 document.setContent("the content");
383   
384    // Ensure that the onEvent method has been called before and after the save
385  1 mockListener.expects(once()).method("onEvent").with(isA(DocumentCreatingEvent.class), same(document),
386    isA(XWikiContext.class));
387  1 mockListener.expects(once()).method("onEvent").with(isA(DocumentCreatedEvent.class), same(document),
388    isA(XWikiContext.class));
389   
390  1 this.xwiki.saveDocument(document, getContext());
391    }
392   
393    /**
394    * We only verify here that the deleteDocument API calls the Observation component.
395    */
 
396  1 toggle public void testDeleteDocumentSendsObservationEvents() throws Exception
397    {
398  1 Mock mockListener = mock(EventListener.class);
399  1 mockListener.stubs().method("getName").will(returnValue("testlistener"));
400  1 DocumentReference ref = new DocumentReference("xwikitest", "Another", "Document");
401  1 mockListener.expects(once()).method("getEvents")
402    .will(returnValue(Arrays.asList(new DocumentDeletedEvent(ref), new DocumentDeletingEvent(ref))));
403   
404  1 ObservationManager om = getComponentManager().getInstance(ObservationManager.class);
405  1 om.addListener((EventListener) mockListener.proxy());
406   
407  1 XWikiDocument document = new XWikiDocument(new DocumentReference("xwikitest", "Another", "Document"));
408  1 document.setContent("the content");
409   
410    // Not expectation on mock Listener since we're not subscribed to Document save events
411   
412  1 this.xwiki.saveDocument(document, getContext());
413   
414    // Ensure that the onEvent method has been called before and after the deletion
415  1 mockListener.expects(once()).method("onEvent")
416    .with(isA(DocumentDeletingEvent.class), isA(XWikiDocument.class), isA(XWikiContext.class));
417  1 mockListener.expects(once()).method("onEvent")
418    .with(isA(DocumentDeletedEvent.class), isA(XWikiDocument.class), isA(XWikiContext.class));
419   
420  1 this.xwiki.deleteDocument(document, false, getContext());
421    }
422   
 
423  1 toggle public void testLanguageSelection() throws Exception
424    {
425  1 getContext().setRequest(new XWikiServletRequest(null)
426    {
 
427  1 toggle @SuppressWarnings("unchecked")
428    @Override
429    public Enumeration getLocales()
430    {
431  1 ArrayList<Locale> locales = new ArrayList<Locale>();
432  1 locales.add(new Locale("*"));
433  1 locales.add(new Locale("en_US"));
434  1 locales.add(new Locale("fr"));
435  1 locales.add(new Locale("de"));
436  1 return IteratorUtils.asEnumeration(locales.iterator());
437    }
438   
 
439  1 toggle @Override
440    public String getHeader(String s)
441    {
442  1 if ("language".equals(s)) {
443  0 return null;
444    }
445  1 return "en";
446    }
447   
 
448  0 toggle @Override
449    public Cookie getCookie(String cookieName)
450    {
451  0 return null;
452    }
453    });
454   
455    // Set the wiki to multilingual mode.
456  1 getConfigurationSource().setProperty("multilingual", "1");
457   
458  1 assertEquals("fr", this.xwiki.getLanguagePreference(getContext()));
459    }
460   
461    /**
462    * XWIKI-8469: Bad default of 1 in XWiki.isMultilingual instead of 0 (when no XWikiPreferences object exists)
463    */
 
464  1 toggle public void testIsMultilingualDefaultFalse() throws Exception
465    {
466  1 assertFalse(this.xwiki.isMultiLingual(getContext()));
467    }
468   
 
469  1 toggle public void testGetCurrentContentSyntaxId()
470    {
471  1 XWikiDocument doc1 = new XWikiDocument();
472  1 doc1.setSyntax(Syntax.CREOLE_1_0);
473  1 XWikiDocument doc2 = new XWikiDocument();
474  1 doc2.setSyntax(Syntax.PLAIN_1_0);
475   
476  1 assertNull(this.xwiki.getCurrentContentSyntaxId(null, getContext()));
477  1 assertEquals("syntaxId", this.xwiki.getCurrentContentSyntaxId("syntaxId", getContext()));
478   
479  1 getContext().setDoc(doc1);
480   
481  1 assertEquals(Syntax.CREOLE_1_0.toIdString(), this.xwiki.getCurrentContentSyntaxId(null, getContext()));
482   
483  1 getContext().put("sdoc", doc2);
484   
485  1 assertEquals(Syntax.PLAIN_1_0.toIdString(), this.xwiki.getCurrentContentSyntaxId(null, getContext()));
486    }
487   
488    /**
489    * Check that the user validation feature works when the validation key is stored both as plain text and as a hashed
490    * field.
491    *
492    * @throws Exception when any exception occurs inside XWiki
493    */
 
494  1 toggle public void testValidationKeyStorage() throws Exception
495    {
496  1 XWikiContext context = getContext();
497  1 context.setLanguage("en");
498   
499    // Prepare the request
500  1 Mock request = mock(XWikiRequest.class);
501  1 request.stubs().method("getParameter").with(eq("xwikiname")).will(returnValue("TestUser"));
502  1 request.stubs().method("getParameter").with(eq("validkey")).will(returnValue("plaintextkey"));
503  1 context.setRequest((XWikiRequest) request.proxy());
504   
505    // Prepare the user profile
506  1 XWikiDocument testUser = new XWikiDocument(new DocumentReference("Wiki", "XWiki", "TestUser"));
507  1 BaseObject userObject = (BaseObject) this.xwiki.getUserClass(context).newObject(context);
508  1 testUser.addObject("XWiki.XWikiUsers", userObject);
509  1 this.xwiki.saveDocument(testUser, context);
510   
511    // Check with a correct plaintext key
512  1 BaseProperty validationKey = new StringProperty();
513  1 validationKey.setValue("plaintextkey");
514  1 userObject.safeput("validkey", validationKey);
515   
516  1 assertEquals(0, this.xwiki.validateUser(false, getContext()));
517   
518    // Check with an incorrect plaintext key
519  1 validationKey.setValue("wrong key");
520   
521  1 assertEquals(-1, this.xwiki.validateUser(false, getContext()));
522   
523    // Check with a correct hashed key
524  1 validationKey = ((PropertyClass) this.xwiki.getUserClass(context).get("validkey")).fromString("plaintextkey");
525  1 assertTrue(validationKey.getValue().toString().startsWith("hash:"));
526  1 userObject.safeput("validkey", validationKey);
527   
528  1 assertEquals(0, this.xwiki.validateUser(false, getContext()));
529   
530    // Check with an incorrect hashed key
531  1 validationKey = ((PropertyClass) this.xwiki.getUserClass(context).get("validkey")).fromString("wrong key");
532  1 assertTrue(validationKey.getValue().toString().startsWith("hash:"));
533  1 userObject.safeput("validkey", validationKey);
534   
535  1 assertEquals(-1, this.xwiki.validateUser(false, getContext()));
536    }
537   
538    /**
539    * Tests that XWiki.XWikiPreferences page is not saved each time XWiki is initialized.
540    *
541    * @throws Exception when any exception occurs inside XWiki
542    */
 
543  1 toggle public void testGetPrefsClass() throws Exception
544    {
545  1 Mock mockStore = registerMockComponent(XWikiStoreInterface.class);
546  1 this.xwiki.setStore((XWikiStoreInterface) mockStore.proxy());
547   
548  1 XWikiDocument prefsDoc = new XWikiDocument(new DocumentReference("xwiki", "XWiki", "XWikiPreferences"));
549  1 final Map<DocumentReference, XWikiDocument> documents = new HashMap<DocumentReference, XWikiDocument>();
550  1 documents.put(prefsDoc.getDocumentReference(), prefsDoc);
551   
552  1 mockStore.expects(atLeastOnce()).method("loadXWikiDoc").with(NOT_NULL, same(getContext()))
553    .will(new CustomStub("Implements XWikiStoreInterface.loadXWikiDoc")
554    {
 
555  4 toggle @Override
556    public Object invoke(Invocation invocation) throws Throwable
557    {
558  4 XWikiDocument document = (XWikiDocument) invocation.parameterValues.get(0);
559  4 if (!documents.containsKey(document.getDocumentReference())) {
560  1 documents.put(document.getDocumentReference(), document);
561    } else {
562  3 document = documents.get(document.getDocumentReference());
563    }
564  4 return document;
565    }
566    });
567  1 mockStore.expects(once()).method("saveXWikiDoc").with(same(prefsDoc), same(getContext()));
568   
569  1 this.xwiki.getPrefsClass(getContext());
570  1 this.xwiki.getPrefsClass(getContext());
571    }
572   
573    /**
574    * XWIKI-12398: No layout for login page in a closed wiki
575    */
 
576  1 toggle public void testSkinResourcesAreAlwaysAllowed() throws XWikiException
577    {
578    // /skin/resources/icons/xwiki/noavatar.png
579  1 XWikiDocument doc1 =
580    new XWikiDocument(new DocumentReference("xwiki", Arrays.asList("resources", "icons", "xwiki"),
581    "noavatar.png"));
582    // /skin/skins/flamingo/style.css
583  1 XWikiDocument doc2 =
584    new XWikiDocument(new DocumentReference("xwiki", Arrays.asList("skins", "flamingo", "xwiki"), "style.css"));
585   
586    // Register a mock authService just so that we limit the test to a minimum.
587  1 Mock mockAuthService = mock(XWikiAuthService.class);
588  1 mockAuthService.expects(exactly(2)).method("checkAuth").with(same(getContext())).will(returnValue(null));
589  1 this.xwiki.setAuthService((XWikiAuthService) mockAuthService.proxy());
590   
591    // Register a mock rights service and make sure it is never called to validate the test's results.
592  1 Mock mockRightService = mock(XWikiRightService.class);
593  1 mockRightService.expects(
594    never("Skin action resources inside the 'skins' and 'resources' folders"
595    + " should never be checked for rights.")).method("checkAccess");
596  1 this.xwiki.setRightService((XWikiRightService) mockRightService.proxy());
597   
598    // Verify the results.
599  1 Assert.assertTrue(this.xwiki.checkAccess("skin", doc1, getContext()));
600  1 Assert.assertTrue(this.xwiki.checkAccess("skin", doc2, getContext()));
601    }
602   
 
603  1 toggle public void testCheckActiveSuperadmin() throws Exception
604    {
605    // Make sure the check for superadmin stops before looking at the configuration.
606  1 Mock mockConfiguration = registerMockComponent(ConfigurationSource.class, "wiki");
607  1 mockConfiguration.expects(never()).method("getProperty").with(eq("auth_active_check"), eq(String.class));
608   
609  1 int isUserActive = this.xwiki.checkActive(XWikiRightService.SUPERADMIN_USER_FULLNAME, this.getContext());
610   
611  1 Assert.assertEquals(1, isUserActive);
612    }
613   
614    /**
615    * XWIKI-14300: Superadmin is locked out of subwikis with "AUTHENTICATION ACTIVE CHECK" enabled
616    */
 
617  1 toggle public void testCheckActivePrefixedSuperadmin() throws Exception
618    {
619    // Make sure the check for superadmin stops before looking at the configuration.
620  1 Mock mockConfiguration = registerMockComponent(ConfigurationSource.class, "wiki");
621  1 mockConfiguration.expects(never()).method("getProperty").with(eq("auth_active_check"), eq(String.class));
622   
623    // In a subwiki, the superadmin always logs in as a global user.
624  1 int isUserActive =
625    this.xwiki.checkActive("xwiki:" + XWikiRightService.SUPERADMIN_USER_FULLNAME, this.getContext());
626   
627  1 Assert.assertEquals(1, isUserActive);
628    }
629    }