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));
}
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));
}
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;
}
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);
}
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);
}
Aggregations