Skip to content

Commit

Permalink
add tests for JSON transformer and fix detected problems
Browse files Browse the repository at this point in the history
  • Loading branch information
pfefferle committed Jan 15, 2025
1 parent 99a0a48 commit 5d89584
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 8 deletions.
4 changes: 4 additions & 0 deletions includes/transformer/class-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public function __construct( $item ) {
* @return Base_Object The transformed ActivityPub Object.
*/
protected function transform_object_properties( $activity_object ) {
if ( ! $activity_object || \is_wp_error( $activity_object ) ) {
return $activity_object;
}

$vars = $activity_object->get_object_var_keys();

foreach ( $vars as $var ) {
Expand Down
12 changes: 6 additions & 6 deletions includes/transformer/class-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ class Json extends Activity_Object {
* @param string|array $item The item that should be transformed.
*/
public function __construct( $item ) {
$item = new Base_Object();
$object = new Base_Object();

if ( is_array( $this->item ) ) {
$item = Base_Object::init_from_array( $this->item );
} elseif ( is_string( $this->item ) ) {
$item = Base_Object::init_from_json( $this->item );
if ( is_array( $item ) ) {
$object = Base_Object::init_from_array( $item );
} elseif ( is_string( $item ) ) {
$object = Base_Object::init_from_json( $item );
}

parent::__construct( $item );
parent::__construct( $object );
}
}
4 changes: 2 additions & 2 deletions tests/includes/collection/class-test-outbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function activity_object_provider() {
),
'Create',
1,
'{"@context":["https://www.w3.org/ns/activitystreams",{"Hashtag":"as:Hashtag","sensitive":"as:sensitive"}],"type":"Object","tag":[],"to":["https://www.w3.org/ns/activitystreams#Public"],"cc":[],"mediaType":"text/html","sensitive":false}',
'{"@context":["https://www.w3.org/ns/activitystreams",{"Hashtag":"as:Hashtag","sensitive":"as:sensitive"}],"id":"https://example.com/1","type":"Note","content":"This is a note","contentMap":{"en":"This is a note"},"tag":[],"to":["https://www.w3.org/ns/activitystreams#Public"],"cc":[],"mediaType":"text/html","sensitive":false}',
),
array(
array(
Expand All @@ -64,7 +64,7 @@ public function activity_object_provider() {
),
'Create',
2,
'{"@context":["https://www.w3.org/ns/activitystreams",{"Hashtag":"as:Hashtag","sensitive":"as:sensitive"}],"type":"Object","tag":[],"to":["https://www.w3.org/ns/activitystreams#Public"],"cc":[],"mediaType":"text/html","sensitive":false}',
'{"@context":["https://www.w3.org/ns/activitystreams",{"Hashtag":"as:Hashtag","sensitive":"as:sensitive"}],"id":"https://example.com/2","type":"Note","content":"This is another note","contentMap":{"en":"This is another note"},"tag":[],"to":["https://www.w3.org/ns/activitystreams#Public"],"cc":[],"mediaType":"text/html","sensitive":false}',
),
);
}
Expand Down
132 changes: 132 additions & 0 deletions tests/includes/transformer/class-test-json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
/**
* Test file for JSON transformer.
*
* @package ActivityPub
*/

namespace Activitypub\Tests\Transformer;

use Activitypub\Transformer\Json;
use Activitypub\Activity\Base_Object;
use WP_UnitTestCase;

/**
* Test class for JSON Transformer.
*
* @coversDefaultClass \Activitypub\Transformer\Json
*/
class Test_Json extends WP_UnitTestCase {
/**
* Test constructor with JSON string.
*
* @covers ::__construct
*/
public function test_constructor_with_json_string() {
$json_string = wp_json_encode(
array(
'type' => 'Note',
'content' => 'Test Content',
'id' => 'https://example.com/test',
)
);

$transformer = new Json( $json_string );
$object = $transformer->to_object();

$this->assertInstanceOf( Base_Object::class, $object );
$this->assertEquals( 'Note', $object->get_type() );
$this->assertEquals( 'Test Content', $object->get_content() );
$this->assertEquals( 'https://example.com/test', $object->get_id() );
}

/**
* Test constructor with array.
*
* @covers ::__construct
*/
public function test_constructor_with_array() {
$array = array(
'type' => 'Article',
'name' => 'Test Title',
'content' => 'Test Content',
'url' => 'https://example.com/article',
);

$transformer = new Json( $array );
$object = $transformer->to_object();

$this->assertInstanceOf( Base_Object::class, $object );
$this->assertEquals( 'Article', $object->get_type() );
$this->assertEquals( 'Test Title', $object->get_name() );
$this->assertEquals( 'Test Content', $object->get_content() );
$this->assertEquals( 'https://example.com/article', $object->get_url() );
}

/**
* Test constructor with invalid JSON string.
*
* @covers ::__construct
*/
public function test_constructor_with_invalid_json() {
$invalid_json = '{invalid json string}';

$transformer = new Json( $invalid_json );
$object = $transformer->to_object();

$this->assertInstanceOf( 'WP_Error', $object );
}

/**
* Test constructor with empty input.
*
* @covers ::__construct
*/
public function test_constructor_with_empty_input() {
$transformer = new Json( '' );
$object = $transformer->to_object();

$this->assertInstanceOf( 'WP_Error', $object );
}

/**
* Test constructor with complex nested data.
*
* @covers ::__construct
*/
public function test_constructor_with_nested_data() {
$data = array(
'type' => 'Note',
'content' => 'Test Content',
'attachment' => array(
array(
'type' => 'Image',
'mediaType' => 'image/jpeg',
'url' => 'https://example.com/image.jpg',
),
),
'tag' => array(
array(
'type' => 'Mention',
'name' => '@test',
'href' => 'https://example.com/@test',
),
),
);

$transformer = new Json( $data );
$object = $transformer->to_object();

$this->assertInstanceOf( Base_Object::class, $object );
$this->assertEquals( 'Note', $object->get_type() );
$this->assertEquals( 'Test Content', $object->get_content() );

$attachment = $object->get_attachment();
$this->assertIsArray( $attachment );
$this->assertEquals( 'Image', $attachment[0]['type'] );

$tags = $object->get_tag();
$this->assertIsArray( $tags );
$this->assertEquals( 'Mention', $tags[0]['type'] );
}
}

0 comments on commit 5d89584

Please sign in to comment.