diff --git a/mailpoet/tests/integration/Migrations/MigrateDefaultInactiveSubscriberFrequencyTest.php b/mailpoet/tests/integration/Migrations/MigrateDefaultInactiveSubscriberFrequencyTest.php deleted file mode 100644 index b85aab0af3..0000000000 --- a/mailpoet/tests/integration/Migrations/MigrateDefaultInactiveSubscriberFrequencyTest.php +++ /dev/null @@ -1,123 +0,0 @@ -activator = $this->diContainer->get(Activator::class); - $this->settingsController = $this->diContainer->get(SettingsController::class); - $this->settingsController->delete('deactivate_subscriber_after_inactive_days'); - } - - public function testItDoesNotUpdateValuesOtherThanThePreviousDefault() { - $nonDefaultOptions = [ '', '90', '365' ]; - foreach ($nonDefaultOptions as $option) { - $this->settingsController->set('db_version', '3.78.0'); - $this->settingsController->set('deactivate_subscriber_after_inactive_days', $option); - $this->activator->activate(); - $this->assertEquals($option, $this->settingsController->get('deactivate_subscriber_after_inactive_days')); - } - } - - public function testItDoesUpdatePreviousDefaultValue() { - $this->settingsController->set('db_version', '3.78.0'); - $this->settingsController->set('deactivate_subscriber_after_inactive_days', '180'); - $this->activator->activate(); - $this->assertEquals('365', $this->settingsController->get('deactivate_subscriber_after_inactive_days')); - } - - public function testItDoesNotRunForUnexpectedVersions() { - $versions = ['3.78.1', '3.900.2', '4.8.0']; - foreach ($versions as $version) { - $this->settingsController->set('db_version', $version); - $this->settingsController->set('deactivate_subscriber_after_inactive_days', '180'); - $this->assertEquals('180', $this->settingsController->get('deactivate_subscriber_after_inactive_days')); - $this->activator->activate(); - $this->assertEquals('180', $this->settingsController->get('deactivate_subscriber_after_inactive_days')); - } - } - - public function testItDoesNotRunForNewInstalls() { - $this->settingsController->delete('db_version'); - $this->activator->activate(); - $setting = $this->settingsController->get('deactivate_subscriber_after_inactive_days', 'not-set'); - $this->assertEquals('not-set', $setting); - } - - public function testItCreatesInactiveSubscribersTaskIfOneNotAlreadyScheduled() { - $currentTime = Carbon::now()->microsecond(0); - - /** @var WPFunctions $wpStub */ - $wpStub = Stub::make(new WPFunctions(), [ - 'currentTime' => asCallable(function() use ($currentTime) { - return $currentTime->getTimestamp(); - }), - ]); - WPFunctions::set($wpStub); - - // Double check there isn't already a task in the DB - $scheduledTasksRepository = $this->diContainer->get(ScheduledTasksRepository::class); - $shouldBeNull = $scheduledTasksRepository->findOneBy([ - 'type' => InactiveSubscribers::TASK_TYPE, - ]); - $this->assertNull($shouldBeNull); - - // Run the migration - $this->settingsController->set('db_version', '3.78.0'); - $this->settingsController->set('deactivate_subscriber_after_inactive_days', '180'); - $this->activator->activate(); - - $scheduledTasksRepository = $this->diContainer->get(ScheduledTasksRepository::class); - $task = $scheduledTasksRepository->findOneBy([ - 'type' => InactiveSubscribers::TASK_TYPE, - ]); - $this->assertNotNull($task); - $this->assertEquals($currentTime->subMinute(), $task->getScheduledAt()); - } - - public function testItReschedulesScheduledInactiveSubscribersTask() { - $currentTime = Carbon::now()->microsecond(0); - - /** @var WPFunctions $wpStub */ - $wpStub = Stub::make(new WPFunctions(), [ - 'currentTime' => asCallable(function() use ($currentTime) { - return $currentTime->getTimestamp(); - }), - ]); - WPFunctions::set($wpStub); - $twoHoursFromNow = $currentTime->copy()->addHours(2); - - // Create existing task scheduled for the future - $existingTask = new ScheduledTaskEntity(); - $existingTask->setType(InactiveSubscribers::TASK_TYPE); - $existingTask->setStatus(ScheduledTaskEntity::STATUS_SCHEDULED); - $existingTask->setScheduledAt($twoHoursFromNow); - $scheduledTasksRepository = $this->diContainer->get(ScheduledTasksRepository::class); - $scheduledTasksRepository->persist($existingTask); - $scheduledTasksRepository->flush(); - - // Run the migration - $this->settingsController->set('db_version', '3.78.0'); - $this->settingsController->set('deactivate_subscriber_after_inactive_days', '180'); - $this->activator->activate(); - - $this->assertEquals($currentTime->subMinute(), $existingTask->getScheduledAt()); - } -} diff --git a/mailpoet/tests/integration/Migrations/Migration_20221028_105818_Test.php b/mailpoet/tests/integration/Migrations/Migration_20221028_105818_Test.php index 150fa4f11c..f8cb3e4a59 100644 --- a/mailpoet/tests/integration/Migrations/Migration_20221028_105818_Test.php +++ b/mailpoet/tests/integration/Migrations/Migration_20221028_105818_Test.php @@ -2,11 +2,17 @@ namespace MailPoet\Migrations; +use Codeception\Stub; use MailPoet\Config\Migrator; +use MailPoet\Cron\Workers\InactiveSubscribers; use MailPoet\Entities\DynamicSegmentFilterData; use MailPoet\Entities\DynamicSegmentFilterEntity; +use MailPoet\Entities\ScheduledTaskEntity; +use MailPoet\Newsletter\Sending\ScheduledTasksRepository; use MailPoet\Segments\DynamicSegments\Filters\EmailAction; use MailPoet\Settings\SettingsController; +use MailPoet\WP\Functions as WPFunctions; +use MailPoetVendor\Carbon\Carbon; class Migration_20221028_105818_Test extends \MailPoetTest { /** @var Migration_20221028_105818 */ @@ -89,6 +95,107 @@ class Migration_20221028_105818_Test extends \MailPoetTest { expect($filterLink->getFilterData()->getData())->equals(['newsletter_id' => '1', 'operator' => 'none', 'link_ids' => [2]]); } + public function testItDoesNotUpdateInactiveSubscribersFrequencyValuesOtherThanThePreviousDefault() { + $this->settings->delete('deactivate_subscriber_after_inactive_days'); + $nonDefaultOptions = [ '', '90', '365' ]; + foreach ($nonDefaultOptions as $option) { + $this->settings->set('db_version', '3.78.0'); + $this->settings->set('deactivate_subscriber_after_inactive_days', $option); + $this->migration->run(); + $this->assertEquals($option, $this->settings->get('deactivate_subscriber_after_inactive_days')); + } + } + + public function testItDoesUpdateInactiveSubscribersFrequencyPreviousDefaultValue() { + $this->settings->delete('deactivate_subscriber_after_inactive_days'); + $this->settings->set('db_version', '3.78.0'); + $this->settings->set('deactivate_subscriber_after_inactive_days', '180'); + $this->migration->run(); + $this->assertEquals('365', $this->settings->get('deactivate_subscriber_after_inactive_days')); + } + + public function testItDoesNotRunInactiveSubscribersFrequencyMigrationForUnexpectedVersions() { + $this->settings->delete('deactivate_subscriber_after_inactive_days'); + $versions = ['3.78.1', '3.900.2', '4.8.0']; + foreach ($versions as $version) { + $this->settings->set('db_version', $version); + $this->settings->set('deactivate_subscriber_after_inactive_days', '180'); + $this->assertEquals('180', $this->settings->get('deactivate_subscriber_after_inactive_days')); + $this->migration->run(); + $this->assertEquals('180', $this->settings->get('deactivate_subscriber_after_inactive_days')); + } + } + + public function testItDoesNotRunInactiveSubscribersFrequencyMigrationForNewInstalls() { + $this->settings->delete('deactivate_subscriber_after_inactive_days'); + $this->settings->delete('db_version'); + $this->migration->run(); + $setting = $this->settings->get('deactivate_subscriber_after_inactive_days', 'not-set'); + $this->assertEquals('not-set', $setting); + } + + public function testItCreatesInactiveSubscribersTaskIfOneNotAlreadyScheduled() { + $this->settings->delete('deactivate_subscriber_after_inactive_days'); + $currentTime = Carbon::now()->microsecond(0); + + /** @var WPFunctions $wpStub */ + $wpStub = Stub::make(new WPFunctions(), [ + 'currentTime' => asCallable(function() use ($currentTime) { + return $currentTime->getTimestamp(); + }), + ]); + WPFunctions::set($wpStub); + + // Double check there isn't already a task in the DB + $scheduledTasksRepository = $this->diContainer->get(ScheduledTasksRepository::class); + $shouldBeNull = $scheduledTasksRepository->findOneBy([ + 'type' => InactiveSubscribers::TASK_TYPE, + ]); + $this->assertNull($shouldBeNull); + + // Run the migration + $this->settings->set('db_version', '3.78.0'); + $this->settings->set('deactivate_subscriber_after_inactive_days', '180'); + $this->migration->run(); + + $scheduledTasksRepository = $this->diContainer->get(ScheduledTasksRepository::class); + $task = $scheduledTasksRepository->findOneBy([ + 'type' => InactiveSubscribers::TASK_TYPE, + ]); + $this->assertNotNull($task); + $this->assertEquals($currentTime->subMinute(), $task->getScheduledAt()); + } + + public function testItReschedulesScheduledInactiveSubscribersTask() { + $this->settings->delete('deactivate_subscriber_after_inactive_days'); + $currentTime = Carbon::now()->microsecond(0); + + /** @var WPFunctions $wpStub */ + $wpStub = Stub::make(new WPFunctions(), [ + 'currentTime' => asCallable(function() use ($currentTime) { + return $currentTime->getTimestamp(); + }), + ]); + WPFunctions::set($wpStub); + $twoHoursFromNow = $currentTime->copy()->addHours(2); + + // Create existing task scheduled for the future + $existingTask = new ScheduledTaskEntity(); + $existingTask->setType(InactiveSubscribers::TASK_TYPE); + $existingTask->setStatus(ScheduledTaskEntity::STATUS_SCHEDULED); + $existingTask->setScheduledAt($twoHoursFromNow); + $scheduledTasksRepository = $this->diContainer->get(ScheduledTasksRepository::class); + $scheduledTasksRepository->persist($existingTask); + $scheduledTasksRepository->flush(); + + // Run the migration + $this->settings->set('db_version', '3.78.0'); + $this->settings->set('deactivate_subscriber_after_inactive_days', '180'); + $this->migration->run(); + + $this->assertEquals($currentTime->subMinute(), $existingTask->getScheduledAt()); + } + private function createSegmentFilter(string $action, array $data, string $type, $segmentId = 1): int { $filterTable = $this->entityManager->getClassMetadata(DynamicSegmentFilterEntity::class)->getTableName(); $this->entityManager->getConnection()->executeQuery(