File: RemoveCustomDimensionTest.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 (156 lines) | stat: -rw-r--r-- 6,164 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
<?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\CustomDimensions\tests\Commands;

use Piwik\Plugins\CustomDimensions\Commands\RemoveCustomDimension;
use Piwik\Plugins\CustomDimensions\CustomDimensions;
use Piwik\Plugins\CustomDimensions\Dao\LogTable;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

/**
 * @group CustomDimensions
 * @group CustomDimensionsTest
 * @group Plugins
 * @group Plugins
 */
class RemoveCustomDimensionTest extends IntegrationTestCase
{
    public function testExecuteShouldThrowExceptionIfArgumentIsMissing()
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('The specified scope is invalid. Use either');

        $this->executeCommand(null, null);
    }

    public function testExecuteShouldThrowExceptionIfScopeIsInvalid()
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('The specified scope is invalid. Use either "--scope=visit" or "--scope=action"');

        $this->executeCommand('invalidscope', null);
    }

    public function testExecuteShouldThrowExceptionIfIndexIsNotSpecified()
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('An option "index" must be specified');

        $this->executeCommand(CustomDimensions::SCOPE_VISIT, null);
    }

    public function testExecuteShouldThrowExceptionIfIndexIsNotANumber()
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('Option "index" must be a number');

        $this->executeCommand(CustomDimensions::SCOPE_VISIT, '545fddfd');
    }

    public function testExecuteShouldThrowExceptionIfCountIsLessThanONe()
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('Specified index is not installed');

        $this->executeCommand(CustomDimensions::SCOPE_VISIT, '14');
    }

    public function testExecuteShouldThrowExceptionIfUserCancelsConfirmation()
    {
        $result = $this->executeCommand(CustomDimensions::SCOPE_VISIT, $index = 5, false);
        $this->assertStringEndsWith('Are you sure you want to perform this action? (y/N)', $result);
    }

    public function testExecuteShouldAddSpecifiedCount()
    {
        $logVisit = new LogTable(CustomDimensions::SCOPE_VISIT);
        $this->assertSame(range(1, 5), $logVisit->getInstalledIndexes());

        $logConversion = new LogTable(CustomDimensions::SCOPE_CONVERSION);
        $this->assertSame(range(1, 5), $logConversion->getInstalledIndexes());

        $logAction = new LogTable(CustomDimensions::SCOPE_ACTION);
        $this->assertSame(range(1, 5), $logAction->getInstalledIndexes());

        $result = $this->executeCommand(CustomDimensions::SCOPE_ACTION, $index = 3);

        self::assertStringContainsString('Remove Custom Dimension at index 3 in scope action.', $result);
        self::assertStringContainsString('Are you sure you want to perform this action?', $result);
        self::assertStringContainsString('Starting to remove this Custom Dimension', $result);
        self::assertStringContainsString('Your Matomo is now configured for up to 4 Custom Dimensions in scope action.', $result);

        $logVisit = new LogTable(CustomDimensions::SCOPE_VISIT);
        $this->assertSame(range(1, 5), $logVisit->getInstalledIndexes());

        $logConversion = new LogTable(CustomDimensions::SCOPE_CONVERSION);
        $this->assertSame(range(1, 5), $logConversion->getInstalledIndexes());

        $logAction = new LogTable(CustomDimensions::SCOPE_ACTION);
        $this->assertSame([1,2,4,5], $logAction->getInstalledIndexes());
    }

    public function testExecuteShouldAddSpecifiedCountIfScopeIsVisitShouldAlsoUpdateConversion()
    {
        $logVisit = new LogTable(CustomDimensions::SCOPE_VISIT);
        $this->assertSame(range(1, 5), $logVisit->getInstalledIndexes());

        $logConversion = new LogTable(CustomDimensions::SCOPE_CONVERSION);
        $this->assertSame(range(1, 5), $logConversion->getInstalledIndexes());

        $result = $this->executeCommand(CustomDimensions::SCOPE_VISIT, $index = 2);

        self::assertStringContainsString('Remove Custom Dimension at index 2 in scope visit', $result);
        self::assertStringContainsString('Are you sure you want to perform this action?', $result);
        self::assertStringContainsString('Starting to remove this Custom Dimension', $result);
        self::assertStringContainsString('Your Matomo is now configured for up to 4 Custom Dimensions in scope visit.', $result);

        $logVisit = new LogTable(CustomDimensions::SCOPE_VISIT);
        $this->assertSame([1,3,4,5], $logVisit->getInstalledIndexes());

        $logConversion = new LogTable(CustomDimensions::SCOPE_CONVERSION);
        $this->assertSame([1,3,4,5], $logConversion->getInstalledIndexes());

        $logAction = new LogTable(CustomDimensions::SCOPE_ACTION);
        $this->assertSame([1,2,4,5], $logAction->getInstalledIndexes());
    }

    /**
     * @param string|null $scope
     * @param int|null $index
     * @param bool  $confirm
     *
     * @return string
     */
    private function executeCommand($scope, $index, $confirm = true)
    {
        $removeCustomDimension = new RemoveCustomDimension();

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

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

        $params = [];
        if (!is_null($scope)) {
            $params['--scope'] = $scope;
        }

        if (!is_null($index)) {
            $params['--index'] = $index;
        }

        $params['command'] = $removeCustomDimension->getName();
        $commandTester->execute($params);
        return $commandTester->getDisplay();
    }
}