Skip to content

Commit

Permalink
Fixed remote plugin tests
Browse files Browse the repository at this point in the history
Mocking the plugin controller doesn't work like before anymore, because
of the singleton mechanim.
  • Loading branch information
splitbrain committed Jul 21, 2018
1 parent 628d182 commit dc0f84f
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 86 deletions.
109 changes: 23 additions & 86 deletions _test/tests/inc/remote.test.php
Expand Up @@ -79,62 +79,13 @@ function publicCall() {return true;}

}

class remote_plugin_testplugin extends RemotePlugin {
function _getMethods() {
return array(
'method1' => array(
'args' => array(),
'return' => 'void'
), 'methodString' => array(
'args' => array(),
'return' => 'string'
), 'method2' => array(
'args' => array('string', 'int'),
'return' => 'array',
'name' => 'method2',
), 'method2ext' => array(
'args' => array('string', 'int', 'bool'),
'return' => 'array',
'name' => 'method2',
), 'publicCall' => array(
'args' => array(),
'return' => 'boolean',
'doc' => 'testing for public access',
'name' => 'publicCall',
'public' => 1
)
);
}

function method1() { return null; }
function methodString() { return 'success'; }
function method2($str, $int, $bool = false) { return array($str, $int, $bool); }
function publicCall() {return true;}
}

class remote_plugin_testplugin2 extends RemotePlugin {
/**
* This is a dummy method
*
* @param string $str some more parameter description
* @param int $int
* @param bool $bool
* @param Object $unknown
* @return array
*/
public function commented($str, $int, $bool, $unknown) { return array($str, $int, $bool); }

private function privateMethod() {return true;}
protected function protectedMethod() {return true;}
public function _underscore() {return true;}
}



