]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/PluginManagerTest.php
Compatibility with PHP 8
[github/shaarli/Shaarli.git] / tests / PluginManagerTest.php
CommitLineData
6fc14d53 1<?php
e1850388 2namespace Shaarli\Plugin;
6fc14d53 3
e1850388 4use Shaarli\Config\ConfigManager;
6fc14d53
A
5
6/**
7 * Unit tests for Plugins
8 */
a5a9cf23 9class PluginManagerTest extends \Shaarli\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
4ff703e3 44 $data = [0 => 'woot'];
51def0d8 45 $this->pluginManager->executeHooks('random', $data);
6fc14d53 46
4ff703e3
A
47 static::assertCount(2, $data);
48 static::assertSame('woot', $data[1]);
49
50 $data = [0 => 'woot'];
51def0d8 51 $this->pluginManager->executeHooks('random', $data, array('target' => 'test'));
6fc14d53 52
4ff703e3
A
53 static::assertCount(2, $data);
54 static::assertSame('page test', $data[1]);
55
56 $data = [0 => 'woot'];
51def0d8 57 $this->pluginManager->executeHooks('random', $data, array('loggedin' => true));
4ff703e3
A
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]);
6fc14d53
A
69 }
70
7e3dc0ba
A
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
ab58f254
A
84 $this->assertMatchesRegularExpression(
85 '/test \[plugin incompatibility\]: Class [\'"]Unknown[\'"] not found/',
7e3dc0ba
A
86 $this->pluginManager->getErrors()[0]
87 );
88 }
89
6fc14d53
A
90 /**
91 * Test missing plugin loading.
6fc14d53 92 */
7e3dc0ba 93 public function testPluginNotFound(): void
6fc14d53 94 {
4ff703e3
A
95 $this->pluginManager->load([]);
96 $this->pluginManager->load(['nope', 'renope']);
def39d0d 97 $this->addToAssertionCount(1);
6fc14d53 98 }
dea0ba28
A
99
100 /**
101 * Test plugin metadata loading.
102 */
7e3dc0ba 103 public function testGetPluginsMeta(): void
dea0ba28 104 {
dea0ba28 105 PluginManager::$PLUGINS_PATH = self::$pluginPath;
4ff703e3 106 $this->pluginManager->load([self::$pluginName]);
dea0ba28 107
4ff703e3
A
108 $expectedParameters = [
109 'pop' => [
15170b51
A
110 'value' => '',
111 'desc' => 'pop description',
4ff703e3
A
112 ],
113 'hip' => [
15170b51
A
114 'value' => '',
115 'desc' => '',
4ff703e3
A
116 ],
117 ];
51def0d8 118 $meta = $this->pluginManager->getPluginsMeta();
dea0ba28
A
119 $this->assertEquals('test plugin', $meta[self::$pluginName]['description']);
120 $this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']);
121 }
db6dec0d 122}