File size: 13,832 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php

declare(strict_types=1);

namespace Mautic\ConfigBundle\Tests\Controller;

use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use PHPUnit\Framework\Assert;
use Symfony\Component\DomCrawler\Field\ChoiceFormField;
use Symfony\Component\HttpFoundation\Request;

class ConfigControllerFunctionalTest extends MauticMysqlTestCase
{
    private const SUBDOMAIN_URL = 'subdomain_url.com';

    /**
     * @var string
     */
    private $prefix;

    protected $useCleanupRollback = false;

    protected function setUp(): void
    {
        $this->configParams['config_allowed_parameters'] = [
            'kernel.project_dir',
        ];

        $this->configParams['locale']        = 'en_US';
        $this->configParams['subdomain_url'] = self::SUBDOMAIN_URL;

        parent::setUp();

        $this->prefix = MAUTIC_TABLE_PREFIX;
    }

    public function testValuesAreEscapedProperly(): void
    {
        $url                         = 'https://test.us/create?key=2MLzQFXBSqd2nqwGero90CpB1jX1FbVhhRd51ojr&domain=https%3A%2F%2Ftest.us%2F&longUrl=';
        $trackIps                    = "%ip1%\n%ip2%\n%kernel.project_dir%";
        $googleAnalytics             = 'reveal pass: %mautic.db_password%';

        // request config edit page
        $crawler = $this->client->request(Request::METHOD_GET, '/s/config/edit');
        Assert::assertTrue($this->client->getResponse()->isOk());

        // Find save & close button
        $buttonCrawler = $crawler->selectButton('config[buttons][save]');
        $form          = $buttonCrawler->form();
        $form->setValues(
            [
                'config[coreconfig][site_url]'                    => 'https://mautic-community.local', // required
                'config[coreconfig][do_not_track_ips]'            => $trackIps,
                'config[pageconfig][google_analytics]'            => $googleAnalytics,
                'config[leadconfig][contact_columns]'             => ['name', 'email', 'id'],
            ]
        );

        $crawler = $this->client->submit($form);
        Assert::assertTrue($this->client->getResponse()->isOk());

        // Check for a flash error
        $response = $this->client->getResponse()->getContent();
        $message  = $crawler->filterXPath("//div[@id='flashes']//span")->count()
            ?
            $crawler->filterXPath("//div[@id='flashes']//span")->first()->text()
            :
            '';
        Assert::assertStringNotContainsString('Could not save updated configuration:', $response, $message);

        // Check values are escaped properly in the config file
        $configParameters = $this->getConfigParameters();
        Assert::assertArrayHasKey('do_not_track_ips', $configParameters);
        Assert::assertSame(
            [
                $this->escape('%ip1%'),
                $this->escape('%ip2%'),
                '%kernel.project_dir%',
            ],
            $configParameters['do_not_track_ips']
        );
        Assert::assertArrayHasKey('google_analytics', $configParameters);
        Assert::assertSame($this->escape($googleAnalytics), $configParameters['google_analytics']);
        // Check values are unescaped properly in the edit form
        $crawler = $this->client->request(Request::METHOD_GET, '/s/config/edit');
        Assert::assertTrue($this->client->getResponse()->isOk());

        $buttonCrawler = $crawler->selectButton('config[buttons][save]');
        $form          = $buttonCrawler->form();
        Assert::assertEquals($trackIps, $form['config[coreconfig][do_not_track_ips]']->getValue());
        Assert::assertEquals($googleAnalytics, $form['config[pageconfig][google_analytics]']->getValue());
    }

    private function getConfigPath(): string
    {
        return static::getContainer()->get('kernel')->getLocalConfigFile();
    }

    private function getConfigParameters(): array
    {
        $parameters = [];
        include $this->getConfigPath();

        return $parameters;
    }

    private function escape(string $value): string
    {
        return str_replace('%', '%%', $value);
    }

