|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MadeByDenis\PhpMjmlRenderer\Tests\Unit\Elements\BodyComponents; |
| 4 | + |
| 5 | +use MadeByDenis\PhpMjmlRenderer\Elements\BodyComponents\MjRaw; |
| 6 | +use MadeByDenis\PhpMjmlRenderer\Elements\ElementFactory; |
| 7 | +use MadeByDenis\PhpMjmlRenderer\Parser\MjmlNode; |
| 8 | + |
| 9 | +beforeEach(function () { |
| 10 | + $this->element = new MjRaw(); |
| 11 | +}); |
| 12 | + |
| 13 | +it('is ending tag', function () { |
| 14 | + expect($this->element->isEndingTag())->toBe(true); |
| 15 | +}); |
| 16 | + |
| 17 | +it('returns the correct component name', function () { |
| 18 | + expect($this->element->getTagName())->toBe('mj-raw'); |
| 19 | +}); |
| 20 | + |
| 21 | +it('has no default attributes', function () { |
| 22 | + // mj-raw has no attributes, so we just verify it doesn't error |
| 23 | + expect($this->element)->toBeInstanceOf(MjRaw::class); |
| 24 | +}); |
| 25 | + |
| 26 | +it('will correctly render raw HTML', function () { |
| 27 | + $rawNode = new MjmlNode( |
| 28 | + 'mj-raw', |
| 29 | + null, |
| 30 | + '<div style="color: red;">Raw HTML Content</div>', |
| 31 | + true, |
| 32 | + null |
| 33 | + ); |
| 34 | + |
| 35 | + $factory = new ElementFactory(); |
| 36 | + $mjRawElement = $factory->create($rawNode); |
| 37 | + |
| 38 | + expect($mjRawElement)->toBeInstanceOf(MjRaw::class); |
| 39 | + |
| 40 | + $out = $mjRawElement->render(); |
| 41 | + |
| 42 | + expect($out)->toBe('<div style="color: red;">Raw HTML Content</div>'); |
| 43 | + expect($out)->not->toBeEmpty(); |
| 44 | +}); |
| 45 | + |
| 46 | +it('will pass through complex HTML unchanged', function () { |
| 47 | + $complexHtml = '<table><tr><td style="padding: 10px;"><a href="https://example.com">Link</a></td></tr></table>'; |
| 48 | + |
| 49 | + $rawNode = new MjmlNode( |
| 50 | + 'mj-raw', |
| 51 | + null, |
| 52 | + $complexHtml, |
| 53 | + true, |
| 54 | + null |
| 55 | + ); |
| 56 | + |
| 57 | + $factory = new ElementFactory(); |
| 58 | + $mjRawElement = $factory->create($rawNode); |
| 59 | + |
| 60 | + $out = $mjRawElement->render(); |
| 61 | + |
| 62 | + expect($out)->toBe($complexHtml); |
| 63 | +}); |
| 64 | + |
| 65 | +it('will preserve whitespace and formatting', function () { |
| 66 | + $htmlWithWhitespace = " <div>\n <p>Paragraph</p>\n </div> "; |
| 67 | + |
| 68 | + $rawNode = new MjmlNode( |
| 69 | + 'mj-raw', |
| 70 | + null, |
| 71 | + $htmlWithWhitespace, |
| 72 | + true, |
| 73 | + null |
| 74 | + ); |
| 75 | + |
| 76 | + $factory = new ElementFactory(); |
| 77 | + $mjRawElement = $factory->create($rawNode); |
| 78 | + |
| 79 | + $out = $mjRawElement->render(); |
| 80 | + |
| 81 | + expect($out)->toBe($htmlWithWhitespace); |
| 82 | +}); |
| 83 | + |
| 84 | +it('will pass through empty content', function () { |
| 85 | + $rawNode = new MjmlNode( |
| 86 | + 'mj-raw', |
| 87 | + null, |
| 88 | + '', |
| 89 | + true, |
| 90 | + null |
| 91 | + ); |
| 92 | + |
| 93 | + $factory = new ElementFactory(); |
| 94 | + $mjRawElement = $factory->create($rawNode); |
| 95 | + |
| 96 | + $out = $mjRawElement->render(); |
| 97 | + |
| 98 | + expect($out)->toBe(''); |
| 99 | +}); |
| 100 | + |
| 101 | +it('will pass through HTML comments', function () { |
| 102 | + $htmlWithComments = '<!-- This is a comment --><div>Content</div><!-- Another comment -->'; |
| 103 | + |
| 104 | + $rawNode = new MjmlNode( |
| 105 | + 'mj-raw', |
| 106 | + null, |
| 107 | + $htmlWithComments, |
| 108 | + true, |
| 109 | + null |
| 110 | + ); |
| 111 | + |
| 112 | + $factory = new ElementFactory(); |
| 113 | + $mjRawElement = $factory->create($rawNode); |
| 114 | + |
| 115 | + $out = $mjRawElement->render(); |
| 116 | + |
| 117 | + expect($out)->toBe($htmlWithComments); |
| 118 | +}); |
| 119 | + |
| 120 | +it('will pass through scripts and styles', function () { |
| 121 | + $htmlWithScript = '<script>console.log("test");</script><style>.class { color: blue; }</style>'; |
| 122 | + |
| 123 | + $rawNode = new MjmlNode( |
| 124 | + 'mj-raw', |
| 125 | + null, |
| 126 | + $htmlWithScript, |
| 127 | + true, |
| 128 | + null |
| 129 | + ); |
| 130 | + |
| 131 | + $factory = new ElementFactory(); |
| 132 | + $mjRawElement = $factory->create($rawNode); |
| 133 | + |
| 134 | + $out = $mjRawElement->render(); |
| 135 | + |
| 136 | + expect($out)->toBe($htmlWithScript); |
| 137 | +}); |
| 138 | + |
| 139 | +it('will pass through special characters', function () { |
| 140 | + $htmlWithSpecialChars = '<div> ©™<>&"\'</div>'; |
| 141 | + |
| 142 | + $rawNode = new MjmlNode( |
| 143 | + 'mj-raw', |
| 144 | + null, |
| 145 | + $htmlWithSpecialChars, |
| 146 | + true, |
| 147 | + null |
| 148 | + ); |
| 149 | + |
| 150 | + $factory = new ElementFactory(); |
| 151 | + $mjRawElement = $factory->create($rawNode); |
| 152 | + |
| 153 | + $out = $mjRawElement->render(); |
| 154 | + |
| 155 | + expect($out)->toBe($htmlWithSpecialChars); |
| 156 | +}); |
| 157 | + |
| 158 | +it('returns empty styles array', function () { |
| 159 | + $styles = $this->element->getStyles(); |
| 160 | + expect($styles)->toBeArray(); |
| 161 | + expect($styles)->toBeEmpty(); |
| 162 | +}); |
0 commit comments