Files
piratepoet/mailpoet/tests/integration/EmailEditor/Integration/MailPoet/EmailEditorTest.php
Rostislav Wolny b70ad064c7 Refactor initialization of the email editor
This changes how the editor is initialized. The integration initialization class
doesn't get the engine class anymore, but integration is initialized via
mailpoet_email_editor_initialized action triggered from the engine.

This allows multiple integrations to hook into the engine.
[MAILPOET-5540]
2023-09-21 14:20:35 +02:00

61 lines
2.3 KiB
PHP

<?php declare(strict_types = 1);
namespace MailPoet\EmailEditor\Integrations\MailPoet;
use MailPoet\Entities\NewsletterEntity;
use MailPoet\Features\FeatureFlagsController;
use MailPoet\Features\FeaturesController;
use MailPoet\Newsletter\NewslettersRepository;
use MailPoet\WP\Functions as WPFunctions;
class EmailEditorTest extends \MailPoetTest {
/** @var EmailEditor */
private $emailEditor;
/** @var FeatureFlagsController */
private $featureFlagsController;
/** @var NewslettersRepository */
private $newslettersRepository;
public function _before() {
$this->emailEditor = $this->diContainer->get(EmailEditor::class);
$this->newslettersRepository = $this->diContainer->get(NewslettersRepository::class);
$this->featureFlagsController = $this->diContainer->get(FeatureFlagsController::class);
$this->featureFlagsController->set(FeaturesController::GUTENBERG_EMAIL_EDITOR, true);
}
public function testItRegistersMailPoetEmailPostType() {
$this->emailEditor->initialize();
$this->diContainer->get(\MailPoet\EmailEditor\Engine\EmailEditor::class)->initialize();
$postTypes = get_post_types();
$this->assertArrayHasKey('mailpoet_email', $postTypes);
}
public function testItCreatesAssociatedNewsletterEntity() {
$this->emailEditor->initialize();
$newsletters = $this->newslettersRepository->findAll();
expect(count($newsletters))->equals(0);
$wp = $this->diContainer->get(WPFunctions::class);
// Add email post
$postId = $wp->wpInsertPost(['post_type' => 'mailpoet_email']);
$newsletters = $this->newslettersRepository->findAll();
expect(count($newsletters))->equals(1);
expect($newsletters[0]->getWpPostId())->greaterThan(0);
expect($newsletters[0]->getWpPostId())->equals($postId);
// Add non-email standard post
$wp->wpInsertPost(['post_type' => 'post']);
$newsletters = $this->newslettersRepository->findAll();
// Newsletters count should not change
expect(count($newsletters))->equals(1);
}
public function _after() {
parent::_after();
remove_filter('mailpoet_email_editor_post_types', [$this->emailEditor, 'addEmailPostType']);
remove_filter('save_post', [$this->emailEditor, 'onEmailSave']);
$this->truncateEntity(NewsletterEntity::class);
$this->featureFlagsController->set(FeaturesController::GUTENBERG_EMAIL_EDITOR, false);
}
}