    public function testConfigNotFoundPageConfiguration(): void
    {
        // insert published record
        $this->connection->insert($this->prefix.'pages', [
            'is_published' => 1,
            'date_added'   => (new \DateTime())->format('Y-m-d H:i:s'),
            'title'        => 'page1',
            'alias'        => 'page1',
            'template'     => 'blank',
            'custom_html'  => 'Page1 Test Html',
            'hits'         => 0,
            'unique_hits'  => 0,
            'variant_hits' => 0,
            'revision'     => 0,
            'lang'         => 'en',
        ]);
        $page1 = $this->connection->lastInsertId();

        // insert unpublished record
        $this->connection->insert($this->prefix.'pages', [
            'is_published' => 0,
            'date_added'   => (new \DateTime())->format('Y-m-d H:i:s'),
            'title'        => 'page2',
            'alias'        => 'page2',
            'template'     => 'blank',
            'custom_html'  => 'Page2 Test Html',
            'hits'         => 0,
            'unique_hits'  => 0,
            'variant_hits' => 0,
            'revision'     => 0,
            'lang'         => 'en',
        ]);
        $this->connection->lastInsertId();

        // insert published record
        $this->connection->insert($this->prefix.'pages', [
            'is_published' => 1,
            'date_added'   => (new \DateTime())->format('Y-m-d H:i:s'),
            'title'        => 'page3',
            'alias'        => 'page3',
            'template'     => 'blank',
            'custom_html'  => 'Page3 Test Html',
            'hits'         => 0,
            'unique_hits'  => 0,
            'variant_hits' => 0,
            'revision'     => 0,
            'lang'         => 'en',
        ]);
        $page3 = $this->connection->lastInsertId();

        // request config edit page
        $crawler = $this->client->request(Request::METHOD_GET, '/s/config/edit');

        // Find save & close button
        $buttonCrawler  = $crawler->selectButton('config[buttons][save]');
        $form           = $buttonCrawler->form();

        // Fetch available option for 404_page field
        $availableOptions = $form['config[coreconfig][404_page]']->availableOptionValues();

        // page 2 should not be available in option list because it is unpublished
        $this->assertEquals(['', $page1, $page3], $availableOptions);

        // page 3 for 404_page
        $form->setValues(
            [
                'config[coreconfig][site_url]'        => 'https://mautic-community.local', // required
                'config[leadconfig][contact_columns]' => ['name', 'email', 'id'],
                'config[coreconfig][404_page]'        => $page3,
            ]
        );

        $crawler = $this->client->submit($form);
        Assert::assertTrue($this->client->getResponse()->isOk());

        $crawler       = $this->client->request(Request::METHOD_GET, '/s/config/edit');
        $buttonCrawler = $crawler->selectButton('config[buttons][save]');
        $form          = $buttonCrawler->form();
        Assert::assertEquals($page3, $form['config[coreconfig][404_page]']->getValue());
        // re-create the Symfony client to make config changes applied
        $this->setUpSymfony($this->configParams);

        // Request not found url page3 page content should be rendered
        $crawler = $this->client->request(Request::METHOD_GET, '/s/config/editnotfoundurlblablabla');
        $this->assertStringContainsString('Page3 Test Html', $crawler->text());
    }

    public function testConfigNotificationConfiguration(): void
    {
        $crawler = $this->client->request(Request::METHOD_GET, '/s/config/edit');

        $buttonCrawler  =  $crawler->selectButton('config[buttons][save]');
        $form           = $buttonCrawler->form();

        $send_notification_to_author           = '0';
        $campaign_notification_email_addresses = '[email protected], [email protected]';
        $webhook_notification_email_addresses  = '[email protected], [email protected]';

        $form->setValues(
            [
                'config[coreconfig][site_url]'                                       => 'https://mautic-community.local', // required
                'config[leadconfig][contact_columns]'                                => ['name', 'email', 'id'],
                'config[notification_config][campaign_send_notification_to_author]'  => $send_notification_to_author,
                'config[notification_config][campaign_notification_email_addresses]' => $campaign_notification_email_addresses,
                'config[notification_config][webhook_send_notification_to_author]'   => $send_notification_to_author,
                'config[notification_config][webhook_notification_email_addresses]'  => $webhook_notification_email_addresses,
            ]
        );

        $this->client->submit($form);
        Assert::assertTrue($this->client->getResponse()->isOk());

        $crawler = $this->client->request(Request::METHOD_GET, '/s/config/edit');
        Assert::assertTrue($this->client->getResponse()->isOk());

        $buttonCrawler = $crawler->selectButton('config[buttons][save]');
        $form          = $buttonCrawler->form();

        Assert::assertEquals($send_notification_to_author, $form['config[notification_config][campaign_send_notification_to_author]']->getValue());
        Assert::assertEquals($campaign_notification_email_addresses, $form['config[notification_config][campaign_notification_email_addresses]']->getValue());
        Assert::assertEquals($send_notification_to_author, $form['config[notification_config][webhook_send_notification_to_author]']->getValue());
        Assert::assertEquals($webhook_notification_email_addresses, $form['config[notification_config][webhook_notification_email_addresses]']->getValue());
    }

