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
|
<?php
/**
* Piwik - 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\Integration;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\Option;
use Piwik\Plugins\CoreUpdater\SystemSettings;
use Piwik\Plugins\Marketplace\UpdateCommunication;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\View;
/**
* @group Plugins
* @group Marketplace
*/
class UpdateCommunicationTest extends IntegrationTestCase
{
/**
* @var UpdateCommunication
*/
private $updateCommunication;
/**
* @var SystemSettings
*/
private $settings;
public function setUp(): void
{
parent::setUp();
$this->settings = StaticContainer::get('Piwik\Plugins\CoreUpdater\SystemSettings');
$this->settings->sendPluginUpdateEmail->setValue(true);
$this->updateCommunication = new UpdateCommunication($this->settings);
}
public function test_canBeEnabled()
{
$this->assertTrue(UpdateCommunication::canBeEnabled());
Config::getInstance()->General['enable_update_communication'] = 0;
$this->assertFalse(UpdateCommunication::canBeEnabled());
Config::getInstance()->General['enable_update_communication'] = 1;
$this->assertTrue(UpdateCommunication::canBeEnabled());
}
public function test_isEnabled_shouldReturnFalse_IfCannotBeEnabled()
{
$this->assertTrue($this->updateCommunication->isEnabled());
Config::getInstance()->General['enable_update_communication'] = 0;
$this->assertFalse($this->updateCommunication->isEnabled());
}
public function test_sendNotificationIfUpdatesAvailable_shouldNotSendNotification_IfNoUpdateAvailable()
{
$mock = $this->getCommunicationMock(array());
$mock->expects($this->never())->method('sendEmailNotification');
$mock->sendNotificationIfUpdatesAvailable();
}
/**
* @dataProvider provideSendNotificationData
*/
public function test_sendNotificationIfUpdatesAvailable($latestVersion, $lastSentVersion, $expects, $expectedLastSentVersion)
{
$pluginsHavingUpdate = array(
array('name' => 'MyTest', 'latestVersion' => $latestVersion, 'isTheme' => false)
);
$this->setLastSentVersion('MyTest', $lastSentVersion);
$mock = $this->getCommunicationMock($pluginsHavingUpdate);
$mock->expects($expects)->method('sendEmailNotification');
$mock->sendNotificationIfUpdatesAvailable();
$this->assertEquals($expectedLastSentVersion, $this->getLastSentVersion('MyTest'));
}
public function provideSendNotificationData()
{
return array(
array('33.0.0', '33.0.0', $this->never(), '33.0.0'), // shouldNotSend_IfAlreadyNotified
array('31.0.0', '33.0.0', $this->never(), '33.0.0'), // shouldNotSend_IfAlreadyNotifiedAboutLaterRelease
array('33.0.0', false, $this->once(), '33.0.0'), // shouldSend_IfUpdateAvailableAndNeverSentAnyBefore
array('33.0.0', '31.0.0', $this->once(), '33.0.0'), // shouldSend_IfUpdateAvailable,
);
}
public function test_sendNotificationIfUpdatesAvailable_ShouldSendOnlyOneEmail_IfMultipleUpdatesAreAvailable()
{
$mock = $this->getCommunicationMockHavingManyUpdates();
$mock->expects($this->once())->method('sendEmailNotification');
$mock->sendNotificationIfUpdatesAvailable();
}
public function test_sendNotificationIfUpdatesAvailable_ShouldUpdateAllSentVersions_IfMultipleUpdatesAreAvailable()
{
$mock = $this->getCommunicationMockHavingManyUpdates();
$mock->expects($this->once())->method('sendEmailNotification');
$mock->sendNotificationIfUpdatesAvailable();
$this->assertEquals('33.0.0', $this->getLastSentVersion('MyTest1'));
$this->assertEquals('32.0.0', $this->getLastSentVersion('MyTest2'));
$this->assertEquals('31.0.0', $this->getLastSentVersion('MyTest3'));
}
public function test_sendNotificationIfUpdatesAvailable_ShouldSendCorrectText()
{
$subject = 'CoreUpdater_NotificationSubjectAvailablePluginUpdate';
$rootUrl = Fixture::getTestRootUrl();
$message = "<p>ScheduledReports_EmailHello</p>
<p>CoreUpdater_ThereIsNewPluginVersionAvailableForUpdate</p>
<ul>
<li>MyTest1 33.0.0</li>
<li>MyTest2 32.0.0</li>
<li>MyTest3 31.0.0</li>
</ul>
<p>
CoreUpdater_NotificationClickToUpdatePlugins<br/>
{$rootUrl}index.php?module=CorePluginsAdmin&action=plugins
</p>
<p>
Installation_HappyAnalysing
</p>
";
$mock = $this->getCommunicationMockHavingManyUpdates();
$mock->expects($this->once())->method('sendEmailNotification')
->with($this->equalTo($subject), $this->callback(function (View $view) use ($message) {
$this->assertEquals($message, $view->render());
return true;
}));
$mock->sendNotificationIfUpdatesAvailable();
}
private function setLastSentVersion($pluginName, $version)
{
Option::set('last_update_communication_sent_plugin_' . $pluginName, $version);
}
private function getLastSentVersion($pluginName)
{
return Option::get('last_update_communication_sent_plugin_' . $pluginName);
}
/**
* @param array $pluginsHavingUpdate
* @return UpdateCommunication
*/
private function getCommunicationMock($pluginsHavingUpdate)
{
$mock = $this->getMockBuilder('\Piwik\Plugins\Marketplace\UpdateCommunication')
->setMethods(array('getPluginsHavingUpdate', 'sendEmailNotification'))
->setConstructorArgs(array($this->settings))
->getMock();
$mock->expects($this->any())
->method('getPluginsHavingUpdate')
->will($this->returnValue($pluginsHavingUpdate));
return $mock;
}
private function getCommunicationMockHavingManyUpdates()
{
$pluginsHavingUpdate = array(
array('name' => 'MyTest1', 'latestVersion' => '33.0.0', 'isTheme' => false),
array('name' => 'MyTest2', 'latestVersion' => '32.0.0', 'isTheme' => false),
array('name' => 'MyTest3', 'latestVersion' => '31.0.0', 'isTheme' => false),
);
$this->setLastSentVersion('MyTest1', false);
$this->setLastSentVersion('MyTest2', false);
$this->setLastSentVersion('MyTest3', false);
$mock = $this->getCommunicationMock($pluginsHavingUpdate);
return $mock;
}
}
|