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