]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/PluginManagerTest.php
New plugin hook: ability to add custom filters to Shaarli search engine
[github/shaarli/Shaarli.git] / tests / PluginManagerTest.php
1 <?php
2
3 namespace Shaarli\Plugin;
4
5 use Shaarli\Bookmark\Bookmark;
6 use Shaarli\Config\ConfigManager;
7
8 /**
9 * Unit tests for Plugins
10 */
11 class PluginManagerTest extends \Shaarli\TestCase
12 {
13 /**
14 * Path to tests plugin.
15 * @var string $pluginPath
16 */
17 private static $pluginPath = 'tests/plugins';
18
19 /**
20 * Test plugin.
21 * @var string $pluginName
22 */
23 private static $pluginName = 'test';
24
25 /**
26 * @var PluginManager $pluginManager Plugin Mananger instance.
27 */
28 protected $pluginManager;
29
30 public function setUp(): void
31 {
32 $conf = new ConfigManager('');
33 $this->pluginManager = new PluginManager($conf);
34 }
35
36 /**
37 * Test plugin loading and hook execution.
38 */
39 public function testPlugin(): void
40 {
41 PluginManager::$PLUGINS_PATH = self::$pluginPath;
42 $this->pluginManager->load(array(self::$pluginName));
43
44 $this->assertTrue(function_exists('hook_test_random'));
45
46 $data = [0 => 'woot'];
47 $this->pluginManager->executeHooks('random', $data);
48
49 static::assertCount(2, $data);
50 static::assertSame('woot', $data[1]);
51
52 $data = [0 => 'woot'];
53 $this->pluginManager->executeHooks('random', $data, array('target' => 'test'));
54
55 static::assertCount(2, $data);
56 static::assertSame('page test', $data[1]);
57
58 $data = [0 => 'woot'];
59 $this->pluginManager->executeHooks('random', $data, array('loggedin' => true));
60
61 static::assertCount(2, $data);
62 static::assertEquals('loggedin', $data[1]);
63
64 $data = [0 => 'woot'];
65 $this->pluginManager->executeHooks('random', $data, array('loggedin' => null));
66
67 static::assertCount(3, $data);
68 static::assertEquals('loggedin', $data[1]);
69 static::assertArrayHasKey(2, $data);
70 static::assertNull($data[2]);
71 }
72
73 /**
74 * Test plugin loading and hook execution with an error: raise an incompatibility error.
75 */
76 public function testPluginWithPhpError(): void
77 {
78 PluginManager::$PLUGINS_PATH = self::$pluginPath;
79 $this->pluginManager->load(array(self::$pluginName));
80
81 $this->assertTrue(function_exists('hook_test_error'));
82
83 $data = [];
84 $this->pluginManager->executeHooks('error', $data);
85
86 $this->assertRegExp(
87 '/test \[plugin incompatibility\]: Class [\'"]Unknown[\'"] not found/',
88 $this->pluginManager->getErrors()[0]
89 );
90 }
91
92 /**
93 * Test missing plugin loading.
94 */
95 public function testPluginNotFound(): void
96 {
97 $this->pluginManager->load([]);
98 $this->pluginManager->load(['nope', 'renope']);
99 $this->addToAssertionCount(1);
100 }
101
102 /**
103 * Test plugin metadata loading.
104 */
105 public function testGetPluginsMeta(): void
106 {
107 PluginManager::$PLUGINS_PATH = self::$pluginPath;
108 $this->pluginManager->load([self::$pluginName]);
109
110 $expectedParameters = [
111 'pop' => [
112 'value' => '',
113 'desc' => 'pop description',
114 ],
115 'hip' => [
116 'value' => '',
117 'desc' => '',
118 ],
119 ];
120 $meta = $this->pluginManager->getPluginsMeta();
121 $this->assertEquals('test plugin', $meta[self::$pluginName]['description']);
122 $this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']);
123 }
124
125 /**
126 * Test plugin custom routes - note that there is no check on callable functions
127 */
128 public function testRegisteredRoutes(): void
129 {
130 PluginManager::$PLUGINS_PATH = self::$pluginPath;
131 $this->pluginManager->load([self::$pluginName]);
132
133 $expectedParameters = [
134 [
135 'method' => 'GET',
136 'route' => '/test',
137 'callable' => 'getFunction',
138 ],
139 [
140 'method' => 'POST',
141 'route' => '/custom',
142 'callable' => 'postFunction',
143 ],
144 ];
145 $meta = $this->pluginManager->getRegisteredRoutes();
146 static::assertSame($expectedParameters, $meta[self::$pluginName]);
147 }
148
149 /**
150 * Test plugin custom routes with invalid route
151 */
152 public function testRegisteredRoutesInvalid(): void
153 {
154 $plugin = 'test_route_invalid';
155 $this->pluginManager->load([$plugin]);
156
157 $meta = $this->pluginManager->getRegisteredRoutes();
158 static::assertSame([], $meta);
159
160 $errors = $this->pluginManager->getErrors();
161 static::assertSame(['test_route_invalid [plugin incompatibility]: trying to register invalid route.'], $errors);
162 }
163
164 public function testSearchFilterPlugin(): void
165 {
166 PluginManager::$PLUGINS_PATH = self::$pluginPath;
167 $this->pluginManager->load([self::$pluginName]);
168
169 static::assertNull($this->pluginManager->getFilterSearchEntryHooks());
170
171 static::assertTrue($this->pluginManager->filterSearchEntry(new Bookmark(), ['_result' => true]));
172
173 static::assertCount(1, $this->pluginManager->getFilterSearchEntryHooks());
174 static::assertSame('hook_test_filter_search_entry', $this->pluginManager->getFilterSearchEntryHooks()[0]);
175
176 static::assertFalse($this->pluginManager->filterSearchEntry(new Bookmark(), ['_result' => false]));
177 }
178 }