Spaces:
No application file
No application file
File size: 12,106 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
<?php
namespace Mautic\ConfigBundle\Tests\Form\Helper;
use Mautic\ConfigBundle\Form\DataTransformer\DsnTransformerFactory;
use Mautic\ConfigBundle\Form\Helper\RestrictionHelper;
use Mautic\ConfigBundle\Form\Type\ConfigType;
use Mautic\ConfigBundle\Form\Type\DsnType;
use Mautic\ConfigBundle\Form\Type\EscapeTransformer;
use Mautic\CoreBundle\Form\Type\ButtonGroupType;
use Mautic\CoreBundle\Form\Type\FormButtonsType;
use Mautic\CoreBundle\Form\Type\StandAloneButtonType;
use Mautic\CoreBundle\Form\Type\YesNoButtonGroupType;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Translation\Translator;
use Mautic\EmailBundle\EventListener\ProcessBounceSubscriber;
use Mautic\EmailBundle\EventListener\ProcessUnsubscribeSubscriber;
use Mautic\EmailBundle\Form\Type\ConfigMonitoredEmailType;
use Mautic\EmailBundle\Form\Type\ConfigMonitoredMailboxesType;
use Mautic\EmailBundle\Form\Type\ConfigType as EmailConfigType;
use Mautic\EmailBundle\MonitoredEmail\Mailbox;
use Mautic\EmailBundle\MonitoredEmail\Processor\Bounce;
use Mautic\EmailBundle\MonitoredEmail\Processor\FeedbackLoop;
use Mautic\EmailBundle\MonitoredEmail\Processor\Unsubscribe;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\PreloadedExtension;
use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* Mocking a representative ConfigForm by leveraging Symfony's TypeTestCase to test RestrictionHelper.
*/
class RestrictionHelperTest extends TypeTestCase
{
/**
* @var string
*/
private $displayMode = RestrictionHelper::MODE_REMOVE;
/**
* @var array
*/
private $restrictedFields = [
'monitored_email' => [
'EmailBundle_bounces',
'EmailBundle_unsubscribes' => [
'address',
],
],
];
private $forms = [
'emailconfig' => [
'bundle' => 'EmailBundle',
'formAlias' => 'emailconfig',
'formType' => EmailConfigType::class,
'formTheme' => 'MauticEmailBundle:FormTheme\\Config',
'parameters' => [
'mailer_from_name' => 'Mautic',
'mailer_from_email' => '[email protected]',
'mailer_return_path' => null,
'mailer_transport' => 'mail',
'mailer_append_tracking_pixel' => true,
'mailer_convert_embed_images' => false,
'mailer_dsn' => 'smtp://null:25',
'messenger_dsn_email' => 'doctrine://default',
'messenger_retry_strategy_max_retries' => 3,
'messenger_retry_strategy_delay' => 1000,
'messenger_retry_strategy_multiplier' => 2,
'messenger_retry_strategy_max_delay' => 0,
'unsubscribe_text' => null,
'webview_text' => null,
'unsubscribe_message' => null,
'resubscribe_message' => null,
'monitored_email' => [
'general' => [
'address' => null,
'host' => null,
'port' => '993',
'encryption' => '/ssl',
'user' => null,
'password' => null,
],
'EmailBundle_bounces' => [
'address' => null,
'host' => null,
'port' => '993',
'encryption' => '/ssl',
'user' => null,
'password' => null,
'override_settings' => 0,
'folder' => null,
],
'EmailBundle_unsubscribes' => [
'address' => null,
'host' => null,
'port' => '993',
'encryption' => '/ssl',
'user' => null,
'password' => null,
'override_settings' => 0,
'folder' => null,
],
'EmailBundle_replies' => [
'address' => null,
'host' => null,
'port' => '993',
'encryption' => '/ssl',
'user' => null,
'password' => null,
'override_settings' => 0,
'folder' => null,
],
],
'mailer_is_owner' => false,
'default_signature_text' => null,
'email_frequency_number' => null,
'email_frequency_time' => null,
'show_contact_preferences' => false,
'show_contact_frequency' => false,
'show_contact_pause_dates' => false,
'show_contact_preferred_channels' => false,
'show_contact_categories' => false,
'show_contact_segments' => false,
'mailer_mailjet_sandbox' => false,
'mailer_mailjet_sandbox_default_mail' => null,
'disable_trackable_urls' => false,
],
],
];
/**
* @testdox Test that the restricted fields are removed from the config
*
* @covers \Mautic\ConfigBundle\Form\Helper\RestrictionHelper::applyRestrictions
* @covers \Mautic\ConfigBundle\Form\Helper\RestrictionHelper::restrictField
*/
public function testRestrictedFieldsAreRemoved(): void
{
$form = $this->factory->create(ConfigType::class, $this->forms);
$this->assertTrue($form->has('emailconfig'));
$emailConfig = $form->get('emailconfig');
// monitored_email is partially restricted so should be included
$this->assertTrue($emailConfig->has('monitored_email'));
$monitoredEmail = $emailConfig->get('monitored_email');
// EmailBundle_bounces is restricted in entirety and thus should not be included
$this->assertFalse($monitoredEmail->has('EmailBundle_bounces'));
// EmailBundle_unsubscribes is partially restricted so should be included
$this->assertTrue($monitoredEmail->has('EmailBundle_unsubscribes'));
$unsubscribes = $monitoredEmail->get('EmailBundle_unsubscribes');
// address under EmailBundle_unsubscribes is restricted so should not be included
$this->assertFalse($unsubscribes->has('address'));
// host under EmailBundle_unsubscribes is not restricted so should be included
$this->assertTrue($unsubscribes->has('host'));
}
/**
* @testdox Test that the restricted fields are masked
*
* @covers \Mautic\ConfigBundle\Form\Helper\RestrictionHelper::applyRestrictions
* @covers \Mautic\ConfigBundle\Form\Helper\RestrictionHelper::restrictField
*/
public function testRestrictedFieldsAreMasked(): void
{
$this->displayMode = RestrictionHelper::MODE_MASK;
// Rebuild factory to get updated RestrictionHelper
$this->factory = Forms::createFormFactoryBuilder()
->addExtensions($this->getExtensions())
->getFormFactory();
$form = $this->factory->create(ConfigType::class, $this->forms);
/** @var FormInterface<mixed> $address */
$address = $form['emailconfig']['monitored_email']['EmailBundle_unsubscribes']['address'];
$this->assertTrue($address->getConfig()->getOption('attr')['readonly']);
$this->assertTrue($address->getConfig()->getOption('disabled'));
$this->assertEquals(
[
'class' => 'form-control',
'tooltip' => 'mautic.email.config.monitored_email_address.tooltip',
'data-show-on' => '{"config_emailconfig_monitored_email_EmailBundle_unsubscribes_override_settings_1": "checked"}',
'placeholder' => 'mautic.config.restricted',
'readonly' => true,
],
$address->getConfig()->getOption('attr')
);
}
/**
* @return array
*/
protected function getExtensions()
{
$translator = $this->getMockBuilder(Translator::class)
->disableOriginalConstructor()
->getMock();
$translator->method('trans')
->willReturnCallback(
fn ($key) => $key
);
$validator = $this->getMockBuilder(ValidatorInterface::class)
->disableOriginalConstructor()
->getMock();
$validator
->method('validate')
->will($this->returnValue(new ConstraintViolationList()));
$validator
->method('getMetadataFor')
->will($this->returnValue(new ClassMetadata(Form::class)));
$imapHelper = $this->getMockBuilder(Mailbox::class)
->disableOriginalConstructor()
->getMock();
// Register monitored email listeners
$dispatcher = new EventDispatcher();
$bouncer = $this->getMockBuilder(Bounce::class)
->disableOriginalConstructor()
->getMock();
$dispatcher->addSubscriber(new ProcessBounceSubscriber($bouncer));
$unsubscriber = $this->getMockBuilder(Unsubscribe::class)
->disableOriginalConstructor()
->getMock();
$looper = $this->getMockBuilder(FeedbackLoop::class)
->disableOriginalConstructor()
->getMock();
$dispatcher->addSubscriber(new ProcessUnsubscribeSubscriber($unsubscriber, $looper));
// This is what we're really testing here
$restrictionHelper = new RestrictionHelper($translator, $this->restrictedFields, $this->displayMode);
$escapeTransformer = new EscapeTransformer([]);
return [
// register the type instances with the PreloadedExtension
new PreloadedExtension(
[
new TextType(),
new ChoiceType(),
new YesNoButtonGroupType(),
new PasswordType(),
new StandAloneButtonType(),
new NumberType(),
new FormButtonsType(),
new ButtonGroupType(),
new EmailConfigType($translator),
new DsnType($this->createMock(DsnTransformerFactory::class), $this->createMock(CoreParametersHelper::class)),
new ConfigMonitoredEmailType($dispatcher),
new ConfigMonitoredMailboxesType($imapHelper),
new ConfigType($restrictionHelper, $escapeTransformer),
],
[]
),
new ValidatorExtension($validator),
];
}
}
|