Skip to content

Commit

Permalink
Automatically cancel/remove a plugin trial request when the plugin is…
Browse files Browse the repository at this point in the history
… installed
  • Loading branch information
sgiehl committed Apr 25, 2024
1 parent 1726732 commit a4f9444
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 9 deletions.
14 changes: 12 additions & 2 deletions plugins/Marketplace/Marketplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function registerEvents()
'Controller.CoreHome.checkForUpdates' => 'checkForUpdates',
'Controller.CoreHome.markNotificationAsRead' => 'dismissPluginTrialNotification',
'Request.dispatch' => 'createPluginTrialNotification',
'PluginManager.pluginInstalled' => 'removePluginTrialRequest',
'Widget.filterWidgets' => 'filterWidgets'
);
}
Expand Down Expand Up @@ -175,7 +176,7 @@ public function filterWidgets($list)
}
}

public function dismissPluginTrialNotification()
public function dismissPluginTrialNotification(): void
{
try {
$notificationId = Request::fromRequest()->getStringParameter('notificationId');
Expand All @@ -186,7 +187,7 @@ public function dismissPluginTrialNotification()
}
}

public function createPluginTrialNotification()
public function createPluginTrialNotification(): void
{
try {
StaticContainer::get(PluginTrialService::class)->createNotificationsIfNeeded();
Expand All @@ -195,6 +196,15 @@ public function createPluginTrialNotification()
}
}

public function removePluginTrialRequest(string $pluginName): void
{
try {
StaticContainer::get(PluginTrialService::class)->cancelRequest($pluginName);
} catch (\Exception $e) {
// ignore any type of error
}
}

public static function isMarketplaceEnabled()
{
return self::getPluginManager()->isPluginActivated('Marketplace');
Expand Down
11 changes: 11 additions & 0 deletions plugins/Marketplace/PluginTrial/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function __construct(string $pluginName, Storage $storage)
public function setNotificationDismissed(): void
{
$this->storage->setNotificationDismissed();
$this->removeFromSession();
}

/**
Expand Down Expand Up @@ -78,6 +79,16 @@ public function createNotificationIfNeeded(): void
MatomoNotification\Manager::notify($this->getNotificationId(), $notification);
}

/**
* Removes a notification from current users session
*
* @return void
*/
public function removeFromSession(): void
{
MatomoNotification\Manager::cancel($this->getNotificationId());
}

private function getNotificationId(): string
{
return sprintf('Marketplace_PluginTrialRequest_%s_%s', md5(Piwik::getCurrentUserLogin()), $this->pluginName);
Expand Down
14 changes: 14 additions & 0 deletions plugins/Marketplace/PluginTrial/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ public function create(): void
$this->sendEmailToSuperUsers();
}

/**
* Cancels a trial request
*
* @return void
*/
public function cancel(): void
{
if (!$this->wasRequested()) {
return; // not requested
}

$this->storage->clearStorage();
}


/**
* Returns if a plugin was already requested
Expand Down
16 changes: 16 additions & 0 deletions plugins/Marketplace/PluginTrial/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ public function dismissNotification(string $notificationId): void
$pluginTrial->setNotificationDismissed();
}

/**
* Cancels a plugin trial request
*
* @param string $pluginName
* @return void
* @throws Exception
*/
public function cancelRequest(string $pluginName): void
{
$request = new Request($pluginName, new Storage($pluginName));
$request->cancel();

$notification = new Notification($pluginName, new Storage($pluginName));
$notification->removeFromSession();
}

public function isEnabled(): bool
{
return -1 !== (int) GeneralConfig::getConfigValue('plugin_trial_request_expiration_in_days');
Expand Down
15 changes: 10 additions & 5 deletions plugins/Marketplace/PluginTrial/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ public function isNotificationDismissed(): bool
return !empty($this->storage['dismissed']) && in_array(Piwik::getCurrentUserLogin(), $this->storage['dismissed']);
}

/**
* Removes the trial request from storage
*
* @return void
*/
public function clearStorage(): void
{
Option::delete($this->optionName);
}

/**
* Returns the names of plugins where trial requests are stored for, sorted by request time descending
*
Expand Down Expand Up @@ -119,9 +129,4 @@ protected function saveStorage(): void
{
Option::set($this->optionName, json_encode($this->storage));
}

protected function clearStorage(): void
{
Option::delete($this->optionName);
}
}
10 changes: 10 additions & 0 deletions plugins/Marketplace/tests/Integration/PluginTrial/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ public function testCreateAlreadyRequested()
$request->create();
}

public function testCancel()
{
$storageMock = self::createMock(Storage::class);
$storageMock->method('wasRequested')->willReturn(true);
$storageMock->expects(self::once())->method('clearStorage');

$request = new Request('PremiumPlugin', $storageMock);
$request->cancel();
}

public function testCreateSucceedsAndSendsMail()
{
Fixture::createSuperUser();
Expand Down
10 changes: 10 additions & 0 deletions plugins/Marketplace/tests/Integration/PluginTrial/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ public function testWasRequested()
self::assertTrue($service->wasRequested('PremiumPlugin'));
}

public function testCancel()
{
$this->setRequested();

$service = new Service();
self::assertTrue($service->wasRequested('PremiumPlugin'));
$service->cancelRequest('PremiumPlugin');
self::assertFalse($service->wasRequested('PremiumPlugin'));
}

public function testCreateAndDismissNotifications()
{
$service = new Service();
Expand Down
18 changes: 16 additions & 2 deletions plugins/Marketplace/tests/Integration/PluginTrial/StorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testConstructorFailsOnInvalidPlugin()
$storage = new Storage('Inval$dPlü§1n');
}

public function testRequested()
public function testWasRequested()
{
$storage = new Storage('PremiumPlugin');
self::assertFalse($storage->wasRequested());
Expand All @@ -48,7 +48,21 @@ public function testRequested()
self::assertTrue($storage->wasRequested());
}

public function testRequestedClearsStorageWhenOutdated()
public function testClearStorage()
{
// Manually create a request that is 25 hours old
Option::set('Marketplace.PluginTrialRequest.PremiumPlugin', json_encode([
'requestTime' => time() - (25 * 3600),
'dismissed' => [],
'requestedBy' => 'olaf',
]));

$storage = new Storage('PremiumPlugin');
$storage->clearStorage();
self::assertFalse(Option::get('Marketplace.PluginTrialRequest.PremiumPlugin'));
}

public function testWasRequestedClearsStorageWhenOutdated()
{
// Manually create a request that is 25 hours old
Option::set('Marketplace.PluginTrialRequest.PremiumPlugin', json_encode([
Expand Down

0 comments on commit a4f9444

Please sign in to comment.