|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Unit tests covering WP_HTML_Processor META tag handling. |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + * @subpackage HTML-API |
| 7 | + * |
| 8 | + * @since 6.9 |
| 9 | + * |
| 10 | + * @group html-api |
| 11 | + * |
| 12 | + * @coversDefaultClass WP_HTML_Processor |
| 13 | + */ |
| 14 | +class Tests_HtmlApi_WpHtmlProcessorMetaTag extends WP_UnitTestCase { |
| 15 | + /** |
| 16 | + * Data provider. |
| 17 | + */ |
| 18 | + public static function data_supported_meta_tags(): array { |
| 19 | + return array( |
| 20 | + 'No attributes' => array( '<meta>' ), |
| 21 | + 'Unrelated attributes' => array( '<meta not-charset="OK">' ), |
| 22 | + 'Boolean charset' => array( '<meta charset>' ), |
| 23 | + 'HTTP Equiv: accept' => array( '<meta http-equiv="accept" content="">' ), |
| 24 | + 'HTTP Equiv: content-type, no content' => array( '<meta http-equiv="content-type">' ), |
| 25 | + 'Boolean HTTP Equiv' => array( '<meta http-equiv content="">' ), |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Ensures that META tags correctly handle encoding confidence. |
| 31 | + * |
| 32 | + * @ticket 63738 |
| 33 | + * |
| 34 | + * @dataProvider data_supported_meta_tags |
| 35 | + */ |
| 36 | + public function test_supported_meta_tag( string $html ) { |
| 37 | + $html = '<!DOCTYPE html>' . $html; |
| 38 | + $processor = new class($html) extends WP_HTML_Processor { |
| 39 | + public function __construct( $html ) { |
| 40 | + parent::__construct( $html, parent::CONSTRUCTOR_UNLOCK_CODE ); |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + $this->assertTrue( $processor->next_tag( 'META' ) ); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Data provider. |
| 49 | + */ |
| 50 | + public function data_unsupported_meta_tags(): array { |
| 51 | + return array( |
| 52 | + 'With charset' => array( '<meta charset="utf8">', 'Cannot yet process META tags with charset to determine encoding.' ), |
| 53 | + 'With CHARSET' => array( '<meta CHARSET="utf8">', 'Cannot yet process META tags with charset to determine encoding.' ), |
| 54 | + 'With http-equiv' => array( '<meta http-equiv="content-type" content="">', 'Cannot yet process META tags with http-equiv Content-Type to determine encoding.' ), |
| 55 | + 'With http-equiv and content' => array( '<meta http-equiv="Content-Type" content="UTF-8">', 'Cannot yet process META tags with http-equiv Content-Type to determine encoding.' ), |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Ensures that unsupported encoding META tags bail. |
| 61 | + * |
| 62 | + * @ticket 63738 |
| 63 | + * |
| 64 | + * @dataProvider data_unsupported_meta_tags |
| 65 | + */ |
| 66 | + public function test_unsupported_meta_tags( string $html, string $unsupported_message ) { |
| 67 | + $html = '<!DOCTYPE html>' . $html; |
| 68 | + $processor = new class($html) extends WP_HTML_Processor { |
| 69 | + public function __construct( $html ) { |
| 70 | + parent::__construct( $html, parent::CONSTRUCTOR_UNLOCK_CODE ); |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + $this->assertFalse( $processor->next_tag( 'META' ) ); |
| 75 | + $this->assertInstanceOf( WP_HTML_Unsupported_Exception::class, $processor->get_unsupported_exception() ); |
| 76 | + } |
| 77 | +} |
0 commit comments