| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
| |
|
| 99.1% |
Uncovered Elements: 1 (117) |
Complexity: 12 |
Complexity Density: 0.12 |
|
| 47 |
|
public class DocumentTest |
| 48 |
|
{ |
| 49 |
|
@Rule |
| 50 |
|
public MockitoOldcoreRule oldcore = new MockitoOldcoreRule(); |
| 51 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 52 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
| 60 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (22) |
Complexity: 4 |
Complexity Density: 0.25 |
1PASS
|
|
| 86 |
1 |
@Test... |
| 87 |
|
public void testRemoveObjectDoesntCauseDataLoss() throws XWikiException |
| 88 |
|
{ |
| 89 |
|
|
| 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 |
|
|
| 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 |
|
|
| 111 |
1 |
for (BaseObject obj : xdoc.getXObjects(commentDocument.getDocumentReference())) { |
| 112 |
10 |
assertNull(obj.get("comment")); |
| 113 |
|
} |
| 114 |
|
|
| 115 |
|
|
| 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 |
|
|
| |
|
| 93.3% |
Uncovered Elements: 1 (15) |
Complexity: 2 |
Complexity Density: 0.13 |
1PASS
|
|
| 123 |
1 |
@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 |
|
|
| 137 |
1 |
xdoc.setAuthorReference(new DocumentReference("wiki", "XWiki", "Alice")); |
| 138 |
1 |
xdoc.setContentAuthorReference(xdoc.getAuthorReference()); |
| 139 |
|
|
| 140 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 159 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
| 170 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
| 194 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
| 226 |
1 |
@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 |
|
} |