1
0
قرینه از https://github.com/matomo-org/matomo.git synced 2025-08-25 00:17:37 +00:00
Files
matomo/tests/PHPUnit/Unit/Widget/WidgetsListTest.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

178 خطوط
5.9 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\Unit\Widget;
use Piwik\Widget\WidgetConfig;
use Piwik\Widget\WidgetContainerConfig;
use Piwik\Widget\WidgetsList;
/**
* @group Widget
* @group WidgetsList
* @group WidgetsListTest
*/
class WidgetsListTest extends \PHPUnit\Framework\TestCase
{
/**
* @var WidgetsList
*/
private $list;
public function setUp(): void
{
parent::setUp();
$this->list = new WidgetsList();
}
public function testGetWidgetUniqueIdWithoutParameters()
{
$id = WidgetsList::getWidgetUniqueId('CoreHome', 'render');
$this->assertSame('widgetCoreHomerender', $id);
}
public function testGetWidgetUniqueIdWithParameters()
{
$id = WidgetsList::getWidgetUniqueId('CoreHome', 'render', array('test1' => 'value', 'key' => array('test'), 'test2' => '4k3k'));
$this->assertSame('widgetCoreHomerendertest1valuekeyArraytest24k3k', $id);
}
public function testGetWidgetConfigsShouldBeEmptyByDefault()
{
$this->assertSame(array(), $this->list->getWidgetConfigs());
}
public function testAddWidgetShouldAddAnyWidgetConfigs()
{
$this->list->addWidgetConfig($widget1 = $this->createWidget('widget1'));
$this->list->addWidgetConfig($widget2 = $this->createWidgetContainer('widget2'));
$this->list->addWidgetConfig($widget3 = $this->createWidget('widget3'));
$this->assertSame(array($widget1, $widget2, $widget3), $this->list->getWidgetConfigs());
}
public function testAddWidgetsShouldAddAnyWidgetConfigs()
{
$this->list->addWidgetConfigs(array(
$widget1 = $this->createWidget('widget1'),
$widget2 = $this->createWidgetContainer('widget2'),
$widget3 = $this->createWidget('widget3'),
));
$this->assertSame(array($widget1, $widget2, $widget3), $this->list->getWidgetConfigs());
}
public function testAddToContainerWidgetShouldAddWidgetToContainerImmediatelyIfContainerAlreadyExistsInList()
{
$this->list->addWidgetConfigs(array(
$widget1 = $this->createWidget('widget1'),
$widget2 = $this->createWidgetContainer('widget2')->setId('testId'),
$widget3 = $this->createWidget('widget3'),
));
$this->list->addToContainerWidget('testId', $widget4 = $this->createWidget('widget4'));
$this->assertSame(array($widget4), $widget2->getWidgetConfigs());
// widget4 should not be added to this widgetConfigs
$this->assertSame(array($widget1, $widget2, $widget3), $this->list->getWidgetConfigs());
}
public function testAddToContainerWidgetShouldAddWidgetToContainerAsSoonAsContainerAddedIfContainerNotAlreadyExistsInList()
{
$this->list->addToContainerWidget('testId', $widget4 = $this->createWidget('widget4'));
$this->list->addWidgetConfigs(array(
$widget1 = $this->createWidget('widget1'),
$widget2 = $this->createWidgetContainer('widget2')->setId('testId'),
$widget3 = $this->createWidget('widget3'),
));
$this->assertSame(array($widget4), $widget2->getWidgetConfigs());
// widget4 should not be added to this widgetConfigs
$this->assertSame(array($widget1, $widget2, $widget3), $this->list->getWidgetConfigs());
}
/**
* @dataProvider getWidgetsToRemove
*/
public function testRemove($categoryId, $name, $expectedWidgetNamesInList)
{
$this->list->addWidgetConfigs(array(
$widget1 = $this->createWidget('widget1')->setCategoryId('Visits'),
$widget2 = $this->createWidgetContainer('widget2')->setCategoryId('Actions'),
$widget3 = $this->createWidget('widget3')->setCategoryId('Visits'),
));
$this->list->remove($categoryId, $name);
$names = array();
foreach ($this->list->getWidgetConfigs() as $config) {
$names[] = $config->getName();
}
$this->assertSame($expectedWidgetNamesInList, $names);
}
public function getWidgetsToRemove()
{
return array(
array('Visits', false, array('widget2')),
array('Visits', 'widget3', array('widget1', 'widget2')),
array('Actions', false, array('widget1', 'widget3')),
array('Actions', 'widget2', array('widget1', 'widget3')),
array('Actions', 'notExist', array('widget1', 'widget2', 'widget3')),
array('NotExiSt', false, array('widget1', 'widget2', 'widget3')),
);
}
/**
* @dataProvider getWidgetsDefined
*/
public function testIsDefined($module, $action, $exists)
{
$this->list->addWidgetConfigs(array(
$widget1 = $this->createWidget('widget1')->setModule('CoreHome')->setAction('renderMe'),
$widget2 = $this->createWidgetContainer('widget2')->setModule('CoreHome')->setAction('renderContainer'),
$widget3 = $this->createWidget('widget3')->setModule('Actions')->setAction('index'),
));
$this->assertSame($exists, $this->list->isDefined($module, $action));
}
public function getWidgetsDefined()
{
return array(
array('CoreHome', 'renderMe', $isDefined = true),
array('CoreHome', 'renderContainer', $isDefined = true),
array('Actions', 'index', $isDefined = true),
array('Actions', 'renderMe', $isDefined = false),
array('AnyThiNg', 'renderMe', $isDefined = false),
array('CoreHome', 'index', $isDefined = false)
);
}
private function createWidget($name)
{
$config = new WidgetConfig();
$config->setName($name);
return $config;
}
private function createWidgetContainer($name)
{
$config = new WidgetContainerConfig();
$config->setName($name);
return $config;
}
}