7
7
8
8
namespace Activitypub \Tests ;
9
9
10
+ use Activitypub \Activitypub ;
11
+ use Activitypub \Query ;
10
12
use Activitypub \Collection \Outbox ;
11
13
12
14
/**
@@ -40,7 +42,7 @@ public static function wpSetUpBeforeClass( $factory ) {
40
42
*/
41
43
public function setUp (): void {
42
44
parent ::setUp ();
43
- \ Activitypub \ Activitypub::init ();
45
+ Activitypub::init ();
44
46
}
45
47
46
48
/**
@@ -83,7 +85,7 @@ function () {
83
85
);
84
86
85
87
// Test that the filter is applied.
86
- $ template = \ Activitypub \ Activitypub::render_activitypub_template ( 'original.php ' );
88
+ $ template = Activitypub::render_activitypub_template ( 'original.php ' );
87
89
$ this ->assertEquals ( '/custom/template.php ' , $ template , 'Custom preview template should be used when filter is applied. ' );
88
90
89
91
// Clean up.
@@ -168,7 +170,7 @@ public function test_custom_post_type_returns_200() {
168
170
$ this ->go_to ( '/?p= ' . $ post_id );
169
171
170
172
// Test the template response.
171
- $ template = \ Activitypub \ Activitypub::render_activitypub_template ( 'index.php ' );
173
+ $ template = Activitypub::render_activitypub_template ( 'index.php ' );
172
174
$ this ->assertStringContainsString ( 'activitypub-json.php ' , $ template );
173
175
$ this ->assertFalse ( $ wp_query ->is_404 );
174
176
@@ -211,7 +213,7 @@ public function test_custom_post_type_with_support_returns_200() {
211
213
$ this ->go_to ( '/?p= ' . $ post_id );
212
214
213
215
// Test the template response.
214
- $ template = \ Activitypub \ Activitypub::render_activitypub_template ( 'index.php ' );
216
+ $ template = Activitypub::render_activitypub_template ( 'index.php ' );
215
217
$ this ->assertStringContainsString ( 'activitypub-json.php ' , $ template );
216
218
$ this ->assertFalse ( $ wp_query ->is_404 );
217
219
@@ -220,6 +222,46 @@ public function test_custom_post_type_with_support_returns_200() {
220
222
_unregister_post_type ( 'test_cpt_supported ' );
221
223
}
222
224
225
+ /**
226
+ * Test 406/404 response for non-ActivityPub requests to Outbox post type.
227
+ *
228
+ * @covers ::render_activitypub_template
229
+ */
230
+ public function test_outbox_post_type_non_activitypub_request_returns_406 () {
231
+ $ data = array (
232
+ '@context ' => 'https://www.w3.org/ns/activitystreams ' ,
233
+ 'id ' => 'https://example.com/ ' . self ::$ user_id ,
234
+ 'type ' => 'Note ' ,
235
+ 'content ' => '<p>This is a note</p> ' ,
236
+ );
237
+ $ post_id = \Activitypub \add_to_outbox ( $ data , 'Create ' , self ::$ user_id );
238
+
239
+ $ _SERVER ['HTTP_ACCEPT ' ] = 'application/activity+json ' ;
240
+ $ this ->go_to ( '/?p= ' . $ post_id );
241
+ $ template = Activitypub::render_activitypub_template ( 'index.php ' );
242
+ $ this ->assertStringContainsString ( 'activitypub-json.php ' , $ template );
243
+
244
+ Query::get_instance ()->__destruct ();
245
+
246
+ $ status = null ;
247
+ add_filter (
248
+ 'status_header ' ,
249
+ function ( $ status_header ) use ( &$ status ) {
250
+ $ status = $ status_header ;
251
+ return $ status_header ;
252
+ },
253
+ 100
254
+ );
255
+
256
+ unset( $ _SERVER ['HTTP_ACCEPT ' ] );
257
+ $ this ->go_to ( '/?p= ' . $ post_id );
258
+ $ template = Activitypub::render_activitypub_template ( 'index.php ' );
259
+ $ this ->assertStringContainsString ( 'index.php ' , $ template );
260
+ $ this ->assertStringContainsString ( '406 ' , $ status );
261
+
262
+ wp_delete_post ( $ post_id , true );
263
+ }
264
+
223
265
/**
224
266
* Test no_trailing_redirect method.
225
267
*
@@ -231,15 +273,15 @@ public function test_no_trailing_redirect() {
231
273
$ requested_url = 'https://example.org/@testuser ' ;
232
274
$ redirect_url = 'https://example.org/@testuser/ ' ;
233
275
234
- $ result = \ Activitypub \ Activitypub::no_trailing_redirect ( $ redirect_url , $ requested_url );
276
+ $ result = Activitypub::no_trailing_redirect ( $ redirect_url , $ requested_url );
235
277
$ this ->assertEquals ( $ requested_url , $ result , 'Should return requested URL when actor query var is set. ' );
236
278
237
279
// Test case 2: When actor query var is not set, it should return the redirect URL.
238
280
set_query_var ( 'actor ' , '' );
239
281
$ requested_url = 'https://example.org/some-page ' ;
240
282
$ redirect_url = 'https://example.org/some-page/ ' ;
241
283
242
- $ result = \ Activitypub \ Activitypub::no_trailing_redirect ( $ redirect_url , $ requested_url );
284
+ $ result = Activitypub::no_trailing_redirect ( $ redirect_url , $ requested_url );
243
285
$ this ->assertEquals ( $ redirect_url , $ result , 'Should return redirect URL when actor query var is not set. ' );
244
286
245
287
// Clean up.
0 commit comments