27
27
import org .jsoup .nodes .Document ;
28
28
import org .jsoup .nodes .Element ;
29
29
import org .jsoup .select .Elements ;
30
+ import org .junit .jupiter .api .BeforeEach ;
30
31
import org .junit .jupiter .api .Test ;
31
32
import org .xwiki .context .internal .concurrent .DefaultContextStoreManager ;
32
33
import org .xwiki .localization .macro .internal .TranslationMacro ;
49
50
import org .xwiki .test .annotation .ComponentList ;
50
51
import org .xwiki .test .page .HTML50ComponentList ;
51
52
import org .xwiki .test .page .PageTest ;
53
+ import org .xwiki .test .page .TestNoScriptMacro ;
52
54
import org .xwiki .test .page .XWikiSyntax21ComponentList ;
53
55
54
56
import com .xpn .xwiki .DefaultSkinAccessBridge ;
55
57
import com .xpn .xwiki .doc .XWikiDocument ;
56
58
import com .xpn .xwiki .objects .BaseObject ;
57
59
58
60
import static org .junit .jupiter .api .Assertions .assertEquals ;
61
+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
59
62
import static org .mockito .ArgumentMatchers .any ;
60
63
import static org .mockito .Mockito .mock ;
61
64
import static org .mockito .Mockito .when ;
88
91
// End of XWikiWikiModel
89
92
DocumentXHTMLLinkTypeRenderer .class ,
90
93
DocumentResourceReferenceEntityReferenceResolver .class ,
94
+ TestNoScriptMacro .class ,
91
95
TranslationMacro .class
92
96
})
93
97
class XWikiSyntaxMacrosListPageTest extends PageTest
94
98
{
95
99
public static final DocumentReference DOCUMENT_REFERENCE =
96
100
new DocumentReference ("xwiki" , "XWiki" , "XWikiSyntaxMacrosList" );
97
101
98
- @ Test
99
- void renderTable () throws Exception
102
+ private DefaultWikiMacro myMacro ;
103
+
104
+ @ BeforeEach
105
+ void setUp () throws Exception
100
106
{
101
107
// Initialize "WikiMacroClass"
102
108
this .xwiki .initializeMandatoryDocuments (this .context );
103
109
110
+ // Mock the database.
111
+ Query query = mock (Query .class );
112
+ QueryManagerScriptService queryManagerScriptService =
113
+ this .componentManager .registerMockComponent (ScriptService .class , "query" , QueryManagerScriptService .class ,
114
+ false );
115
+ when (queryManagerScriptService .xwql (any ())).thenReturn (query );
116
+ when (query .execute ()).thenReturn (List .of ("xwiki:XWiki.MyMacro" ));
117
+
104
118
// Create a wiki macro.
105
119
XWikiDocument myMacroDocument = this .xwiki .getDocument (new DocumentReference ("xwiki" , "XWiki" , "MyMacro" ),
106
120
this .context );
@@ -111,20 +125,17 @@ void renderTable() throws Exception
111
125
this .xwiki .saveDocument (myMacroDocument , this .context );
112
126
113
127
// Register the wiki macro component.
114
- DefaultWikiMacro myMacro =
128
+ this . myMacro =
115
129
this .componentManager .registerMockComponent (Macro .class , "mymacro" , DefaultWikiMacro .class , false );
116
- DefaultMacroDescriptor macroDescriptor =
117
- new DefaultMacroDescriptor (new MacroId ("mymacro" ), "My Macro" , "My Macro Description" );
118
- macroDescriptor .setDefaultCategories (Set .of ("Category1" , "Category2" ));
119
- when (myMacro .getDescriptor ()).thenReturn (macroDescriptor );
130
+ }
120
131
121
- // Mock the database.
122
- Query query = mock ( Query . class );
123
- QueryManagerScriptService queryManagerScriptService =
124
- this . componentManager . registerMockComponent ( ScriptService . class , "query" , QueryManagerScriptService . class ,
125
- false );
126
- when ( queryManagerScriptService . xwql ( any ())). thenReturn ( query );
127
- when (query . execute ()).thenReturn (List . of ( "xwiki:XWiki.MyMacro" ) );
132
+ @ Test
133
+ void renderTable () throws Exception
134
+ {
135
+ DefaultMacroDescriptor macroDescriptor = new DefaultMacroDescriptor ( new MacroId ( "mymacro" ), "My Macro" ,
136
+ "My Macro Description" );
137
+ macroDescriptor . setDefaultCategories ( Set . of ( "Category1" , "Category2" ) );
138
+ when (this . myMacro . getDescriptor ()).thenReturn (macroDescriptor );
128
139
129
140
// Render the page.
130
141
Document document = renderHTMLPage (DOCUMENT_REFERENCE );
@@ -144,12 +155,39 @@ void renderTable() throws Exception
144
155
"XWiki.WikiMacroClass_visibility_Global" );
145
156
assertWikiMacro (trs .get (3 ), "mymacro" , "/xwiki/bin/view/XWiki/MyMacro" , "My Macro" ,
146
157
Set .of ("Category1" , "Category2" ), "My Macro Description" , "XWiki.WikiMacroClass_visibility_WIKI" );
147
- assertJavaMacro (trs .get (4 ), "translation" , "Translation" , "Content" ,
158
+ assertJavaMacro (trs .get (4 ), "noscript" , "NoScript" , "" , "No Script!" , "XWiki.WikiMacroClass_visibility_Global" );
159
+ assertJavaMacro (trs .get (5 ), "translation" , "Translation" , "Content" ,
148
160
"Display a translation message." , "XWiki.WikiMacroClass_visibility_Global" );
149
- assertJavaMacro (trs .get (5 ), "velocity" , "Velocity" , "Development" , "Executes a Velocity script." ,
161
+ assertJavaMacro (trs .get (6 ), "velocity" , "Velocity" , "Development" , "Executes a Velocity script." ,
150
162
"XWiki.WikiMacroClass_visibility_Global" );
151
163
}
152
164
165
+ @ Test
166
+ void checkTableEscaping () throws Exception
167
+ {
168
+ String unescapedString = "{{noscript /}}" ;
169
+
170
+ DefaultMacroDescriptor macroDescriptor = new DefaultMacroDescriptor (new MacroId ("mymacro" ), unescapedString ,
171
+ unescapedString );
172
+ macroDescriptor .setDefaultCategories (Set .of (unescapedString ));
173
+ when (this .myMacro .getDescriptor ()).thenReturn (macroDescriptor );
174
+
175
+ Document document = renderHTMLPage (DOCUMENT_REFERENCE );
176
+
177
+ Elements trs = document .select ("tr" );
178
+ Element myMacroTr = null ;
179
+ for (Element tr : trs ) {
180
+ Element th = tr .selectFirst ("td" );
181
+ if (th != null && th .text ().equals ("mymacro" )) {
182
+ myMacroTr = tr ;
183
+ }
184
+ }
185
+
186
+ assertNotNull (myMacroTr );
187
+ assertWikiMacro (myMacroTr , "mymacro" , "/xwiki/bin/view/XWiki/MyMacro" , unescapedString , Set .of (unescapedString ),
188
+ unescapedString , "XWiki.WikiMacroClass_visibility_WIKI" );
189
+ }
190
+
153
191
private void assertWikiMacro (Element rowElement , String id , String link , String name , Set <String > categories ,
154
192
String description , String visibility )
155
193
{
0 commit comments