@@ -302,4 +302,108 @@ public function testGetHostnameFallsBackToGethostnameFunction(): void
302302
303303 $ this ->assertSame (gethostname (), $ getHostname ());
304304 }
305+
306+ #[DataProvider('providePrepQuotedPrintableWithLfCrlf ' )]
307+ public function testPrepQuotedPrintableWithLfCrlf (string $ input , string $ expected ): void
308+ {
309+ $ email = new Email ();
310+ $ email ->CRLF = "\n" ;
311+ $ prepQP = self ::getPrivateMethodInvoker ($ email , 'prepQuotedPrintable ' );
312+
313+ $ this ->assertSame ($ expected , $ prepQP ($ input ));
314+ }
315+
316+ public static function providePrepQuotedPrintableWithLfCrlf (): iterable
317+ {
318+ return [
319+ 'empty string ' => ['' , '' ],
320+ 'safe ascii only ' => ['hello world ' , 'hello world ' ],
321+ 'safe chars only ' => ['abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789(),-./:? ' , 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789(),-./:? ' ],
322+ 'unsafe char encoded ' => ["a \x01b " , 'a=01b ' ],
323+ 'trailing space encoded ' => ["hello \nworld " , "hello=20 \nworld " ],
324+ 'trailing tab encoded ' => ["hello \t\nworld " , "hello=09 \nworld " ],
325+ 'equals sign encoded as =3D ' => ['a=b ' , 'a=3Db ' ],
326+ 'multiple spaces reduced ' => ['a b ' , 'a b ' ],
327+ 'null bytes removed ' => ["a \x00b " , 'ab ' ],
328+ 'unwrap tags removed ' => ['{unwrap}secret{/unwrap} ' , 'secret ' ],
329+ 'single line ' => ['test ' , 'test ' ],
330+ 'two lines ' => ["line1 \nline2 " , "line1 \nline2 " ],
331+ 'three lines trailing empty ' => ["line1 \nline2 \n" , "line1 \nline2 \n" ],
332+ ];
333+ }
334+
335+ public function testPrepQuotedPrintableWithCrlfNative (): void
336+ {
337+ $ email = new Email ();
338+ $ email ->CRLF = "\r\n" ;
339+ $ prepQP = self ::getPrivateMethodInvoker ($ email , 'prepQuotedPrintable ' );
340+
341+ $ result = $ prepQP ('test ' );
342+
343+ $ this ->assertSame (quoted_printable_encode ('test ' ), $ result );
344+ }
345+
346+ public function testPrepQuotedPrintableSoftLineBreak (): void
347+ {
348+ $ email = new Email ();
349+ $ email ->CRLF = "\n" ;
350+ $ prepQP = self ::getPrivateMethodInvoker ($ email , 'prepQuotedPrintable ' );
351+
352+ // 76 'a' chars fit in one line; add 2 more 'b' chars and they soft-wrap
353+ // After reduction: no trailing spaces, just safe chars
354+ $ input = str_repeat ('a ' , 76 ) . 'bb ' ;
355+ $ result = $ prepQP ($ input );
356+
357+ $ this ->assertStringContainsString ("= \n" , $ result , 'Soft line break must be present ' );
358+ $ this ->assertStringNotContainsString ("\r\n" , $ result , 'Custom CRLF must not contain \\r ' );
359+ }
360+
361+ public function testPrepQuotedPrintableSoftBreakAfterEncodedChar (): void
362+ {
363+ $ email = new Email ();
364+ $ email ->CRLF = "\n" ;
365+ $ prepQP = self ::getPrivateMethodInvoker ($ email , 'prepQuotedPrintable ' );
366+
367+ // 74 safe chars + 1 encoded (=3D = 3 bytes) = 77 → must break before encoded
368+ $ input = str_repeat ('a ' , 74 ) . '= ' ;
369+ $ result = $ prepQP ($ input );
370+
371+ $ this ->assertSame (str_repeat ('a ' , 74 ) . "= \n=3D " , $ result );
372+ }
373+
374+ public function testPrepQuotedPrintableHardLineBreakNoInternalSpaceReduction (): void
375+ {
376+ $ email = new Email ();
377+ $ email ->CRLF = "\n" ;
378+ $ prepQP = self ::getPrivateMethodInvoker ($ email , 'prepQuotedPrintable ' );
379+
380+ // Spaces not at end of line must be left as-is
381+ $ this ->assertSame ('a b ' , $ prepQP ('a b ' ));
382+ }
383+
384+ public function testPrepQuotedPrintableMixedContent (): void
385+ {
386+ $ email = new Email ();
387+ $ email ->CRLF = "\n" ;
388+ $ prepQP = self ::getPrivateMethodInvoker ($ email , 'prepQuotedPrintable ' );
389+
390+ $ input = "Hello, World! \nline ends with tab \t\n=special chars: \x01\x02" ;
391+ $ result = $ prepQP ($ input );
392+
393+ $ this ->assertStringContainsString ('Hello, World=21 ' , $ result );
394+ $ this ->assertStringContainsString ('=09 ' , $ result );
395+ $ this ->assertStringContainsString ('=3D ' , $ result );
396+ $ this ->assertStringContainsString ('=01 ' , $ result );
397+ $ this ->assertStringContainsString ('=02 ' , $ result );
398+ }
399+
400+ public function testPrepQuotedPrintableUnwrapRemovesTagsOnly (): void
401+ {
402+ $ email = new Email ();
403+ $ email ->CRLF = "\n" ;
404+ $ prepQP = self ::getPrivateMethodInvoker ($ email , 'prepQuotedPrintable ' );
405+
406+ $ this ->assertSame ('keep =7Bbraces=7D ' , $ prepQP ('keep {braces} ' ));
407+ $ this ->assertSame ('keep (parentheses) ' , $ prepQP ('keep (parentheses) ' ));
408+ }
305409}
0 commit comments