@Test public void onEvent() throws ComponentLookupException, XWikiException { Utils.setComponentManager(this.mocker); DocumentReference docReference = new DocumentReference("xwiki", "Groups", "Group1"); XWikiContext context = mock(XWikiContext.class); XWiki xwiki = mock(XWiki.class); when(context.getWiki()).thenReturn(xwiki); XWikiDocument doc = mock(XWikiDocument.class); when(doc.getDocumentReference()).thenReturn(docReference); BaseObject groupObject = new BaseObject(); BaseObject rightsObject = new BaseObject(); when(doc.newXObject(eq(this.groupsClassReference), eq(context))).thenReturn(groupObject); when(doc.newXObject(eq(this.rightsClassReference), eq(context))).thenReturn(rightsObject); when(doc.getXObject(Group.CLASS_REFERENCE)).thenReturn(mock(BaseObject.class)); DocumentReference adminsDocReference = new DocumentReference("xwiki", "Groups", "Group1 Administrators"); XWikiDocument adminsDoc = mock(XWikiDocument.class); BaseObject adminsGroupObject = new BaseObject(); BaseObject adminsRightsObject = new BaseObject(); when(adminsDoc.newXObject(eq(this.groupsClassReference), eq(context))) .thenReturn(adminsGroupObject); when(adminsDoc.newXObject(eq(this.rightsClassReference), eq(context))) .thenReturn(adminsRightsObject); when(xwiki.getDocument(eq(adminsDocReference), eq(context))).thenReturn(adminsDoc); DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class); DocumentReference userReference = new DocumentReference("xwiki", "XWiki", "User"); when(dab.getCurrentUserReference()).thenReturn(userReference); this.mocker .getComponentUnderTest() .onEvent(new DocumentCreatingEvent(docReference), doc, context); Mockito.verify(xwiki).saveDocument(eq(adminsDoc), any(String.class), eq(true), eq(context)); Mockito.verify(xwiki, Mockito.never()) .saveDocument(eq(doc), any(String.class), any(Boolean.class), eq(context)); Assert.assertEquals(1, rightsObject.getIntValue("allow")); Assert.assertEquals("edit", rightsObject.getStringValue("levels")); Assert.assertEquals( "xwiki:Groups.Group1 Administrators", rightsObject.getLargeStringValue("groups")); Assert.assertEquals("", rightsObject.getLargeStringValue("users")); Assert.assertEquals(1, adminsRightsObject.getIntValue("allow")); Assert.assertEquals("edit", adminsRightsObject.getStringValue("levels")); Assert.assertEquals( "xwiki:Groups.Group1 Administrators", adminsRightsObject.getLargeStringValue("groups")); Assert.assertEquals("", adminsRightsObject.getLargeStringValue("users")); Assert.assertEquals("xwiki:Groups.Group1 Administrators", groupObject.getStringValue("member")); Assert.assertEquals("xwiki:XWiki.User", adminsGroupObject.getStringValue("member")); }
/** * Convert the old WorkspaceManager.WorkspaceCandidateMemberClass objects to the new candidacies * format. * * @param wikiId id of the wiki to upgrade * @throws XWikiException if problems occur */ private void upgradeWorkspaceCandidacies(String wikiId) throws XWikiException { XWikiContext xcontext = getXWikiContext(); XWiki xwiki = xcontext.getWiki(); // We need to get the document that holds the candidacies DocumentReference candidaciesDocumentReference = new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "XWikiAllGroup"); XWikiDocument candidaciesDocument = xwiki.getDocument(candidaciesDocumentReference, xcontext); // We need to get all the old candidacies DocumentReference oldCandidateClassReference = new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "WorkspaceCandidateMemberClass"); List<BaseObject> candidacyObjects = candidaciesDocument.getXObjects(oldCandidateClassReference); if (candidacyObjects != null) { DocumentReference newCandidateClassReference = new DocumentReference( wikiId, WikiCandidateMemberClassInitializer.DOCUMENT_SPACE, WikiCandidateMemberClassInitializer.DOCUMENT_NAME); for (BaseObject oldObject : candidacyObjects) { if (oldObject == null) { continue; } // Transform the candidacy to the new class BaseObject newObject = candidaciesDocument.newXObject(newCandidateClassReference, xcontext); newObject.setStringValue( WikiCandidateMemberClassInitializer.FIELD_TYPE, oldObject.getStringValue("type")); newObject.setStringValue( WikiCandidateMemberClassInitializer.FIELD_STATUS, oldObject.getStringValue("status")); newObject.setStringValue( WikiCandidateMemberClassInitializer.FIELD_USER, oldObject.getStringValue("userName")); newObject.setLargeStringValue( WikiCandidateMemberClassInitializer.FIELD_USER_COMMENT, oldObject.getLargeStringValue("userComment")); newObject.setStringValue( WikiCandidateMemberClassInitializer.FIELD_ADMIN, oldObject.getStringValue("reviewer")); newObject.setLargeStringValue( WikiCandidateMemberClassInitializer.FIELD_ADMIN_COMMENT, oldObject.getLargeStringValue("reviewerComment")); newObject.setLargeStringValue( WikiCandidateMemberClassInitializer.FIELD_ADMIN_PRIVATE_COMMENT, oldObject.getLargeStringValue("reviewerPrivateComment")); newObject.setDateValue( WikiCandidateMemberClassInitializer.FIELD_DATE_OF_CREATION, oldObject.getDateValue("date")); newObject.setDateValue( WikiCandidateMemberClassInitializer.FIELD_DATE_OF_CLOSURE, oldObject.getDateValue("resolutionDate")); // Remove the old object candidaciesDocument.removeXObject(oldObject); } // Save xwiki.saveDocument( candidaciesDocument, "Upgrade candidacies from the old Workspace Application to the " + "new Wiki Application.", xcontext); } }