Search in sources :

Example 6 with ModelContext

use of org.xwiki.model.ModelContext in project xwiki-platform by xwiki.

the class TemporaryChartImageWriterTest method getURL.

@Test
public void getURL() throws Exception {
    WikiReference currentWikiReference = new WikiReference("wiki");
    ModelContext modelContext = this.componentManager.getInstance(ModelContext.class);
    when(modelContext.getCurrentEntityReference()).thenReturn(currentWikiReference);
    DocumentAccessBridge dab = this.componentManager.getInstance(DocumentAccessBridge.class);
    when(dab.getDocumentURL(new DocumentReference("wiki", "space", "page"), "temp", null, null)).thenReturn("temp/Space/Page");
    String location = this.componentManager.getComponentUnderTest().getURL(new ImageId(new ChartMacroParameters()));
    Assert.assertTrue("Got: " + location, location.toString().matches("temp/Space/Page/chart/.*\\.png"));
}
Also used : ModelContext(org.xwiki.model.ModelContext) ChartMacroParameters(org.xwiki.rendering.macro.chart.ChartMacroParameters) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 7 with ModelContext

use of org.xwiki.model.ModelContext 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 8 with ModelContext

use of org.xwiki.model.ModelContext in project xwiki-platform by xwiki.

the class AuthenticatingIntegrationTest method registerConfiguration.

@BeforeComponent
public void registerConfiguration() throws Exception {
    Properties properties = new Properties();
    properties.setProperty("mail.smtp.starttls.enable", "true");
    // Required by GreenMail. When using XWiki with Gmail for example this is not required.
    properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    this.configuration = new TestMailSenderConfiguration(this.mail.getSmtps().getPort(), "peter", "password", properties);
    this.componentManager.registerComponent(MailSenderConfiguration.class, this.configuration);
    // Set the current wiki in the Context
    ModelContext modelContext = this.componentManager.registerMockComponent(ModelContext.class);
    when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("wiki"));
    Provider<XWikiContext> xwikiContextProvider = this.componentManager.registerMockComponent(XWikiContext.TYPE_PROVIDER);
    when(xwikiContextProvider.get()).thenReturn(Mockito.mock(XWikiContext.class));
    this.componentManager.registerMockComponent(ExecutionContextManager.class);
    this.componentManager.registerMockComponent(Execution.class);
    this.componentManager.registerMockComponent(new DefaultParameterizedType(null, Copier.class, ExecutionContext.class));
    EnvironmentConfiguration environmentConfiguration = this.componentManager.registerMockComponent(EnvironmentConfiguration.class);
    when(environmentConfiguration.getPermanentDirectoryPath()).thenReturn(System.getProperty("java.io.tmpdir"));
}
Also used : ModelContext(org.xwiki.model.ModelContext) ExecutionContext(org.xwiki.context.ExecutionContext) EnvironmentConfiguration(org.xwiki.environment.internal.EnvironmentConfiguration) Copier(org.xwiki.mail.internal.thread.context.Copier) XWikiContext(com.xpn.xwiki.XWikiContext) Properties(java.util.Properties) WikiReference(org.xwiki.model.reference.WikiReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) BeforeComponent(org.xwiki.test.annotation.BeforeComponent)

Example 9 with ModelContext

use of org.xwiki.model.ModelContext in project xwiki-platform by xwiki.

the class ScriptingIntegrationTest method registerConfiguration.

@BeforeComponent
public void registerConfiguration() throws Exception {
    MailSenderConfiguration configuration = new TestMailSenderConfiguration(this.mail.getSmtp().getPort(), null, null, new Properties());
    this.componentManager.registerComponent(MailSenderConfiguration.class, configuration);
    // Register a test Permission Checker that allows sending mails
    ScriptServicePermissionChecker checker = mock(ScriptServicePermissionChecker.class);
    this.componentManager.registerComponent(ScriptServicePermissionChecker.class, "test", checker);
    // Set the current wiki in the Context
    ModelContext modelContext = this.componentManager.registerMockComponent(ModelContext.class);
    Mockito.when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("wiki"));
    Provider<XWikiContext> xwikiContextProvider = this.componentManager.registerMockComponent(XWikiContext.TYPE_PROVIDER);
    when(xwikiContextProvider.get()).thenReturn(Mockito.mock(XWikiContext.class));
    this.componentManager.registerMockComponent(ExecutionContextManager.class);
    this.componentManager.registerMockComponent(new DefaultParameterizedType(null, Copier.class, ExecutionContext.class));
    EnvironmentConfiguration environmentConfiguration = this.componentManager.registerMockComponent(EnvironmentConfiguration.class);
    when(environmentConfiguration.getPermanentDirectoryPath()).thenReturn(System.getProperty("java.io.tmpdir"));
    this.componentManager.registerMockComponent(ConverterManager.class);
}
Also used : ModelContext(org.xwiki.model.ModelContext) ExecutionContext(org.xwiki.context.ExecutionContext) EnvironmentConfiguration(org.xwiki.environment.internal.EnvironmentConfiguration) Copier(org.xwiki.mail.internal.thread.context.Copier) XWikiContext(com.xpn.xwiki.XWikiContext) Properties(java.util.Properties) WikiReference(org.xwiki.model.reference.WikiReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) ScriptServicePermissionChecker(org.xwiki.mail.script.ScriptServicePermissionChecker) MailSenderConfiguration(org.xwiki.mail.MailSenderConfiguration) BeforeComponent(org.xwiki.test.annotation.BeforeComponent)

