3 namespace Shaarli\Plugin
;
5 use Shaarli\Bookmark\Bookmark
;
6 use Shaarli\Config\ConfigManager
;
9 * Unit tests for Plugins
11 class PluginManagerTest
extends \Shaarli\TestCase
14 * Path to tests plugin.
15 * @var string $pluginPath
17 private static $pluginPath = 'tests/plugins';
21 * @var string $pluginName
23 private static $pluginName = 'test';
26 * @var PluginManager $pluginManager Plugin Mananger instance.
28 protected $pluginManager;
30 public function setUp(): void
32 $conf = new ConfigManager('');
33 $this->pluginManager
= new PluginManager($conf);
37 * Test plugin loading and hook execution.
39 public function testPlugin(): void
41 PluginManager
::$PLUGINS_PATH = self
::$pluginPath;
42 $this->pluginManager
->load(array(self
::$pluginName));
44 $this->assertTrue(function_exists('hook_test_random'));
46 $data = [0 => 'woot'];
47 $this->pluginManager
->executeHooks('random', $data);
49 static::assertCount(2, $data);
50 static::assertSame('woot', $data[1]);
52 $data = [0 => 'woot'];
53 $this->pluginManager
->executeHooks('random', $data, array('target' => 'test'));
55 static::assertCount(2, $data);
56 static::assertSame('page test', $data[1]);
58 $data = [0 => 'woot'];
59 $this->pluginManager
->executeHooks('random', $data, array('loggedin' => true));
61 static::assertCount(2, $data);
62 static::assertEquals('loggedin', $data[1]);
64 $data = [0 => 'woot'];
65 $this->pluginManager
->executeHooks('random', $data, array('loggedin' => null));
67 static::assertCount(3, $data);
68 static::assertEquals('loggedin', $data[1]);
69 static::assertArrayHasKey(2, $data);
70 static::assertNull($data[2]);
74 * Test plugin loading and hook execution with an error: raise an incompatibility error.
76 public function testPluginWithPhpError(): void
78 PluginManager
::$PLUGINS_PATH = self
::$pluginPath;
79 $this->pluginManager
->load(array(self
::$pluginName));
81 $this->assertTrue(function_exists('hook_test_error'));
84 $this->pluginManager
->executeHooks('error', $data);
87 '/test \[plugin incompatibility\]: Class [\'"]Unknown[\'"] not found/',
88 $this->pluginManager
->getErrors()[0]
93 * Test missing plugin loading.
95 public function testPluginNotFound(): void
97 $this->pluginManager
->load([]);
98 $this->pluginManager
->load(['nope', 'renope']);
99 $this->addToAssertionCount(1);
103 * Test plugin metadata loading.
105 public function testGetPluginsMeta(): void
107 PluginManager
::$PLUGINS_PATH = self
::$pluginPath;
108 $this->pluginManager
->load([self
::$pluginName]);
110 $expectedParameters = [
113 'desc' => 'pop description',
120 $meta = $this->pluginManager
->getPluginsMeta();
121 $this->assertEquals('test plugin', $meta[self
::$pluginName]['description']);
122 $this->assertEquals($expectedParameters, $meta[self
::$pluginName]['parameters']);
126 * Test plugin custom routes - note that there is no check on callable functions
128 public function testRegisteredRoutes(): void
130 PluginManager
::$PLUGINS_PATH = self
::$pluginPath;
131 $this->pluginManager
->load([self
::$pluginName]);
133 $expectedParameters = [
137 'callable' => 'getFunction',
141 'route' => '/custom',
142 'callable' => 'postFunction',
145 $meta = $this->pluginManager
->getRegisteredRoutes();
146 static::assertSame($expectedParameters, $meta[self
::$pluginName]);
150 * Test plugin custom routes with invalid route
152 public function testRegisteredRoutesInvalid(): void
154 $plugin = 'test_route_invalid';
155 $this->pluginManager
->load([$plugin]);
157 $meta = $this->pluginManager
->getRegisteredRoutes();
158 static::assertSame([], $meta);
160 $errors = $this->pluginManager
->getErrors();
161 static::assertSame(['test_route_invalid [plugin incompatibility]: trying to register invalid route.'], $errors);
164 public function testSearchFilterPlugin(): void
166 PluginManager
::$PLUGINS_PATH = self
::$pluginPath;
167 $this->pluginManager
->load([self
::$pluginName]);
169 static::assertNull($this->pluginManager
->getFilterSearchEntryHooks());
171 static::assertTrue($this->pluginManager
->filterSearchEntry(new Bookmark(), ['_result' => true]));
173 static::assertCount(1, $this->pluginManager
->getFilterSearchEntryHooks());
174 static::assertSame('hook_test_filter_search_entry', $this->pluginManager
->getFilterSearchEntryHooks()[0]);
176 static::assertFalse($this->pluginManager
->filterSearchEntry(new Bookmark(), ['_result' => false]));