View Javadoc
1   /**
2    * This file Copyright (c) 2013-2016 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.module.googlesitemap.setup;
35  
36  import static info.magnolia.test.hamcrest.ExecutionMatcher.throwsNothing;
37  import static info.magnolia.test.hamcrest.NodeMatchers.*;
38  import static org.hamcrest.CoreMatchers.not;
39  import static org.hamcrest.MatcherAssert.assertThat;
40  import static org.junit.Assert.*;
41  
42  import info.magnolia.cms.core.version.VersionManager;
43  import info.magnolia.cms.util.ClasspathResourcesUtil;
44  import info.magnolia.context.MgnlContext;
45  import info.magnolia.jcr.util.NodeTypes;
46  import info.magnolia.jcr.util.NodeUtil;
47  import info.magnolia.module.ModuleManagementException;
48  import info.magnolia.module.ModuleVersionHandler;
49  import info.magnolia.module.ModuleVersionHandlerTestCase;
50  import info.magnolia.module.googlesitemap.GoogleSiteMapConfiguration;
51  import info.magnolia.module.googlesitemap.SiteMapNodeTypes;
52  import info.magnolia.module.googlesitemap.app.subapp.sitemapdetail.contentviews.formatter.FolderNameColumnFormatter;
53  import info.magnolia.module.model.Version;
54  import info.magnolia.objectfactory.Components;
55  import info.magnolia.repository.RepositoryConstants;
56  import info.magnolia.repository.RepositoryManager;
57  import info.magnolia.test.hamcrest.Execution;
58  
59  import java.util.Arrays;
60  import java.util.List;
61  
62  import javax.jcr.Node;
63  import javax.jcr.NodeIterator;
64  import javax.jcr.RepositoryException;
65  import javax.jcr.Session;
66  
67  import org.junit.Test;
68  
69  /**
70   * Tests for {@link GoogleSiteMapVersionHandler}.
71   */
72  public class GoogleSiteMapVersionHandlerTest extends ModuleVersionHandlerTestCase {
73  
74      private Session session;
75      private final String siteMapNodeTypeConfigFile = "/test-google-sitemap-nodetypes.xml";
76  
77      @Override
78      protected String getModuleDescriptorPath() {
79          return "/META-INF/magnolia/google-sitemap.xml";
80      }
81  
82      @Override
83      protected ModuleVersionHandler newModuleVersionHandlerForTests() {
84          return new GoogleSiteMapVersionHandler();
85      }
86  
87      @Override
88      protected String[] getExtraWorkspaces() {
89          return new String[]{"templates", GoogleSiteMapConfiguration.WORKSPACE};
90      }
91  
92      @Override
93      protected List<String> getModuleDescriptorPathsForTests() {
94          return Arrays.asList("/META-INF/magnolia/core.xml");
95      }
96  
97      @Override
98      protected String getExtraNodeTypes() {
99          return siteMapNodeTypeConfigFile;
100     }
101 
102     @Override
103     public String getRepositoryConfigFileName() {
104         return "/test-siteMap-repositories.xml";
105     }
106 
107     @Override
108     public void setUp() throws Exception {
109         super.setUp();
110 
111         session = MgnlContext.getJCRSession(RepositoryConstants.CONFIG);
112 
113         addSupportForSetupModuleRepositoriesTask(null);
114 
115         // Register Google SiteMap node type
116         try {
117             Components.getComponent(RepositoryManager.class).getRepositoryProvider("magnolia").registerNodeTypes(ClasspathResourcesUtil.getResource(siteMapNodeTypeConfigFile).openStream());
118         } catch (RepositoryException e) {
119         }
120 
121         this.setupConfigProperty("/modules/google-sitemap/dialogs/components/content/siteComponentTab/form/tabs/tabSites/fields/sites/field", "workspace", "website");
122         this.setupConfigNode("/modules/google-sitemap/dialogs/generic/controls/googleSiteMapTab/form/tabs/tabGoogleSitemapProps/fields/googleSitemapPriority");
123         this.setupConfigNode("/modules/google-sitemap/dialogs/generic/controls/googleSiteMapTab/form/tabs/tabGoogleSitemapProps/fields/googleSitemapChangefreq");
124         this.setupConfigNode("/modules/google-sitemap/dialogs/generic/controls/googleVirtualUriMapTab/form/tabs/tabGoogleSitemapProps/fields/googleSitemapPriority");
125         this.setupConfigNode("/modules/google-sitemap/dialogs/generic/controls/googleVirtualUriMapTab/form/tabs/tabGoogleSitemapProps/fields/googleSitemapChangefreq");
126         this.setupConfigNode("/modules/google-sitemap/templates/pages/siteMapsConfiguration");
127         this.setupConfigNode("/modules/google-sitemap/apps/siteMaps/subApps/browser/actions/deactivate");
128     }
129 
130     @Test
131     public void testUpdateTo20ReordersSitemapBeforeConfigApp() throws Exception {
132         // GIVEN
133         Node manageApps = NodeUtil.createPath(session.getRootNode(), "modules/ui-admincentral/config/appLauncherLayout/groups/manage/apps", NodeTypes.ContentNode.NAME);
134         NodeUtil.createPath(manageApps, "rssAggregator", NodeTypes.ContentNode.NAME);
135         NodeUtil.createPath(manageApps, "configuration", NodeTypes.ContentNode.NAME);
136 
137         // WHEN
138         executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("1.2.3"));
139 
140         // THEN
141         NodeIterator it = manageApps.getNodes();
142         assertTrue(manageApps.hasNode("siteMaps"));
143         assertEquals("rssAggregator", it.nextNode().getName());
144         assertEquals("siteMaps", it.nextNode().getName());
145         assertEquals("configuration", it.nextNode().getName());
146     }
147 
148     @Test
149     public void testFreshInstallReordersSitemapBeforeConfigApp() throws Exception {
150         // GIVEN
151         Node manageApps = NodeUtil.createPath(session.getRootNode(), "modules/ui-admincentral/config/appLauncherLayout/groups/manage/apps", NodeTypes.ContentNode.NAME);
152         NodeUtil.createPath(manageApps, "rssAggregator", NodeTypes.ContentNode.NAME);
153         NodeUtil.createPath(manageApps, "configuration", NodeTypes.ContentNode.NAME);
154         // we have to create the siteMaps node artificially before bootstrapping, otherwise test would fail in maven
155         NodeUtil.createPath(manageApps, "siteMaps", NodeTypes.ContentNode.NAME);
156 
157         // WHEN
158         executeUpdatesAsIfTheCurrentlyInstalledVersionWas(null);
159 
160         // THEN
161         NodeIterator it = manageApps.getNodes();
162         assertTrue(manageApps.hasNode("siteMaps"));
163         assertEquals("rssAggregator", it.nextNode().getName());
164         assertEquals("siteMaps", it.nextNode().getName());
165         assertEquals("configuration", it.nextNode().getName());
166     }
167 
168     @Test
169     public void testUpdateTo22() throws RepositoryException, ModuleManagementException {
170         // GIVEN
171         this.setupConfigProperty("/modules/google-sitemap/dialogs/components/content/siteComponentTab/form/tabs/siteMap", "property", "value");
172         this.setupConfigNode("/modules/google-sitemap/apps/siteMaps/subApps/browser");
173         this.setupConfigProperty("/modules/google-sitemap/apps/siteMaps/subApps/pages", "property", "value");
174         this.setupConfigProperty("/modules/google-sitemap/apps/siteMaps/subApps/browser/workbench/contentViews/list/columns/name", "formatterClass", "info.magnolia.module.googlesitemap.app.subapp.sitemapdetail.formatter.FolderNameColumnFormatter");
175 
176         // WHEN
177         executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("2.0"));
178 
179         // THEN
180         NodeIterator it = session.getNode("/modules/google-sitemap/apps/siteMaps/subApps").getNodes();
181         assertEquals("browser", it.nextNode().getName());
182         assertEquals("pages", it.nextNode().getName());
183         assertConfig(FolderNameColumnFormatter.class.getName(), "/modules/google-sitemap/apps/siteMaps/subApps/browser/workbench/contentViews/list/columns/name/formatterClass");
184         assertConfig("0.5", "/modules/google-sitemap/config/priority");
185         assertConfig("weekly", "/modules/google-sitemap/config/changeFrequency");
186         assertTrue(session.nodeExists("/modules/google-sitemap/fieldTypes/siteMapSelect"));
187     }
188 
189     @Test
190     public void testUpdateTo22ConfigureActions() throws Exception {
191         // GIVEN
192         Session session = MgnlContext.getJCRSession(RepositoryConstants.CONFIG);
193         this.setupConfigProperty("/modules/google-sitemap/apps/siteMaps/subApps/browser/workbench/contentViews/list/columns/name", "formatterClass", "info.magnolia.module.googlesitemap.app.subapp.sitemapdetail.formatter.FolderNameColumnFormatter");
194         Node addSiteMapAction = NodeUtil.createPath(session.getRootNode(), GoogleSiteMapVersionHandler.GOOGLESITEMAP_APP_BROWSER_ACTIONS + "addSiteMap", NodeTypes.ContentNode.NAME);
195         Node deleteAction = NodeUtil.createPath(session.getRootNode(), GoogleSiteMapVersionHandler.GOOGLESITEMAP_APP_BROWSER_ACTIONS + "delete", NodeTypes.ContentNode.NAME);
196         Node editSiteMapAction = NodeUtil.createPath(session.getRootNode(), GoogleSiteMapVersionHandler.GOOGLESITEMAP_APP_BROWSER_ACTIONS + "editSiteMap", NodeTypes.ContentNode.NAME);
197         Node activateAction = NodeUtil.createPath(session.getRootNode(), GoogleSiteMapVersionHandler.GOOGLESITEMAP_APP_BROWSER_ACTIONS + "activate", NodeTypes.ContentNode.NAME);
198         Node deactivateAction = NodeUtil.createPath(session.getRootNode(), GoogleSiteMapVersionHandler.GOOGLESITEMAP_APP_BROWSER_ACTIONS + "deactivate", NodeTypes.ContentNode.NAME);
199         Node activateDeletedAction = NodeUtil.createPath(session.getRootNode(), GoogleSiteMapVersionHandler.GOOGLESITEMAP_APP_BROWSER_ACTIONS + "activateDeleted", NodeTypes.ContentNode.NAME);
200 
201         // WHEN
202         executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("2.1"));
203 
204         // THEN
205         assertThat(addSiteMapAction, hasNode("availability"));
206         assertThat(addSiteMapAction.getNode("availability"), hasProperty("writePermissionRequired", true));
207         assertThat(deleteAction, hasNode("availability"));
208         assertThat(deleteAction.getNode("availability"), hasProperty("writePermissionRequired", true));
209         assertThat(editSiteMapAction, hasNode("availability"));
210         assertThat(editSiteMapAction.getNode("availability"), hasProperty("writePermissionRequired", true));
211         assertThat(activateAction, hasNode("availability"));
212         assertThat(activateAction.getNode("availability"), hasProperty("writePermissionRequired", true));
213         assertThat(deactivateAction, hasNode("availability"));
214         assertThat(deactivateAction.getNode("availability"), hasProperty("writePermissionRequired", true));
215         assertThat(activateDeletedAction, hasNode("availability"));
216         assertThat(activateDeletedAction.getNode("availability"), hasProperty("writePermissionRequired", true));
217     }
218 
219     @Test
220     public void testUpdateTo223NewCatalogProperty() throws RepositoryException, ModuleManagementException {
221         // GIVEN
222 
223         // WHEN
224         executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("2.2.2"));
225 
226         // THEN
227         assertTrue(session.propertyExists("/modules/google-sitemap/apps/siteMaps/subApps/browser/actions/deactivate/catalog"));
228         assertEquals("website", session.getProperty("/modules/google-sitemap/apps/siteMaps/subApps/browser/actions/deactivate/catalog").getString());
229     }
230 
231     @Test
232     public void updateFrom232RemovesHiddenProperty() throws Exception {
233         // GIVEN
234         setupConfigProperty("/modules/google-sitemap/dialogs/components/content/siteComponentTab/form/tabs/siteMap/fields/template", "hidden", "true");
235 
236         // WHEN
237         executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("2.3.2"));
238 
239         // THEN
240         assertThat(session.getNode("/modules/google-sitemap/dialogs/components/content/siteComponentTab/form/tabs/siteMap/fields/template"), not(hasProperty("hidden")));
241     }
242 
243     @Test
244     public void updateFrom232ChangesBrokenTemplateOnExistingSites() throws Exception {
245         // GIVEN
246         final Session siteMapSession = MgnlContext.getJCRSession(GoogleSiteMapConfiguration.WORKSPACE);
247         final Node siteMap = NodeUtil.createPath(siteMapSession.getRootNode(), "testSiteMap", SiteMapNodeTypes.SiteMap.NAME);
248         siteMap.setProperty(NodeTypes.Renderable.TEMPLATE, "GoogleSiteMap");
249 
250         Components.getComponent(VersionManager.class).addVersion(siteMap);
251 
252         // WHEN
253         assertThat("We want to make sure that the update task is not executed on jcr:system nodes (modifying a property would result in exception)", new Execution() {
254             @Override
255             public void evaluate() throws Exception {
256                 executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("2.3.2"));
257             }
258         }, throwsNothing());
259 
260         // THEN
261         assertThat(siteMapSession.getNode("/testSiteMap"), hasProperty(NodeTypes.Renderable.TEMPLATE, "google-sitemap:pages/siteMapsConfiguration"));
262     }
263 
264     @Test
265     public void updateFrom233SetsDefaultType() throws Exception {
266         // GIVEN
267 
268         // WHEN
269         executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("2.3.3"));
270 
271         // THEN
272         assertThat(session.getNode("/modules/google-sitemap/dialogs/components/content/siteComponentTab/form/tabs/siteMap/fields/mgnl:googleSiteMapType/options/standard"), hasProperty("selected", "true"));
273     }
274 
275     @Test
276     public void updateFrom233AddsRendererAndUsage() throws Exception {
277         // GIVEN
278         setupConfigProperty("/modules/google-sitemap/templates/pages/siteMapsConfiguration", "renderType", "freemarker");
279 
280         // WHEN
281         executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("2.3.3"));
282 
283         // THEN
284         assertThat(session.getNode("/modules/google-sitemap/renderers/sitemap"), hasProperty("contentType", "application/xml"));
285         assertThat(session.getNode("/modules/google-sitemap/templates/pages/siteMapsConfiguration"), hasProperty("renderType", "sitemap"));
286     }
287 
288 }