Some more tests of basic Etemplate stuff

This commit is contained in:
nathangray 2017-04-13 12:21:41 -06:00
parent bcd86f7ae9
commit 944718daaa
5 changed files with 177 additions and 36 deletions

View File

@ -12,12 +12,14 @@
namespace EGroupware\Api\Etemplate\Widget;
require_once realpath(__DIR__.'/../../test/WidgetBaseTest.php');
/**
* Description of TemplateTest
*
* @author nathan
*/
class TemplateTest extends WidgetBaseTest {
class TemplateTest extends \EGroupware\Api\Etemplate\WidgetBaseTest {
/**
* Test instanciation of a template

View File

@ -16,11 +16,65 @@ namespace EGroupware\Api\Etemplate;
require_once realpath(__DIR__.'/../../test/LoggedInTest.php');
/**
* Base class for all widget tests
* Base class for all widget tests doing needed setup so the tests can run, and
* providing common utilities.
*
* Widget scans the apps for widgets, which needs the app list, pulled from the
* database, so we need to log in.
*/
class WidgetBaseTest extends \EGroupware\Api\LoggedInTest {
//put your code here
abstract class WidgetBaseTest extends \EGroupware\Api\LoggedInTest {
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
// Call Etemplate constructor once to make sure etemplate::$request is set,
// otherwise some tests will fail.
// In normal usage, this is not needed as Etemplate constructor does it.
new \EGroupware\Api\Etemplate();
}
/**
* Mocks what is needed to fake a call to exec, and catch its output.
* The resulting array of information, which would normally be sent to the
* client as JSON, is returned for evaluation.
*
* @param String $method
* @param array $content
* @param array $sel_options
* @param array $readonlys
* @param array $preserv
*/
protected function mockedExec(\EGroupware\Api\Etemplate $etemplate, $method,array $content,array $sel_options=null,array $readonlys=null,array $preserv=null)
{
$response = $this->getMockBuilder('\\EGroupware\\Api\\Json\\Response')
->disableOriginalConstructor()
->setMethods(['get'/*,'generic'*/])
->getMock($etemplate);
// Replace protected self reference with mock object
$ref = new \ReflectionProperty('\\EGroupware\\Api\\Json\\Response', 'response');
$ref->setAccessible(true);
$ref->setValue(null, $response);
$response
->method('get')
->with(function() {
// Don't send headers, like the real one does
return self::$response;
});
ob_start();
// Exec the template
$etemplate->exec($method, $content, $sel_options, $readonlys, $preserv, 4);
$result = $response->returnResult();
// Clean json response
$response->initResponseArray();
$ref->setValue(null, null);
ob_end_clean();
return $result;
}
}

View File

@ -12,8 +12,7 @@
namespace EGroupware\Api\Etemplate;
// test base providing Egw environment, since we need the DB
require_once realpath(__DIR__.'/../../test/LoggedInTest.php');
require_once realpath(__DIR__.'/WidgetBaseTest.php');
/**
* Tests for the base widget class
@ -21,7 +20,7 @@ require_once realpath(__DIR__.'/../../test/LoggedInTest.php');
* Widget scans the apps for widgets, which needs the app list, pulled from the
* database, so we need to log in.
*/
class WidgetTest extends \EGroupware\Api\LoggedInTest {
class WidgetTest extends WidgetBaseTest {
/**
* @var Array Used as a common content for expansion
@ -39,11 +38,11 @@ class WidgetTest extends \EGroupware\Api\LoggedInTest {
);
/**
* Test that setting and retrieving widget attributes is sane
* Test that setting and retrieving widget attributes is working as expected
*/
public function testAttributes()
{
$xml = "<widget id=\"test\" attribute=\"set\" />";
$xml = '<widget id="test" attribute="set" />';
$widget = new Widget($xml);
@ -55,10 +54,16 @@ class WidgetTest extends \EGroupware\Api\LoggedInTest {
// get/setElementAttribute do not include xml
$this->assertNull($widget->getElementAttribute('test','attribute'));
// Calling setElementAttribute without a request will give an error when
// it tries to set the header.
ob_start();
// XML does not include get/setElementAttribute
$widget->setElementAttribute('test', 'other_attribute', 'set');
$this->assertEquals('set', $widget->getElementAttribute('test','other_attribute'));
$this->assertNull($widget->attrs['other_attribute']);
ob_end_clean();
}
/**
@ -114,4 +119,36 @@ class WidgetTest extends \EGroupware\Api\LoggedInTest {
['container', '@expand_2[@expand_me]', 'container[]']
);
}
/**
* Test that the widget loads the xml and gets all children
*/
public function testSimpleLoad()
{
$test_template = <<<EOT
<widget id="main_parent">
<widget id="first_child"/>
<widget id="second_child">
<widget id="sub_widget"/>
</widget>
<widget id="third_child">
<widget id="@expand_me"/>
</widget>
</widget>
EOT;
$widget = new Widget($test_template);
// getElementsByType does not include the widget itself
$this->assertEquals(5, count($widget->getElementsByType('widget')), 'Missing children');
// Check that it can find the sub
$this->assertNotNull($widget->getElementById('sub_widget'), 'Could not find sub_widget');
// Check that it can find the un-expanded - expansion doesn't happen on load
$this->assertNotNull($widget->getElementById('@expand_me'), 'Could not find @expand_me');
// Check failure
$this->assertNull($widget->getElementById('totally_invalid'), 'Found widget that is not there');
}
}

View File

@ -12,6 +12,8 @@
namespace EGroupware\Api;
require_once realpath(__DIR__.'/../Etemplate/test/WidgetBaseTest.php');
use EGroupware\Api;
use PHPUnit_Framework_TestCase as TestCase;
@ -21,13 +23,18 @@ use PHPUnit_Framework_TestCase as TestCase;
* Etemplate Widget base classscans the apps for widgets, which needs the app
* list, pulled from the database, so we need to log in.
*/
class EtemplateTest extends \EGroupware\Api\LoggedInTest {
class EtemplateTest extends Etemplate\WidgetBaseTest {
/**
* Etemplate checks in the app/template/ directory, so we can't easily
* use a specific test template. Using this real template for testing.
*/
const TEST_TEMPLATE = 'api.prompt';
/**
* Test reading xml files
*
* This really just tests that the files can be found. Testing the parsing
* is done in the Template test.
* This really just tests that the files can be found and executed.
*/
public function testRead()
{
@ -37,39 +44,80 @@ class EtemplateTest extends \EGroupware\Api\LoggedInTest {
$this->assertEquals(false, $etemplate->read('totally invalid'), 'Reading invalid template');
// Templates must be in the correct templates directory - use one from API
$this->assertEquals(true, $etemplate->read('api.prompt'));
// This does not actually do anything with the template file
$this->assertEquals(true, $etemplate->read(static::TEST_TEMPLATE));
// This loads and parses
$result = $this->mockedExec($etemplate, '',array());
// Look for the load and match the template name
foreach($result as $command)
{
if($command['type'] == 'et2_load')
{
$this->assertEquals(static::TEST_TEMPLATE, $command['data']['name']);
break;
}
}
}
/**
* Test that we can load the etemplate into a different DOM ID than the
* default, which is based on the template name.
*/
public function testSetDOMId()
{
// Templates must be in the correct templates directory - use one from API
$etemplate = new Etemplate();
/*
Etemplate::$response = $this->getMockBuilder(Etemplate\Api\Json\Response)
->disableOriginalConstructor()
->setMethods(['generic'])
->getMock($etemplate);
Etemplate::$response->expects($this->once())
->method('generic');
*/
$etemplate->read(static::TEST_TEMPLATE);
// Change the target DOM ID
$etemplate->set_dom_id('test_id');
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
$result = $this->mockedExec($etemplate, '',array());
public function testArrayMerge()
// Check for the load
foreach($result as $command)
{
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
if($command['type'] == 'et2_load')
{
$this->assertEquals('test_id', $command['data']['DOMNodeID']);
break;
}
}
}
/**
* Test that data that is passed in is passed on
*/
public function testExec()
{
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
$content = array('id' => 'value');
$sel_options = array(array('value' => 0, 'label' => 'label'));
$readonlys = array('id' => true);
// Templates must be in the correct templates directory - use one from API
$etemplate = new Etemplate();
$etemplate->read(static::TEST_TEMPLATE);
// Change the target DOM ID
$etemplate->set_dom_id('test_id');
$result = $this->mockedExec($etemplate, '',$content, $sel_options, $readonlys);
// Check for the load
$data = array();
foreach($result as $command)
{
if($command['type'] == 'et2_load')
{
$data = $command['data'];
break;
}
}
$this->assertArraySubset($content, $data['data']['content'], false, 'Content does not match');
$this->assertArraySubset($sel_options, $data['data']['sel_options'], false, 'Select options do not match');
$this->assertArraySubset($readonlys, $data['data']['readonlys'], false, 'Readonlys does not match');
}
}

View File

@ -2,7 +2,7 @@
<!DOCTYPE overlay PUBLIC "-//EGroupware GmbH//eTemplate 2//EN" "http://www.egroupware.org/etemplate2.dtd">
<!-- $Id$ -->
<overlay>
<template id="etemplate.prompt" template="" lang="" group="0" version="1.9.001">
<template id="api.prompt" template="" lang="" group="0" version="1.9.001">
<hbox class="et2_prompt">
<image src="dialog_help" class="dialog_icon"/>
<vbox class="ui-dialog-content">