]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/PluginManagerTest.php
38cd5ebaeeb05db58c435be68c08d1c4408acd9e
[github/shaarli/Shaarli.git] / tests / PluginManagerTest.php
1 <?php
2 namespace Shaarli\Plugin;
3
4 use Shaarli\Config\ConfigManager;
5
6 /**
7 * Unit tests for Plugins
8 */
9 class PluginManagerTest extends \Shaarli\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 = [0 => 'woot'];
45 $this->pluginManager->executeHooks('random', $data);
46
47 static::assertCount(2, $data);
48 static::assertSame('woot', $data[1]);
49
50 $data = [0 => 'woot'];
51 $this->pluginManager->executeHooks('random', $data, array('target' => 'test'));
52
53 static::assertCount(2, $data);
54 static::assertSame('page test', $data[1]);
55
56 $data = [0 => 'woot'];
57 $this->pluginManager->executeHooks('random', $data, array('loggedin' => true));
58
59 static::assertCount(2, $data);
60 static::assertEquals('loggedin', $data[1]);
61
62 $data = [0 => 'woot'];
63 $this->pluginManager->executeHooks('random', $data, array('loggedin' => null));
64
65 static::assertCount(3, $data);
66 static::assertEquals('loggedin', $data[1]);
67 static::assertArrayHasKey(2, $data);
68 static::assertNull($data[2]);
69 }
70
71 /**
72 * Test plugin loading and hook execution with an error: raise an incompatibility error.
73 */
74 public function testPluginWithPhpError(): void
75 {
76 PluginManager::$PLUGINS_PATH = self::$pluginPath;
77 $this->pluginManager->load(array(self::$pluginName));
78
79 $this->assertTrue(function_exists('hook_test_error'));
80
81 $data = [];
82 $this->pluginManager->executeHooks('error', $data);
83
84 $this->assertSame(
85 'test [plugin incompatibility]: Class \'Unknown\' not found',
86 $this->pluginManager->getErrors()[0]
87 );
88 }
89
90 /**
91 * Test missing plugin loading.
92 */
93 public function testPluginNotFound(): void
94 {
95 $this->pluginManager->load([]);
96 $this->pluginManager->load(['nope', 'renope']);
97 $this->addToAssertionCount(1);
98 }
99
100 /**
101 * Test plugin metadata loading.
102 */
103 public function testGetPluginsMeta(): void
104 {
105 PluginManager::$PLUGINS_PATH = self::$pluginPath;
106 $this->pluginManager->load([self::$pluginName]);
107
108 $expectedParameters = [
109 'pop' => [
110 'value' => '',
111 'desc' => 'pop description',
112 ],
113 'hip' => [
114 'value' => '',
115 'desc' => '',
116 ],
117 ];
118 $meta = $this->pluginManager->getPluginsMeta();
119 $this->assertEquals('test plugin', $meta[self::$pluginName]['description']);
120 $this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']);
121 }
122 }