Skip to content

Commit

Permalink
Add namespace check and warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
cbravobernal committed May 22, 2024
1 parent 77d14a8 commit 42a0572
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,15 @@ private function process_directives_args( string $html, array &$context_stack, a
*/
private function evaluate( $directive_value, string $default_namespace, $context = false ) {
list( $ns, $path ) = $this->extract_directive_value( $directive_value, $default_namespace );
if ( empty( $path ) ) {
if ( 'null' === $default_namespace ) {
$message = 'The namespace defined cannot be null.';
_doing_it_wrong( __METHOD__, $message, '6.6.0' );
return null;
}
if ( empty( $ns ) || empty( $path ) ) {
/* translators: %s: The directive value referenced. */
$message = sprintf( 'Namespace or reference path cannot be empty. Directive value referenced: %s ', $directive_value );
_doing_it_wrong( __METHOD__, $message, '6.6.0' );
return null;
}

Expand Down
23 changes: 21 additions & 2 deletions tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ public function test_process_directives_does_not_change_inner_html_in_math() {
* @param string $directive_value The directive attribute value to evaluate.
* @return mixed The result of the evaluate method.
*/
private function evaluate( $directive_value ) {
private function evaluate( $directive_value, $default_namespace = 'myPlugin' ) {
$generate_state = function ( $name ) {
return array(
'key' => $name,
Expand All @@ -829,7 +829,7 @@ private function evaluate( $directive_value ) {
);
$evaluate = new ReflectionMethod( $this->interactivity, 'evaluate' );
$evaluate->setAccessible( true );
return $evaluate->invokeArgs( $this->interactivity, array( $directive_value, 'myPlugin', $context ) );
return $evaluate->invokeArgs( $this->interactivity, array( $directive_value, $default_namespace, $context ) );
}

/**
Expand Down Expand Up @@ -923,6 +923,25 @@ public function test_evaluate_nested_value() {
$this->assertEquals( 'otherPlugin-context-nested', $result );
}

/**
* Tests the `evaluate` method for non valid namespace values.
*
* @ticket 61044
*
* @covers ::evaluate
* @expectedIncorrectUsage WP_Interactivity_API::evaluate
*/
public function test_evaluate_unvalid_namespaces() {
$result = $this->evaluate( 'path', 'null' );
$this->assertNull( $result );

$result = $this->evaluate( 'path', '' );
$this->assertNull( $result );

$result = $this->evaluate( 'path', '{}' );
$this->assertNull( $result );
}

/**
* Tests the `kebab_to_camel_case` method.
*
Expand Down

0 comments on commit 42a0572

Please sign in to comment.