]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | namespace Shaarli\Plugin; | |
3 | ||
4 | use Shaarli\Config\ConfigManager; | |
5 | ||
6 | /** | |
7 | * Unit tests for Plugins | |
8 | */ | |
9 | class PluginManagerTest extends \PHPUnit\Framework\TestCase | |
10 | { | |
11 | /** | |
12 | * Path to tests plugin. | |
13 | * @var string $pluginPath | |
14 | */ | |
15 | private static $pluginPath = 'tests/plugins'; | |
16 | ||
17 | /** | |
18 | * Test plugin. | |
19 | * @var string $pluginName | |
20 | */ | |
21 | private static $pluginName = 'test'; | |
22 | ||
23 | /** | |
24 | * @var PluginManager $pluginManager Plugin Mananger instance. | |
25 | */ | |
26 | protected $pluginManager; | |
27 | ||
28 | public function setUp(): void | |
29 | { | |
30 | $conf = new ConfigManager(''); | |
31 | $this->pluginManager = new PluginManager($conf); | |
32 | } | |
33 | ||
34 | /** | |
35 | * Test plugin loading and hook execution. | |
36 | */ | |
37 | public function testPlugin(): void | |
38 | { | |
39 | PluginManager::$PLUGINS_PATH = self::$pluginPath; | |
40 | $this->pluginManager->load(array(self::$pluginName)); | |
41 | ||
42 | $this->assertTrue(function_exists('hook_test_random')); | |
43 | ||
44 | $data = array(0 => 'woot'); | |
45 | $this->pluginManager->executeHooks('random', $data); | |
46 | $this->assertEquals('woot', $data[1]); | |
47 | ||
48 | $data = array(0 => 'woot'); | |
49 | $this->pluginManager->executeHooks('random', $data, array('target' => 'test')); | |
50 | $this->assertEquals('page test', $data[1]); | |
51 | ||
52 | $data = array(0 => 'woot'); | |
53 | $this->pluginManager->executeHooks('random', $data, array('loggedin' => true)); | |
54 | $this->assertEquals('loggedin', $data[1]); | |
55 | } | |
56 | ||
57 | /** | |
58 | * Test plugin loading and hook execution with an error: raise an incompatibility error. | |
59 | */ | |
60 | public function testPluginWithPhpError(): void | |
61 | { | |
62 | PluginManager::$PLUGINS_PATH = self::$pluginPath; | |
63 | $this->pluginManager->load(array(self::$pluginName)); | |
64 | ||
65 | $this->assertTrue(function_exists('hook_test_error')); | |
66 | ||
67 | $data = []; | |
68 | $this->pluginManager->executeHooks('error', $data); | |
69 | ||
70 | $this->assertSame( | |
71 | 'test [plugin incompatibility]: Class \'Unknown\' not found', | |
72 | $this->pluginManager->getErrors()[0] | |
73 | ); | |
74 | } | |
75 | ||
76 | /** | |
77 | * Test missing plugin loading. | |
78 | */ | |
79 | public function testPluginNotFound(): void | |
80 | { | |
81 | $this->pluginManager->load(array()); | |
82 | $this->pluginManager->load(array('nope', 'renope')); | |
83 | $this->addToAssertionCount(1); | |
84 | } | |
85 | ||
86 | /** | |
87 | * Test plugin metadata loading. | |
88 | */ | |
89 | public function testGetPluginsMeta(): void | |
90 | { | |
91 | PluginManager::$PLUGINS_PATH = self::$pluginPath; | |
92 | $this->pluginManager->load(array(self::$pluginName)); | |
93 | ||
94 | $expectedParameters = array( | |
95 | 'pop' => array( | |
96 | 'value' => '', | |
97 | 'desc' => 'pop description', | |
98 | ), | |
99 | 'hip' => array( | |
100 | 'value' => '', | |
101 | 'desc' => '', | |
102 | ), | |
103 | ); | |
104 | $meta = $this->pluginManager->getPluginsMeta(); | |
105 | $this->assertEquals('test plugin', $meta[self::$pluginName]['description']); | |
106 | $this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']); | |
107 | } | |
108 | } |