1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.administration.test.ui

File CreatePageTest.java

 

Code metrics

6
146
4
1
364
217
7
0.05
36.5
4
1.75

Classes

Class Line # Actions
CreatePageTest 53 146 0% 7 2
0.9871794698.7%
 

Contributing tests

This file is covered by 3 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.administration.test.ui;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.openqa.selenium.By;
28    import org.openqa.selenium.WebElement;
29    import org.xwiki.administration.test.po.TemplateProviderInlinePage;
30    import org.xwiki.administration.test.po.TemplatesAdministrationSectionPage;
31    import org.xwiki.model.reference.EntityReference;
32    import org.xwiki.model.reference.LocalDocumentReference;
33    import org.xwiki.test.ui.AbstractTest;
34    import org.xwiki.test.ui.SuperAdminAuthenticationRule;
35    import org.xwiki.test.ui.browser.IgnoreBrowser;
36    import org.xwiki.test.ui.browser.IgnoreBrowsers;
37    import org.xwiki.test.ui.po.CreatePagePage;
38    import org.xwiki.test.ui.po.DocumentDoesNotExistPage;
39    import org.xwiki.test.ui.po.ViewPage;
40    import org.xwiki.test.ui.po.editor.EditPage;
41    import org.xwiki.test.ui.po.editor.WikiEditPage;
42   
43    import static org.junit.Assert.assertEquals;
44    import static org.junit.Assert.assertFalse;
45    import static org.junit.Assert.assertTrue;
46   
47    /**
48    * Tests page creation with and without a Template Provider/Template.
49    *
50    * @version $Id: ec0ae5a71891896f233f955473c70ca9e5bc64e4 $
51    * @since 2.4M1
52    */
 
