2017-04-06 19:11:58 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test Etemplate main file
|
|
|
|
*
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
* @package api
|
|
|
|
* @copyright (c) 2017 Nathan Gray
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace EGroupware\Api;
|
|
|
|
|
2017-10-23 10:14:14 +02:00
|
|
|
require_once realpath(__DIR__.'/Etemplate/WidgetBaseTest.php');
|
2017-04-13 20:21:41 +02:00
|
|
|
|
2017-04-06 19:11:58 +02:00
|
|
|
/**
|
|
|
|
* Test the main class of Etemplate
|
|
|
|
*
|
2018-01-29 17:04:29 +01:00
|
|
|
* Etemplate Widget base class scans the apps for widgets, which needs the app
|
2017-04-06 19:11:58 +02:00
|
|
|
* list, pulled from the database, so we need to log in.
|
|
|
|
*/
|
2017-04-13 20:21:41 +02:00
|
|
|
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';
|
2017-04-06 19:11:58 +02:00
|
|
|
|
2017-04-21 20:17:59 +02:00
|
|
|
protected $content = array('value' => 'test content');
|
|
|
|
protected $sel_options = array(array('value' => 0, 'label' => 'label'));
|
|
|
|
protected $readonlys = array('value' => true);
|
2017-08-18 11:45:10 +02:00
|
|
|
|
2017-04-06 19:11:58 +02:00
|
|
|
/**
|
|
|
|
* Test reading xml files
|
|
|
|
*
|
2017-08-18 11:45:10 +02:00
|
|
|
* This really just tests that the files can be found and executed.
|
2017-04-06 19:11:58 +02:00
|
|
|
*/
|
|
|
|
public function testRead()
|
|
|
|
{
|
|
|
|
$etemplate = new Etemplate();
|
|
|
|
|
|
|
|
// Test missing template fails
|
|
|
|
$this->assertEquals(false, $etemplate->read('totally invalid'), 'Reading invalid template');
|
|
|
|
|
|
|
|
// Templates must be in the correct templates directory - use one from API
|
2017-04-13 20:21:41 +02:00
|
|
|
// This does not actually do anything with the template file
|
|
|
|
$this->assertEquals(true, $etemplate->read(static::TEST_TEMPLATE));
|
|
|
|
|
|
|
|
// This loads and parses
|
2017-04-21 20:17:59 +02:00
|
|
|
$result = $this->mockedExec($etemplate, array());
|
2017-04-13 20:21:41 +02:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2017-04-06 19:11:58 +02:00
|
|
|
}
|
|
|
|
|
2017-04-13 20:21:41 +02:00
|
|
|
/**
|
|
|
|
* Test that we can load the etemplate into a different DOM ID than the
|
|
|
|
* default, which is based on the template name.
|
|
|
|
*/
|
2017-04-06 19:11:58 +02:00
|
|
|
public function testSetDOMId()
|
|
|
|
{
|
2017-04-13 20:21:41 +02:00
|
|
|
// Templates must be in the correct templates directory - use one from API
|
2017-04-06 19:11:58 +02:00
|
|
|
$etemplate = new Etemplate();
|
2017-04-13 20:21:41 +02:00
|
|
|
$etemplate->read(static::TEST_TEMPLATE);
|
|
|
|
|
|
|
|
// Change the target DOM ID
|
2017-04-06 19:11:58 +02:00
|
|
|
$etemplate->set_dom_id('test_id');
|
|
|
|
|
2017-04-21 20:17:59 +02:00
|
|
|
$result = $this->mockedExec($etemplate, array());
|
2017-04-06 19:11:58 +02:00
|
|
|
|
2017-04-13 20:21:41 +02:00
|
|
|
// Check for the load
|
|
|
|
foreach($result as $command)
|
|
|
|
{
|
|
|
|
if($command['type'] == 'et2_load')
|
|
|
|
{
|
|
|
|
$this->assertEquals('test_id', $command['data']['DOMNodeID']);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-04-06 19:11:58 +02:00
|
|
|
}
|
|
|
|
|
2017-04-13 20:21:41 +02:00
|
|
|
/**
|
|
|
|
* Test that data that is passed in is passed on
|
|
|
|
*/
|
2017-04-06 19:11:58 +02:00
|
|
|
public function testExec()
|
|
|
|
{
|
2017-04-13 20:21:41 +02:00
|
|
|
// 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');
|
|
|
|
|
2017-04-21 20:17:59 +02:00
|
|
|
$result = $this->mockedExec($etemplate, $this->content, $this->sel_options, $this->readonlys);
|
2017-04-13 20:21:41 +02:00
|
|
|
|
|
|
|
// Check for the load
|
|
|
|
$data = array();
|
2020-03-10 22:53:26 +01:00
|
|
|
foreach ($result as $command)
|
2017-04-13 20:21:41 +02:00
|
|
|
{
|
2020-03-10 22:53:26 +01:00
|
|
|
if ($command['type'] == 'et2_load')
|
2017-04-13 20:21:41 +02:00
|
|
|
{
|
|
|
|
$data = $command['data'];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 22:53:26 +01:00
|
|
|
foreach ($this->content as $check_key => $check_value)
|
|
|
|
{
|
|
|
|
$this->assertArrayHasKey($check_key, $data['data']['content'], 'Content does not match');
|
|
|
|
$this->assertEquals($check_value, $data['data']['content'][$check_key], 'Content does not match');
|
|
|
|
}
|
|
|
|
foreach ($this->sel_options as $check_key => $check_value)
|
|
|
|
{
|
|
|
|
$this->assertArrayHasKey($check_key, $data['data']['sel_options'], 'Select options does not match');
|
|
|
|
$this->assertEquals($check_value, $data['data']['sel_options'][$check_key], 'Select options does not match');
|
|
|
|
}
|
|
|
|
foreach ($this->readonlys as $check_key => $check_value)
|
|
|
|
{
|
|
|
|
$this->assertArrayHasKey($check_key, $data['data']['readonlys'], 'Readonlys does not match');
|
|
|
|
$this->assertEquals($check_value, $data['data']['readonlys'][$check_key], 'Readonlys does not match');
|
|
|
|
}
|
2017-04-21 20:17:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that data passed in is passed back
|
|
|
|
*
|
|
|
|
* In this case, since there's one input widget and we're passing it's value, and
|
|
|
|
* we're not passing anything extra and no preserve, it should be the same.
|
|
|
|
*
|
|
|
|
* @depends testExec
|
|
|
|
*/
|
|
|
|
public function testRoundTrip()
|
|
|
|
{
|
|
|
|
// Templates must be in the correct templates directory - use one from API
|
|
|
|
$etemplate = new Etemplate();
|
|
|
|
$etemplate->read(static::TEST_TEMPLATE);
|
|
|
|
|
|
|
|
$this->readonlys['value'] = false;
|
2017-08-18 11:45:10 +02:00
|
|
|
|
2017-04-21 20:17:59 +02:00
|
|
|
$result = $this->mockedRoundTrip($etemplate, $this->content, $this->sel_options, $this->readonlys);
|
|
|
|
|
|
|
|
$this->assertEquals($this->content, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple test of a read-only widget
|
|
|
|
*
|
|
|
|
* The value is passed in, but does not come back
|
|
|
|
*
|
|
|
|
* @depends testExec
|
|
|
|
*/
|
|
|
|
public function testSimpleReadonly()
|
|
|
|
{
|
|
|
|
// Templates must be in the correct templates directory - use one from API
|
|
|
|
$etemplate = new Etemplate();
|
|
|
|
$etemplate->read(static::TEST_TEMPLATE);
|
|
|
|
|
|
|
|
$this->readonlys['value'] = true;
|
|
|
|
|
|
|
|
$result = $this->mockedRoundTrip($etemplate, $this->content, $this->sel_options, $this->readonlys);
|
|
|
|
|
|
|
|
// The only input widget is readonly, expect an empty array
|
|
|
|
$this->assertEquals(array(), $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple test of preserve
|
|
|
|
*
|
|
|
|
* The value is passed in, and comes back, even if the widget is readonly,
|
|
|
|
* or if there is no matching widget.
|
|
|
|
*
|
|
|
|
* @depends testExec
|
|
|
|
*/
|
|
|
|
public function testArbitraryPreserve()
|
|
|
|
{
|
|
|
|
// Templates must be in the correct templates directory - use one from API
|
|
|
|
$etemplate = new Etemplate();
|
|
|
|
$etemplate->read(static::TEST_TEMPLATE);
|
|
|
|
|
|
|
|
$this->readonlys['value'] = true;
|
|
|
|
|
|
|
|
$preserve = array('arbitrary' => 'value');
|
|
|
|
$result = $this->mockedRoundTrip($etemplate, $this->content, $this->sel_options, $this->readonlys, $preserve);
|
|
|
|
|
|
|
|
// The only input widget is readonly, expect preserve back
|
|
|
|
$this->assertEquals($preserve, $result);
|
|
|
|
|
|
|
|
// Now try with widget
|
|
|
|
$this->readonlys['value'] = false;
|
|
|
|
|
2017-08-18 11:45:10 +02:00
|
|
|
$result2 = $this->mockedRoundTrip($etemplate, $this->content, $this->sel_options, $this->readonlys, $preserve);
|
2017-04-21 20:17:59 +02:00
|
|
|
|
|
|
|
// The only input widget is readonly, expect preserve + content back
|
2020-03-10 22:53:26 +01:00
|
|
|
foreach ($this->content as $check_key => $check_value)
|
|
|
|
{
|
|
|
|
$this->assertArrayHasKey($check_key, $result2, 'Content does not match');
|
|
|
|
$this->assertEquals($check_value, $result2[$check_key], 'Content does not match');
|
|
|
|
}
|
|
|
|
foreach ($preserve as $check_key => $check_value)
|
|
|
|
{
|
|
|
|
$this->assertArrayHasKey($check_key, $result2, 'Preserve does not match');
|
|
|
|
$this->assertEquals($check_value, $result2[$check_key], 'Preserve does not match');
|
|
|
|
}
|
2017-04-21 20:17:59 +02:00
|
|
|
}
|
|
|
|
|
2017-08-21 23:15:20 +02:00
|
|
|
/**
|
|
|
|
* Test of editable widget value overriding preserved value but a readonly
|
|
|
|
* widget does not override preserved value.
|
|
|
|
*/
|
2017-04-21 20:17:59 +02:00
|
|
|
public function testReadonlyPreserve()
|
|
|
|
{
|
|
|
|
$etemplate = new Etemplate();
|
|
|
|
$etemplate->read(static::TEST_TEMPLATE);
|
2017-08-18 11:45:10 +02:00
|
|
|
|
2017-04-21 20:17:59 +02:00
|
|
|
$this->readonlys['value'] = true;
|
|
|
|
$preserve['value'] = 'preserved_value';
|
|
|
|
|
|
|
|
$result = $this->mockedRoundTrip($etemplate, $this->content, $this->sel_options, $this->readonlys, $preserve);
|
|
|
|
|
|
|
|
// The only input widget is readonly, expect preserve back, not content
|
|
|
|
$this->assertEquals($preserve['value'], $result['value']);
|
|
|
|
|
|
|
|
$this->readonlys['value'] = false;
|
2017-08-18 11:45:10 +02:00
|
|
|
$result2 = $this->mockedRoundTrip($etemplate, $this->content, $this->sel_options, $this->readonlys, $preserve);
|
2017-04-21 20:17:59 +02:00
|
|
|
|
|
|
|
// The only input widget is editable, expect content back, not preserve
|
2017-08-18 11:45:10 +02:00
|
|
|
$this->assertEquals($this->content['value'], $result2['value']);
|
2017-04-06 19:11:58 +02:00
|
|
|
}
|
|
|
|
}
|