قرینه از
https://github.com/matomo-org/matomo.git
synced 2025-08-24 16:07:37 +00:00

* [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>
294 خطوط
9.7 KiB
PHP
294 خطوط
9.7 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\Settings\Plugin;
|
|
|
|
use Piwik\Settings\FieldConfig;
|
|
use Piwik\Settings\Setting;
|
|
use Piwik\Settings\Storage\Storage;
|
|
use Piwik\Settings\Storage\Backend;
|
|
use Exception;
|
|
use Piwik\Tests\Framework\Fixture;
|
|
use Piwik\Tests\Framework\Mock\Settings\FakeBackend;
|
|
use Piwik\Validators\NotEmpty;
|
|
use Piwik\Validators\NumberRange;
|
|
|
|
/**
|
|
* @group Settings
|
|
* @group Setting
|
|
*/
|
|
class SettingTest extends \PHPUnit\Framework\TestCase
|
|
{
|
|
public function setUp(): void
|
|
{
|
|
$fixutre = new Fixture();
|
|
$fixutre->createEnvironmentInstance();
|
|
}
|
|
|
|
public function tearDown(): void
|
|
{
|
|
Fixture::clearInMemoryCaches();
|
|
|
|
$fixutre = new Fixture();
|
|
$fixutre->destroyEnvironment();
|
|
}
|
|
|
|
public function testConstructorShouldThrowExceptionIfTheSettingNameIsNotValid()
|
|
{
|
|
$this->expectException(\Exception::class);
|
|
$this->expectExceptionMessage('setting name "myname-" in plugin "MyPluginName" is invalid');
|
|
|
|
$this->makeSetting('myname-');
|
|
}
|
|
|
|
public function testConfigureFieldShouldAssignDefaultFieldIfTypeIsGivenButNoField()
|
|
{
|
|
$setting = $this->makeSetting('myname', FieldConfig::TYPE_ARRAY);
|
|
$field = $setting->configureField();
|
|
$this->assertEquals(FieldConfig::UI_CONTROL_MULTI_SELECT, $field->uiControl);
|
|
|
|
$setting = $this->makeSetting('myname2', FieldConfig::TYPE_BOOL);
|
|
$field = $setting->configureField();
|
|
$this->assertEquals(FieldConfig::UI_CONTROL_CHECKBOX, $field->uiControl);
|
|
}
|
|
|
|
public function testConfigureFieldShouldCheckThatTypeMakesActuallySenseForConfiguredUiControl()
|
|
{
|
|
$this->expectException(\Exception::class);
|
|
$this->expectExceptionMessage('Type must be an array when using a multi select');
|
|
|
|
$setting = $this->makeSetting('myname', FieldConfig::TYPE_STRING, $default = '', function (FieldConfig $field) {
|
|
$field->uiControl = FieldConfig::UI_CONTROL_MULTI_SELECT;
|
|
});
|
|
$setting->configureField();
|
|
}
|
|
|
|
public function testConfigureFieldChecksTheGivenTypeIsKnown()
|
|
{
|
|
$this->expectException(\Exception::class);
|
|
$this->expectExceptionMessage('Type does not exist');
|
|
|
|
$setting = $this->makeSetting('myname', 'unknOwnTyPe');
|
|
$setting->configureField();
|
|
}
|
|
|
|
public function testSetValueShouldValidateAutomaticallyIfFieldOptionsAreGiven()
|
|
{
|
|
$setting = $this->makeSetting('myname', null, $default = '', function (FieldConfig $field) {
|
|
$field->availableValues = array('allowedval' => 'DisplayName', 'allowedval2' => 'Name 2');
|
|
});
|
|
|
|
// valid value
|
|
$setting->setValue('allowedval');
|
|
$this->assertSame('allowedval', $setting->getValue());
|
|
|
|
try {
|
|
$setting->setValue('invAliDValue');
|
|
} catch (Exception $e) {
|
|
self::assertStringContainsString('CoreAdminHome_PluginSettingsValueNotAllowed', $e->getMessage());
|
|
return;
|
|
}
|
|
|
|
$this->fail('An expected exception has not been thrown');
|
|
}
|
|
|
|
public function testSetValueShouldApplyValidationAndFailIfOptionsAreSetAndValueIsAnArray()
|
|
{
|
|
$this->expectException(\Exception::class);
|
|
$this->expectExceptionMessage('CoreAdminHome_PluginSettingsValueNotAllowed');
|
|
|
|
$setting = $this->makeSetting('myname', FieldConfig::TYPE_ARRAY, $default = '', function (FieldConfig $field) {
|
|
$field->availableValues = array('allowedval' => 'DisplayName', 'allowedval2' => 'Name 2');
|
|
$field->uiControl = FieldConfig::UI_CONTROL_MULTI_SELECT;
|
|
});
|
|
|
|
$setting->setValue(array('allowed', 'notallowed'));
|
|
}
|
|
|
|
public function testSetSettingValueShouldApplyValidationAndSucceedIfOptionsAreSet()
|
|
{
|
|
$setting = $this->makeSetting('myname', FieldConfig::TYPE_ARRAY, $default = '', function (FieldConfig $field) {
|
|
$field->availableValues = array('allowedval' => 'DisplayName', 'allowedval2' => 'Name 2');
|
|
$field->uiControl = FieldConfig::UI_CONTROL_MULTI_SELECT;
|
|
});
|
|
$setting->setValue(array('allowedval', 'allowedval2'));
|
|
$this->assertSame($setting->getValue(), array('allowedval', 'allowedval2'));
|
|
|
|
$setting = $this->makeSetting('myname2', null, $default = '', function (FieldConfig $field) {
|
|
$field->availableValues = array('allowedval' => 'DisplayName', 'allowedval2' => 'Name 2');
|
|
});
|
|
|
|
$setting->setValue('allowedval');
|
|
$this->assertSame($setting->getValue(), 'allowedval');
|
|
}
|
|
|
|
public function testSetValueShouldValidateAutomaticallyIfTypeBoolIsUsed()
|
|
{
|
|
$setting = $this->makeSetting('myname', FieldConfig::TYPE_BOOL);
|
|
|
|
// valid values
|
|
$setting->setValue('1');
|
|
$this->assertSame(true, $setting->getValue());
|
|
$setting->setValue(false);
|
|
$this->assertSame(false, $setting->getValue());
|
|
$setting->setValue(1);
|
|
$this->assertSame(true, $setting->getValue());
|
|
|
|
try {
|
|
$setting->setValue('invAliDValue');
|
|
} catch (Exception $e) {
|
|
self::assertStringContainsString('CoreAdminHome_PluginSettingsValueNotAllowed', $e->getMessage());
|
|
return;
|
|
}
|
|
|
|
$this->fail('An expected exception has not been thrown');
|
|
}
|
|
|
|
/**
|
|
* @dataProvider getNumericTypes
|
|
*/
|
|
public function testSetValueShouldValidateAutomaticallyIfTypeIsNumeric($type)
|
|
{
|
|
$setting = $this->makeSetting('myname', $type);
|
|
|
|
// valid values
|
|
$setting->setValue('1');
|
|
$setting->setValue('1.5');
|
|
$setting->setValue(0);
|
|
$setting->setValue(0.5);
|
|
$setting->setValue(-22.5);
|
|
|
|
try {
|
|
$setting->setValue('1invalid');
|
|
} catch (Exception $e) {
|
|
self::assertStringContainsString('CoreAdminHome_PluginSettingsValueNotAllowed', $e->getMessage());
|
|
return;
|
|
}
|
|
|
|
$this->fail('An expected exception has not been thrown');
|
|
}
|
|
|
|
public function testSetValueShouldExecuteValidators()
|
|
{
|
|
$setting = $this->makeSetting('myname');
|
|
$config = $setting->configureField();
|
|
$config->validators[] = new NotEmpty();
|
|
$config->validators[] = new NumberRange(5, 10);
|
|
|
|
// valid values
|
|
$setting->setValue('7');
|
|
$setting->setValue('8.5');
|
|
|
|
try {
|
|
$setting->setValue('1invalid');
|
|
$this->fail('An expected exception has not been thrown');
|
|
} catch (Exception $e) {
|
|
self::assertStringContainsString('General_ValidatorErrorNotANumber', $e->getMessage());
|
|
}
|
|
|
|
try {
|
|
$setting->setValue('3');
|
|
$this->fail('An expected exception has not been thrown');
|
|
} catch (Exception $e) {
|
|
self::assertStringContainsString('General_ValidatorErrorNumberTooLow', $e->getMessage());
|
|
}
|
|
|
|
try {
|
|
$setting->setValue('');
|
|
$this->fail('An expected exception has not been thrown');
|
|
} catch (Exception $e) {
|
|
self::assertStringContainsString('General_ValidatorErrorEmptyValue', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function getNumericTypes()
|
|
{
|
|
return array(array(FieldConfig::TYPE_INT), array(FieldConfig::TYPE_FLOAT));
|
|
}
|
|
|
|
public function testIsWritableByCurrentUserShouldNotBeWritableByDefault()
|
|
{
|
|
$setting = new Setting($name = 'test', $default = 0, $type = FieldConfig::TYPE_INT, function () {
|
|
});
|
|
$this->assertFalse($setting->isWritableByCurrentUser());
|
|
}
|
|
|
|
public function testSetIsWritableByCurrentUser()
|
|
{
|
|
$setting = $this->makeSetting('myName');
|
|
$this->assertTrue($setting->isWritableByCurrentUser());
|
|
|
|
$setting->setIsWritableByCurrentUser(0);
|
|
$this->assertFalse($setting->isWritableByCurrentUser());
|
|
|
|
$setting->setIsWritableByCurrentUser(1);
|
|
$this->assertTrue($setting->isWritableByCurrentUser());
|
|
|
|
$setting->setIsWritableByCurrentUser(false);
|
|
$this->assertFalse($setting->isWritableByCurrentUser());
|
|
}
|
|
|
|
public function testSetDefaultValueGetDefaultValue()
|
|
{
|
|
$setting = $this->makeSetting('myname');
|
|
$setting->setDefaultValue(5);
|
|
$this->assertSame(5, $setting->getDefaultValue());
|
|
}
|
|
|
|
public function testGetType()
|
|
{
|
|
$setting = $this->makeSetting('myname', FieldConfig::TYPE_ARRAY);
|
|
$this->assertSame(FieldConfig::TYPE_ARRAY, $setting->getType());
|
|
}
|
|
|
|
public function testGetName()
|
|
{
|
|
$setting = $this->makeSetting('myName');
|
|
$this->assertSame('myName', $setting->getName());
|
|
}
|
|
|
|
protected function makeSetting($name, $type = null, $default = '', $configure = null)
|
|
{
|
|
if (!isset($type)) {
|
|
$type = FieldConfig::TYPE_STRING;
|
|
}
|
|
|
|
$setting = new Setting($name, $default, $type, 'MyPluginName');
|
|
$setting->setStorage(new Storage(new Backend\NullBackend('myId')));
|
|
$setting->setIsWritableByCurrentUser(true);
|
|
|
|
if (isset($configure)) {
|
|
$setting->setConfigureCallback($configure);
|
|
}
|
|
|
|
return $setting;
|
|
}
|
|
|
|
public function testSaveShouldPersistValue()
|
|
{
|
|
$value = array(2,3,4);
|
|
|
|
$backend = new FakeBackend('test');
|
|
$backend->save(array());
|
|
$storage = new Storage($backend);
|
|
|
|
$setting = $this->makeSetting('mysetting', FieldConfig::TYPE_ARRAY);
|
|
$setting->setStorage($storage);
|
|
$setting->setValue($value);
|
|
|
|
// assert not saved in backend
|
|
$this->assertSame(array(), $backend->load());
|
|
|
|
$setting->save();
|
|
|
|
// assert saved in backend
|
|
$this->assertSame(array('mysetting' => $value), $backend->load());
|
|
}
|
|
}
|