use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.
the class DocumentTitleDisplayerTest method whenSettingTheContextDocumentTheContextWikiIsAlsoSet.
@Test
public void whenSettingTheContextDocumentTheContextWikiIsAlsoSet() throws Exception {
EntityReferenceProvider defaultEntityReferenceProvider = this.mocker.getInstance(EntityReferenceProvider.class);
when(defaultEntityReferenceProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(new EntityReference("Page", EntityType.DOCUMENT));
DocumentModelBridge document = mock(DocumentModelBridge.class);
DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("Space"), "Page");
when(document.getDocumentReference()).thenReturn(documentReference);
when(document.getTitle()).thenReturn("title");
XDOM titleXDOM = new XDOM(Arrays.asList(new WordBlock("title")));
Parser plainTextParser = this.mocker.getInstance(Parser.class, "plain/1.0");
when(plainTextParser.parse(any(StringReader.class))).thenReturn(titleXDOM);
ModelContext modelContext = this.mocker.getInstance(ModelContext.class);
WikiReference currentWikiReference = new WikiReference("currentWiki");
when(modelContext.getCurrentEntityReference()).thenReturn(currentWikiReference);
AuthorizationManager authorizationManager = this.mocker.getInstance(AuthorizationManager.class);
when(authorizationManager.hasAccess(eq(Right.SCRIPT), any(), any())).thenReturn(true);
DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
DocumentDisplayerParameters params = new DocumentDisplayerParameters();
params.setTitleDisplayed(true);
params.setExecutionContextIsolated(true);
this.mocker.getComponentUnderTest().display(document, params);
// Check that the context is set.
verify(dab).pushDocumentInContext(any(), eq(documentReference));
verify(modelContext).setCurrentEntityReference(documentReference.getWikiReference());
// Check that the context is restored.
verify(dab).popDocumentFromContext(any());
verify(modelContext).setCurrentEntityReference(currentWikiReference);
}
use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.
the class EventAndFactoryTest method configure.
@Before
public void configure() throws Exception {
final DocumentAccessBridge mockDocumentAccessBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
getMockery().checking(new Expectations() {
{
allowing(mockDocumentAccessBridge).getCurrentUser();
will(returnValue("XWiki.Admin"));
}
});
final ExecutionContext context = new ExecutionContext();
final Execution mockExecution = getComponentManager().getInstance(Execution.class);
getMockery().checking(new Expectations() {
{
allowing(mockExecution).getContext();
will(returnValue(context));
}
});
final EntityReferenceResolver<String> mockResolver = getComponentManager().getInstance(EntityReferenceResolver.TYPE_STRING);
getMockery().checking(new Expectations() {
{
allowing(mockResolver).resolve("XWiki.Admin", EntityType.DOCUMENT);
will(returnValue(new DocumentReference("xwiki", "XWiki", "Admin")));
}
});
this.factory = getComponentManager().getInstance(EventFactory.class);
this.defaultEvent = this.factory.createEvent();
this.rawEvent = this.factory.createRawEvent();
}
use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.
the class DocumentContentDisplayerTest method testBaseMetaDataIsSetBeforeExecutingTransformations.
@Test
public void testBaseMetaDataIsSetBeforeExecutingTransformations() throws Exception {
// The execution context is expected to have the "xwikicontext" property set.
Execution mockExecution = mocker.getInstance(Execution.class);
ExecutionContext executionContext = new ExecutionContext();
executionContext.setProperty("xwikicontext", new HashMap<String, Object>());
Mockito.when(mockExecution.getContext()).thenReturn(executionContext);
// The document being displayed.
DocumentModelBridge mockDocument = Mockito.mock(DocumentModelBridge.class);
XDOM content = new XDOM(Collections.<Block>emptyList());
Mockito.when(mockDocument.getXDOM()).thenReturn(content);
// The reference of the current document musts be set as the value of the BASE meta data.
DocumentReference currentDocRef = new DocumentReference("wiki", "Space", "Page");
DocumentAccessBridge mockDocumentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
Mockito.when(mockDocumentAccessBridge.getCurrentDocumentReference()).thenReturn(currentDocRef);
EntityReferenceSerializer<String> serializer = mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
Mockito.when(serializer.serialize(currentDocRef)).thenReturn("foo");
// We can't verify the meta data after the display method is called because we want to make sure the BASE meta
// data is correctly set before XDOM transformations are executed, not after.
TransformationManager mockTransformationManager = mocker.getInstance(TransformationManager.class);
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
XDOM xdom = (XDOM) invocation.getArguments()[0];
// We have to assert the meta data before the transformations are executed not at the end!
Assert.assertEquals("foo", xdom.getMetaData().getMetaData(MetaData.BASE));
return null;
}
}).when(mockTransformationManager).performTransformations(Mockito.any(XDOM.class), Mockito.any(TransformationContext.class));
// Execute the display.
Assert.assertSame(content, mocker.getComponentUnderTest().display(mockDocument, new DocumentDisplayerParameters()));
// Make sure the transformations are executed exactly once, and on the right content.
Mockito.verify(mockTransformationManager, Mockito.times(1)).performTransformations(Mockito.same(content), Mockito.any(TransformationContext.class));
}
use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.
the class DefaultMailTemplateManagerTest method evaluateWithLanguage.
@Test
public void evaluateWithLanguage() throws Exception {
DocumentAccessBridge documentBridge = this.mocker.getInstance(DocumentAccessBridge.class);
DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
when(documentBridge.getObjectNumber(any(), any(), eq("language"), eq("fr"))).thenReturn(1);
when(documentBridge.getProperty(any(DocumentReference.class), any(), eq(1), eq("html"))).thenReturn("Salut <b>${name}</b> <br />${email}");
VelocityEngine velocityEngine = mock(VelocityEngine.class);
VelocityManager velocityManager = this.mocker.getInstance(VelocityManager.class);
when(velocityManager.getVelocityEngine()).thenReturn(velocityEngine);
when(velocityEvaluator.evaluateVelocity(eq("Salut <b>${name}</b> <br />${email}"), any(), any(VelocityContext.class))).thenReturn("Salut <b>John Doe</b> <br />john@doe.com");
// Set the default Locale to be different from the locale we pass to verify we restore it properly
when(this.xwikiContext.getLocale()).thenReturn(Locale.ITALIAN);
String result = this.mocker.getComponentUnderTest().evaluate(documentReference, "html", Collections.emptyMap(), Locale.FRENCH);
verify(documentBridge).getObjectNumber(any(), any(), eq("language"), eq("fr"));
// Make sure we set the right locale in the XWiki Context
verify(this.xwikiContext).setLocale(Locale.FRENCH);
verify(this.xwikiContext).setLocale(Locale.ITALIAN);
assertEquals(result, "Salut <b>John Doe</b> <br />john@doe.com");
}
use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.
the class DefaultMailTemplateManagerTest method evaluate.
@Test
public void evaluate() throws Exception {
DocumentAccessBridge documentBridge = this.mocker.getInstance(DocumentAccessBridge.class);
DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
when(documentBridge.getProperty(same(documentReference), any(), anyInt(), eq("html"))).thenReturn("Hello <b>${name}</b> <br />${email}");
VelocityEngine velocityEngine = mock(VelocityEngine.class);
VelocityManager velocityManager = this.mocker.getInstance(VelocityManager.class);
when(velocityManager.getVelocityEngine()).thenReturn(velocityEngine);
when(velocityEvaluator.evaluateVelocity(eq("Hello <b>${name}</b> <br />${email}"), any(), any(VelocityContext.class))).thenReturn("Hello <b>John Doe</b> <br />john@doe.com");
String result = this.mocker.getComponentUnderTest().evaluate(documentReference, "html", Collections.emptyMap());
assertEquals(result, "Hello <b>John Doe</b> <br />john@doe.com");
}
Aggregations