You've already forked Atomcms-edit
256 lines
8.5 KiB
PHP
Executable File
256 lines
8.5 KiB
PHP
Executable File
<?php
|
|
|
|
use App\Models\Miscellaneous\WebsiteSetting;
|
|
use App\Services\SettingsService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
uses(TestCase::class, RefreshDatabase::class);
|
|
|
|
test('settings service returns default value when setting not found', function () {
|
|
$settingsService = app(SettingsService::class);
|
|
|
|
$value = $settingsService->getOrDefault('nonexistent_key', 'default_value');
|
|
|
|
// Value checks
|
|
expect($value)->toBe('default_value');
|
|
expect($value)->toBeString();
|
|
expect(strlen($value))->toBeGreaterThan(0);
|
|
expect(strlen($value))->toBe(13);
|
|
|
|
// Type checks
|
|
expect($value)->not->toBeNull();
|
|
expect($value)->not->toBeInt();
|
|
expect($value)->not->toBeBool();
|
|
expect($value)->not->toBeArray();
|
|
|
|
// Content checks
|
|
expect($value)->toContain('default');
|
|
expect($value)->toContain('_');
|
|
expect($value)->toStartWith('default');
|
|
expect($value)->toEndWith('value');
|
|
|
|
// Verify setting does not exist in database
|
|
$this->assertDatabaseMissing('website_settings', [
|
|
'key' => 'nonexistent_key',
|
|
]);
|
|
|
|
// Count check
|
|
expect(WebsiteSetting::where('key', 'nonexistent_key')->count())->toBe(0);
|
|
});
|
|
|
|
test('settings service returns existing setting value', function () {
|
|
installHotel();
|
|
|
|
// Clear cache and re-instantiate service to get fresh data
|
|
SettingsService::clearCache();
|
|
$settingsService = new SettingsService;
|
|
$value = $settingsService->getOrDefault('disable_registration', 'default');
|
|
|
|
// Value checks
|
|
expect($value)->toBe('0');
|
|
expect($value)->not->toBe('default');
|
|
expect($value)->toBeString();
|
|
expect(strlen($value))->toBe(1);
|
|
|
|
// Type checks
|
|
expect($value)->not->toBeNull();
|
|
expect($value)->not->toBeInt();
|
|
expect($value)->not->toBeBool();
|
|
|
|
// Content checks
|
|
expect($value)->toBeNumeric();
|
|
expect($value)->toBe('0');
|
|
|
|
// Verify setting exists in database
|
|
$this->assertDatabaseHas('website_settings', [
|
|
'key' => 'disable_registration',
|
|
'value' => '0',
|
|
]);
|
|
|
|
// Retrieve setting and verify all fields
|
|
$setting = WebsiteSetting::where('key', 'disable_registration')->first();
|
|
expect($setting)->not->toBeNull();
|
|
expect($setting->key)->toBe('disable_registration');
|
|
expect($setting->value)->toBe('0');
|
|
expect($setting->id)->toBeInt();
|
|
expect($setting->id)->toBeGreaterThan(0);
|
|
});
|
|
|
|
test('settings service returns default for non-existent key', function () {
|
|
installHotel();
|
|
|
|
// Clear cache and re-instantiate service to get fresh data
|
|
SettingsService::clearCache();
|
|
$settingsService = new SettingsService;
|
|
$value = $settingsService->getOrDefault('non_existent_key', 'default');
|
|
|
|
// Value checks
|
|
expect($value)->toBe('default');
|
|
expect($value)->toBeString();
|
|
expect(strlen($value))->toBe(7);
|
|
|
|
// Type checks
|
|
expect($value)->not->toBeNull();
|
|
|
|
// Verify setting does not exist in database
|
|
$this->assertDatabaseMissing('website_settings', [
|
|
'key' => 'non_existent_key',
|
|
]);
|
|
|
|
// Count check
|
|
expect(WebsiteSetting::where('key', 'non_existent_key')->count())->toBe(0);
|
|
expect(WebsiteSetting::where('key', 'non_existent_key')->exists())->toBeFalse();
|
|
|
|
// Verify existing settings are not affected
|
|
expect(WebsiteSetting::count())->toBeGreaterThan(0);
|
|
});
|
|
|
|
test('install helper creates basic settings', function () {
|
|
// Check current state before installHotel
|
|
$themeExistedBefore = WebsiteSetting::where('key', 'theme')->exists();
|
|
$countBefore = WebsiteSetting::count();
|
|
|
|
installHotel();
|
|
|
|
// Verify settings exist after installHotel
|
|
expect(WebsiteSetting::where('key', 'theme')->exists())->toBeTrue();
|
|
expect(WebsiteSetting::where('key', 'max_accounts_per_ip')->exists())->toBeTrue();
|
|
expect(WebsiteSetting::where('key', 'disable_registration')->exists())->toBeTrue();
|
|
|
|
// Count check
|
|
$countAfter = WebsiteSetting::count();
|
|
expect($countAfter)->toBeGreaterThanOrEqual(9);
|
|
expect($countAfter)->toBeGreaterThan($countBefore);
|
|
|
|
// Verify all expected settings in database
|
|
$this->assertDatabaseHas('website_settings', ['key' => 'theme']);
|
|
$this->assertDatabaseHas('website_settings', ['key' => 'max_accounts_per_ip']);
|
|
$this->assertDatabaseHas('website_settings', ['key' => 'disable_registration']);
|
|
$this->assertDatabaseHas('website_settings', ['key' => 'requires_beta_code']);
|
|
$this->assertDatabaseHas('website_settings', ['key' => 'username_regex']);
|
|
$this->assertDatabaseHas('website_settings', ['key' => 'start_motto']);
|
|
$this->assertDatabaseHas('website_settings', ['key' => 'start_look']);
|
|
$this->assertDatabaseHas('website_settings', ['key' => 'start_credits']);
|
|
$this->assertDatabaseHas('website_settings', ['key' => 'hotel_home_room']);
|
|
$this->assertDatabaseHas('website_settings', ['key' => 'enable_discord_webhook']);
|
|
|
|
// Verify specific setting values
|
|
$themeSetting = WebsiteSetting::where('key', 'theme')->first();
|
|
expect($themeSetting->value)->toBe('atom');
|
|
expect($themeSetting->comment)->toBe('Theme');
|
|
|
|
$creditsSetting = WebsiteSetting::where('key', 'start_credits')->first();
|
|
expect($creditsSetting->value)->toBe('1000');
|
|
|
|
// Verify setting count
|
|
$settingsCount = WebsiteSetting::count();
|
|
expect($settingsCount)->toBeGreaterThanOrEqual(9);
|
|
expect($settingsCount)->toBeLessThan(100);
|
|
});
|
|
|
|
test('settings service cache works correctly', function () {
|
|
installHotel();
|
|
|
|
// Clear cache
|
|
SettingsService::clearCache();
|
|
|
|
// Create service instance and get value
|
|
$settingsService1 = new SettingsService;
|
|
$value1 = $settingsService1->getOrDefault('theme', 'default');
|
|
expect($value1)->toBe('atom');
|
|
|
|
// Create another service instance and get same value (should use cache)
|
|
$settingsService2 = new SettingsService;
|
|
$value2 = $settingsService2->getOrDefault('theme', 'default');
|
|
expect($value2)->toBe('atom');
|
|
|
|
// Both values should be identical
|
|
expect($value1)->toBe($value2);
|
|
|
|
// Verify value is string
|
|
expect($value1)->toBeString();
|
|
expect($value2)->toBeString();
|
|
});
|
|
|
|
test('settings can be updated and retrieved', function () {
|
|
installHotel();
|
|
|
|
// Clear cache
|
|
SettingsService::clearCache();
|
|
|
|
// Verify initial value
|
|
$settingsService = new SettingsService;
|
|
$initialValue = $settingsService->getOrDefault('test_setting', 'initial');
|
|
expect($initialValue)->toBe('initial');
|
|
|
|
// Create setting in database
|
|
WebsiteSetting::create([
|
|
'key' => 'test_setting',
|
|
'value' => 'updated_value',
|
|
'comment' => 'Test comment',
|
|
]);
|
|
|
|
// Clear cache to get fresh data
|
|
SettingsService::clearCache();
|
|
$settingsService2 = new SettingsService;
|
|
$updatedValue = $settingsService2->getOrDefault('test_setting', 'default');
|
|
|
|
// Verify updated value
|
|
expect($updatedValue)->toBe('updated_value');
|
|
expect($updatedValue)->not->toBe('initial');
|
|
expect($updatedValue)->toBeString();
|
|
expect(strlen($updatedValue))->toBe(13);
|
|
|
|
// Database verification
|
|
$this->assertDatabaseHas('website_settings', [
|
|
'key' => 'test_setting',
|
|
'value' => 'updated_value',
|
|
]);
|
|
|
|
// Retrieve and verify all fields
|
|
$setting = WebsiteSetting::where('key', 'test_setting')->first();
|
|
expect($setting->comment)->toBe('Test comment');
|
|
expect($setting->id)->toBeInt();
|
|
expect($setting->id)->toBeGreaterThan(0);
|
|
});
|
|
|
|
test('settings service handles empty string default', function () {
|
|
installHotel();
|
|
|
|
SettingsService::clearCache();
|
|
$settingsService = new SettingsService;
|
|
$value = $settingsService->getOrDefault('non_existent_key_2', '');
|
|
|
|
// Verify empty string is returned
|
|
expect($value)->toBe('');
|
|
expect($value)->toBeString();
|
|
expect(strlen($value))->toBe(0);
|
|
expect($value)->toBeEmpty();
|
|
|
|
// Verify setting does not exist
|
|
$this->assertDatabaseMissing('website_settings', [
|
|
'key' => 'non_existent_key_2',
|
|
]);
|
|
});
|
|
|
|
test('settings service handles numeric defaults', function () {
|
|
installHotel();
|
|
|
|
SettingsService::clearCache();
|
|
$settingsService = new SettingsService;
|
|
$value = $settingsService->getOrDefault('non_existent_key_3', '12345');
|
|
|
|
// Verify numeric string is returned
|
|
expect($value)->toBe('12345');
|
|
expect($value)->toBeString();
|
|
expect($value)->toBeNumeric();
|
|
expect(strlen($value))->toBe(5);
|
|
expect((int) $value)->toBe(12345);
|
|
|
|
// Verify setting does not exist
|
|
$this->assertDatabaseMissing('website_settings', [
|
|
'key' => 'non_existent_key_3',
|
|
]);
|
|
});
|