Search in sources :

Example 11 with WordBlock

use of org.xwiki.rendering.block.WordBlock in project xwiki-platform by xwiki.

the class CurrentMacroEntityReferenceResolverTest method resolveWhenMetaDataBlock.

@Test
public void resolveWhenMetaDataBlock() throws Exception {
    DocumentReference baseReference = new DocumentReference("basewiki", "basespace", "basepage");
    EntityReference expectedReference = new AttachmentReference("file", baseReference);
    EntityReferenceResolver<String> currentEntityReferenceResolver = mocker.getInstance(EntityReferenceResolver.TYPE_STRING, "current");
    when(currentEntityReferenceResolver.resolve("basewiki:basespace.basepage", EntityType.DOCUMENT)).thenReturn(baseReference);
    when(currentEntityReferenceResolver.resolve("file", EntityType.ATTACHMENT, baseReference)).thenReturn(expectedReference);
    Block wordBlock = new WordBlock("whatever");
    MetaData metaData = new MetaData(Collections.<String, Object>singletonMap(MetaData.BASE, "basewiki:basespace.basepage"));
    new XDOM(Arrays.<Block>asList(new MetaDataBlock(Arrays.<Block>asList(wordBlock), metaData)));
    Assert.assertEquals(expectedReference, mocker.getComponentUnderTest().resolve("file", EntityType.ATTACHMENT, wordBlock));
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XDOM(org.xwiki.rendering.block.XDOM) MetaData(org.xwiki.rendering.listener.MetaData) WordBlock(org.xwiki.rendering.block.WordBlock) EntityReference(org.xwiki.model.reference.EntityReference) WordBlock(org.xwiki.rendering.block.WordBlock) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) DocumentReference(org.xwiki.model.reference.DocumentReference) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) Test(org.junit.Test)

Example 12 with WordBlock

use of org.xwiki.rendering.block.WordBlock in project xwiki-platform by xwiki.

the class CurrentMacroEntityReferenceResolverTest method resolveWhenNoMetaDataBlock.

@Test
public void resolveWhenNoMetaDataBlock() throws Exception {
    EntityReference expectedReference = new DocumentReference("wiki", "Space", "Page");
    EntityReferenceResolver<String> currentEntityReferenceResolver = mocker.getInstance(EntityReferenceResolver.TYPE_STRING, "current");
    when(currentEntityReferenceResolver.resolve("Space.Page", EntityType.DOCUMENT)).thenReturn(expectedReference);
    Block block = new WordBlock("whatever");
    Assert.assertEquals(expectedReference, mocker.getComponentUnderTest().resolve("Space.Page", EntityType.DOCUMENT, block));
}
Also used : WordBlock(org.xwiki.rendering.block.WordBlock) EntityReference(org.xwiki.model.reference.EntityReference) WordBlock(org.xwiki.rendering.block.WordBlock) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 13 with WordBlock

use of org.xwiki.rendering.block.WordBlock in project xwiki-platform by xwiki.

the class EditableGadgetRenderer method getGadgetEditMetadata.

/**
 * @param gadget the gadget to decorate
 * @return the block containing the metadata that will allow clients to edit this gadget
 */
protected Block getGadgetEditMetadata(Gadget gadget) {
    GroupBlock metadataBlock = new GroupBlock();
    metadataBlock.setParameter(CLASS, METADATA);
    // look at the content of the gadget and store whether it's a macro or not
    boolean isMacro = gadget.getContent().size() == 1 && gadget.getContent().get(0) instanceof MacroMarkerBlock;
    GroupBlock isMacroBlock = new GroupBlock();
    isMacroBlock.setParameter(CLASS, "isMacro");
    isMacroBlock.addChild(new WordBlock(Boolean.toString(isMacro)));
    metadataBlock.addChild(isMacroBlock);
    if (isMacro) {
        // render the annotated macro call in the page, to be able to edit it. Only the macro call comments will be
        // rendered, since transformations are not ran, so there is no content in the macro. But annotation is
        // enough.
        GroupBlock renderedContentBlock = new GroupBlock();
        renderedContentBlock.setParameter(CLASS, "content");
        WikiPrinter printer = new DefaultWikiPrinter();
        BlockRenderer gadgetContentRenderer = getGadgetContentRenderer();
        gadgetContentRenderer.render(gadget.getContent(), printer);
        RawBlock rawBlock = new RawBlock(printer.toString(), getRawBlockSyntax(gadgetContentRenderer));
        renderedContentBlock.addChild(rawBlock);
        // render the title in the page as well, to be edited as source
        GroupBlock gadgetTitleBlock = new GroupBlock();
        gadgetTitleBlock.setParameter(CLASS, "title");
        // even if it's not a word, it's fine since it will be rendered in one piece
        gadgetTitleBlock.addChild(new WordBlock(gadget.getTitleSource()));
        metadataBlock.addChild(renderedContentBlock);
        metadataBlock.addChild(gadgetTitleBlock);
    }
    return metadataBlock;
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) WordBlock(org.xwiki.rendering.block.WordBlock) RawBlock(org.xwiki.rendering.block.RawBlock) GroupBlock(org.xwiki.rendering.block.GroupBlock) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Example 14 with WordBlock

use of org.xwiki.rendering.block.WordBlock 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 15 with WordBlock

use of org.xwiki.rendering.block.WordBlock in project xwiki-platform by xwiki.

the class IntegrationTest method initialize.

