|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Pluswerk\PlusMinify\Tests\Unit\Service; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\Test; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Pluswerk\PlusMinify\Service\MinifyService; |
| 10 | + |
| 11 | +class MinifyServiceTest extends TestCase |
| 12 | +{ |
| 13 | + private MinifyService $subject; |
| 14 | + |
| 15 | + protected function setUp(): void |
| 16 | + { |
| 17 | + parent::setUp(); |
| 18 | + |
| 19 | + // Reset all minify feature flags — everything disabled by default |
| 20 | + $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['minify'] = []; |
| 21 | + |
| 22 | + $this->subject = new MinifyService(); |
| 23 | + } |
| 24 | + |
| 25 | + // ------------------------------------------------------------------------- |
| 26 | + // All features off (default) |
| 27 | + // ------------------------------------------------------------------------- |
| 28 | + |
| 29 | + #[Test] |
| 30 | + public function minifyKeepsHtmlCommentsWhenNoFeaturesAreEnabled(): void |
| 31 | + { |
| 32 | + $html = '<html><head><meta charset="utf-8"><!-- a comment --></head><body><p>Hello</p></body></html>'; |
| 33 | + |
| 34 | + $result = $this->subject->minify($html); |
| 35 | + |
| 36 | + self::assertStringContainsString('<!-- a comment -->', $result); |
| 37 | + } |
| 38 | + |
| 39 | + #[Test] |
| 40 | + public function minifyKeepsTypo3CommentWhenNoFeaturesAreEnabled(): void |
| 41 | + { |
| 42 | + $html = '<html><head><meta charset="utf-8"><!-- TYPO3 page info --></head><body><p>Hello</p></body></html>'; |
| 43 | + |
| 44 | + $result = $this->subject->minify($html); |
| 45 | + |
| 46 | + self::assertStringContainsString('<!-- TYPO3 page info -->', $result); |
| 47 | + } |
| 48 | + |
| 49 | + // ------------------------------------------------------------------------- |
| 50 | + // remove_comments alone — comment removal needs the DOM parser too, |
| 51 | + // so the output stays unchanged when only remove_comments is set. |
| 52 | + // ------------------------------------------------------------------------- |
| 53 | + |
| 54 | + #[Test] |
| 55 | + public function minifyDoesNotRemoveCommentsWithOnlyRemoveCommentsFlagEnabled(): void |
| 56 | + { |
| 57 | + $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['minify']['remove_comments'] = '1'; |
| 58 | + |
| 59 | + $html = '<html><head><meta charset="utf-8"></head><body><!-- just a comment --><p>Hello</p></body></html>'; |
| 60 | + |
| 61 | + $result = $this->subject->minify($html); |
| 62 | + |
| 63 | + // Without the DOM parser the minifier cannot parse the tree, |
| 64 | + // so the comment is left in the output. |
| 65 | + self::assertStringContainsString('<!-- just a comment -->', $result); |
| 66 | + } |
| 67 | + |
| 68 | + // ------------------------------------------------------------------------- |
| 69 | + // remove_comments + optimize_via_html_dom_parser (the realistic combination) |
| 70 | + // ------------------------------------------------------------------------- |
| 71 | + |
| 72 | + #[Test] |
| 73 | + public function minifyRemovesRegularCommentsWhenRemoveCommentsAndDomParserAreEnabled(): void |
| 74 | + { |
| 75 | + $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['minify']['remove_comments'] = '1'; |
| 76 | + $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['minify']['optimize_via_html_dom_parser'] = '1'; |
| 77 | + |
| 78 | + $html = '<html><head><meta charset="utf-8"></head><body><!-- just a comment --><p>Hello</p></body></html>'; |
| 79 | + |
| 80 | + $result = $this->subject->minify($html); |
| 81 | + |
| 82 | + self::assertStringNotContainsString('<!-- just a comment -->', $result); |
| 83 | + self::assertStringContainsString('<p>Hello</p>', $result); |
| 84 | + } |
| 85 | + |
| 86 | + #[Test] |
| 87 | + public function minifyPreservesTypo3CommentWhenRemoveCommentsAndDomParserAreEnabled(): void |
| 88 | + { |
| 89 | + $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['minify']['remove_comments'] = '1'; |
| 90 | + $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['minify']['optimize_via_html_dom_parser'] = '1'; |
| 91 | + |
| 92 | + $html = '<html><head><meta charset="utf-8"><!-- TYPO3CMS id=1 --></head><body><p>Hello</p></body></html>'; |
| 93 | + |
| 94 | + $result = $this->subject->minify($html); |
| 95 | + |
| 96 | + self::assertStringContainsString('<!-- TYPO3CMS id=1 -->', $result); |
| 97 | + } |
| 98 | + |
| 99 | + #[Test] |
| 100 | + public function minifyRemovesRegularCommentButKeepsTypo3CommentWhenBothFlagsAreEnabled(): void |
| 101 | + { |
| 102 | + $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['minify']['remove_comments'] = '1'; |
| 103 | + $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['minify']['optimize_via_html_dom_parser'] = '1'; |
| 104 | + |
| 105 | + $html = '<html><head><meta charset="utf-8"><!-- TYPO3 page --></head><body><!-- drop me --><p>Hello</p></body></html>'; |
| 106 | + |
| 107 | + $result = $this->subject->minify($html); |
| 108 | + |
| 109 | + self::assertStringContainsString('<!-- TYPO3 page -->', $result); |
| 110 | + self::assertStringNotContainsString('<!-- drop me -->', $result); |
| 111 | + } |
| 112 | + |
| 113 | + // ------------------------------------------------------------------------- |
| 114 | + // Output integrity |
| 115 | + // ------------------------------------------------------------------------- |
| 116 | + |
| 117 | + #[Test] |
| 118 | + public function minifyReturnsFallbackHtmlWhenMinifierProducesEmptyString(): void |
| 119 | + { |
| 120 | + // An empty string causes the minifier to return '' which triggers |
| 121 | + // the $originalHtml fallback — the method must not crash. |
| 122 | + $result = $this->subject->minify(''); |
| 123 | + |
| 124 | + self::assertSame('', $result); |
| 125 | + } |
| 126 | + |
| 127 | + #[Test] |
| 128 | + public function minifyAppendsClosingBodyTagWhenAbsentFromMinifiedOutput(): void |
| 129 | + { |
| 130 | + // Enable features that actually cause HtmlMin to strip closing tags. |
| 131 | + $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['minify']['remove_comments'] = '1'; |
| 132 | + $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['minify']['optimize_via_html_dom_parser'] = '1'; |
| 133 | + $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['minify']['remove_omitted_html_tags'] = '1'; |
| 134 | + |
| 135 | + $html = '<html><head><meta charset="utf-8"></head><body><p>Hello</p></body></html>'; |
| 136 | + |
| 137 | + $result = $this->subject->minify($html); |
| 138 | + |
| 139 | + self::assertStringEndsWith('</body>', $result); |
| 140 | + } |
| 141 | +} |
0 commit comments