2015-07-15 11:42:15 +02:00
|
|
|
<?php
|
2018-12-04 00:26:50 +01:00
|
|
|
namespace Shaarli\Plugin;
|
2015-07-15 11:42:15 +02:00
|
|
|
|
2018-12-04 00:26:50 +01:00
|
|
|
use Shaarli\Config\ConfigManager;
|
2015-07-15 11:42:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for Plugins
|
|
|
|
*/
|
2018-12-04 00:26:50 +01:00
|
|
|
class PluginManagerTest extends \PHPUnit\Framework\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;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$conf = new ConfigManager('');
|
|
|
|
$this->pluginManager = new PluginManager($conf);
|
|
|
|
}
|
|
|
|
|
2015-07-15 11:42:15 +02:00
|
|
|
/**
|
|
|
|
* Test plugin loading and hook execution.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testPlugin()
|
|
|
|
{
|
2015-07-16 13:53:39 +02:00
|
|
|
PluginManager::$PLUGINS_PATH = self::$pluginPath;
|
2016-06-09 20:04:32 +02:00
|
|
|
$this->pluginManager->load(array(self::$pluginName));
|
2015-07-15 11:42:15 +02:00
|
|
|
|
|
|
|
$this->assertTrue(function_exists('hook_test_random'));
|
|
|
|
|
|
|
|
$data = array(0 => 'woot');
|
2016-06-09 20:04:32 +02:00
|
|
|
$this->pluginManager->executeHooks('random', $data);
|
2015-07-15 11:42:15 +02:00
|
|
|
$this->assertEquals('woot', $data[1]);
|
|
|
|
|
|
|
|
$data = array(0 => 'woot');
|
2016-06-09 20:04:32 +02:00
|
|
|
$this->pluginManager->executeHooks('random', $data, array('target' => 'test'));
|
2015-07-15 11:42:15 +02:00
|
|
|
$this->assertEquals('page test', $data[1]);
|
|
|
|
|
|
|
|
$data = array(0 => 'woot');
|
2016-06-09 20:04:32 +02:00
|
|
|
$this->pluginManager->executeHooks('random', $data, array('loggedin' => true));
|
2015-07-15 11:42:15 +02:00
|
|
|
$this->assertEquals('loggedin', $data[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test missing plugin loading.
|
|
|
|
*/
|
|
|
|
public function testPluginNotFound()
|
|
|
|
{
|
2016-06-09 20:04:32 +02:00
|
|
|
$this->pluginManager->load(array());
|
|
|
|
$this->pluginManager->load(array('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.
|
|
|
|
*/
|
|
|
|
public function testGetPluginsMeta()
|
|
|
|
{
|
|
|
|
PluginManager::$PLUGINS_PATH = self::$pluginPath;
|
2016-06-09 20:04:32 +02:00
|
|
|
$this->pluginManager->load(array(self::$pluginName));
|
2015-11-18 17:40:42 +01:00
|
|
|
|
|
|
|
$expectedParameters = array(
|
2016-08-02 11:02:20 +02:00
|
|
|
'pop' => array(
|
|
|
|
'value' => '',
|
|
|
|
'desc' => 'pop description',
|
|
|
|
),
|
|
|
|
'hip' => array(
|
|
|
|
'value' => '',
|
|
|
|
'desc' => '',
|
|
|
|
),
|
2015-11-18 17:40:42 +01: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']);
|
|
|
|
}
|
2016-08-13 14:22:22 +02:00
|
|
|
}
|