d36bb954d1
Baseline before backporting wp-data-optimizer v3.0.1-v3.4.6. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TbG1keQQ7XBa7qMQY16KCY
165 lines
8.9 KiB
PHP
165 lines
8.9 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/**
|
|
* Integration test: WPDO_Term_Comment_Garbage_Filter (v2.12.1).
|
|
*
|
|
* Tests the metadata filter callbacks directly with synthetic args.
|
|
* Live WP add_filter / get_term_meta wiring is exercised by a dev10
|
|
* smoke test (see CHANGELOG); these unit-style integration tests focus
|
|
* on the pure logic of pattern matching + counter behavior.
|
|
*/
|
|
class TermCommentGarbageFilterTest extends TestCase {
|
|
|
|
public static function setUpBeforeClass(): void {
|
|
if ( ! class_exists( 'WPDO_Term_Comment_Garbage_Filter' ) ) {
|
|
require_once WPDO_PLUGIN_DIR . 'includes/integrations/class-tmdo-term-comment-garbage-filter.php';
|
|
}
|
|
}
|
|
|
|
protected function setUp(): void {
|
|
// Reset wp_options state for clean per-test counter behavior.
|
|
$GLOBALS['_wp_options'] = array();
|
|
update_option( WPDO_Term_Comment_Garbage_Filter::OPT_ENABLED, '1' );
|
|
}
|
|
|
|
// ── is_shared_garbage_key ────────────────────────────────────────────────
|
|
|
|
public function test_is_shared_garbage_key_matches_wxr_import(): void {
|
|
$this->assertTrue( WPDO_Term_Comment_Garbage_Filter::is_shared_garbage_key( '_wxr_import_user' ) );
|
|
$this->assertTrue( WPDO_Term_Comment_Garbage_Filter::is_shared_garbage_key( '_wxr_import_post' ) );
|
|
$this->assertTrue( WPDO_Term_Comment_Garbage_Filter::is_shared_garbage_key( '_wxr_import_term' ) );
|
|
}
|
|
|
|
public function test_is_shared_garbage_key_matches_2meet_demo(): void {
|
|
$this->assertTrue( WPDO_Term_Comment_Garbage_Filter::is_shared_garbage_key( '_2meet_demo_music' ) );
|
|
$this->assertTrue( WPDO_Term_Comment_Garbage_Filter::is_shared_garbage_key( '_2meet_demo_adv' ) );
|
|
}
|
|
|
|
public function test_is_shared_garbage_key_rejects_legitimate_keys(): void {
|
|
$this->assertFalse( WPDO_Term_Comment_Garbage_Filter::is_shared_garbage_key( 'hp_sort_order' ) );
|
|
$this->assertFalse( WPDO_Term_Comment_Garbage_Filter::is_shared_garbage_key( 'hp_default' ) );
|
|
$this->assertFalse( WPDO_Term_Comment_Garbage_Filter::is_shared_garbage_key( 'hp_rating' ) );
|
|
}
|
|
|
|
public function test_is_shared_garbage_key_rejects_partial_match(): void {
|
|
// Substring matches should NOT trigger.
|
|
$this->assertFalse( WPDO_Term_Comment_Garbage_Filter::is_shared_garbage_key( 'something_wxr_import_' ) );
|
|
$this->assertFalse( WPDO_Term_Comment_Garbage_Filter::is_shared_garbage_key( '_wxr_imp' ) );
|
|
$this->assertFalse( WPDO_Term_Comment_Garbage_Filter::is_shared_garbage_key( '_2meet_demos' ) );
|
|
}
|
|
|
|
public function test_is_shared_garbage_key_rejects_non_string(): void {
|
|
$this->assertFalse( WPDO_Term_Comment_Garbage_Filter::is_shared_garbage_key( null ) );
|
|
$this->assertFalse( WPDO_Term_Comment_Garbage_Filter::is_shared_garbage_key( 123 ) );
|
|
$this->assertFalse( WPDO_Term_Comment_Garbage_Filter::is_shared_garbage_key( array() ) );
|
|
}
|
|
|
|
// ── is_comment_orphan_key ────────────────────────────────────────────────
|
|
|
|
public function test_is_comment_orphan_key_matches_post_domain_keys(): void {
|
|
$this->assertTrue( WPDO_Term_Comment_Garbage_Filter::is_comment_orphan_key( '_hp_price' ) );
|
|
$this->assertTrue( WPDO_Term_Comment_Garbage_Filter::is_comment_orphan_key( '_hp_status' ) );
|
|
$this->assertTrue( WPDO_Term_Comment_Garbage_Filter::is_comment_orphan_key( '_hp_featured' ) );
|
|
$this->assertTrue( WPDO_Term_Comment_Garbage_Filter::is_comment_orphan_key( '_hp_verified' ) );
|
|
$this->assertTrue( WPDO_Term_Comment_Garbage_Filter::is_comment_orphan_key( '_hp_view_count' ) );
|
|
$this->assertTrue( WPDO_Term_Comment_Garbage_Filter::is_comment_orphan_key( '_thumbnail_id' ) );
|
|
$this->assertTrue( WPDO_Term_Comment_Garbage_Filter::is_comment_orphan_key( '_edit_lock' ) );
|
|
$this->assertTrue( WPDO_Term_Comment_Garbage_Filter::is_comment_orphan_key( '_edit_last' ) );
|
|
}
|
|
|
|
public function test_is_comment_orphan_key_rejects_legitimate_comment_keys(): void {
|
|
$this->assertFalse( WPDO_Term_Comment_Garbage_Filter::is_comment_orphan_key( 'hp_rating' ) );
|
|
$this->assertFalse( WPDO_Term_Comment_Garbage_Filter::is_comment_orphan_key( 'note_group' ) );
|
|
}
|
|
|
|
public function test_is_comment_orphan_key_requires_exact_match(): void {
|
|
$this->assertFalse( WPDO_Term_Comment_Garbage_Filter::is_comment_orphan_key( '_hp_price_extended' ) );
|
|
$this->assertFalse( WPDO_Term_Comment_Garbage_Filter::is_comment_orphan_key( '_hp_pric' ) );
|
|
}
|
|
|
|
// ── on_term_write callback ───────────────────────────────────────────────
|
|
|
|
public function test_on_term_write_drops_garbage_keys(): void {
|
|
$result = WPDO_Term_Comment_Garbage_Filter::on_term_write( null, 1, '_wxr_import_user', 'val', false );
|
|
$this->assertTrue( $result, 'garbage write must short-circuit (return true)' );
|
|
}
|
|
|
|
public function test_on_term_write_passes_through_legitimate_keys(): void {
|
|
$result = WPDO_Term_Comment_Garbage_Filter::on_term_write( null, 1, 'hp_sort_order', '5', false );
|
|
$this->assertNull( $result, 'legitimate write must fall through (return null)' );
|
|
}
|
|
|
|
public function test_on_term_write_does_not_apply_comment_orphan_rules(): void {
|
|
// _hp_price is comment-only orphan; for term writes it must pass through
|
|
$result = WPDO_Term_Comment_Garbage_Filter::on_term_write( null, 1, '_hp_price', '99', false );
|
|
$this->assertNull( $result );
|
|
}
|
|
|
|
// ── on_comment_write callback ────────────────────────────────────────────
|
|
|
|
public function test_on_comment_write_drops_shared_garbage(): void {
|
|
$this->assertTrue( WPDO_Term_Comment_Garbage_Filter::on_comment_write( null, 1, '_wxr_import_user', 'a', false ) );
|
|
$this->assertTrue( WPDO_Term_Comment_Garbage_Filter::on_comment_write( null, 1, '_2meet_demo_music', '1', false ) );
|
|
}
|
|
|
|
public function test_on_comment_write_drops_orphan_post_meta(): void {
|
|
$this->assertTrue( WPDO_Term_Comment_Garbage_Filter::on_comment_write( null, 1, '_hp_price', '99', false ) );
|
|
$this->assertTrue( WPDO_Term_Comment_Garbage_Filter::on_comment_write( null, 1, '_thumbnail_id', '50', false ) );
|
|
$this->assertTrue( WPDO_Term_Comment_Garbage_Filter::on_comment_write( null, 1, '_edit_lock', '111:1', false ) );
|
|
}
|
|
|
|
public function test_on_comment_write_passes_through_legitimate_keys(): void {
|
|
$this->assertNull( WPDO_Term_Comment_Garbage_Filter::on_comment_write( null, 1, 'hp_rating', '5', false ) );
|
|
$this->assertNull( WPDO_Term_Comment_Garbage_Filter::on_comment_write( null, 1, 'note_group', 'foo', false ) );
|
|
}
|
|
|
|
// ── 24h drop counter ────────────────────────────────────────────────────
|
|
|
|
public function test_drop_counter_starts_at_zero(): void {
|
|
$this->assertSame( 0, WPDO_Term_Comment_Garbage_Filter::get_drop_count_24h() );
|
|
}
|
|
|
|
public function test_drop_counter_increments_on_each_drop(): void {
|
|
WPDO_Term_Comment_Garbage_Filter::on_term_write( null, 1, '_wxr_import_user', 'a', false );
|
|
WPDO_Term_Comment_Garbage_Filter::on_term_write( null, 2, '_2meet_demo_music', '1', false );
|
|
WPDO_Term_Comment_Garbage_Filter::on_comment_write( null, 3, '_hp_price', '99', false );
|
|
|
|
$this->assertSame( 3, WPDO_Term_Comment_Garbage_Filter::get_drop_count_24h() );
|
|
}
|
|
|
|
public function test_drop_counter_does_not_increment_on_legitimate_writes(): void {
|
|
WPDO_Term_Comment_Garbage_Filter::on_term_write( null, 1, 'hp_sort_order', '5', false );
|
|
WPDO_Term_Comment_Garbage_Filter::on_comment_write( null, 2, 'hp_rating', '5', false );
|
|
|
|
$this->assertSame( 0, WPDO_Term_Comment_Garbage_Filter::get_drop_count_24h() );
|
|
}
|
|
|
|
public function test_drop_counter_resets_after_24h(): void {
|
|
// Simulate counter from 25h ago
|
|
update_option( WPDO_Term_Comment_Garbage_Filter::OPT_DROPPED_COUNT, 100 );
|
|
update_option( WPDO_Term_Comment_Garbage_Filter::OPT_DROPPED_RESET_AT, time() - 25 * HOUR_IN_SECONDS );
|
|
|
|
// get_drop_count_24h returns 0 (auto-reset semantics)
|
|
$this->assertSame( 0, WPDO_Term_Comment_Garbage_Filter::get_drop_count_24h() );
|
|
|
|
// First new drop after expiry resets counter to 1
|
|
WPDO_Term_Comment_Garbage_Filter::on_term_write( null, 1, '_wxr_import_user', 'a', false );
|
|
$this->assertSame( 1, WPDO_Term_Comment_Garbage_Filter::get_drop_count_24h() );
|
|
}
|
|
|
|
// ── is_enabled toggle ───────────────────────────────────────────────────
|
|
|
|
public function test_is_enabled_defaults_true(): void {
|
|
delete_option( WPDO_Term_Comment_Garbage_Filter::OPT_ENABLED );
|
|
$this->assertTrue( WPDO_Term_Comment_Garbage_Filter::is_enabled() );
|
|
}
|
|
|
|
public function test_is_enabled_respects_zero_value(): void {
|
|
update_option( WPDO_Term_Comment_Garbage_Filter::OPT_ENABLED, '0' );
|
|
$this->assertFalse( WPDO_Term_Comment_Garbage_Filter::is_enabled() );
|
|
}
|
|
}
|