144 lines
4.3 KiB
PHP
144 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace WPContentSync\Tests\Unit;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use WPContentSync\Settings\Settings;
|
|
|
|
class SettingsTest extends TestCase {
|
|
public function test_it_provides_secure_defaults(): void {
|
|
$settings = Settings::fromArray( array() );
|
|
|
|
self::assertSame( array(), $settings->syncPairs() );
|
|
self::assertSame( 'warning', $settings->loggingLevel() );
|
|
self::assertTrue( $settings->automaticUrlReplacementEnabled() );
|
|
self::assertSame( 'last_write_wins', $settings->conflictStrategy() );
|
|
}
|
|
|
|
public function test_it_sanitizes_scalar_settings(): void {
|
|
$settings = Settings::fromArray(
|
|
array(
|
|
'logging_level' => '<b>debug</b>',
|
|
'conflict_strategy' => "manual_review\n",
|
|
'automatic_url_replacement' => false,
|
|
)
|
|
);
|
|
|
|
self::assertSame( 'debug', $settings->loggingLevel() );
|
|
self::assertSame( 'manual_review', $settings->conflictStrategy() );
|
|
self::assertFalse( $settings->automaticUrlReplacementEnabled() );
|
|
}
|
|
|
|
public function test_it_rejects_unknown_logging_level(): void {
|
|
$settings = Settings::fromArray(
|
|
array(
|
|
'logging_level' => 'verbose',
|
|
)
|
|
);
|
|
|
|
self::assertSame( 'warning', $settings->loggingLevel() );
|
|
}
|
|
|
|
public function test_it_normalizes_string_boolean_values(): void {
|
|
$settings = Settings::fromArray(
|
|
array(
|
|
'automatic_url_replacement' => 'false',
|
|
)
|
|
);
|
|
|
|
self::assertFalse( $settings->automaticUrlReplacementEnabled() );
|
|
|
|
$settings = Settings::fromArray(
|
|
array(
|
|
'automatic_url_replacement' => '1',
|
|
)
|
|
);
|
|
|
|
self::assertTrue( $settings->automaticUrlReplacementEnabled() );
|
|
}
|
|
|
|
public function test_it_sanitizes_full_admin_workflow_settings(): void {
|
|
$settings = Settings::fromArray(
|
|
array(
|
|
'sync_pairs' => array(
|
|
array(
|
|
'name' => '<b>Production to Staging</b>',
|
|
'source_url' => 'https://example.test/',
|
|
'destination_url' => 'https://staging.example.test/',
|
|
'username' => '<script>codex</script>',
|
|
'application_password' => 'secret app password',
|
|
'default_direction' => 'push',
|
|
'content_types' => array( 'posts', 'terms', 'media', 'bad_type' ),
|
|
'url_mappings' => array(
|
|
array(
|
|
'source' => 'https://example.test',
|
|
'destination' => 'https://staging.example.test',
|
|
),
|
|
),
|
|
),
|
|
),
|
|
'log_retention' => '50',
|
|
'debug_logging' => '1',
|
|
)
|
|
);
|
|
|
|
$pairs = $settings->syncPairs();
|
|
|
|
self::assertSame( 'Production to Staging', $pairs[0]['name'] );
|
|
self::assertSame( 'https://example.test/', $pairs[0]['source_url'] );
|
|
self::assertSame( 'https://staging.example.test/', $pairs[0]['destination_url'] );
|
|
self::assertSame( 'codex', $pairs[0]['username'] );
|
|
self::assertSame( 'secret app password', $pairs[0]['application_password'] );
|
|
self::assertSame( 'push', $pairs[0]['default_direction'] );
|
|
self::assertSame( array( 'posts', 'terms', 'media' ), $pairs[0]['content_types'] );
|
|
self::assertSame(
|
|
array(
|
|
array(
|
|
'source' => 'https://example.test',
|
|
'destination' => 'https://staging.example.test',
|
|
),
|
|
),
|
|
$pairs[0]['url_mappings']
|
|
);
|
|
self::assertSame( 50, $settings->logRetention() );
|
|
self::assertTrue( $settings->debugLoggingEnabled() );
|
|
}
|
|
|
|
public function test_it_serializes_to_array(): void {
|
|
$settings = Settings::fromArray(
|
|
array(
|
|
'sync_pairs' => array(
|
|
array(
|
|
'name' => 'Staging',
|
|
'source_url' => 'https://example.test',
|
|
'destination_url' => 'https://staging.example.test',
|
|
),
|
|
),
|
|
)
|
|
);
|
|
|
|
self::assertSame(
|
|
array(
|
|
'sync_pairs' => array(
|
|
array(
|
|
'name' => 'Staging',
|
|
'source_url' => 'https://example.test',
|
|
'destination_url' => 'https://staging.example.test',
|
|
'username' => '',
|
|
'application_password' => '',
|
|
'default_direction' => 'push',
|
|
'content_types' => array( 'posts', 'terms', 'media', 'custom_post_types' ),
|
|
'url_mappings' => array(),
|
|
),
|
|
),
|
|
'logging_level' => 'warning',
|
|
'automatic_url_replacement' => true,
|
|
'conflict_strategy' => 'last_write_wins',
|
|
'log_retention' => 200,
|
|
'debug_logging' => false,
|
|
),
|
|
$settings->toArray()
|
|
);
|
|
}
|
|
}
|