-
Notifications
You must be signed in to change notification settings - Fork 85
Add Tag transformer for WP_Term objects #2552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
98a370a
Add Tag transformer for WP_Term objects
pfefferle 1fc9b97
Add tests for Tag transformer
pfefferle b9d128b
Add changelog
matticbot a3508b7
class-tag.php aktualisieren
pfefferle 6246a77
class-tag.php aktualisieren
pfefferle 19ef10b
Update includes/transformer/class-tag.php
pfefferle 53c7992
Rename Tag transformer to Term transformer
pfefferle f487793
Merge branch 'trunk' into add/tag-support
pfefferle e6a4782
Merge branch 'trunk' into add/tag-support
pfefferle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <?php | ||
| /** | ||
| * User Transformer Class file. | ||
pfefferle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @package Activitypub | ||
| */ | ||
|
|
||
| namespace Activitypub\Transformer; | ||
|
|
||
| /** | ||
| * Tag Transformer Class. | ||
| */ | ||
| class Tag extends Base { | ||
| /** | ||
| * Transforms the WP_Term object to an Actor. | ||
| * | ||
| * @see \Activitypub\Activity\Actor | ||
| * | ||
| * @return \Activitypub\Activity\Base_Object|\WP_Error The Actor or WP_Error on failure. | ||
pfefferle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| 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 Actor ID. | ||
| * | ||
| * @return string The Actor ID. | ||
pfefferle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| public function to_id() { | ||
| return \get_term_link( $this->item ); | ||
pfefferle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
tests/phpunit/tests/includes/transformer/class-test-tag.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| <?php | ||
| /** | ||
| * Test file for Tag Transformer. | ||
| * | ||
| * @package Activitypub | ||
| */ | ||
|
|
||
| namespace Activitypub\Tests\Transformer; | ||
|
|
||
| use Activitypub\Transformer\Factory; | ||
| use Activitypub\Transformer\Tag; | ||
|
|
||
| /** | ||
| * Test class for Tag Transformer. | ||
| * | ||
| * @coversDefaultClass \Activitypub\Transformer\Tag | ||
| */ | ||
| class Test_Tag 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 Tag', | ||
| '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( Tag::class, $transformer ); | ||
| } | ||
|
|
||
| /** | ||
| * Test to_object method. | ||
| * | ||
| * @covers ::to_object | ||
| */ | ||
| public function test_to_object() { | ||
| $term = get_term( self::$term_id ); | ||
| $transformer = new Tag( $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 Tag( $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 Tag( $category ); | ||
| $object = $transformer->to_object(); | ||
|
|
||
| $this->assertEquals( 'OrderedCollection', $object->get_type() ); | ||
| $this->assertEquals( get_term_link( $category ), $object->get_id() ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.