Make WordPress Core


Ignore:
Timestamp:
02/01/2024 01:00:56 AM (3 years ago)
Author:
dmsnell
Message:

HTML API: Test cleanup

Rename $p variable to $processor in tests for clarity.

Use static data providers. A mix of static and non-static data providers were
used in HTML API tests. Data providers are required to be static in the next
PHPUnit version and there's no harm in using them consistently now.

Follow-up to [57507]

Props jonsurrell
See #59647

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/html-api/wpHtmlProcessorBreadcrumbs.php

    r57348 r57508  
    2424         */
    2525        public function test_navigates_into_normative_html_for_supported_elements( $html, $tag_name ) {
    26                 $p = WP_HTML_Processor::create_fragment( $html );
    27 
    28                 $this->assertTrue( $p->step(), "Failed to step into supported {$tag_name} element." );
    29                 $this->assertSame( $tag_name, $p->get_tag(), "Misread {$tag_name} as a {$p->get_tag()} element." );
     26                $processor = WP_HTML_Processor::create_fragment( $html );
     27
     28                $this->assertTrue( $processor->step(), "Failed to step into supported {$tag_name} element." );
     29                $this->assertSame( $tag_name, $processor->get_tag(), "Misread {$tag_name} as a {$processor->get_tag()} element." );
    3030        }
    3131
     
    3535         * @return array[]
    3636         */
    37         public function data_single_tag_of_supported_elements() {
     37        public static function data_single_tag_of_supported_elements() {
    3838                $supported_elements = array(
    3939                        'A',
     
    156156         */
    157157        public function test_fails_when_encountering_unsupported_tag( $html ) {
    158                 $p = WP_HTML_Processor::create_fragment( $html );
    159 
    160                 $this->assertFalse( $p->step(), "Should not have stepped into unsupported {$p->get_tag()} element." );
     158                $processor = WP_HTML_Processor::create_fragment( $html );
     159
     160                $this->assertFalse( $processor->step(), "Should not have stepped into unsupported {$processor->get_tag()} element." );
    161161        }
    162162
     
    166166         * @return array[]
    167167         */
    168         public function data_unsupported_elements() {
     168        public static function data_unsupported_elements() {
    169169                $unsupported_elements = array(
    170170                        'APPLET', // Deprecated.
     
    230230         */
    231231        public function test_fails_when_encountering_unsupported_markup( $html, $description ) {
    232                 $p = WP_HTML_Processor::create_fragment( $html );
    233 
    234                 while ( $p->step() && null === $p->get_attribute( 'supported' ) ) {
     232                $processor = WP_HTML_Processor::create_fragment( $html );
     233
     234                while ( $processor->step() && null === $processor->get_attribute( 'supported' ) ) {
    235235                        continue;
    236236                }
    237237
    238                 $this->assertTrue( $p->get_attribute( 'supported' ), 'Did not find required supported element.' );
    239                 $this->assertFalse( $p->step(), "Didn't properly reject unsupported markup: {$description}" );
     238                $this->assertTrue( $processor->get_attribute( 'supported' ), 'Did not find required supported element.' );
     239                $this->assertFalse( $processor->step(), "Didn't properly reject unsupported markup: {$description}" );
    240240        }
    241241
     
    245245         * @return array[]
    246246         */
    247         public function data_unsupported_markup() {
     247        public static function data_unsupported_markup() {
    248248                return array(
    249249                        'A with formatting following unclosed A' => array(
     
    271271         */
    272272        public function test_finds_correct_tag_given_breadcrumbs( $html, $breadcrumbs, $n ) {
    273                 $p = WP_HTML_Processor::create_fragment( $html );
    274 
    275                 $p->next_tag(
     273                $processor = WP_HTML_Processor::create_fragment( $html );
     274
     275                $processor->next_tag(
    276276                        array(
    277277                                'breadcrumbs'  => $breadcrumbs,
     
    280280                );
    281281
    282                 $this->assertNotNull( $p->get_tag(), 'Failed to find target node.' );
    283                 $this->assertTrue( $p->get_attribute( 'target' ), "Found {$p->get_tag()} element didn't contain the necessary 'target' attribute." );
     282                $this->assertNotNull( $processor->get_tag(), 'Failed to find target node.' );
     283                $this->assertTrue( $processor->get_attribute( 'target' ), "Found {$processor->get_tag()} element didn't contain the necessary 'target' attribute." );
    284284        }
    285285
     
    296296         */
    297297        public function test_reports_correct_breadcrumbs_for_html( $html, $breadcrumbs, $ignored_n ) {
    298                 $p = WP_HTML_Processor::create_fragment( $html );
    299 
    300                 while ( $p->next_tag() && null === $p->get_attribute( 'target' ) ) {
     298                $processor = WP_HTML_Processor::create_fragment( $html );
     299
     300                while ( $processor->next_tag() && null === $processor->get_attribute( 'target' ) ) {
    301301                        continue;
    302302                }
    303303
    304                 $this->assertNotNull( $p->get_tag(), 'Failed to find the target node.' );
    305                 $this->assertSame( $breadcrumbs, $p->get_breadcrumbs(), 'Found the wrong path from the root of the HTML document to the target node.' );
     304                $this->assertNotNull( $processor->get_tag(), 'Failed to find the target node.' );
     305                $this->assertSame( $breadcrumbs, $processor->get_breadcrumbs(), 'Found the wrong path from the root of the HTML document to the target node.' );
    306306        }
    307307
     
    311311         * @return array[]
    312312         */
    313         public function data_html_target_with_breadcrumbs() {
     313        public static function data_html_target_with_breadcrumbs() {
    314314                return array(
    315315                        'Simple IMG tag'                        => array( '<img target>', array( 'HTML', 'BODY', 'IMG' ), 1 ),
     
    394394         * @return array[].
    395395         */
    396         public function data_html_with_breadcrumbs_of_various_specificity() {
     396        public static function data_html_with_breadcrumbs_of_various_specificity() {
    397397                return array(
    398398                        // Test with void elements.
     
    434434         */
    435435        public function test_remains_stable_when_editing_attributes() {
    436                 $p = WP_HTML_Processor::create_fragment( '<div><button>First<button><b here>Second' );
    437                 $p->next_tag( array( 'breadcrumbs' => array( 'BUTTON', 'B' ) ) );
     436                $processor = WP_HTML_Processor::create_fragment( '<div><button>First<button><b here>Second' );
     437                $processor->next_tag( array( 'breadcrumbs' => array( 'BUTTON', 'B' ) ) );
    438438
    439439                $this->assertSame(
    440440                        array( 'HTML', 'BODY', 'DIV', 'BUTTON', 'B' ),
    441                         $p->get_breadcrumbs(),
     441                        $processor->get_breadcrumbs(),
    442442                        'Found the wrong nested structure at the matched tag.'
    443443                );
    444444
    445                 $p->set_attribute( 'a-name', 'a-value' );
     445                $processor->set_attribute( 'a-name', 'a-value' );
    446446
    447447                $this->assertTrue(
    448                         $p->get_attribute( 'here' ),
     448                        $processor->get_attribute( 'here' ),
    449449                        'Should have found the B tag but could not find expected "here" attribute.'
    450450                );
     
    452452                $this->assertSame(
    453453                        array( 'HTML', 'BODY', 'DIV', 'BUTTON', 'B' ),
    454                         $p->get_breadcrumbs(),
     454                        $processor->get_breadcrumbs(),
    455455                        'Found the wrong nested structure at the matched tag.'
    456456                );
    457457
    458                 $p->get_updated_html();
     458                $processor->get_updated_html();
    459459
    460460                $this->assertTrue(
    461                         $p->get_attribute( 'here' ),
     461                        $processor->get_attribute( 'here' ),
    462462                        'Should have stayed at the B tag but could not find expected "here" attribute.'
    463463                );
     
    465465                $this->assertSame(
    466466                        array( 'HTML', 'BODY', 'DIV', 'BUTTON', 'B' ),
    467                         $p->get_breadcrumbs(),
     467                        $processor->get_breadcrumbs(),
    468468                        'Found the wrong nested structure at the matched tag after updating attributes.'
    469469                );
     
    480480         */
    481481        public function test_can_modify_attributes_after_finding_tag() {
    482                 $p = WP_HTML_Processor::create_fragment( '<div><figure><img><figcaption>test</figcaption></figure>' );
    483 
    484                 $this->assertTrue( $p->next_tag( array( 'breadcrumbs' => array( 'figcaption' ) ) ), 'Unable to find given tag.' );
    485 
    486                 $p->set_attribute( 'found-it', true );
    487                 $this->assertSame( '<div><figure><img><figcaption found-it>test</figcaption></figure>', $p->get_updated_html() );
     482                $processor = WP_HTML_Processor::create_fragment( '<div><figure><img><figcaption>test</figcaption></figure>' );
     483
     484                $this->assertTrue( $processor->next_tag( array( 'breadcrumbs' => array( 'figcaption' ) ) ), 'Unable to find given tag.' );
     485
     486                $processor->set_attribute( 'found-it', true );
     487                $this->assertSame( '<div><figure><img><figcaption found-it>test</figcaption></figure>', $processor->get_updated_html() );
    488488        }
    489489
     
    498498         */
    499499        public function test_can_query_an_element_by_tag_name() {
    500                 $p = WP_HTML_Processor::create_fragment( '<div><DIV><strong><img></strong></DIV>' );
    501                 $p->next_tag( 'IMG' );
    502                 $p->set_attribute( 'loading', 'lazy' );
    503 
    504                 $this->assertSame( '<div><DIV><strong><img loading="lazy"></strong></DIV>', $p->get_updated_html() );
     500                $processor = WP_HTML_Processor::create_fragment( '<div><DIV><strong><img></strong></DIV>' );
     501                $processor->next_tag( 'IMG' );
     502                $processor->set_attribute( 'loading', 'lazy' );
     503
     504                $this->assertSame( '<div><DIV><strong><img loading="lazy"></strong></DIV>', $processor->get_updated_html() );
    505505        }
    506506
     
    515515         */
    516516        public function test_can_seek_back_and_forth() {
    517                 $p = WP_HTML_Processor::create_fragment(
     517                $processor = WP_HTML_Processor::create_fragment(
    518518                        <<<'HTML'
    519519<div>text<p one>more stuff<div><![CDATA[this is not real CDATA]]><p><!-- hi --><div two><p><div><p>three comes soon<div><p three>' );
     
    522522
    523523                // Find first tag of interest.
    524                 while ( $p->next_tag() && null === $p->get_attribute( 'one' ) ) {
     524                while ( $processor->next_tag() && null === $processor->get_attribute( 'one' ) ) {
    525525                        continue;
    526526                }
    527                 $p->set_bookmark( 'first' );
     527                $processor->set_bookmark( 'first' );
    528528
    529529                // Find second tag of interest.
    530                 while ( $p->next_tag() && null === $p->get_attribute( 'two' ) ) {
     530                while ( $processor->next_tag() && null === $processor->get_attribute( 'two' ) ) {
    531531                        continue;
    532532                }
    533                 $p->set_bookmark( 'second' );
     533                $processor->set_bookmark( 'second' );
    534534
    535535                // Find third tag of interest.
    536                 while ( $p->next_tag() && null === $p->get_attribute( 'three' ) ) {
     536                while ( $processor->next_tag() && null === $processor->get_attribute( 'three' ) ) {
    537537                        continue;
    538538                }
    539                 $p->set_bookmark( 'third' );
     539                $processor->set_bookmark( 'third' );
    540540
    541541                // Seek backwards.
    542                 $p->seek( 'first' );
     542                $processor->seek( 'first' );
    543543
    544544                // Seek forwards. If the current token isn't also updated this could appear like a backwards seek.
    545                 $p->seek( 'second' );
    546                 $this->assertTrue( $p->get_attribute( 'two' ) );
     545                $processor->seek( 'second' );
     546                $this->assertTrue( $processor->get_attribute( 'two' ) );
    547547        }
    548548}
Note: See TracChangeset for help on using the changeset viewer.