|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Flarum. |
| 5 | + * |
| 6 | + * For detailed copyright and license information, please view the |
| 7 | + * LICENSE file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Flarum\Tests\integration\api\settings; |
| 11 | + |
| 12 | +use Flarum\Testing\integration\RetrievesAuthorizedUsers; |
| 13 | +use Flarum\Testing\integration\TestCase; |
| 14 | +use PHPUnit\Framework\Attributes\Test; |
| 15 | + |
| 16 | +class MailSettingsTest extends TestCase |
| 17 | +{ |
| 18 | + use RetrievesAuthorizedUsers; |
| 19 | + |
| 20 | + /** |
| 21 | + * @inheritDoc |
| 22 | + */ |
| 23 | + protected function setUp(): void |
| 24 | + { |
| 25 | + parent::setUp(); |
| 26 | + |
| 27 | + $this->prepareDatabase([ |
| 28 | + 'users' => [ |
| 29 | + $this->normalUser(), |
| 30 | + ], |
| 31 | + ]); |
| 32 | + } |
| 33 | + |
| 34 | + #[Test] |
| 35 | + public function smtpDriverWithWhitespaceIsInvalidated() |
| 36 | + { |
| 37 | + $this->setting('mail_driver', 'smtp'); |
| 38 | + $this->setting('mail_host', ' world'); |
| 39 | + $this->setting('mail_port', ' 587 '); |
| 40 | + $this->setting('mail_encryption', 'tls'); |
| 41 | + $this->setting('mail_username', 'user '); |
| 42 | + $this->setting('mail_password', ' password'); |
| 43 | + |
| 44 | + $mailSettingsResponse = $this->send( |
| 45 | + $this->request('GET', '/api/mail/settings', [ |
| 46 | + 'authenticatedAs' => 1, |
| 47 | + ]) |
| 48 | + ); |
| 49 | + |
| 50 | + $this->assertEquals(200, $mailSettingsResponse->getStatusCode()); |
| 51 | + |
| 52 | + $data = json_decode((string) $mailSettingsResponse->getBody(), true); |
| 53 | + |
| 54 | + $this->assertFalse($data['data']['attributes']['sending']); |
| 55 | + |
| 56 | + $this->assertArrayHasKey('errors', $data['data']['attributes']); |
| 57 | + |
| 58 | + $this->assertArrayHasKey('mail_host', $data['data']['attributes']['errors']); |
| 59 | + $this->assertEquals('The mail host must not contain leading or trailing whitespace.', $data['data']['attributes']['errors']['mail_host'][0]); |
| 60 | + |
| 61 | + $this->assertArrayHasKey('mail_port', $data['data']['attributes']['errors']); |
| 62 | + $this->assertEquals('The mail port must not contain leading or trailing whitespace.', $data['data']['attributes']['errors']['mail_port'][0]); |
| 63 | + |
| 64 | + $this->assertArrayHasKey('mail_username', $data['data']['attributes']['errors']); |
| 65 | + $this->assertEquals('The mail username must not contain leading or trailing whitespace.', $data['data']['attributes']['errors']['mail_username'][0]); |
| 66 | + |
| 67 | + $this->assertArrayHasKey('mail_password', $data['data']['attributes']['errors']); |
| 68 | + $this->assertEquals('The mail password must not contain leading or trailing whitespace.', $data['data']['attributes']['errors']['mail_password'][0]); |
| 69 | + } |
| 70 | + |
| 71 | + #[Test] |
| 72 | + public function smtpDriverWithValidSettingsIsNotInvalidated() |
| 73 | + { |
| 74 | + $this->setting('mail_driver', 'smtp'); |
| 75 | + $this->setting('mail_host', 'mail.example.com'); |
| 76 | + $this->setting('mail_port', '587'); |
| 77 | + $this->setting('mail_encryption', 'tls'); |
| 78 | + $this->setting('mail_username', 'user'); |
| 79 | + $this->setting('mail_password', 'password'); |
| 80 | + |
| 81 | + $mailSettingsResponse = $this->send( |
| 82 | + $this->request('GET', '/api/mail/settings', [ |
| 83 | + 'authenticatedAs' => 1, |
| 84 | + ]) |
| 85 | + ); |
| 86 | + |
| 87 | + $this->assertEquals(200, $mailSettingsResponse->getStatusCode()); |
| 88 | + |
| 89 | + $data = json_decode((string) $mailSettingsResponse->getBody(), true); |
| 90 | + |
| 91 | + $this->assertEmpty($data['data']['attributes']['errors']); |
| 92 | + $this->assertTrue($data['data']['attributes']['sending']); |
| 93 | + } |
| 94 | + |
| 95 | + #[Test] |
| 96 | + public function mailgunDriverWithWhitespaceIsInvalidated() |
| 97 | + { |
| 98 | + $this->setting('mail_driver', 'mailgun'); |
| 99 | + $this->setting('mail_mailgun_secret', 'key '); |
| 100 | + $this->setting('mail_mailgun_domain', ' example.com'); |
| 101 | + $this->setting('mail_mailgun_region', 'api.mailgun.net'); |
| 102 | + |
| 103 | + $mailSettingsResponse = $this->send( |
| 104 | + $this->request('GET', '/api/mail/settings', [ |
| 105 | + 'authenticatedAs' => 1, |
| 106 | + ]) |
| 107 | + ); |
| 108 | + |
| 109 | + $this->assertEquals(200, $mailSettingsResponse->getStatusCode()); |
| 110 | + |
| 111 | + $data = json_decode((string) $mailSettingsResponse->getBody(), true); |
| 112 | + |
| 113 | + $this->assertFalse($data['data']['attributes']['sending']); |
| 114 | + |
| 115 | + $this->assertArrayHasKey('errors', $data['data']['attributes']); |
| 116 | + |
| 117 | + $this->assertArrayHasKey('mail_mailgun_secret', $data['data']['attributes']['errors']); |
| 118 | + $this->assertEquals('The mail mailgun secret must not contain leading or trailing whitespace.', $data['data']['attributes']['errors']['mail_mailgun_secret'][0]); |
| 119 | + |
| 120 | + $this->assertArrayHasKey('mail_mailgun_domain', $data['data']['attributes']['errors']); |
| 121 | + $this->assertEquals('The mail mailgun domain field format is invalid.', $data['data']['attributes']['errors']['mail_mailgun_domain'][0]); |
| 122 | + } |
| 123 | + |
| 124 | + #[Test] |
| 125 | + public function mailgunDriverWithValidSettingsIsNotInvalidated() |
| 126 | + { |
| 127 | + $this->setting('mail_driver', 'mailgun'); |
| 128 | + $this->setting('mail_mailgun_secret', 'key'); |
| 129 | + $this->setting('mail_mailgun_domain', 'example.com'); |
| 130 | + $this->setting('mail_mailgun_region', 'api.mailgun.net'); |
| 131 | + |
| 132 | + $mailSettingsResponse = $this->send( |
| 133 | + $this->request('GET', '/api/mail/settings', [ |
| 134 | + 'authenticatedAs' => 1, |
| 135 | + ]) |
| 136 | + ); |
| 137 | + |
| 138 | + $this->assertEquals(200, $mailSettingsResponse->getStatusCode()); |
| 139 | + |
| 140 | + $data = json_decode((string) $mailSettingsResponse->getBody(), true); |
| 141 | + |
| 142 | + $this->assertEmpty($data['data']['attributes']['errors']); |
| 143 | + $this->assertTrue($data['data']['attributes']['sending']); |
| 144 | + } |
| 145 | +} |
0 commit comments