    public function testUserAndSystemLocale(): void
    {
        // 1. Change user locale in account - should change _locale session
        $accountCrawler    = $this->client->request(Request::METHOD_GET, '/s/account');
        $accountSaveButton = $accountCrawler->selectButton('user[buttons][save]');
        $accountForm       = $accountSaveButton->form();
        $accountForm->setValues(
            [
                'user[locale]' => 'en_US',
            ]
        );
        $this->client->submit($accountForm);
        Assert::assertTrue($this->client->getResponse()->isOk());
        Assert::assertSame('en_US', static::getContainer()->get('session')->get('_locale'));

        // 2. Change system locale in configuration - should not change _locale session
        $configCrawler    = $this->client->request(Request::METHOD_GET, '/s/config/edit');
        $configSaveButton = $configCrawler->selectButton('config[buttons][save]');
        $configForm       = $configSaveButton->form();
        $configForm->setValues(
            [
                'config[coreconfig][locale]'   => 'en_US',
                'config[coreconfig][site_url]' => 'https://mautic-cloud.local', // required
            ]
        );
        $this->client->submit($configForm);
        Assert::assertTrue($this->client->getResponse()->isOk());
        Assert::assertSame('en_US', static::getContainer()->get('session')->get('_locale'));

        // 3. Change user locale to system default in account - should change _locale session to system default
        $accountCrawler    = $this->client->request(Request::METHOD_GET, '/s/account');
        $accountSaveButton = $accountCrawler->selectButton('user[buttons][save]');
        $accountForm       = $accountSaveButton->form();
        $accountForm->setValues(
            [
                'user[locale]' => '',
            ]
        );
        $this->client->submit($accountForm);
        Assert::assertTrue($this->client->getResponse()->isOk());
        Assert::assertSame('en_US', static::getContainer()->get('session')->get('_locale'));

        // 2. Change system locale in configuration to en_US - should change _locale session
        $configCrawler    = $this->client->request(Request::METHOD_GET, '/s/config/edit');
        $configSaveButton = $configCrawler->selectButton('config[buttons][save]');
        $configForm       = $configSaveButton->form();
        $configForm->setValues(
            [
                'config[coreconfig][locale]'   => 'en_US',
                'config[coreconfig][site_url]' => 'https://mautic-cloud.local', // required
            ]
        );
        $this->client->submit($configForm);
        Assert::assertTrue($this->client->getResponse()->isOk());
        Assert::assertSame('en_US', static::getContainer()->get('session')->get('_locale'));
    }

    public function testSSOSettingEntityId(): void
    {
        $configCrawler    = $this->client->request(Request::METHOD_GET, '/s/config/edit');
        $configSaveButton = $configCrawler->selectButton('config[buttons][apply]');
        $configForm       = $configSaveButton->form();

        /** @var ChoiceFormField $entityIdField */
        $entityIdField    = $configForm['config[userconfig][saml_idp_entity_id]'];
        $availableOptions = $entityIdField->availableOptionValues();
        Assert::assertCount(3, $availableOptions);
        $configForm->setValues(
            [
                'config[userconfig][saml_idp_entity_id]'   => $availableOptions[1],
                'config[coreconfig][site_url]'             => 'https://mautic-cloud.local', // required
            ]
        );
        $this->client->submit($configForm);
        Assert::assertTrue($this->client->getResponse()->isOk());
        Assert::assertEquals($availableOptions[1], $configForm['config[userconfig][saml_idp_entity_id]']->getValue());
    }
}