View Javadoc
1   /**
2    * This file Copyright (c) 2015-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.imaging.support;
35  
36  import static org.hamcrest.CoreMatchers.is;
37  import static org.junit.Assert.assertThat;
38  import static org.mockito.Mockito.*;
39  
40  import info.magnolia.context.MgnlContext;
41  import info.magnolia.module.site.ConfiguredSite;
42  import info.magnolia.module.site.DefaultSiteManager;
43  import info.magnolia.module.site.SiteManager;
44  import info.magnolia.module.site.SiteModule;
45  import info.magnolia.module.site.theme.ConfiguredTheme;
46  import info.magnolia.module.site.theme.ThemeReference;
47  import info.magnolia.templating.functions.TemplatingFunctions;
48  import info.magnolia.templating.imaging.VariationAwareImagingSupport;
49  import info.magnolia.test.mock.MockWebContext;
50  
51  import javax.inject.Provider;
52  import javax.jcr.Node;
53  import javax.jcr.Property;
54  import javax.jcr.Session;
55  import javax.jcr.Workspace;
56  
57  import org.junit.Before;
58  import org.junit.Test;
59  
60  /**
61   * Tests for {@link ThemeDelegatingImagingSupport}.
62   */
63  public class ThemeDelegatingImagingSupportTest {
64  
65      private SiteModule siteModule;
66      private SiteManager siteManager;
67      private ThemeDelegatingImagingSupport themeDelegatingImagingSupport;
68      private ConfiguredSite site;
69  
70      @Before
71      public void setUp() {
72          final MockWebContext context = new MockWebContext();
73          context.setContextPath("ctx");
74          MgnlContext.setInstance(context);
75  
76          siteModule = mock(SiteModule.class);
77          Provider<SiteModule> siteModuleProvider = new Provider<SiteModule>() {
78              @Override
79              public SiteModule get() {
80                  return siteModule;
81              }
82          };
83          siteManager = new DefaultSiteManager(siteModuleProvider, mock(TemplatingFunctions.class));
84  
85          themeDelegatingImagingSupport = new ThemeDelegatingImagingSupport(siteManager, siteModuleProvider);
86  
87          site = new ConfiguredSite();
88      }
89  
90      @Test
91      public void checkThatImagingSupportDelegatesToThemesImagingSupport() throws Exception {
92          // GIVEN
93          final String variationName = "variationName";
94          final String themeName = "test";
95  
96          final VariationAwareImagingSupport variationAwareImagingSupport = mock(VariationAwareImagingSupport.class);
97          final ConfiguredTheme theme = new ConfiguredTheme(variationAwareImagingSupport);
98          theme.setName(themeName);
99  
100         final ThemeReference themeReference = new ThemeReference();
101         themeReference.setName(themeName);
102 
103         site.setTheme(themeReference);
104 
105         final Node node = mock(Node.class);
106         final Property property = mock(Property.class);
107         when(property.getParent()).thenReturn(node);
108 
109         when(siteModule.getSite()).thenReturn(site);
110         when(siteModule.getTheme(anyString())).thenReturn(theme);
111 
112         // WHEN
113         themeDelegatingImagingSupport.createLink(property);
114 
115         // THEN
116         verify(variationAwareImagingSupport, times(1)).createLink(property);
117 
118         // WHEN
119         themeDelegatingImagingSupport.createLink(property, variationName);
120 
121         // THEN
122         verify(variationAwareImagingSupport, times(1)).createLink(property, variationName);
123     }
124 
125     @Test
126     public void checkFallback() throws Exception {
127         // GIVEN
128         final Session session = mock(Session.class);
129         final Workspace workspace = mock(Workspace.class);
130         final Node node = mock(Node.class);
131         final Property property = mock(Property.class);
132         when(property.getNode()).thenReturn(node);
133         when(property.getParent()).thenReturn(node);
134         when(node.getPath()).thenReturn("/path/to/binary/node");
135         when(property.getSession()).thenReturn(session);
136         when(session.getWorkspace()).thenReturn(workspace);
137         when(workspace.getName()).thenReturn("dummy-workspace");
138 
139         when(siteModule.getSite()).thenReturn(site);
140 
141         // WHEN
142         String link = themeDelegatingImagingSupport.createLink(property);
143 
144         // THEN
145         assertThat(link, is("ctx/.imaging/default/dummy-workspace/path/to/binary/node.jpg"));
146 
147         // WHEN
148         String linkWithVariation = themeDelegatingImagingSupport.createLink(property, "variationName");
149 
150         // THEN
151         assertThat(linkWithVariation, is("ctx/.imaging/default/dummy-workspace/path/to/binary/node.jpg"));
152     }
153 
154 }