Skip to content

Commit 3cb6ce0

Browse files
authored
Fix. WPMS. Errors output for WPMS mutual-mutual mode fixed. (#772)
* Fix. WPMS. Errors output for WPMS mutual-mutual mode fixed. * Fix. Code. Unit test added: check errors array in State. * Fix. Code. Unit test added: `init()` in State. * Fix. Code. Code style fixed. * Fix. Code. Debug removed. * Fix. Code. Code style fixed.
1 parent dfea27d commit 3cb6ce0

File tree

2 files changed

+86
-6
lines changed

2 files changed

+86
-6
lines changed

lib/Cleantalk/ApbctWP/State.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -482,10 +482,10 @@ protected function setDefinitions()
482482
{
483483
global $wpdb;
484484

485-
$db_prefix = is_multisite() && is_main_site() ? $wpdb->base_prefix : $wpdb->prefix;
485+
$db_prefix = is_multisite() && $this->isMainSite() ? $wpdb->base_prefix : $wpdb->prefix;
486486
// Use tables from main site on wpms_mode=2
487487
$fw_db_prefix =
488-
is_multisite() && ! is_main_site() && $this->network_settings['multisite__work_mode'] == 2
488+
is_multisite() && ! $this->isMainSite() && $this->getWpmsMode() == 2
489489
? $wpdb->base_prefix
490490
: $db_prefix;
491491

@@ -648,12 +648,16 @@ protected function init()
648648
$this->stats['no_cookie_data_taken'] = null;
649649

650650
// Network with Mutual Access key
651-
if ( ! is_main_site() && $this->network_settings['multisite__work_mode'] == 2 ) {
652-
// Get stats from main blog
653-
switch_to_blog(get_main_site_id());
651+
if ( ! $this->isMainSite() && $this->getWpmsMode() === 2 ) {
652+
// Get stats and errors from main blog
653+
654+
$this->switchToMainBlog();
654655
$main_blog_stats = get_option($this->option_prefix . '_stats');
655-
restore_current_blog();
656+
$main_blog_errors = get_option($this->option_prefix . '_errors');
657+
$this->switchToCurrentBlog();
658+
656659
$this->stats = $main_blog_stats;
660+
$this->errors = $main_blog_errors;
657661
$this->api_key = $this->network_settings['apikey'];
658662
$this->key_is_ok = $this->network_data['key_is_ok'];
659663
$this->user_token = $this->network_data['user_token'];
@@ -1047,4 +1051,24 @@ public function getJsErrorsReport()
10471051

10481052
return $this->js_errors_report;
10491053
}
1054+
1055+
protected function isMainSite()
1056+
{
1057+
return is_main_site();
1058+
}
1059+
1060+
protected function getWpmsMode()
1061+
{
1062+
return (int) $this->network_settings['multisite__work_mode'];
1063+
}
1064+
1065+
protected function switchToMainBlog()
1066+
{
1067+
switch_to_blog(get_main_site_id());
1068+
}
1069+
1070+
protected function switchToCurrentBlog()
1071+
{
1072+
restore_current_blog();
1073+
}
10501074
}

tests/ApbctWP/TestState.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
use PHPUnit\Framework\TestCase;
55
use PHPUnit\Framework\Error\Notice;
66

7+
require_once(CLEANTALK_PLUGIN_DIR . 'inc/cleantalk-updater.php');
8+
79
class TestApbctState extends TestCase
810
{
911

@@ -22,27 +24,46 @@ public function testIsHaveErrors_haveErrors()
2224
update_option( 'cleantalk_errors', array( 'error_type' => 'Error text' ) );
2325
$apbct = new State( 'cleantalk', array('settings', 'data', 'errors', 'remote_calls', 'stats', 'fw_stats') );
2426
$this->assertTrue( $apbct->isHaveErrors() );
27+
delete_option('cleantalk_errors');
2528
}
2629

2730
public function testIsHaveErrors_emptyErrors()
2831
{
2932
update_option( 'cleantalk_errors', array() );
3033
$apbct = new State( 'cleantalk', array('settings', 'data', 'errors', 'remote_calls', 'stats', 'fw_stats') );
3134
$this->assertFalse( $apbct->isHaveErrors() );
35+
delete_option('cleantalk_errors');
3236
}
3337

3438
public function testIsHaveErrors_emptyInnerErrors()
3539
{
3640
update_option( 'cleantalk_errors', array( 'error_type' => array() ) );
3741
$apbct = new State( 'cleantalk', array('settings', 'data', 'errors', 'remote_calls', 'stats', 'fw_stats') );
3842
$this->assertFalse( $apbct->isHaveErrors() );
43+
delete_option('cleantalk_errors');
3944
}
4045

4146
public function testIsHaveErrors_filledInnerErrors()
4247
{
4348
update_option( 'cleantalk_errors', array( 'error_type' => array( 'error_text' => 'Error text' ) ) );
4449
$apbct = new State( 'cleantalk', array('settings', 'data', 'errors', 'remote_calls', 'stats', 'fw_stats') );
4550
$this->assertTrue( $apbct->isHaveErrors() );
51+
delete_option('cleantalk_errors');
52+
}
53+
54+
public function testErrorsArrayFromState()
55+
{
56+
$apbct = new State('cleantalk', array('settings', 'errors'));
57+
58+
$apbct->errorAdd('api', 'error');
59+
60+
$errors_from_state = (array) $apbct->errors;
61+
62+
$this->assertArrayHasKey('api', $errors_from_state);
63+
$this->assertArrayHasKey('error', $errors_from_state['api'][0]);
64+
$this->assertArrayHasKey('error_time', $errors_from_state['api'][0]);
65+
66+
delete_option('cleantalk_errors');
4667
}
4768

4869
//UpdateVars section
@@ -58,6 +79,7 @@ public function testAutoSaveVars__remote_calls(){
5879
apbct_run_update_actions('6.1','6.2');
5980
$db_result = get_option('cleantalk_remote_calls')['post_api_key'];
6081
$this->assertEquals(array ('last_call' => 0,), $db_result);
82+
delete_option('cleantalk_remote_calls');
6183
}
6284

6385
public function testAutoSaveVars__settings(){
@@ -71,6 +93,7 @@ public function testAutoSaveVars__settings(){
7193
apbct_run_update_actions('6.1','6.2');
7294
$db_result = get_option('cleantalk_settings')['forms__registrations_test'];
7395
$this->assertEquals(1, $db_result);
96+
delete_option('cleantalk_settings');
7497
}
7598

7699
public function testAutoSaveVars__data(){
@@ -84,6 +107,7 @@ public function testAutoSaveVars__data(){
84107
apbct_run_update_actions('6.1','6.2');
85108
$db_result = get_option('cleantalk_data')['js_key_lifetime'];
86109
$this->assertEquals(86400, $db_result);
110+
delete_option('cleantalk_data');
87111
}
88112

89113
public function testAutoSaveVars__network_settings(){
@@ -97,6 +121,7 @@ public function testAutoSaveVars__network_settings(){
97121
apbct_run_update_actions('6.1','6.2');
98122
$db_result = get_option('cleantalk_network_settings')['multisite__white_label__plugin_name'];
99123
$this->assertEquals('Anti-Spam by CleanTalk', $db_result);
124+
delete_option('network_settings');
100125
}
101126

102127
public function testAutoSaveVars__network_data(){
@@ -110,6 +135,7 @@ public function testAutoSaveVars__network_data(){
110135
apbct_run_update_actions('6.1','6.2');
111136
$db_result = get_option('cleantalk_network_data')['moderate'];
112137
$this->assertEquals(0, $db_result);
138+
delete_option('cleantalk_network_data');
113139
}
114140

115141
public function testAutoSaveVars__stats(){
@@ -161,6 +187,7 @@ public function testAutoSaveVars__stats(){
161187
$this->assertEquals(14400, $db_result);
162188
$db_result = get_option('cleantalk_stats')['sfw']['sending_logs__timestamp'];
163189
$this->assertEquals(10000, $db_result);
190+
delete_option('cleantalk_stats');
164191

165192
}
166193

@@ -175,6 +202,7 @@ public function testAutoSaveVars__fw_stats(){
175202
apbct_run_update_actions('6.1','6.2');
176203
$db_result = get_option('cleantalk_fw_stats')['firewall_updating_id'];
177204
$this->assertEquals(null, $db_result);
205+
delete_option('cleantalk_fw_stats');
178206
}
179207

180208
public function testAutoSaveVars__fw_stats_await_exception_without_var_updater(){
@@ -191,5 +219,33 @@ public function testAutoSaveVars__fw_stats_await_exception_without_var_updater()
191219
//await udefined index
192220
$this->expectException(Notice::class);
193221
$db_result = get_option('cleantalk_fw_stats')['firewall_updating_id'];
222+
delete_option('cleantalk_fw_stats');
223+
}
224+
225+
public function testInit()
226+
{
227+
$apbct = new class ('cleantalk', array('settings', 'errors')) extends State {
228+
protected function isMainSite()
229+
{
230+
return false;
231+
}
232+
protected function getWpmsMode()
233+
{
234+
return 2;
235+
}
236+
protected function switchToMainBlog(){}
237+
protected function switchToCurrentBlog(){}
238+
239+
};
240+
241+
$apbct->errorAdd('api', 'error');
242+
243+
$errors_from_state = (array) $apbct->errors;
244+
245+
$this->assertArrayHasKey('api', $errors_from_state);
246+
$this->assertArrayHasKey('error', $errors_from_state['api'][0]);
247+
$this->assertArrayHasKey('error_time', $errors_from_state['api'][0]);
248+
249+
delete_option('cleantalk_errors');
194250
}
195251
}

0 commit comments

Comments
 (0)