Make WordPress Core

Ticket #64567: 64567-tests.diff

File 64567-tests.diff, 3.9 KB (added by motylanogha, 8 weeks ago)

Unit tests for get_attribute_names_with_prefix() (no-flush path). Pass against PR #10828, fail on trunk.

  • tests/phpunit/tests/html-api/wpHtmlTagProcessor.php

    diff --git a/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php b/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php
    index 22ace3890f..5720a96414 100644
    a b class Tests_HtmlApi_WpHtmlTagProcessor extends WP_UnitTestCase {  
    460460                );
    461461        }
    462462
     463        /**
     464         * Ensures that an attribute added via set_attribute() is reported by
     465         * get_attribute_names_with_prefix() without first flushing the updates
     466         * through get_updated_html().
     467         *
     468         * @ticket 64567
     469         *
     470         * @covers WP_HTML_Tag_Processor::get_attribute_names_with_prefix
     471         */
     472        public function test_get_attribute_names_with_prefix_reflects_set_attribute_without_flushing() {
     473                $processor = new WP_HTML_Tag_Processor( '<div>Test</div>' );
     474                $processor->next_tag();
     475                $processor->set_attribute( 'id', 'test' );
     476
     477                $this->assertSame(
     478                        array( 'id' ),
     479                        $processor->get_attribute_names_with_prefix( '' ),
     480                        'Did not report an attribute enqueued via set_attribute() before the updates were flushed.'
     481                );
     482        }
     483
     484        /**
     485         * Ensures that an attribute removed via remove_attribute() is no longer
     486         * reported by get_attribute_names_with_prefix() without first flushing the
     487         * updates through get_updated_html().
     488         *
     489         * @ticket 64567
     490         *
     491         * @covers WP_HTML_Tag_Processor::get_attribute_names_with_prefix
     492         */
     493        public function test_get_attribute_names_with_prefix_reflects_removed_attribute_without_flushing() {
     494                $processor = new WP_HTML_Tag_Processor( '<div id="main" data-foo="bar">Test</div>' );
     495                $processor->next_tag();
     496                $processor->remove_attribute( 'data-foo' );
     497
     498                $this->assertSame(
     499                        array(),
     500                        $processor->get_attribute_names_with_prefix( 'data-' ),
     501                        'Reported an attribute that had been enqueued for removal before the updates were flushed.'
     502                );
     503                $this->assertSame(
     504                        array( 'id' ),
     505                        $processor->get_attribute_names_with_prefix( '' ),
     506                        'Did not reflect a removed attribute in the full attribute name list before flushing.'
     507                );
     508        }
     509
     510        /**
     511         * Ensures that a class enqueued via add_class() surfaces the `class`
     512         * attribute in get_attribute_names_with_prefix() without first flushing the
     513         * updates through get_updated_html().
     514         *
     515         * @ticket 64567
     516         *
     517         * @covers WP_HTML_Tag_Processor::get_attribute_names_with_prefix
     518         */
     519        public function test_get_attribute_names_with_prefix_reflects_added_class_without_flushing() {
     520                $processor = new WP_HTML_Tag_Processor( '<div>Test</div>' );
     521                $processor->next_tag();
     522                $processor->add_class( 'highlight' );
     523
     524                $this->assertSame(
     525                        array( 'class' ),
     526                        $processor->get_attribute_names_with_prefix( 'class' ),
     527                        'Did not report the class attribute enqueued via add_class() before the updates were flushed.'
     528                );
     529        }
     530
     531        /**
     532         * Ensures get_attribute_names_with_prefix() agrees with get_attribute()
     533         * after pending updates, returning each name once with no stale entries.
     534         *
     535         * @ticket 64567
     536         *
     537         * @covers WP_HTML_Tag_Processor::get_attribute_names_with_prefix
     538         */
     539        public function test_get_attribute_names_with_prefix_agrees_with_get_attribute_after_updates() {
     540                $processor = new WP_HTML_Tag_Processor( '<div data-keep="1" data-drop="2">Test</div>' );
     541                $processor->next_tag();
     542                $processor->set_attribute( 'data-keep', 'updated' );
     543                $processor->set_attribute( 'data-add', 'new' );
     544                $processor->remove_attribute( 'data-drop' );
     545
     546                $names = $processor->get_attribute_names_with_prefix( 'data-' );
     547                sort( $names );
     548
     549                $this->assertSame(
     550                        array( 'data-add', 'data-keep' ),
     551                        $names,
     552                        'Enqueued additions, updates, and removals were not reflected accurately, or a name was duplicated.'
     553                );
     554
     555                foreach ( $names as $name ) {
     556                        $this->assertNotNull(
     557                                $processor->get_attribute( $name ),
     558                                "get_attribute_names_with_prefix() reported \"{$name}\" but get_attribute() did not agree."
     559                        );
     560                }
     561        }
     562
    463563        /**
    464564         * @ticket 56299
    465565         *