Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/wp-includes/IXR/class-IXR-introspectionserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function call($methodname, $args)
break;
case 'date':
case 'dateTime.iso8601':
if (!is_a($arg, 'IXR_Date')) {
if (!($arg instanceof IXR_Date)) {
$ok = false;
}
break;
Expand Down
4 changes: 2 additions & 2 deletions src/wp-includes/IXR/class-IXR-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function serve($data = false)
$result = $this->call($this->message->methodName, $this->message->params);

// Is the result an error?
if (is_a($result, 'IXR_Error')) {
if ($result instanceof IXR_Error) {
$this->error($result);
}

Expand Down Expand Up @@ -234,7 +234,7 @@ function multiCall($methodcalls)
} else {
$result = $this->call($method, $params);
}
if (is_a($result, 'IXR_Error')) {
if ($result instanceof IXR_Error) {
$return[] = array(
'faultCode' => $result->code,
'faultString' => $result->message
Expand Down
4 changes: 2 additions & 2 deletions src/wp-includes/IXR/class-IXR-value.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ function calculateType()
}

// Deal with IXR object types base64 and date
if (is_object($this->data) && is_a($this->data, 'IXR_Date')) {
if ($this->data instanceof IXR_Date) {
return 'date';
}
if (is_object($this->data) && is_a($this->data, 'IXR_Base64')) {
if ($this->data instanceof IXR_Base64) {
return 'base64';
}

Expand Down
12 changes: 6 additions & 6 deletions src/wp-includes/Text/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ function countAddedLines()
{
$count = 0;
foreach ($this->_edits as $edit) {
if (is_a($edit, 'Text_Diff_Op_add') ||
is_a($edit, 'Text_Diff_Op_change')) {
if ($edit instanceof Text_Diff_Op_add ||
$edit instanceof Text_Diff_Op_change) {
$count += $edit->nfinal();
}
}
Expand All @@ -100,8 +100,8 @@ function countDeletedLines()
{
$count = 0;
foreach ($this->_edits as $edit) {
if (is_a($edit, 'Text_Diff_Op_delete') ||
is_a($edit, 'Text_Diff_Op_change')) {
if ($edit instanceof Text_Diff_Op_delete ||
$edit instanceof Text_Diff_Op_change) {
$count += $edit->norig();
}
}
Expand Down Expand Up @@ -144,7 +144,7 @@ function reverse()
function isEmpty()
{
foreach ($this->_edits as $edit) {
if (!is_a($edit, 'Text_Diff_Op_copy')) {
if (!($edit instanceof Text_Diff_Op_copy)) {
return false;
}
}
Expand All @@ -162,7 +162,7 @@ function lcs()
{
$lcs = 0;
foreach ($this->_edits as $edit) {
if (is_a($edit, 'Text_Diff_Op_copy')) {
if ($edit instanceof Text_Diff_Op_copy) {
$lcs += count($edit->orig);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/Text/Diff/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function render($diff)
/* If these are unchanged (copied) lines, and we want to keep
* leading or trailing context lines, extract them from the copy
* block. */
if (is_a($edit, 'Text_Diff_Op_copy')) {
if ($edit instanceof Text_Diff_Op_copy) {
/* Do we have any diff blocks yet? */
if (is_array($block)) {
/* How many lines to keep as context from the copy
Expand Down
75 changes: 75 additions & 0 deletions tests/phpunit/tests/diff/textDiff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/**
* Tests for the edit class checks in Text_Diff and Text_Diff_Renderer.
*
* @group diff
*/
final class Tests_Diff_TextDiff extends WP_UnitTestCase {

public static function set_up_before_class() {
parent::set_up_before_class();

require_once ABSPATH . 'wp-includes/Text/Diff.php';
require_once ABSPATH . 'wp-includes/Text/Diff/Renderer.php';
}

Comment thread
peterwilsoncc marked this conversation as resolved.
Resolved
/**
* @ticket 66418

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a one line summary at the top of each test.

For this one it might be:

/**
 * Ensure edit counts are correct.

🔢 Applies to other tests too.

*
* @covers Text_Diff::countAddedLines
* @covers Text_Diff::countDeletedLines
* @covers Text_Diff::isEmpty
* @covers Text_Diff::lcs
*
* @dataProvider data_edit_counts
*
* @param non-empty-string[] $from_lines Lines before the change.
* @param non-empty-string[] $to_lines Lines after the change.
* @param non-negative-int $added Expected number of added lines.
* @param non-negative-int $deleted Expected number of deleted lines.
* @param bool $is_empty Whether the diff is expected to have no changes.
* @param non-negative-int $unchanged_lines Expected number of unchanged lines.
*/
public function test_edit_counts( array $from_lines, array $to_lines, int $added, int $deleted, bool $is_empty, int $unchanged_lines ) {
$diff = new Text_Diff( 'native', array( $from_lines, $to_lines ) );

$this->assertSame( $added, $diff->countAddedLines(), 'Added lines do not match.' );
$this->assertSame( $deleted, $diff->countDeletedLines(), 'Deleted lines do not match.' );
$this->assertSame( $is_empty, $diff->isEmpty(), 'isEmpty() does not match.' );
$this->assertSame( $unchanged_lines, $diff->lcs(), 'Unchanged lines do not match.' );
}

/**
* Data provider for {@see self::test_edit_counts()}.
*
* @return array<non-falsy-string, array{ non-empty-string[], non-empty-string[], non-negative-int, non-negative-int, bool, non-negative-int }>
*/
public static function data_edit_counts(): array {
return array(
'both empty' => array( array(), array(), 0, 0, true, 0 ),
'identical' => array( array( 'a', 'b' ), array( 'a', 'b' ), 0, 0, true, 2 ),
'add only' => array( array( 'a' ), array( 'a', 'b' ), 1, 0, false, 1 ),
'delete only' => array( array( 'a', 'b' ), array( 'a' ), 0, 1, false, 1 ),
'changed only' => array( array( 'a', 'b', 'c' ), array( 'x', 'b', 'z' ), 2, 2, false, 1 ),
'change, copy and add' => array( array( 'a', 'b' ), array( 'x', 'b', 'c' ), 2, 1, false, 1 ),
);
}

/**
* @ticket 66418
*
* @covers Text_Diff_Renderer::render
*/
public function test_render_keeps_context_lines_from_copied_blocks() {
$diff = new Text_Diff( 'native', array( array( 'a', 'b', 'c', 'd', 'e' ), array( 'a', 'b', 'X', 'd', 'e' ) ) );
$renderer = new Text_Diff_Renderer(
array(
'leading_context_lines' => 1,
'trailing_context_lines' => 1,
)
);

$this->assertSame( "2,4c2,4\n b\n< c\n---\n> X\n d\n", $renderer->render( $diff ) );
}
}
107 changes: 107 additions & 0 deletions tests/phpunit/tests/xmlrpc/basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,113 @@ public function test_multicall_with_missing_method_name(): void {
$this->assertSame( array( 'Hello!' ), $result[1] );
}

/**
* Tests that a multicall entry returning a non-object result is not treated as an error.
*
* @ticket 66418
*
* @covers IXR_Server::multiCall
*
* @dataProvider data_multicall_with_non_object_result
*
* @param mixed $value The value the method returns.
*/
public function test_multicall_with_non_object_result( $value ): void {
$this->myxmlrpcserver->callbacks = array(
'test.echo' => array( $this, 'echo_args' ),
);

$result = $this->myxmlrpcserver->multiCall(
array(
array(
'methodName' => 'test.echo',
'params' => array( $value ),
),
)
);

$this->assertSame( array( array( $value ) ), $result );
}

/**
* Data provider.
*
* @return array<non-falsy-string, array{ 0: mixed }>
*/
public static function data_multicall_with_non_object_result(): array {
return array(
'string' => array( 'IXR_Error' ),
'integer' => array( 42 ),
'boolean' => array( true ),
'non-struct array' => array( array( 'IXR_Error' ) ),
);
}

/**
* Tests that the introspection server rejects a non-object argument for a date parameter.
*
* @ticket 66418
*
* @covers IXR_IntrospectionServer::call
*
* @dataProvider data_introspection_server_rejects_non_date_argument
*
* @param mixed $arg The argument passed for the date parameter.
*/
public function test_introspection_server_rejects_non_date_argument( $arg ): void {
$server = new IXR_IntrospectionServer();
$server->addCallback( 'test.echo', array( $this, 'echo_args' ), array( 'string', 'dateTime.iso8601' ), 'Echoes a date.' );

$result = $server->call( 'test.echo', array( $arg ) );

$this->assertIXRError( $result, 'A non-date argument should return an IXR_Error.' );
$this->assertSame( -32602, $result->code, 'The error code should indicate invalid method parameters.' );
$this->assertSame( 'server error. invalid method parameters', $result->message, 'The error message should indicate an invalid parameter type rather than a wrong parameter count.' );
}

/**
* Data provider.
*
* @return array<non-falsy-string, array{ 0: mixed }>
*/
public static function data_introspection_server_rejects_non_date_argument(): array {
return array(
'date string' => array( '20261030T12:00:00' ),
'class name' => array( 'IXR_Date' ),
'integer' => array( 1793361600 ),
);
}

/**
* Tests that the introspection server accepts an IXR_Date argument for a date parameter.
*
* @ticket 66418
*
* @covers IXR_IntrospectionServer::call
*/
public function test_introspection_server_accepts_date_argument(): void {
$server = new IXR_IntrospectionServer();
$server->addCallback( 'test.echo', array( $this, 'echo_args' ), array( 'string', 'dateTime.iso8601' ), 'Echoes a date.' );

$date = new IXR_Date( '20261030T12:00:00' );
$result = $server->call( 'test.echo', array( $date ) );

$this->assertSame( $date, $result );
}

/**
* Tests that IXR_Value encodes IXR_Date and IXR_Base64 objects as their XML-RPC types, and strings as strings.
*
* @ticket 66418
*
* @covers IXR_Value::calculateType
*/
public function test_ixr_value_calculate_type(): void {
$this->assertSame( 'date', ( new IXR_Value( new IXR_Date( '20261030T12:00:00' ) ) )->calculateType(), 'An IXR_Date object should be typed as a date.' );
$this->assertSame( 'base64', ( new IXR_Value( new IXR_Base64( 'data' ) ) )->calculateType(), 'An IXR_Base64 object should be typed as base64.' );
$this->assertSame( 'string', ( new IXR_Value( 'IXR_Date' ) )->calculateType(), 'A string matching a class name should be typed as a string.' );
}

/**
* @ticket 36586
*/
Expand Down
Loading