View Javadoc
1   /**
2    * This file Copyright (c) 2016-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.templating.functions;
35  
36  import static info.magnolia.test.hamcrest.NodeMatchers.nodeName;
37  import static org.hamcrest.MatcherAssert.assertThat;
38  import static org.junit.Assert.*;
39  import static org.mockito.Mockito.mock;
40  import static org.mockito.Mockito.when;
41  
42  import info.magnolia.cms.beans.config.ServerConfiguration;
43  import info.magnolia.cms.core.Path;
44  import info.magnolia.cms.i18n.DefaultI18nContentSupport;
45  import info.magnolia.cms.i18n.I18nContentSupport;
46  import info.magnolia.context.MgnlContext;
47  import info.magnolia.context.WebContext;
48  import info.magnolia.jcr.util.ContentMap;
49  import info.magnolia.jcr.util.NodeTypes;
50  import info.magnolia.objectfactory.guice.GuiceUtils;
51  import info.magnolia.registry.RegistrationException;
52  import info.magnolia.rendering.template.configured.ConfiguredTemplateDefinition;
53  import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
54  import info.magnolia.rendering.template.type.TemplateTypeHelper;
55  import info.magnolia.test.ComponentsTestUtil;
56  import info.magnolia.test.mock.MockWebContext;
57  import info.magnolia.test.mock.jcr.MockSession;
58  
59  import java.util.Iterator;
60  import java.util.List;
61  
62  import javax.jcr.Node;
63  import javax.jcr.RepositoryException;
64  import javax.jcr.Session;
65  
66  import org.junit.After;
67  import org.junit.Before;
68  import org.junit.Test;
69  
70  
71  public class NavigationTemplatingFunctionsTest {
72  
73      private Session websiteSession;
74      private Session contactsSession;
75      private Session configSession;
76  
77      private TemplateDefinitionRegistry templateDefinitionRegistry;
78      private NavigationTemplatingFunctions navfn;
79  
80      @Before
81      public void setUp() throws RepositoryException {
82          websiteSession = new MockSession("website");
83          contactsSession = new MockSession("contacts");
84          configSession = new MockSession("config");
85  
86          MockWebContext ctx = new MockWebContext();
87          ComponentsTestUtil.setInstance(WebContext.class, ctx);
88          ctx.addSession("website", websiteSession);
89          ctx.addSession("contacts", contactsSession);
90          ctx.addSession("config", configSession);
91          MgnlContext.setInstance(ctx);
92          templateDefinitionRegistry = mock(TemplateDefinitionRegistry.class);
93          TemplateTypeHelper templateTypeHelper = new TemplateTypeHelper(templateDefinitionRegistry);
94          I18nContentSupport defaultI18nContentSupport = new DefaultI18nContentSupport();
95          ComponentsTestUtil.setInstance(I18nContentSupport.class, defaultI18nContentSupport);
96          TemplatingFunctions templatingFunctions = new TemplatingFunctions(GuiceUtils.providerForInstance(MgnlContext.getAggregationState()), templateTypeHelper, GuiceUtils.providerForInstance(defaultI18nContentSupport));
97  
98          navfn = new NavigationTemplatingFunctions(templatingFunctions);
99      }
100 
101     @After
102     public void tearDown() {
103         ComponentsTestUtil.clear();
104         MgnlContext.setInstance(null);
105     }
106 
107     @Test
108     public void rootPage() throws RepositoryException {
109         // GIVEN
110         Node topPageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
111         Node component = topPageNode.addNode("firstSubPage", NodeTypes.Page.NAME).addNode("secondSubPage", NodeTypes.Page.NAME).addNode("component", NodeTypes.Component.NAME);
112 
113         // WHEN
114         ContentMap rootPage = navfn.rootPage(new ContentMap(component));
115 
116         // THEN
117         assertTrue(rootPage.getJCRNode().isSame(topPageNode));
118     }
119 
120     @Test
121     public void ancestorPageAtLevel() throws RepositoryException {
122         // GIVEN
123         Node topPageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
124         Node component = topPageNode.addNode("firstSubPage", NodeTypes.Page.NAME).addNode("secondSubPage", NodeTypes.Page.NAME).addNode("component", NodeTypes.Component.NAME);
125 
126         // WHEN
127         ContentMap ancestorPage = navfn.ancestorPageAtLevel(new ContentMap(component), 2);
128 
129         // THEN
130         assertThat(ancestorPage.getJCRNode(), nodeName("firstSubPage"));
131     }
132 
133     @Test
134     public void navItems() throws RepositoryException {
135         // GIVEN
136         Node topPageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
137         Node firstSubPage = topPageNode.addNode("firstSubPage", NodeTypes.Page.NAME);
138         Node secondSubPage = topPageNode.addNode("secondSubPage", NodeTypes.Page.NAME);
139         secondSubPage.setProperty(NavigationTemplatingFunctions.HIDE_IN_NAVIGATION_PROPERTY_NAME, false);
140         Node subPageHiddenInNav = topPageNode.addNode("subPageHiddenInNav", NodeTypes.Page.NAME);
141         subPageHiddenInNav.setProperty(NavigationTemplatingFunctions.HIDE_IN_NAVIGATION_PROPERTY_NAME, true);
142         topPageNode.addNode("component", NodeTypes.Component.NAME);
143 
144         // WHEN
145         List<ContentMap> navItems = navfn.navItems(new ContentMap(topPageNode));
146 
147         // THEN
148         String[] expected = {firstSubPage.getName(), secondSubPage.getName()};
149         assertContentMapListEqualStringDefinitions(expected, navItems);
150     }
151 
152     @Test
153     public void navItemsFromApp() throws RepositoryException {
154         // GIVEN
155         Node topFolder = contactsSession.getRootNode().addNode("topFolder", NodeTypes.Folder.NAME);
156         Node firstContact = topFolder.addNode("firstContact", "mgnl:contact");
157         Node secondContact = topFolder.addNode("secondContact", "mgnl:contact");
158         Node subFolder = topFolder.addNode("subFolder", NodeTypes.Folder.NAME);
159         subFolder.addNode("contactInSubFolder", "mgnl:contact");
160 
161         // WHEN
162         List<ContentMap> navItems = navfn.navItemsFromApp("contacts", "/topFolder", "mgnl:contact");
163 
164         // THEN
165         String[] expected = {firstContact.getName(), secondContact.getName()};
166         assertContentMapListEqualStringDefinitions(expected, navItems);
167     }
168 
169     @Test
170     public void hasTemplate() throws RepositoryException {
171         // GIVEN
172         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
173         pageNode.setProperty(NodeTypes.Renderable.TEMPLATE, "mgnl:template");
174 
175         // WHEN
176         boolean hasTemplate = navfn.hasTemplate(new ContentMap(pageNode), "mgnl:template");
177 
178         // THEN
179         assertTrue(hasTemplate);
180     }
181 
182     @Test
183     public void hasNotTemplate() throws RepositoryException {
184         // GIVEN
185         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
186         pageNode.setProperty(NodeTypes.Renderable.TEMPLATE, "mgnl:template");
187 
188         // WHEN
189         boolean hasTemplate = navfn.hasTemplate(new ContentMap(pageNode), "mgnl:somethingelse");
190 
191         // THEN
192         assertFalse(hasTemplate);
193     }
194 
195     @Test
196     public void hasTemplateType() throws RepositoryException, RegistrationException {
197         // GIVEN
198         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
199         pageNode.setProperty(NodeTypes.Renderable.TEMPLATE, "mgnl:template");
200 
201         ConfiguredTemplateDefinition pageTemplateDefinition = new ConfiguredTemplateDefinition(null);
202         pageTemplateDefinition.setId("mgnl:template");
203         pageTemplateDefinition.setType("templateType");
204         when(templateDefinitionRegistry.getTemplateDefinition("mgnl:template")).thenReturn(pageTemplateDefinition);
205 
206         // WHEN
207         boolean hasTemplateType = navfn.hasTemplateType(new ContentMap(pageNode), "templateType");
208 
209         // THEN
210         assertTrue(hasTemplateType);
211     }
212 
213     @Test
214     public void hasNotTemplateType() throws RepositoryException, RegistrationException {
215         // GIVEN
216         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
217 
218         // WHEN
219         boolean hasTemplateType = navfn.hasTemplateType(new ContentMap(pageNode), "templateType");
220 
221         // THEN
222         assertFalse(hasTemplateType);
223     }
224 
225     @Test
226     public void hasTemplateSubType() throws RepositoryException, RegistrationException {
227         // GIVEN
228         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
229         pageNode.setProperty(NodeTypes.Renderable.TEMPLATE, "mgnl:template");
230 
231         ConfiguredTemplateDefinition pageTemplateDefinition = new ConfiguredTemplateDefinition(null);
232         pageTemplateDefinition.setId("mgnl:template");
233         pageTemplateDefinition.setType("templateType");
234         pageTemplateDefinition.setSubtype("subtemplateType");
235         when(templateDefinitionRegistry.getTemplateDefinition("mgnl:template")).thenReturn(pageTemplateDefinition);
236 
237         // WHEN
238         boolean hasTemplateSubtype = navfn.hasTemplateSubtype(new ContentMap(pageNode), "subtemplateType");
239 
240         // THEN
241         assertTrue(hasTemplateSubtype);
242     }
243 
244     @Test
245     public void hasTemplateSubTypeDoNotMatchWithTemplateType() throws RepositoryException, RegistrationException {
246         // GIVEN
247         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
248         pageNode.setProperty(NodeTypes.Renderable.TEMPLATE, "mgnl:template");
249 
250         ConfiguredTemplateDefinition pageTemplateDefinition = new ConfiguredTemplateDefinition(null);
251         pageTemplateDefinition.setId("mgnl:template");
252         pageTemplateDefinition.setType("templateType");
253         when(templateDefinitionRegistry.getTemplateDefinition("mgnl:template")).thenReturn(pageTemplateDefinition);
254 
255         // WHEN
256         boolean hasTemplateSubtype = navfn.hasTemplateSubtype(new ContentMap(pageNode), "templateType");
257 
258         // THEN
259         assertFalse(hasTemplateSubtype);
260     }
261 
262     @Test
263     public void hasNotTemplateSubtype() throws RepositoryException {
264         // GIVEN
265         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
266 
267         // WHEN
268         boolean hasTemplateSubtype = navfn.hasTemplateSubtype(new ContentMap(pageNode), "templateType");
269 
270         // THEN
271         assertFalse(hasTemplateSubtype);
272     }
273 
274     @Test
275     public void linkWithSelector() throws RepositoryException {
276         // GIVEN
277         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
278         Node contactNode = contactsSession.getRootNode().addNode("contact", "mgnl:contact");
279 
280         // WHEN
281         String resultLink = navfn.linkWithSelector(new ContentMap(pageNode), new ContentMap(contactNode));
282 
283         // THEN
284         assertEquals(pageNode.getPath() + Path.SELECTOR_DELIMITER + contactNode.getName() + Path.SELECTOR_DELIMITER, resultLink);
285     }
286 
287     @Test
288     public void linkWithSelectorAndExtension() throws RepositoryException {
289         // GIVEN
290         ServerConfiguration serverConfiguration = new ServerConfiguration();
291         serverConfiguration.setDefaultExtension("html");
292         ComponentsTestUtil.setInstance(ServerConfiguration.class, serverConfiguration);
293 
294         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
295         Node contactNode = contactsSession.getRootNode().addNode("contact", "mgnl:contact");
296 
297         // WHEN
298         String resultLink = navfn.linkWithSelector(new ContentMap(pageNode), new ContentMap(contactNode));
299 
300         // THEN
301         assertEquals(pageNode.getPath() + Path.SELECTOR_DELIMITER + contactNode.getName() + Path.SELECTOR_DELIMITER + ".html", resultLink);
302     }
303 
304     @Test
305     public void linkWithParameter() throws RepositoryException {
306         // GIVEN
307         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
308         Node contactNode = contactsSession.getRootNode().addNode("contact", "mgnl:contact");
309 
310         // WHEN
311         String resultLink = navfn.linkWithParameter(new ContentMap(pageNode), new ContentMap(contactNode));
312 
313         // THEN
314         assertEquals(pageNode.getPath() + "?contacts=contact", resultLink);
315     }
316 
317     @Test
318     public void linkWithParameterAndExtension() throws RepositoryException {
319         // GIVEN
320         ServerConfiguration serverConfiguration = new ServerConfiguration();
321         serverConfiguration.setDefaultExtension("html");
322         ComponentsTestUtil.setInstance(ServerConfiguration.class, serverConfiguration);
323 
324         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
325         Node contactNode = contactsSession.getRootNode().addNode("contact", "mgnl:contact");
326 
327         // WHEN
328         String resultLink = navfn.linkWithParameter(new ContentMap(pageNode), new ContentMap(contactNode));
329 
330         // THEN
331         assertEquals(pageNode.getPath() + ".html?contacts=contact", resultLink);
332     }
333 
334     @Test
335     public void linkWithSpecificParameterName() throws RepositoryException {
336         // GIVEN
337         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
338         Node contactNode = contactsSession.getRootNode().addNode("contact", "mgnl:contact");
339 
340         // WHEN
341         String resultLink = navfn.linkWithParameter(new ContentMap(pageNode), new ContentMap(contactNode), "paramName");
342 
343         // THEN
344         assertEquals(pageNode.getPath() + "?paramName=contact", resultLink);
345     }
346 
347     @Test
348     public void linkWithSpecificParameterNameAndExtension() throws RepositoryException {
349         // GIVEN
350         ServerConfiguration serverConfiguration = new ServerConfiguration();
351         serverConfiguration.setDefaultExtension("html");
352         ComponentsTestUtil.setInstance(ServerConfiguration.class, serverConfiguration);
353 
354         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
355         Node contactNode = contactsSession.getRootNode().addNode("contact", "mgnl:contact");
356 
357         // WHEN
358         String resultLink = navfn.linkWithParameter(new ContentMap(pageNode), new ContentMap(contactNode), "paramName");
359 
360         // THEN
361         assertEquals(pageNode.getPath() + ".html?paramName=contact", resultLink);
362     }
363 
364     @Test
365     public void link() throws RepositoryException {
366         // GIVEN
367         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
368 
369         // WHEN
370         String resultLink = navfn.link(new ContentMap(pageNode));
371 
372         // THEN
373         assertEquals(pageNode.getPath(), resultLink);
374     }
375 
376     @Test
377     public void isActive() throws RepositoryException {
378         // GIVEN
379         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
380 
381         // WHEN
382         boolean isActive = navfn.isActive(new ContentMap(pageNode), new ContentMap(pageNode));
383 
384         // THEN
385         assertTrue(isActive);
386     }
387 
388     @Test
389     public void isNotActive() throws RepositoryException {
390         // GIVEN
391         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
392         Node subPageNode = pageNode.addNode("subPageNode", NodeTypes.Page.NAME);
393 
394         // WHEN
395         boolean isActive = navfn.isActive(new ContentMap(pageNode), new ContentMap(subPageNode));
396 
397         // THEN
398         assertFalse(isActive);
399     }
400 
401     @Test
402     public void isActiveWhenOnComponent() throws RepositoryException {
403         // GIVEN
404         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
405         Node componentNode = pageNode.addNode("component", NodeTypes.Component.NAME);
406 
407         // WHEN
408         boolean isActive = navfn.isActive(new ContentMap(componentNode), new ContentMap(pageNode));
409 
410         // THEN
411         assertTrue(isActive);
412     }
413 
414     @Test
415     public void isOpen() throws RepositoryException {
416         // GIVEN
417         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
418         Node subPageNode = pageNode.addNode("subPageNode", NodeTypes.Page.NAME);
419 
420         // WHEN
421         boolean isOpen = navfn.isOpen(new ContentMap(subPageNode), new ContentMap(pageNode));
422 
423         // THEN
424         assertTrue(isOpen);
425     }
426 
427     @Test
428     public void isNotOpen() throws RepositoryException {
429         // GIVEN
430         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
431         Node firstSubPageNode = pageNode.addNode("firstSubPageNode", NodeTypes.Page.NAME);
432         Node secondSubPageNode = pageNode.addNode("secondSubPageNode", NodeTypes.Page.NAME);
433 
434         // WHEN
435         boolean isOpen = navfn.isOpen(new ContentMap(firstSubPageNode), new ContentMap(secondSubPageNode));
436 
437         // THEN
438         assertFalse(isOpen);
439     }
440 
441     @Test
442     public void isNotOpenWhenOnActivePage() throws RepositoryException {
443         // GIVEN
444         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
445 
446         // WHEN
447         boolean isOpen = navfn.isOpen(new ContentMap(pageNode), new ContentMap(pageNode));
448 
449         // THEN
450         assertFalse(isOpen);
451     }
452 
453     @Test
454     public void isHiddenInNavWhenPropertyExistAndIsSetToFalse() throws RepositoryException {
455         // GIVEN
456         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
457         pageNode.setProperty(NavigationTemplatingFunctions.HIDE_IN_NAVIGATION_PROPERTY_NAME, false);
458 
459         // WHEN
460         boolean isHiddenInNav = navfn.isHiddenInNav(new ContentMap(pageNode));
461 
462         // THEN
463         assertFalse(isHiddenInNav);
464     }
465 
466     @Test
467     public void isHiddenInNavWhenPropertyDoesNotExist() throws RepositoryException {
468         // GIVEN
469         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
470 
471         // WHEN
472         boolean isHiddenInNav = navfn.isHiddenInNav(new ContentMap(pageNode));
473 
474         // THEN
475         assertFalse(isHiddenInNav);
476     }
477 
478     @Test
479     public void isHiddenInNavWhenPropertyExistAndIsSetToTrue() throws RepositoryException {
480         // GIVEN
481         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
482         pageNode.setProperty(NavigationTemplatingFunctions.HIDE_IN_NAVIGATION_PROPERTY_NAME, true);
483 
484         // WHEN
485         boolean isHiddenInNav = navfn.isHiddenInNav(new ContentMap(pageNode));
486 
487         // THEN
488         assertTrue(isHiddenInNav);
489     }
490 
491     @Test
492     public void isNotHiddenInNavWhenPropertyExistAndIsSetToTrue() throws RepositoryException {
493         // GIVEN
494         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
495         pageNode.setProperty(NavigationTemplatingFunctions.HIDE_IN_NAVIGATION_PROPERTY_NAME, true);
496 
497         // WHEN
498         boolean isNotHiddenInNav = navfn.isNotHiddenInNav(new ContentMap(pageNode));
499 
500         // THEN
501         assertFalse(isNotHiddenInNav);
502     }
503 
504     @Test
505     public void isNotHiddenInNavWhenPropertyExistAndIsSetToFalse() throws RepositoryException {
506         // GIVEN
507         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
508         pageNode.setProperty(NavigationTemplatingFunctions.HIDE_IN_NAVIGATION_PROPERTY_NAME, false);
509 
510         // WHEN
511         boolean isNotHiddenInNav = navfn.isNotHiddenInNav(new ContentMap(pageNode));
512 
513         // THEN
514         assertTrue(isNotHiddenInNav);
515     }
516 
517     @Test
518     public void isTrue() throws RepositoryException {
519         // GIVEN
520         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
521         pageNode.setProperty("propName", true);
522 
523         // WHEN
524         boolean isNotHiddenInNav = navfn.isTrue(new ContentMap(pageNode), "propName");
525 
526         // THEN
527         assertTrue(isNotHiddenInNav);
528     }
529 
530     @Test
531     public void isTrueWhenPropertySetToFalse() throws RepositoryException {
532         // GIVEN
533         Node pageNode = websiteSession.getRootNode().addNode("topPage", NodeTypes.Page.NAME);
534         pageNode.setProperty("propName", false);
535 
536         // WHEN
537         boolean isNotHiddenInNav = navfn.isTrue(new ContentMap(pageNode), "propName");
538 
539         // THEN
540         assertFalse(isNotHiddenInNav);
541     }
542 
543     private void assertContentMapListEqualStringDefinitions(String[] expectedNames, List<ContentMap> actualMapList) throws RepositoryException {
544         int i = 0;
545         assertEquals("Actual number of items doesn't match with expected.", expectedNames.length, actualMapList.size());
546         for (Iterator<ContentMap> it = actualMapList.iterator(); it.hasNext(); i++) {
547             ContentMap actualMap = it.next();
548             assertEquals(expectedNames[i], actualMap.get("@name"));
549         }
550     }
551 }