Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ApiTest.php « Integration « tests « CorePluginsAdmin « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b497f999d41e438b8f97d7ac71915b170ddcf266 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\CorePluginsAdmin\tests\Integration;

use Piwik\Access;
use Piwik\Auth;
use Piwik\Container\StaticContainer;
use Piwik\Piwik;
use Piwik\Plugins\CoreUpdater\SystemSettings;
use Piwik\Plugins\UsersManager\API;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Version;

class ApiTest extends IntegrationTestCase
{
    const TEST_USER = 'atestuser';
    const TEST_PASSWORD = 'testpassword';

    private $testSystemSettingsPayload = [
        'CoreUpdater' => [
            ['name' => 'release_channel', 'value' => 'latest_beta'],
        ],
    ];

    protected static function beforeTableDataCached()
    {
        parent::beforeTableDataCached();

        API::getInstance()->addUser(self::TEST_USER, self::TEST_PASSWORD, 'someuser@email.com');
        API::getInstance()->setSuperUserAccess(self::TEST_USER, true, Fixture::ADMIN_USER_PASSWORD);
    }

    public function setUp(): void
    {
        parent::setUp();

        Access::getInstance()->setSuperUserAccess(false);
        $auth = StaticContainer::get(Auth::class);
        $auth->setLogin(self::TEST_USER);
        $auth->setPassword(self::TEST_PASSWORD);
        Access::getInstance()->reloadAccess($auth);
    }

    public function test_setSystemSettings_throwsIfNoPasswordConfirmation()
    {
        if (version_compare(Version::VERSION, '4.4.0-b1', '<')) {
            $this->markTestSkipped('Skipping test since passwordConfirmation is optional until version 4.4.');
        }

        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('UsersManager_ConfirmWithPassword');

        $settingValues = $this->testSystemSettingsPayload;
        \Piwik\Plugins\CorePluginsAdmin\API::getInstance()->setSystemSettings($settingValues);
    }

    public function test_setSystemSettings_throwsIfPasswordConfirmationWrong()
    {
        if (version_compare(Version::VERSION, '4.4.0-b1', '<')) {
            $this->markTestSkipped('Skipping test since passwordConfirmation is optional until version 4.4.');
        }

        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('UsersManager_CurrentPasswordNotCorrect');

        $settingValues = $this->testSystemSettingsPayload;
        \Piwik\Plugins\CorePluginsAdmin\API::getInstance()->setSystemSettings($settingValues, 'blahblah');
    }

    public function test_setSystemSettings_correctlySetsSettings()
    {
        $settingValues = $this->testSystemSettingsPayload;
        \Piwik\Plugins\CorePluginsAdmin\API::getInstance()->setSystemSettings($settingValues, self::TEST_PASSWORD);

        $coreUpdaterSettings = StaticContainer::get(SystemSettings::class);
        $value = $coreUpdaterSettings->releaseChannel->getValue();
        $this->assertEquals('latest_beta', $value);
    }

    protected static function configureFixture($fixture)
    {
        parent::configureFixture($fixture);
        $fixture->createSuperUser = true;
    }
}