@Test public void onEvent() throws ComponentLookupException, XWikiException { Utils.setComponentManager(this.mocker); DocumentReference docReference = new DocumentReference("xwiki", "Groups", "Group1"); Provider<XWikiContext> contextProvider = this.mocker.getInstance(XWikiContext.TYPE_PROVIDER); XWikiContext context = mock(XWikiContext.class); when(contextProvider.get()).thenReturn(context); 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(GroupSetupEventListenerTest.GROUPS_CLASS_REFERENCE), eq(context))) .thenReturn(groupObject); when(doc.newXObject(eq(GroupSetupEventListenerTest.RIGHTS_CLASS_REFERENCE), eq(context))) .thenReturn(rightsObject); when(doc.getXObject(Group.CLASS_REFERENCE)).thenReturn(mock(BaseObject.class)); DocumentReference adminsDocReference = new DocumentReference("xwiki", "Groups", "Group1 Managers"); XWikiDocument adminsDoc = mock(XWikiDocument.class); BaseObject adminsGroupObject = new BaseObject(); BaseObject adminsRightsObject = new BaseObject(); when(adminsDoc.newXObject(eq(GroupSetupEventListenerTest.GROUPS_CLASS_REFERENCE), eq(context))) .thenReturn(adminsGroupObject); when(adminsDoc.newXObject(eq(GroupSetupEventListenerTest.RIGHTS_CLASS_REFERENCE), 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 Managers", 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 Managers", adminsRightsObject.getLargeStringValue("groups")); Assert.assertEquals("", adminsRightsObject.getLargeStringValue("users")); Assert.assertEquals("xwiki:Groups.Group1 Managers", groupObject.getStringValue("member")); Assert.assertEquals("xwiki:XWiki.User", adminsGroupObject.getStringValue("member")); }
@Override public void onEvent(Event event, Object source, Object data) { XWikiDocument doc = (XWikiDocument) source; if (doc.getXObject(Group.CLASS_REFERENCE) == null) { return; } DocumentReference docReference = doc.getDocumentReference(); if ("PhenoTipsGroupTemplate".equals(docReference.getName())) { return; } XWikiContext context = this.xcontextProvider.get(); DocumentReference adminsReference = new DocumentReference(docReference.getName() + " Managers", docReference.getLastSpaceReference()); XWiki xwiki = context.getWiki(); try { XWikiDocument adminsDoc = xwiki.getDocument(adminsReference, context); setMembers(this.dab.getCurrentUserReference(), adminsDoc, context); setRights(adminsReference, adminsDoc, context); adminsDoc.setParentReference(new EntityReference(docReference)); xwiki.saveDocument(adminsDoc, "Automatically created managers group", true, context); setMembers(adminsReference, doc, context); setRights(adminsReference, doc, context); } catch (XWikiException ex) { this.logger.error("Failed to set up the new group [{}]: {}", docReference, ex.getMessage(), ex); } }
GroupSetupEventListener implements EventListener { @Override public void onEvent(Event event, Object source, Object data) { XWikiDocument doc = (XWikiDocument) source; if (doc.getXObject(Group.CLASS_REFERENCE) == null) { return; } DocumentReference docReference = doc.getDocumentReference(); if ("PhenoTipsGroupTemplate".equals(docReference.getName())) { return; } XWikiContext context = this.xcontextProvider.get(); DocumentReference adminsReference = new DocumentReference(docReference.getName() + " Managers", docReference.getLastSpaceReference()); XWiki xwiki = context.getWiki(); try { XWikiDocument adminsDoc = xwiki.getDocument(adminsReference, context); setMembers(this.dab.getCurrentUserReference(), adminsDoc, context); setRights(adminsReference, adminsDoc, context); adminsDoc.setParentReference(new EntityReference(docReference)); xwiki.saveDocument(adminsDoc, "Automatically created managers group", true, context); setMembers(adminsReference, doc, context); setRights(adminsReference, doc, context); } catch (XWikiException ex) { this.logger.error("Failed to set up the new group [{}]: {}", docReference, ex.getMessage(), ex); } } }
GroupSetupEventListener implements EventListener { @Override public void onEvent(Event event, Object source, Object data) { XWikiDocument doc = (XWikiDocument) source; if (doc.getXObject(Group.CLASS_REFERENCE) == null) { return; } DocumentReference docReference = doc.getDocumentReference(); if ("PhenoTipsGroupTemplate".equals(docReference.getName())) { return; } XWikiContext context = this.xcontextProvider.get(); DocumentReference adminsReference = new DocumentReference(docReference.getName() + " Managers", docReference.getLastSpaceReference()); XWiki xwiki = context.getWiki(); try { XWikiDocument adminsDoc = xwiki.getDocument(adminsReference, context); setMembers(this.dab.getCurrentUserReference(), adminsDoc, context); setRights(adminsReference, adminsDoc, context); adminsDoc.setParentReference(new EntityReference(docReference)); xwiki.saveDocument(adminsDoc, "Automatically created managers group", true, context); setMembers(adminsReference, doc, context); setRights(adminsReference, doc, context); } catch (XWikiException ex) { this.logger.error("Failed to set up the new group [{}]: {}", docReference, ex.getMessage(), ex); } }  }
GroupSetupEventListener implements EventListener { @Override public void onEvent(Event event, Object source, Object data) { XWikiDocument doc = (XWikiDocument) source; if (doc.getXObject(Group.CLASS_REFERENCE) == null) { return; } DocumentReference docReference = doc.getDocumentReference(); if ("PhenoTipsGroupTemplate".equals(docReference.getName())) { return; } XWikiContext context = this.xcontextProvider.get(); DocumentReference adminsReference = new DocumentReference(docReference.getName() + " Managers", docReference.getLastSpaceReference()); XWiki xwiki = context.getWiki(); try { XWikiDocument adminsDoc = xwiki.getDocument(adminsReference, context); setMembers(this.dab.getCurrentUserReference(), adminsDoc, context); setRights(adminsReference, adminsDoc, context); adminsDoc.setParentReference(new EntityReference(docReference)); xwiki.saveDocument(adminsDoc, "Automatically created managers group", true, context); setMembers(adminsReference, doc, context); setRights(adminsReference, doc, context); } catch (XWikiException ex) { this.logger.error("Failed to set up the new group [{}]: {}", docReference, ex.getMessage(), ex); } }  @Override String getName(); @Override List<Event> getEvents(); @Override void onEvent(Event event, Object source, Object data); }
GroupSetupEventListener implements EventListener { @Override public void onEvent(Event event, Object source, Object data) { XWikiDocument doc = (XWikiDocument) source; if (doc.getXObject(Group.CLASS_REFERENCE) == null) { return; } DocumentReference docReference = doc.getDocumentReference(); if ("PhenoTipsGroupTemplate".equals(docReference.getName())) { return; } XWikiContext context = this.xcontextProvider.get(); DocumentReference adminsReference = new DocumentReference(docReference.getName() + " Managers", docReference.getLastSpaceReference()); XWiki xwiki = context.getWiki(); try { XWikiDocument adminsDoc = xwiki.getDocument(adminsReference, context); setMembers(this.dab.getCurrentUserReference(), adminsDoc, context); setRights(adminsReference, adminsDoc, context); adminsDoc.setParentReference(new EntityReference(docReference)); xwiki.saveDocument(adminsDoc, "Automatically created managers group", true, context); setMembers(adminsReference, doc, context); setRights(adminsReference, doc, context); } catch (XWikiException ex) { this.logger.error("Failed to set up the new group [{}]: {}", docReference, ex.getMessage(), ex); } }  @Override String getName(); @Override List<Event> getEvents(); @Override void onEvent(Event event, Object source, Object data);  }