/**
   * Tests the creation of a page from a save and edit template, tests that the page is indeed
   * saved.
   */
  @Test
  @IgnoreBrowsers({
    @IgnoreBrowser(
        value = "internet.*",
        version = "8\\.*",
        reason = "See http://jira.xwiki.org/browse/XE-1146"),
    @IgnoreBrowser(
        value = "internet.*",
        version = "9\\.*",
        reason = "See http://jira.xwiki.org/browse/XE-1177")
  })
  public void testCreatePageWithSaveAndEditTemplate() {
    String space = this.getClass().getSimpleName();
    String newPageName = "NewPage";
    getUtil().deletePage(space, newPageName);

    // create a template
    String templateProviderName = TEMPLATE_NAME + "Provider";
    String templateContent = "Templates are fun";
    String templateTitle = "Funny templates";
    String templateProviderFullName = space + "." + templateProviderName;
    ViewPage templatePage =
        createTemplate(templateProviderName, templateContent, templateTitle, true);

    // create the page
    CreatePagePage createPage = templatePage.createPage();
    WYSIWYGEditPage editCreatedPage =
        createPage.createPageFromTemplate(space, newPageName, templateProviderFullName, true);
    // ensure that we're indeed in edit mode
    Assert.assertTrue(getUtil().isInWYSIWYGEditMode());
    // wait for editor to load (so that content is loaded)
    editCreatedPage.waitUntilPageIsLoaded();
    // and now cancel it
    ViewPage newPage = editCreatedPage.clickCancel();
    // make sure we're not in unexisting page
    Assert.assertTrue(newPage.exists());
    // we should be in view mode (useless check since the impl of isNonExisting page calls it
    // anyway)
    Assert.assertTrue(getUtil().isInViewMode());
    // make sure it's the page we want
    Assert.assertEquals(space, newPage.getMetaDataValue("space"));
    Assert.assertEquals(newPageName, newPage.getMetaDataValue("page"));
    // and now test the title is the name of the page and the content is the one from the template
    Assert.assertEquals(newPageName, newPage.getDocumentTitle());
    Assert.assertEquals(templateContent, newPage.getContent());
    // and the parent, it should be the template provider, since that's where we created it from
    Assert.assertTrue(newPage.hasBreadcrumbContent(templateProviderName, false));
  }
  /** Tests if a new page can be created from a template. */
  @Test
  @IgnoreBrowsers({
    @IgnoreBrowser(
        value = "internet.*",
        version = "8\\.*",
        reason = "See http://jira.xwiki.org/browse/XE-1146"),
    @IgnoreBrowser(
        value = "internet.*",
        version = "9\\.*",
        reason = "See http://jira.xwiki.org/browse/XE-1177")
  })
  public void testCreatePageFromTemplate() {
    // Setup the correct environment for the test
    getUtil().deletePage(getTestClassName(), getTestMethodName());
    // all these pages are created during this test
    getUtil().deletePage(getTestClassName(), TEMPLATE_NAME + "Instance");
    getUtil().deletePage(getTestClassName(), "NewPage");
    getUtil().deletePage(getTestClassName(), TEMPLATE_NAME + "UnexistingInstance");
    getUtil().deletePage(getTestClassName(), "EmptyPage");

    String templateContent = "My Template Content";
    String templateTitle = "My Template Title";
    String templateProviderName = TEMPLATE_NAME + "Provider";
    String templateProviderFullName = getTestClassName() + "." + templateProviderName;

    ViewPage templateProviderView =
        createTemplate(templateProviderName, templateContent, templateTitle, false);

    // Create the new document from template
    CreatePagePage createPagePage = templateProviderView.createPage();
    // Save the number of available templates so that we can make some checks later on.
    int availableTemplateSize = createPagePage.getAvailableTemplateSize();
    String templateInstanceName = TEMPLATE_NAME + "Instance";
    WYSIWYGEditPage templateInstanceEditWysiwyg =
        createPagePage.createPageFromTemplate(
            getTestClassName(), templateInstanceName, templateProviderFullName, true);
    templateInstanceEditWysiwyg.waitUntilPageIsLoaded();
    WikiEditPage templateInstanceEdit = templateInstanceEditWysiwyg.clickSaveAndView().editWiki();

    // Verify template instance content
    Assert.assertEquals(templateInstanceName, templateInstanceEdit.getTitle());
    Assert.assertEquals(templateContent, templateInstanceEdit.getContent());
    // check the parent of the template instance
    Assert.assertEquals(templateProviderFullName, templateInstanceEdit.getParent());

    // Put a wanted link in the template instance
    templateInstanceEdit.setContent("[[NewPage]]");
    ViewPage vp = templateInstanceEdit.clickSaveAndView();

    // Verify that clicking on the wanted link pops up a box to choose the template.
    vp.clickWantedLink(getTestClassName(), "NewPage", true);
    List<WebElement> templates = getDriver().findElements(By.name("templateprovider"));
    // Note: We need to remove 1 to exclude the "Empty Page" template entry
    Assert.assertEquals(availableTemplateSize, templates.size() - 1);
    Assert.assertTrue(createPagePage.getAvailableTemplates().contains(templateProviderFullName));

    // Create a new page from template by going to a non-existing page
    // And make sure we're on a non-existing page
    getUtil()
        .gotoPage(
            getTestClassName(),
            TEMPLATE_NAME + "UnexistingInstance",
            "view",
            "spaceRedirect=false");
    vp = new ViewPage();
    Assert.assertFalse(vp.exists());
    DocumentDoesNotExistPage unexistingPage = new DocumentDoesNotExistPage();
    unexistingPage.clickEditThisPageToCreate();
    CreatePagePage createUnexistingPage = new CreatePagePage();
    // Make sure we're in create mode.
    Assert.assertTrue(getUtil().isInCreateMode());
    // count the available templates, make sure they're as many as before and that our template is
    // among them
    templates = getDriver().findElements(By.name("templateprovider"));
    // Note: We need to remove 1 to exclude the "Empty Page" template entry
    Assert.assertEquals(availableTemplateSize, templates.size() - 1);
    Assert.assertTrue(createPagePage.getAvailableTemplates().contains(templateProviderFullName));
    // select it
    createUnexistingPage.setTemplate(templateProviderFullName);
    // and create
    createUnexistingPage.clickCreate();
    WYSIWYGEditPage unexistingPageEditWysiwyg = new WYSIWYGEditPage();
    unexistingPageEditWysiwyg.waitUntilPageIsLoaded();
    WikiEditPage unexistingPageEdit = unexistingPageEditWysiwyg.clickSaveAndView().editWiki();

    // Verify template instance content
    Assert.assertEquals(TEMPLATE_NAME + "UnexistingInstance", unexistingPageEdit.getTitle());
    Assert.assertEquals(templateContent, unexistingPageEdit.getContent());
    // test that this page has no parent
    Assert.assertEquals("Main.WebHome", unexistingPageEdit.getParent());

    // create an empty page when there is a template available, make sure it's empty
    CreatePagePage createEmptyPage = CreatePagePage.gotoPage();
    Assert.assertTrue(createEmptyPage.getAvailableTemplateSize() > 0);
    WYSIWYGEditPage editEmptyPage = createEmptyPage.createPage(getTestClassName(), "EmptyPage");
    Assert.assertTrue(getUtil().isInWYSIWYGEditMode());
    // wait to load editor to make sure that what we're saving is the content that is supposed to be
    // in this
    // document
    editEmptyPage.waitUntilPageIsLoaded();
    ViewPage emptyPage = editEmptyPage.clickSaveAndView();
    // make sure it's empty
    Assert.assertEquals("", emptyPage.getContent());
    // make sure parent is the right one
    Assert.assertTrue(emptyPage.hasBreadcrumbContent("Wiki Home", false));
    // mare sure title is the right one
    Assert.assertEquals("EmptyPage", emptyPage.getDocumentTitle());

    // Restrict the template to its own space
    templateProviderView = getUtil().gotoPage(getTestClassName(), TEMPLATE_NAME + "Provider");
    templateProviderView.editInline();
    TemplateProviderInlinePage templateProviderInline = new TemplateProviderInlinePage();
    List<String> allowedSpaces = new ArrayList<String>();
    allowedSpaces.add(getTestClassName());
    templateProviderInline.setSpaces(allowedSpaces);
    templateProviderView = templateProviderInline.clickSaveAndView();

    // Verify we can still create a page from template in the test space
    createPagePage = templateProviderView.createPage();
    // Make sure we get in create mode.
    Assert.assertTrue(getUtil().isInCreateMode());
    Assert.assertEquals(availableTemplateSize, createPagePage.getAvailableTemplateSize());
    Assert.assertTrue(createPagePage.getAvailableTemplates().contains(templateProviderFullName));

    // Modify the target space and verify the form can't be submitted
    createPagePage.setTemplate(templateProviderFullName);
    createPagePage.setSpace("Foobar");
    String currentURL = getDriver().getCurrentUrl();
    createPagePage.clickCreate();
    Assert.assertEquals(currentURL, getDriver().getCurrentUrl());
    // and check that an error is displayed to the user
    createPagePage.waitForFieldErrorMessage();

    // Verify the template we have removed is no longer available.
    createEmptyPage = CreatePagePage.gotoPage();

    // make sure that the template provider is not in the list of templates
    Assert.assertFalse(createPagePage.getAvailableTemplates().contains(templateProviderFullName));
  }