Search in sources :

Example 31 with DocumentAccessBridge

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);
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) WordBlock(org.xwiki.rendering.block.WordBlock) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) Parser(org.xwiki.rendering.parser.Parser) ModelContext(org.xwiki.model.ModelContext) EntityReferenceProvider(org.xwiki.model.reference.EntityReferenceProvider) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) EntityReference(org.xwiki.model.reference.EntityReference) StringReader(java.io.StringReader) AuthorizationManager(org.xwiki.security.authorization.AuthorizationManager) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 32 with DocumentAccessBridge

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();
}
Also used : Expectations(org.jmock.Expectations) ExecutionContext(org.xwiki.context.ExecutionContext) Execution(org.xwiki.context.Execution) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) DefaultEventFactory(org.xwiki.eventstream.internal.DefaultEventFactory) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 33 with DocumentAccessBridge

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));
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TransformationManager(org.xwiki.rendering.transformation.TransformationManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 34 with DocumentAccessBridge

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");
}
Also used : VelocityEngine(org.xwiki.velocity.VelocityEngine) VelocityManager(org.xwiki.velocity.VelocityManager) VelocityContext(org.apache.velocity.VelocityContext) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 35 with DocumentAccessBridge

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");
}
Also used : VelocityEngine(org.xwiki.velocity.VelocityEngine) VelocityManager(org.xwiki.velocity.VelocityManager) VelocityContext(org.apache.velocity.VelocityContext) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)56 DocumentReference (org.xwiki.model.reference.DocumentReference)39 Test (org.junit.Test)33 Expectations (org.jmock.Expectations)17 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)15 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)11 Before (org.junit.Before)10 XDOM (org.xwiki.rendering.block.XDOM)10 Execution (org.xwiki.context.Execution)7 SpaceReference (org.xwiki.model.reference.SpaceReference)7 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)7 EntityReferenceResolver (org.xwiki.model.reference.EntityReferenceResolver)6 MacroBlock (org.xwiki.rendering.block.MacroBlock)6 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)6 HashMap (java.util.HashMap)5 VelocityContext (org.apache.velocity.VelocityContext)5 Event (org.xwiki.eventstream.Event)5 DefaultEvent (org.xwiki.eventstream.internal.DefaultEvent)5 VelocityEngine (org.xwiki.velocity.VelocityEngine)5 VelocityManager (org.xwiki.velocity.VelocityManager)5