1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.wiki.rendering.impl;
18
19 import org.exoplatform.container.ExoContainerContext;
20 import org.exoplatform.portal.config.model.PortalConfig;
21 import org.exoplatform.wiki.mow.api.Attachment;
22 import org.exoplatform.wiki.mow.api.Page;
23 import org.exoplatform.wiki.mow.api.Wiki;
24 import org.exoplatform.wiki.service.WikiContext;
25 import org.exoplatform.wiki.service.WikiService;
26 import org.xwiki.context.Execution;
27 import org.xwiki.context.ExecutionContext;
28 import org.xwiki.rendering.syntax.Syntax;
29
30 public class TestRenderingService extends AbstractRenderingTestCase {
31
32 private WikiService wikiService;
33
34 @Override
35 protected void setUp() throws Exception {
36 super.setUp();
37 wikiService = ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(WikiService.class);
38 }
39
40 public void testRender() throws Exception {
41 assertEquals("<p>This is <strong>bold</strong></p>", renderingService.render("This is **bold**", Syntax.XWIKI_2_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false));
42 }
43
44 public void testRenderExternalLink() throws Exception{
45 String expectedHttpHtml = "<p><span class=\"wikiexternallink\"><a href=\"http://exoplatform.com\">eXo</a></span></p>";
46 assertEquals(expectedHttpHtml, renderingService.render("[eXo|http://exoplatform.com]", Syntax.CONFLUENCE_1_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false));
47 String expectedMailtoHtml = "<p><span class=\"wikiexternallink\"><a href=\"mailto:exoplatform.com\">Mail to eXo</a></span></p>";
48 assertEquals(expectedMailtoHtml, renderingService.render("[Mail to eXo|mailto:exoplatform.com]", Syntax.CONFLUENCE_1_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false));
49 }
50
51 public void testRenderAnExistedInternalLink() throws Exception {
52 Wiki wiki = wikiService.createWiki(PortalConfig.PORTAL_TYPE, "classic");
53
54 Page page = wikiService.createPage(wiki, "WikiHome", new Page("CreateWikiPage-002", "CreateWikiPage-002"));
55
56 Attachment attachment = new Attachment();
57 attachment.setName("eXoWikiHome.png");
58 attachment.setMimeType("image/png");
59 attachment.setContent("logo".getBytes());
60 wikiService.addAttachmentToPage(attachment, page);
61
62 Execution ec = renderingService.getExecution();
63 ec.setContext(new ExecutionContext());
64 WikiContext wikiContext = new WikiContext();
65 wikiContext.setPortalURL("http://loclahost:8080/portal/classic/");
66 wikiContext.setPortletURI("wiki");
67 wikiContext.setType("portal");
68 wikiContext.setOwner("classic");
69 wikiContext.setPageName("CreateWikiPage-002");
70
71 ec.getContext().setProperty(WikiContext.WIKICONTEXT, wikiContext);
72
73 String expectedHtml = "<p><span class=\"wikilink\"><a href=\"http://loclahost:8080/portal/classic/wiki/CreateWikiPage-002\">CreateWikiPage-002</a></span></p>";
74 assertEquals(expectedHtml, renderingService.render("[[CreateWikiPage-002>>CreateWikiPage-002]]", Syntax.XWIKI_2_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false));
75 assertEquals(expectedHtml, renderingService.render("[[CreateWikiPage-002>>classic.CreateWikiPage-002]]", Syntax.XWIKI_2_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false));
76 assertEquals(expectedHtml, renderingService.render("[[CreateWikiPage-002>>portal:classic.CreateWikiPage-002]]", Syntax.XWIKI_2_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false));
77 }
78
79 public void testRenderCreatePageLink() throws Exception {
80 Wiki wiki = wikiService.createWiki(PortalConfig.PORTAL_TYPE, "classic");
81
82 Execution ec = renderingService.getExecution();
83 ec.setContext(new ExecutionContext());
84 WikiContext wikiContext = new WikiContext();
85 wikiContext.setPortalURL("http://loclahost:8080/portal/classic/");
86 wikiContext.setPortletURI("wiki");
87 wikiContext.setType("portal");
88 wikiContext.setOwner("classic");
89 wikiContext.setPageName("WikiHome");
90
91 ec.getContext().setProperty(WikiContext.WIKICONTEXT, wikiContext);
92
93 String expectedHtml = "<p><span class=\"wikicreatelink\"><a href=\"http://loclahost:8080/portal/classic/wiki/WikiHome?action=AddPage&pageTitle=NonExistedWikiPage-001&wiki=classic&wikiType=portal\">NonExistedWikiPage-001</a></span></p>";
94 assertEquals(expectedHtml, renderingService.render("[[NonExistedWikiPage-001>>NonExistedWikiPage-001]]", Syntax.XWIKI_2_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false));
95 assertEquals(expectedHtml, renderingService.render("[[NonExistedWikiPage-001>>classic.NonExistedWikiPage-001]]", Syntax.XWIKI_2_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false));
96 assertEquals(expectedHtml, renderingService.render("[[NonExistedWikiPage-001>>portal:classic.NonExistedWikiPage-001]]", Syntax.XWIKI_2_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false));
97 }
98
99 public void testRenderAttachmentsAndImages() throws Exception {
100 Wiki wiki = wikiService.createWiki(PortalConfig.PORTAL_TYPE, "classic");
101 Page page = wikiService.createPage(wiki, "WikiHome", new Page("CreateWikiPage-003", "CreateWikiPage-003"));
102 Attachment attachment1 = new Attachment();
103 attachment1.setName("space in name.png");
104 attachment1.setContent("logo".getBytes());
105 attachment1.setMimeType("image/png");
106 wikiService.addAttachmentToPage(attachment1, page);
107 Attachment attachment2 = new Attachment();
108 attachment2.setName("eXoWikiHome.png");
109 attachment2.setContent("logo".getBytes());
110 attachment2.setMimeType("image/png");
111 wikiService.addAttachmentToPage(attachment2, page);
112 Execution ec = renderingService.getExecution();
113 ec.setContext(new ExecutionContext());
114 WikiContext wikiContext = new WikiContext();
115 wikiContext.setPortalURL("http://portal.exo.com:8080/portal/classic/portalPage");
116 wikiContext.setPortletURI("wiki");
117 wikiContext.setType("portal");
118 wikiContext.setOwner("classic");
119 wikiContext.setPageName("CreateWikiPage-003");
120 ec.getContext().setProperty(WikiContext.WIKICONTEXT, wikiContext);
121 String expectedXwikiAttachmentHtml = "<p><span class=\"wikiattachmentlink\"><a href=\"http://portal.exo.com:8080/portal/rest/wiki/images/portal/space/classic/page/CreateWikiPage-003/eXoWikiHome.png\">eXoWikiHome.png</a></span></p>";
122 assertEquals(expectedXwikiAttachmentHtml, renderingService.render("[[eXoWikiHome.png>>attach:eXoWikiHome.png]]", Syntax.XWIKI_2_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false));
123 assertEquals(expectedXwikiAttachmentHtml, renderingService.render("[[eXoWikiHome.png>>attach:CreateWikiPage-003@eXoWikiHome.png]]", Syntax.XWIKI_2_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false));
124 assertEquals(expectedXwikiAttachmentHtml, renderingService.render("[[eXoWikiHome.png>>attach:classic.CreateWikiPage-003@eXoWikiHome.png]]", Syntax.XWIKI_2_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false));
125 assertEquals(expectedXwikiAttachmentHtml, renderingService.render("[[eXoWikiHome.png>>attach:portal:classic.CreateWikiPage-003@eXoWikiHome.png]]", Syntax.XWIKI_2_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false));
126
127 String expectedConfluenceAttachmentHtml = "<p><span class=\"wikiattachmentlink\"><a href=\"http://portal.exo.com:8080/portal/rest/wiki/images/portal/space/classic/page/CreateWikiPage-003/eXoWikiHome.png\"><span class=\"wikigeneratedlinkcontent\">eXoWikiHome.png</span></a></span></p>";
128 assertEquals(expectedConfluenceAttachmentHtml, renderingService.render("[^eXoWikiHome.png]", Syntax.CONFLUENCE_1_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false));
129 String expectedConfluenceLabelAttachmentHtml = "<p><span class=\"wikiattachmentlink\"><a href=\"http://portal.exo.com:8080/portal/rest/wiki/images/portal/space/classic/page/CreateWikiPage-003/eXoWikiHome.png\">eXoWikiHome.png</a></span></p>";
130 assertEquals(expectedConfluenceLabelAttachmentHtml, renderingService.render("[eXoWikiHome.png|^eXoWikiHome.png]", Syntax.CONFLUENCE_1_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false));
131
132 String expectedImageHtml = "<p><img src=\"http://portal.exo.com:8080/portal/rest/wiki/images/portal/space/classic/page/CreateWikiPage-003/eXoWikiHome.png\" alt=\"eXoWikiHome.png\"/></p>";
133 assertEquals(expectedImageHtml, renderingService.render("[[image:eXoWikiHome.png]]", Syntax.XWIKI_2_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false));
134
135 String expectedImageHtmlPageName = "<p><img src=\"http://portal.exo.com:8080/portal/rest/wiki/images/portal/space/classic/page/CreateWikiPage-003/eXoWikiHome.png\" alt=\"CreateWikiPage-003@eXoWikiHome.png\"/></p>";
136 assertEquals(expectedImageHtmlPageName, renderingService.render("[[image:CreateWikiPage-003@eXoWikiHome.png]]", Syntax.XWIKI_2_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false));
137
138 String expectedImageHtmlWikiName = "<p><img src=\"http://portal.exo.com:8080/portal/rest/wiki/images/portal/space/classic/page/CreateWikiPage-003/eXoWikiHome.png\" alt=\"classic.CreateWikiPage-003@eXoWikiHome.png\"/></p>";
139 assertEquals(expectedImageHtmlWikiName, renderingService.render("[[image:classic.CreateWikiPage-003@eXoWikiHome.png]]", Syntax.XWIKI_2_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false));
140
141 String expectedImageHtmlSpaceName = "<p><img src=\"http://portal.exo.com:8080/portal/rest/wiki/images/portal/space/classic/page/CreateWikiPage-003/eXoWikiHome.png\" alt=\"portal:classic.CreateWikiPage-003@eXoWikiHome.png\"/></p>";
142 assertEquals(expectedImageHtmlSpaceName, renderingService.render("[[image:portal:classic.CreateWikiPage-003@eXoWikiHome.png]]", Syntax.XWIKI_2_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false));
143
144 String expectedFreeStandingImageHtml = "<p><img src=\"http://portal.exo.com:8080/portal/rest/wiki/images/portal/space/classic/page/CreateWikiPage-003/eXoWikiHome.png\" class=\"wikimodel-freestanding\" alt=\"eXoWikiHome.png\"/></p>";
145 assertEquals(expectedFreeStandingImageHtml, renderingService.render("image:eXoWikiHome.png", Syntax.XWIKI_2_0.toIdString(), Syntax.XHTML_1_0.toIdString(), false));
146 }
147
148 public void testGetContentOfSection() throws Exception {
149 String content = "= Section 1 =\n== Section 1.1 ==\n== Section 1.2 ==\n= Section 2 =\n== Section 2.1 ==\n== Section 2.2 ==";
150 assertEquals("= Section 1 =\n\n== Section 1.1 ==\n\n== Section 1.2 ==", renderingService.getContentOfSection(content, Syntax.XWIKI_2_0.toIdString(), "1"));
151 assertEquals("== Section 1.1 ==", renderingService.getContentOfSection(content, Syntax.XWIKI_2_0.toIdString(), "2"));
152 assertEquals("== Section 1.2 ==", renderingService.getContentOfSection(content, Syntax.XWIKI_2_0.toIdString(), "3"));
153 assertEquals("= Section 2 =\n\n== Section 2.1 ==\n\n== Section 2.2 ==", renderingService.getContentOfSection(content, Syntax.XWIKI_2_0.toIdString(), "4"));
154 assertEquals("== Section 2.1 ==", renderingService.getContentOfSection(content, Syntax.XWIKI_2_0.toIdString(), "5"));
155 assertEquals("== Section 2.2 ==", renderingService.getContentOfSection(content, Syntax.XWIKI_2_0.toIdString(), "6"));
156 }
157
158 public void testUpdateContentOfSection() throws Exception {
159 String content = "= Section 1 =\n== Section 1.1 ==\n== Section 1.2 ==\n= Section 2 =\n== Section 2.1 ==\n== Section 2.2 ==";
160 content = renderingService.updateContentOfSection(content, Syntax.XWIKI_2_0.toIdString(), "1", "= Section 1 updated =\n\n== Section 1.1 ==\n\n== Section 1.2 ==");
161 assertEquals("= Section 1 updated =\n\n== Section 1.1 ==\n\n== Section 1.2 ==", renderingService.getContentOfSection(content, Syntax.XWIKI_2_0.toIdString(), "1"));
162 content = renderingService.updateContentOfSection(content, Syntax.XWIKI_2_0.toIdString(), "2", "== Section 1.1 updated ==");
163 assertEquals("== Section 1.1 updated ==", renderingService.getContentOfSection(content, Syntax.XWIKI_2_0.toIdString(), "2"));
164 content = renderingService.updateContentOfSection(content, Syntax.XWIKI_2_0.toIdString(), "3", "== Section 1.2 updated ==");
165 assertEquals("== Section 1.2 updated ==", renderingService.getContentOfSection(content, Syntax.XWIKI_2_0.toIdString(), "3"));
166 content = renderingService.updateContentOfSection(content, Syntax.XWIKI_2_0.toIdString(), "4", "= Section 2 updated =\n\n== Section 2.1 ==\n\n== Section 2.2 ==");
167 assertEquals("= Section 2 updated =\n\n== Section 2.1 ==\n\n== Section 2.2 ==", renderingService.getContentOfSection(content, Syntax.XWIKI_2_0.toIdString(), "4"));
168 content = renderingService.updateContentOfSection(content, Syntax.XWIKI_2_0.toIdString(), "5", "== Section 2.1 updated ==");
169 assertEquals("== Section 2.1 updated ==", renderingService.getContentOfSection(content, Syntax.XWIKI_2_0.toIdString(), "5"));
170 content = renderingService.updateContentOfSection(content, Syntax.XWIKI_2_0.toIdString(), "6", "== Section 2.2 updated ==");
171 assertEquals("== Section 2.2 updated ==", renderingService.getContentOfSection(content, Syntax.XWIKI_2_0.toIdString(), "6"));
172 }
173
174 public void testEscapeString() throws Exception {
175 String expectedHtml = "<p><tt class=\"wikimodel-verbatim\">_</tt>hello</p>";
176 String outputConfluence = renderingService.render("\\_hello",
177 Syntax.CONFLUENCE_1_0.toIdString(),
178 Syntax.XHTML_1_0.toIdString(),
179 false);
180 assertEquals(expectedHtml, outputConfluence);
181 }
182 }