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

File XWikiTest.java

 

Code metrics

8
212
30
1
581
421
35
0.17
7.07
30
1.17

Classes

Class Line # Actions
XWikiTest 76 212 0% 35 4
0.98498.4%
 

Contributing tests

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