Skip to content
Merged
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
4 changes: 4 additions & 0 deletions .github/changelog/2552-from-description
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Adds support for turning tags, categories, and custom taxonomies into federated collections in the Reader view so you can browse and follow topics more seamlessly.
2 changes: 2 additions & 0 deletions includes/transformer/class-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public static function get_transformer( $data ) {
return new User( $data );
}
break;
case 'WP_Term':
return new Term( $data );
case 'json':
return new Json( $data );
}
Expand Down
38 changes: 38 additions & 0 deletions includes/transformer/class-term.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Term Transformer Class file.
*
* @package Activitypub
*/

namespace Activitypub\Transformer;

/**
* Term Transformer Class.
*/
class Term extends Base {
/**
* Transforms the WP_Term object to an OrderedCollection.
*
* @see \Activitypub\Activity\Base_Object
*
* @return \Activitypub\Activity\Base_Object|\WP_Error The OrderedCollection or WP_Error on failure.
*/
public function to_object() {
$base_object = new \Activitypub\Activity\Base_Object();
$base_object->{'@context'} = 'https://www.w3.org/ns/activitystreams';
$base_object->set_type( 'OrderedCollection' );
$base_object->set_id( \get_term_link( $this->item ) );

return $base_object;
}

/**
* Get the OrderedCollection ID (term link).
*
* @return string The OrderedCollection ID (term link).
*/
public function to_id() {
return \get_term_link( $this->item );
}
}
30 changes: 30 additions & 0 deletions tests/phpunit/tests/includes/transformer/class-test-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Activitypub\Transformer\Factory;
use Activitypub\Transformer\Json;
use Activitypub\Transformer\Post;
use Activitypub\Transformer\Term;

/**
* Test class for Transformer Factory.
Expand Down Expand Up @@ -48,6 +49,13 @@ class Test_Factory extends \WP_UnitTestCase {
*/
protected static $user_id;

/**
* Test term ID.
*
* @var int
*/
protected static $term_id;

/**
* Create fake data before tests run.
*
Expand Down Expand Up @@ -80,6 +88,16 @@ public static function wpSetUpBeforeClass( $factory ) {
),
)
);

// Create test term.
$term = $factory->term->create_and_get(
array(
'taxonomy' => 'post_tag',
'name' => 'Test Tag',
'slug' => 'test-tag',
)
);
self::$term_id = $term->term_id;
}

/**
Expand Down Expand Up @@ -266,4 +284,16 @@ public function test_get_transformer_with_wp_error() {
$this->assertEquals( 'test_error', $result->get_error_code() );
$this->assertEquals( 'Test error message', $result->get_error_message() );
}

/**
* Test get_transformer with WP_Term.
*
* @covers ::get_transformer
*/
public function test_get_transformer_term() {
$term = get_term( self::$term_id );
$transformer = Factory::get_transformer( $term );

$this->assertInstanceOf( Term::class, $transformer );
}
}
112 changes: 112 additions & 0 deletions tests/phpunit/tests/includes/transformer/class-test-term.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* Test file for Term Transformer.
*
* @package Activitypub
*/

namespace Activitypub\Tests\Transformer;

use Activitypub\Transformer\Factory;
use Activitypub\Transformer\Term;

/**
* Test class for Term Transformer.
*
* @coversDefaultClass \Activitypub\Transformer\Term
*/
class Test_Term extends \WP_UnitTestCase {
/**
* Test term ID.
*
* @var int
*/
protected static $term_id;

/**
* Create fake data before tests run.
*
* @param \WP_UnitTest_Factory $factory Helper that creates fake data.
*/
public static function wpSetUpBeforeClass( $factory ) {
// Create a test tag.
$term = $factory->term->create_and_get(
array(
'taxonomy' => 'post_tag',
'name' => 'Test Term',
'slug' => 'test-tag',
)
);
self::$term_id = $term->term_id;
}

/**
* Test get_transformer with WP_Term.
*
* @covers \Activitypub\Transformer\Factory::get_transformer
*/
public function test_get_transformer_term() {
$term = get_term( self::$term_id );
$transformer = Factory::get_transformer( $term );

$this->assertInstanceOf( Term::class, $transformer );
}

/**
* Test to_object method.
*
* @covers ::to_object
*/
public function test_to_object() {
$term = get_term( self::$term_id );
$transformer = new Term( $term );
$object = $transformer->to_object();

// Should return a Base_Object.
$this->assertInstanceOf( \Activitypub\Activity\Base_Object::class, $object );

// Check ActivityStreams context.
$this->assertEquals( 'https://www.w3.org/ns/activitystreams', $object->{'@context'} );

// Check type is OrderedCollection.
$this->assertEquals( 'OrderedCollection', $object->get_type() );

// Check ID is the term link.
$expected_url = get_term_link( $term );
$this->assertEquals( $expected_url, $object->get_id() );
}

/**
* Test to_id method.
*
* @covers ::to_id
*/
public function test_to_id() {
$term = get_term( self::$term_id );
$transformer = new Term( $term );
$id = $transformer->to_id();

// Should return the term link.
$expected_url = get_term_link( $term );
$this->assertEquals( $expected_url, $id );
}

/**
* Test with category taxonomy.
*/
public function test_category_term() {
$category = self::factory()->term->create_and_get(
array(
'taxonomy' => 'category',
'name' => 'Test Category',
'slug' => 'test-category',
)
);

$transformer = new Term( $category );
$object = $transformer->to_object();

$this->assertEquals( 'OrderedCollection', $object->get_type() );
$this->assertEquals( get_term_link( $category ), $object->get_id() );
}
}