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