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

InvalidLicensesTest.php « Plugins « Integration « tests « Marketplace « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 28c748dee0e97083ecc9941f1934149669198df2 (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
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
169
170
171
172
173
174
175
176
177
178
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\Marketplace\tests\Integration\Plugins;

use Piwik\Cache\Backend\ArrayCache;
use Piwik\Cache\Eager;
use Piwik\Container\StaticContainer;
use Piwik\Plugins\Marketplace\Consumer;
use Piwik\Plugins\Marketplace\Plugins;
use Piwik\Plugins\Marketplace\Plugins\InvalidLicenses;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Plugins\Marketplace\tests\Framework\Mock\Consumer as ConsumerBuilder;
use Piwik\Translate;

class CustomInvalidLicenses extends InvalidLicenses {
    private $isActivated = true;

    public function setPluginIsActivated($isActivated)
    {
        $this->isActivated = $isActivated;
    }

    public function isPluginActivated($pluginName)
    {
        return $this->isActivated;
    }
}

/**
 * @group Marketplace
 * @group InvalidLicensesTest
 * @group InvalidLicenses
 * @group Plugins
 */
class InvalidLicensesTest extends IntegrationTestCase
{
    /**
     * @var Eager
     */
    private $cache;

    private $cacheKey = 'Marketplace_ExpiredPlugins';

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

        Translate::loadEnglishTranslation();

        $this->cache = new Eager(new ArrayCache(), 'test');
    }

    public function tearDown()
    {
        Translate::unloadEnglishTranslation();
        parent::tearDown();
    }

    public function test_getNamesOfExpiredPaidPlugins_validLicenses_noPaidPluginActivated()
    {
        $expired = $this->buildWithValidLicense();
        $expired->setPluginIsActivated(false);

        $expected = array('exceeded' => array(), 'expired' => array(), 'noLicense' => array());

        $this->assertSame($expected, $expired->getPluginNamesOfInvalidLicenses());
    }

    public function test_getNamesOfExpiredPaidPlugins_noLicenses_noPaidPluginActivated()
    {
        $expired = $this->buildWithNoLicense();
        $expired->setPluginIsActivated(false);

        $expected = array(
            'exceeded' => array(),
            'expired' => array(),
            'noLicense' => array());

        $this->assertSame($expected, $expired->getPluginNamesOfInvalidLicenses());
    }

    public function test_getNamesOfExpiredPaidPlugins_invalidLicenses_noPaidPluginActivated()
    {
        $expired = $this->buildWithExpiredLicense();
        $expired->setPluginIsActivated(false);

        $expected = array(
            'exceeded' => array(),
            'expired' => array(),
            'noLicense' => array());

        $this->assertSame($expected, $expired->getPluginNamesOfInvalidLicenses());
    }

    public function test_getNamesOfExpiredPaidPlugins_exceededLicenses_noPaidPluginActivated()
    {
        $expired = $this->buildWithExceededLicense();
        $expired->setPluginIsActivated(false);

        $expected = array('exceeded' => array(), 'expired' => array(), 'noLicense' => array());

        $this->assertSame($expected, $expired->getPluginNamesOfInvalidLicenses());
    }

    public function test_getNamesOfExpiredPaidPlugins_validLicenses()
    {
        $expired = $this->buildWithValidLicense();

        $expected = array('exceeded' => array(), 'expired' => array(), 'noLicense' => array());

        $this->assertSame($expected, $expired->getPluginNamesOfInvalidLicenses());
    }

    public function test_getNamesOfExpiredPaidPlugins_noLicenses()
    {
        $expired = $this->buildWithNoLicense();

        $expected = array(
            'exceeded' => array(),
            'expired' => array(),
            'noLicense' => array('PaidPlugin1'));

        $this->assertSame($expected, $expired->getPluginNamesOfInvalidLicenses());
    }

    public function test_getNamesOfExpiredPaidPlugins_invalidLicenses()
    {
        $expired = $this->buildWithExpiredLicense();

        $expected = array(
            'exceeded' => array(),
            'expired' => array('PaidPlugin1'),
            'noLicense' => array());

        $this->assertSame($expected, $expired->getPluginNamesOfInvalidLicenses());
    }

    public function test_getNamesOfExpiredPaidPlugins_exceededLicenses()
    {
        $expired = $this->buildWithExceededLicense();

        $expected = array(
            'exceeded' => array('PaidPlugin2'),
            'expired' => array('PaidPlugin1'),
            'noLicense' => array());
        $this->assertEquals($expected, $expired->getPluginNamesOfInvalidLicenses());
    }

    public function test_getNamesOfExpiredPaidPlugins_shouldCacheAnyResult()
    {
        $this->assertFalse($this->cache->contains($this->cacheKey));

        $this->buildWithValidLicense()->getPluginNamesOfInvalidLicenses();

        $this->assertTrue($this->cache->contains($this->cacheKey));

        $expected = array('exceeded' => array(), 'expired' => array(), 'noLicense' => array());

        $this->assertSame($expected, $this->cache->fetch($this->cacheKey));
    }

    public function test_getNamesOfExpiredPaidPlugins_shouldCache_IfNotValidLicenseKeyButPaidPluginsInstalled()
    {
        $this->buildWithExpiredLicense()->getPluginNamesOfInvalidLicenses();

        $expected = array(
            'exceeded' => array(),
            'expired' => array('PaidPlugin1'),
            'noLicense' => array());

        $this->assertSame($expected, $this->cache->fetch($this->cacheKey));
    }

    public function test_getMessageExceededLicenses_getMessageExpiredLicenses_validLicenses_noPaidPluginActivated()
    {
        $expired = $this->buildWithValidLicense();
        $expired->setPluginIsActivated(false);

        $this->assertNull($expired->getMessageExceededLicenses());
        $this->assertNull($expired->getMessageExpiredLicenses());
        $this->assertNull($expired->getMessageNoLicense());
    }

    public function test_getMessageExceededLicenses_getMessageExpiredLicenses_invalidLicenses_noPaidPluginActivated()
    {
        $expired = $this->buildWithExpiredLicense();
        $expired->setPluginIsActivated(false);

        $this->assertNull($expired->getMessageExceededLicenses());
        $this->assertNull($expired->getMessageExpiredLicenses());
        $this->assertNull($expired->getMessageNoLicense());
    }

    public function test_getMessageExceededLicenses_getMessageExpiredLicenses_exceededLicenses_noPaidPluginActivated()
    {
        $expired = $this->buildWithExceededLicense();
        $expired->setPluginIsActivated(false);
        $this->assertNull($expired->getMessageExceededLicenses());
        $this->assertNull($expired->getMessageExpiredLicenses());
        $this->assertNull($expired->getMessageNoLicense());
    }

    public function test_getMessageExceededLicenses_getMessageExpiredLicenses_validLicenses_PaidPluginActivated()
    {
        $expired = $this->buildWithValidLicense();

        $this->assertNull($expired->getMessageExceededLicenses());
        $this->assertNull($expired->getMessageExpiredLicenses());
        $this->assertNull($expired->getMessageNoLicense());
    }

    public function test_getMessageExceededLicenses_getMessageExpiredLicenses_noLicenses_PaidPluginActivated()
    {
        // in theory we would need to show a warning as there is no license, but this can also happen if there's some random
        // error and the user actually has a license, eg if the request aborted when fetching consumer etc
        $expired = $this->buildWithNoLicense();

        $this->assertEquals('', $expired->getMessageExceededLicenses());
        $this->assertEquals('', $expired->getMessageExpiredLicenses());
        $this->assertEquals('The following plugins have been deactivated because you are using them without a license: <strong>PaidPlugin1</strong>. <br/>To resolve this issue either update your license key, <strong><a href="https://shop.piwik.org/my-account" target="_blank" rel="noreferrer">get a subscription now</a></strong> or deactivate the plugin. <br/><a href="?module=Marketplace&action=subscriptionOverview">View your plugin subscriptions.</a>', $expired->getMessageNoLicense());
    }

    public function test_getMessageExceededLicenses_getMessageExpiredLicenses_invalidLicenses_PaidPluginActivated()
    {
        $expired = $this->buildWithExpiredLicense();

        $this->assertNull($expired->getMessageExceededLicenses());
        $this->assertEquals('The licenses for the following plugins are expired: <strong>PaidPlugin1</strong>. <br/>You will no longer receive any updates for these plugins. To resolve this issue either <strong><a href="https://shop.piwik.org/my-account" target="_blank" rel="noreferrer">renew your subscription now</a></strong>, or deactivate the plugin if you no longer use it. <br/><a href="?module=Marketplace&action=subscriptionOverview">View your plugin subscriptions.</a>', $expired->getMessageExpiredLicenses());
    }

    public function test_getMessageExceededLicenses_getMessageExpiredLicenses_exceededLicenses_PaidPluginActivated()
    {
        $expired = $this->buildWithExceededLicense();
        $this->assertEquals('The licenses for the following plugins are no longer valid as the number of authorized users for the license is exceeded: <strong>PaidPlugin2</strong>. <br/>You will not be able to download updates for these plugins. To resolve this issue either delete some users or <strong><a href="https://shop.piwik.org/my-account" target="_blank" rel="noreferrer">upgrade the subscription now</a></strong>. <br/><a href="?module=Marketplace&action=subscriptionOverview">View your plugin subscriptions.</a>', $expired->getMessageExceededLicenses());
        $this->assertEquals('The licenses for the following plugins are expired: <strong>PaidPlugin1</strong>. <br/>You will no longer receive any updates for these plugins. To resolve this issue either <strong><a href="https://shop.piwik.org/my-account" target="_blank" rel="noreferrer">renew your subscription now</a></strong>, or deactivate the plugin if you no longer use it. <br/><a href="?module=Marketplace&action=subscriptionOverview">View your plugin subscriptions.</a>', $expired->getMessageExpiredLicenses());
    }

    public function test_getMessageMissingLicenses_getMessageMissingLicenses_PaidPluginActivated()
    {
        $expired = $this->buildWithNoLicense();
        $this->assertEquals('The following plugins have been deactivated because you are using them without a license: <strong>PaidPlugin1</strong>. <br/>To resolve this issue either update your license key, <strong><a href="https://shop.piwik.org/my-account" target="_blank" rel="noreferrer">get a subscription now</a></strong> or deactivate the plugin. <br/><a href="?module=Marketplace&action=subscriptionOverview">View your plugin subscriptions.</a>', $expired->getMessageNoLicense());
    }

    private function buildWithValidLicense()
    {
        $consumer = ConsumerBuilder::buildValidLicense();
        return $this->buildInvalidLicense($consumer);
    }

    private function buildWithExpiredLicense()
    {
        $consumer = ConsumerBuilder::buildExpiredLicense();
        return $this->buildInvalidLicense($consumer);
    }

    private function buildWithNoLicense()
    {
        $consumer = ConsumerBuilder::buildNoLicense();
        return $this->buildInvalidLicense($consumer);
    }

    private function buildWithExceededLicense()
    {
        $consumer = ConsumerBuilder::buildExceededLicense();
        return $this->buildInvalidLicense($consumer);
    }

    /**
     * @param Consumer $consumer
     * @return CustomInvalidLicenses
     */
    private function buildInvalidLicense($consumer)
    {
        $translator = StaticContainer::get('Piwik\Translation\Translator');
        $advertising = StaticContainer::get('Piwik\ProfessionalServices\Advertising');
        $client = $consumer->getApiClient();
        $plugins = new Plugins($client, $consumer, $advertising);

        $licenses = new CustomInvalidLicenses($client, $this->cache, $translator, $plugins);
        $licenses->clearCache();
        return $licenses;
    }

}