Make WordPress Core


Ignore:
Timestamp:
06/04/2024 07:16:48 AM (2 years ago)
Author:
gziolo
Message:

Interactivity API: Print debug warning when server directives processing encounters errors

Aims to improve the developer experience of the Interactivity API server directives processing.

Props cbravobernal, jonsurrell, westonruter, darerodz, czapla, gziolo.
Fixes #61044.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php

    r58320 r58321  
    650650         * @dataProvider data_html_with_unbalanced_tags
    651651         *
     652         * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args
     653         *
    652654         * @param string $html HTML containing unbalanced tags and also a directive.
    653655         */
    … …  
    697699                $html           = '
    698700                        <header>
    699                                 <svg height="100" data-wp-bind--width="myPlugin::state.width">
     701                                <svg height="100">
    700702                                        <title>Red Circle</title>
    701703                                        <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
    702704                                </svg>
    703705                                <div data-wp-bind--id="myPlugin::state.id"></div>
    704                                 <div data-wp-bind--id="myPlugin::state.width"></div>
    705706                        </header>
    706707                ';
    707708                $processed_html = $this->interactivity->process_directives( $html );
    708709                $p              = new WP_HTML_Tag_Processor( $processed_html );
    709                 $p->next_tag( 'svg' );
    710                 $this->assertNull( $p->get_attribute( 'width' ) );
    711710                $p->next_tag( 'div' );
    712711                $this->assertEquals( 'some-id', $p->get_attribute( 'id' ) );
    713                 $p->next_tag( 'div' );
    714                 $this->assertEquals( '100', $p->get_attribute( 'id' ) );
    715712        }
    716713
    … …  
    722719         *
    723720         * @covers ::process_directives
     721         * @expectedIncorrectUsage WP_Interactivity_API_Directives_Processor::skip_to_tag_closer
    724722         */
    725723        public function test_process_directives_does_not_change_inner_html_in_svgs() {
    … …  
    751749         *
    752750         * @covers ::process_directives
     751         * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args
    753752         */
    754753        public function test_process_directives_change_html_if_contains_math() {
    … …  
    785784         *
    786785         * @covers ::process_directives
     786         * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args
     787         * @expectedIncorrectUsage WP_Interactivity_API_Directives_Processor::skip_to_tag_closer
    787788         */
    788789        public function test_process_directives_does_not_change_inner_html_in_math() {
    … …  
    812813         * Invokes the private `evaluate` method of WP_Interactivity_API class.
    813814         *
    814          * @param string $directive_value The directive attribute value to evaluate.
     815         * @param string $directive_value   The directive attribute value to evaluate.
     816         * @param string $default_namespace The default namespace used with directives.
    815817         * @return mixed The result of the evaluate method.
    816818         */
    817         private function evaluate( $directive_value ) {
     819        private function evaluate( $directive_value, $default_namespace = 'myPlugin' ) {
    818820                $generate_state = function ( $name ) {
    819821                        $obj       = new stdClass();
    … …  
    848850                $evaluate = new ReflectionMethod( $this->interactivity, 'evaluate' );
    849851                $evaluate->setAccessible( true );
    850                 return $evaluate->invokeArgs( $this->interactivity, array( $directive_value, 'myPlugin', $context ) );
     852                return $evaluate->invokeArgs( $this->interactivity, array( $directive_value, $default_namespace, $context ) );
    851853        }
    852854
    … …  
    946948                $result = $this->evaluate( 'otherPlugin::context.nested.key' );
    947949                $this->assertEquals( 'otherPlugin-context-nested', $result );
     950        }
     951
     952        /**
     953         * Tests the `evaluate` method for non valid namespace values.
     954         *
     955         * @ticket 61044
     956         *
     957         * @covers ::evaluate
     958         * @expectedIncorrectUsage WP_Interactivity_API::evaluate
     959         */
     960        public function test_evaluate_unvalid_namespaces() {
     961                $result = $this->evaluate( 'path', 'null' );
     962                $this->assertNull( $result );
     963
     964                $result = $this->evaluate( 'path', '' );
     965                $this->assertNull( $result );
     966
     967                $result = $this->evaluate( 'path', '{}' );
     968                $this->assertNull( $result );
    948969        }
    949970
Note: See TracChangeset for help on using the changeset viewer.