diff --git a/lib/Config/MP2Migrator.php b/lib/Config/MP2Migrator.php index fac7c1ac5e..004a1f6093 100644 --- a/lib/Config/MP2Migrator.php +++ b/lib/Config/MP2Migrator.php @@ -2,8 +2,9 @@ namespace MailPoet\Config; +use MailPoet\Entities\FormEntity; +use MailPoet\Form\FormsRepository; use MailPoet\Models\CustomField; -use MailPoet\Models\Form; use MailPoet\Models\MappingToExternalEntities; use MailPoet\Models\Segment; use MailPoet\Models\Subscriber; @@ -28,6 +29,9 @@ class MP2Migrator { /** @var Activator */ private $activator; + /** @var FormsRepository */ + private $formsRepository; + private $logFile; public $logFileUrl; public $progressbar; @@ -44,6 +48,7 @@ class MP2Migrator { public function __construct( SettingsController $settings, + FormsRepository $formsRepository, Activator $activator ) { $this->defineMP2Tables(); @@ -53,6 +58,7 @@ class MP2Migrator { $this->progressbar = new ProgressBar('mp2migration'); $this->settings = $settings; $this->activator = $activator; + $this->formsRepository = $formsRepository; } private function defineMP2Tables() { @@ -825,12 +831,11 @@ class MP2Migrator { if (is_array($forms)) { foreach ($forms as $form) { - $newForm = $this->importForm($form); - if (!empty($newForm)) { - $importedFormsCount++; - } + $this->importForm($form); + $importedFormsCount++; } } + $this->formsRepository->flush(); $this->progressbar->incrementCurrentCount($formsCount); } while (($forms != null) && ($formsCount > 0)); @@ -865,7 +870,6 @@ class MP2Migrator { * Import a form * * @param array $formData Form data - * @return Form */ private function importForm($formData) { $serializedData = base64_decode($formData['data']); @@ -916,13 +920,13 @@ class MP2Migrator { ]; } - $form = Form::createOrUpdate([ - 'name' => $formData['name'], - 'body' => $mp3FormBody, - 'settings' => $mp3FormSettings, - ]); + $form = new FormEntity($formData['name']); + $form->setBody($mp3FormBody); + $form->setSettings($mp3FormSettings); + + $this->formsRepository->persist($form); + $this->settings->set('last_imported_form_id', $formData['form_id']); - return $form; } /** diff --git a/tests/integration/Config/MP2MigratorTest.php b/tests/integration/Config/MP2MigratorTest.php index 46c0044c9b..fc22c9bf84 100644 --- a/tests/integration/Config/MP2MigratorTest.php +++ b/tests/integration/Config/MP2MigratorTest.php @@ -6,8 +6,8 @@ use Helper\Database; use MailPoet\Config\Activator; use MailPoet\Config\MP2Migrator; use MailPoet\DI\ContainerWrapper; +use MailPoet\Form\FormsRepository; use MailPoet\Models\CustomField; -use MailPoet\Models\Form; use MailPoet\Models\MappingToExternalEntities; use MailPoet\Models\Segment; use MailPoet\Models\Subscriber; @@ -24,7 +24,11 @@ class MP2MigratorTest extends \MailPoetTest { public function _before() { parent::_before(); $this->settings = SettingsController::getInstance(); - $this->MP2Migrator = new MP2Migrator($this->settings, ContainerWrapper::getInstance()->get(Activator::class)); + $this->MP2Migrator = new MP2Migrator( + $this->settings, + ContainerWrapper::getInstance()->get(FormsRepository::class), + ContainerWrapper::getInstance()->get(Activator::class) + ); } public function _after() { @@ -452,12 +456,13 @@ class MP2MigratorTest extends \MailPoetTest { */ public function testImportForms() { global $wpdb; + $formRepository = ContainerWrapper::getInstance()->get(FormsRepository::class); // Check the forms number $this->initImport(); $this->loadMP2Fixtures(); $this->invokeMethod($this->MP2Migrator, 'importForms'); - expect(Form::count())->equals(2); + expect($formRepository->countBy([]))->equals(2); // Check a form data $this->initImport();