53    public class CreatePageTest extends AbstractTest
54    {
55    @Rule
56    public SuperAdminAuthenticationRule authenticationRule = new SuperAdminAuthenticationRule(getUtil());
57   
58    /**
59    * Name of the template.
60    */
61    public static final String TEMPLATE_NAME = "TestTemplate";
62   
63    /**
64    * Tests if a new page can be created from a template.
65    */
 
66  1 toggle @Test
67    @IgnoreBrowsers({
68    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
69    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
70    })
71    public void createPagesFromTemplate() throws Exception
72    {
73    // Step 0: Setup the correct environment for the test
74   
75    // All these pages are created during this test
76  1 getUtil().deleteSpace(getTestClassName());
77  1 getUtil().rest().deletePage(getTestClassName(), getTestMethodName());
78  1 EntityReference templateInstanceReference =
79    getUtil().resolveDocumentReference(getTestClassName() + "." + TEMPLATE_NAME + "Instance" + ".WebHome");
80  1 getUtil().rest().delete(templateInstanceReference);
81  1 getUtil().rest().deletePage(getTestClassName(), "NewPage");
82  1 getUtil().rest().deletePage(getTestClassName(), TEMPLATE_NAME + "UnexistingInstance");
83  1 getUtil().rest().deletePage(getTestClassName(), "EmptyPage");
84   
85  1 String templateContent = "Test Template Content";
86  1 String templateTitle = "Test Template Title";
87  1 String templateProviderName = TEMPLATE_NAME + "Provider";
88  1 String templateProviderFullName = getTestClassName() + "." + templateProviderName;
89   
90    // Step 1: Create a Template and a Template Provider and try to create a new page by using the Add Menu and
91    // using the created Template
92   
93  1 ViewPage templateProviderView =
94    createTemplateAndTemplateProvider(templateProviderName, templateContent, templateTitle, false);
95   
96    // Create the new document from template
97  1 CreatePagePage createPagePage = templateProviderView.createPage();
98    // Save the number of available templates so that we can make some checks later on.
99  1 int availableTemplateSize = createPagePage.getAvailableTemplateSize();
100  1 String templateInstanceName = TEMPLATE_NAME + "Instance";
101  1 createPagePage.getDocumentPicker().toggleLocationAdvancedEdit();
102  1 EditPage templateInstanceEditWysiwyg =
103    createPagePage.createPageFromTemplate(templateInstanceName, getTestClassName(), null,
104    templateProviderFullName);
105  1 WikiEditPage templateInstanceEdit = templateInstanceEditWysiwyg.clickSaveAndView().editWiki();
106   
107    // Verify template instance location and content
108  1 assertEquals(templateInstanceName, templateInstanceEdit.getTitle());
109  1 assertEquals(getTestClassName() + "." + templateInstanceName, templateInstanceEdit.getMetaDataValue("space"));
110  1 assertEquals("WebHome", templateInstanceEdit.getMetaDataValue("page"));
111  1 assertEquals(templateContent, templateInstanceEdit.getContent());
112    // check the parent of the template instance
113  1 assertEquals(templateProviderFullName, templateInstanceEdit.getParent());
114   
115    // Step 2: Create a wanted link and verify that clicking it displays the Template and that we can use it.
116   
117    // Put a wanted link in the template instance
118  1 templateInstanceEdit.setContent("[[doc:NewPage]]");
119  1 ViewPage vp = templateInstanceEdit.clickSaveAndView();
120   
121    // Verify that clicking on the wanted link pops up a box to choose the template.
122  1 EntityReference wantedLinkReference =
123    getUtil().resolveDocumentReference(getTestClassName() + "." + TEMPLATE_NAME + "Instance" + ".NewPage");
124  1 vp.clickWantedLink(wantedLinkReference, true);
125    // TODO: a page object should be better here
126  1 List<WebElement> templates =
127    getDriver().findElements(By.xpath("//input[@name='type' and @data-type='template']"));
128    // Note: We need to remove 1 to exclude the "Empty Page" template entry
129  1 assertEquals(availableTemplateSize, templates.size());
130  1 assertTrue(createPagePage.getAvailableTemplates().contains(templateProviderFullName));
131   
132    // Step 3: Create a new page when located on a non-existing page
133   
134  1 getUtil().gotoPage(getTestClassName(), TEMPLATE_NAME + "UnexistingInstance", "view", "spaceRedirect=false");
135  1 vp = new ViewPage();
136  1 assertFalse(vp.exists());
137  1 DocumentDoesNotExistPage unexistingPage = new DocumentDoesNotExistPage();
138  1 unexistingPage.clickEditThisPageToCreate();
139  1 CreatePagePage createUnexistingPage = new CreatePagePage();
140    // Make sure we're in create mode.
141  1 assertTrue(getUtil().isInCreateMode());
142    // count the available templates, make sure they're as many as before and that our template is among them
143  1 assertEquals(availableTemplateSize, createUnexistingPage.getAvailableTemplateSize());
144  1 assertTrue(createUnexistingPage.getAvailableTemplates().contains(templateProviderFullName));
145    // select it
146  1 createUnexistingPage.setTemplate(templateProviderFullName);
147  1 createUnexistingPage.setTerminalPage(true);
148    // and create
149  1 createUnexistingPage.clickCreate();
150  1 EditPage ep = new EditPage();
151  1 WikiEditPage unexistingPageEdit = ep.clickSaveAndView().editWiki();
152   
153    // Verify template instance location and content
154  1 assertEquals(getTestClassName(), templateInstanceEdit.getMetaDataValue("space"));
155  1 assertEquals(TEMPLATE_NAME + "UnexistingInstance", templateInstanceEdit.getMetaDataValue("page"));
156  1 assertEquals(TEMPLATE_NAME + "UnexistingInstance", unexistingPageEdit.getTitle());
157  1 assertEquals(templateContent, unexistingPageEdit.getContent());
158    // test that this page has no parent
159  1 assertEquals("Main.WebHome", unexistingPageEdit.getParent());
160   
161    // Step 4: Create an empty new page when there are Templates available
162   
163    // Make sure we are on a page that exists so that Add > Page will show the space and page fields
164   
165  1 CreatePagePage createEmptyPage = unexistingPageEdit.clickCancel().createPage();
166  1 assertTrue(createEmptyPage.getAvailableTemplateSize() > 0);
167  1 createEmptyPage.getDocumentPicker().toggleLocationAdvancedEdit();
168  1 EditPage editEmptyPage = createEmptyPage.createPage(getTestClassName(), "EmptyPage");
169  1 ViewPage emptyPage = editEmptyPage.clickSaveAndView();
170    // make sure it's empty
171  1 assertEquals("", emptyPage.getContent());
172    // make sure parent is the right one
173  1 assertEquals("/" + getTestClassName() + "/EmptyPage", emptyPage.getBreadcrumbContent());
174    // mare sure title is the right one
175  1 assertEquals("EmptyPage", emptyPage.getDocumentTitle());
176   
177    // Step 5: Verify that restricting a Template to a space works
178   
179    // Restrict the template to its own space
180  1 templateProviderView = getUtil().gotoPage(getTestClassName(), TEMPLATE_NAME + "Provider");
181  1 templateProviderView.editInline();
182  1 TemplateProviderInlinePage templateProviderInline = new TemplateProviderInlinePage();
183  1 List<String> allowedSpaces = new ArrayList<String>();
184  1 allowedSpaces.add(getTestClassName());
185  1 templateProviderInline.setVisibilityRestrictions(allowedSpaces);
186  1 templateProviderInline.setCreationRestrictions(allowedSpaces);
187  1 templateProviderView = templateProviderInline.clickSaveAndView();
188   
189    // Verify we can still create a page from template in the test space
190  1 createPagePage = templateProviderView.createPage();
191    // Make sure we get in create mode.
192  1 assertTrue(getUtil().isInCreateMode());
193  1 assertEquals(availableTemplateSize, createPagePage.getAvailableTemplateSize());
194  1 assertTrue(createPagePage.getAvailableTemplates().contains(templateProviderFullName));
195   
196    // Modify the target space and verify the form can't be submitted
197  1 createPagePage.setTemplate(templateProviderFullName);
198  1 createPagePage.getDocumentPicker().toggleLocationAdvancedEdit();
199  1 createPagePage.getDocumentPicker().setParent("Foo");
200  1 createPagePage.getDocumentPicker().setName("Bar");
201  1 String currentURL = getDriver().getCurrentUrl();
202  1 createPagePage.clickCreate();
203  1 assertEquals(currentURL, getDriver().getCurrentUrl());
204    // and check that an error is displayed to the user
205  1 createPagePage.waitForFieldErrorMessage();
206   
207    // Verify the template we have removed is no longer available.
208  1 CreatePagePage.gotoPage();
209   
210    // make sure that the template provider is not in the list of templates
211  1 assertFalse(createPagePage.getAvailableTemplates().contains(templateProviderFullName));
212    }
213   
214    /**
215    * Tests that creating a page or a space that already exists displays an error.
216    */
 
217  1 toggle @Test
218    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See http://jira.xwiki.org/browse/XE-1146")
219    public void createExistingPageAndSpace() throws Exception
220    {
221    // Step 0: Setup the correct environment for the test
222   
223  1 EntityReference existingPageReference =
224    getUtil().resolveDocumentReference(getTestClassName() + ".ExistingPage.WebHome");
225  1 String existingSpaceName = getTestClassName() + "Existing";
226   
227    // All these pages are created during this test
228  1 getUtil().rest().delete(existingPageReference);
229  1 getUtil().rest().deletePage(existingSpaceName, "WebHome");
230   
231    // create a template to make sure that we have a template to create from
232  1 String templateProviderName = TEMPLATE_NAME + "Provider";
233  1 String templateContent = "Templates are fun";
234  1 String templateTitle = "Funny templates";
235  1 createTemplateAndTemplateProvider(templateProviderName, templateContent, templateTitle, false);
236   
237    // create a page and a space webhome
238  1 getUtil().rest().savePage(existingPageReference, "Page that already exists", "Existing page");
239  1 getUtil().rest().savePage(new LocalDocumentReference(existingSpaceName, "WebHome"), "Some content",
240    "Existing space");
241   
242    // Step 1: Create an empty page for a page that already exists
243    // First we must click on create from a page that already exists as otherwise we won't get the create UI
244  1 ViewPage vp = getUtil().gotoPage(existingPageReference);
245  1 CreatePagePage createPage = vp.createPage();
246  1 createPage.getDocumentPicker().toggleLocationAdvancedEdit();
247  1 createPage.getDocumentPicker().setParent(getTestClassName());
248  1 createPage.getDocumentPicker().setName("ExistingPage");
249  1 String currentURL = getDriver().getCurrentUrl();
250  1 createPage.clickCreate();
251    // make sure that we stay on the same page and that an error is displayed to the user. Maybe we should check the
252    // error
253  1 assertEquals(currentURL, getDriver().getCurrentUrl());
254  1 createPage.waitForErrorMessage();
255   
256    // Step 2: Create a page from Template for a page that already exists
257    // restart everything to make sure it's not the error before
258  1 vp = getUtil().gotoPage(existingPageReference);
259  1 createPage = vp.createPage();
260  1 createPage.getDocumentPicker().toggleLocationAdvancedEdit();
261  1 createPage.getDocumentPicker().setParent(getTestClassName());
262  1 createPage.getDocumentPicker().setName("ExistingPage");
263  1 createPage.setTemplate(getTestClassName() + "." + templateProviderName);
264  1 currentURL = getDriver().getCurrentUrl();
265  1 createPage.clickCreate();
266    // make sure that we stay on the same page and that an error is displayed to the user. Maybe we should check the
267    // error
268  1 assertEquals(currentURL, getDriver().getCurrentUrl());
269  1 createPage.waitForErrorMessage();
270   
271    // Step 3: Create a space that already exists
272    // Since the Flamingo skin no longer supports creating a space from the UI, trigger the Space creation UI
273    // by using directly the direct action URL for it.
274  1 getUtil().gotoPage(
275    getUtil().getURL("create", new String[] {getTestClassName(), "ExistingPage", "WebHome"}, "tocreate=space"));
276  1 CreatePagePage createSpace = new CreatePagePage();
277    // Check that the terminal choice is not displayed in this mode.
278  1 assertFalse(createSpace.isTerminalOptionDisplayed());
279   
280  1 currentURL = getDriver().getCurrentUrl();
281    // strip the parameters out of this URL
282  1 currentURL =
283  1 currentURL.substring(0, currentURL.indexOf('?') > 0 ? currentURL.indexOf('?') : currentURL.length());
284    // Try to create the a space (non-terminal document) that already exist.
285  1 createSpace.getDocumentPicker().toggleLocationAdvancedEdit();
286  1 createSpace.fillForm(existingSpaceName, "", null, false);
287  1 createSpace.clickCreate();
288  1 String urlAfterSubmit = getDriver().getCurrentUrl();
289  1 urlAfterSubmit =
290    urlAfterSubmit.substring(0,
291  1 urlAfterSubmit.indexOf('?') > 0 ? urlAfterSubmit.indexOf('?') : urlAfterSubmit.length());
292    // make sure that we stay on the same page and that an error is displayed to the user. Maybe we should check the
293    // error
294  1 assertEquals(currentURL, urlAfterSubmit);
295  1 createSpace.waitForErrorMessage();
296    }
297   
298    /**
299    * Tests the creation of a page from a save and edit template, tests that the page is indeed saved.
300    */
 
301  1 toggle @Test
302    @IgnoreBrowsers({
303    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
304    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
305    })
306    public void createPageWithSaveAndEditTemplate() throws Exception
307    {
308    // Cleanup of the test space for any leftovers from previous tests.
309  1 getUtil().deleteSpace(getTestClassName());
310   
311    // Create a template
312  1 String templateProviderName = TEMPLATE_NAME + "Provider";
313  1 String templateContent = "Templates are fun";
314  1 String templateTitle = "Funny templates";
315  1 String templateProviderFullName = getTestClassName() + "." + templateProviderName;
316  1 ViewPage templatePage =
317    createTemplateAndTemplateProvider(templateProviderName, templateContent, templateTitle, true);
318   
319    // create the page
320  1 CreatePagePage createPage = templatePage.createPage();
321  1 createPage.getDocumentPicker().toggleLocationAdvancedEdit();
322  1 EditPage editCreatedPage =
323    createPage.createPageFromTemplate(getTestClassName(), "NewPage", templateProviderFullName);
324    // and now cancel it
325  1 ViewPage newPage = editCreatedPage.clickCancel();
326    // make sure we're not in unexisting page
327  1 assertTrue(newPage.exists());
328    // we should be in view mode (useless check since the impl of isNonExisting page calls it anyway)
329  1 assertTrue(getUtil().isInViewMode());
330    // make sure it's the page we want
331  1 assertEquals(getTestClassName() + ".NewPage", newPage.getMetaDataValue("space"));
332  1 assertEquals("WebHome", newPage.getMetaDataValue("page"));
333    // and now test the title is the name of the page and the content is the one from the template
334  1 assertEquals("NewPage", newPage.getDocumentTitle());
335  1 assertEquals(templateContent, newPage.getContent());
336    // and the parent, it should be the template provider, since that's where we created it from
337  1 assertEquals("/CreatePageTest/NewPage", newPage.getBreadcrumbContent());
338    }
339   
340    /**
341    * Helper function to Create both a Template and a Template Provider for the tests in this class.
342    */
 
343  3 toggle private ViewPage createTemplateAndTemplateProvider(String templateProviderName, String templateContent,
344    String templateTitle, boolean saveAndEdit) throws Exception
345    {
346    // Cleanup of the test space for any leftovers from previous tests.
347  3 getUtil().deleteSpace(getTestClassName());
348   
349    // Create a Template page
350  3 getUtil().rest().savePage(new LocalDocumentReference(getTestClassName(), TEMPLATE_NAME), templateContent,
351    templateTitle);
352   
353    // Create a Template Provider
354  3 TemplatesAdministrationSectionPage sectionPage = TemplatesAdministrationSectionPage.gotoPage();
355  3 TemplateProviderInlinePage templateProviderInline =
356    sectionPage.createTemplateProvider(getTestClassName(), templateProviderName);
357  3 templateProviderInline.setTemplateName("Test Template");
358  3 templateProviderInline.setTemplate(getTestClassName() + "." + TEMPLATE_NAME);
359  3 if (saveAndEdit) {
360  1 templateProviderInline.setActionOnCreate(TemplateProviderInlinePage.ACTION_SAVEANDEDIT);
361    }
362  3 return templateProviderInline.clickSaveAndView();
363    }
364    }