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
204 lines
8.2 KiB
PHP
204 lines
8.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/**
|
|
* Integration tests for WPDO_Zone_Archive against real MariaDB.
|
|
*
|
|
* Covers archive/get (with gzip decompression), archive_batch (transaction),
|
|
* delete, stats(), and restore().
|
|
*/
|
|
class ZoneArchiveIntegrationTest extends TestCase {
|
|
|
|
private const TABLE = 'wp_itest_wpdo_archive';
|
|
|
|
// ── Fixture lifecycle ─────────────────────────────────────────────────
|
|
|
|
public static function setUpBeforeClass(): void {
|
|
global $wpdb;
|
|
$wpdb->query(
|
|
'CREATE TABLE IF NOT EXISTS `' . self::TABLE . '` (
|
|
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
|
`post_id` bigint(20) unsigned NOT NULL DEFAULT 0,
|
|
`post_type` varchar(20) NOT NULL DEFAULT \'\',
|
|
`meta_key` varchar(255) NOT NULL DEFAULT \'\',
|
|
`meta_value` longtext DEFAULT NULL,
|
|
`compressed` tinyint(1) NOT NULL DEFAULT 0,
|
|
`archived_at` datetime NOT NULL DEFAULT \'0000-00-00 00:00:00\',
|
|
`original_meta_id` bigint(20) unsigned NOT NULL DEFAULT 0,
|
|
PRIMARY KEY (`id`),
|
|
KEY `post_id` (`post_id`),
|
|
KEY `archived_at` (`archived_at`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4'
|
|
);
|
|
}
|
|
|
|
public static function tearDownAfterClass(): void {
|
|
global $wpdb;
|
|
$wpdb->query( 'DROP TABLE IF EXISTS `' . self::TABLE . '`' );
|
|
}
|
|
|
|
protected function setUp(): void {
|
|
global $wpdb;
|
|
$wpdb->query( 'TRUNCATE TABLE `' . self::TABLE . '`' );
|
|
$GLOBALS['_wp_postmeta'] = [];
|
|
}
|
|
|
|
// ── archive / get ─────────────────────────────────────────────────────
|
|
|
|
public function test_archive_stores_plain_entry(): void {
|
|
WPDO_Zone_Archive::archive( 1, 'hp_listing', 'hp_price', '199.99' );
|
|
|
|
$rows = WPDO_Zone_Archive::get( 1 );
|
|
$this->assertCount( 1, $rows );
|
|
$this->assertSame( 'hp_price', $rows[0]['meta_key'] );
|
|
$this->assertSame( '199.99', $rows[0]['meta_value'] );
|
|
}
|
|
|
|
public function test_archive_with_compression_stores_and_decompresses(): void {
|
|
$original = 'large content that compresses well: ' . str_repeat( 'abcdef', 50 );
|
|
WPDO_Zone_Archive::archive( 2, 'hp_listing', 'hp_desc', $original, 0, true );
|
|
|
|
$rows = WPDO_Zone_Archive::get( 2, 'hp_desc' );
|
|
$this->assertCount( 1, $rows );
|
|
// get() decompresses automatically — value must match original.
|
|
$this->assertSame( $original, $rows[0]['meta_value'] );
|
|
}
|
|
|
|
public function test_get_with_meta_key_filter_returns_only_that_key(): void {
|
|
WPDO_Zone_Archive::archive( 3, 'hp_listing', 'hp_price', '50.00' );
|
|
WPDO_Zone_Archive::archive( 3, 'hp_listing', 'hp_featured', '1' );
|
|
WPDO_Zone_Archive::archive( 3, 'hp_listing', 'hp_verified', '1' );
|
|
|
|
$rows = WPDO_Zone_Archive::get( 3, 'hp_price' );
|
|
$this->assertCount( 1, $rows );
|
|
$this->assertSame( 'hp_price', $rows[0]['meta_key'] );
|
|
}
|
|
|
|
public function test_get_without_filter_returns_all_keys(): void {
|
|
WPDO_Zone_Archive::archive( 4, 'hp_listing', 'hp_price', '75.00' );
|
|
WPDO_Zone_Archive::archive( 4, 'hp_listing', 'hp_featured', '0' );
|
|
|
|
$rows = WPDO_Zone_Archive::get( 4 );
|
|
$this->assertCount( 2, $rows );
|
|
}
|
|
|
|
public function test_get_returns_empty_for_missing_post(): void {
|
|
$rows = WPDO_Zone_Archive::get( 9999 );
|
|
$this->assertSame( [], $rows );
|
|
}
|
|
|
|
// ── archive_batch ────────────────────────────────────────────────────
|
|
|
|
public function test_archive_batch_stores_all_entries_in_transaction(): void {
|
|
$entries = [
|
|
[ 'post_id' => 5, 'post_type' => 'hp_listing', 'meta_key' => 'hp_price', 'meta_value' => '100.00', 'meta_id' => 0 ],
|
|
[ 'post_id' => 5, 'post_type' => 'hp_listing', 'meta_key' => 'hp_featured', 'meta_value' => '1', 'meta_id' => 0 ],
|
|
[ 'post_id' => 6, 'post_type' => 'hp_vendor', 'meta_key' => 'hp_rate', 'meta_value' => '50.00', 'meta_id' => 0 ],
|
|
];
|
|
|
|
WPDO_Zone_Archive::archive_batch( $entries );
|
|
|
|
$this->assertCount( 2, WPDO_Zone_Archive::get( 5 ) );
|
|
$this->assertCount( 1, WPDO_Zone_Archive::get( 6 ) );
|
|
}
|
|
|
|
public function test_archive_batch_with_compression(): void {
|
|
$original = str_repeat( 'x', 200 );
|
|
WPDO_Zone_Archive::archive_batch(
|
|
[ [ 'post_id' => 7, 'post_type' => 'hp_listing', 'meta_key' => 'hp_desc', 'meta_value' => $original, 'meta_id' => 0 ] ],
|
|
true
|
|
);
|
|
|
|
$rows = WPDO_Zone_Archive::get( 7, 'hp_desc' );
|
|
$this->assertSame( $original, $rows[0]['meta_value'] );
|
|
}
|
|
|
|
// ── delete ───────────────────────────────────────────────────────────
|
|
|
|
public function test_delete_removes_all_entries_for_post(): void {
|
|
WPDO_Zone_Archive::archive( 8, 'hp_listing', 'hp_price', '30.00' );
|
|
WPDO_Zone_Archive::archive( 8, 'hp_listing', 'hp_featured', '1' );
|
|
$this->assertCount( 2, WPDO_Zone_Archive::get( 8 ) );
|
|
|
|
WPDO_Zone_Archive::delete( 8 );
|
|
$this->assertSame( [], WPDO_Zone_Archive::get( 8 ) );
|
|
}
|
|
|
|
public function test_delete_does_not_affect_other_posts(): void {
|
|
WPDO_Zone_Archive::archive( 9, 'hp_listing', 'hp_price', '10.00' );
|
|
WPDO_Zone_Archive::archive( 10, 'hp_listing', 'hp_price', '20.00' );
|
|
|
|
WPDO_Zone_Archive::delete( 9 );
|
|
|
|
$this->assertSame( [], WPDO_Zone_Archive::get( 9 ) );
|
|
$this->assertCount( 1, WPDO_Zone_Archive::get( 10 ) );
|
|
}
|
|
|
|
// ── stats ─────────────────────────────────────────────────────────────
|
|
|
|
public function test_stats_counts_total_and_compressed_rows(): void {
|
|
WPDO_Zone_Archive::archive( 11, 'hp_listing', 'hp_price', '1.00', 0, false );
|
|
WPDO_Zone_Archive::archive( 12, 'hp_listing', 'hp_price', '2.00', 0, true );
|
|
WPDO_Zone_Archive::archive( 13, 'hp_listing', 'hp_price', '3.00', 0, true );
|
|
|
|
$stats = WPDO_Zone_Archive::stats();
|
|
$this->assertSame( 3, $stats['total_rows'] );
|
|
$this->assertSame( 2, $stats['compressed_rows'] );
|
|
}
|
|
|
|
public function test_stats_groups_by_post_type(): void {
|
|
WPDO_Zone_Archive::archive( 14, 'hp_listing', 'hp_price', '1.00' );
|
|
WPDO_Zone_Archive::archive( 15, 'hp_listing', 'hp_price', '2.00' );
|
|
WPDO_Zone_Archive::archive( 16, 'hp_vendor', 'hp_rate', '3.00' );
|
|
|
|
$stats = WPDO_Zone_Archive::stats();
|
|
$type_map = array_column( $stats['post_types'], 'cnt', 'post_type' );
|
|
|
|
$this->assertSame( '2', $type_map['hp_listing'] );
|
|
$this->assertSame( '1', $type_map['hp_vendor'] );
|
|
}
|
|
|
|
public function test_stats_returns_zeros_on_empty_table(): void {
|
|
$stats = WPDO_Zone_Archive::stats();
|
|
$this->assertSame( 0, $stats['total_rows'] );
|
|
$this->assertSame( 0, $stats['compressed_rows'] );
|
|
$this->assertSame( [], $stats['post_types'] );
|
|
}
|
|
|
|
// ── restore ───────────────────────────────────────────────────────────
|
|
|
|
public function test_restore_writes_to_postmeta_and_removes_from_archive(): void {
|
|
WPDO_Zone_Archive::archive( 17, 'hp_listing', 'hp_price', '99.00' );
|
|
WPDO_Zone_Archive::archive( 17, 'hp_listing', 'hp_featured', '1' );
|
|
|
|
$count = WPDO_Zone_Archive::restore( 17 );
|
|
|
|
// Two entries restored.
|
|
$this->assertSame( 2, $count );
|
|
|
|
// Postmeta updated via stub.
|
|
$this->assertSame( '99.00', $GLOBALS['_wp_postmeta'][17]['hp_price'] );
|
|
$this->assertSame( '1', $GLOBALS['_wp_postmeta'][17]['hp_featured'] );
|
|
|
|
// Archive cleared.
|
|
$this->assertSame( [], WPDO_Zone_Archive::get( 17 ) );
|
|
}
|
|
|
|
public function test_restore_with_meta_key_filter_only_restores_that_key(): void {
|
|
WPDO_Zone_Archive::archive( 18, 'hp_listing', 'hp_price', '55.00' );
|
|
WPDO_Zone_Archive::archive( 18, 'hp_listing', 'hp_featured', '0' );
|
|
|
|
$count = WPDO_Zone_Archive::restore( 18, 'hp_price' );
|
|
|
|
$this->assertSame( 1, $count );
|
|
$this->assertSame( '55.00', $GLOBALS['_wp_postmeta'][18]['hp_price'] );
|
|
|
|
// hp_featured should still be in archive.
|
|
$remaining = WPDO_Zone_Archive::get( 18 );
|
|
$this->assertCount( 1, $remaining );
|
|
$this->assertSame( 'hp_featured', $remaining[0]['meta_key'] );
|
|
}
|
|
}
|