### Question:
RightsFilterListener extends AbstractEventListener { @Override public void onEvent(Event event, Object source, Object data) { XWikiDocument document = (XWikiDocument) source; UserEvent userEvent = (UserEvent) event; checkModifiedRights(userEvent.getUserReference(), document, XWikiRightsDocumentInitializer.CLASS_REFERENCE, null); checkModifiedRights(userEvent.getUserReference(), document, XWikiGlobalRightsDocumentInitializer.CLASS_REFERENCE, (CancelableEvent) event); } RightsFilterListener(); @Override void onEvent(Event event, Object source, Object data); static final String NAME; }### Answer:
@Test public void noRight() { XWikiDocument document = new XWikiDocument(new DocumentReference("wiki", "space", "page")); document.setOriginalDocument(document.clone()); XWikiDocument before = document.clone(); this.listener.onEvent(new UserUpdatingDocumentEvent(), document, null); assertEquals(before, document); }
@Test public void noChange() throws XWikiException { XWikiDocument document = new XWikiDocument(new DocumentReference("wiki", "space", "page")); document.newXObject(XWikiRightsDocumentInitializer.CLASS_REFERENCE, oldcore.getXWikiContext()); document.setOriginalDocument(document.clone()); XWikiDocument before = document.clone(); this.listener.onEvent(new UserUpdatingDocumentEvent(), document, null); assertEquals(before, document); }
@Test public void newEmptyRightObject() throws XWikiException { XWikiDocument document = new XWikiDocument(new DocumentReference("wiki", "space", "page")); document.setOriginalDocument(document.clone()); document.newXObject(XWikiRightsDocumentInitializer.CLASS_REFERENCE, oldcore.getXWikiContext()); XWikiDocument before = document.clone(); this.listener.onEvent(new UserUpdatingDocumentEvent(), document, null); assertEquals(before, document); }
@Test public void newAllowedRightObject() throws XWikiException { XWikiDocument document = new XWikiDocument(new DocumentReference("wiki", "space", "page")); document.setOriginalDocument(document.clone()); BaseObject rightObject = document.newXObject(XWikiRightsDocumentInitializer.CLASS_REFERENCE, oldcore.getXWikiContext()); rightObject.setStringValue("levels", "view"); XWikiDocument before = document.clone(); this.listener.onEvent(new UserUpdatingDocumentEvent(), document, null); assertEquals(before, document); }
@Test public void newDeniedRightObject() throws XWikiException, AccessDeniedException { XWikiDocument document = new XWikiDocument(new DocumentReference("wiki", "space", "page")); BaseObject rightObject = document.newXObject(XWikiRightsDocumentInitializer.CLASS_REFERENCE, oldcore.getXWikiContext()); document.setOriginalDocument(document.clone()); document.getOriginalDocument().removeXObjects(XWikiRightsDocumentInitializer.CLASS_REFERENCE); rightObject.setStringValue("levels", "view"); doThrow(AccessDeniedException.class).when(this.authorization).checkAccess(Right.VIEW, null, document.getDocumentReference()); XWikiDocument before = document.clone(); this.listener.onEvent(new UserUpdatingDocumentEvent(), document, null); assertNotEquals(before, document); assertEquals(document.getOriginalDocument(), document); }