Files
crmPRO/tests/Domain/Tasks/MailToTaskImporterTest.php
Jacek Pyziak 8f2e332069 Add unit tests for MailToTaskImporter attachment name normalization
- Implemented tests for various scenarios of attachment name normalization in MailToTaskImporter.
- Covered cases for technical names, named files, missing extensions, and content-based detection.
- Added tests for extracting names from MIME headers, including handling of Content-Description.
2026-02-18 23:30:11 +01:00

86 lines
3.3 KiB
PHP

<?php
require_once __DIR__ . '/../../../autoload/Domain/Tasks/MailToTaskImporter.php';
use Domain\Tasks\MailToTaskImporter;
function run_mail_to_task_importer_tests()
{
$importer = new MailToTaskImporter( null );
$normalized_pdf = MailToTaskImporter::normalizeImportedAttachmentName( 'att_6995b8ad9d4567', 'application/pdf' );
assert_true(
$normalized_pdf === 'zalacznik.pdf',
'Expected technical att_* name to be normalized to readable fallback with MIME extension.'
);
$normalized_named = MailToTaskImporter::normalizeImportedAttachmentName( 'faktura_01_2026.pdf', 'application/pdf' );
assert_true(
$normalized_named === 'faktura_01_2026.pdf',
'Expected normal file name to stay unchanged.'
);
$normalized_without_ext = MailToTaskImporter::normalizeImportedAttachmentName( 'umowa_final', 'application/pdf' );
assert_true(
$normalized_without_ext === 'umowa_final.pdf',
'Expected missing extension to be appended from MIME type.'
);
$normalized_from_content = MailToTaskImporter::normalizeImportedAttachmentName(
'att_6995b8ad9d4567',
'application/octet-stream',
"%PDF-1.7\nexample"
);
assert_true(
$normalized_from_content === 'zalacznik.pdf',
'Expected PDF extension to be detected from file content when MIME is generic.'
);
$normalized_technical_with_ext = MailToTaskImporter::normalizeImportedAttachmentName(
'att_6995b8ad9d4567.pdf',
'application/octet-stream'
);
assert_true(
$normalized_technical_with_ext === 'zalacznik.pdf',
'Expected technical name with extension to preserve extension in normalized fallback.'
);
$normalized_original_should_stay = MailToTaskImporter::normalizeImportedAttachmentName(
'VIDOK_Instrukcja Montażu I Uruchomienia - Wyroby Elektryczne.pdf',
'application/octet-stream'
);
assert_true(
$normalized_original_should_stay === 'VIDOK_Instrukcja Montażu I Uruchomienia - Wyroby Elektryczne.pdf',
'Expected original non-technical PDF name to be preserved as-is.'
);
$raw_headers = "Content-Type: application/octet-stream; name=VIDOK_Instrukcja Montażu I Uruchomienia - Wyroby Elektryczne.pdf\r\n"
. "Content-Disposition: attachment; filename=att_6995b8ad9d4567\r\n";
$parsed_name = $importer -> debugExtractNameFromMimeHeaders( $raw_headers );
assert_true(
$parsed_name === 'VIDOK_Instrukcja Montażu I Uruchomienia - Wyroby Elektryczne.pdf',
'Expected parser to keep full unquoted file name with spaces and prefer it over technical att_* name.'
);
$raw_headers_with_description = "Content-Type: application/octet-stream\r\n"
. "Content-Disposition: attachment; filename=att_6995b8ad9d4567\r\n"
. "Content-Description: VIDOK_Instrukcja Montażu I Uruchomienia - Wyroby Elektryczne.pdf\r\n";
$parsed_name_from_description = $importer -> debugExtractNameFromMimeHeaders( $raw_headers_with_description );
assert_true(
$parsed_name_from_description === 'VIDOK_Instrukcja Montażu I Uruchomienia - Wyroby Elektryczne.pdf',
'Expected parser to use Content-Description when filename is only technical att_*.'
);
$normalized_octet_pdf = MailToTaskImporter::normalizeImportedAttachmentName(
'',
'application/octet-stream',
"%PDF-1.6\n..."
);
assert_true(
$normalized_octet_pdf === 'zalacznik.pdf',
'Expected unnamed octet-stream with PDF signature to get pdf extension.'
);
}