feat: save admin sync settings
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for admin settings saves.
|
||||
*
|
||||
* @package WPContentSync
|
||||
*/
|
||||
|
||||
namespace WPContentSync\Tests\Unit\Admin;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use WPContentSync\Admin\SettingsController;
|
||||
use WPContentSync\Settings\SettingsRepository;
|
||||
|
||||
class SettingsControllerTest extends TestCase {
|
||||
protected function tearDown(): void {
|
||||
unset(
|
||||
$GLOBALS['wpcs_current_user_can'],
|
||||
$GLOBALS['wpcs_nonce_valid'],
|
||||
$GLOBALS['wpcs_redirect_location'],
|
||||
$GLOBALS['wpcs_test_options'],
|
||||
$GLOBALS['wpcs_test_option_autoloads']
|
||||
);
|
||||
|
||||
$_POST = array();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_it_saves_settings_with_nonce_and_capability(): void {
|
||||
$GLOBALS['wpcs_current_user_can']['manage_options'] = true;
|
||||
|
||||
$GLOBALS['wpcs_nonce_valid']['wpcs_save_settings']['wpcs_settings_nonce'] = true;
|
||||
|
||||
$_POST['wpcs_settings'] = array(
|
||||
'logging_level' => 'debug',
|
||||
'conflict_strategy' => 'manual_review',
|
||||
'sync_pairs' => array(
|
||||
array(
|
||||
'name' => 'Staging',
|
||||
'source_url' => 'https://example.test',
|
||||
'destination_url' => 'https://staging.example.test',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
( new SettingsController( new SettingsRepository() ) )->handleSave();
|
||||
|
||||
self::assertSame( 'debug', $GLOBALS['wpcs_test_options'][ SettingsRepository::OPTION_NAME ]['logging_level'] );
|
||||
self::assertSame( 'manual_review', $GLOBALS['wpcs_test_options'][ SettingsRepository::OPTION_NAME ]['conflict_strategy'] );
|
||||
self::assertStringContainsString( 'wpcs_settings_saved=1', $GLOBALS['wpcs_redirect_location'] );
|
||||
}
|
||||
|
||||
public function test_it_rejects_users_without_manage_options(): void {
|
||||
$GLOBALS['wpcs_current_user_can']['manage_options'] = false;
|
||||
|
||||
$this->expectException( \RuntimeException::class );
|
||||
$this->expectExceptionMessage( 'You do not have permission to save WP Content Sync settings.' );
|
||||
|
||||
( new SettingsController( new SettingsRepository() ) )->handleSave();
|
||||
}
|
||||
|
||||
public function test_it_rejects_invalid_nonces(): void {
|
||||
$GLOBALS['wpcs_nonce_valid']['wpcs_save_settings']['wpcs_settings_nonce'] = false;
|
||||
|
||||
$this->expectException( \RuntimeException::class );
|
||||
$this->expectExceptionMessage( 'The settings save request could not be verified.' );
|
||||
|
||||
( new SettingsController( new SettingsRepository() ) )->handleSave();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user