Skip to content

Commit 7308875

Browse files
authored
Add Tag transformer for WP_Term objects (#2552)
1 parent ba120ff commit 7308875

File tree

5 files changed

+186
-0
lines changed

5 files changed

+186
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: added
3+
4+
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.

includes/transformer/class-factory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ public static function get_transformer( $data ) {
110110
return new User( $data );
111111
}
112112
break;
113+
case 'WP_Term':
114+
return new Term( $data );
113115
case 'json':
114116
return new Json( $data );
115117
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Term Transformer Class file.
4+
*
5+
* @package Activitypub
6+
*/
7+
8+
namespace Activitypub\Transformer;
9+
10+
/**
11+
* Term Transformer Class.
12+
*/
13+
class Term extends Base {
14+
/**
15+
* Transforms the WP_Term object to an OrderedCollection.
16+
*
17+
* @see \Activitypub\Activity\Base_Object
18+
*
19+
* @return \Activitypub\Activity\Base_Object|\WP_Error The OrderedCollection or WP_Error on failure.
20+
*/
21+
public function to_object() {
22+
$base_object = new \Activitypub\Activity\Base_Object();
23+
$base_object->{'@context'} = 'https://www.w3.org/ns/activitystreams';
24+
$base_object->set_type( 'OrderedCollection' );
25+
$base_object->set_id( \get_term_link( $this->item ) );
26+
27+
return $base_object;
28+
}
29+
30+
/**
31+
* Get the OrderedCollection ID (term link).
32+
*
33+
* @return string The OrderedCollection ID (term link).
34+
*/
35+
public function to_id() {
36+
return \get_term_link( $this->item );
37+
}
38+
}

tests/phpunit/tests/includes/transformer/class-test-factory.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Activitypub\Transformer\Factory;
1414
use Activitypub\Transformer\Json;
1515
use Activitypub\Transformer\Post;
16+
use Activitypub\Transformer\Term;
1617

1718
/**
1819
* Test class for Transformer Factory.
@@ -48,6 +49,13 @@ class Test_Factory extends \WP_UnitTestCase {
4849
*/
4950
protected static $user_id;
5051

52+
/**
53+
* Test term ID.
54+
*
55+
* @var int
56+
*/
57+
protected static $term_id;
58+
5159
/**
5260
* Create fake data before tests run.
5361
*
@@ -80,6 +88,16 @@ public static function wpSetUpBeforeClass( $factory ) {
8088
),
8189
)
8290
);
91+
92+
// Create test term.
93+
$term = $factory->term->create_and_get(
94+
array(
95+
'taxonomy' => 'post_tag',
96+
'name' => 'Test Tag',
97+
'slug' => 'test-tag',
98+
)
99+
);
100+
self::$term_id = $term->term_id;
83101
}
84102

85103
/**
@@ -266,4 +284,16 @@ public function test_get_transformer_with_wp_error() {
266284
$this->assertEquals( 'test_error', $result->get_error_code() );
267285
$this->assertEquals( 'Test error message', $result->get_error_message() );
268286
}
287+
288+
/**
289+
* Test get_transformer with WP_Term.
290+
*
291+
* @covers ::get_transformer
292+
*/
293+
public function test_get_transformer_term() {
294+
$term = get_term( self::$term_id );
295+
$transformer = Factory::get_transformer( $term );
296+
297+
$this->assertInstanceOf( Term::class, $transformer );
298+
}
269299
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
/**
3+
* Test file for Term Transformer.
4+
*
5+
* @package Activitypub
6+
*/
7+
8+
namespace Activitypub\Tests\Transformer;
9+
10+
use Activitypub\Transformer\Factory;
11+
use Activitypub\Transformer\Term;
12+
13+
/**
14+
* Test class for Term Transformer.
15+
*
16+
* @coversDefaultClass \Activitypub\Transformer\Term
17+
*/
18+
class Test_Term extends \WP_UnitTestCase {
19+
/**
20+
* Test term ID.
21+
*
22+
* @var int
23+
*/
24+
protected static $term_id;
25+
26+
/**
27+
* Create fake data before tests run.
28+
*
29+
* @param \WP_UnitTest_Factory $factory Helper that creates fake data.
30+
*/
31+
public static function wpSetUpBeforeClass( $factory ) {
32+
// Create a test tag.
33+
$term = $factory->term->create_and_get(
34+
array(
35+
'taxonomy' => 'post_tag',
36+
'name' => 'Test Term',
37+
'slug' => 'test-tag',
38+
)
39+
);
40+
self::$term_id = $term->term_id;
41+
}
42+
43+
/**
44+
* Test get_transformer with WP_Term.
45+
*
46+
* @covers \Activitypub\Transformer\Factory::get_transformer
47+
*/
48+
public function test_get_transformer_term() {
49+
$term = get_term( self::$term_id );
50+
$transformer = Factory::get_transformer( $term );
51+
52+
$this->assertInstanceOf( Term::class, $transformer );
53+
}
54+
55+
/**
56+
* Test to_object method.
57+
*
58+
* @covers ::to_object
59+
*/
60+
public function test_to_object() {
61+
$term = get_term( self::$term_id );
62+
$transformer = new Term( $term );
63+
$object = $transformer->to_object();
64+
65+
// Should return a Base_Object.
66+
$this->assertInstanceOf( \Activitypub\Activity\Base_Object::class, $object );
67+
68+
// Check ActivityStreams context.
69+
$this->assertEquals( 'https://www.w3.org/ns/activitystreams', $object->{'@context'} );
70+
71+
// Check type is OrderedCollection.
72+
$this->assertEquals( 'OrderedCollection', $object->get_type() );
73+
74+
// Check ID is the term link.
75+
$expected_url = get_term_link( $term );
76+
$this->assertEquals( $expected_url, $object->get_id() );
77+
}
78+
79+
/**
80+
* Test to_id method.
81+
*
82+
* @covers ::to_id
83+
*/
84+
public function test_to_id() {
85+
$term = get_term( self::$term_id );
86+
$transformer = new Term( $term );
87+
$id = $transformer->to_id();
88+
89+
// Should return the term link.
90+
$expected_url = get_term_link( $term );
91+
$this->assertEquals( $expected_url, $id );
92+
}
93+
94+
/**
95+
* Test with category taxonomy.
96+
*/
97+
public function test_category_term() {
98+
$category = self::factory()->term->create_and_get(
99+
array(
100+
'taxonomy' => 'category',
101+
'name' => 'Test Category',
102+
'slug' => 'test-category',
103+
)
104+
);
105+
106+
$transformer = new Term( $category );
107+
$object = $transformer->to_object();
108+
109+
$this->assertEquals( 'OrderedCollection', $object->get_type() );
110+
$this->assertEquals( get_term_link( $category ), $object->get_id() );
111+
}
112+
}

0 commit comments

Comments
 (0)