Search in sources :

Example 6 with XWikiMessageTool

use of com.xpn.xwiki.web.XWikiMessageTool in project xwiki-platform by xwiki.

the class DeprecatedScriptContextInitializer method initialize.

@Override
public void initialize(ScriptContext scriptContext) {
    // Make deprecated XWiki message tool available from scripts
    scriptContext.setAttribute("msg", new XWikiMessageTool(Utils.getComponent(ContextualLocalizationManager.class)), ScriptContext.ENGINE_SCOPE);
    // Make the Syntax Factory component available from Script.
    // TODO: We need to decide how we want to expose components in general and how to protect users from
    // "dangerous" apis.
    scriptContext.setAttribute("syntaxFactory", this.syntaxFactory, ScriptContext.ENGINE_SCOPE);
}
Also used : XWikiMessageTool(com.xpn.xwiki.web.XWikiMessageTool)

Example 7 with XWikiMessageTool

use of com.xpn.xwiki.web.XWikiMessageTool in project xwiki-platform by xwiki.

the class XWikiContext method getMessageTool.

/**
 * @deprecated since 4.3M2 use {@link org.xwiki.localization.ContextualLocalizationManager} component instead
 */
@Deprecated
public XWikiMessageTool getMessageTool() {
    XWikiMessageTool msg = ((XWikiMessageTool) get("msg"));
    if (msg == null) {
        getWiki().prepareResources(this);
        msg = ((XWikiMessageTool) get("msg"));
    }
    return msg;
}
Also used : XWikiMessageTool(com.xpn.xwiki.web.XWikiMessageTool)

Example 8 with XWikiMessageTool

use of com.xpn.xwiki.web.XWikiMessageTool in project celements-blog by celements.

the class BlogPluginTest method testBatchImportReceivers_inactiveAdd.

