feat: add connection diagnostics
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for admin REST connection diagnostics.
|
||||
*
|
||||
* @package WPContentSync
|
||||
*/
|
||||
|
||||
namespace WPContentSync\Tests\Unit\Admin;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use WPContentSync\Admin\ConnectionTestController;
|
||||
use WPContentSync\Logging\OptionLogger;
|
||||
use WPContentSync\Settings\SettingsRepository;
|
||||
use WPContentSync\Transport\RestTransportClient;
|
||||
|
||||
class ConnectionTestControllerTest 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'],
|
||||
$GLOBALS['wpcs_http_response'],
|
||||
$GLOBALS['wpcs_last_http_request']
|
||||
);
|
||||
|
||||
$_POST = array();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_it_tests_a_configured_pair_with_nonce_and_capability(): void {
|
||||
$this->storePair();
|
||||
$GLOBALS['wpcs_current_user_can']['manage_options'] = true;
|
||||
$GLOBALS['wpcs_nonce_valid']['wpcs_test_connection']['wpcs_connection_nonce'] = true;
|
||||
$_POST['pair_index'] = '0';
|
||||
|
||||
$this->controller()->handleTest();
|
||||
|
||||
self::assertStringContainsString( 'wpcs_connection_ok=1', $GLOBALS['wpcs_redirect_location'] );
|
||||
self::assertSame( 'GET', $GLOBALS['wpcs_last_http_request']['method'] );
|
||||
self::assertSame( 'https://destination.test/wp-json/wp-content-sync/v1/status', $GLOBALS['wpcs_last_http_request']['url'] );
|
||||
}
|
||||
|
||||
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 test WP Content Sync connections.' );
|
||||
|
||||
$this->controller()->handleTest();
|
||||
}
|
||||
|
||||
public function test_it_rejects_invalid_nonces(): void {
|
||||
$GLOBALS['wpcs_nonce_valid']['wpcs_test_connection']['wpcs_connection_nonce'] = false;
|
||||
|
||||
$this->expectException( \RuntimeException::class );
|
||||
$this->expectExceptionMessage( 'The connection test request could not be verified.' );
|
||||
|
||||
$this->controller()->handleTest();
|
||||
}
|
||||
|
||||
public function test_it_redirects_failures_without_leaking_application_passwords(): void {
|
||||
$this->storePair();
|
||||
$GLOBALS['wpcs_current_user_can']['manage_options'] = true;
|
||||
$GLOBALS['wpcs_nonce_valid']['wpcs_test_connection']['wpcs_connection_nonce'] = true;
|
||||
$GLOBALS['wpcs_http_response'] = array(
|
||||
'response' => array( 'code' => 401 ),
|
||||
'body' => '{"message":"Unauthorized"}',
|
||||
);
|
||||
$_POST['pair_index'] = '0';
|
||||
|
||||
$this->controller()->handleTest();
|
||||
|
||||
self::assertStringContainsString( 'wpcs_connection_error=', $GLOBALS['wpcs_redirect_location'] );
|
||||
self::assertStringNotContainsString( 'app-pass', $GLOBALS['wpcs_redirect_location'] );
|
||||
self::assertStringNotContainsString( 'app-pass', wp_json_encode( $GLOBALS['wpcs_test_options'][ OptionLogger::OPTION_NAME ] ) );
|
||||
}
|
||||
|
||||
private function controller(): ConnectionTestController {
|
||||
return new ConnectionTestController(
|
||||
new SettingsRepository(),
|
||||
new RestTransportClient(),
|
||||
new OptionLogger()
|
||||
);
|
||||
}
|
||||
|
||||
private function storePair(): void {
|
||||
update_option(
|
||||
SettingsRepository::OPTION_NAME,
|
||||
array(
|
||||
'sync_pairs' => array(
|
||||
array(
|
||||
'name' => 'Staging',
|
||||
'source_url' => 'https://source.test',
|
||||
'destination_url' => 'https://destination.test',
|
||||
'username' => 'codex',
|
||||
'application_password' => 'app-pass',
|
||||
),
|
||||
),
|
||||
),
|
||||
false
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -55,6 +55,27 @@ class DashboardTemplateTest extends TestCase {
|
||||
self::assertStringContainsString( 'name="wpcs_settings[sync_pairs][0][url_mappings][0][source]"', $output );
|
||||
}
|
||||
|
||||
public function test_it_renders_connection_diagnostics_for_each_pair(): void {
|
||||
$settings = Settings::fromArray(
|
||||
array(
|
||||
'sync_pairs' => array(
|
||||
array(
|
||||
'name' => 'Staging',
|
||||
'destination_url' => 'https://staging.example.test',
|
||||
'username' => 'codex',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$output = $this->renderDashboard( $settings );
|
||||
|
||||
self::assertStringContainsString( 'name="action" value="wpcs_test_connection"', $output );
|
||||
self::assertStringContainsString( 'name="wpcs_connection_nonce"', $output );
|
||||
self::assertStringContainsString( 'name="pair_index" value="0"', $output );
|
||||
self::assertStringContainsString( 'Test REST Connection', $output );
|
||||
}
|
||||
|
||||
private function renderDashboard( ?Settings $settings = null ): string {
|
||||
$settings = $settings ?? Settings::fromArray( array() );
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace WPContentSync\Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use WPContentSync\Admin\FileImportController;
|
||||
use WPContentSync\Admin\ConnectionTestController;
|
||||
use WPContentSync\Admin\SettingsController;
|
||||
use WPContentSync\Content\ContentHandlerRegistry;
|
||||
use WPContentSync\Content\ContentRecordNormalizer;
|
||||
@@ -61,6 +62,15 @@ class PluginTest extends TestCase {
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_registers_connection_test_controller(): void {
|
||||
$container = $this->getPluginContainer( Plugin::create() );
|
||||
|
||||
self::assertInstanceOf(
|
||||
ConnectionTestController::class,
|
||||
$container->get( ConnectionTestController::class )
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_registers_rest_transport_services(): void {
|
||||
$container = $this->getPluginContainer( Plugin::create() );
|
||||
|
||||
@@ -104,6 +114,15 @@ class PluginTest extends TestCase {
|
||||
self::assertArrayHasKey( 'admin_post_wpcs_save_settings', $GLOBALS['wpcs_test_actions'] );
|
||||
}
|
||||
|
||||
public function test_it_hooks_connection_test_controller_on_register(): void {
|
||||
unset( $GLOBALS['wpcs_test_actions'] );
|
||||
|
||||
$plugin = Plugin::create();
|
||||
$plugin->register();
|
||||
|
||||
self::assertArrayHasKey( 'admin_post_wpcs_test_connection', $GLOBALS['wpcs_test_actions'] );
|
||||
}
|
||||
|
||||
private function getPluginContainer( Plugin $plugin ): Container {
|
||||
$reflection = new \ReflectionClass( $plugin );
|
||||
$property = $reflection->getProperty( 'container' );
|
||||
|
||||
Reference in New Issue
Block a user