Skip to content

Commit 242a39a

Browse files
authored
Transformers: Allow setting properties to false. (#1567)
1 parent d3cf57f commit 242a39a

File tree

7 files changed

+84
-30
lines changed

7 files changed

+84
-30
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: fixed
3+
4+
Transformers allow settings values to false again, a regression from 5.5.0.

includes/activity/class-generic-object.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public static function init_from_array( $data ) {
195195
*/
196196
public function from_array( $data ) {
197197
foreach ( $data as $key => $value ) {
198-
if ( $value ) {
198+
if ( null !== $value ) {
199199
$key = camel_to_snake_case( $key );
200200
call_user_func( array( $this, 'set_' . $key ), $value );
201201
}

includes/transformer/class-base.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected function transform_object_properties( $activity_object ) {
9090
if ( \method_exists( $this, $getter ) ) {
9191
$value = \call_user_func( array( $this, $getter ) );
9292

93-
if ( ! empty( $value ) ) {
93+
if ( null !== $value ) {
9494
$setter = 'set_' . $var;
9595

9696
/**

tests/includes/activity/class-test-generic-object.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ public function test_init_from_array_empty_values() {
7777

7878
$this->assertEquals( $test_data['id'], $object->get_id() );
7979
$this->assertEquals( $test_data['type'], $object->get_type() );
80-
$this->assertNull( $object->get_name() );
80+
$this->assertEmpty( $object->get_name() );
8181
$this->assertNull( $object->get_summary() );
82-
$this->assertNull( $object->get_content() );
82+
$this->assertFalse( $object->get_content() );
8383
}
8484

8585
/**

tests/includes/collection/class-test-outbox.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function activity_object_provider() {
115115
),
116116
'Create',
117117
1,
118-
'{"@context":["https:\/\/www.w3.org\/ns\/activitystreams",{"Hashtag":"as:Hashtag","sensitive":"as:sensitive"}],"actor":"http:\/\/example.org\/?author=1","id":"http:\/\/example.org\/?post_type=ap_outbox\u0026p=351","type":"Create","to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"object":{"id":"https:\/\/example.com\/1","type":"Note","content":"\u003Cp\u003EThis is a note\u003C\/p\u003E","contentMap":{"en":"\u003Cp\u003EThis is a note\u003C\/p\u003E"},"to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"mediaType":"text\/html"}}',
118+
'{"@context":["https:\/\/www.w3.org\/ns\/activitystreams",{"Hashtag":"as:Hashtag","sensitive":"as:sensitive"}],"actor":"http:\/\/example.org\/?author=1","id":"http:\/\/example.org\/?post_type=ap_outbox\u0026p=351","type":"Create","to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"object":{"id":"https:\/\/example.com\/1","type":"Note","content":"\u003Cp\u003EThis is a note\u003C\/p\u003E","contentMap":{"en":"\u003Cp\u003EThis is a note\u003C\/p\u003E"},"tag":[],"to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"mediaType":"text\/html"}}',
119119
),
120120
array(
121121
array(
@@ -126,7 +126,7 @@ public function activity_object_provider() {
126126
),
127127
'Create',
128128
2,
129-
'{"@context":["https:\/\/www.w3.org\/ns\/activitystreams",{"Hashtag":"as:Hashtag","sensitive":"as:sensitive"}],"actor":"http:\/\/example.org\/?author=0","id":"http:\/\/example.org\/?post_type=ap_outbox\u0026p=352","type":"Create","to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"object":{"id":"https:\/\/example.com\/2","type":"Note","content":"\u003Cp\u003EThis is another note\u003C\/p\u003E","contentMap":{"en":"\u003Cp\u003EThis is another note\u003C\/p\u003E"},"to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"mediaType":"text\/html"}}',
129+
'{"@context":["https:\/\/www.w3.org\/ns\/activitystreams",{"Hashtag":"as:Hashtag","sensitive":"as:sensitive"}],"actor":"http:\/\/example.org\/?author=0","id":"http:\/\/example.org\/?post_type=ap_outbox\u0026p=352","type":"Create","to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"object":{"id":"https:\/\/example.com\/2","type":"Note","content":"\u003Cp\u003EThis is another note\u003C\/p\u003E","contentMap":{"en":"\u003Cp\u003EThis is another note\u003C\/p\u003E"},"tag":[],"to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"mediaType":"text\/html"}}',
130130
),
131131
array(
132132
Event::init_from_array(
@@ -163,7 +163,7 @@ public function activity_object_provider() {
163163
),
164164
'Create',
165165
1,
166-
'{"@context":["https:\/\/schema.org\/","https:\/\/www.w3.org\/ns\/activitystreams",{"pt":"https:\/\/joinpeertube.org\/ns#","mz":"https:\/\/joinmobilizon.org\/ns#","status":"http:\/\/www.w3.org\/2002\/12\/cal\/ical#status","commentsEnabled":"pt:commentsEnabled","isOnline":"mz:isOnline","timezone":"mz:timezone","participantCount":"mz:participantCount","anonymousParticipationEnabled":"mz:anonymousParticipationEnabled","joinMode":{"@id":"mz:joinMode","@type":"mz:joinModeType"},"externalParticipationUrl":{"@id":"mz:externalParticipationUrl","@type":"schema:URL"},"repliesModerationOption":{"@id":"mz:repliesModerationOption","@type":"@vocab"},"contacts":{"@id":"mz:contacts","@type":"@id"}}],"actor":"http:\/\/example.org\/?author=1","id":"http:\/\/example.org\/?post_type=ap_outbox\u0026p=353","type":"Create","to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"object":{"id":"https:\/\/example.com\/3","type":"Event","content":"\u003Cp\u003EYou should not miss this Event!\u003C\/p\u003E","contentMap":{"en":"\u003Cp\u003EYou should not miss this Event!\u003C\/p\u003E"},"name":"WP Test Event","nameMap":{"en":"WP Test Event"},"endTime":"2030-02-29T17:00:00+01:00","location":[{"id":"https:\/\/example.com\/place\/1","type":"Place","attributedTo":"https:\/\/wp-test.event-federation.eu\/@test","name":"Fediverse Place","address":{"type":"PostalAddress","addressCountry":"FediCountry","addressLocality":"FediTown","postalCode":"1337","streetAddress":"FediStreet"}},{"type":"VirtualLocation","url":"https:\/\/example.com\/VirtualMeetingRoom"}],"startTime":"2030-02-29T16:00:00+01:00","to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"mediaType":"text\/html","timezone":"Europe\/Vienna","category":"MOVEMENTS_POLITICS","joinMode":"external"}}',
166+
'{"@context":["https:\/\/schema.org\/","https:\/\/www.w3.org\/ns\/activitystreams",{"pt":"https:\/\/joinpeertube.org\/ns#","mz":"https:\/\/joinmobilizon.org\/ns#","status":"http:\/\/www.w3.org\/2002\/12\/cal\/ical#status","commentsEnabled":"pt:commentsEnabled","isOnline":"mz:isOnline","timezone":"mz:timezone","participantCount":"mz:participantCount","anonymousParticipationEnabled":"mz:anonymousParticipationEnabled","joinMode":{"@id":"mz:joinMode","@type":"mz:joinModeType"},"externalParticipationUrl":{"@id":"mz:externalParticipationUrl","@type":"schema:URL"},"repliesModerationOption":{"@id":"mz:repliesModerationOption","@type":"@vocab"},"contacts":{"@id":"mz:contacts","@type":"@id"}}],"actor":"http:\/\/example.org\/?author=1","id":"http:\/\/example.org\/?post_type=ap_outbox\u0026p=353","type":"Create","to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"object":{"id":"https:\/\/example.com\/3","type":"Event","content":"\u003Cp\u003EYou should not miss this Event!\u003C\/p\u003E","contentMap":{"en":"\u003Cp\u003EYou should not miss this Event!\u003C\/p\u003E"},"name":"WP Test Event","nameMap":{"en":"WP Test Event"},"endTime":"2030-02-29T17:00:00+01:00","location":[{"id":"https:\/\/example.com\/place\/1","type":"Place","attributedTo":"https:\/\/wp-test.event-federation.eu\/@test","name":"Fediverse Place","address":{"type":"PostalAddress","addressCountry":"FediCountry","addressLocality":"FediTown","postalCode":"1337","streetAddress":"FediStreet"}},{"type":"VirtualLocation","url":"https:\/\/example.com\/VirtualMeetingRoom"}],"startTime":"2030-02-29T16:00:00+01:00","to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"mediaType":"text\/html","tag":[],"timezone":"Europe\/Vienna","category":"MOVEMENTS_POLITICS","joinMode":"external"}}',
167167
),
168168
);
169169
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* Test file for Base Transformer.
4+
*
5+
* @package ActivityPub
6+
*/
7+
8+
namespace Activitypub\Tests\Transformer;
9+
10+
use Activitypub\Activity\Base_Object;
11+
use Activitypub\Transformer\Base;
12+
13+
/**
14+
* Test class for Base Transformer.
15+
*
16+
* @coversDefaultClass \Activitypub\Transformer\Base
17+
*/
18+
class Test_Base extends \WP_UnitTestCase {
19+
/**
20+
* Test that false values are properly set in object properties.
21+
*
22+
* @covers ::transform_object_properties
23+
*/
24+
public function test_transform_object_properties_with_false_value() {
25+
// Create a mock transformer that extends Base and has a method returning false.
26+
$mock_transformer = new class('test') extends Base {
27+
/**
28+
* Get a property that returns false.
29+
*
30+
* @return bool Always returns false.
31+
*/
32+
public function get_sensitive() {
33+
return false;
34+
}
35+
36+
/**
37+
* Public wrapper to test the protected transform_object_properties method.
38+
*
39+
* @param Base_Object $activity_object The ActivityPub Object.
40+
*
41+
* @return Base_Object|\WP_Error The transformed ActivityPub Object or WP_Error on failure.
42+
*/
43+
public function test_transform_properties( $activity_object ) {
44+
return $this->transform_object_properties( $activity_object );
45+
}
46+
};
47+
48+
// Create a test object that has 'sensitive' in its var keys.
49+
$test_object = new class() extends Base_Object {
50+
/**
51+
* Whether the content is sensitive.
52+
*
53+
* @var bool|null
54+
*/
55+
protected $sensitive;
56+
57+
/**
58+
* Override get_object_var_keys to include 'sensitive'.
59+
*
60+
* @return array The keys of the object vars.
61+
*/
62+
public function get_object_var_keys() {
63+
return array( 'sensitive' );
64+
}
65+
};
66+
67+
// Transform the object.
68+
$transformed_object = $mock_transformer->test_transform_properties( $test_object );
69+
70+
// Assert that the sensitive property could be set to false.
71+
$this->assertFalse( $transformed_object->get_sensitive(), 'The sensitive property should be set to false.' );
72+
}
73+
}

tests/includes/wp-admin/class-test-admin.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)