1
0
قرینه از https://github.com/matomo-org/matomo.git synced 2025-08-24 16:07:37 +00:00
Files
matomo/tests/PHPUnit/Integration/Plugin/ManagerTest.php
Stefan Giehl d6d72d1fa7 [Coding Style] Enable rule PSR1.Methods.CamelCapsMethodName.NotCamelCaps (#22144)
* [Coding Style] Enable rule PSR1.Methods.CamelCapsMethodName.NotCamelCaps

* [Coding Style] Use camel case for method names in API plugin tests (#22145)

* [Coding Style] Use camel case for method names in Core* plugin tests (#22147)

* [Coding Style] Use camel case for method names in core Unit tests (#22149)

* [Coding Style] Use camel case for method names in Actions and BulkTracking plugin tests (#22146)

* [Coding Style] Use camel case for method names in CustomDimensions and CustomJSTracker plugin tests (#22148)

* [Coding Style] Use camel case for method names in core Integration tests (#22151)

* [Coding Style] Use camel case for method names in more core plugin tests (#22153)

* [Coding Style] Use camel case for method names in more core plugin tests (#22157)

* [Coding Style] Use camel case for method names in more core plugin tests

* Update plugins/Monolog/tests/Unit/Processor/ExceptionToTextProcessorTest.php

Co-authored-by: Michal Kleiner <michal@innocraft.com>

---------

Co-authored-by: Michal Kleiner <michal@innocraft.com>

* [Coding Style] Use camel case for method names in more core plugin tests (#22159)

* [Coding Style] Use camel case for method names in remaining tests (#22160)

* [Coding Style] Use camel case for method names in remaining tests

* rename expected test files

---------

Co-authored-by: Michal Kleiner <michal@innocraft.com>
2024-04-25 20:57:55 +02:00

210 خطوط
7.3 KiB
PHP

<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Integration\Plugin;
use Piwik\Common;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\Http\ControllerResolver;
use Piwik\Plugin;
use Piwik\Cache as PiwikCache;
use Piwik\Tests\Integration\Settings\IntegrationTestCase;
/**
* @group Plugin
* @group PluginManager
*/
class ManagerTest extends IntegrationTestCase
{
private $trackerCacheId = 'PluginsTracker';
/**
* @var Plugin\Manager
*/
private $manager;
public function setUp(): void
{
parent::setUp();
$this->manager = Plugin\Manager::getInstance();
}
public function testLoadTrackerPluginsShouldDetectTrackerPlugins()
{
$this->assertGreaterThan(50, count($this->manager->getLoadedPlugins())); // make sure all plugins are loaded
$pluginsToLoad = $this->manager->loadTrackerPlugins();
$this->assertOnlyTrackerPluginsAreLoaded($pluginsToLoad);
}
public function testLoadTrackerPluginsShouldCacheListOfPlugins()
{
$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 testLoadTrackerPluginsShouldBeAbleToLoadPluginsCorrectWhenItIsCached()
{
$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 testLoadTrackerPluginsShouldUnloadAllPluginsIfThereAreNoneToLoad()
{
$pluginsToLoad = array();
$this->getCacheForTrackerPlugins()->save($this->trackerCacheId, $pluginsToLoad);
$pluginsToLoad = $this->manager->loadTrackerPlugins();
$this->assertEquals(array(), $pluginsToLoad);
$this->assertEquals(array(), $this->manager->getLoadedPlugins());
}
public function testActivateDeactivatePlugin()
{
$plugin = new Plugin('ExampleTheme');
$this->assertNull($plugin->getPluginLastActivationTime());
$this->assertNull($plugin->getPluginLastDeactivationTime());
$this->assertFalse($this->manager->isPluginActivated('ExampleTheme'));
$this->manager->activatePlugin('ExampleTheme');
$lastActivationTime = $plugin->getPluginLastActivationTime();
$this->assertNotNull($lastActivationTime);
$this->assertNull($plugin->getPluginLastDeactivationTime());
$this->assertTrue($this->manager->isPluginActivated('ExampleTheme'));
$this->manager->deactivatePlugin('ExampleTheme');
$this->assertFalse($this->manager->isPluginActivated('ExampleTheme'));
$lastDeactivationTime = $plugin->getPluginLastDeactivationTime();
$this->assertNotNull($lastDeactivationTime);
sleep(1);
$this->manager->activatePlugin('ExampleTheme');
$nextLastActivationTime = $plugin->getPluginLastActivationTime();
$this->assertGreaterThan($lastActivationTime->getTimestamp(), $nextLastActivationTime->getTimestamp());
$this->manager->deactivatePlugin('ExampleTheme');
$nextLastDeactivationTime = $plugin->getPluginLastDeactivationTime();
$this->assertGreaterThan($lastDeactivationTime->getTimestamp(), $nextLastDeactivationTime->getTimestamp());
}
/** @see Issue https://github.com/piwik/piwik/issues/8422 */
public function testListenNotToControllerMethodEventsThatDoesNotExists()
{
foreach ($this->manager->getLoadedPlugins() as $plugin) {
$hooks = $plugin->registerEvents();
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);
}
}
}
}
public function testHasPremiumFeatures()
{
$this->assertFalse($this->manager->hasPremiumFeatures());
}
public function testIsPluginInstalledCorePluginThatExists()
{
$this->assertTrue($this->manager->isPluginInstalled('CoreAdminHome', true));
$this->assertTrue($this->manager->isPluginInstalled('CoreAdminHome', false));
}
public function testIsPluginInstalledPluginNotExists()
{
$this->assertFalse($this->manager->isPluginInstalled('FooBarBaz', true));
$this->assertFalse($this->manager->isPluginInstalled('FooBarBaz', false));
}
public function testIsPluginInstalledPluginInstalledConfigButNotExists()
{
Config::getInstance()->PluginsInstalled['PluginsInstalled'][] = 'FooBarBaz';
$this->assertFalse($this->manager->isPluginInstalled('FooBarBaz', true));
$this->assertTrue($this->manager->isPluginInstalled('FooBarBaz', false));
}
/**
* @dataProvider getPluginNameProvider
*/
public function testIsValidPluginName($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, 'PluginNameTest' . Common::getRandomString()),
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, '/'),
array(true, 'a' . Common::getRandomString(59)),
array(false, 'a' . Common::getRandomString(60)),
);
}
private function getCacheForTrackerPlugins()
{
return PiwikCache::getEagerCache();
}
private function assertOnlyTrackerPluginsAreLoaded($expectedPluginNamesLoaded)
{
// should currently load between 10 and 35 plugins
$this->assertLessThan(35, 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()));
}
}