Apply new limit to new users

I refactored the SubscribersFeature class so that it's easily
unit tested. Computing the subscribers count in the constructor
is better then computing it on every call to check().

[MAILPOET-2394]
This commit is contained in:
Amine Ben hammou
2019-11-26 00:30:48 +01:00
committed by Jack Kitterhing
parent 53e0934a87
commit 042557aafa
4 changed files with 75 additions and 47 deletions

View File

@ -24,6 +24,7 @@ use MailPoet\AdminPages\Pages\WelcomeWizard;
use MailPoet\AdminPages\Pages\WooCommerceListImport;
use MailPoet\DI\ContainerWrapper;
use MailPoet\Features\FeaturesController;
use MailPoet\Settings\SettingsController;
use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
use MailPoet\Util\License\License;
use MailPoet\WP\Functions as WPFunctions;
@ -66,7 +67,7 @@ class Menu {
}
function init() {
$subscribers_feature = new SubscribersFeature();
$subscribers_feature = new SubscribersFeature($this->container->get(SettingsController::class));
$this->subscribers_over_limit = $subscribers_feature->check();
$this->checkMailPoetAPIKey();
$this->checkPremiumKey();

View File

@ -3,18 +3,32 @@
namespace MailPoet\Util\License\Features;
use MailPoet\Models\Subscriber as SubscriberModel;
use MailPoet\Settings\SettingsController;
use MailPoet\Util\License\License;
class Subscribers {
public $license;
const SUBSCRIBERS_LIMIT = 2000;
const SUBSCRIBERS_OLD_LIMIT = 2000;
const SUBSCRIBERS_NEW_LIMIT = 1000;
const NEW_LIMIT_DATE = '2019-11-00';
function __construct($license = false) {
private $license;
/** @var int */
private $installation_time;
/** @var int */
private $subscribers_count;
function __construct(SettingsController $settings, $license = false) {
$this->license = ($license) ? $license : License::getLicense();
$this->installation_time = strtotime($settings->get('installed_at'));
$this->subscribers_count = SubscriberModel::getTotalSubscribers();
}
function check($subscribers_limit = self::SUBSCRIBERS_LIMIT) {
function check() {
if ($this->license) return false;
return SubscriberModel::getTotalSubscribers() > $subscribers_limit;
$old_user = $this->installation_time < strtotime(self::NEW_LIMIT_DATE);
$subscribers_limit = $old_user ? self::SUBSCRIBERS_OLD_LIMIT : self::SUBSCRIBERS_NEW_LIMIT;
return $this->subscribers_count > $subscribers_limit;
}
}

View File

@ -1,41 +0,0 @@
<?php
namespace MailPoet\Test\Util\License\Features;
use Codeception\Util\Fixtures;
use Codeception\Util\Stub;
use MailPoet\Models\Subscriber;
use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
use MailPoetVendor\Idiorm\ORM;
class SubscribersTest extends \MailPoetTest {
function testChecksIfSubscribersWithinLimitWhenPremiumLicenseDoesNotExist() {
// if premium unlocker plugin is enabled, skip this check
if (defined('MAILPOET_PREMIUM_LICENSE')) return;
$subscribers_feature = new SubscribersFeature();
expect($subscribers_feature->check(0))->false();
$subscriber = Subscriber::create();
$subscriber->hydrate(Fixtures::get('subscriber_template'));
$subscriber->save();
expect($subscribers_feature->check(0))->true();
}
function testChecksIfSubscribersWithinLimitWhenPremiumLicenseExists() {
$subscribers_feature = Stub::construct(
new SubscribersFeature(),
[
'license' => true,
]
);
$subscriber = Subscriber::create();
$subscriber->hydrate(Fixtures::get('subscriber_template'));
$subscriber->save();
expect($subscribers_feature->check(0))->false();
}
function _after() {
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
}
}

View File

@ -0,0 +1,54 @@
<?php
namespace MailPoet\Test\Util\License\Features;
use Codeception\Util\Stub;
use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
class SubscribersTest extends \MailPoetUnitTest {
function testCheckReturnsTrueIfOldUserReachedLimit() {
$subscribers_feature = Stub::make(SubscribersFeature::class, [
'license' => false,
'installation_time' => strtotime('2018-11-11'),
'subscribers_count' => 2500,
]);
expect($subscribers_feature->check())->true();
}
function testCheckReturnsFalseIfOldUserDidntReachLimit() {
$subscribers_feature = Stub::make(SubscribersFeature::class, [
'license' => false,
'installation_time' => strtotime('2018-11-11'),
'subscribers_count' => 1500,
]);
expect($subscribers_feature->check())->false();
}
function testCheckReturnsTrueIfNewUserReachedLimit() {
$subscribers_feature = Stub::make(SubscribersFeature::class, [
'license' => false,
'installation_time' => strtotime('2019-11-11'),
'subscribers_count' => 1500,
]);
expect($subscribers_feature->check())->true();
}
function testCheckReturnsFalseIfNewUserDidntReachLimit() {
$subscribers_feature = Stub::make(SubscribersFeature::class, [
'license' => false,
'installation_time' => strtotime('2019-11-11'),
'subscribers_count' => 900,
]);
expect($subscribers_feature->check())->false();
}
function testCheckReturnsFalseIfLicenseExists() {
$subscribers_feature = Stub::make(SubscribersFeature::class, [
'license' => true,
'installation_time' => strtotime('2019-11-11'),
'subscribers_count' => 1500,
]);
expect($subscribers_feature->check())->false();
}
}