Skip to content

Commit 2086fb0

Browse files
committed
Add tests for Nodeinfo main class
Tests cover: - Singleton pattern behavior - REST routes registration - Integration registration - Rewrite rules - Deprecated filter handling - WebFinger and admin hooks registration
1 parent 3c7475c commit 2086fb0

1 file changed

Lines changed: 137 additions & 0 deletions

File tree

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
/**
3+
* Test Nodeinfo main class.
4+
*
5+
* @package Nodeinfo
6+
*/
7+
8+
namespace Nodeinfo\Tests;
9+
10+
/**
11+
* Test class for the main Nodeinfo class.
12+
*
13+
* @coversDefaultClass \Nodeinfo\Nodeinfo
14+
*/
15+
class Test_Nodeinfo extends \WP_UnitTestCase {
16+
17+
/**
18+
* Test that get_instance returns same instance (singleton).
19+
*
20+
* @covers ::get_instance
21+
*/
22+
public function test_singleton_pattern() {
23+
$instance1 = \Nodeinfo\Nodeinfo::get_instance();
24+
$instance2 = \Nodeinfo\Nodeinfo::get_instance();
25+
26+
$this->assertSame( $instance1, $instance2 );
27+
}
28+
29+
/**
30+
* Test that get_instance returns Nodeinfo instance.
31+
*
32+
* @covers ::get_instance
33+
*/
34+
public function test_get_instance_returns_nodeinfo() {
35+
$instance = \Nodeinfo\Nodeinfo::get_instance();
36+
37+
$this->assertInstanceOf( \Nodeinfo\Nodeinfo::class, $instance );
38+
}
39+
40+
/**
41+
* Test that REST routes are registered.
42+
*
43+
* @covers ::register_routes
44+
*/
45+
public function test_rest_routes_registered() {
46+
global $wp_rest_server;
47+
48+
$wp_rest_server = new \WP_REST_Server();
49+
do_action( 'rest_api_init' );
50+
51+
$routes = $wp_rest_server->get_routes();
52+
53+
$this->assertArrayHasKey( '/nodeinfo/discovery', $routes );
54+
$this->assertArrayHasKey( '/nodeinfo2/1.0', $routes );
55+
56+
$wp_rest_server = null;
57+
}
58+
59+
/**
60+
* Test that integrations register nodeinfo_versions filter.
61+
*
62+
* @covers ::register_integrations
63+
*/
64+
public function test_integrations_register_versions() {
65+
$versions = apply_filters( 'nodeinfo_versions', array() );
66+
67+
$this->assertContains( '1.0', $versions );
68+
$this->assertContains( '1.1', $versions );
69+
$this->assertContains( '2.0', $versions );
70+
$this->assertContains( '2.1', $versions );
71+
$this->assertContains( '2.2', $versions );
72+
}
73+
74+
/**
75+
* Test that rewrite rules are added.
76+
*
77+
* @covers ::add_rewrite_rules
78+
*/
79+
public function test_rewrite_rules_added() {
80+
global $wp_rewrite;
81+
82+
// Flush rules to ensure they're registered.
83+
\Nodeinfo\Nodeinfo::get_instance()->add_rewrite_rules();
84+
$wp_rewrite->flush_rules();
85+
86+
$rules = $wp_rewrite->wp_rewrite_rules();
87+
88+
$this->assertArrayHasKey( '^.well-known/nodeinfo', $rules );
89+
$this->assertArrayHasKey( '^.well-known/x-nodeinfo2', $rules );
90+
}
91+
92+
/**
93+
* Test deprecated wellknown_nodeinfo_data filter triggers deprecation.
94+
*
95+
* @covers ::deprecated_wellknown_nodeinfo_data
96+
*/
97+
public function test_deprecated_filter_triggers_notice() {
98+
// Add a callback to the deprecated filter.
99+
add_filter(
100+
'wellknown_nodeinfo_data',
101+
function ( $data ) {
102+
$data['test'] = 'value';
103+
return $data;
104+
}
105+
);
106+
107+
// Expect a deprecation notice.
108+
$this->setExpectedDeprecated( 'wellknown_nodeinfo_data' );
109+
110+
// Trigger the filter chain.
111+
$discovery = apply_filters( 'nodeinfo_discovery', array( 'links' => array() ) );
112+
113+
// Verify the deprecated filter was applied.
114+
$this->assertArrayHasKey( 'test', $discovery );
115+
$this->assertEquals( 'value', $discovery['test'] );
116+
}
117+
118+
/**
119+
* Test WebFinger filter is registered.
120+
*
121+
* @covers ::register_hooks
122+
*/
123+
public function test_webfinger_filter_registered() {
124+
$this->assertTrue( has_filter( 'webfinger_user_data' ) !== false );
125+
$this->assertTrue( has_filter( 'webfinger_post_data' ) !== false );
126+
$this->assertTrue( has_filter( 'host_meta' ) !== false );
127+
}
128+
129+
/**
130+
* Test admin hooks are registered.
131+
*
132+
* @covers ::register_admin_hooks
133+
*/
134+
public function test_admin_hooks_registered() {
135+
$this->assertTrue( has_action( 'admin_init' ) !== false );
136+
}
137+
}

0 commit comments

Comments
 (0)