Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions lib/Cleantalk/ApbctWP/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -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();
}
}
56 changes: 56 additions & 0 deletions tests/ApbctWP/TestState.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

Expand All @@ -22,27 +24,46 @@ 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()
{
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()
{
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()
{
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
Expand All @@ -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(){
Expand All @@ -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(){
Expand All @@ -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(){
Expand All @@ -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(){
Expand All @@ -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(){
Expand Down Expand Up @@ -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');

}

Expand All @@ -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(){
Expand All @@ -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');
}
}
Loading