Make WordPress Core


Ignore:
Timestamp:
06/04/2024 10:59:01 AM (2 years ago)
Author:
gziolo
Message:

Interactivity API: Directives cannot derive state on the server

The Interactivity API has a concept of "derived state" but it only worked on the client (JavaScript). This is the implementation that mirrors it, so derived state has good server-side solution.

Props jonsurrell, darerodz, gziolo, luisherranz, cbravobernal.
Fixes #61037.

File:
1 edited

Legend:

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

    r58321 r58327  
    3333
    3434        /**
     35         * Modifies the internal namespace stack as if the WP_Interactivity_API
     36         * instance had found `data-wp-interactive` directives during
     37         * `process_directives` execution.
     38         *
     39         * @param array<string> $stack Values for the internal namespace stack.
     40         */
     41        private function set_internal_namespace_stack( ...$stack ) {
     42                $interactivity   = new ReflectionClass( $this->interactivity );
     43                $namespace_stack = $interactivity->getProperty( 'namespace_stack' );
     44                $namespace_stack->setAccessible( true );
     45                $namespace_stack->setValue( $this->interactivity, $stack );
     46        }
     47
     48        /**
     49         * Modifies the internal context stack as if the WP_Interactivity_API
     50         * instance had found `data-wp-context` directives during
     51         * `process_directives` execution.
     52         *
     53         * @param array<array<mixed>> $stack Values for the internal context stack.
     54         */
     55        private function set_internal_context_stack( ...$stack ) {
     56                $interactivity = new ReflectionClass( $this->interactivity );
     57                $context_stack = $interactivity->getProperty( 'context_stack' );
     58                $context_stack->setAccessible( true );
     59                $context_stack->setValue( $this->interactivity, $stack );
     60        }
     61
     62        /**
    3563         * Tests that the state and config methods return an empty array at the
    3664         * beginning.
     
    424452JSON;
    425453                $this->assertEquals( $expected, $interactivity_data_string[1] );
     454        }
     455
     456        /**
     457         * Test that calling state without a namespace arg returns the state data
     458         * for the current namespace in the internal namespace stack.
     459         *
     460         * @ticket 61037
     461         *
     462         * @covers ::state
     463         */
     464        public function test_state_without_namespace() {
     465                $this->set_internal_namespace_stack( 'myPlugin' );
     466
     467                $this->interactivity->state( 'myPlugin', array( 'a' => 1 ) );
     468                $this->interactivity->state( 'otherPlugin', array( 'b' => 2 ) );
     469
     470                $this->assertEquals(
     471                        array( 'a' => 1 ),
     472                        $this->interactivity->state()
     473                );
     474        }
     475
     476        /**
     477         * Test that passing state data without a valid namespace does nothing and
     478         * just returns an empty array.
     479         *
     480         * @ticket 61037
     481         *
     482         * @covers ::state
     483         * @expectedIncorrectUsage WP_Interactivity_API::state
     484         */
     485        public function test_state_with_data_and_invalid_namespace() {
     486                $this->set_internal_namespace_stack( 'myPlugin' );
     487
     488                $this->interactivity->state( 'myPlugin', array( 'a' => 1 ) );
     489                $this->interactivity->state( 'otherPlugin', array( 'b' => 2 ) );
     490
     491                $this->assertEquals(
     492                        array(),
     493                        $this->interactivity->state( null, array( 'newProp' => 'value' ) )
     494                );
     495        }
     496
     497        /**
     498         * Test that calling state with an empty string as namespace is not allowed.
     499         *
     500         * @ticket 61037
     501         *
     502         * @covers ::state
     503         * @expectedIncorrectUsage WP_Interactivity_API::state
     504         */
     505        public function test_state_with_empty_string_as_namespace() {
     506                $this->set_internal_namespace_stack( 'myPlugin' );
     507
     508                $this->interactivity->state( 'myPlugin', array( 'a' => 1 ) );
     509                $this->interactivity->state( 'otherPlugin', array( 'b' => 2 ) );
     510
     511                $this->assertEquals(
     512                        array(),
     513                        $this->interactivity->state( '' )
     514                );
     515        }
     516
     517        /**
     518         * Tests that calling state without namespace outside of
     519         * `process_directives` execution is not allowed.
     520         *
     521         * @ticket 61037
     522         *
     523         * @covers ::state
     524         * @expectedIncorrectUsage WP_Interactivity_API::state
     525         */
     526        public function test_state_without_namespace_outside_directive_processing() {
     527                $this->assertEquals(
     528                        array(),
     529                        $this->interactivity->state()
     530                );
     531        }
     532
     533        /**
     534         * Test that `get_context` returns the latest context value for the given
     535         * namespace.
     536         *
     537         * @ticket 61037
     538         *
     539         * @covers ::get_context
     540         */
     541        public function test_get_context_with_namespace() {
     542                $this->set_internal_namespace_stack( 'myPlugin' );
     543                $this->set_internal_context_stack(
     544                        array(
     545                                'myPlugin' => array( 'a' => 0 ),
     546                        ),
     547                        array(
     548                                'myPlugin'    => array( 'a' => 1 ),
     549                                'otherPlugin' => array( 'b' => 2 ),
     550                        )
     551                );
     552
     553                $this->assertEquals(
     554                        array( 'a' => 1 ),
     555                        $this->interactivity->get_context( 'myPlugin' )
     556                );
     557                $this->assertEquals(
     558                        array( 'b' => 2 ),
     559                        $this->interactivity->get_context( 'otherPlugin' )
     560                );
     561        }
     562
     563        /**
     564         * Test that `get_context` uses the current namespace in the internal
     565         * namespace stack when the parameter is omitted.
     566         *
     567         * @ticket 61037
     568         *
     569         * @covers ::get_context
     570         */
     571        public function test_get_context_without_namespace() {
     572                $this->set_internal_namespace_stack( 'myPlugin' );
     573                $this->set_internal_context_stack(
     574                        array(
     575                                'myPlugin' => array( 'a' => 0 ),
     576                        ),
     577                        array(
     578                                'myPlugin'    => array( 'a' => 1 ),
     579                                'otherPlugin' => array( 'b' => 2 ),
     580                        )
     581                );
     582
     583                $this->assertEquals(
     584                        array( 'a' => 1 ),
     585                        $this->interactivity->get_context()
     586                );
     587        }
     588
     589        /**
     590         * Test that `get_context` returns an empty array when the context stack is
     591         * empty.
     592         *
     593         * @ticket 61037
     594         *
     595         * @covers ::get_context
     596         */
     597        public function test_get_context_with_empty_context_stack() {
     598                $this->set_internal_namespace_stack( 'myPlugin' );
     599                $this->set_internal_context_stack();
     600
     601                $this->assertEquals(
     602                        array(),
     603                        $this->interactivity->get_context( 'myPlugin' )
     604                );
     605        }
     606
     607        /**
     608         * Test that `get_context` returns an empty array if the given namespace is
     609         * not defined.
     610         *
     611         * @ticket 61037
     612         *
     613         * @covers ::get_context
     614         */
     615        public function test_get_context_with_undefined_namespace() {
     616                $this->set_internal_namespace_stack( 'myPlugin' );
     617                $this->set_internal_context_stack(
     618                        array(
     619                                'myPlugin' => array( 'a' => 0 ),
     620                        ),
     621                        array(
     622                                'myPlugin' => array( 'a' => 1 ),
     623                        )
     624                );
     625
     626                $this->assertEquals(
     627                        array(),
     628                        $this->interactivity->get_context( 'otherPlugin' )
     629                );
     630        }
     631
     632        /**
     633         * Test that `get_context` should not be called with an empty string.
     634         *
     635         * @ticket 61037
     636         *
     637         * @covers ::get_context
     638         * @expectedIncorrectUsage WP_Interactivity_API::get_context
     639         */
     640        public function test_get_context_with_empty_namespace() {
     641                $this->set_internal_namespace_stack( 'myPlugin' );
     642                $this->set_internal_context_stack(
     643                        array(
     644                                'myPlugin' => array( 'a' => 0 ),
     645                        ),
     646                        array(
     647                                'myPlugin' => array( 'a' => 1 ),
     648                        )
     649                );
     650
     651                $this->assertEquals(
     652                        array(),
     653                        $this->interactivity->get_context( '' )
     654                );
     655        }
     656
     657
     658        /**
     659         * Tests that `get_context` should not be called outside of
     660         * `process_directives` execution.
     661         *
     662         * @ticket 61037
     663         *
     664         * @covers ::get_context
     665         * @expectedIncorrectUsage WP_Interactivity_API::get_context
     666         */
     667        public function test_get_context_outside_of_directive_processing() {
     668                $context = $this->interactivity->get_context();
     669                $this->assertEquals( array(), $context );
    426670        }
    427671
     
    650894         * @dataProvider data_html_with_unbalanced_tags
    651895         *
    652          * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args
     896         * @expectedIncorrectUsage WP_Interactivity_API::_process_directives
    653897         *
    654898         * @param string $html HTML containing unbalanced tags and also a directive.
     
    749993         *
    750994         * @covers ::process_directives
    751          * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args
     995         * @expectedIncorrectUsage WP_Interactivity_API::_process_directives
    752996         */
    753997        public function test_process_directives_change_html_if_contains_math() {
     
    7841028         *
    7851029         * @covers ::process_directives
    786          * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args
     1030         * @expectedIncorrectUsage WP_Interactivity_API::_process_directives
    7871031         * @expectedIncorrectUsage WP_Interactivity_API_Directives_Processor::skip_to_tag_closer
    7881032         */
     
    8141058         *
    8151059         * @param string $directive_value   The directive attribute value to evaluate.
    816          * @param string $default_namespace The default namespace used with directives.
    8171060         * @return mixed The result of the evaluate method.
    8181061         */
    819         private function evaluate( $directive_value, $default_namespace = 'myPlugin' ) {
    820                 $generate_state = function ( $name ) {
    821                         $obj       = new stdClass();
    822                         $obj->prop = $name;
    823                         return array(
    824                                 'key'       => $name,
    825                                 'nested'    => array( 'key' => $name . '-nested' ),
     1062        private function evaluate( $directive_value ) {
     1063                /*
     1064                 * The global WP_Interactivity_API instance is momentarily replaced to
     1065                 * make global functions like `wp_interactivity_state` and
     1066                 * `wp_interactivity_get_config` work as expected.
     1067                 */
     1068                global $wp_interactivity;
     1069                $wp_interactivity_prev = $wp_interactivity;
     1070                $wp_interactivity      = $this->interactivity;
     1071
     1072                $evaluate = new ReflectionMethod( $this->interactivity, 'evaluate' );
     1073                $evaluate->setAccessible( true );
     1074
     1075                $result = $evaluate->invokeArgs( $this->interactivity, array( $directive_value ) );
     1076
     1077                // Restore the original WP_Interactivity_API instance.
     1078                $wp_interactivity = $wp_interactivity_prev;
     1079
     1080                return $result;
     1081        }
     1082
     1083        /**
     1084         * Tests that the `evaluate` method operates correctly for valid expressions.
     1085         *
     1086         * @ticket 60356
     1087         *
     1088         * @covers ::evaluate
     1089         */
     1090        public function test_evaluate_value() {
     1091                $obj       = new stdClass();
     1092                $obj->prop = 'object property';
     1093                $this->interactivity->state(
     1094                        'myPlugin',
     1095                        array(
     1096                                'key'       => 'myPlugin-state',
    8261097                                'obj'       => $obj,
    8271098                                'arrAccess' => new class() implements ArrayAccess {
    828                                         #[\ReturnTypeWillChange]
    829                                         public function offsetExists( $offset ) {
     1099                                        public function offsetExists( $offset ): bool {
    8301100                                                return true;
    8311101                                        }
    8321102
    833                                         public function offsetGet( $offset ): string {
     1103                                        #[\ReturnTypeWillChange]
     1104                                        public function offsetGet( $offset ) {
    8341105                                                return $offset;
    8351106                                        }
     
    8391110                                        public function offsetUnset( $offset ): void {}
    8401111                                },
    841                         );
    842                 };
    843                 $this->interactivity->state( 'myPlugin', $generate_state( 'myPlugin-state' ) );
    844                 $this->interactivity->state( 'otherPlugin', $generate_state( 'otherPlugin-state' ) );
    845                 $context  = array(
    846                         'myPlugin'    => $generate_state( 'myPlugin-context' ),
    847                         'otherPlugin' => $generate_state( 'otherPlugin-context' ),
    848                         'obj'         => new stdClass(),
    849                 );
    850                 $evaluate = new ReflectionMethod( $this->interactivity, 'evaluate' );
    851                 $evaluate->setAccessible( true );
    852                 return $evaluate->invokeArgs( $this->interactivity, array( $directive_value, $default_namespace, $context ) );
    853         }
    854 
    855         /**
    856          * Tests that the `evaluate` method operates correctly for valid expressions.
    857          *
    858          * @ticket 60356
    859          *
    860          * @covers ::evaluate
    861          */
    862         public function test_evaluate_value() {
     1112                        )
     1113                );
     1114                $this->interactivity->state( 'otherPlugin', array( 'key' => 'otherPlugin-state' ) );
     1115                $this->set_internal_context_stack(
     1116                        array(
     1117                                'myPlugin'    => array( 'key' => 'myPlugin-context' ),
     1118                                'otherPlugin' => array( 'key' => 'otherPlugin-context' ),
     1119                        )
     1120                );
     1121                $this->set_internal_namespace_stack( 'myPlugin' );
     1122
    8631123                $result = $this->evaluate( 'state.key' );
    8641124                $this->assertEquals( 'myPlugin-state', $result );
     
    8741134
    8751135                $result = $this->evaluate( 'state.obj.prop' );
    876                 $this->assertSame( 'myPlugin-state', $result );
     1136                $this->assertSame( 'object property', $result );
    8771137
    8781138                $result = $this->evaluate( 'state.arrAccess.1' );
     
    8891149         */
    8901150        public function test_evaluate_value_negation() {
     1151                $this->interactivity->state( 'myPlugin', array( 'key' => 'myPlugin-state' ) );
     1152                $this->interactivity->state( 'otherPlugin', array( 'key' => 'otherPlugin-state' ) );
     1153                $this->set_internal_context_stack(
     1154                        array(
     1155                                'myPlugin'    => array( 'key' => 'myPlugin-context' ),
     1156                                'otherPlugin' => array( 'key' => 'otherPlugin-context' ),
     1157                        )
     1158                );
     1159                $this->set_internal_namespace_stack( 'myPlugin' );
     1160
    8911161                $result = $this->evaluate( '!state.key' );
    8921162                $this->assertFalse( $result );
     
    9101180         */
    9111181        public function test_evaluate_non_existent_path() {
     1182                $this->interactivity->state( 'myPlugin', array( 'key' => 'myPlugin-state' ) );
     1183                $this->interactivity->state( 'otherPlugin', array( 'key' => 'otherPlugin-state' ) );
     1184                $this->set_internal_context_stack(
     1185                        array(
     1186                                'myPlugin'    => array( 'key' => 'myPlugin-context' ),
     1187                                'otherPlugin' => array( 'key' => 'otherPlugin-context' ),
     1188                        )
     1189                );
     1190                $this->set_internal_namespace_stack( 'myPlugin' );
     1191
    9121192                $result = $this->evaluate( 'state.nonExistentKey' );
    9131193                $this->assertNull( $result );
     
    9371217         */
    9381218        public function test_evaluate_nested_value() {
     1219                $this->interactivity->state(
     1220                        'myPlugin',
     1221                        array(
     1222                                'nested' => array( 'key' => 'myPlugin-state-nested' ),
     1223                        )
     1224                );
     1225                $this->interactivity->state(
     1226                        'otherPlugin',
     1227                        array(
     1228                                'nested' => array( 'key' => 'otherPlugin-state-nested' ),
     1229                        )
     1230                );
     1231                $this->set_internal_context_stack(
     1232                        array(
     1233                                'myPlugin'    => array(
     1234                                        'nested' => array( 'key' => 'myPlugin-context-nested' ),
     1235                                ),
     1236                                'otherPlugin' => array(
     1237                                        'nested' => array( 'key' => 'otherPlugin-context-nested' ),
     1238                                ),
     1239                        )
     1240                );
     1241                $this->set_internal_namespace_stack( 'myPlugin' );
     1242
    9391243                $result = $this->evaluate( 'state.nested.key' );
    9401244                $this->assertEquals( 'myPlugin-state-nested', $result );
     
    9591263         */
    9601264        public function test_evaluate_unvalid_namespaces() {
     1265                $this->set_internal_context_stack( array() );
     1266                $this->set_internal_namespace_stack();
     1267
    9611268                $result = $this->evaluate( 'path', 'null' );
    9621269                $this->assertNull( $result );
     
    9661273
    9671274                $result = $this->evaluate( 'path', '{}' );
     1275                $this->assertNull( $result );
     1276        }
     1277
     1278        /**
     1279         * Tests the `evaluate` method for derived state functions.
     1280         *
     1281         * @ticket 61037
     1282         *
     1283         * @covers ::evaluate
     1284         * @covers wp_interactivity_state
     1285         * @covers wp_interactivity_get_context
     1286         */
     1287        public function test_evaluate_derived_state() {
     1288                $this->interactivity->state(
     1289                        'myPlugin',
     1290                        array(
     1291                                'key'     => 'myPlugin-state',
     1292                                'derived' => function () {
     1293                                        $state   = wp_interactivity_state();
     1294                                        $context = wp_interactivity_get_context();
     1295                                        return 'Derived state: ' .
     1296                                                $state['key'] .
     1297                                                "\n" .
     1298                                                'Derived context: ' .
     1299                                                $context['key'];
     1300                                },
     1301                        )
     1302                );
     1303                $this->set_internal_context_stack(
     1304                        array(
     1305                                'myPlugin' => array(
     1306                                        'key' => 'myPlugin-context',
     1307                                ),
     1308                        )
     1309                );
     1310                $this->set_internal_namespace_stack( 'myPlugin' );
     1311
     1312                $result = $this->evaluate( 'state.derived' );
     1313                $this->assertSame( "Derived state: myPlugin-state\nDerived context: myPlugin-context", $result );
     1314        }
     1315
     1316        /**
     1317         * Tests the `evaluate` method for derived state functions accessing a
     1318         * different namespace.
     1319         *
     1320         * @ticket 61037
     1321         *
     1322         * @covers ::evaluate
     1323         * @covers wp_interactivity_state
     1324         * @covers wp_interactivity_get_context
     1325         */
     1326        public function test_evaluate_derived_state_accessing_different_namespace() {
     1327                $this->interactivity->state(
     1328                        'myPlugin',
     1329                        array(
     1330                                'key'     => 'myPlugin-state',
     1331                                'derived' => function () {
     1332                                        $state   = wp_interactivity_state( 'otherPlugin' );
     1333                                        $context = wp_interactivity_get_context( 'otherPlugin' );
     1334                                        return 'Derived state: ' .
     1335                                                $state['key'] .
     1336                                                "\n" .
     1337                                                'Derived context: ' .
     1338                                                $context['key'];
     1339                                },
     1340                        )
     1341                );
     1342                $this->interactivity->state( 'otherPlugin', array( 'key' => 'otherPlugin-state' ) );
     1343                $this->set_internal_context_stack(
     1344                        array(
     1345                                'myPlugin'    => array(
     1346                                        'key' => 'myPlugin-context',
     1347                                ),
     1348                                'otherPlugin' => array(
     1349                                        'key' => 'otherPlugin-context',
     1350                                ),
     1351                        )
     1352                );
     1353                $this->set_internal_namespace_stack( 'myPlugin' );
     1354
     1355                $result = $this->evaluate( 'state.derived' );
     1356                $this->assertSame( "Derived state: otherPlugin-state\nDerived context: otherPlugin-context", $result );
     1357        }
     1358
     1359        /**
     1360         * Tests the `evaluate` method for derived state functions defined in a
     1361         * different namespace.
     1362         *
     1363         * @ticket 61037
     1364         *
     1365         * @covers ::evaluate
     1366         * @covers wp_interactivity_state
     1367         * @covers wp_interactivity_get_context
     1368         */
     1369        public function test_evaluate_derived_state_defined_in_different_namespace() {
     1370                $this->interactivity->state( 'myPlugin', array( 'key' => 'myPlugin-state' ) );
     1371                $this->interactivity->state(
     1372                        'otherPlugin',
     1373                        array(
     1374                                'key'     => 'otherPlugin-state',
     1375                                'derived' => function () {
     1376                                        $state   = wp_interactivity_state();
     1377                                        $context = wp_interactivity_get_context();
     1378                                        return 'Derived state: ' .
     1379                                                $state['key'] .
     1380                                                "\n" .
     1381                                                'Derived context: ' .
     1382                                                $context['key'];
     1383                                },
     1384                        )
     1385                );
     1386                $this->set_internal_context_stack(
     1387                        array(
     1388                                'myPlugin'    => array(
     1389                                        'key' => 'myPlugin-context',
     1390                                ),
     1391                                'otherPlugin' => array(
     1392                                        'key' => 'otherPlugin-context',
     1393                                ),
     1394                        )
     1395                );
     1396                $this->set_internal_namespace_stack( 'myPlugin' );
     1397
     1398                $result = $this->evaluate( 'otherPlugin::state.derived' );
     1399                $this->assertSame( "Derived state: otherPlugin-state\nDerived context: otherPlugin-context", $result );
     1400        }
     1401
     1402
     1403        /**
     1404         * Tests the `evaluate` method for derived state functions that throw.
     1405         *
     1406         * @ticket 61037
     1407         *
     1408         * @covers ::evaluate
     1409         * @expectedIncorrectUsage WP_Interactivity_API::evaluate
     1410         */
     1411        public function test_evaluate_derived_state_that_throws() {
     1412                $this->interactivity->state(
     1413                        'myPlugin',
     1414                        array(
     1415                                'derivedThatThrows' => function () {
     1416                                        throw new Error( 'Something bad happened.' );
     1417                                },
     1418                        )
     1419                );
     1420                $this->set_internal_context_stack();
     1421                $this->set_internal_namespace_stack( 'myPlugin' );
     1422
     1423                $result = $this->evaluate( 'state.derivedThatThrows' );
    9681424                $this->assertNull( $result );
    9691425        }
Note: See TracChangeset for help on using the changeset viewer.