Remove old form model from MP2 migrator

[MAILPOET-3644]
This commit is contained in:
Pavel Dohnal
2021-09-15 13:59:19 +02:00
committed by Veljko V
parent 05d69913a6
commit b94b8f119a
2 changed files with 24 additions and 15 deletions

View File

@@ -2,8 +2,9 @@
namespace MailPoet\Config; namespace MailPoet\Config;
use MailPoet\Entities\FormEntity;
use MailPoet\Form\FormsRepository;
use MailPoet\Models\CustomField; use MailPoet\Models\CustomField;
use MailPoet\Models\Form;
use MailPoet\Models\MappingToExternalEntities; use MailPoet\Models\MappingToExternalEntities;
use MailPoet\Models\Segment; use MailPoet\Models\Segment;
use MailPoet\Models\Subscriber; use MailPoet\Models\Subscriber;
@@ -28,6 +29,9 @@ class MP2Migrator {
/** @var Activator */ /** @var Activator */
private $activator; private $activator;
/** @var FormsRepository */
private $formsRepository;
private $logFile; private $logFile;
public $logFileUrl; public $logFileUrl;
public $progressbar; public $progressbar;
@@ -44,6 +48,7 @@ class MP2Migrator {
public function __construct( public function __construct(
SettingsController $settings, SettingsController $settings,
FormsRepository $formsRepository,
Activator $activator Activator $activator
) { ) {
$this->defineMP2Tables(); $this->defineMP2Tables();
@@ -53,6 +58,7 @@ class MP2Migrator {
$this->progressbar = new ProgressBar('mp2migration'); $this->progressbar = new ProgressBar('mp2migration');
$this->settings = $settings; $this->settings = $settings;
$this->activator = $activator; $this->activator = $activator;
$this->formsRepository = $formsRepository;
} }
private function defineMP2Tables() { private function defineMP2Tables() {
@@ -825,12 +831,11 @@ class MP2Migrator {
if (is_array($forms)) { if (is_array($forms)) {
foreach ($forms as $form) { foreach ($forms as $form) {
$newForm = $this->importForm($form); $this->importForm($form);
if (!empty($newForm)) { $importedFormsCount++;
$importedFormsCount++;
}
} }
} }
$this->formsRepository->flush();
$this->progressbar->incrementCurrentCount($formsCount); $this->progressbar->incrementCurrentCount($formsCount);
} while (($forms != null) && ($formsCount > 0)); } while (($forms != null) && ($formsCount > 0));
@@ -865,7 +870,6 @@ class MP2Migrator {
* Import a form * Import a form
* *
* @param array $formData Form data * @param array $formData Form data
* @return Form
*/ */
private function importForm($formData) { private function importForm($formData) {
$serializedData = base64_decode($formData['data']); $serializedData = base64_decode($formData['data']);
@@ -916,13 +920,13 @@ class MP2Migrator {
]; ];
} }
$form = Form::createOrUpdate([ $form = new FormEntity($formData['name']);
'name' => $formData['name'], $form->setBody($mp3FormBody);
'body' => $mp3FormBody, $form->setSettings($mp3FormSettings);
'settings' => $mp3FormSettings,
]); $this->formsRepository->persist($form);
$this->settings->set('last_imported_form_id', $formData['form_id']); $this->settings->set('last_imported_form_id', $formData['form_id']);
return $form;
} }
/** /**

View File

@@ -6,8 +6,8 @@ use Helper\Database;
use MailPoet\Config\Activator; use MailPoet\Config\Activator;
use MailPoet\Config\MP2Migrator; use MailPoet\Config\MP2Migrator;
use MailPoet\DI\ContainerWrapper; use MailPoet\DI\ContainerWrapper;
use MailPoet\Form\FormsRepository;
use MailPoet\Models\CustomField; use MailPoet\Models\CustomField;
use MailPoet\Models\Form;
use MailPoet\Models\MappingToExternalEntities; use MailPoet\Models\MappingToExternalEntities;
use MailPoet\Models\Segment; use MailPoet\Models\Segment;
use MailPoet\Models\Subscriber; use MailPoet\Models\Subscriber;
@@ -24,7 +24,11 @@ class MP2MigratorTest extends \MailPoetTest {
public function _before() { public function _before() {
parent::_before(); parent::_before();
$this->settings = SettingsController::getInstance(); $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() { public function _after() {
@@ -452,12 +456,13 @@ class MP2MigratorTest extends \MailPoetTest {
*/ */
public function testImportForms() { public function testImportForms() {
global $wpdb; global $wpdb;
$formRepository = ContainerWrapper::getInstance()->get(FormsRepository::class);
// Check the forms number // Check the forms number
$this->initImport(); $this->initImport();
$this->loadMP2Fixtures(); $this->loadMP2Fixtures();
$this->invokeMethod($this->MP2Migrator, 'importForms'); $this->invokeMethod($this->MP2Migrator, 'importForms');
expect(Form::count())->equals(2); expect($formRepository->countBy([]))->equals(2);
// Check a form data // Check a form data
$this->initImport(); $this->initImport();