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