Download tests/PHPUnit/Integration/Settings/Storage/Backend/PluginSettingsTableTest.php from Leon4gr45/matomo: direct link, hf CLI and curl.
- Browser
- Download file 14.5 kB
-
https://huggingface.co/spaces/Leon4gr45/matomo/resolve/main/tests/PHPUnit/Integration/Settings/Storage/Backend/PluginSettingsTableTest.php
- Command line
-
hf download hf://spaces/Leon4gr45/matomo/tests/PHPUnit/Integration/Settings/Storage/Backend/PluginSettingsTableTest.php
-
curl -L -o PluginSettingsTableTest.php https://huggingface.co/spaces/Leon4gr45/matomo/resolve/main/tests/PHPUnit/Integration/Settings/Storage/Backend/PluginSettingsTableTest.php
14.5 kB
| /** | |
| * 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\Storage\Backend; | |
| use Piwik\Settings\Storage\Backend\PluginSettingsTable; | |
| use Piwik\Tests\Framework\TestCase\IntegrationTestCase; | |
| /** | |
| * @group Settings | |
| * @group Backend | |
| * @group Storage | |
| */ | |
| class PluginSettingsTableTest extends IntegrationTestCase | |
| { | |
| /** | |
| * @var PluginSettingsTable | |
| */ | |
| private $backendPlugin1; | |
| /** | |
| * @var PluginSettingsTable | |
| */ | |
| private $backendPlugin2; | |
| /** | |
| * @var PluginSettingsTable | |
| */ | |
| private $backendUser1; | |
| /** | |
| * @var PluginSettingsTable | |
| */ | |
| private $backendUser2; | |
| /** | |
| * @var PluginSettingsTable[] | |
| */ | |
| private $allBackends = array(); | |
| public function setUp(): void | |
| { | |
| parent::setUp(); | |
| $this->backendPlugin1 = $this->createSettings('MyPluginName', ''); | |
| $this->backendPlugin2 = $this->createSettings('MyPluginName2', ''); | |
| $this->backendUser1 = $this->createSettings('MyPluginName', 'user1'); | |
| $this->backendUser2 = $this->createSettings('MyPluginName', 'user2'); | |
| $this->allBackends = array($this->backendPlugin1, $this->backendPlugin2, $this->backendUser1, $this->backendUser2); | |
| } | |
| private function createSettings($plugin, $login) | |
| { | |
| return new PluginSettingsTable($plugin, $login); | |
| } | |
| public function testConstructShouldThrowAnExceptionIfPluginNameIsEmpty() | |
| { | |
| $this->expectException(\Exception::class); | |
| $this->expectExceptionMessage('No plugin name given'); | |
| $this->createSettings('', ''); | |
| } | |
| public function testConstructShouldThrowAnExceptionIfUserLoginFalse() | |
| { | |
| $this->expectException(\Exception::class); | |
| $this->expectExceptionMessage('Invalid user login name'); | |
| $this->createSettings('MyPlugin', false); | |
| } | |
| public function testConstructShouldThrowAnExceptionIfUserLoginNull() | |
| { | |
| $this->expectException(\Exception::class); | |
| $this->expectExceptionMessage('Invalid user login name'); | |
| $this->createSettings('MyPlugin', null); | |
| } | |
| public function testLoadShouldNotHaveAnySettingsByDefault() | |
| { | |
| $this->assertSame(array(), $this->backendPlugin1->load()); | |
| $this->assertSame(array(), $this->backendPlugin2->load()); | |
| $this->assertSame(array(), $this->backendUser1->load()); | |
| $this->assertSame(array(), $this->backendUser2->load()); | |
| } | |
| public function testGetStorageIdShouldIncludePluginNameAndLogin() | |
| { | |
| $this->assertSame('PluginSettings_MyPluginName_User_', $this->backendPlugin1->getStorageId()); | |
| $this->assertSame('PluginSettings_MyPluginName2_User_', $this->backendPlugin2->getStorageId()); | |
| $this->assertSame('PluginSettings_MyPluginName_User_user1', $this->backendUser1->getStorageId()); | |
| $this->assertSame('PluginSettings_MyPluginName_User_user2', $this->backendUser2->getStorageId()); | |
| } | |
| public function testSaveShouldOnlySaveForSpecificPluginNoUserGiven() | |
| { | |
| $value1 = array('Mysetting1' => 'value1', 'Mysetting2' => 'val'); | |
| $this->backendPlugin1->save($value1); | |
| $this->assertSame($value1, $this->backendPlugin1->load()); | |
| $this->assertSame(array(), $this->backendPlugin2->load()); | |
| $this->assertSame(array(), $this->backendUser1->load()); | |
| $this->assertSame(array(), $this->backendUser2->load()); | |
| $value2 = array('mytest' => 'test2'); | |
| $this->backendPlugin2->save($value2); | |
| $this->assertSame($value1, $this->backendPlugin1->load()); | |
| $this->assertSame($value2, $this->backendPlugin2->load()); | |
| $this->assertSame(array(), $this->backendUser1->load()); | |
| $this->assertSame(array(), $this->backendUser2->load()); | |
| } | |
| public function testSaveShouldOnlySaveForSpecificPluginAndUser() | |
| { | |
| $values = array_fill(0, count($this->allBackends), array()); | |
| foreach ($this->allBackends as $index => $backend) { | |
| $values[$index] = $this->getExampleValues(); | |
| $backend->save($values[$index]); | |
| foreach ($this->allBackends as $j => $backend2) { | |
| $this->assertSame($values[$j], $backend2->load()); | |
| } | |
| } | |
| } | |
| public function testDeleteShouldDeleteAllValuesButOnlyForSpecificPluginAndLogin() | |
| { | |
| $values = array(); | |
| foreach ($this->allBackends as $index => $backend) { | |
| $values[$index] = $this->getExampleValues(); | |
| $backend->save($values[$index]); | |
| $this->assertSame($values[$index], $backend->load()); | |
| } | |
| foreach ($this->allBackends as $index => $backend) { | |
| $backend->delete(); | |
| $values[$index] = array(); | |
| // we make sure the values for all others are still set and only the current one was deleted | |
| foreach ($this->allBackends as $j => $backend2) { | |
| $this->assertSame($values[$j], $backend2->load()); | |
| } | |
| } | |
| } | |
| public function testSaveDuplicateValuesShouldBeOverwritten() | |
| { | |
| $value1 = array('Mysetting1' => 'value1', 'Mysetting2' => 'val'); | |
| $this->backendPlugin1->save($value1); | |
| $this->assertSame($value1, $this->backendPlugin1->load()); | |
| $value2 = array('mytest' => 'test2', 'Mysetting2' => 'val', 'Mysetting1' => 'valueNew'); | |
| $this->backendPlugin1->save($value2); | |
| $this->assertEquals($value2, $this->backendPlugin1->load()); | |
| } | |
| public function testSaveNoLongerExistingValuesShouldBeRemoved() | |
| { | |
| $value = $this->saveValueForAllBackends(); | |
| // overwrite only user1 | |
| $value2 = array('mytest' => 'test2', 'Mysetting1' => 'valueNew'); | |
| $this->backendUser1->save($value2); | |
| $this->assertEquals($value2, $this->backendUser1->load()); | |
| // make sure other backends remain unchanged | |
| foreach ($this->allBackends as $backend) { | |
| if ($backend !== $this->backendUser1) { | |
| $this->assertSame($value, $backend->load()); | |
| } | |
| } | |
| } | |
| public function testSaveLoadShouldBeAbleToSaveAndLoadArrayValues() | |
| { | |
| $value1 = array('Mysetting1' => 'value1', 'Mysetting2' => array('val', 'val7', 'val5')); | |
| $this->backendUser1->save($value1); | |
| $this->assertEquals($value1, $this->backendUser1->load()); | |
| } | |
| public function testSaveLoadShouldBeAbleToSaveAndLoadObjectValues() | |
| { | |
| $value1 = array('Mysetting1' => 'value1', 'Mysetting2' => (object) array('val', 'val7', 'val5')); | |
| $this->backendUser1->save($value1); | |
| $value1['Mysetting2'] = (array) $value1['Mysetting2']; | |
| $this->assertEquals($value1, $this->backendUser1->load()); | |
| } | |
| public function testSaveLoadShouldBeAbleToSaveNestedArrays() | |
| { | |
| $value1 = array('Mysetting1' => 'value1', 'Mysetting2' => array(array('foo' => 'bar'),array('foo' => 'baz'))); | |
| $this->backendUser1->save($value1); | |
| $this->assertEquals($value1, $this->backendUser1->load()); | |
| } | |
| public function testSaveLoadShouldBeAbleToSaveAndLoadArrayValuesOnlyOneKey() | |
| { | |
| $value1 = array('Mysetting1' => 'value1', 'Mysetting2' => array('val')); | |
| $this->backendUser1->save($value1); | |
| $value1 = array('Mysetting1' => 'value1', 'Mysetting2' => array('val')); | |
| // it doesn't return an array for Mysetting2 but it is supposed to be casted to array by storage in this case | |
| $this->assertEquals($value1, $this->backendUser1->load()); | |
| } | |
| public function testSaveShouldBeAbleToSaveBoolValues() | |
| { | |
| $value1 = array('Mysetting1' => true, 'Mysetting2' => array('val', 'val7', false, true, 'val5')); | |
| $this->backendUser1->save($value1); | |
| $value1 = array('Mysetting1' => '1', 'Mysetting2' => array('val', 'val7', false, true, 'val5')); | |
| $this->assertEquals($value1, $this->backendUser1->load()); | |
| } | |
| public function testSaveShouldIgnoreNullValuesButNotInArray() | |
| { | |
| $value1 = array('Mysetting1' => true, 'MySetting3' => null, 'Mysetting2' => array('val', null, true, 'val5')); | |
| $this->backendUser1->save($value1); | |
| $value1 = array('Mysetting1' => '1', 'Mysetting2' => array('val', null, true, 'val5')); | |
| $this->assertEquals($value1, $this->backendUser1->load()); | |
| } | |
| public function testSaveValueShouldSaveOnlySingleValueAndKeepOtherValues() | |
| { | |
| $this->backendUser1->save(array('Mysetting1' => 'value1', 'Mysetting2' => 'value2')); | |
| $this->backendUser1->saveValue('Mysetting1', 'valueNew'); | |
| $this->assertSame('valueNew', $this->backendUser1->loadValue('Mysetting1')); | |
| $this->assertSame('value2', $this->backendUser1->loadValue('Mysetting2')); | |
| $this->assertEquals(array('Mysetting1' => 'valueNew', 'Mysetting2' => 'value2'), $this->backendUser1->load()); | |
| } | |
| public function testSaveValueShouldDeleteValueWhenValueIsNull() | |
| { | |
| $this->backendUser1->save(array('Mysetting1' => 'value1', 'Mysetting2' => 'value2')); | |
| $this->backendUser1->saveValue('Mysetting1', null); | |
| $this->assertSame('defaultValue', $this->backendUser1->loadValue('Mysetting1', 'defaultValue')); | |
| $this->assertSame(array('Mysetting2' => 'value2'), $this->backendUser1->load()); | |
| } | |
| public function testSaveValueShouldSupportArrayObjectAndBoolValues() | |
| { | |
| $this->backendUser1->saveValue('arrayValue', array('foo' => 'bar')); | |
| $this->backendUser1->saveValue('objectValue', (object) array('one' => 'two')); | |
| $this->backendUser1->saveValue('boolValue', true); | |
| $this->assertSame(array('foo' => 'bar'), $this->backendUser1->loadValue('arrayValue')); | |
| $this->assertSame(array('one' => 'two'), $this->backendUser1->loadValue('objectValue')); | |
| $this->assertSame('1', $this->backendUser1->loadValue('boolValue')); | |
| } | |
| public function testLoadValueShouldReturnDefaultIfValueDoesNotExist() | |
| { | |
| $this->assertSame('myDefault', $this->backendUser1->loadValue('unknownSetting', 'myDefault')); | |
| $this->assertNull($this->backendUser1->loadValue('unknownSetting')); | |
| } | |
| public function testDeleteValueShouldDeleteOnlySpecificValue() | |
| { | |
| $this->backendUser1->save(array('Mysetting1' => 'value1', 'Mysetting2' => 'value2')); | |
| $this->backendUser1->deleteValue('Mysetting1'); | |
| $this->assertSame('defaultValue', $this->backendUser1->loadValue('Mysetting1', 'defaultValue')); | |
| $this->assertSame('value2', $this->backendUser1->loadValue('Mysetting2')); | |
| $this->assertSame(array('Mysetting2' => 'value2'), $this->backendUser1->load()); | |
| } | |
| public function testLoadValuesForUsersShouldReturnValuesForSelectedSettingsAndOnlyUsers() | |
| { | |
| $this->backendPlugin1->saveValue('Mysetting1', 'pluginValue'); | |
| $this->backendUser1->save(array( | |
| 'Mysetting1' => 'userValue1', | |
| 'Mysetting2' => array('foo' => 'bar'), | |
| 'Mysetting3' => 'ignoreMe', | |
| )); | |
| $this->backendUser2->save(array( | |
| 'Mysetting1' => 'userValue2', | |
| 'Mysetting2' => array('foo' => 'baz'), | |
| )); | |
| $result = $this->backendPlugin1->loadValuesForUsers(array('Mysetting1', 'Mysetting2')); | |
| $this->assertSame(array( | |
| 'user1' => array( | |
| 'Mysetting1' => 'userValue1', | |
| 'Mysetting2' => array('foo' => 'bar'), | |
| ), | |
| 'user2' => array( | |
| 'Mysetting1' => 'userValue2', | |
| 'Mysetting2' => array('foo' => 'baz'), | |
| ), | |
| ), $result); | |
| } | |
| public function testLoadValuesForUsersShouldReturnEmptyArrayWhenNoSettingNamesProvided() | |
| { | |
| $this->backendUser1->save(array('Mysetting1' => 'userValue1')); | |
| $this->assertSame(array(), $this->backendPlugin1->loadValuesForUsers(array())); | |
| } | |
| public function testSaveValueShouldThrowExceptionIfSettingNameIsEmpty() | |
| { | |
| $this->expectException(\Exception::class); | |
| $this->expectExceptionMessage('No setting name given'); | |
| $this->backendUser1->saveValue('', 'value1'); | |
| } | |
| public function testLoadValueShouldThrowExceptionIfSettingNameIsEmpty() | |
| { | |
| $this->expectException(\Exception::class); | |
| $this->expectExceptionMessage('No setting name given'); | |
| $this->backendUser1->loadValue(''); | |
| } | |
| public function testDeleteValueShouldThrowExceptionIfSettingNameIsEmpty() | |
| { | |
| $this->expectException(\Exception::class); | |
| $this->expectExceptionMessage('No setting name given'); | |
| $this->backendUser1->deleteValue(''); | |
| } | |
| public function testRemoveAllUserSettingsForUserShouldOnlyRemoveSettingsForThatUser() | |
| { | |
| $value = $this->saveValueForAllBackends(); | |
| PluginSettingsTable::removeAllUserSettingsForUser('user1'); | |
| foreach ($this->allBackends as $backend) { | |
| if ($backend === $this->backendUser1) { | |
| $this->assertSame(array(), $backend->load()); | |
| } else { | |
| $this->assertSame($value, $backend->load()); | |
| } | |
| } | |
| } | |
| public function testRemoveAllUserSettingsForUserShouldThrowAnExceptionIfLoginIsEmpty() | |
| { | |
| $this->expectException(\Exception::class); | |
| $this->expectExceptionMessage('No userLogin specified'); | |
| PluginSettingsTable::removeAllUserSettingsForUser(''); | |
| } | |
| public function testRemoveAllSettingsForPluginShouldOnlyRemoveSettingsForThatPlugin() | |
| { | |
| $value = $this->saveValueForAllBackends(); | |
| PluginSettingsTable::removeAllSettingsForPlugin('MyPluginName'); | |
| foreach ($this->allBackends as $backend) { | |
| if ($backend === $this->backendPlugin2) { | |
| $this->assertSame($value, $backend->load()); | |
| } else { | |
| $this->assertSame(array(), $backend->load()); | |
| } | |
| } | |
| } | |
| private function saveValueForAllBackends() | |
| { | |
| $value = array('Mysetting1' => 'value1', 'Mysetting2' => 'val'); | |
| foreach ($this->allBackends as $backend) { | |
| $backend->save($value); | |
| $this->assertSame($value, $backend->load()); | |
| } | |
| return $value; | |
| } | |
| private function getExampleValues() | |
| { | |
| return array('Mysetting3' => 'value3', 'Mysetting4' . rand(4, 99) => 'val' . rand(0, 10)); | |
| } | |
| } | |