Example 10 with ModelContext

use of org.xwiki.model.ModelContext in project xwiki-platform by xwiki.

the class DefaultWikiMacroManagerTest method registerAndUnregisterWikiMacroWhenGlobalVisibilityAndAllowed.

@Test
public void registerAndUnregisterWikiMacroWhenGlobalVisibilityAndAllowed() throws Exception {
    DefaultWikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.GLOBAL);
    // Simulate a user who's allowed for the GLOBAL visibility
    WikiMacroFactory wikiMacroFactory = this.mocker.getInstance(WikiMacroFactory.class);
    when(wikiMacroFactory.isAllowed(wikiMacro.getDocumentReference(), WikiMacroVisibility.GLOBAL)).thenReturn(true);
    WikiMacroManager wikiMacroManager = this.mocker.getComponentUnderTest();
    assertFalse(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference()));
    EntityReferenceSerializer<String> serializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
    when(serializer.serialize(this.authorReference)).thenReturn("authorwiki:authorspace.authorpage");
    // Test registration
    wikiMacroManager.registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro);
    assertTrue(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference()));
    // Verify that the WikiMacroManager has registered the macro against the root CM
    assertTrue(this.mocker.hasComponent(Macro.class, "testwikimacro"));
    // Verify that the user and wiki where the macro is located have been set in the context
    DocumentAccessBridge bridge = this.mocker.getInstance(DocumentAccessBridge.class);
    verify(bridge).setCurrentUser("authorwiki:authorspace.authorpage");
    ModelContext modelContext = this.mocker.getInstance(ModelContext.class);
    verify(modelContext).setCurrentEntityReference(wikiMacro.getDocumentReference());
    // Test unregistration
    wikiMacroManager.unregisterWikiMacro(wikiMacro.getDocumentReference());
    assertFalse(wikiMacroManager.hasWikiMacro(wikiMacro.getDocumentReference()));
    // Verify that the WikiMacroManager has unregistered the macro from the root CM
    assertFalse(this.mocker.hasComponent(Macro.class, "testwikimacro"));
}
Also used : WikiMacroFactory(org.xwiki.rendering.macro.wikibridge.WikiMacroFactory) ModelContext(org.xwiki.model.ModelContext) Macro(org.xwiki.rendering.macro.Macro) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) WikiMacroManager(org.xwiki.rendering.macro.wikibridge.WikiMacroManager) Test(org.junit.Test)

Aggregations

ModelContext (org.xwiki.model.ModelContext)12 WikiReference (org.xwiki.model.reference.WikiReference)7 ExecutionContext (org.xwiki.context.ExecutionContext)5 DocumentReference (org.xwiki.model.reference.DocumentReference)5 Test (org.junit.Test)4 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)4 XWikiContext (com.xpn.xwiki.XWikiContext)3 Properties (java.util.Properties)3 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)3 EnvironmentConfiguration (org.xwiki.environment.internal.EnvironmentConfiguration)3 Copier (org.xwiki.mail.internal.thread.context.Copier)3 BeforeComponent (org.xwiki.test.annotation.BeforeComponent)3 VelocityContext (org.apache.velocity.VelocityContext)2 Before (org.junit.Before)2 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)2 Execution (org.xwiki.context.Execution)2 XDOM (org.xwiki.rendering.block.XDOM)2 ChartMacroParameters (org.xwiki.rendering.macro.chart.ChartMacroParameters)2 Parser (org.xwiki.rendering.parser.Parser)2 AuthorizationManager (org.xwiki.security.authorization.AuthorizationManager)2