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

File DocumentTest.java

 

Code metrics

6
107
8
1
273
193
12
0.11
13.38
8
1.5

Classes

Class Line # Actions
DocumentTest 52 107 0% 12 1
0.991735599.2%
 

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