class remote_test extends DokuWikiTest {

protected $userinfo;

protected $pluginsEnabled = ['testing'];

/** @var Api */
protected $remote;

Expand All @@ -147,20 +98,6 @@ function setUp() {

parent::setUp();

// mock plugin controller to return our test plugins
$pluginManager = $this->createMock('dokuwiki\Extension\PluginController');
$pluginManager->method('getList')->willReturn(array('testplugin', 'testplugin2'));
$pluginManager->method('load')->willReturnCallback(
function($type, $plugin) {
if($plugin == 'testplugin2') {
return new remote_plugin_testplugin2();
} else {
return new remote_plugin_testplugin();
}
}
);
$plugin_controller = $pluginManager;

$conf['remote'] = 1;
$conf['remoteuser'] = '!!not set!!';
$conf['useacl'] = 0;
Expand All @@ -180,27 +117,27 @@ function tearDown() {
function test_pluginMethods() {
$methods = $this->remote->getPluginMethods();
$actual = array_keys($methods);
sort($actual);
$expect = array(
'plugin.testplugin.method1',
'plugin.testplugin.method2',
'plugin.testplugin.methodString',
'plugin.testplugin.method2ext',
'plugin.testplugin.publicCall',
'plugin.testing_manual.method1',
'plugin.testing_manual.method2',
'plugin.testing_manual.methodString',
'plugin.testing_manual.method2ext',
'plugin.testing_manual.publicCall',

'plugin.testplugin2.commented'
'plugin.testing_auto.commented'
);
sort($expect);
$this->assertEquals($expect,$actual);
foreach($expect as $e) {
$this->assertContains($e, $actual);
}
}

function test_pluginDescriptors() {
$methods = $this->remote->getPluginMethods();
$this->assertEquals(array('string','int','bool','string'), $methods['plugin.testplugin2.commented']['args']);
$this->assertEquals('array', $methods['plugin.testplugin2.commented']['return']);
$this->assertEquals(0, $methods['plugin.testplugin2.commented']['public']);
$this->assertContains('This is a dummy method', $methods['plugin.testplugin2.commented']['doc']);
$this->assertContains('string $str some more parameter description', $methods['plugin.testplugin2.commented']['doc']);
$this->assertEquals(array('string','int','bool','string'), $methods['plugin.testing_auto.commented']['args']);
$this->assertEquals('array', $methods['plugin.testing_auto.commented']['return']);
$this->assertEquals(0, $methods['plugin.testing_auto.commented']['public']);
$this->assertContains('This is a dummy method', $methods['plugin.testing_auto.commented']['doc']);
$this->assertContains('string $str some more parameter description', $methods['plugin.testing_auto.commented']['doc']);
}

function test_hasAccessSuccess() {
Expand Down Expand Up @@ -326,10 +263,10 @@ function test_pluginCallMethods() {
$conf['useacl'] = 1;

$remoteApi = new Api();
$this->assertEquals($remoteApi->call('plugin.testplugin.method1'), null);
$this->assertEquals($remoteApi->call('plugin.testplugin.method2', array('string', 7)), array('string', 7, false));
$this->assertEquals($remoteApi->call('plugin.testplugin.method2ext', array('string', 7, true)), array('string', 7, true));
$this->assertEquals($remoteApi->call('plugin.testplugin.methodString'), 'success');
$this->assertEquals($remoteApi->call('plugin.testing_manual.method1'), null);
$this->assertEquals($remoteApi->call('plugin.testing_manual.method2', array('string', 7)), array('string', 7, false));
$this->assertEquals($remoteApi->call('plugin.testing_manual.method2ext', array('string', 7, true)), array('string', 7, true));
$this->assertEquals($remoteApi->call('plugin.testing_manual.methodString'), 'success');
}

/**
Expand All @@ -355,7 +292,7 @@ function test_publicCallPlugin() {
global $conf;
$conf['useacl'] = 1;
$remoteApi = new Api();
$this->assertTrue($remoteApi->call('plugin.testplugin.publicCall'));
$this->assertTrue($remoteApi->call('plugin.testing_manual.publicCall'));
}

/**
Expand All @@ -376,7 +313,7 @@ function test_publicCallPluginDeny() {
global $conf;
$conf['useacl'] = 1;
$remoteApi = new Api();
$remoteApi->call('plugin.testplugin.methodString');
$remoteApi->call('plugin.testing_manual.methodString');
}

function test_pluginCallCustomPath() {
Expand All @@ -394,6 +331,6 @@ function test_pluginCallCustomPath() {
}

function pluginCallCustomPathRegister(&$event, $param) {
$event->data['custom.path'] = array('testplugin', 'methodString');
$event->data['custom.path'] = array('testing_manual', 'methodString');
}
}
52 changes: 52 additions & 0 deletions lib/plugins/testing/remote/auto.php
@@ -0,0 +1,52 @@
<?php

use dokuwiki\Extension\RemotePlugin;

/**
* For testing automatically extracted method descriptions
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
class remote_plugin_testing_auto extends RemotePlugin
{
/**
* This is a dummy method
*
* @param string $str some more parameter description
* @param int $int
* @param bool $bool
* @param Object $unknown
* @return array
*/
public function commented($str, $int, $bool, $unknown)
{
return array($str, $int, $bool);
}

/**
* This should not be accessible via API
* @return bool
*/
private function privateMethod()
{
return true;
}

/**
* This should not be accessible via API
* @return bool
*/
protected function protectedMethod()
{
return true;
}

/**
* This should not be accessible via API
* @return bool
*/
public function _underscore()
{
return true;
}
}
67 changes: 67 additions & 0 deletions lib/plugins/testing/remote/manual.php
@@ -0,0 +1,67 @@
<?php

use dokuwiki\Extension\RemotePlugin;

/**
* For testing manual method descriptions
*
* @author Dominik Eckelmann <deckelmann@gmail.com>
*/
class remote_plugin_testing_manual extends RemotePlugin
{
/** @inheritdoc */
function _getMethods()
{
return array(
'method1' => array(
'args' => array(),
'return' => 'void',
),
'methodString' => array(
'args' => array(),
'return' => 'string',
),
'method2' => array(
'args' => array('string', 'int'),
'return' => 'array',
'name' => 'method2',
),
'method2ext' => array(
'args' => array('string', 'int', 'bool'),
'return' => 'array',
'name' => 'method2',
),
'publicCall' => array(
'args' => array(),
'return' => 'boolean',
'doc' => 'testing for public access',
'name' => 'publicCall',
'public' => 1,
),
);
}

/** @inheritdoc */
function method1()
{
return null;
}

/** @inheritdoc */
function methodString()
{
return 'success';
}

/** @inheritdoc */
function method2($str, $int, $bool = false)
{
return array($str, $int, $bool);
}

/** @inheritdoc */
function publicCall()
{
return true;
}
}

0 comments on commit dc0f84f

Please sign in to comment.