@Test
public void testBatchImportReceivers_inactiveAdd() throws XWikiException {
    Map<String, String> result = new TreeMap<>();
    result.put("myname@email.com", "inactive");
    result.put("myName2@email", "invalid");
    result.put("myName @email.com", "invalid");
    String importData = "\r\n,myName @email.com,,,myName2@email\nmyName@email.com";
    String nl = "My.Newsletter";
    XWikiMessageTool messageTool = createMock(XWikiMessageTool.class);
    XWikiContext context = createMock(XWikiContext.class);
    expect(context.getMessageTool()).andReturn(messageTool).anyTimes();
    expect(context.getWiki()).andReturn(xwiki).anyTimes();
    expect(context.getUser()).andReturn("XWiki.Admin").anyTimes();
    expect(xwiki.search((String) anyObject(), same(context))).andReturn(null).anyTimes();
    expect(xwiki.generateRandomString(eq(16))).andReturn("abc");
    expect(xwiki.exists(eq("NewsletterReceivers.abc"), same(context))).andReturn(false);
    XWikiDocument doc1 = createMock(XWikiDocument.class);
    expect(doc1.getObject(eq("Celements2.BlogConfigClass"))).andReturn(null);
    expect(doc1.getObject(eq("XWiki.ArticleClass"))).andReturn(null);
    expect(context.getDoc()).andReturn(doc1);
    expect(xwiki.getDocument(eq("NewsletterReceivers.abc"), same(context))).andReturn(doc1).times(2);
    XWikiDocument nlDoc = createMock(XWikiDocument.class);
    expect(xwiki.exists(eq(nl), same(context))).andReturn(true);
    expect(xwiki.getDocument(eq(nl), same(context))).andReturn(nlDoc);
    expect(nlDoc.getFullName()).andReturn(nl).anyTimes();
    expect(doc1.getObject(eq("Celements.NewsletterReceiverClass"), eq("subscribed"), eq(nl), eq(false))).andReturn(null);
    BaseObject obj = new BaseObject();
    expect(doc1.newObject(eq("Celements.NewsletterReceiverClass"), same(context))).andReturn(obj);
    expect(doc1.getObject(eq("Celements.NewsletterReceiverClass"), eq("subscribed"), eq(nl), eq(false))).andReturn(obj);
    xwiki.saveDocument(same(doc1), same(context));
    expectLastCall();
    expect(messageTool.get(eq("cel_newsletter_subscriber_invalid"))).andReturn("invalid").times(2);
    expect(messageTool.get(eq("cel_newsletter_subscriber_inactive"))).andReturn("inactive").once();
    replayAll(context, doc1, messageTool, nlDoc);
    assertEquals(result, plugin.batchImportReceivers(true, importData, nl, context));
    verifyAll(context, doc1, messageTool, nlDoc);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiMessageTool(com.xpn.xwiki.web.XWikiMessageTool) TreeMap(java.util.TreeMap) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 9 with XWikiMessageTool

use of com.xpn.xwiki.web.XWikiMessageTool in project celements-blog by celements.

the class BlogPluginTest method testBatchImportReceivers_Exception.

@Test
public void testBatchImportReceivers_Exception() throws XWikiException {
    Map<String, String> result = new TreeMap<>();
    result.put("myname@email.com", "invalid");
    String importData = "\r\n,My Name <myName@email.com>,,,,";
    String nl = "My.Newsletter";
    XWikiMessageTool messageTool = createMock(XWikiMessageTool.class);
    XWikiContext context = createMock(XWikiContext.class);
    expect(context.getMessageTool()).andReturn(messageTool).anyTimes();
    expect(context.getWiki()).andReturn(xwiki).anyTimes();
    expect(context.getUser()).andReturn("XWiki.Admin").anyTimes();
    expect(xwiki.search((String) anyObject(), same(context))).andReturn(null).anyTimes();
    expect(xwiki.generateRandomString(eq(16))).andReturn("abc");
    expect(xwiki.exists(eq("NewsletterReceivers.abc"), same(context))).andReturn(false);
    expect(xwiki.getDocument(eq("NewsletterReceivers.abc"), same(context))).andThrow(new XWikiException());
    expect(messageTool.get(eq("cel_newsletter_subscriber_invalid"))).andReturn("invalid").once();
    replayAll(context, messageTool);
    assertEquals(result, plugin.batchImportReceivers(false, importData, nl, context));
    verifyAll(context, messageTool);
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) XWikiMessageTool(com.xpn.xwiki.web.XWikiMessageTool) TreeMap(java.util.TreeMap) XWikiException(com.xpn.xwiki.XWikiException) Test(org.junit.Test)

Example 10 with XWikiMessageTool

use of com.xpn.xwiki.web.XWikiMessageTool in project celements-blog by celements.

the class BlogPluginTest method testBatchImportReceivers_activeAdd.

@Test
public void testBatchImportReceivers_activeAdd() throws XWikiException {
    Map<String, String> result = new TreeMap<>();
    result.put("myname@email.com", "active");
    String importData = "\r\n,My Name <myName@email.com>,,,,";
    String nl = "My.Newsletter";
    XWikiMessageTool messageTool = createMock(XWikiMessageTool.class);
    XWikiContext context = createMock(XWikiContext.class);
    expect(context.getMessageTool()).andReturn(messageTool).anyTimes();
    expect(context.getWiki()).andReturn(xwiki).anyTimes();
    expect(context.getUser()).andReturn("XWiki.Admin").anyTimes();
    expect(xwiki.search((String) anyObject(), same(context))).andReturn(null).anyTimes();
    expect(xwiki.generateRandomString(eq(16))).andReturn("abc");
    expect(xwiki.exists(eq("NewsletterReceivers.abc"), same(context))).andReturn(false);
    XWikiDocument doc1 = createMock(XWikiDocument.class);
    expect(doc1.getObject(eq("Celements2.BlogConfigClass"))).andReturn(null);
    expect(doc1.getObject(eq("XWiki.ArticleClass"))).andReturn(null);
    expect(context.getDoc()).andReturn(doc1);
    expect(xwiki.getDocument(eq("NewsletterReceivers.abc"), same(context))).andReturn(doc1).times(2);
    XWikiDocument nlDoc = createMock(XWikiDocument.class);
    expect(xwiki.exists(eq(nl), same(context))).andReturn(true);
    expect(xwiki.getDocument(eq(nl), same(context))).andReturn(nlDoc);
    expect(nlDoc.getFullName()).andReturn(nl).anyTimes();
    expect(doc1.getObject(eq("Celements.NewsletterReceiverClass"), eq("subscribed"), eq(nl), eq(false))).andReturn(null);
    BaseObject obj = new BaseObject();
    expect(doc1.newObject(eq("Celements.NewsletterReceiverClass"), same(context))).andReturn(obj);
    expect(doc1.getObject(eq("Celements.NewsletterReceiverClass"), eq("subscribed"), eq(nl), eq(false))).andReturn(obj);
    xwiki.saveDocument(same(doc1), same(context));
    expectLastCall();
    expect(messageTool.get(eq("cel_newsletter_subscriber_active"))).andReturn("active").once();
    replayAll(context, doc1, messageTool, nlDoc);
    assertEquals(result, plugin.batchImportReceivers(false, importData, nl, context));
    verifyAll(context, doc1, messageTool, nlDoc);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiMessageTool(com.xpn.xwiki.web.XWikiMessageTool) TreeMap(java.util.TreeMap) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Aggregations

XWikiMessageTool (com.xpn.xwiki.web.XWikiMessageTool)11 XWikiContext (com.xpn.xwiki.XWikiContext)5 TreeMap (java.util.TreeMap)5 Test (org.junit.Test)5 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)4 BaseObject (com.xpn.xwiki.objects.BaseObject)4 ArrayList (java.util.ArrayList)3 VelocityContext (org.apache.velocity.VelocityContext)2 XWikiException (com.xpn.xwiki.XWikiException)1 Locale (java.util.Locale)1 ContextualLocalizationManager (org.xwiki.localization.ContextualLocalizationManager)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1