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

File DocumentTest.java

 

Code metrics

6
103
8
1
256
180
12
0.12
12.88
8
1.5

Classes

Class Line # Actions
DocumentTest 47 103 0% 12 1
0.99145399.1%
 

Contributing tests

This file is covered by 8 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.api;
21   
22    import java.util.List;
23   
24    import org.junit.Rule;
25    import org.junit.Test;
26    import org.xwiki.model.reference.DocumentReference;
27   
28    import com.xpn.xwiki.XWikiContext;
29    import com.xpn.xwiki.XWikiException;
30    import com.xpn.xwiki.doc.XWikiDocument;
31    import com.xpn.xwiki.objects.BaseObject;
32    import com.xpn.xwiki.objects.BaseProperty;
33    import com.xpn.xwiki.test.MockitoOldcoreRule;
34    import com.xpn.xwiki.test.reference.ReferenceComponentList;
35    import com.xpn.xwiki.user.api.XWikiRightService;
36   
37    import static com.xpn.xwiki.test.mockito.OldcoreMatchers.anyXWikiContext;
38    import static org.junit.Assert.assertEquals;
39    import static org.junit.Assert.assertNull;
40    import static org.junit.Assert.assertTrue;
41    import static org.junit.Assert.fail;
42    import static org.mockito.ArgumentMatchers.any;
43    import static org.mockito.ArgumentMatchers.eq;
44    import static org.mockito.Mockito.when;
45   
46    @ReferenceComponentList
 
