feca-mailshots-plugin/tests/integration/test_mailshot_list_lightwei...

84 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
require_once dirname(__DIR__, 2) . '/feca_mailshots_plugin/src/autoload.php';
use FecaMailshots\Infrastructure\Env;
use FecaMailshots\Infrastructure\PdoDatabaseRouter;
use FecaMailshots\Repository\MailshotRepository;
Env::load(dirname(__DIR__, 2) . '/credentials/.env');
$dbConfig = [
'MYSQL_HOST' => '127.0.0.1',
'MYSQL_PORT' => (string) (getenv('MYSQL_TUNNEL_LOCAL_PORT') ?: '13306'),
'MYSQL_USER' => Env::require('REMOTE_MYSQL_USER'),
'MYSQL_PASSWORD' => Env::require('REMOTE_MYSQL_PASSWORD'),
'MAILSHOTS_REMOTE_MYSQL_DB' => Env::require('MAILSHOTS_REMOTE_MYSQL_DB'),
'MEMBERS_REMOTE_MYSQL_DB' => Env::require('MEMBERS_REMOTE_MYSQL_DB'),
'FEN_REMOTE_MYSQL_DB' => Env::require('FEN_REMOTE_MYSQL_DB'),
];
$repo = new MailshotRepository(new PdoDatabaseRouter($dbConfig));
$id = null;
$purpose = 'list_lightweight_' . gmdate('Ymd_His') . '_' . bin2hex(random_bytes(3));
$subject = str_repeat('S', 1800000);
$message = str_repeat('M', 1800000);
$pdfAttachment = str_repeat('P', 1800000);
try {
$id = $repo->create([
'Purpose' => $purpose,
'DataSource' => 'test_source',
'CC' => '',
'BCC' => '',
'Subject' => $subject,
'Message' => $message,
'PDFAttachment' => $pdfAttachment,
'AttachmentNames' => '[]',
'PDFFilenameDerivedFrom' => '',
'ReplyTo' => '',
'RecipientEmailField' => '',
]);
if ($id <= 0) {
fwrite(STDERR, "Mailshot create did not return an id\n");
exit(1);
}
$listedRow = null;
foreach ($repo->all() as $row) {
if ((int) ($row['id'] ?? 0) === $id) {
$listedRow = $row;
break;
}
}
if (!is_array($listedRow)) {
fwrite(STDERR, "Mailshot was not returned by list query\n");
exit(1);
}
if (array_key_exists('Message', $listedRow) || array_key_exists('PDFAttachment', $listedRow) || array_key_exists('CC', $listedRow) || array_key_exists('BCC', $listedRow)) {
fwrite(STDERR, "Mailshot list query should not fetch large template fields\n");
exit(1);
}
if (strlen((string) ($listedRow['Subject'] ?? '')) > 1000) {
fwrite(STDERR, "Mailshot list query should only fetch a bounded subject preview\n");
exit(1);
}
$fullRow = $repo->find($id);
if (!is_array($fullRow) || (string) ($fullRow['Subject'] ?? '') !== $subject || (string) ($fullRow['Message'] ?? '') !== $message || (string) ($fullRow['PDFAttachment'] ?? '') !== $pdfAttachment) {
fwrite(STDERR, "Mailshot find query should fetch full template fields for editing/sending\n");
exit(1);
}
} finally {
if ($id !== null && $id > 0) {
try {
$repo->delete($id);
} catch (Throwable $e) {
}
}
}
echo "Mailshot list lightweight regression test passed\n";