(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] ); // Cache-seam keys (WpCache -> transients): $key = 'sg_xxx_' . md5(...) preg_match_all( "/'(sg_\w+?_)'\s*\.\s*md5/", $src, $d ); $dyn = array_merge( $dyn, $d[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->assertContains( 'sg_pp_token_', Uninstaller::transient_prefixes(), 'cached PayPal access token' ); $this->assertContains( 'sg_wh_', Uninstaller::transient_prefixes(), 'webhook de-dupe markers' ); $this->assertContains( 'sg_pp_token_', $dyn, 'scan sanity check' ); $this->assertContains( 'sg_wh_', $dyn, 'scan sanity check' ); $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( 2 * count( Uninstaller::transient_prefixes() ), $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|pp_token|wh)_%/', str_replace( '\\', '', $q ) ); } } }