File size: 5,443 Bytes
1f0da20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?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\CustomVariables\tests\Commands;

use Piwik\Plugins\CustomVariables\Commands\SetNumberOfCustomVariables;
use Piwik\Plugins\CustomVariables\CustomVariables;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

/**
 * @group CustomVariables
 * @group CustomVariablesTest
 * @group Plugins
 * @group Plugins
 */
class SetNumberOfCustomVariablesTest extends IntegrationTestCase
{
    public function testExecuteShouldThrowExceptionIfArgumentIsMissing()
    {
        $this->expectException(\RuntimeException::class);
        $this->expectExceptionMessage('Not enough arguments');

        $this->executeCommand(null);
    }

    public function testExecuteShouldThrowExceptionHasToBeANumber()
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('The number of available custom variables has to be a number');

        $this->executeCommand('a');
    }

    public function testExecuteShouldThrowExceptionMinimum2CustomVarsRequired()
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('There has to be at least five custom variables');

        $this->executeCommand(4);
    }

    public function testExecuteShouldThrowExceptionIfUserCancelsConfirmation()
    {
        $result = $this->executeCommand(7, false);
        $this->assertStringEndsWith('Are you sure you want to perform these actions? (y/N)', $result);
    }

    public function testExecuteShouldDoNothingIfExpectedResultIsAlreadyTheCase()
    {
        $result = $this->executeCommand(5);

        self::assertStringContainsString('Your Matomo is already configured for 5 custom variables', $result);
    }

    public function testExecuteShouldAddMaxCustomVarsIfNumberIsHigherThanActual()
    {
        $this->assertEquals(5, CustomVariables::getNumUsableCustomVariables());

        $result = $this->executeCommand(6);

        self::assertStringContainsString('Configuring Matomo for 6 custom variables', $result);
        self::assertStringContainsString('1 new custom variables having the index(es) 6 will be ADDED', $result);
        self::assertStringContainsString('Starting to apply changes', $result);
        self::assertStringContainsString('Added a variable in scope "Page" having the index 6', $result);
        self::assertStringContainsString('Added a variable in scope "Visit" having the index 6', $result);
        self::assertStringContainsString('Added a variable in scope "Conversion" having the index 6', $result);
        self::assertStringContainsString('Your Matomo is now configured for 6 custom variables.', $result);

        $this->assertEquals(6, CustomVariables::getNumUsableCustomVariables());
    }

    public function testExecuteShouldRemoveMaxCustomVarsIfNumberIsLessThanActual()
    {
        $this->executeCommand(6, true);
        $this->assertEquals(6, CustomVariables::getNumUsableCustomVariables());

        $result = $this->executeCommand(5);

        self::assertStringContainsString('Configuring Matomo for 5 custom variables', $result);
        self::assertStringContainsString('1 existing custom variables having the index(es) 6 will be REMOVED.', $result);
        self::assertStringContainsString('Starting to apply changes', $result);
        self::assertStringContainsString('Removed a variable in scope "Page" having the index 6', $result);
        self::assertStringContainsString('Removed a variable in scope "Visit" having the index 6', $result);
        self::assertStringContainsString('Removed a variable in scope "Conversion" having the index 6', $result);
        self::assertStringContainsString('Your Matomo is now configured for 5 custom variables.', $result);

        $this->assertEquals(5, CustomVariables::getNumUsableCustomVariables());
    }

    public function testExecuteAddMultipleRemoveMultiple()
    {
        $this->assertEquals(5, CustomVariables::getNumUsableCustomVariables());

        $this->executeCommand(9);
        $this->assertEquals(9, CustomVariables::getNumUsableCustomVariables());

        $this->executeCommand(6);
        $this->assertEquals(6, CustomVariables::getNumUsableCustomVariables());
    }

    /**
     * @param int|null $maxCustomVars
     * @param bool  $confirm
     *
     * @return string
     */
    private function executeCommand($maxCustomVars, $confirm = true)
    {
        $setNumberCmd = new SetNumberOfCustomVariables();

        $application = new Application();
        $application->add($setNumberCmd);

        $commandTester = new CommandTester($setNumberCmd);
        $commandTester->setInputs([($confirm ? 'yes' : 'no') . '\n']);

        if (is_null($maxCustomVars)) {
            $params = [];
        } else {
            $params = ['maxCustomVars' => $maxCustomVars];
        }

        $params['command'] = $setNumberCmd->getName();
        $commandTester->execute($params);
        $result = $commandTester->getDisplay();

        return $result;
    }

    protected function getInputStream($input)
    {
        $stream = fopen('php://memory', 'r+', false);
        fputs($stream, $input);
        rewind($stream);

        return $stream;
    }
}