2017-04-06 19:11:58 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base test file for all widget tests
|
|
|
|
*
|
|
|
|
* @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\Etemplate;
|
|
|
|
|
2017-04-21 20:17:59 +02:00
|
|
|
use Egroupware\Api\Etemplate;
|
|
|
|
|
2017-04-06 19:11:58 +02:00
|
|
|
// test base providing Egw environment, since we need the DB
|
|
|
|
require_once realpath(__DIR__.'/../../test/LoggedInTest.php');
|
|
|
|
|
2017-04-21 20:17:59 +02:00
|
|
|
// Store request in the session, file access probably won't work due to permissions
|
|
|
|
\EGroupware\Api\Etemplate\Request::$request_class = 'EGroupware\Api\Etemplate\Request\Session';
|
|
|
|
|
2017-04-06 19:11:58 +02:00
|
|
|
/**
|
2017-04-13 20:21:41 +02:00
|
|
|
* Base class for all widget tests doing needed setup so the tests can run, and
|
|
|
|
* providing common utilities.
|
2017-04-06 19:11:58 +02:00
|
|
|
*
|
|
|
|
* Widget scans the apps for widgets, which needs the app list, pulled from the
|
|
|
|
* database, so we need to log in.
|
|
|
|
*/
|
2017-04-13 20:21:41 +02:00
|
|
|
abstract class WidgetBaseTest extends \EGroupware\Api\LoggedInTest {
|
|
|
|
|
2017-04-21 20:17:59 +02:00
|
|
|
/**
|
|
|
|
* We use our own callback for results of executing, here we store the results
|
|
|
|
* until returned
|
|
|
|
*
|
|
|
|
* @var Array
|
|
|
|
*/
|
|
|
|
protected static $mocked_exec_result = array();
|
|
|
|
|
|
|
|
protected $ajax_response = null;
|
|
|
|
|
2017-04-13 20:21:41 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-04-21 20:17:59 +02:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
// Mock AJAX response
|
|
|
|
$this->ajax_response = $this->mock_ajax_response();
|
|
|
|
}
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
// Clean up AJAX response
|
|
|
|
$this->ajax_response->initResponseArray();
|
|
|
|
$ref = new \ReflectionProperty('\\EGroupware\\Api\\Json\\Response', 'response');
|
|
|
|
$ref->setAccessible(true);
|
|
|
|
$ref->setValue(null, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use a known, public callback so we can hook into it, if needed
|
|
|
|
*/
|
|
|
|
public static function public_callback($content)
|
|
|
|
{
|
|
|
|
static::$mocked_exec_result = $content;
|
|
|
|
}
|
|
|
|
|
2017-04-13 20:21:41 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2017-04-21 20:17:59 +02:00
|
|
|
* @param Etemplate $etemplate
|
2017-04-13 20:21:41 +02:00
|
|
|
* @param array $content
|
|
|
|
* @param array $sel_options
|
|
|
|
* @param array $readonlys
|
|
|
|
* @param array $preserv
|
|
|
|
*/
|
2017-04-21 20:17:59 +02:00
|
|
|
protected function mockedExec(Etemplate $etemplate, array $content,array $sel_options=null,array $readonlys=null,array $preserv=null)
|
|
|
|
{
|
|
|
|
ob_start();
|
|
|
|
|
|
|
|
// Exec the template
|
|
|
|
$etemplate->exec(__CLASS__ . '::public_callback', $content, $sel_options, $readonlys, $preserv, 4);
|
|
|
|
$result = $this->ajax_response->returnResult();
|
|
|
|
|
|
|
|
// Store & clean the request
|
|
|
|
//$etemplate->destroy_request();
|
|
|
|
|
|
|
|
ob_end_clean();
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function mockedRoundTrip(\EGroupware\Api\Etemplate $etemplate, array $content,array $sel_options=null,array $readonlys=null,array $preserv=null)
|
|
|
|
{
|
|
|
|
|
|
|
|
$result = $this->mockedExec($etemplate, $content, $sel_options, $readonlys, $preserv);
|
|
|
|
|
|
|
|
// Check for the load
|
|
|
|
$data = array();
|
|
|
|
foreach($result as $command)
|
|
|
|
{
|
|
|
|
if($command['type'] == 'et2_load')
|
|
|
|
{
|
|
|
|
$data = $command['data'];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Etemplate::ajax_process_content($data['data']['etemplate_exec_id'], $data['data']['content'], false);
|
|
|
|
|
|
|
|
$content = static::$mocked_exec_result;
|
|
|
|
static::$mocked_exec_result = array();
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock the ajax response and override it so it doesn't try to send headers,
|
|
|
|
* which generates errors since PHPUnit has already done that
|
|
|
|
*/
|
|
|
|
protected function mock_ajax_response()
|
2017-04-13 20:21:41 +02:00
|
|
|
{
|
|
|
|
$response = $this->getMockBuilder('\\EGroupware\\Api\\Json\\Response')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->setMethods(['get'/*,'generic'*/])
|
2017-04-21 20:17:59 +02:00
|
|
|
->getMock();
|
2017-04-13 20:21:41 +02:00
|
|
|
// 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;
|
|
|
|
});
|
2017-04-21 20:17:59 +02:00
|
|
|
return $response;
|
2017-04-13 20:21:41 +02:00
|
|
|
}
|
2017-04-06 19:11:58 +02:00
|
|
|
}
|