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