]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/PluginManagerTest.php
Merge pull request #1698 from ArthurHoaro/feature/plugins-search-filter
[github/shaarli/Shaarli.git] / tests / PluginManagerTest.php
CommitLineData
6fc14d53 1<?php
d246e2c5 2
e1850388 3namespace Shaarli\Plugin;
6fc14d53 4
bcba6bd3 5use Shaarli\Bookmark\Bookmark;
e1850388 6use Shaarli\Config\ConfigManager;
6fc14d53
A
7
8/**
9 * Unit tests for Plugins
10 */
a5a9cf23 11class PluginManagerTest extends \Shaarli\TestCase
6fc14d53
A
12{
13 /**
14 * Path to tests plugin.
d06265fb 15 * @var string $pluginPath
6fc14d53 16 */
d06265fb 17 private static $pluginPath = 'tests/plugins';
6fc14d53
A
18
19 /**
20 * Test plugin.
d06265fb 21 * @var string $pluginName
6fc14d53 22 */
d06265fb 23 private static $pluginName = 'test';
6fc14d53 24
51def0d8
A
25 /**
26 * @var PluginManager $pluginManager Plugin Mananger instance.
27 */
28 protected $pluginManager;
29
7e3dc0ba 30 public function setUp(): void
51def0d8
A
31 {
32 $conf = new ConfigManager('');
33 $this->pluginManager = new PluginManager($conf);
34 }
35
6fc14d53
A
36 /**
37 * Test plugin loading and hook execution.
6fc14d53 38 */
7e3dc0ba 39 public function testPlugin(): void
6fc14d53 40 {
d06265fb 41 PluginManager::$PLUGINS_PATH = self::$pluginPath;
51def0d8 42 $this->pluginManager->load(array(self::$pluginName));
6fc14d53
A
43
44 $this->assertTrue(function_exists('hook_test_random'));
45
4ff703e3 46 $data = [0 => 'woot'];
51def0d8 47 $this->pluginManager->executeHooks('random', $data);
6fc14d53 48
4ff703e3
A
49 static::assertCount(2, $data);
50 static::assertSame('woot', $data[1]);
51
52 $data = [0 => 'woot'];
51def0d8 53 $this->pluginManager->executeHooks('random', $data, array('target' => 'test'));
6fc14d53 54
4ff703e3
A
55 static::assertCount(2, $data);
56 static::assertSame('page test', $data[1]);
57
58 $data = [0 => 'woot'];
51def0d8 59 $this->pluginManager->executeHooks('random', $data, array('loggedin' => true));
4ff703e3
A
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]);
6fc14d53
A
71 }
72
7e3dc0ba
A
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
d246e2c5 86 $this->assertRegExp(
ab58f254 87 '/test \[plugin incompatibility\]: Class [\'"]Unknown[\'"] not found/',
7e3dc0ba
A
88 $this->pluginManager->getErrors()[0]
89 );
90 }
91
6fc14d53
A
92 /**
93 * Test missing plugin loading.
6fc14d53 94 */
7e3dc0ba 95 public function testPluginNotFound(): void
6fc14d53 96 {
4ff703e3
A
97 $this->pluginManager->load([]);
98 $this->pluginManager->load(['nope', 'renope']);
def39d0d 99 $this->addToAssertionCount(1);
6fc14d53 100 }
dea0ba28
A
101
102 /**
103 * Test plugin metadata loading.
104 */
7e3dc0ba 105 public function testGetPluginsMeta(): void
dea0ba28 106 {
dea0ba28 107 PluginManager::$PLUGINS_PATH = self::$pluginPath;
4ff703e3 108 $this->pluginManager->load([self::$pluginName]);
dea0ba28 109
4ff703e3
A
110 $expectedParameters = [
111 'pop' => [
15170b51
A
112 'value' => '',
113 'desc' => 'pop description',
4ff703e3
A
114 ],
115 'hip' => [
15170b51
A
116 'value' => '',
117 'desc' => '',
4ff703e3
A
118 ],
119 ];
51def0d8 120 $meta = $this->pluginManager->getPluginsMeta();
dea0ba28
A
121 $this->assertEquals('test plugin', $meta[self::$pluginName]['description']);
122 $this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']);
123 }
a6e9c084
A
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 }
bcba6bd3
A
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 }
db6dec0d 178}