1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.pdf.impl

File PdfExportImplTest.java

 

Code metrics

0
17
1
1
138
89
1
0.06
17
1
1

Classes

Class Line # Actions
PdfExportImplTest 45 17 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.pdf.impl;
21   
22    import org.junit.Rule;
23    import org.junit.Test;
24    import org.xwiki.bridge.DocumentAccessBridge;
25    import org.xwiki.environment.Environment;
26    import org.xwiki.model.reference.DocumentReferenceResolver;
27    import org.xwiki.model.reference.EntityReferenceSerializer;
28    import org.xwiki.test.AllLogRule;
29    import org.xwiki.velocity.VelocityManager;
30   
31    import com.xpn.xwiki.XWikiContext;
32    import com.xpn.xwiki.doc.XWikiDocument;
33    import com.xpn.xwiki.internal.pdf.XSLFORenderer;
34    import com.xpn.xwiki.test.MockitoOldcoreRule;
35   
36    import static org.junit.Assert.assertEquals;
37    import static org.mockito.Mockito.mock;
38    import static org.mockito.Mockito.when;
39   
40    /**
41    * Unit tests for {@link PdfExportImpl}.
42    *
43    * @version $Id: 7dfa519511cf7a815f1249f6a5385436ac0d2e97 $
44    */
 
45    public class PdfExportImplTest
46    {
47    @Rule
48    public AllLogRule logRule = new AllLogRule();
49   
50    @Rule
51    public MockitoOldcoreRule oldcoreRule = new MockitoOldcoreRule();
52   
53    /**
54    * Verify that PDF Export can apply some CSS on the XHTML when that XHTML already has some style defined and in
55    * shorthand notation.
56    */
 
57  1 toggle @Test
58    public void applyCSSWhenExistingStyleDefinedUsingShorthandNotation() throws Exception
59    {
60  1 this.oldcoreRule.getMocker().registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "currentmixed");
61  1 this.oldcoreRule.getMocker().registerMockComponent(EntityReferenceSerializer.TYPE_STRING);
62  1 this.oldcoreRule.getMocker().registerMockComponent(DocumentAccessBridge.class);
63  1 this.oldcoreRule.getMocker().registerMockComponent(DocumentAccessBridge.class);
64  1 this.oldcoreRule.getMocker().registerMockComponent(PDFResourceResolver.class);
65  1 this.oldcoreRule.getMocker().registerMockComponent(Environment.class);
66  1 this.oldcoreRule.getMocker().registerMockComponent(VelocityManager.class);
67  1 this.oldcoreRule.getMocker().registerMockComponent(XSLFORenderer.class, "fop");
68   
69  1 PdfExportImpl pdfExport = new PdfExportImpl();
70   
71    // The content below allows us to test several points:
72    // 1) The SPAN below already has some style defined in shorthand notation( "background" is shorthand,
73    // see https://www.w3schools.com/css/css_background.asp). That's important for the test since that's what was
74    // failing in the past and why this test was written.
75    // 2) We also test that HTML entities are correctly kept since we had issues with this at one point.
76  1 String html = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
77    + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
78    + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
79    + "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head>\n"
80    + "<title>\n"
81    + " Main.ttt - ttt\n"
82    + "</title>\n"
83    + "<meta content=\"text/html; charset=UTF-8\" http-equiv=\"Content-Type\" />\n"
84    + "<meta content=\"en\" name=\"language\" />\n"
85    + "\n"
86    + "</head><body class=\"exportbody\" id=\"body\" pdfcover=\"0\" pdftoc=\"0\">\n"
87    + "\n"
88    + "<div id=\"xwikimaincontainer\">\n"
89    + "<div id=\"xwikimaincontainerinner\">\n"
90    + "\n"
91    + "<div id=\"xwikicontent\">\n"
92    + " <p><span style=\"background: white;\">Hello Cl&eacute;ment</span></p>\n"
93    + " </div>\n"
94    + "</div>\n"
95    + "</div>\n"
96    + "\n"
97    + "</body></html>";
98   
99  1 String css = "span { color:red; }";
100   
101  1 XWikiContext xcontext = this.oldcoreRule.getXWikiContext();
102  1 XWikiDocument doc = mock(XWikiDocument.class);
103  1 when(doc.getExternalURL("view", xcontext)).thenReturn("http://localhost:8080/export");
104  1 xcontext.setDoc(doc);
105   
106    // - Verify that element's style attributes are normalized and that the SPAN's color is set to red.
107    // - Verify that the accent in the content is still there.
108    // TODO: right now we output the DOM with DOM4J and use the default of converting entities when using the
109    // XMLWriter. We need to decide if that's correct or if we should call XMLWriter#setResolveEntityRefs(false)
110    // instead.
111  1 String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
112    + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
113    + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
114    + "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head style=\"display: none; \">"
115    + "<title style=\"display: none; \">\n"
116    + " Main.ttt - ttt\n"
117    + "</title>"
118    + "<meta content=\"text/html; charset=UTF-8\" http-equiv=\"Content-Type\" style=\"display: none; \"/>"
119    + "<meta content=\"en\" name=\"language\" style=\"display: none; \"/>"
120    + "</head><body class=\"exportbody\" id=\"body\" pdfcover=\"0\" pdftoc=\"0\" "
121    + "style=\"display: block; margin-top: 8px; margin-right: 8px; margin-bottom: 8px; margin-left: 8px; "
122    + "unicode-bidi: embed; \">"
123    + "<div id=\"xwikimaincontainer\" style=\"display: block; unicode-bidi: embed; \">\n"
124    + "<div id=\"xwikimaincontainerinner\" style=\"display: block; unicode-bidi: embed; \">\n"
125    + "\n"
126    + "<div id=\"xwikicontent\" style=\"display: block; unicode-bidi: embed; \">\n"
127    + " <p style=\"display: block; margin-top: 3pt; margin-bottom: 3pt; unicode-bidi: embed; \">"
128    + "<span style=\"color: #f00; background-color: #fff; background-image: none; "
129    + "background-position: 0% 0%; background-size: auto auto; background-origin: padding-box; "
130    + "background-clip: border-box; background-repeat: repeat repeat; "
131    + "background-attachment: scroll; \">Hello ClĂ©ment</span></p>\n"
132    + " </div>\n"
133    + "</div>\n"
134    + "</div>"
135    + "</body></html>";
136  1 assertEquals(expected, pdfExport.applyCSS(html, css, xcontext));
137    }
138    }