From 944718daaac5e158b106a41d049eeeaddff50f92 Mon Sep 17 00:00:00 2001 From: nathangray Date: Thu, 13 Apr 2017 12:21:41 -0600 Subject: [PATCH] Some more tests of basic Etemplate stuff --- .../Etemplate/Widget/test/TemplateTest.php | 4 +- api/src/Etemplate/test/WidgetBaseTest.php | 60 ++++++++++- api/src/Etemplate/test/WidgetTest.php | 47 +++++++- api/src/test/EtemplateTest.php | 100 +++++++++++++----- api/templates/default/prompt.xet | 2 +- 5 files changed, 177 insertions(+), 36 deletions(-) diff --git a/api/src/Etemplate/Widget/test/TemplateTest.php b/api/src/Etemplate/Widget/test/TemplateTest.php index 2135674513..c147f4ded0 100644 --- a/api/src/Etemplate/Widget/test/TemplateTest.php +++ b/api/src/Etemplate/Widget/test/TemplateTest.php @@ -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 diff --git a/api/src/Etemplate/test/WidgetBaseTest.php b/api/src/Etemplate/test/WidgetBaseTest.php index 8d21fe4591..3da03e1a93 100644 --- a/api/src/Etemplate/test/WidgetBaseTest.php +++ b/api/src/Etemplate/test/WidgetBaseTest.php @@ -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; + } } diff --git a/api/src/Etemplate/test/WidgetTest.php b/api/src/Etemplate/test/WidgetTest.php index 792fa4304e..f0f4eb5028 100644 --- a/api/src/Etemplate/test/WidgetTest.php +++ b/api/src/Etemplate/test/WidgetTest.php @@ -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 = ""; + $xml = ''; $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 = 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'); + } } diff --git a/api/src/test/EtemplateTest.php b/api/src/test/EtemplateTest.php index e76a3bcce5..52f3927c49 100644 --- a/api/src/test/EtemplateTest.php +++ b/api/src/test/EtemplateTest.php @@ -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.' - ); - } - - public function testArrayMerge() - { - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $result = $this->mockedExec($etemplate, '',array()); + + // Check for the load + foreach($result as $command) + { + 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'); } } diff --git a/api/templates/default/prompt.xet b/api/templates/default/prompt.xet index 04f05fa0ff..b717d2ca14 100644 --- a/api/templates/default/prompt.xet +++ b/api/templates/default/prompt.xet @@ -2,7 +2,7 @@ -