2015-07-15 11:42:15 +02:00
|
|
|
<?php
|
2020-09-29 18:49:02 +02:00
|
|
|
|
2018-12-04 00:26:50 +01:00
|
|
|
namespace Shaarli\Plugin;
|
2015-07-15 11:42:15 +02:00
|
|
|
|
2021-01-20 15:59:00 +01:00
|
|
|
use Shaarli\Bookmark\Bookmark;
|
2018-12-04 00:26:50 +01:00
|
|
|
use Shaarli\Config\ConfigManager;
|
2015-07-15 11:42:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for Plugins
|
|
|
|
*/
|
2020-09-29 14:41:40 +02:00
|
|
|
class PluginManagerTest extends \Shaarli\TestCase
|
2015-07-15 11:42:15 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Path to tests plugin.
|
2015-07-16 13:53:39 +02:00
|
|
|
* @var string $pluginPath
|
2015-07-15 11:42:15 +02:00
|
|
|
*/
|
2015-07-16 13:53:39 +02:00
|
|
|
private static $pluginPath = 'tests/plugins';
|
2015-07-15 11:42:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test plugin.
|
2015-07-16 13:53:39 +02:00
|
|
|
* @var string $pluginName
|
2015-07-15 11:42:15 +02:00
|
|
|
*/
|
2015-07-16 13:53:39 +02:00
|
|
|
private static $pluginName = 'test';
|
2015-07-15 11:42:15 +02:00
|
|
|
|
2016-06-09 20:04:32 +02:00
|
|
|
/**
|
|
|
|
* @var PluginManager $pluginManager Plugin Mananger instance.
|
|
|
|
*/
|
|
|
|
protected $pluginManager;
|
|
|
|
|
2020-08-27 12:04:36 +02:00
|
|
|
public function setUp(): void
|
2016-06-09 20:04:32 +02:00
|
|
|
{
|
|
|
|
$conf = new ConfigManager('');
|
|
|
|
$this->pluginManager = new PluginManager($conf);
|
|
|
|
}
|
|
|
|
|
2015-07-15 11:42:15 +02:00
|
|
|
/**
|
|
|
|
* Test plugin loading and hook execution.
|
|
|
|
*/
|
2020-08-27 12:04:36 +02:00
|
|
|
public function testPlugin(): void
|
2015-07-15 11:42:15 +02:00
|
|
|
{
|
2015-07-16 13:53:39 +02:00
|
|
|
PluginManager::$PLUGINS_PATH = self::$pluginPath;
|
2021-04-05 09:39:34 +02:00
|
|
|
$this->pluginManager->load([self::$pluginName]);
|
2015-07-15 11:42:15 +02:00
|
|
|
|
|
|
|
$this->assertTrue(function_exists('hook_test_random'));
|
|
|
|
|
2020-09-10 14:08:19 +02:00
|
|
|
$data = [0 => 'woot'];
|
2016-06-09 20:04:32 +02:00
|
|
|
$this->pluginManager->executeHooks('random', $data);
|
2015-07-15 11:42:15 +02:00
|
|
|
|
2020-09-10 14:08:19 +02:00
|
|
|
static::assertCount(2, $data);
|
|
|
|
static::assertSame('woot', $data[1]);
|
|
|
|
|
|
|
|
$data = [0 => 'woot'];
|
2021-04-05 09:39:34 +02:00
|
|
|
$this->pluginManager->executeHooks('random', $data, ['target' => 'test']);
|
2015-07-15 11:42:15 +02:00
|
|
|
|
2020-09-10 14:08:19 +02:00
|
|
|
static::assertCount(2, $data);
|
|
|
|
static::assertSame('page test', $data[1]);
|
|
|
|
|
|
|
|
$data = [0 => 'woot'];
|
2021-04-05 09:39:34 +02:00
|
|
|
$this->pluginManager->executeHooks('random', $data, ['loggedin' => true]);
|
2020-09-10 14:08:19 +02:00
|
|
|
|
|
|
|
static::assertCount(2, $data);
|
|
|
|
static::assertEquals('loggedin', $data[1]);
|
|
|
|
|
|
|
|
$data = [0 => 'woot'];
|
2021-04-05 09:39:34 +02:00
|
|
|
$this->pluginManager->executeHooks('random', $data, ['loggedin' => null]);
|
2020-09-10 14:08:19 +02:00
|
|
|
|
|
|
|
static::assertCount(3, $data);
|
|
|
|
static::assertEquals('loggedin', $data[1]);
|
|
|
|
static::assertArrayHasKey(2, $data);
|
|
|
|
static::assertNull($data[2]);
|
2015-07-15 11:42:15 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 12:04:36 +02:00
|
|
|
/**
|
|
|
|
* Test plugin loading and hook execution with an error: raise an incompatibility error.
|
|
|
|
*/
|
|
|
|
public function testPluginWithPhpError(): void
|
|
|
|
{
|
|
|
|
PluginManager::$PLUGINS_PATH = self::$pluginPath;
|
2021-04-05 09:39:34 +02:00
|
|
|
$this->pluginManager->load([self::$pluginName]);
|
2020-08-27 12:04:36 +02:00
|
|
|
|
|
|
|
$this->assertTrue(function_exists('hook_test_error'));
|
|
|
|
|
|
|
|
$data = [];
|
|
|
|
$this->pluginManager->executeHooks('error', $data);
|
|
|
|
|
2020-09-29 18:49:02 +02:00
|
|
|
$this->assertRegExp(
|
2020-09-29 15:00:11 +02:00
|
|
|
'/test \[plugin incompatibility\]: Class [\'"]Unknown[\'"] not found/',
|
2020-08-27 12:04:36 +02:00
|
|
|
$this->pluginManager->getErrors()[0]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-07-15 11:42:15 +02:00
|
|
|
/**
|
|
|
|
* Test missing plugin loading.
|
|
|
|
*/
|
2020-08-27 12:04:36 +02:00
|
|
|
public function testPluginNotFound(): void
|
2015-07-15 11:42:15 +02:00
|
|
|
{
|
2020-09-10 14:08:19 +02:00
|
|
|
$this->pluginManager->load([]);
|
|
|
|
$this->pluginManager->load(['nope', 'renope']);
|
2019-08-10 12:31:32 +02:00
|
|
|
$this->addToAssertionCount(1);
|
2015-07-15 11:42:15 +02:00
|
|
|
}
|
2015-11-18 17:40:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test plugin metadata loading.
|
|
|
|
*/
|
2020-08-27 12:04:36 +02:00
|
|
|
public function testGetPluginsMeta(): void
|
2015-11-18 17:40:42 +01:00
|
|
|
{
|
|
|
|
PluginManager::$PLUGINS_PATH = self::$pluginPath;
|
2020-09-10 14:08:19 +02:00
|
|
|
$this->pluginManager->load([self::$pluginName]);
|
2015-11-18 17:40:42 +01:00
|
|
|
|
2020-09-10 14:08:19 +02:00
|
|
|
$expectedParameters = [
|
|
|
|
'pop' => [
|
2016-08-02 11:02:20 +02:00
|
|
|
'value' => '',
|
|
|
|
'desc' => 'pop description',
|
2020-09-10 14:08:19 +02:00
|
|
|
],
|
|
|
|
'hip' => [
|
2016-08-02 11:02:20 +02:00
|
|
|
'value' => '',
|
|
|
|
'desc' => '',
|
2020-09-10 14:08:19 +02:00
|
|
|
],
|
|
|
|
];
|
2016-06-09 20:04:32 +02:00
|
|
|
$meta = $this->pluginManager->getPluginsMeta();
|
2015-11-18 17:40:42 +01:00
|
|
|
$this->assertEquals('test plugin', $meta[self::$pluginName]['description']);
|
|
|
|
$this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']);
|
|
|
|
}
|
2020-10-27 19:23:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test plugin custom routes - note that there is no check on callable functions
|
|
|
|
*/
|
|
|
|
public function testRegisteredRoutes(): void
|
|
|
|
{
|
|
|
|
PluginManager::$PLUGINS_PATH = self::$pluginPath;
|
|
|
|
$this->pluginManager->load([self::$pluginName]);
|
|
|
|
|
|
|
|
$expectedParameters = [
|
|
|
|
[
|
|
|
|
'method' => 'GET',
|
|
|
|
'route' => '/test',
|
|
|
|
'callable' => 'getFunction',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'method' => 'POST',
|
|
|
|
'route' => '/custom',
|
|
|
|
'callable' => 'postFunction',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
$meta = $this->pluginManager->getRegisteredRoutes();
|
|
|
|
static::assertSame($expectedParameters, $meta[self::$pluginName]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test plugin custom routes with invalid route
|
|
|
|
*/
|
|
|
|
public function testRegisteredRoutesInvalid(): void
|
|
|
|
{
|
|
|
|
$plugin = 'test_route_invalid';
|
|
|
|
$this->pluginManager->load([$plugin]);
|
|
|
|
|
|
|
|
$meta = $this->pluginManager->getRegisteredRoutes();
|
|
|
|
static::assertSame([], $meta);
|
|
|
|
|
|
|
|
$errors = $this->pluginManager->getErrors();
|
|
|
|
static::assertSame(['test_route_invalid [plugin incompatibility]: trying to register invalid route.'], $errors);
|
|
|
|
}
|
2021-01-20 15:59:00 +01:00
|
|
|
|
|
|
|
public function testSearchFilterPlugin(): void
|
|
|
|
{
|
|
|
|
PluginManager::$PLUGINS_PATH = self::$pluginPath;
|
|
|
|
$this->pluginManager->load([self::$pluginName]);
|
|
|
|
|
|
|
|
static::assertNull($this->pluginManager->getFilterSearchEntryHooks());
|
|
|
|
|
|
|
|
static::assertTrue($this->pluginManager->filterSearchEntry(new Bookmark(), ['_result' => true]));
|
|
|
|
|
|
|
|
static::assertCount(1, $this->pluginManager->getFilterSearchEntryHooks());
|
|
|
|
static::assertSame('hook_test_filter_search_entry', $this->pluginManager->getFilterSearchEntryHooks()[0]);
|
|
|
|
|
|
|
|
static::assertFalse($this->pluginManager->filterSearchEntry(new Bookmark(), ['_result' => false]));
|
|
|
|
}
|
2016-08-13 14:22:22 +02:00
|
|
|
}
|