Newer
Older
Thomas Steur
a validé
<?php
/**
* Piwik - free/libre analytics platform
Thomas Steur
a validé
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Integration\Plugin;
Thomas Steur
a validé
use Piwik\Access;
Thomas Steur
a validé
use Piwik\Plugin\Settings as PluginSettings;
use Piwik\Tests\Framework\Mock\FakeAccess;
Thomas Steur
a validé
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
Thomas Steur
a validé
class CorePluginSettingsTest extends \Piwik\Plugins\ExampleSettingsPlugin\Settings {
Thomas Steur
a validé
public function init()
{
}
public function addSetting(Setting $setting)
Thomas Steur
a validé
{
parent::addSetting($setting);
}
}
/**
* @group Core
* @group PluginSettings
*/
class SettingsTest extends IntegrationTestCase
Thomas Steur
a validé
{
/**
Thomas Steur
a validé
*/
private $settings;
public function setUp()
{
parent::setUp();
Access::setSingletonInstance(null);
Thomas Steur
a validé
$this->settings = $this->createSettingsInstance();
}
public function tearDown()
{
$this->setSuperUser();
if ($this->settings) {
$this->settings->removeAllPluginSettings();
Thomas Steur
a validé
parent::tearDown();
}
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
public function test_constructor_shouldNotEstablishADatabaseConnection()
{
Db::destroyDatabaseObject();
$this->assertNotDbConnectionCreated();
$this->createSettingsInstance();
$this->assertNotDbConnectionCreated();
}
public function test_constructor_shouldEstablishADatabaseConnection_AsSoonAsWeGetAValue()
{
$this->setSuperUser();
Db::destroyDatabaseObject();
$setting = $this->buildUserSetting('testSetting', 'Test Setting');
$settings = $this->createSettingsInstance();
$settings->addSetting($setting);
$this->assertNotDbConnectionCreated();
$settings->getSettingValue($setting);
$this->assertDbConnectionCreated();
}
public function test_constructor_shouldEstablishADatabaseConnection_AsSoonAsWeSetAValue()
{
$this->setSuperUser();
Db::destroyDatabaseObject();
$setting = $this->buildUserSetting('testSetting', 'Test Setting');
$settings = $this->createSettingsInstance();
$settings->addSetting($setting);
$this->assertNotDbConnectionCreated();
$settings->setSettingValue($setting, '5');
$this->assertDbConnectionCreated();
}
Thomas Steur
a validé
/**
* @expectedException \Exception
* @expectedExceptionMessage A setting with name "myname" does already exist for plugin "ExampleSettingsPlugin"
*/
public function test_addSetting_shouldThrowException_InCaseTwoSettingsHaveTheSameName()
{
$this->addUserSetting('myname', 'mytitle');
$setting = $this->buildUserSetting('myname', 'mytitle2');
$this->settings->addSetting($setting);
}
/**
* @expectedException \Exception
* @expectedExceptionMessage The setting name "myname_" in plugin "ExampleSettingsPlugin" is not valid. Only alpha and numerical characters are allowed
*/
public function test_addSetting_shouldThrowException_IfTheSettingNameIsNotValid()
{
$setting = $this->buildUserSetting('myname_', 'mytitle');
$this->settings->addSetting($setting);
}
public function test_addSetting_shouldAssignDefaultType_IfFieldIsGivenButNoType()
{
$setting = $this->buildUserSetting('myname', 'mytitle');
$setting->uiControlType = CorePluginSettingsTest::CONTROL_MULTI_SELECT;
Thomas Steur
a validé
$this->settings->addSetting($setting);
$this->assertEquals(CorePluginSettingsTest::TYPE_ARRAY, $setting->type);
Thomas Steur
a validé
}
public function test_addSetting_shouldAssignDefaultField_IfTypeIsGivenButNoField()
{
$setting = $this->buildUserSetting('myname', 'mytitle');
$setting->type = CorePluginSettingsTest::TYPE_ARRAY;
Thomas Steur
a validé
$this->settings->addSetting($setting);
$this->assertEquals(CorePluginSettingsTest::CONTROL_MULTI_SELECT, $setting->uiControlType);
Thomas Steur
a validé
}
public function test_addSetting_shouldAddAValidator_IfFieldOptionsAreGiven()
{
$setting = $this->buildUserSetting('myname', 'mytitle');
$setting->availableValues = array('allowedval' => 'DisplayName', 'allowedval2' => 'Name 2');
Thomas Steur
a validé
$this->settings->addSetting($setting);
$this->assertInstanceOf('\Closure', $setting->validate);
}
public function test_addSetting_shouldAddTheSettings_IfValid()
{
$setting = $this->addUserSetting('myname', 'mytitle');
$this->assertEquals(array('myname' => $setting), $this->settings->getSettings());
}
public function test_addSetting_shouldPassTheStorage_ToTheSetting()
{
$this->setSuperUser();
$setting = $this->buildUserSetting('myname', 'mytitle', 'myRandomName');
$this->settings->addSetting($setting);
$this->settings->setSettingValue($setting, 5);
$this->assertSettingHasValue($setting, 5);
$this->assertEquals($setting->getValue(), 5);
$this->settings->setSettingValue($setting, 'test3434');
$this->assertEquals($setting->getValue(), 'test3434');
}
Thomas Steur
a validé
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* @expectedException \Exception
* @expectedExceptionMessage The setting myname2 does not exist
*/
public function test_setSettingValue_shouldThrowException_IfTryingToSetAValueForNotAvailableSetting()
{
$this->addUserSetting('myname', 'mytitle');
$setting = $this->buildUserSetting('myname2', 'mytitle2');
$this->settings->setSettingValue($setting, 2);
}
/**
* @expectedException \Exception
* @expectedExceptionMessage CoreAdminHome_PluginSettingChangeNotAllowed
*/
public function test_setSettingValue_shouldThrowException_IfAUserIsTryingToSetASettingWhichNeedsSuperUserPermission()
{
$this->setUser();
$setting = $this->addSystemSetting('mysystem', 'mytitle');
$this->settings->setSettingValue($setting, 2);
}
/**
* @expectedException \Exception
* @expectedExceptionMessage CoreAdminHome_PluginSettingChangeNotAllowed
*/
public function test_setSettingValue_shouldThrowException_IfAnonymousIsTryingToSetASettingWhichNeedsUserPermission()
{
$setting = $this->addUserSetting('mysystem', 'mytitle');
$this->settings->setSettingValue($setting, 2);
}
public function test_setSettingValue_shouldSucceed_IfUserIsTryingToSetASettingWhichNeedsUserPermission()
{
$this->setUser();
$setting = $this->addUserSetting('mysystem', 'mytitle');
$this->settings->setSettingValue($setting, 2);
$this->assertSettingHasValue($setting, 2);
}
public function test_setSettingValue_shouldSucceed_IfSuperUserTriesToSaveASettingWhichRequiresSuperUserPermission()
{
$this->setSuperUser();
$setting = $this->addSystemSetting('mysystem', 'mytitle');
$this->settings->setSettingValue($setting, 2);
$this->assertSettingHasValue($setting, 2);
}
public function test_setSettingValue_shouldNotPersistValueInDatabase_OnSuccess()
{
$this->setSuperUser();
$setting = $this->buildSystemSetting('mysystem', 'mytitle');
$this->settings->addSetting($setting);
$this->settings->setSettingValue($setting, 2);
// make sure stored on the instance
$this->assertSettingHasValue($setting, 2);
$this->assertSettingIsNotSavedInTheDb('mysystem', null);
}
/**
* @expectedException \Exception
* @expectedExceptionMessage CoreAdminHome_PluginSettingsValueNotAllowed
*/
public function test_setSettingValue_shouldApplyValidationAndFail_IfOptionsAreSet()
{
$this->setUser();
$setting = $this->buildUserSetting('mysystem', 'mytitle');
$setting->availableValues = array('allowed' => 'text', 'allowed2' => 'text2');
Thomas Steur
a validé
$this->settings->addSetting($setting);
$this->settings->setSettingValue($setting, 'notallowed');
}
/**
* @expectedException \Exception
* @expectedExceptionMessage CoreAdminHome_PluginSettingsValueNotAllowed
*/
public function test_setSettingValue_shouldApplyValidationAndFail_IfOptionsAreSetAndValueIsAnArray()
{
$this->setUser();
$setting = $this->buildUserSetting('mysystem', 'mytitle');
$setting->availableValues = array('allowed' => 'text', 'allowed2' => 'text2');
$setting->uiControlType = PluginSettings::CONTROL_MULTI_SELECT;
Thomas Steur
a validé
$this->settings->addSetting($setting);
$this->settings->setSettingValue($setting, array('allowed', 'notallowed'));
}
public function test_setSettingValue_shouldApplyValidationAndSucceed_IfOptionsAreSet()
{
$this->setUser();
$setting = $this->buildUserSetting('mysystem', 'mytitle');
$setting->availableValues = array('allowed' => 'text', 'allowed2' => 'text2');
$setting->uiControlType = PluginSettings::CONTROL_MULTI_SELECT;
Thomas Steur
a validé
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
$this->settings->addSetting($setting);
$this->settings->setSettingValue($setting, array('allowed', 'allowed2'));
$this->assertSettingHasValue($setting, array('allowed', 'allowed2'));
$setting->type = PluginSettings::TYPE_STRING;
$this->settings->setSettingValue($setting, 'allowed');
$this->assertSettingHasValue($setting, 'allowed');
}
public function test_setSettingValue_shouldCastValue_IfTypeIsSetButNoFilter()
{
$this->setUser();
// cast to INT
$setting = $this->addUserSetting('mysystem', 'mytitle');
$setting->type = PluginSettings::TYPE_INT;
$this->settings->setSettingValue($setting, '31xm42');
$this->assertSettingHasValue($setting, 31, 'integer');
// ARRAY
$setting->type = PluginSettings::TYPE_ARRAY;
$this->settings->setSettingValue($setting, '31xm42');
$this->assertSettingHasValue($setting, array('31xm42'), 'array');
// BOOL
$setting->type = PluginSettings::TYPE_BOOL;
$this->settings->setSettingValue($setting, '1');
$this->assertSettingHasValue($setting, true, 'boolean');
// FLOAT
$setting->type = PluginSettings::TYPE_FLOAT;
$this->settings->setSettingValue($setting, '1.21');
$this->assertSettingHasValue($setting, 1.21, 'float');
// STRING
$setting->type = PluginSettings::TYPE_STRING;
$this->settings->setSettingValue($setting, '31xm42');
$this->assertSettingHasValue($setting, '31xm42');
}
public function test_setSettingValue_shouldApplyFilterAndNotCast_IfAFilterIsSet()
{
$this->setUser();
$setting = $this->buildUserSetting('mysystem', 'mytitle');
$setting->type = PluginSettings::TYPE_INT;
$self = $this;
$setting->transform = function ($value, $userSetting) use ($self, $setting) {
Thomas Steur
a validé
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
$self->assertEquals('31xm42', $value);
$self->assertEquals($setting, $userSetting);
return '43939kmf3m3';
};
$this->settings->addSetting($setting);
$this->settings->setSettingValue($setting, '31xm42');
// should not be casted to int
$this->assertSettingHasValue($setting, '43939kmf3m3', 'string');
}
public function test_getSettingValue_shouldReturnUncastedDefaultValue_IfNoValueIsSet()
{
$this->setUser();
$setting = $this->addUserSetting('mydefaultsystem', 'mytitle');
$setting->type = PluginSettings::TYPE_INT;
$setting->defaultValue ='mytestvalue';
// should not be casted to int
$this->assertSettingHasValue($setting, 'mytestvalue', 'string');
}
/**
* @expectedException \Exception
* @expectedExceptionMessage The setting myusersetting does not exist
*/
public function test_getSettingValue_shouldThrowException_IfGivenSettingDoesNotExist()
{
$setting = $this->buildUserSetting('myusersetting', 'mytitle');
$this->settings->getSettingValue($setting);
}
/**
* @expectedException \Exception
Thomas Steur
a validé
* @expectedExceptionMessage CoreAdminHome_PluginSettingReadNotAllowed
Thomas Steur
a validé
*/
public function test_getSettingValue_shouldThrowException_IfUserHasNotEnoughPermissionToReadValue()
{
$this->setUser();
$setting = $this->addSystemSetting('myusersetting', 'mytitle');
$this->settings->getSettingValue($setting);
}
Thomas Steur
a validé
public function test_getSettingValue_shouldReturnValue_IfReadbleByCurrentUserIsAllowed()
{
$this->setUser();
$setting = $this->addSystemSetting('myusersetting', 'mytitle');
$setting->readableByCurrentUser = true;
$this->assertEquals('', $this->settings->getSettingValue($setting));
}
Thomas Steur
a validé
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
public function test_getSettingValue_shouldReturnValue_IfValueExistsAndUserHasPermission()
{
$this->setUser();
$setting = $this->addUserSetting('myusersetting', 'mytitle');
$setting->type = PluginSettings::TYPE_ARRAY;
$this->settings->setSettingValue($setting, array(2,3,4));
$this->assertSettingHasValue($setting, array(2,3,4));
}
/**
* @expectedException \Exception
* @expectedExceptionMessage CoreAdminHome_PluginSettingChangeNotAllowed
*/
public function test_removeSettingValue_shouldThrowException_IfUserHasNotEnoughUserPermissions()
{
$setting = $this->addUserSetting('myusersetting', 'mytitle');
$this->settings->removeSettingValue($setting);
}
/**
* @expectedException \Exception
* @expectedExceptionMessage CoreAdminHome_PluginSettingChangeNotAllowed
*/
public function test_removeSettingValue_shouldThrowException_IfUserHasNotEnoughAdminPermissions()
{
$this->setUser();
$setting = $this->addSystemSetting('mysystemsetting', 'mytitle');
$this->settings->removeSettingValue($setting);
}
public function test_removeSettingValue_shouldRemoveValue_IfValueExistsAndHasEnoughPermissions()
{
$this->setUser();
$setting = $this->addUserSetting('myusersetting', 'mytitle');
$this->settings->setSettingValue($setting, '12345657');
$this->assertSettingHasValue($setting, '12345657');
$this->settings->removeSettingValue($setting);
$this->assertSettingHasValue($setting, null);
}
public function test_removeSettingValue_shouldRemoveValue_ShouldNotSaveValueInDb()
{
$this->setSuperUser();
$setting = $this->addSystemSetting('myusersetting', 'mytitle');
$this->settings->setSettingValue($setting, '12345657');
$this->settings->save();
$this->settings->removeSettingValue($setting);
$this->assertSettingHasValue($setting, null);
// should still have same value
$this->assertSettingIsNotSavedInTheDb('myusersetting', '12345657');
}
public function test_getSettingsForCurrentUser_shouldOnlyReturnSettingsHavingEnoughAdminPermissions()
{
$this->setUser();
$this->addSystemSetting('mysystemsetting1', 'mytitle1');
$this->addSystemSetting('mysystemsetting2', 'mytitle2');
$this->addSystemSetting('mysystemsetting3', 'mytitle3');
$this->addSystemSetting('mysystemsetting4', 'mytitle4');
$userSetting = $this->addUserSetting('myusersetting1', 'mytitle5');
$this->assertEquals(array('myusersetting1' => $userSetting), $this->settings->getSettingsForCurrentUser());
// but all of them should be available via getSettings()
$this->assertCount(5, $this->settings->getSettings());
}
public function test_getSettingsForCurrentUser_shouldReturnAllSettingsIfEnoughPermissionsAndSortThemBySettingOrder()
Thomas Steur
a validé
{
Thomas Steur
a validé
$this->setSuperUser();
$this->addSystemSetting('mysystemsetting1', 'mytitle1');
$this->addSystemSetting('mysystemsetting2', 'mytitle2');
$this->addUserSetting('myusersetting2', 'mytitle6');
Thomas Steur
a validé
$this->addSystemSetting('mysystemsetting3', 'mytitle3');
$this->addSystemSetting('mysystemsetting4', 'mytitle4');
$this->addUserSetting('myusersetting1', 'mytitle5');
$expected = array('myusersetting2', 'myusersetting1', 'mysystemsetting1', 'mysystemsetting2', 'mysystemsetting3', 'mysystemsetting4');
Thomas Steur
a validé
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
$this->assertEquals($expected, array_keys($this->settings->getSettingsForCurrentUser()));
}
public function test_save_shouldSaveAllValues()
{
$this->setSuperUser();
$this->addSystemSetting('mysystemsetting2', 'mytitle2');
$this->settings->setSettingValue($this->addSystemSetting('mysystemsetting1', 'mytitle1'), '111');
$this->settings->setSettingValue($this->addSystemSetting('mysystemsetting4', 'mytitle4'), '4444');
$this->settings->setSettingValue($this->addUserSetting('myusersetting1', 'mytitle5'), '55555');
$this->addSystemSetting('mysystemsetting3', 'mytitle3');
$this->settings->save();
$verifySettings = $this->createSettingsInstance();
$setting1 = $this->buildSystemSetting('mysystemsetting1', 'mytitle1');
$setting2 = $this->buildSystemSetting('mysystemsetting2', 'mytitle2');
$setting3 = $this->buildSystemSetting('mysystemsetting3', 'mytitle3');
$setting4 = $this->buildSystemSetting('mysystemsetting4', 'mytitle4');
$setting5 = $this->buildUserSetting('myusersetting1', 'mytitle5');
$verifySettings->addSetting($setting1);
$verifySettings->addSetting($setting2);
$verifySettings->addSetting($setting3);
$verifySettings->addSetting($setting4);
$verifySettings->addSetting($setting5);
$this->assertEquals('111', $verifySettings->getSettingValue($setting1));
$this->assertEquals(null, $verifySettings->getSettingValue($setting2));
$this->assertEquals(null, $verifySettings->getSettingValue($setting3));
$this->assertEquals('4444', $verifySettings->getSettingValue($setting4));
$this->assertEquals('55555', $verifySettings->getSettingValue($setting5));
}
public function test_removeAllPluginSettings_shouldRemoveAllSettings()
{
$this->setSuperUser();
$this->addSystemSetting('mysystemsetting3', 'mytitle3');
$this->addSystemSetting('mysystemsetting4', 'mytitle4');
$this->settings->setSettingValue($this->addSystemSetting('mysystemsetting1', 'mytitle1'), '111');
$this->settings->setSettingValue($this->addSystemSetting('mysystemsetting2', 'mytitle2'), '4444');
$this->settings->setSettingValue($this->addUserSetting('myusersetting1', 'mytitle5'), '55555');
$this->settings->save();
$this->settings->removeAllPluginSettings();
$verifySettings = $this->createSettingsInstance();
$setting1 = $this->buildSystemSetting('mysystemsetting1', 'mytitle1');
$setting2 = $this->buildSystemSetting('mysystemsetting2', 'mytitle2');
$setting3 = $this->buildSystemSetting('mysystemsetting3', 'mytitle3');
$setting4 = $this->buildSystemSetting('mysystemsetting4', 'mytitle4');
$setting5 = $this->buildUserSetting('myusersetting1', 'mytitle5');
$verifySettings->addSetting($setting1);
$verifySettings->addSetting($setting2);
$verifySettings->addSetting($setting3);
$verifySettings->addSetting($setting4);
$verifySettings->addSetting($setting5);
$this->assertEquals(null, $verifySettings->getSettingValue($setting1));
$this->assertEquals(null, $verifySettings->getSettingValue($setting2));
$this->assertEquals(null, $verifySettings->getSettingValue($setting3));
$this->assertEquals(null, $verifySettings->getSettingValue($setting4));
$this->assertEquals(null, $verifySettings->getSettingValue($setting5));
}
/**
* @expectedException \Exception
* @expectedExceptionMessage checkUserHasSuperUserAccess Fake exception
Thomas Steur
a validé
*/
public function test_removeAllPluginSettings_shouldThrowException_InCaseUserIsNotSuperUser()
{
$this->setUser();
$this->settings->removeAllPluginSettings();
}
public function test_userSetting_shouldGenerateDifferentKey_ThenSystemSetting()
{
$this->setSuperUser();
$user = $this->buildUserSetting('myname', 'mytitle');
$system = $this->buildSystemSetting('myname', 'mytitle');
$this->assertNotEquals($user->getKey(), $system->getKey());
$this->assertEquals('myname', $system->getKey());
$this->assertEquals('myname#superUserLogin#', $user->getKey());
}
Thomas Steur
a validé
public function test_userSetting_shouldGenerateDifferentKey_ForDifferentUsers()
Thomas Steur
a validé
{
Thomas Steur
a validé
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
$this->setSuperUser();
$user1 = $this->buildUserSetting('myname', 'mytitle', 'user1');
$user2 = $this->buildUserSetting('myname', 'mytitle', '_user2_');
$user3 = $this->buildUserSetting('myname', 'mytitle');
$this->assertEquals('myname#user1#', $user1->getKey());
$this->assertEquals('myname#_user2_#', $user2->getKey());
$this->assertEquals('myname#superUserLogin#', $user3->getKey());
}
public function test_userSetting_shouldSaveValuesPerUser()
{
$this->setSuperUser();
$user1Login = 'user1';
$user2Login = '_user2_';
$user3Login = null; // current loggged in user
$user = $this->buildUserSetting('myuser', 'mytitle', $user1Login);
$this->settings->addSetting($user);
$this->settings->setSettingValue($user, '111');
$user->setUserLogin($user2Login);
$this->settings->setSettingValue($user, '222');
$user->setUserLogin($user3Login);
$this->settings->setSettingValue($user, '333');
$user->setUserLogin($user1Login);
$this->assertSettingHasValue($user, '111');
$user->setUserLogin($user2Login);
$this->assertSettingHasValue($user, '222');
$user->setUserLogin($user3Login);
$this->assertSettingHasValue($user, '333');
$user->setUserLogin($user2Login);
$this->settings->removeSettingValue($user);
$user->setUserLogin($user1Login);
$this->assertSettingHasValue($user, '111');
$user->setUserLogin($user2Login);
$this->assertSettingHasValue($user, null);
$user->setUserLogin($user3Login);
$this->assertSettingHasValue($user, '333');
$this->settings->removeAllPluginSettings();
$user->setUserLogin($user1Login);
$this->assertSettingHasValue($user, null);
$user->setUserLogin($user2Login);
$this->assertSettingHasValue($user, null);
$user->setUserLogin($user3Login);
$this->assertSettingHasValue($user, null);
}
/**
* @expectedException \Exception
* @expectedExceptionMessage You do not have the permission to read the settings of a different user
*/
public function test_userSetting_shouldThrowException_IfSomeoneTriesToReadSettingsFromAnotherUserAndIsNotSuperuser()
{
$this->setUser();
$this->buildUserSetting('myname', 'mytitle', 'myRandomName');
}
Thomas Steur
a validé
public function test_userSetting_shouldBeAbleToSetLoginAndChangeValues_IfUserHasSuperUserAccess()
{
$this->setSuperUser();
$setting = $this->buildUserSetting('myname', 'mytitle', 'myRandomName');
$this->settings->addSetting($setting);
$this->settings->setSettingValue($setting, 5);
$this->assertSettingHasValue($setting, 5);
$this->settings->removeSettingValue($setting);
$this->assertSettingHasValue($setting, null);
}
Thomas Steur
a validé
public function test_construct_shouldDetectTheNameOfThePluginAutomatically_IfPluginNameNotGiven()
{
$setting = new \Piwik\Plugins\ExampleSettingsPlugin\Settings();
Thomas Steur
a validé
$this->assertEquals('ExampleSettingsPlugin', $setting->getPluginName());
}
Thomas Steur
a validé
private function buildUserSetting($name, $title, $userLogin = null)
{
return new \Piwik\Settings\UserSetting($name, $title, $userLogin);
Thomas Steur
a validé
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
}
private function buildSystemSetting($name, $title)
{
return new \Piwik\Settings\SystemSetting($name, $title);
}
private function setSuperUser()
{
$pseudoMockAccess = new FakeAccess;
FakeAccess::$superUser = true;
Access::setSingletonInstance($pseudoMockAccess);
}
private function setUser()
{
$pseudoMockAccess = new FakeAccess;
FakeAccess::$idSitesView = array(1);
Access::setSingletonInstance($pseudoMockAccess);
}
private function addSystemSetting($name, $title)
{
$setting = $this->buildSystemSetting($name, $title);
$this->settings->addSetting($setting);
return $setting;
}
private function addUserSetting($name, $title)
{
$setting = $this->buildUserSetting($name, $title);
$this->settings->addSetting($setting);
return $setting;
}
private function assertSettingHasValue($setting, $expectedValue, $expectedType = null)
{
$value = $this->settings->getSettingValue($setting);
$this->assertEquals($expectedValue, $value);
if (!is_null($expectedType)) {
$this->assertInternalType($expectedType, $value);
}
}
private function assertSettingIsNotSavedInTheDb($settingName, $expectedValue)
{
// by creating a new instance...
$setting = $this->buildSystemSetting($settingName, 'mytitle');
$verifySettings = $this->createSettingsInstance();
$verifySettings->addSetting($setting);
$this->assertEquals($expectedValue, $verifySettings->getSettingValue($setting));
}
private function createSettingsInstance()
{
return new CorePluginSettingsTest('ExampleSettingsPlugin');
Thomas Steur
a validé
}