1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Integration\Plugin;
use Piwik\Container\StaticContainer;
use Piwik\Db;
use Piwik\Http\ControllerResolver;
use Piwik\Plugin;
use Piwik\Settings\Storage;
use Piwik\Cache as PiwikCache;
use Piwik\Tests\Integration\Settings\IntegrationTestCase;
use Piwik\Widget\WidgetsList;
/**
* @group Plugin
* @group PluginManager
*/
class ManagerTest extends IntegrationTestCase
{
private $trackerCacheId = 'PluginsTracker';
/**
* @var Plugin\Manager
*/
private $manager;
public function setUp()
{
parent::setUp();
$this->manager = Plugin\Manager::getInstance();
}
public function test_loadTrackerPlugins_shouldDetectTrackerPlugins()
{
$this->assertGreaterThan(50, count($this->manager->getLoadedPlugins())); // make sure all plugins are loaded
$pluginsToLoad = $this->manager->loadTrackerPlugins();
$this->assertOnlyTrackerPluginsAreLoaded($pluginsToLoad);
}
public function test_loadTrackerPlugins_shouldCacheListOfPlugins()
{
$cache = $this->getCacheForTrackerPlugins();
$this->assertFalse($cache->contains($this->trackerCacheId));
$pluginsToLoad = $this->manager->loadTrackerPlugins();
$this->assertTrue($cache->contains($this->trackerCacheId));
$this->assertEquals($pluginsToLoad, $cache->fetch($this->trackerCacheId));
}
public function test_loadTrackerPlugins_shouldBeAbleToLoadPluginsCorrectWhenItIsCached()
{
$pluginsToLoad = array('CoreAdminHome', 'CoreHome', 'UserLanguage', 'Login');
$this->getCacheForTrackerPlugins()->save($this->trackerCacheId, $pluginsToLoad);
$pluginsToLoad = $this->manager->loadTrackerPlugins();
$this->assertCount(4, $this->manager->getLoadedPlugins());
$this->assertEquals($pluginsToLoad, array_keys($this->manager->getLoadedPlugins()));
}
public function test_loadTrackerPlugins_shouldUnloadAllPlugins_IfThereAreNoneToLoad()
{
$pluginsToLoad = array();
$this->getCacheForTrackerPlugins()->save($this->trackerCacheId, $pluginsToLoad);
$pluginsToLoad = $this->manager->loadTrackerPlugins();
$this->assertEquals(array(), $pluginsToLoad);
$this->assertEquals(array(), $this->manager->getLoadedPlugins());
}
public function test_deactivatePlugin()
{
$this->assertFalse($this->manager->isPluginActivated('ExampleTheme'));
$this->manager->activatePlugin('ExampleTheme');
$this->assertTrue($this->manager->isPluginActivated('ExampleTheme'));
$this->manager->deactivatePlugin('ExampleTheme');
$this->assertFalse($this->manager->isPluginActivated('ExampleTheme'));
}
/** @see Issue https://github.com/piwik/piwik/issues/8422 */
public function test_ListenNotToControllerMethodEventsThatDoesNotExists()
{
foreach ($this->manager->getLoadedPlugins() as $plugin) {
$hooks = $plugin->getListHooksRegistered();
foreach ($hooks as $hook => $callback) {
if (0 === strpos($hook, 'Controller.')) {
list($controller, $module, $action) = explode('.', $hook);
try {
$resolver = new ControllerResolver(StaticContainer::getContainer(), new Plugin\WidgetsProvider($this->manager));
$params = array();
$controller = $resolver->getController($module, $action, $params);
} catch (\Exception $e) {
$this->fail("$hook is listening to a controller method that does not exist");
}
$this->assertNotEmpty($controller);
}
}
}
}
/**
* @dataProvider getPluginNameProvider
*/
public function test_isValidPluginName($expectedIsValid, $pluginName)
{
$valid = $this->manager->isValidPluginName($pluginName);
$this->assertSame($expectedIsValid, $valid);
}
public function getPluginNameProvider()
{
return array(
array(true, 'a'),
array(true, 'a0'),
array(true, 'pluginNameTest'),
array(true, 'PluginNameTest'),
array(true, 'PluginNameTest92323232eerwrwere938'),
array(true, 'a_ererer'),
array(true, 'a_'),
array(false, ''),
array(false, '0'),
array(false, '0a'),
array(false, 'a.'),
array(false, 'a-'),
array(false, 'a-ererer'),
array(false, '..'),
array(false, '/'),
);
}
private function getCacheForTrackerPlugins()
{
return PiwikCache::getEagerCache();
}
private function assertOnlyTrackerPluginsAreLoaded($expectedPluginNamesLoaded)
{
// should currently load between 10 and 27 plugins
$this->assertLessThan(27, count($this->manager->getLoadedPlugins()));
$this->assertGreaterThan(10, count($this->manager->getLoadedPlugins()));
// we need to make sure it actually only loaded the correct ones
$this->assertEquals($expectedPluginNamesLoaded, array_keys($this->manager->getLoadedPlugins()));
}
}
|