Skip to content

Commit 35e31eb

Browse files
committed
Add documentation and tests for lifecycle methods
- Document why activate() calls both init() and add_rewrite_rules() - Add test for init() guard clause preventing double initialization - Add test for activate() method - Add test for deactivate() method
1 parent aae2d65 commit 35e31eb

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

includes/class-nodeinfo.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ public function add_rewrite_rules() {
158158
* Handle plugin activation.
159159
*
160160
* Initializes the plugin and flushes rewrite rules.
161+
*
162+
* Note: We call init() to register all hooks. However, during activation
163+
* the 'init' hook may have already fired, so we also call add_rewrite_rules()
164+
* directly to ensure rules are registered.
161165
*/
162166
public static function activate() {
163167
$instance = self::get_instance();

tests/phpunit/tests/class-test-nodeinfo.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,65 @@ public function test_admin_hooks_registered() {
153153

154154
$this->assertNotFalse( has_action( 'admin_init' ) );
155155
}
156+
157+
/**
158+
* Test that init() guard prevents multiple initializations.
159+
*
160+
* @covers ::init
161+
*/
162+
public function test_init_guard_prevents_double_initialization() {
163+
$instance = \Nodeinfo\Nodeinfo::get_instance();
164+
165+
// Get the filter count before calling init again.
166+
$filter_count_before = has_filter( 'nodeinfo_discovery' );
167+
168+
// Call init() again - should be guarded.
169+
$instance->init();
170+
171+
// Filter count should remain the same.
172+
$filter_count_after = has_filter( 'nodeinfo_discovery' );
173+
174+
$this->assertSame( $filter_count_before, $filter_count_after );
175+
}
176+
177+
/**
178+
* Test activate() method registers rewrite rules.
179+
*
180+
* @covers ::activate
181+
*/
182+
public function test_activate_registers_rewrite_rules() {
183+
global $wp_rewrite;
184+
185+
// Save original permalink structure.
186+
$original_structure = $wp_rewrite->permalink_structure;
187+
188+
// Enable permalinks for testing.
189+
$wp_rewrite->set_permalink_structure( '/%postname%/' );
190+
191+
// Call activate.
192+
\Nodeinfo\Nodeinfo::activate();
193+
194+
$rules = $wp_rewrite->wp_rewrite_rules();
195+
196+
$this->assertIsArray( $rules );
197+
$this->assertArrayHasKey( '^.well-known/nodeinfo', $rules );
198+
199+
// Restore original permalink structure.
200+
$wp_rewrite->set_permalink_structure( $original_structure );
201+
$wp_rewrite->flush_rules();
202+
}
203+
204+
/**
205+
* Test deactivate() method flushes rewrite rules.
206+
*
207+
* @covers ::deactivate
208+
*/
209+
public function test_deactivate_flushes_rewrite_rules() {
210+
// This test verifies deactivate() runs without errors.
211+
// The actual effect (flushing rules) is internal to WordPress.
212+
\Nodeinfo\Nodeinfo::deactivate();
213+
214+
// If we get here without exceptions, the test passes.
215+
$this->assertTrue( true );
216+
}
156217
}

0 commit comments

Comments
 (0)