File: ConfigSettingManipulationTest.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (168 lines) | stat: -rw-r--r-- 5,595 bytes parent folder | download
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?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\Plugins\CoreAdminHome\tests\Unit\Commands\SetConfig;

use Piwik\Config;
use Piwik\Plugins\CoreAdminHome\Commands\SetConfig\ConfigSettingManipulation;

// phpunit mocks can't return references, so we need a manual one
class DumbMockConfig extends \Piwik\Config
{
    /**
     * @var array
     */
    public $mockConfigData;

    public function __construct()
    {
        // empty
    }

    public function &__get($sectionName)
    {
        if (!isset($this->mockConfigData[$sectionName])) {
            $this->mockConfigData[$sectionName] = array();
        }

        $result =& $this->mockConfigData[$sectionName];
        return $result;
    }

    public function __set($sectionName, $section)
    {
        $this->mockConfigData[$sectionName] = $section;
    }
}

/**
 * @group CoreAdminHome
 * @group CoreAdminHome_Unit
 */
class ConfigSettingManipulationTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var Config
     */
    private $mockConfig;

    public function setUp(): void
    {
        $this->mockConfig = new DumbMockConfig();
    }

    /**
     * @dataProvider getTestDataForMake
     */
    public function testMakeCreatesCorrectManipulation(
        $assignmentString,
        $expectedSectionName,
        $expectedSettingName,
        $expectedSettingValue,
        $expectedIsArrayAppend
    ) {
        $manipulation = ConfigSettingManipulation::make($assignmentString);

        $this->assertEquals($expectedSectionName, $manipulation->getSectionName());
        $this->assertEquals($expectedSettingName, $manipulation->getName());
        $this->assertEquals($expectedSettingValue, $manipulation->getValue());
        $this->assertEquals($expectedIsArrayAppend, $manipulation->isArrayAppend());
    }

    public function getTestDataForMake()
    {
        return array(
            // normal assign
            array("General.myconfig=0", "General", "myconfig", 0, false),

            // array append
            array("General.myconfig444[]=5", "General", "myconfig444", 5, true),

            // assign array
            array("1General1.2config2=[\"abc\",\"def\"]", "1General1", "2config2", array('abc', 'def'), false),

            // assign string
            array("MySection.value=\"ghi\"", "MySection", "value", "ghi", false),

            // assign boolean
            array("MySection.value=false", "MySection", "value", false, false),
            array("MySection.value=true", "MySection", "value", true, false),
        );
    }

    /**
     * @dataProvider getFailureTestDataForMake
     */
    public function testMakeThrowsWhenInvalidAssignmentStringSupplied($assignmentString)
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('Invalid assignment string');

        ConfigSettingManipulation::make($assignmentString);
    }

    public function getFailureTestDataForMake()
    {
        return array(
            array("General&.value=1"),
            array("General.val&*ue=12"),
            array("General.value=[notjson]"),
            array("General.value=notjson"),
            array("General.array[abc]=\"def\""),
        );
    }

    public function testManipulateThrowsIfAppendingNonArraySetting()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('Trying to append to non-array setting value');

        $this->mockConfig->mockConfigData['General']['config'] = "5";

        $manipulation = new ConfigSettingManipulation("General", "config", "10", true);
        $manipulation->manipulate($this->mockConfig);
    }

    public function testManipulateThrowsIfAssigningNonArrayValueToArraySetting()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('Trying to set non-array value to array setting');

        $this->mockConfig->mockConfigData['General']['config'] = array("5");

        $manipulation = new ConfigSettingManipulation("General", "config", "10", false);
        $manipulation->manipulate($this->mockConfig);
    }

    /**
     * @dataProvider getTestDataForManipulate
     */
    public function testManipulateCorrectlyManipulatesConfig($sectionName, $name, $value, $isArrayAppend, $expectedConfig)
    {
        $manipulation = new ConfigSettingManipulation($sectionName, $name, $value, $isArrayAppend);
        $manipulation->manipulate($this->mockConfig);

        $this->assertEquals($expectedConfig, $this->mockConfig->mockConfigData);
    }

    public function getTestDataForManipulate()
    {
        return array(
            // normal assign (string, int, array, bool)
            array("Section", "config_setting", "stringvalue", false, array("Section" => array("config_setting" => "stringvalue"))),
            array("Section", "config_setting", 25, false, array("Section" => array("config_setting" => 25))),
            array("Section", "config_setting", array('a' => 'b'), false, array("Section" => array("config_setting" => array('a' => 'b')))),
            array("Section", "config_setting", false, false, array("Section" => array("config_setting" => false))),

            // array append
            array("Section", "config_setting", "value", true, array("Section" => array("config_setting" => array('value')))),
            array("Section", "config_setting", array(1,2), true, array("Section" => array("config_setting" => array(array(1,2))))),
        );
    }
}