From 042557aafacbcd738c14066cbefef7f743839e5d Mon Sep 17 00:00:00 2001 From: Amine Ben hammou Date: Tue, 26 Nov 2019 00:30:48 +0100 Subject: [PATCH] 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] --- lib/Config/Menu.php | 3 +- lib/Util/License/Features/Subscribers.php | 24 +++++++-- .../Util/License/Features/SubscribersTest.php | 41 -------------- .../Util/License/Features/SubscribersTest.php | 54 +++++++++++++++++++ 4 files changed, 75 insertions(+), 47 deletions(-) delete mode 100644 tests/integration/Util/License/Features/SubscribersTest.php create mode 100644 tests/unit/Util/License/Features/SubscribersTest.php diff --git a/lib/Config/Menu.php b/lib/Config/Menu.php index ef9f6a2539..c4b991d05d 100644 --- a/lib/Config/Menu.php +++ b/lib/Config/Menu.php @@ -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(); diff --git a/lib/Util/License/Features/Subscribers.php b/lib/Util/License/Features/Subscribers.php index f96055e938..bd2f64eda3 100644 --- a/lib/Util/License/Features/Subscribers.php +++ b/lib/Util/License/Features/Subscribers.php @@ -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; } } diff --git a/tests/integration/Util/License/Features/SubscribersTest.php b/tests/integration/Util/License/Features/SubscribersTest.php deleted file mode 100644 index fee5a2aaca..0000000000 --- a/tests/integration/Util/License/Features/SubscribersTest.php +++ /dev/null @@ -1,41 +0,0 @@ -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); - } - -} diff --git a/tests/unit/Util/License/Features/SubscribersTest.php b/tests/unit/Util/License/Features/SubscribersTest.php new file mode 100644 index 0000000000..3e094fd85d --- /dev/null +++ b/tests/unit/Util/License/Features/SubscribersTest.php @@ -0,0 +1,54 @@ + 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(); + } +}