Files
piratepoet/mailpoet/tests/integration/Subscription/ManageSubscriptionFormRendererTest.php
Rodrigo Primo ee4df28cc3 Fix a bug in ManageSubscriptionFormRendererTest
The test class ManageSubscriptionFormRendererTest creates a subscriber
with a segment. But when calling $this->formRenderer->renderForm(), it
was passing the subscriber without information about its segments. This
meant that the output returned by renderForm() wrongly had the list of
segments without the subscriber segment checked.

This commit fixes this problem by passing the subscriber with the
segments to renderForm() and changing the expected HTML that should be
returned.

[MAILPOET-4020]
2022-04-04 16:24:21 +02:00

85 lines
4.0 KiB
PHP

<?php
namespace MailPoet\Subscription;
use MailPoet\Entities\SegmentEntity;
use MailPoet\Entities\SubscriberEntity;
use MailPoet\Entities\SubscriberSegmentEntity;
use MailPoet\Models\Subscriber;
use MailPoet\WP\Functions as WPFunctions;
class ManageSubscriptionFormRendererTest extends \MailPoetTest {
/** @var ManageSubscriptionFormRenderer */
private $formRenderer;
public function _before() {
$this->cleanup();
$this->formRenderer = $this->diContainer->get(ManageSubscriptionFormRenderer::class);
parent::_before();
}
public function testItGeneratesForm() {
$subscriber = $this->getSubscriber($this->getSegment());
$form = $this->formRenderer->renderForm(Subscriber::findOne($subscriber->getId())->withSubscriptions());
expect($form)->regExp('/<form class="mailpoet-manage-subscription" method="post" action="[a-z0-9:\/\._]+wp-admin\/admin-post.php" novalidate>/');
expect($form)->stringContainsString('<input type="hidden" name="data[email]" value="subscriber@test.com" />');
expect($form)->regExp('/<input type="text" class="mailpoet_text" name="data\[[a-zA-Z0-9=_]+\]" title="First name" value="Fname" data-automation-id="form_first_name" data-parsley-names=\'\[&quot;Please specify a valid name.&quot;,&quot;Addresses in names are not permitted, please add your name instead\.&quot;\]\'\/>/');
expect($form)->regExp('/<input type="text" class="mailpoet_text" name="data\[[a-zA-Z0-9=_]+\]" title="Last name" value="Lname" data-automation-id="form_last_name" data-parsley-names=\'\[&quot;Please specify a valid name.&quot;,&quot;Addresses in names are not permitted, please add your name instead\.&quot;\]\'\/>/');
expect($form)->regExp('/<input type="checkbox" class="mailpoet_checkbox" name="data\[[a-zA-Z0-9=_]+\]\[\]" value="1" checked="checked" data-parsley-required="true" data-parsley-group="segments" data-parsley-errors-container="\.mailpoet_error_segments" data-parsley-required-message="Please select a list." \/> Test segment/');
expect($form)->stringContainsString('Need to change your email address? Unsubscribe using the form below, then simply sign up again.');
}
public function testItAppliesFieldsFilter() {
$subscriber = $this->getSubscriber($this->getSegment());
$wp = $this->diContainer->get(WPFunctions::class);
$wp->addFilter('mailpoet_manage_subscription_page_form_fields', function($fields) {
$fields[] = [
'type' => 'text',
'name' => 'Additional info',
'id' => 'additional_info',
'params' => [
'label' => 'Additional info',
],
];
return $fields;
});
$form = $this->formRenderer->renderForm(Subscriber::findOne($subscriber->getId())->withSubscriptions());
expect($form)->regExp('/<input type="text" class="mailpoet_text" name="data\[[a-zA-Z0-9=_]+\]" title="Additional info" value="" \/>/');
}
private function getSegment(): SegmentEntity {
$segment = new SegmentEntity('Test segment', SegmentEntity::TYPE_DEFAULT, 'Description');
$this->entityManager->persist($segment);
$this->entityManager->flush();
return $segment;
}
private function getSubscriber(SegmentEntity $segment = null): SubscriberEntity {
$subscriber = new SubscriberEntity();
$subscriber->setStatus(SubscriberEntity::STATUS_SUBSCRIBED);
$subscriber->setFirstName('Fname');
$subscriber->setLastName('Lname');
$subscriber->setEmail('subscriber@test.com');
if ($segment) {
$subscriberSegment = new SubscriberSegmentEntity($segment, $subscriber, SubscriberEntity::STATUS_SUBSCRIBED);
$this->entityManager->persist($subscriberSegment);
$subscriber->getSegments()->add($subscriberSegment);
}
$this->entityManager->persist($subscriber);
$this->entityManager->flush();
return $subscriber;
}
private function cleanup() {
$this->truncateEntity(SubscriberEntity::class);
$this->truncateEntity(SubscriberSegmentEntity::class);
$this->truncateEntity(SegmentEntity::class);
}
public function _after() {
$this->cleanup();
}
}