47    public class DocumentTest
48    {
49    @Rule
50    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule();
51   
 
52  1 toggle @Test
53    public void testToStringReturnsFullName()
54    {
55  1 XWikiDocument doc = new XWikiDocument(new DocumentReference("Wiki", "Space", "Page"));
56  1 assertEquals("Space.Page", new Document(doc, new XWikiContext()).toString());
57  1 assertEquals("Main.WebHome", new Document(new XWikiDocument(), new XWikiContext()).toString());
58    }
59   
 
60  1 toggle @Test
61    public void testGetObjects() throws XWikiException
62    {
63  1 XWikiContext context = new XWikiContext();
64  1 XWikiDocument doc = new XWikiDocument(new DocumentReference("Wiki", "Space", "Page"));
65   
66  1 doc.getXClass().addNumberField("prop", "prop", 5, "long");
67  1 BaseObject obj = (BaseObject) doc.getXClass().newObject(context);
68  1 obj.setLongValue("prop", 1);
69  1 doc.addObject(doc.getFullName(), obj);
70   
71  1 assertEquals(obj, doc.getObject(doc.getFullName(), "prop", "1"));
72  1 assertNull(doc.getObject(doc.getFullName(), "prop", "2"));
73   
74  1 Document adoc = new Document(doc, context);
75  1 List<Object> lst = adoc.getObjects(adoc.getFullName(), "prop", "1");
76  1 assertEquals(1, lst.size());
77  1 assertEquals(obj, lst.get(0).getBaseObject());
78   
79  1 lst = adoc.getObjects(adoc.getFullName(), "prop", "0");
80  1 assertEquals(0, lst.size());
81   
82  1 lst = adoc.getObjects(adoc.getFullName());
83  1 assertEquals(1, lst.size());
84    }
85   
 
86  1 toggle @Test
87    public void testRemoveObjectDoesntCauseDataLoss() throws XWikiException
88    {
89    // Setup comment class
90  1 XWikiDocument commentDocument = new XWikiDocument(new DocumentReference("wiki", "XWiki", "XWikiComments"));
91  1 commentDocument.getXClass().addTextAreaField("comment", "comment", 60, 20);
92  1 this.oldcore.getSpyXWiki().saveDocument(commentDocument, this.oldcore.getXWikiContext());
93   
94    // Setup document
95  1 XWikiDocument xdoc = new XWikiDocument(new DocumentReference("wiki", "Space", "Page"));
96   
97  11 for (int i = 0; i < 10; ++i) {
98  10 xdoc.newXObject(commentDocument.getDocumentReference(), this.oldcore.getXWikiContext());
99    }
100   
101  1 Document adoc = xdoc.newDocument(this.oldcore.getXWikiContext());
102   
103  1 for (Object obj : adoc.getObjects("XWiki.XWikiComments")) {
104  10 obj.set("comment", "Comment");
105  10 if (obj.getNumber() == 4) {
106  1 adoc.removeObject(obj);
107    }
108    }
109   
110    // Let's make sure the original document wasn't changed
111  1 for (BaseObject obj : xdoc.getXObjects(commentDocument.getDocumentReference())) {
112  10 assertNull(obj.get("comment"));
113    }
114   
115    // Let's make sure the cloned document was changed everywhere
116  1 for (BaseObject obj : adoc.getDoc().getXObjects(commentDocument.getDocumentReference())) {
117  10 if (obj != null) {
118  9 assertEquals("Comment", ((BaseProperty) obj.get("comment")).getValue());
119    }
120    }
121    }
122   
 
123  1 toggle @Test
124    public void testSaveAsAuthorUsesGuestIfDroppedPermissions() throws XWikiException
125    {
126  1 XWikiDocument xdoc = new XWikiDocument(new DocumentReference("wiki", "Space", "Page"));
127   
128  1 when(this.oldcore.getMockRightService().hasAccessLevel(eq("edit"), eq("XWiki.Alice"), any(),
129    anyXWikiContext())).thenReturn(true);
130  1 when(this.oldcore.getMockRightService().hasAccessLevel(eq("edit"), eq("XWikiGuest"), any(),
131    anyXWikiContext())).thenReturn(false);
132   
133  1 Document doc = xdoc.newDocument(this.oldcore.getXWikiContext());
134  1 this.oldcore.getXWikiContext().setDoc(xdoc);
135   
136    // Alice is the author.
137  1 xdoc.setAuthorReference(new DocumentReference("wiki", "XWiki", "Alice"));
138  1 xdoc.setContentAuthorReference(xdoc.getAuthorReference());
139   
140    // Bob is the viewer
141  1 this.oldcore.getXWikiContext().setUser("XWiki.Bob");
142   
143  1 doc.saveAsAuthor();
144   
145  1 this.oldcore.getXWikiContext().dropPermissions();
146  1 try {
147  1 doc.saveAsAuthor();
148  0 fail("saveAsAuthor did not throw an exception after dropPermissions() had been called.");
149    } catch (XWikiException e) {
150  1 assertTrue("Wrong error message when trying to save a document after calling dropPermissions()",
151    e.getMessage().contains("Access denied; user XWikiGuest, acting through script in "
152    + "document Space.Page cannot save document Space.Page"));
153    }
154   
155  1 assertEquals("After dropping permissions and attempting to save a document,"
156    + "the user was permanantly switched to guest.", "XWiki.Bob", this.oldcore.getXWikiContext().getUser());
157    }
158   
 
159  1 toggle @Test
160    public void testUser()
161    {
162  1 XWikiDocument xdoc = new XWikiDocument(new DocumentReference("Wiki", "Space", "Page"));
163  1 Document document = xdoc.newDocument(this.oldcore.getXWikiContext());
164   
165  1 assertEquals(XWikiRightService.GUEST_USER_FULLNAME, document.getCreator());
166  1 assertEquals(XWikiRightService.GUEST_USER_FULLNAME, document.getAuthor());
167  1 assertEquals(XWikiRightService.GUEST_USER_FULLNAME, document.getContentAuthor());
168    }
169   
 
170  1 toggle @Test
171    public void testChangeAuthorWhenModifyingDocumentContent()
172    {
173  1 XWikiDocument xdoc = new XWikiDocument(new DocumentReference("wiki0", "Space", "Page"));
174  1 xdoc.setAuthorReference(new DocumentReference("wiki1", "XWiki", "initialauthor"));
175  1 xdoc.setContentAuthorReference(new DocumentReference("wiki1", "XWiki", "initialcontentauthor"));
176  1 xdoc.setCreatorReference(new DocumentReference("wiki1", "XWiki", "initialcreator"));
177   
178  1 this.oldcore.getXWikiContext().setUserReference(new DocumentReference("wiki2", "XWiki", "contextuser"));
179   
180  1 Document document = xdoc.newDocument(this.oldcore.getXWikiContext());
181   
182  1 assertEquals(new DocumentReference("wiki1", "XWiki", "initialauthor"), document.getAuthorReference());
183  1 assertEquals(new DocumentReference("wiki1", "XWiki", "initialcontentauthor"),
184    document.getContentAuthorReference());
185  1 assertEquals(new DocumentReference("wiki1", "XWiki", "initialcreator"), document.getCreatorReference());
186   
187  1 document.setContent("new content");
188   
189  1 assertEquals(new DocumentReference("wiki2", "XWiki", "contextuser"), document.getAuthorReference());
190  1 assertEquals(new DocumentReference("wiki2", "XWiki", "contextuser"), document.getContentAuthorReference());
191  1 assertEquals(new DocumentReference("wiki1", "XWiki", "initialcreator"), document.getCreatorReference());
192    }
193   
 
194  1 toggle @Test
195    public void testChangeAuthorWhenModifyingObjectProperty() throws XWikiException
196    {
197  1 XWikiDocument xdoc = new XWikiDocument(new DocumentReference("wiki0", "Space", "Page"));
198  1 xdoc.setAuthorReference(new DocumentReference("wiki1", "XWiki", "initialauthor"));
199  1 xdoc.setContentAuthorReference(new DocumentReference("wiki1", "XWiki", "initialcontentauthor"));
200  1 xdoc.setCreatorReference(new DocumentReference("wiki1", "XWiki", "initialcreator"));
201   
202  1 xdoc.getXClass().addTextField("key", "Key", 30);
203  1 xdoc.newXObject(xdoc.getDocumentReference(), this.oldcore.getXWikiContext());
204   
205  1 xdoc.setContentDirty(false);
206  1 this.oldcore.getSpyXWiki().saveDocument(xdoc, this.oldcore.getXWikiContext());
207   
208  1 this.oldcore.getXWikiContext().setUserReference(new DocumentReference("wiki2", "XWiki", "contextuser"));
209   
210  1 Document document = xdoc.newDocument(this.oldcore.getXWikiContext());
211   
212  1 assertEquals(new DocumentReference("wiki1", "XWiki", "initialauthor"), document.getAuthorReference());
213  1 assertEquals(new DocumentReference("wiki1", "XWiki", "initialcontentauthor"),
214    document.getContentAuthorReference());
215  1 assertEquals(new DocumentReference("wiki1", "XWiki", "initialcreator"), document.getCreatorReference());
216   
217  1 Object obj = document.getObject(xdoc.getPrefixedFullName());
218  1 obj.set("key", "value");
219   
220  1 assertEquals(new DocumentReference("wiki2", "XWiki", "contextuser"), document.getAuthorReference());
221  1 assertEquals(new DocumentReference("wiki1", "XWiki", "initialcontentauthor"),
222    document.getContentAuthorReference());
223  1 assertEquals(new DocumentReference("wiki1", "XWiki", "initialcreator"), document.getCreatorReference());
224    }
225   
 
226  1 toggle @Test
227    public void testChangeAuthorWhenModifyingDocumentProperty() throws XWikiException
228    {
229  1 XWikiDocument xdoc = new XWikiDocument(new DocumentReference("wiki0", "Space", "Page"));
230  1 xdoc.setAuthorReference(new DocumentReference("wiki1", "XWiki", "initialauthor"));
231  1 xdoc.setContentAuthorReference(new DocumentReference("wiki1", "XWiki", "initialcontentauthor"));
232  1 xdoc.setCreatorReference(new DocumentReference("wiki1", "XWiki", "initialcreator"));
233   
234  1 xdoc.getXClass().addTextField("key", "Key", 30);
235  1 xdoc.newXObject(xdoc.getDocumentReference(), this.oldcore.getXWikiContext());
236   
237  1 xdoc.setContentDirty(false);
238  1 this.oldcore.getSpyXWiki().saveDocument(xdoc, this.oldcore.getXWikiContext());
239   
240  1 this.oldcore.getXWikiContext().setUserReference(new DocumentReference("wiki2", "XWiki", "contextuser"));
241   
242  1 Document document = xdoc.newDocument(this.oldcore.getXWikiContext());
243   
244  1 assertEquals(new DocumentReference("wiki1", "XWiki", "initialauthor"), document.getAuthorReference());
245  1 assertEquals(new DocumentReference("wiki1", "XWiki", "initialcontentauthor"),
246    document.getContentAuthorReference());
247  1 assertEquals(new DocumentReference("wiki1", "XWiki", "initialcreator"), document.getCreatorReference());
248   
249  1 document.set("key", "value");
250   
251  1 assertEquals(new DocumentReference("wiki2", "XWiki", "contextuser"), document.getAuthorReference());
252  1 assertEquals(new DocumentReference("wiki1", "XWiki", "initialcontentauthor"),
253    document.getContentAuthorReference());
254  1 assertEquals(new DocumentReference("wiki1", "XWiki", "initialcreator"), document.getCreatorReference());
255    }
256    }