1
0
قرینه از https://github.com/matomo-org/matomo.git synced 2025-08-24 16:07:37 +00:00
Files
matomo/plugins/Marketplace/tests/Integration/UpdateCommunicationTest.php
Stefan Giehl d6d72d1fa7 [Coding Style] Enable rule PSR1.Methods.CamelCapsMethodName.NotCamelCaps (#22144)
* [Coding Style] Enable rule PSR1.Methods.CamelCapsMethodName.NotCamelCaps

* [Coding Style] Use camel case for method names in API plugin tests (#22145)

* [Coding Style] Use camel case for method names in Core* plugin tests (#22147)

* [Coding Style] Use camel case for method names in core Unit tests (#22149)

* [Coding Style] Use camel case for method names in Actions and BulkTracking plugin tests (#22146)

* [Coding Style] Use camel case for method names in CustomDimensions and CustomJSTracker plugin tests (#22148)

* [Coding Style] Use camel case for method names in core Integration tests (#22151)

* [Coding Style] Use camel case for method names in more core plugin tests (#22153)

* [Coding Style] Use camel case for method names in more core plugin tests (#22157)

* [Coding Style] Use camel case for method names in more core plugin tests

* Update plugins/Monolog/tests/Unit/Processor/ExceptionToTextProcessorTest.php

Co-authored-by: Michal Kleiner <michal@innocraft.com>

---------

Co-authored-by: Michal Kleiner <michal@innocraft.com>

* [Coding Style] Use camel case for method names in more core plugin tests (#22159)

* [Coding Style] Use camel case for method names in remaining tests (#22160)

* [Coding Style] Use camel case for method names in remaining tests

* rename expected test files

---------

Co-authored-by: Michal Kleiner <michal@innocraft.com>
2024-04-25 20:57:55 +02:00

201 خطوط
6.7 KiB
PHP

<?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\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\Twig;
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 testCanBeEnabled()
{
$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 testIsEnabledShouldReturnFalseIfCannotBeEnabled()
{
$this->assertTrue($this->updateCommunication->isEnabled());
Config::getInstance()->General['enable_update_communication'] = 0;
$this->assertFalse($this->updateCommunication->isEnabled());
}
public function testSendNotificationIfUpdatesAvailableShouldNotSendNotificationIfNoUpdateAvailable()
{
$mock = $this->getCommunicationMock(array());
$mock->expects($this->never())->method('sendEmailNotification');
$mock->sendNotificationIfUpdatesAvailable();
}
/**
* @dataProvider provideSendNotificationData
*/
public function testSendNotificationIfUpdatesAvailable($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 testSendNotificationIfUpdatesAvailableShouldSendOnlyOneEmailIfMultipleUpdatesAreAvailable()
{
$mock = $this->getCommunicationMockHavingManyUpdates();
$mock->expects($this->once())->method('sendEmailNotification');
$mock->sendNotificationIfUpdatesAvailable();
}
public function testSendNotificationIfUpdatesAvailableShouldUpdateAllSentVersionsIfMultipleUpdatesAreAvailable()
{
$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 testSendNotificationIfUpdatesAvailableShouldSendCorrectText()
{
$subject = 'CoreUpdater_NotificationSubjectAvailablePluginUpdate';
$rootUrl = Fixture::getTestRootUrl();
$twig = new Twig();
$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/>
<a href=\"" . twig_escape_filter($twig->getTwigEnvironment(), $rootUrl, 'html_attr') . "index.php?module=CorePluginsAdmin&action=plugins\">{$rootUrl}index.php?module=CorePluginsAdmin&action=plugins</a>
</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;
}
}