1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.internal.macro

File IncludeMacroTest.java

 
testIncludeMacroWithCurrentContextShowsVelocityMacrosAreShared: Failed to lookup component [org.xwiki.test.rendering.velo...
testIncludeMacroWithNewContextShowsVelocityMacrosAreIsolated: Failed to lookup component [org.xwiki.test.rendering.velo...
testIncludeMacro: Failed to lookup default document displayer.
testIncludeMacroWhenInvalidSectionSpecified: expected:<[Cannot find section [unknown] in document [wik...
testIncludeMacroWithNewContextShowsPassingOnRestrictedFlag: Failed to lookup default document displayer.
testIncludeMacroWhenIncludingDocumentWithRelativeReferences: Failed to lookup default document displayer.
testIncludeMacroWhenSectionSpecified: Failed to lookup default document displayer.
 

Code metrics

8
218
28
2
609
481
37
0.17
7.79
14
1.32

Classes

Class Line # Actions
IncludeMacroTest 76 218 0% 37 168
0.3385826633.9%
IncludeMacroTest.ExpectedRecursiveInclusionException 306 0 - 0 0
-1.0 -
 

Contributing tests

This file is covered by 11 tests. .

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 org.xwiki.rendering.internal.macro;
21   
22    import java.io.StringReader;
23    import java.io.StringWriter;
24    import java.util.Collections;
25    import java.util.HashMap;
26    import java.util.List;
27    import java.util.Map;
28   
29    import org.hamcrest.collection.IsArray;
30    import org.jmock.Expectations;
31    import org.jmock.api.Invocation;
32    import org.jmock.lib.action.CustomAction;
33    import org.junit.Assert;
34    import org.junit.Test;
35    import org.xwiki.bridge.DocumentModelBridge;
36    import org.xwiki.context.Execution;
37    import org.xwiki.display.internal.DocumentDisplayer;
38    import org.xwiki.display.internal.DocumentDisplayerParameters;
39    import org.xwiki.model.EntityType;
40    import org.xwiki.model.reference.DocumentReference;
41    import org.xwiki.model.reference.EntityReference;
42    import org.xwiki.model.reference.EntityReferenceResolver;
43    import org.xwiki.rendering.block.Block;
44    import org.xwiki.rendering.block.MacroBlock;
45    import org.xwiki.rendering.block.MacroMarkerBlock;
46    import org.xwiki.rendering.block.MetaDataBlock;
47    import org.xwiki.rendering.block.XDOM;
48    import org.xwiki.rendering.internal.macro.include.IncludeMacro;
49    import org.xwiki.rendering.internal.transformation.macro.MacroTransformation;
50    import org.xwiki.rendering.listener.MetaData;
51    import org.xwiki.rendering.macro.Macro;
52    import org.xwiki.rendering.macro.MacroExecutionException;
53    import org.xwiki.rendering.macro.include.IncludeMacroParameters;
54    import org.xwiki.rendering.macro.include.IncludeMacroParameters.Context;
55    import org.xwiki.rendering.macro.script.ScriptMockSetup;
56    import org.xwiki.rendering.parser.Parser;
57    import org.xwiki.rendering.renderer.PrintRendererFactory;
58    import org.xwiki.rendering.syntax.Syntax;
59    import org.xwiki.rendering.transformation.MacroTransformationContext;
60    import org.xwiki.rendering.transformation.Transformation;
61    import org.xwiki.security.authorization.AuthorizationManager;
62    import org.xwiki.security.authorization.ContextualAuthorizationManager;
63    import org.xwiki.security.authorization.Right;
64    import org.xwiki.test.jmock.AbstractComponentTestCase;
65    import org.xwiki.velocity.VelocityManager;
66   
67    import static org.xwiki.rendering.test.BlockAssert.assertBlocks;
68    import static org.xwiki.rendering.test.BlockAssert.assertBlocksStartsWith;
69   
70    /**
71    * Unit tests for {@link IncludeMacro}.
72    *
73    * @version $Id: 20cd62d8215ee4018afecfb5144eba435e4cf25b $
74    * @since 1.5M2
75    */
 
76    public class IncludeMacroTest extends AbstractComponentTestCase
77    {
78    private ScriptMockSetup mockSetup;
79   
80    private IncludeMacro includeMacro;
81   
82    private PrintRendererFactory rendererFactory;
83   
84    /**
85    * Mocks the component that is used to resolve the 'reference' parameter.
86    */
87    private EntityReferenceResolver<String> mockEntityReferenceResolver;
88   
89    private ContextualAuthorizationManager mockContextualAuthorization;
90   
91    private DocumentModelBridge mockDocument;
92   
 
93  11 toggle @Override
94    public void setUp() throws Exception
95    {
96  11 super.setUp();
97   
98    // Put a fake XWiki context on the execution context.
99  11 Execution execution = getComponentManager().getInstance(Execution.class);
100  11 execution.getContext().setProperty("xwikicontext", new HashMap<Object, Object>());
101    }
102   
 
103  11 toggle @Override
104    protected void registerComponents() throws Exception
105    {
106  11 super.registerComponents();
107   
108  11 this.mockSetup = new ScriptMockSetup(getMockery(), getComponentManager());
109   
110  11 this.mockEntityReferenceResolver =
111    registerMockComponent(EntityReferenceResolver.TYPE_STRING, "macro", "macroDocumentReferenceResolver");
112    // Make sure to not load the standard AuthorizationManager which trigger too many things
113  11 registerMockComponent(AuthorizationManager.class);
114   
115  11 this.mockContextualAuthorization = getComponentManager().getInstance(ContextualAuthorizationManager.class);
116  11 this.includeMacro = getComponentManager().getInstance(Macro.class, "include");
117  11 this.rendererFactory = getComponentManager().getInstance(PrintRendererFactory.class, "event/1.0");
118    }
119   
 
120  0 toggle @Test
121    public void testIncludeMacro() throws Exception
122    {
123    // @formatter:off
124  0 String expected = "beginDocument\n"
125    + "beginMetaData [[source]=[wiki:Space.IncludedPage][syntax]=[XWiki 2.0]]\n"
126    + "beginParagraph\n"
127    + "onWord [word]\n"
128    + "endParagraph\n"
129    + "endMetaData [[source]=[wiki:Space.IncludedPage][syntax]=[XWiki 2.0]]\n"
130    + "endDocument";
131    // @formatter:on
132   
133  0 Test failure here List<Block> blocks = runIncludeMacro(Context.CURRENT, "word", false);
134   
135  0 assertBlocks(expected, blocks, this.rendererFactory);
136    }
137   
 
138  0 toggle @Test
139    public void testIncludeMacroWithNewContextShowsVelocityMacrosAreIsolated() throws Exception
140    {
141    // @formatter:off
142  0 String expected = "beginDocument\n"
143    + "beginMetaData [[base]=[wiki:Space.IncludedPage][source]=[wiki:Space.IncludedPage][syntax]=[XWiki 2.0]]\n"
144    + "beginMacroMarkerStandalone [velocity] [] [#testmacro]\n"
145    + "beginParagraph\n"
146    + "onSpecialSymbol [#]\n"
147    + "onWord [testmacro]\n"
148    + "endParagraph\n"
149    + "endMacroMarkerStandalone [velocity] [] [#testmacro]\n"
150    + "endMetaData [[base]=[wiki:Space.IncludedPage][source]=[wiki:Space.IncludedPage][syntax]=[XWiki 2.0]]\n"
151    + "endDocument";
152    // @formatter:on
153   
154    // We verify that a Velocity macro set in the including page is not seen in the included page.
155  0 List<Block> blocks =
156    Test failure here runIncludeMacroWithPreVelocity(Context.NEW, "#macro(testmacro)#end", "{{velocity}}#testmacro{{/velocity}}");
157   
158  0 assertBlocks(expected, blocks, this.rendererFactory);
159    }
160   
 
161  0 toggle @Test
162    public void testIncludeMacroWithNewContextShowsPassingOnRestrictedFlag() throws Exception
163    {
164    // @formatter:off
165  0 String expected = "beginDocument\n"
166    + "beginMetaData "
167    + "[[base]=[wiki:Space.IncludedPage][source]=[wiki:Space.IncludedPage][syntax]=[XWiki 2.0]]\n"
168    + "beginMacroMarkerStandalone [velocity] [] [$foo]\n"
169    + "beginGroup [[class]=[xwikirenderingerror]]\n"
170    + "onWord [Failed to execute the [velocity] macro. Cause: [The execution of the [velocity] script "
171    + "macro is not allowed. Check the rights of its last author or the parameters if it's rendered "
172    + "from another script.]. Click on this message for details.]\n"
173    + "endGroup [[class]=[xwikirenderingerror]]\n"
174    + "beginGroup [[class]=[xwikirenderingerrordescription hidden]]\n"
175    + "onVerbatim [org.xwiki.rendering.macro.MacroExecutionException: "
176    + "The execution of the [velocity] script macro is not allowed.";
177    // @formatter:on
178   
179    // We verify that a Velocity macro set in the including page is not seen in the included page.
180  0 Test failure here List<Block> blocks = runIncludeMacro(Context.NEW, "{{velocity}}$foo{{/velocity}}", true);
181   
182  0 assertBlocksStartsWith(expected, blocks, this.rendererFactory);
183   
184    }
185   
 
186  0 toggle @Test
187    public void testIncludeMacroWithCurrentContextShowsVelocityMacrosAreShared() throws Exception
188    {
189    // @formatter:off
190  0 String expected = "beginDocument\n"
191    + "beginMetaData [[source]=[wiki:Space.IncludedPage][syntax]=[XWiki 2.0]]\n"
192    + "onMacroStandalone [velocity] [] [#testmacro]\n"
193    + "endMetaData [[source]=[wiki:Space.IncludedPage][syntax]=[XWiki 2.0]]\n"
194    + "endDocument";
195    // @formatter:on
196   
197    // We verify that a Velocity macro set in the including page is seen in the included page.
198  0 Test failure here List<Block> blocks = runIncludeMacroWithPreVelocity(Context.CURRENT, "#macro(testmacro)#end",
199    "{{velocity}}#testmacro{{/velocity}}");
200   
201  0 assertBlocks(expected, blocks, this.rendererFactory);
202    }
203   
 
204  1 toggle @Test
205    public void testIncludeMacroWithNoDocumentSpecified() throws Exception
206    {
207  1 IncludeMacroParameters parameters = new IncludeMacroParameters();
208   
209  1 try {
210  1 this.includeMacro.execute(parameters, null, createMacroTransformationContext("whatever", false));
211  0 Assert.fail("An exception should have been thrown");
212    } catch (MacroExecutionException expected) {
213  1 Assert.assertEquals("You must specify a 'reference' parameter pointing to the entity to include.",
214    expected.getMessage());
215    }
216    }
217   
218    /**
219    * Verify that relative links returned by the Include macro as wrapped with a MetaDataBlock.
220    */
 
221  0 toggle @Test
222    public void testIncludeMacroWhenIncludingDocumentWithRelativeReferences() throws Exception
223    {
224    // @formatter:off
225  0 String expected = "beginDocument\n"
226    + "beginMetaData [[base]=[includedWiki:includedSpace.includedPage]"
227    + "[source]=[includedWiki:includedSpace.includedPage][syntax]=[XWiki 2.0]]\n"
228    + "beginParagraph\n"
229    + "beginLink [Typed = [false] Type = [doc] Reference = [page]] [false]\n"
230    + "endLink [Typed = [false] Type = [doc] Reference = [page]] [false]\n"
231    + "onSpace\n"
232    + "beginLink [Typed = [true] Type = [attach] Reference = [test.png]] [false]\n"
233    + "endLink [Typed = [true] Type = [attach] Reference = [test.png]] [false]\n"
234    + "onSpace\n"
235    + "onImage [Typed = [false] Type = [attach] Reference = [test.png]] [true]\n"
236    + "endParagraph\n"
237    + "endMetaData [[base]=[includedWiki:includedSpace.includedPage]"
238    + "[source]=[includedWiki:includedSpace.includedPage][syntax]=[XWiki 2.0]]\n"
239    + "endDocument";
240    // @formatter:on
241   
242  0 final DocumentReference includedDocumentReference =
243    new DocumentReference("includedWiki", "includedSpace", "includedPage");
244  0 setUpDocumentMock("includedWiki:includedSpace.includedPage", includedDocumentReference,
245    "[[page]] [[attach:test.png]] image:test.png");
246  0 getMockery().checking(new Expectations()
247    {
 
248  0 toggle {
249  0 oneOf(mockContextualAuthorization).hasAccess(with(Right.VIEW), with(any(EntityReference.class)));
250  0 will(returnValue(true));
251  0 oneOf(mockSetup.bridge).pushDocumentInContext(with(any(Map.class)),
252    with(any(DocumentModelBridge.class)));
253  0 oneOf(mockSetup.bridge).getCurrentDocumentReference();
254  0 will(returnValue(includedDocumentReference));
255  0 oneOf(mockSetup.bridge).popDocumentFromContext(with(any(Map.class)));
256    }
257    });
258   
259  0 IncludeMacroParameters parameters = new IncludeMacroParameters();
260  0 parameters.setReference("includedWiki:includedSpace.includedPage");
261  0 parameters.setContext(Context.NEW);
262   
263  0 List<Block> blocks =
264    Test failure here this.includeMacro.execute(parameters, null, createMacroTransformationContext("whatever", false));
265   
266  0 assertBlocks(expected, blocks, this.rendererFactory);
267    }
268   
 
269  1 toggle @Test
270    public void testIncludeMacroWithRecursiveIncludeContextCurrent() throws Exception
271    {
272  1 this.includeMacro.setDocumentAccessBridge(mockSetup.bridge);
273   
274  1 final MacroTransformationContext macroContext = createMacroTransformationContext("wiki:space.page", false);
275    // Add an Include Macro MarkerBlock as a parent of the include Macro block since this is what would have
276    // happened if an Include macro is included in another Include macro.
277  1 final MacroMarkerBlock includeMacroMarker =
278    new MacroMarkerBlock("include", Collections.singletonMap("reference", "space.page"),
279    Collections.<Block>singletonList(macroContext.getCurrentMacroBlock()), false);
280   
281  1 getMockery().checking(new Expectations()
282    {
 
283  1 toggle {
284  1 allowing(mockEntityReferenceResolver).resolve("wiki:space.page", EntityType.DOCUMENT,
285    macroContext.getCurrentMacroBlock());
286  1 will(returnValue(new DocumentReference("wiki", "space", "page")));
287  1 allowing(mockEntityReferenceResolver).resolve("space.page", EntityType.DOCUMENT, includeMacroMarker);
288  1 will(returnValue(new DocumentReference("wiki", "space", "page")));
289    }
290    });
291   
292  1 IncludeMacroParameters parameters = new IncludeMacroParameters();
293  1 parameters.setReference("wiki:space.page");
294  1 parameters.setContext(Context.CURRENT);
295   
296  1 try {
297  1 this.includeMacro.execute(parameters, null, macroContext);
298  0 Assert.fail("The include macro hasn't checked the recursive inclusion");
299    } catch (MacroExecutionException expected) {
300  1 if (!expected.getMessage().startsWith("Found recursive inclusion")) {
301  0 throw expected;
302    }
303    }
304    }
305   
 
306    private static class ExpectedRecursiveInclusionException extends RuntimeException
307    {
308    }
309   
 
310  1 toggle @Test
311    public void testIncludeMacroWithRecursiveIncludeContextNew() throws Exception
312    {
313  1 final DocumentDisplayer mockDocumentDisplayer = getMockery().mock(DocumentDisplayer.class);
314   
315  1 this.includeMacro.setDocumentAccessBridge(mockSetup.bridge);
316  1 this.includeMacro.setDocumentDisplayer(mockDocumentDisplayer);
317   
318  1 final MacroTransformationContext macroContext = createMacroTransformationContext("wiki:space.page", false);
319   
320  1 final IncludeMacroParameters parameters = new IncludeMacroParameters();
321  1 parameters.setReference("wiki:space.page");
322  1 parameters.setContext(Context.NEW);
323   
324  1 final DocumentReference includedDocumentReference = new DocumentReference("wiki", "space", "page");
325  1 setUpDocumentMock("wiki:space.page", includedDocumentReference, "");
326  1 getMockery().checking(new Expectations()
327    {
 
328  1 toggle {
329  1 oneOf(mockContextualAuthorization).hasAccess(with(Right.VIEW), with(any(EntityReference.class)));
330  1 will(returnValue(true));
331   
332  1 allowing(mockDocumentDisplayer).display(with(same(mockDocument)),
333    with(any(DocumentDisplayerParameters.class)));
334  1 will(new CustomAction("recursively call the include macro again")
335    {
 
336  1 toggle @Override
337    public Object invoke(Invocation invocation) throws Throwable
338    {
339  1 try {
340  1 includeMacro.execute(parameters, null, macroContext);
341    } catch (Exception expected) {
342  1 if (expected.getMessage().contains("Found recursive inclusion")) {
343  1 throw new ExpectedRecursiveInclusionException();
344    }
345    }
346  0 return true;
347    }
348    });
349    }
350    });
351   
352  1 try {
353  1 this.includeMacro.execute(parameters, null, macroContext);
354  0 Assert.fail("The include macro hasn't checked the recursive inclusion");
355    } catch (MacroExecutionException expected) {
356  1 if (!(expected.getCause() instanceof ExpectedRecursiveInclusionException)) {
357  0 throw expected;
358    }
359    }
360    }
361   
 
362  0 toggle @Test
363    public void testIncludeMacroInsideSourceMetaDataBlockAndWithRelativeDocumentReferencePassed() throws Exception
364    {
365    // @formatter:off
366  0 String expected = "beginDocument\n"
367    + "beginMetaData [[source]=[wiki:space.relativePage][syntax]=[XWiki 2.0]]\n"
368    + "beginParagraph\n"
369    + "onWord [content]\n"
370    + "endParagraph\n"
371    + "endMetaData [[source]=[wiki:space.relativePage][syntax]=[XWiki 2.0]]\n"
372    + "endDocument";
373    // @formatter:on
374   
375  0 IncludeMacroParameters parameters = new IncludeMacroParameters();
376  0 parameters.setReference("relativePage");
377   
378  0 final MacroTransformationContext macroContext = createMacroTransformationContext("whatever", false);
379    // Add a Source MetaData Block as a parent of the include Macro block.
380  0 new MetaDataBlock(Collections.<Block>singletonList(macroContext.getCurrentMacroBlock()),
381    new MetaData(Collections.<String, Object>singletonMap(MetaData.BASE, "wiki:space.page")));
382   
383  0 final DocumentReference sourceReference = new DocumentReference("wiki", "space", "page");
384  0 final DocumentReference resolvedReference = new DocumentReference("wiki", "space", "relativePage");
385  0 final DocumentModelBridge mockDocument = getMockery().mock(DocumentModelBridge.class);
386  0 getMockery().checking(new Expectations()
387    {
 
388  0 toggle {
389  0 oneOf(mockEntityReferenceResolver).resolve("relativePage", EntityType.DOCUMENT,
390    macroContext.getCurrentMacroBlock());
391  0 will(returnValue(resolvedReference));
392  0 oneOf(mockContextualAuthorization).hasAccess(with(Right.VIEW), with(resolvedReference));
393  0 will(returnValue(true));
394  0 oneOf(mockSetup.bridge).getDocumentInstance((EntityReference) resolvedReference);
395  0 will(returnValue(mockDocument));
396  0 oneOf(mockSetup.bridge).getTranslatedDocumentInstance(resolvedReference);
397  0 will(returnValue(mockDocument));
398  0 oneOf(mockSetup.bridge).getCurrentDocumentReference();
399  0 will(returnValue(sourceReference));
400  0 oneOf(mockDocument).getXDOM();
401  0 will(returnValue(getXDOM("content")));
402  0 oneOf(mockDocument).getSyntax();
403  0 will(returnValue(Syntax.XWIKI_2_0));
404  0 allowing(mockDocument).getDocumentReference();
405  0 will(returnValue(resolvedReference));
406  0 allowing(mockDocument).getRealLanguage();
407  0 will(returnValue(""));
408    }
409    });
410   
411  0 Test failure here List<Block> blocks = this.includeMacro.execute(parameters, null, macroContext);
412   
413  0 assertBlocks(expected, blocks, this.rendererFactory);
414    }
415   
 
416  0 toggle @Test
417    public void testIncludeMacroWhenSectionSpecified() throws Exception
418    {
419    // @formatter:off
420  0 String expected = "beginDocument\n"
421    + "beginMetaData [[source]=[wiki:space.document][syntax]=[XWiki 2.0]]\n"
422    + "beginHeader [1, Hsection]\n"
423    + "onWord [section]\n"
424    + "endHeader [1, Hsection]\n"
425    + "beginParagraph\n"
426    + "onWord [content2]\n"
427    + "endParagraph\n"
428    + "endMetaData [[source]=[wiki:space.document][syntax]=[XWiki 2.0]]\n"
429    + "endDocument";
430    // @formatter:on
431   
432  0 IncludeMacroParameters parameters = new IncludeMacroParameters();
433  0 parameters.setReference("document");
434  0 parameters.setSection("Hsection");
435   
436  0 final MacroTransformationContext macroContext = createMacroTransformationContext("whatever", false);
437  0 final DocumentReference resolvedReference = new DocumentReference("wiki", "space", "document");
438  0 final DocumentModelBridge mockDocument = getMockery().mock(DocumentModelBridge.class);
439  0 getMockery().checking(new Expectations()
440    {
 
441  0 toggle {
442  0 oneOf(mockEntityReferenceResolver).resolve("document", EntityType.DOCUMENT,
443    macroContext.getCurrentMacroBlock());
444  0 will(returnValue(resolvedReference));
445  0 oneOf(mockContextualAuthorization).hasAccess(with(Right.VIEW), with(resolvedReference));
446  0 will(returnValue(true));
447  0 oneOf(mockSetup.bridge).getDocumentInstance((EntityReference) resolvedReference);
448  0 will(returnValue(mockDocument));
449  0 oneOf(mockSetup.bridge).getTranslatedDocumentInstance(resolvedReference);
450  0 will(returnValue(mockDocument));
451  0 oneOf(mockSetup.bridge).getCurrentDocumentReference();
452  0 will(returnValue(new DocumentReference("wiki", "Space", "IncludingPage")));
453  0 allowing(mockDocument).getDocumentReference();
454  0 will(returnValue(resolvedReference));
455  0 oneOf(mockDocument).getSyntax();
456  0 will(returnValue(Syntax.XWIKI_2_0));
457  0 oneOf(mockDocument).getXDOM();
458  0 will(returnValue(getXDOM("content1\n\n= section =\ncontent2")));
459  0 allowing(mockDocument).getRealLanguage();
460  0 will(returnValue(""));
461    }
462    });
463   
464  0 Test failure here List<Block> blocks = this.includeMacro.execute(parameters, null, macroContext);
465   
466  0 assertBlocks(expected, blocks, this.rendererFactory);
467    }
468   
 
469  0 toggle @Test
470    public void testIncludeMacroWhenInvalidSectionSpecified() throws Exception
471    {
472  0 IncludeMacroParameters parameters = new IncludeMacroParameters();
473  0 parameters.setReference("document");
474  0 parameters.setSection("unknown");
475   
476  0 final MacroTransformationContext macroContext = createMacroTransformationContext("whatever", false);
477  0 final DocumentReference resolvedReference = new DocumentReference("wiki", "space", "document");
478  0 final DocumentModelBridge mockDocument = getMockery().mock(DocumentModelBridge.class);
479  0 getMockery().checking(new Expectations()
480    {
 
481  0 toggle {
482  0 oneOf(mockEntityReferenceResolver).resolve("document", EntityType.DOCUMENT,
483    macroContext.getCurrentMacroBlock());
484  0 will(returnValue(resolvedReference));
485  0 oneOf(mockContextualAuthorization).hasAccess(with(Right.VIEW), with(resolvedReference));
486  0 will(returnValue(true));
487  0 oneOf(mockSetup.bridge).getDocumentInstance((EntityReference) resolvedReference);
488  0 will(returnValue(mockDocument));
489  0 allowing(mockSetup.bridge).getTranslatedDocumentInstance(resolvedReference);
490  0 will(returnValue(mockDocument));
491  0 oneOf(mockSetup.bridge).getCurrentDocumentReference();
492  0 will(returnValue(new DocumentReference("wiki", "Space", "IncludingPage")));
493  0 oneOf(mockDocument).getSyntax();
494  0 will(returnValue(Syntax.XWIKI_2_0));
495  0 oneOf(mockDocument).getXDOM();
496  0 will(returnValue(getXDOM("content")));
497  0 allowing(mockDocument).getDocumentReference();
498  0 will(returnValue(resolvedReference));
499  0 allowing(mockDocument).getRealLanguage();
500  0 will(returnValue(""));
501    }
502    });
503   
504  0 try {
505  0 this.includeMacro.execute(parameters, null, macroContext);
506  0 Assert.fail("Should have raised an exception");
507    } catch (MacroExecutionException expected) {
508  0 Test failure here Assert.assertEquals("Cannot find section [unknown] in document [wiki:space.document]",
509    expected.getMessage());
510    }
511    }
512   
 
513  9 toggle private MacroTransformationContext createMacroTransformationContext(String documentName, boolean isInline)
514    {
515  9 MacroTransformationContext context = new MacroTransformationContext();
516  9 MacroBlock includeMacro =
517    new MacroBlock("include", Collections.singletonMap("reference", documentName), isInline);
518  9 context.setCurrentMacroBlock(includeMacro);
519  9 return context;
520    }
521   
 
522  4 toggle private void setUpDocumentMock(final String resolve, final DocumentReference reference, final String content)
523    throws Exception
524    {
525  4 this.mockDocument = getMockery().mock(DocumentModelBridge.class, resolve);
526  4 getMockery().checking(new Expectations()
527    {
 
528  4 toggle {
529  4 allowing(mockEntityReferenceResolver).resolve(with(resolve), with(EntityType.DOCUMENT),
530    with(IsArray.array(any(MacroBlock.class))));
531  4 will(returnValue(reference));
532  4 allowing(mockSetup.bridge).getDocumentInstance((EntityReference) reference);
533  4 will(returnValue(mockDocument));
534  4 allowing(mockSetup.bridge).getTranslatedDocumentInstance(reference);
535  4 will(returnValue(mockDocument));
536  4 allowing(mockDocument).getSyntax();
537  4 will(returnValue(Syntax.XWIKI_2_0));
538  4 allowing(mockDocument).getXDOM();
539  4 will(returnValue(getXDOM(content)));
540  4 allowing(mockDocument).getDocumentReference();
541  4 will(returnValue(reference));
542  4 allowing(mockDocument).getRealLanguage();
543  4 will(returnValue(""));
544    }
545    });
546    }
547   
 
548  7 toggle private XDOM getXDOM(String content) throws Exception
549    {
550  7 Parser parser = getComponentManager().getInstance(Parser.class, "xwiki/2.0");
551  7 return parser.parse(new StringReader(content));
552    }
553   
 
554  0 toggle private List<Block> runIncludeMacroWithPreVelocity(Context context, String velocity, String includedContent)
555    throws Exception
556    {
557  0 Test failure here VelocityManager velocityManager = getComponentManager().getInstance(VelocityManager.class);
558  0 StringWriter writer = new StringWriter();
559  0 velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer,
560    "wiki:Space.IncludingPage", velocity);
561   
562  0 return runIncludeMacro(context, includedContent);
563    }
564   
 
565  0 toggle private List<Block> runIncludeMacro(final Context context, String includedContent) throws Exception
566    {
567  0 return runIncludeMacro(context, includedContent, false);
568    }
569   
 
570  0 toggle private List<Block> runIncludeMacro(final Context context, String includedContent, boolean restricted)
571    throws Exception
572    {
573  0 final DocumentReference includedDocumentReference = new DocumentReference("wiki", "Space", "IncludedPage");
574  0 String includedDocStringRef = "wiki:space.page";
575  0 setUpDocumentMock(includedDocStringRef, includedDocumentReference, includedContent);
576  0 getMockery().checking(new Expectations()
577    {
 
578  0 toggle {
579  0 oneOf(mockContextualAuthorization).hasAccess(with(Right.VIEW), with(same(includedDocumentReference)));
580  0 will(returnValue(true));
581    // Verify that push/pop are called when context is NEW
582  0 if (context == Context.NEW) {
583  0 oneOf(mockSetup.bridge).pushDocumentInContext(with(any(Map.class)), with(same(mockDocument)));
584  0 oneOf(mockSetup.bridge).getCurrentDocumentReference();
585  0 will(returnValue(includedDocumentReference));
586  0 oneOf(mockSetup.bridge).popDocumentFromContext(with(any(Map.class)));
587    } else {
588  0 oneOf(mockSetup.bridge).getCurrentDocumentReference();
589  0 will(returnValue(new DocumentReference("wiki", "Space", "IncludingPage")));
590    }
591    }
592    });
593  0 this.includeMacro.setDocumentAccessBridge(this.mockSetup.bridge);
594   
595  0 IncludeMacroParameters parameters = new IncludeMacroParameters();
596  0 parameters.setReference(includedDocStringRef);
597  0 parameters.setContext(context);
598   
599    // Create a Macro transformation context with the Macro transformation object defined so that the include
600    // macro can transform included page which is using a new context.
601  0 MacroTransformation macroTransformation = getComponentManager().getInstance(Transformation.class, "macro");
602  0 MacroTransformationContext macroContext = createMacroTransformationContext(includedDocStringRef, false);
603  0 macroContext.setId("wiki:Space.IncludingPage");
604  0 macroContext.setTransformation(macroTransformation);
605  0 macroContext.getTransformationContext().setRestricted(restricted);
606   
607  0 Test failure here return this.includeMacro.execute(parameters, null, macroContext);
608    }
609    }