@RenderingTestSuite.Initialized
@SuppressWarnings("unchecked")
public void initialize(final MockingComponentManager componentManager) throws Exception {
    Mockery mockery = new JUnit4Mockery();
    // Since we have a dependency on XWiki Platform Oldcore the Context Component Manager will be found and the
    // test will try to look up the Dashboard macro in the User and Wiki Component Manager and thus need a Current
    // User and a Current Wiki. It's easier for this test to simply unregister the Context Component Manager rather
    // than have to provide mocks for them.
    componentManager.unregisterComponent(ComponentManager.class, "context");
    final SkinExtension mockSsfx = componentManager.registerMockComponent(mockery, SkinExtension.class, "ssfx", "ssfxMock");
    final SkinExtension mockJsfx = componentManager.registerMockComponent(mockery, SkinExtension.class, "jsfx", "jsfxMock");
    mockery.checking(new Expectations() {

        {
            allowing(mockSsfx).use(with("uicomponents/container/columns.css"), with(any(Map.class)));
            allowing(mockSsfx).use(with("uicomponents/dashboard/dashboard.css"), with(any(Map.class)));
            allowing(mockJsfx).use(with("js/scriptaculous/dragdrop.js"));
            allowing(mockJsfx).use(with("js/scriptaculous/effects.js"));
            allowing(mockJsfx).use(with("uicomponents/dashboard/dashboard.js"), with(any(Map.class)));
        }
    });
    final GadgetSource mockGadgetSource = componentManager.registerMockComponent(mockery, GadgetSource.class);
    mockery.checking(new Expectations() {

        {
            // Mock gadget for macrodashboard_nested_velocity.test
            allowing(mockGadgetSource).getGadgets(with("nested_velocity"), with(any(MacroTransformationContext.class)));
            will(returnValue(Arrays.asList(new Gadget("0", Arrays.asList(new WordBlock("title")), Arrays.asList(new MacroMarkerBlock("velocity", Collections.emptyMap(), "someVelocityCodeHere", Collections.singletonList(new WordBlock("someVelocityOutputHere")), true)), "1,1"))));
            // Mock gadget for macrodashboard1.test
            allowing(mockGadgetSource).getGadgets(with(aNull(String.class)), with(any(MacroTransformationContext.class)));
            will(returnValue(Arrays.asList(new Gadget("0", Arrays.<Block>asList(new WordBlock("title")), Arrays.<Block>asList(new WordBlock("content")), "1,1"))));
            allowing(mockGadgetSource).getDashboardSourceMetadata(with(AnyOf.anyOf(aNull(String.class), any(String.class))), with(any(MacroTransformationContext.class)));
            will(returnValue(Collections.<Block>emptyList()));
            allowing(mockGadgetSource).isEditing();
            // return true on is editing, to take as many paths possible
            will(returnValue(true));
        }
    });
    // Mock VelocityManager used in macrodashboard_nested_velocity.test because we do not have an XWikiContext
    // instance in the ExecutionContext.
    final VelocityManager mockVelocityManager = componentManager.registerMockComponentWithId(mockery, VelocityManager.class, "velocityManagerMock");
    mockery.checking(new Expectations() {

        {
            allowing(mockVelocityManager).getVelocityContext();
            will(returnValue(new VelocityContext()));
            allowing(mockVelocityManager).getCurrentVelocityContext();
            will(returnValue(new VelocityContext()));
            allowing(mockVelocityManager).getVelocityEngine();
            will(doAll(new CustomAction("mockGetVelocityEngine") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    VelocityEngine velocityEngine = componentManager.getInstance(VelocityEngine.class);
                    Properties properties = new Properties();
                    properties.setProperty("resource.loader", "file");
                    velocityEngine.initialize(properties);
                    return velocityEngine;
                }
            }));
            allowing(mockVelocityManager).evaluate(with(any(Writer.class)), with(any(String.class)), with(any(Reader.class)));
            will(doAll(new CustomAction("mockEvaluate") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    VelocityEngine velocityEngine = mockVelocityManager.getVelocityEngine();
                    return velocityEngine.evaluate(mockVelocityManager.getVelocityContext(), (Writer) invocation.getParameter(0), (String) invocation.getParameter(1), (Reader) invocation.getParameter(2));
                }
            }));
        }
    });
    componentManager.registerMockComponent(mockery, ContextualAuthorizationManager.class);
}
Also used : Expectations(org.jmock.Expectations) VelocityEngine(org.xwiki.velocity.VelocityEngine) SkinExtension(org.xwiki.skinx.SkinExtension) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) VelocityContext(org.apache.velocity.VelocityContext) WordBlock(org.xwiki.rendering.block.WordBlock) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) Reader(java.io.Reader) Properties(java.util.Properties) Mockery(org.jmock.Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) VelocityManager(org.xwiki.velocity.VelocityManager) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) Map(java.util.Map) Writer(java.io.Writer)

Aggregations

WordBlock (org.xwiki.rendering.block.WordBlock)24 Test (org.junit.Test)13 Block (org.xwiki.rendering.block.Block)13 XDOM (org.xwiki.rendering.block.XDOM)8 DocumentReference (org.xwiki.model.reference.DocumentReference)6 TranslationMessage (org.xwiki.localization.message.TranslationMessage)5 ArrayList (java.util.ArrayList)4 HeaderBlock (org.xwiki.rendering.block.HeaderBlock)4 LinkBlock (org.xwiki.rendering.block.LinkBlock)4 ParagraphBlock (org.xwiki.rendering.block.ParagraphBlock)4 SpecialSymbolBlock (org.xwiki.rendering.block.SpecialSymbolBlock)4 EntityReference (org.xwiki.model.reference.EntityReference)3 IdBlock (org.xwiki.rendering.block.IdBlock)3 SectionBlock (org.xwiki.rendering.block.SectionBlock)3 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)3 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 List (java.util.List)2 Expectations (org.jmock.Expectations)2 Mockery (org.jmock.Mockery)2