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
|
<?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\Marketplace\tests\Unit;
use Piwik\Plugins\Marketplace\tests\Framework\Mock\Consumer;
use Piwik\Plugins\Marketplace\tests\Framework\Mock\Service;
use Piwik\Plugins\Marketplace\tests\Framework\Mock\Consumer as ConsumerBuilder;
/**
* @group Marketplace
* @group ConsumerTest
* @group Consumer
* @group Plugins
*/
class ConsumerTest extends \PHPUnit\Framework\TestCase
{
/**
* @var Service
*/
private $service;
public function setUp(): void
{
$this->service = new Service();
}
/**
* @dataProvider getConsumerNotAuthenticated
*/
public function test_isValidConsumer_shouldReturnFalse_WhenNotAuthenticedBecauseNoTokenSetOrInvalidToken($fixture)
{
$this->service->returnFixture($fixture);
$this->assertFalse($this->buildConsumer()->isValidConsumer());
}
/**
* @dataProvider getConsumerAuthenticated
*/
public function test_isValidConsumer_shouldReturnTrue_WhenValidTokenGiven($fixture)
{
$this->service->returnFixture($fixture);
$this->assertTrue($this->buildConsumer()->isValidConsumer());
}
public function test_getConsumer_shouldReturnConsumerInformation_WhenValid()
{
$this->service->returnFixture('v2.0_consumer-access_token-consumer1_paid2_custom1.json');
$expected = array (
'licenses' =>
array (
0 =>
array (
'startDate' => '2014-05-27 04:46:05',
'endDate' => '2014-06-01 06:22:35',
'nextPaymentDate' => NULL,
'status' => 'Cancelled',
'productType' => 'Up to 4 users',
'isValid' => false,
'isExceeded' => false,
'isExpiredSoon' => false,
'plugin' => array('name' => 'PaidPlugin1', 'displayName' => 'Paid Plugin 1', 'htmlUrl' => 'https://plugins.piwik.org/PaidPlugin1'),
),
1 =>
array (
'startDate' => '2016-05-20 04:46:05',
'endDate' => '2030-05-27 11:03:06',
'nextPaymentDate' => '2030-05-27 11:03:06',
'status' => 'Active',
'productType' => '5 to 15 users',
'isValid' => true,
'isExceeded' => NULL,
'isExpiredSoon' => false,
'plugin' => array('name' => 'PaidPlugin2', 'displayName' => 'Paid Plugin 2', 'htmlUrl' => 'https://plugins.piwik.org/PaidPlugin2'),
),
2 =>
array (
'startDate' => '2016-05-25 04:46:05',
'endDate' => '2030-06-03 11:03:06',
'nextPaymentDate' => '2030-06-03 11:03:06',
'status' => 'Active',
'productType' => 'Up to 4 users',
'isValid' => true,
'isExceeded' => NULL,
'isExpiredSoon' => false,
'plugin' => array('name' => 'CustomPlugin1', 'displayName' => 'Custom Plugin 1', 'htmlUrl' => ''),
),
),
'loginUrl' => 'https://shop.piwik.org/my-account',
);
$this->assertEquals($expected, $this->buildConsumer()->getConsumer());
}
public function test_getConsumer_shouldNotReturnAnyInformationWhenNotAuthenticated()
{
$this->service->returnFixture('v2.0_consumer-access_token-notexistingtoken.json');
$this->assertSame(array(), $this->buildConsumer()->getConsumer());
}
public function test_getConsumer_shouldNotReturnInformationWhenAuthenticatedButNoLicense()
{
$this->service->returnFixture('v2.0_consumer-access_token-validbutnolicense.json');
$expected = array(
'licenses' => array(),
'loginUrl' => 'https://shop.piwik.org/my-account'
);
$this->assertSame($expected, $this->buildConsumer()->getConsumer());
}
public function getConsumerNotAuthenticated()
{
return array(
array('v2.0_consumer_validate.json'),
array('v2.0_consumer_validate-access_token-notexistingtoken.json'),
);
}
public function getConsumerAuthenticated()
{
return array(
array('v2.0_consumer_validate-access_token-consumer1_paid2_custom1.json'),
array('v2.0_consumer_validate-access_token-consumer2_paid1.json'),
array('v2.0_consumer_validate-access_token-validbutnolicense.json') // valid token but no license
);
}
public function test_buildInvalidLicenseKey()
{
$isValid = Consumer::buildNoLicense()->isValidConsumer();
$this->assertFalse($isValid);
}
public function test_buildValidLicenseKey()
{
$isValid = Consumer::buildValidLicense()->isValidConsumer();
$this->assertTrue($isValid);
}
private function buildConsumer()
{
return ConsumerBuilder::build($this->service);
}
}
|