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
170 lines
7.0 KiB
PHP
170 lines
7.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/**
|
|
* Integration test: WPDO_Commentmeta_Cleaner — wp_commentmeta garbage cleanup (v2.12.0).
|
|
*
|
|
* Verifies count_garbage() and delete_garbage() against a real MariaDB test table:
|
|
* - target=wxr_import → meta_key LIKE '_wxr_import_%'
|
|
* - target=demo_data → meta_key LIKE '_2meet_demo_%'
|
|
* - target=transients → meta_key LIKE '_transient_%' OR LIKE '_transient_timeout_%'
|
|
* - target=orphan_post_meta → meta_key IN known orphan post-domain keys
|
|
* - target=all → union of all four
|
|
*/
|
|
class CommentmetaCleanerIntegrationTest extends TestCase {
|
|
|
|
private const COMMENTMETA = 'wp_itest_commentmeta';
|
|
|
|
public static function setUpBeforeClass(): void {
|
|
global $wpdb;
|
|
|
|
require_once WPDO_PLUGIN_DIR . 'includes/class-tmdo-commentmeta-cleaner.php';
|
|
|
|
$wpdb->commentmeta = self::COMMENTMETA;
|
|
|
|
$wpdb->query( 'DROP TABLE IF EXISTS `' . self::COMMENTMETA . '`' );
|
|
$wpdb->query(
|
|
'CREATE TABLE `' . self::COMMENTMETA . '` (
|
|
meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
|
comment_id bigint(20) unsigned NOT NULL DEFAULT 0,
|
|
meta_key varchar(255) DEFAULT NULL,
|
|
meta_value longtext,
|
|
PRIMARY KEY (meta_id),
|
|
KEY comment_id (comment_id),
|
|
KEY meta_key (meta_key(191))
|
|
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci'
|
|
);
|
|
}
|
|
|
|
public static function tearDownAfterClass(): void {
|
|
global $wpdb;
|
|
$wpdb->query( 'DROP TABLE IF EXISTS `' . self::COMMENTMETA . '`' );
|
|
}
|
|
|
|
protected function setUp(): void {
|
|
global $wpdb;
|
|
$wpdb->query( 'TRUNCATE TABLE `' . self::COMMENTMETA . '`' );
|
|
}
|
|
|
|
private function seed( array $rows ): void {
|
|
global $wpdb;
|
|
foreach ( $rows as $row ) {
|
|
$wpdb->insert( self::COMMENTMETA, $row );
|
|
}
|
|
}
|
|
|
|
// ── count_garbage ────────────────────────────────────────────────────────
|
|
|
|
public function test_count_garbage_returns_zero_for_empty_table(): void {
|
|
$counts = WPDO_Commentmeta_Cleaner::count_garbage( 'all' );
|
|
$this->assertSame( 0, $counts['wxr_import'] );
|
|
$this->assertSame( 0, $counts['demo_data'] );
|
|
$this->assertSame( 0, $counts['transients'] );
|
|
$this->assertSame( 0, $counts['orphan_post_meta'] );
|
|
$this->assertSame( 0, $counts['total'] );
|
|
}
|
|
|
|
public function test_count_garbage_counts_wxr_import(): void {
|
|
$this->seed( array(
|
|
array( 'comment_id' => 1, 'meta_key' => '_wxr_import_user', 'meta_value' => 'a' ),
|
|
array( 'comment_id' => 2, 'meta_key' => '_wxr_import_post', 'meta_value' => 'b' ),
|
|
array( 'comment_id' => 3, 'meta_key' => 'hp_rating', 'meta_value' => '5' ),
|
|
) );
|
|
|
|
$counts = WPDO_Commentmeta_Cleaner::count_garbage( 'wxr_import' );
|
|
$this->assertSame( 2, $counts['wxr_import'] );
|
|
$this->assertSame( 2, $counts['total'] );
|
|
}
|
|
|
|
public function test_count_garbage_counts_orphan_post_meta(): void {
|
|
$this->seed( array(
|
|
array( 'comment_id' => 1, 'meta_key' => '_hp_price', 'meta_value' => '99' ),
|
|
array( 'comment_id' => 1, 'meta_key' => '_hp_status', 'meta_value' => 'publish' ),
|
|
array( 'comment_id' => 2, 'meta_key' => '_thumbnail_id', 'meta_value' => '50' ),
|
|
array( 'comment_id' => 3, 'meta_key' => 'hp_rating', 'meta_value' => '5' ),
|
|
array( 'comment_id' => 4, 'meta_key' => 'note_group', 'meta_value' => 'foo' ),
|
|
) );
|
|
|
|
$counts = WPDO_Commentmeta_Cleaner::count_garbage( 'orphan_post_meta' );
|
|
$this->assertSame( 3, $counts['orphan_post_meta'] );
|
|
$this->assertSame( 3, $counts['total'] );
|
|
}
|
|
|
|
public function test_count_garbage_all_unions_four_buckets(): void {
|
|
$this->seed( array(
|
|
array( 'comment_id' => 1, 'meta_key' => '_wxr_import_user', 'meta_value' => 'a' ),
|
|
array( 'comment_id' => 2, 'meta_key' => '_2meet_demo_music', 'meta_value' => '1' ),
|
|
array( 'comment_id' => 3, 'meta_key' => '_transient_foo', 'meta_value' => 'b' ),
|
|
array( 'comment_id' => 4, 'meta_key' => '_hp_price', 'meta_value' => '99' ),
|
|
array( 'comment_id' => 5, 'meta_key' => 'hp_rating', 'meta_value' => '5' ),
|
|
) );
|
|
|
|
$counts = WPDO_Commentmeta_Cleaner::count_garbage( 'all' );
|
|
$this->assertSame( 1, $counts['wxr_import'] );
|
|
$this->assertSame( 1, $counts['demo_data'] );
|
|
$this->assertSame( 1, $counts['transients'] );
|
|
$this->assertSame( 1, $counts['orphan_post_meta'] );
|
|
$this->assertSame( 4, $counts['total'] );
|
|
}
|
|
|
|
// ── delete_garbage ────────────────────────────────────────────────────────
|
|
|
|
public function test_delete_garbage_removes_targeted_rows_only(): void {
|
|
$this->seed( array(
|
|
array( 'comment_id' => 1, 'meta_key' => '_wxr_import_user', 'meta_value' => 'a' ),
|
|
array( 'comment_id' => 2, 'meta_key' => '_2meet_demo_music', 'meta_value' => '1' ),
|
|
array( 'comment_id' => 3, 'meta_key' => '_transient_foo', 'meta_value' => 'b' ),
|
|
array( 'comment_id' => 4, 'meta_key' => '_hp_price', 'meta_value' => '99' ),
|
|
array( 'comment_id' => 5, 'meta_key' => 'hp_rating', 'meta_value' => '5' ),
|
|
array( 'comment_id' => 6, 'meta_key' => 'note_group', 'meta_value' => 'foo' ),
|
|
) );
|
|
|
|
$deleted = WPDO_Commentmeta_Cleaner::delete_garbage( 'all' );
|
|
$this->assertSame( 1, $deleted['wxr_import'] );
|
|
$this->assertSame( 1, $deleted['demo_data'] );
|
|
$this->assertSame( 1, $deleted['transients'] );
|
|
$this->assertSame( 1, $deleted['orphan_post_meta'] );
|
|
$this->assertSame( 4, $deleted['total'] );
|
|
|
|
global $wpdb;
|
|
$remaining = (int) $wpdb->get_var( 'SELECT COUNT(*) FROM `' . self::COMMENTMETA . '`' );
|
|
$this->assertSame( 2, $remaining, 'hp_rating + note_group must survive' );
|
|
}
|
|
|
|
public function test_delete_garbage_orphan_post_meta_specific(): void {
|
|
$this->seed( array(
|
|
array( 'comment_id' => 1, 'meta_key' => '_hp_price', 'meta_value' => '99' ),
|
|
array( 'comment_id' => 2, 'meta_key' => '_hp_featured', 'meta_value' => '1' ),
|
|
array( 'comment_id' => 3, 'meta_key' => '_edit_lock', 'meta_value' => '111:1' ),
|
|
array( 'comment_id' => 4, 'meta_key' => 'hp_rating', 'meta_value' => '5' ),
|
|
array( 'comment_id' => 5, 'meta_key' => 'note_group', 'meta_value' => 'foo' ),
|
|
) );
|
|
|
|
$deleted = WPDO_Commentmeta_Cleaner::delete_garbage( 'orphan_post_meta' );
|
|
$this->assertSame( 3, $deleted['orphan_post_meta'] );
|
|
$this->assertSame( 3, $deleted['total'] );
|
|
|
|
global $wpdb;
|
|
$keys = $wpdb->get_col( 'SELECT meta_key FROM `' . self::COMMENTMETA . '` ORDER BY meta_key' );
|
|
$this->assertSame( array( 'hp_rating', 'note_group' ), $keys );
|
|
}
|
|
|
|
public function test_delete_garbage_idempotent_on_clean_table(): void {
|
|
$this->seed( array(
|
|
array( 'comment_id' => 1, 'meta_key' => 'hp_rating', 'meta_value' => '5' ),
|
|
) );
|
|
|
|
$first = WPDO_Commentmeta_Cleaner::delete_garbage( 'all' );
|
|
$second = WPDO_Commentmeta_Cleaner::delete_garbage( 'all' );
|
|
$this->assertSame( 0, $first['total'] );
|
|
$this->assertSame( 0, $second['total'] );
|
|
}
|
|
|
|
public function test_invalid_target_throws(): void {
|
|
$this->expectException( InvalidArgumentException::class );
|
|
WPDO_Commentmeta_Cleaner::count_garbage( 'bogus' );
|
|
}
|
|
}
|