|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace CustomGento\Cookiebot\Test\Unit\Model; |
| 6 | + |
| 7 | +use CustomGento\Cookiebot\Model\ExternalVideoReplacer; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +class ExternalVideoReplacerTest extends TestCase |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @var ExternalVideoReplacer |
| 14 | + */ |
| 15 | + private $externalVideoReplacer; |
| 16 | + |
| 17 | + protected function setUp(): void |
| 18 | + { |
| 19 | + $this->externalVideoReplacer = new ExternalVideoReplacer(); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * @dataProvider iframeDataProvider |
| 24 | + */ |
| 25 | + public function testReplaceIframeSources(string $input, string $expected): void |
| 26 | + { |
| 27 | + $result = $this->externalVideoReplacer->replaceIframeSources($input); |
| 28 | + $this->assertEquals($expected, $result); |
| 29 | + } |
| 30 | + |
| 31 | + public function iframeDataProvider(): array |
| 32 | + { |
| 33 | + return [ |
| 34 | + // YouTube test cases |
| 35 | + 'youtube.com embed' => [ |
| 36 | + '<iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ" width="560" height="315"></iframe>', |
| 37 | + '<iframe data-cookieblock-src="https://www.youtube.com/embed/dQw4w9WgXcQ" data-cookieconsent="marketing" width="560" height="315"></iframe>' |
| 38 | + ], |
| 39 | + 'youtube-nocookie.com embed' => [ |
| 40 | + '<iframe src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" width="560" height="315"></iframe>', |
| 41 | + '<iframe data-cookieblock-src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ" data-cookieconsent="marketing" width="560" height="315"></iframe>' |
| 42 | + ], |
| 43 | + 'youtu.be URL' => [ |
| 44 | + '<iframe src="https://youtu.be/dQw4w9WgXcQ" width="560" height="315"></iframe>', |
| 45 | + '<iframe data-cookieblock-src="https://youtu.be/dQw4w9WgXcQ" data-cookieconsent="marketing" width="560" height="315"></iframe>' |
| 46 | + ], |
| 47 | + 'youtube.com without www' => [ |
| 48 | + '<iframe src="https://youtube.com/embed/dQw4w9WgXcQ" width="560" height="315"></iframe>', |
| 49 | + '<iframe data-cookieblock-src="https://youtube.com/embed/dQw4w9WgXcQ" data-cookieconsent="marketing" width="560" height="315"></iframe>' |
| 50 | + ], |
| 51 | + 'youtube-nocookie.com without www' => [ |
| 52 | + '<iframe src="https://youtube-nocookie.com/embed/dQw4w9WgXcQ" width="560" height="315"></iframe>', |
| 53 | + '<iframe data-cookieblock-src="https://youtube-nocookie.com/embed/dQw4w9WgXcQ" data-cookieconsent="marketing" width="560" height="315"></iframe>' |
| 54 | + ], |
| 55 | + 'youtu.be without www' => [ |
| 56 | + '<iframe src="https://youtu.be/dQw4w9WgXcQ" width="560" height="315"></iframe>', |
| 57 | + '<iframe data-cookieblock-src="https://youtu.be/dQw4w9WgXcQ" data-cookieconsent="marketing" width="560" height="315"></iframe>' |
| 58 | + ], |
| 59 | + 'http instead of https for youtube' => [ |
| 60 | + '<iframe src="http://www.youtube.com/embed/dQw4w9WgXcQ" width="560" height="315"></iframe>', |
| 61 | + '<iframe data-cookieblock-src="http://www.youtube.com/embed/dQw4w9WgXcQ" data-cookieconsent="marketing" width="560" height="315"></iframe>' |
| 62 | + ], |
| 63 | + |
| 64 | + // Vimeo test cases |
| 65 | + 'vimeo.com embed' => [ |
| 66 | + '<iframe src="https://vimeo.com/123456789" width="640" height="360"></iframe>', |
| 67 | + '<iframe data-cookieblock-src="https://vimeo.com/123456789" data-cookieconsent="marketing" width="640" height="360"></iframe>' |
| 68 | + ], |
| 69 | + 'player.vimeo.com embed' => [ |
| 70 | + '<iframe src="https://player.vimeo.com/video/123456789" width="640" height="360"></iframe>', |
| 71 | + '<iframe data-cookieblock-src="https://player.vimeo.com/video/123456789" data-cookieconsent="marketing" width="640" height="360"></iframe>' |
| 72 | + ], |
| 73 | + 'www.vimeo.com embed' => [ |
| 74 | + '<iframe src="https://www.vimeo.com/123456789" width="640" height="360"></iframe>', |
| 75 | + '<iframe data-cookieblock-src="https://www.vimeo.com/123456789" data-cookieconsent="marketing" width="640" height="360"></iframe>' |
| 76 | + ], |
| 77 | + 'www.player.vimeo.com embed' => [ |
| 78 | + '<iframe src="https://www.player.vimeo.com/video/123456789" width="640" height="360"></iframe>', |
| 79 | + '<iframe data-cookieblock-src="https://www.player.vimeo.com/video/123456789" data-cookieconsent="marketing" width="640" height="360"></iframe>' |
| 80 | + ], |
| 81 | + 'http vimeo' => [ |
| 82 | + '<iframe src="http://vimeo.com/123456789" width="640" height="360"></iframe>', |
| 83 | + '<iframe data-cookieblock-src="http://vimeo.com/123456789" data-cookieconsent="marketing" width="640" height="360"></iframe>' |
| 84 | + ], |
| 85 | + // General test cases |
| 86 | + 'single quotes' => [ |
| 87 | + '<iframe src=\'https://www.youtube.com/embed/dQw4w9WgXcQ\' width="560" height="315"></iframe>', |
| 88 | + '<iframe data-cookieblock-src="https://www.youtube.com/embed/dQw4w9WgXcQ" data-cookieconsent="marketing" width="560" height="315"></iframe>' |
| 89 | + ], |
| 90 | + 'multiple iframes different services' => [ |
| 91 | + '<iframe src="https://www.youtube.com/embed/video1"></iframe><iframe src="https://vimeo.com/video2"></iframe><iframe src="https://www.google.com/maps/embed?pb=test"></iframe>', |
| 92 | + '<iframe data-cookieblock-src="https://www.youtube.com/embed/video1" data-cookieconsent="marketing"></iframe><iframe data-cookieblock-src="https://vimeo.com/video2" data-cookieconsent="marketing"></iframe><iframe src="https://www.google.com/maps/embed?pb=test"></iframe>' |
| 93 | + ], |
| 94 | + 'mixed content' => [ |
| 95 | + '<p>Some text</p><iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ"></iframe><p>More text</p><iframe src="https://vimeo.com/123456789"></iframe>', |
| 96 | + '<p>Some text</p><iframe data-cookieblock-src="https://www.youtube.com/embed/dQw4w9WgXcQ" data-cookieconsent="marketing"></iframe><p>More text</p><iframe data-cookieblock-src="https://vimeo.com/123456789" data-cookieconsent="marketing"></iframe>' |
| 97 | + ], |
| 98 | + 'iframe with frameborder and allowfullscreen' => [ |
| 99 | + '<iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen=""></iframe>', |
| 100 | + '<iframe data-cookieblock-src="https://www.youtube.com/embed/dQw4w9WgXcQ" data-cookieconsent="marketing" frameborder="0" allowfullscreen=""></iframe>' |
| 101 | + ], |
| 102 | + 'iframe with existing data-cookieconsent' => [ |
| 103 | + '<iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ" data-cookieconsent="marketing"></iframe>', |
| 104 | + '<iframe data-cookieblock-src="https://www.youtube.com/embed/dQw4w9WgXcQ" data-cookieconsent="marketing"></iframe>' |
| 105 | + ], |
| 106 | + 'iframe with different data-cookieconsent value' => [ |
| 107 | + '<iframe src="https://vimeo.com/123456789" data-cookieconsent="statistics"></iframe>', |
| 108 | + '<iframe data-cookieblock-src="https://vimeo.com/123456789" data-cookieconsent="statistics"></iframe>' |
| 109 | + ], |
| 110 | + 'no matching iframe' => [ |
| 111 | + '<iframe src="https://example.com/video"></iframe>', |
| 112 | + '<iframe src="https://example.com/video"></iframe>' |
| 113 | + ], |
| 114 | + 'no iframe at all' => [ |
| 115 | + '<p>Just some text content</p>', |
| 116 | + '<p>Just some text content</p>' |
| 117 | + ], |
| 118 | + 'empty content' => [ |
| 119 | + '', |
| 120 | + '' |
| 121 | + ] |
| 122 | + ]; |
| 123 | + } |
| 124 | +} |
0 commit comments