/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.test.ui;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.xwiki.test.ui.framework.elements.editor.WikiEditPage;
import org.xwiki.test.ui.framework.AbstractAdminAuthenticatedTest;
/**
* Test template handling.
*
* @version $Id$
* @since 2.4M1
*/
public class TemplateTest extends AbstractAdminAuthenticatedTest
{
/** Page used for testing: Main.TemplateTest */
private WikiEditPage editPage;
@Before
@Override
public void setUp()
{
super.setUp();
editPage = new WikiEditPage();
}
/**
* Test that velocity is rendered
*/
@Test
public void testHelloVelocity()
{
String hello = "Hello Velocity Test Test Test";
saveVelocity(hello);
Assert.assertTrue(getDriver().findElement(By.id("xwikicontent")).getText().contains(hello));
}
/**
* Test that an included existing template is displayed correctly
*/
@Test
public void testCorrectTemplate()
{
saveVelocity(includeTemplate("code.vm"), true);
Assert.assertNotNull(getDriver().findElement(
By.xpath("//div[@id='xwikicontent']//textarea[@class='wiki-code']")));
}
/**
* See XWIKI-2580
*/
@Test
public void testWrongTemplate()
{
saveVelocity(includeTemplate("../../"));
Assert.assertTrue("root directory", getDriver().findElement(By.id("xwikicontent")).getText().length() == 0);
saveVelocity(includeTemplate("asdfasdf"));
Assert.assertTrue("not existing template",
getDriver().findElement(By.id("xwikicontent")).getText().length() == 0);
saveVelocity(includeTemplate("../redirect"));
Assert.assertTrue("file in the parent directory", getDriver().findElement(By.id("xwikicontent")).getText()
.length() == 0);
saveVelocity(includeTemplate("../WEB-INF/version.properties"));
Assert.assertTrue("file in the wrong directory", getDriver().findElement(By.id("xwikicontent")).getText()
.length() == 0);
saveVelocity(includeTemplate("/chw/../../WEB-INF/../WEB-INF/lib/../version.properties"));
Assert.assertTrue("file in the wrong directory, not normalized path", getDriver().findElement(
By.id("xwikicontent")).getText().length() == 0);
}
/**
* @see #saveVelocity(String, boolean)
*/
private void saveVelocity(String code)
{
saveVelocity(code, false);
}
/**
* Save a page with given velocity code and switch to view. Encloses <code>code</code> into the {{velocity}} macro
* and optionally also {{html}} macro.
*
* @param code velocity code to save
* @param html additionally enclose <code>code</code> in {{html}} if true
*/
private void saveVelocity(String code, boolean html)
{
editPage.switchToEdit("Main", "TemplateTest");
if (html) {
code = "{{html wiki=\"false\"}}\n" + code + "\n{{/html}}";
}
editPage.setContent("{{velocity filter=\"none\"}}\n" + code + "\n{{/velocity}}\n");
editPage.clickSaveAndView();
}
/**
* Return velocity code to include a velocity template <code>name</code>
*
* @param name template to use
*/
private String includeTemplate(String name)
{
return "#template(\"" + name + "\")\n";
}
}