Files
wp-donations/tests/Unit/UninstallerTest.php
T

113 lines
5.3 KiB
PHP
Raw Permalink Normal View History

<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use PHPUnit\Framework\TestCase;
use StreamGoals\Core\Maintenance;
use StreamGoals\Core\Settings;
use StreamGoals\Core\Uninstaller;
/** Guards the uninstall lists so a newly added table/option/transient/cron cannot be forgotten. */
final class UninstallerTest extends TestCase {
private const ROOT = __DIR__ . '/../../';
/** @return string[] contents of every PHP file under includes/ plus the main file, keyed by path */
private function sources(): array {
$out = array( 'streamgoals-donate.php' => (string) file_get_contents( self::ROOT . 'streamgoals-donate.php' ) );
$it = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( self::ROOT . 'includes', \FilesystemIterator::SKIP_DOTS ) );
foreach ( $it as $f ) {
if ( 'php' === $f->getExtension() && 'Uninstaller.php' !== $f->getFilename() ) {
$out[ $f->getPathname() ] = (string) file_get_contents( $f->getPathname() );
}
}
return $out;
}
public function test_every_installer_table_is_dropped(): void {
$src = (string) file_get_contents( self::ROOT . 'includes/Core/Installer.php' );
preg_match_all( '/CREATE TABLE \{\$p\}(\w+)/', $src, $m );
$this->assertNotEmpty( $m[1] );
$created = array_map( static fn( $t ) => 'sg_' . $t, $m[1] );
$this->assertSame( array(), array_diff( $created, Uninstaller::tables() ), 'table created by Installer but not dropped on uninstall' );
$this->assertSame( array(), array_diff( Uninstaller::tables(), $created ), 'uninstall drops a table Installer does not create' );
}
public function test_every_option_used_in_code_is_deleted(): void {
$found = array();
foreach ( $this->sources() as $src ) {
preg_match_all( "/(?:add|update|delete)_option\(\s*'(sg_\w+)'/", $src, $a );
preg_match_all( "/const\s+(?:OPTION|VERIFIED)\s*=\s*'(sg_\w+)'/", $src, $b );
$found = array_merge( $found, $a[1], $b[1] );
}
$found = array_unique( $found );
$this->assertContains( 'sg_settings', $found, 'scan sanity check' );
$this->assertContains( 'sg_overlay_epoch', $found );
$this->assertContains( 'sg_paypal_verified', $found );
$this->assertSame( array(), array_values( array_diff( $found, Uninstaller::options() ) ), 'option written by the plugin but not removed on uninstall' );
}
public function test_every_transient_and_cron_hook_is_cleaned(): void {
$fixed = array();
$dyn = array();
foreach ( $this->sources() as $src ) {
preg_match_all( "/(?:CACHE_KEY\s*=\s*|(?:set|get|delete)_site_transient\(\s*)'(sg_\w+)'/", $src, $a );
$fixed = array_merge( $fixed, $a[1] );
preg_match_all( "/(?:set|get|delete)_transient\(\s*'(sg_\w+?_)'\s*\./", $src, $b );
$dyn = array_merge( $dyn, $b[1] );
preg_match_all( "/'(sg_rl_)'\s*\./", $src, $c );
$dyn = array_merge( $dyn, $c[1] );
}
$this->assertContains( 'sg_update_release', $fixed, 'scan sanity check' );
$this->assertSame( array(), array_values( array_diff( array_unique( $fixed ), Uninstaller::site_transients() ) ) );
$this->assertSame( array(), array_values( array_diff( array_unique( $dyn ), Uninstaller::transient_prefixes() ) ) );
$this->assertContains( 'sg_rl_', Uninstaller::transient_prefixes() );
$this->assertContains( 'sg_notice_', Uninstaller::transient_prefixes() );
$this->assertSame( Maintenance::HOOK, Uninstaller::CRON_HOOK );
}
public function test_deactivation_unschedules_the_cron_event(): void {
$main = (string) file_get_contents( self::ROOT . 'streamgoals-donate.php' );
$this->assertMatchesRegularExpression( '/register_deactivation_hook\([^;]*Maintenance::class,\s*\'unschedule\'/', $main );
}
public function test_uninstall_is_opt_in_and_off_by_default(): void {
$this->assertFalse( Settings::defaults()['delete_data_on_uninstall'] );
$this->assertFalse( Uninstaller::wants_data_deleted( false ) );
$this->assertFalse( Uninstaller::wants_data_deleted( array() ) );
$this->assertFalse( Uninstaller::wants_data_deleted( array( 'delete_data_on_uninstall' => false ) ) );
$this->assertTrue( Uninstaller::wants_data_deleted( array( 'delete_data_on_uninstall' => true ) ) );
}
private function fakeDb(): object {
return new class() {
public string $prefix = 'wp7_';
public string $options = 'wp7_options';
public array $queries = array();
public function query( string $q ): int { $this->queries[] = $q; return 0; }
public function esc_like( string $s ): string { return addcslashes( $s, '_%\\' ); }
public function prepare( string $q, ...$a ): string { return str_replace( '%s', "'" . $a[0] . "'", $q ); }
};
}
public function test_drop_tables_issues_one_drop_per_table_with_prefix(): void {
$db = $this->fakeDb();
Uninstaller::drop_tables( $db );
$this->assertCount( count( Uninstaller::tables() ), $db->queries );
$this->assertContains( 'DROP TABLE IF EXISTS `wp7_sg_donations`', $db->queries );
foreach ( $db->queries as $q ) {
$this->assertMatchesRegularExpression( '/^DROP TABLE IF EXISTS `wp7_sg_\w+`$/', $q );
}
}
public function test_dynamic_transient_cleanup_only_targets_plugin_prefixes(): void {
$db = $this->fakeDb();
Uninstaller::delete_dynamic_transients( $db );
$this->assertCount( 4, $db->queries );
foreach ( $db->queries as $q ) {
$this->assertStringStartsWith( 'DELETE FROM wp7_options WHERE option_name LIKE', $q );
$this->assertMatchesRegularExpression( '/_transient_(timeout_)?sg_(rl|notice)_%/', str_replace( '\\', '', $q ) );
}
}
}