mirror of
https://github.com/EGroupware/egroupware.git
synced 2025-01-12 17:08:34 +01:00
Some more tests of basic Etemplate stuff
This commit is contained in:
parent
bcd86f7ae9
commit
944718daaa
@ -12,12 +12,14 @@
|
|||||||
|
|
||||||
namespace EGroupware\Api\Etemplate\Widget;
|
namespace EGroupware\Api\Etemplate\Widget;
|
||||||
|
|
||||||
|
require_once realpath(__DIR__.'/../../test/WidgetBaseTest.php');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Description of TemplateTest
|
* Description of TemplateTest
|
||||||
*
|
*
|
||||||
* @author nathan
|
* @author nathan
|
||||||
*/
|
*/
|
||||||
class TemplateTest extends WidgetBaseTest {
|
class TemplateTest extends \EGroupware\Api\Etemplate\WidgetBaseTest {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test instanciation of a template
|
* Test instanciation of a template
|
||||||
|
@ -16,11 +16,65 @@ namespace EGroupware\Api\Etemplate;
|
|||||||
require_once realpath(__DIR__.'/../../test/LoggedInTest.php');
|
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
|
* Widget scans the apps for widgets, which needs the app list, pulled from the
|
||||||
* database, so we need to log in.
|
* database, so we need to log in.
|
||||||
*/
|
*/
|
||||||
class WidgetBaseTest extends \EGroupware\Api\LoggedInTest {
|
abstract class WidgetBaseTest extends \EGroupware\Api\LoggedInTest {
|
||||||
//put your code here
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,8 +12,7 @@
|
|||||||
|
|
||||||
namespace EGroupware\Api\Etemplate;
|
namespace EGroupware\Api\Etemplate;
|
||||||
|
|
||||||
// test base providing Egw environment, since we need the DB
|
require_once realpath(__DIR__.'/WidgetBaseTest.php');
|
||||||
require_once realpath(__DIR__.'/../../test/LoggedInTest.php');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for the base widget class
|
* 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
|
* Widget scans the apps for widgets, which needs the app list, pulled from the
|
||||||
* database, so we need to log in.
|
* 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
|
* @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()
|
public function testAttributes()
|
||||||
{
|
{
|
||||||
$xml = "<widget id=\"test\" attribute=\"set\" />";
|
$xml = '<widget id="test" attribute="set" />';
|
||||||
$widget = new Widget($xml);
|
$widget = new Widget($xml);
|
||||||
|
|
||||||
|
|
||||||
@ -55,10 +54,16 @@ class WidgetTest extends \EGroupware\Api\LoggedInTest {
|
|||||||
// get/setElementAttribute do not include xml
|
// get/setElementAttribute do not include xml
|
||||||
$this->assertNull($widget->getElementAttribute('test','attribute'));
|
$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
|
// XML does not include get/setElementAttribute
|
||||||
$widget->setElementAttribute('test', 'other_attribute', 'set');
|
$widget->setElementAttribute('test', 'other_attribute', 'set');
|
||||||
$this->assertEquals('set', $widget->getElementAttribute('test','other_attribute'));
|
$this->assertEquals('set', $widget->getElementAttribute('test','other_attribute'));
|
||||||
$this->assertNull($widget->attrs['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[]']
|
['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');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
|
|
||||||
namespace EGroupware\Api;
|
namespace EGroupware\Api;
|
||||||
|
|
||||||
|
require_once realpath(__DIR__.'/../Etemplate/test/WidgetBaseTest.php');
|
||||||
|
|
||||||
use EGroupware\Api;
|
use EGroupware\Api;
|
||||||
use PHPUnit_Framework_TestCase as TestCase;
|
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
|
* Etemplate Widget base classscans the apps for widgets, which needs the app
|
||||||
* list, pulled from the database, so we need to log in.
|
* 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
|
* Test reading xml files
|
||||||
*
|
*
|
||||||
* This really just tests that the files can be found. Testing the parsing
|
* This really just tests that the files can be found and executed.
|
||||||
* is done in the Template test.
|
|
||||||
*/
|
*/
|
||||||
public function testRead()
|
public function testRead()
|
||||||
{
|
{
|
||||||
@ -37,39 +44,80 @@ class EtemplateTest extends \EGroupware\Api\LoggedInTest {
|
|||||||
$this->assertEquals(false, $etemplate->read('totally invalid'), 'Reading invalid template');
|
$this->assertEquals(false, $etemplate->read('totally invalid'), 'Reading invalid template');
|
||||||
|
|
||||||
// Templates must be in the correct templates directory - use one from API
|
// 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()
|
public function testSetDOMId()
|
||||||
{
|
{
|
||||||
|
// Templates must be in the correct templates directory - use one from API
|
||||||
$etemplate = new Etemplate();
|
$etemplate = new Etemplate();
|
||||||
/*
|
$etemplate->read(static::TEST_TEMPLATE);
|
||||||
Etemplate::$response = $this->getMockBuilder(Etemplate\Api\Json\Response)
|
|
||||||
->disableOriginalConstructor()
|
// Change the target DOM ID
|
||||||
->setMethods(['generic'])
|
|
||||||
->getMock($etemplate);
|
|
||||||
Etemplate::$response->expects($this->once())
|
|
||||||
->method('generic');
|
|
||||||
*/
|
|
||||||
$etemplate->set_dom_id('test_id');
|
$etemplate->set_dom_id('test_id');
|
||||||
|
|
||||||
$this->markTestIncomplete(
|
$result = $this->mockedExec($etemplate, '',array());
|
||||||
'This test has not been implemented yet.'
|
|
||||||
);
|
// Check for the load
|
||||||
}
|
foreach($result as $command)
|
||||||
|
{
|
||||||
public function testArrayMerge()
|
if($command['type'] == 'et2_load')
|
||||||
{
|
{
|
||||||
$this->markTestIncomplete(
|
$this->assertEquals('test_id', $command['data']['DOMNodeID']);
|
||||||
'This test has not been implemented yet.'
|
break;
|
||||||
);
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that data that is passed in is passed on
|
||||||
|
*/
|
||||||
public function testExec()
|
public function testExec()
|
||||||
{
|
{
|
||||||
$this->markTestIncomplete(
|
$content = array('id' => 'value');
|
||||||
'This test has not been implemented yet.'
|
$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');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
<!DOCTYPE overlay PUBLIC "-//EGroupware GmbH//eTemplate 2//EN" "http://www.egroupware.org/etemplate2.dtd">
|
<!DOCTYPE overlay PUBLIC "-//EGroupware GmbH//eTemplate 2//EN" "http://www.egroupware.org/etemplate2.dtd">
|
||||||
<!-- $Id$ -->
|
<!-- $Id$ -->
|
||||||
<overlay>
|
<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">
|
<hbox class="et2_prompt">
|
||||||
<image src="dialog_help" class="dialog_icon"/>
|
<image src="dialog_help" class="dialog_icon"/>
|
||||||
<vbox class="ui-dialog-content">
|
<vbox class="ui-dialog-content">
|
||||||
|
Loading…
Reference in New Issue
Block a user