You've already forked wp-prometheus
190 lines
5.9 KiB
PHP
190 lines
5.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Magdev\WpPrometheus\Tests\Unit;
|
||
|
|
|
||
|
|
use Magdev\WpPrometheus\Installer;
|
||
|
|
use Magdev\WpPrometheus\Tests\Helpers\GlobalFunctionState;
|
||
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||
|
|
use PHPUnit\Framework\Attributes\Test;
|
||
|
|
|
||
|
|
#[CoversClass(Installer::class)]
|
||
|
|
class InstallerTest extends TestCase
|
||
|
|
{
|
||
|
|
// ── activate() ───────────────────────────────────────────────────
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function activate_stores_activation_time(): void
|
||
|
|
{
|
||
|
|
Installer::activate();
|
||
|
|
|
||
|
|
$this->assertArrayHasKey(
|
||
|
|
'wp_prometheus_activated',
|
||
|
|
GlobalFunctionState::$options
|
||
|
|
);
|
||
|
|
$this->assertIsInt(GlobalFunctionState::$options['wp_prometheus_activated']);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function activate_flushes_rewrite_rules(): void
|
||
|
|
{
|
||
|
|
Installer::activate();
|
||
|
|
|
||
|
|
$this->assertGreaterThanOrEqual(
|
||
|
|
1,
|
||
|
|
GlobalFunctionState::getCallCount('flush_rewrite_rules')
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function activate_generates_auth_token_when_not_set(): void
|
||
|
|
{
|
||
|
|
Installer::activate();
|
||
|
|
|
||
|
|
$this->assertArrayHasKey(
|
||
|
|
'wp_prometheus_auth_token',
|
||
|
|
GlobalFunctionState::$options
|
||
|
|
);
|
||
|
|
$this->assertNotEmpty(GlobalFunctionState::$options['wp_prometheus_auth_token']);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function activate_preserves_existing_auth_token(): void
|
||
|
|
{
|
||
|
|
GlobalFunctionState::$options['wp_prometheus_auth_token'] = 'existing-token';
|
||
|
|
|
||
|
|
Installer::activate();
|
||
|
|
|
||
|
|
$this->assertSame(
|
||
|
|
'existing-token',
|
||
|
|
GlobalFunctionState::$options['wp_prometheus_auth_token']
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function activate_enables_default_metrics(): void
|
||
|
|
{
|
||
|
|
Installer::activate();
|
||
|
|
|
||
|
|
$this->assertSame(
|
||
|
|
1,
|
||
|
|
GlobalFunctionState::$options['wp_prometheus_enable_default_metrics']
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function activate_sets_default_enabled_metrics_list(): void
|
||
|
|
{
|
||
|
|
Installer::activate();
|
||
|
|
|
||
|
|
$enabled = GlobalFunctionState::$options['wp_prometheus_enabled_metrics'];
|
||
|
|
$this->assertIsArray($enabled);
|
||
|
|
$this->assertContains('wordpress_info', $enabled);
|
||
|
|
$this->assertContains('wordpress_users_total', $enabled);
|
||
|
|
$this->assertContains('wordpress_posts_total', $enabled);
|
||
|
|
$this->assertContains('wordpress_comments_total', $enabled);
|
||
|
|
$this->assertContains('wordpress_plugins_total', $enabled);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function activate_preserves_existing_enabled_metrics(): void
|
||
|
|
{
|
||
|
|
$custom = ['wordpress_info', 'wordpress_users_total'];
|
||
|
|
GlobalFunctionState::$options['wp_prometheus_enabled_metrics'] = $custom;
|
||
|
|
|
||
|
|
Installer::activate();
|
||
|
|
|
||
|
|
$this->assertSame(
|
||
|
|
$custom,
|
||
|
|
GlobalFunctionState::$options['wp_prometheus_enabled_metrics']
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ── deactivate() ─────────────────────────────────────────────────
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function deactivate_flushes_rewrite_rules(): void
|
||
|
|
{
|
||
|
|
Installer::deactivate();
|
||
|
|
|
||
|
|
$this->assertGreaterThanOrEqual(
|
||
|
|
1,
|
||
|
|
GlobalFunctionState::getCallCount('flush_rewrite_rules')
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ── uninstall() ──────────────────────────────────────────────────
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function uninstall_removes_all_plugin_options(): void
|
||
|
|
{
|
||
|
|
// Set up options that should be cleaned.
|
||
|
|
$expected_options = [
|
||
|
|
'wp_prometheus_activated',
|
||
|
|
'wp_prometheus_license_key',
|
||
|
|
'wp_prometheus_license_server_url',
|
||
|
|
'wp_prometheus_license_server_secret',
|
||
|
|
'wp_prometheus_license_status',
|
||
|
|
'wp_prometheus_license_data',
|
||
|
|
'wp_prometheus_license_last_check',
|
||
|
|
'wp_prometheus_auth_token',
|
||
|
|
'wp_prometheus_enable_default_metrics',
|
||
|
|
'wp_prometheus_enabled_metrics',
|
||
|
|
'wp_prometheus_runtime_metrics',
|
||
|
|
'wp_prometheus_custom_metrics',
|
||
|
|
'wp_prometheus_isolated_mode',
|
||
|
|
'wp_prometheus_storage_adapter',
|
||
|
|
'wp_prometheus_redis_host',
|
||
|
|
'wp_prometheus_redis_port',
|
||
|
|
'wp_prometheus_redis_password',
|
||
|
|
'wp_prometheus_redis_database',
|
||
|
|
'wp_prometheus_redis_prefix',
|
||
|
|
'wp_prometheus_apcu_prefix',
|
||
|
|
];
|
||
|
|
|
||
|
|
foreach ($expected_options as $option) {
|
||
|
|
GlobalFunctionState::$options[$option] = 'test_value';
|
||
|
|
}
|
||
|
|
|
||
|
|
Installer::uninstall();
|
||
|
|
|
||
|
|
// Verify all options were deleted.
|
||
|
|
foreach ($expected_options as $option) {
|
||
|
|
$this->assertArrayNotHasKey(
|
||
|
|
$option,
|
||
|
|
GlobalFunctionState::$options,
|
||
|
|
"Option '$option' was not deleted during uninstall"
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function uninstall_removes_license_transient(): void
|
||
|
|
{
|
||
|
|
Installer::uninstall();
|
||
|
|
|
||
|
|
$this->assertGreaterThanOrEqual(
|
||
|
|
1,
|
||
|
|
GlobalFunctionState::getCallCount('delete_transient')
|
||
|
|
);
|
||
|
|
|
||
|
|
// Verify the specific transient was targeted.
|
||
|
|
$args = GlobalFunctionState::$callArgs['delete_transient'] ?? [];
|
||
|
|
$transientNames = array_column($args, 0);
|
||
|
|
$this->assertContains('wp_prometheus_license_check', $transientNames);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Test]
|
||
|
|
public function uninstall_delete_option_call_count_matches(): void
|
||
|
|
{
|
||
|
|
Installer::uninstall();
|
||
|
|
|
||
|
|
// Should call delete_option for each option in the list (20 options).
|
||
|
|
$this->assertSame(
|
||
|
|
20,
|
||
|
|
GlobalFunctionState::getCallCount('delete_option')
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|