diff --git a/lib/Cleantalk/ApbctWP/State.php b/lib/Cleantalk/ApbctWP/State.php index 92a01c04f..b17ac727e 100644 --- a/lib/Cleantalk/ApbctWP/State.php +++ b/lib/Cleantalk/ApbctWP/State.php @@ -482,10 +482,10 @@ protected function setDefinitions() { global $wpdb; - $db_prefix = is_multisite() && is_main_site() ? $wpdb->base_prefix : $wpdb->prefix; + $db_prefix = is_multisite() && $this->isMainSite() ? $wpdb->base_prefix : $wpdb->prefix; // Use tables from main site on wpms_mode=2 $fw_db_prefix = - is_multisite() && ! is_main_site() && $this->network_settings['multisite__work_mode'] == 2 + is_multisite() && ! $this->isMainSite() && $this->getWpmsMode() == 2 ? $wpdb->base_prefix : $db_prefix; @@ -648,12 +648,16 @@ protected function init() $this->stats['no_cookie_data_taken'] = null; // Network with Mutual Access key - if ( ! is_main_site() && $this->network_settings['multisite__work_mode'] == 2 ) { - // Get stats from main blog - switch_to_blog(get_main_site_id()); + if ( ! $this->isMainSite() && $this->getWpmsMode() === 2 ) { + // Get stats and errors from main blog + + $this->switchToMainBlog(); $main_blog_stats = get_option($this->option_prefix . '_stats'); - restore_current_blog(); + $main_blog_errors = get_option($this->option_prefix . '_errors'); + $this->switchToCurrentBlog(); + $this->stats = $main_blog_stats; + $this->errors = $main_blog_errors; $this->api_key = $this->network_settings['apikey']; $this->key_is_ok = $this->network_data['key_is_ok']; $this->user_token = $this->network_data['user_token']; @@ -1047,4 +1051,24 @@ public function getJsErrorsReport() return $this->js_errors_report; } + + protected function isMainSite() + { + return is_main_site(); + } + + protected function getWpmsMode() + { + return (int) $this->network_settings['multisite__work_mode']; + } + + protected function switchToMainBlog() + { + switch_to_blog(get_main_site_id()); + } + + protected function switchToCurrentBlog() + { + restore_current_blog(); + } } diff --git a/tests/ApbctWP/TestState.php b/tests/ApbctWP/TestState.php index aa413ad8d..e326f550c 100644 --- a/tests/ApbctWP/TestState.php +++ b/tests/ApbctWP/TestState.php @@ -4,6 +4,8 @@ use PHPUnit\Framework\TestCase; use PHPUnit\Framework\Error\Notice; +require_once(CLEANTALK_PLUGIN_DIR . 'inc/cleantalk-updater.php'); + class TestApbctState extends TestCase { @@ -22,6 +24,7 @@ public function testIsHaveErrors_haveErrors() update_option( 'cleantalk_errors', array( 'error_type' => 'Error text' ) ); $apbct = new State( 'cleantalk', array('settings', 'data', 'errors', 'remote_calls', 'stats', 'fw_stats') ); $this->assertTrue( $apbct->isHaveErrors() ); + delete_option('cleantalk_errors'); } public function testIsHaveErrors_emptyErrors() @@ -29,6 +32,7 @@ public function testIsHaveErrors_emptyErrors() update_option( 'cleantalk_errors', array() ); $apbct = new State( 'cleantalk', array('settings', 'data', 'errors', 'remote_calls', 'stats', 'fw_stats') ); $this->assertFalse( $apbct->isHaveErrors() ); + delete_option('cleantalk_errors'); } public function testIsHaveErrors_emptyInnerErrors() @@ -36,6 +40,7 @@ public function testIsHaveErrors_emptyInnerErrors() update_option( 'cleantalk_errors', array( 'error_type' => array() ) ); $apbct = new State( 'cleantalk', array('settings', 'data', 'errors', 'remote_calls', 'stats', 'fw_stats') ); $this->assertFalse( $apbct->isHaveErrors() ); + delete_option('cleantalk_errors'); } public function testIsHaveErrors_filledInnerErrors() @@ -43,6 +48,22 @@ public function testIsHaveErrors_filledInnerErrors() update_option( 'cleantalk_errors', array( 'error_type' => array( 'error_text' => 'Error text' ) ) ); $apbct = new State( 'cleantalk', array('settings', 'data', 'errors', 'remote_calls', 'stats', 'fw_stats') ); $this->assertTrue( $apbct->isHaveErrors() ); + delete_option('cleantalk_errors'); + } + + public function testErrorsArrayFromState() + { + $apbct = new State('cleantalk', array('settings', 'errors')); + + $apbct->errorAdd('api', 'error'); + + $errors_from_state = (array) $apbct->errors; + + $this->assertArrayHasKey('api', $errors_from_state); + $this->assertArrayHasKey('error', $errors_from_state['api'][0]); + $this->assertArrayHasKey('error_time', $errors_from_state['api'][0]); + + delete_option('cleantalk_errors'); } //UpdateVars section @@ -58,6 +79,7 @@ public function testAutoSaveVars__remote_calls(){ apbct_run_update_actions('6.1','6.2'); $db_result = get_option('cleantalk_remote_calls')['post_api_key']; $this->assertEquals(array ('last_call' => 0,), $db_result); + delete_option('cleantalk_remote_calls'); } public function testAutoSaveVars__settings(){ @@ -71,6 +93,7 @@ public function testAutoSaveVars__settings(){ apbct_run_update_actions('6.1','6.2'); $db_result = get_option('cleantalk_settings')['forms__registrations_test']; $this->assertEquals(1, $db_result); + delete_option('cleantalk_settings'); } public function testAutoSaveVars__data(){ @@ -84,6 +107,7 @@ public function testAutoSaveVars__data(){ apbct_run_update_actions('6.1','6.2'); $db_result = get_option('cleantalk_data')['js_key_lifetime']; $this->assertEquals(86400, $db_result); + delete_option('cleantalk_data'); } public function testAutoSaveVars__network_settings(){ @@ -97,6 +121,7 @@ public function testAutoSaveVars__network_settings(){ apbct_run_update_actions('6.1','6.2'); $db_result = get_option('cleantalk_network_settings')['multisite__white_label__plugin_name']; $this->assertEquals('Anti-Spam by CleanTalk', $db_result); + delete_option('network_settings'); } public function testAutoSaveVars__network_data(){ @@ -110,6 +135,7 @@ public function testAutoSaveVars__network_data(){ apbct_run_update_actions('6.1','6.2'); $db_result = get_option('cleantalk_network_data')['moderate']; $this->assertEquals(0, $db_result); + delete_option('cleantalk_network_data'); } public function testAutoSaveVars__stats(){ @@ -161,6 +187,7 @@ public function testAutoSaveVars__stats(){ $this->assertEquals(14400, $db_result); $db_result = get_option('cleantalk_stats')['sfw']['sending_logs__timestamp']; $this->assertEquals(10000, $db_result); + delete_option('cleantalk_stats'); } @@ -175,6 +202,7 @@ public function testAutoSaveVars__fw_stats(){ apbct_run_update_actions('6.1','6.2'); $db_result = get_option('cleantalk_fw_stats')['firewall_updating_id']; $this->assertEquals(null, $db_result); + delete_option('cleantalk_fw_stats'); } public function testAutoSaveVars__fw_stats_await_exception_without_var_updater(){ @@ -191,5 +219,33 @@ public function testAutoSaveVars__fw_stats_await_exception_without_var_updater() //await udefined index $this->expectException(Notice::class); $db_result = get_option('cleantalk_fw_stats')['firewall_updating_id']; + delete_option('cleantalk_fw_stats'); + } + + public function testInit() + { + $apbct = new class ('cleantalk', array('settings', 'errors')) extends State { + protected function isMainSite() + { + return false; + } + protected function getWpmsMode() + { + return 2; + } + protected function switchToMainBlog(){} + protected function switchToCurrentBlog(){} + + }; + + $apbct->errorAdd('api', 'error'); + + $errors_from_state = (array) $apbct->errors; + + $this->assertArrayHasKey('api', $errors_from_state); + $this->assertArrayHasKey('error', $errors_from_state['api'][0]); + $this->assertArrayHasKey('error_time', $errors_from_state['api'][0]); + + delete_option('cleantalk_errors'); } }