|
| 1 | +<?php |
| 2 | + |
| 3 | +use CodeIgniter\I18n\Time; |
| 4 | +use CodeIgniter\Test\DatabaseTestTrait; |
| 5 | +use Tests\Support\TestCase; |
| 6 | + |
| 7 | +/** |
| 8 | + * @internal |
| 9 | + */ |
| 10 | +final class HelperTest extends TestCase |
| 11 | +{ |
| 12 | + use DatabaseTestTrait; |
| 13 | + |
| 14 | +/* |
| 15 | + function preference(string $key, $value = null) |
| 16 | + { |
| 17 | + $setting = service('settings'); |
| 18 | +
|
| 19 | + // Check for an active user |
| 20 | + if (null === $userId = user_id()) { |
| 21 | + // Allow anonymous gets |
| 22 | + if (count(func_get_args()) === 1) { |
| 23 | + return $setting->get($key); |
| 24 | + } |
| 25 | +
|
| 26 | + throw new RuntimeException('You cannot set a preference without an active user.'); |
| 27 | + } |
| 28 | +
|
| 29 | + // Getting the contextual value |
| 30 | + $context = 'user:' . $userId; |
| 31 | + if (count(func_get_args()) === 1) { |
| 32 | + return $setting->get($key, $context); |
| 33 | + } |
| 34 | +
|
| 35 | + // Setting the contextual value |
| 36 | + return $setting->set($key, $value, $context); |
| 37 | + } |
| 38 | +} |
| 39 | +*/ |
| 40 | + |
| 41 | + public function testAnonymousGets() |
| 42 | + { |
| 43 | + $this->assertSame('Apple', preference('Food.fruit')); |
| 44 | + } |
| 45 | + |
| 46 | + public function testAnonymousThrowsOnSet() |
| 47 | + { |
| 48 | + $this->expectException('RuntimeException'); |
| 49 | + $this->expectExceptionMessage('You cannot set a preference without an active user.'); |
| 50 | + |
| 51 | + preference('Food.fruit', 'Banana'); |
| 52 | + } |
| 53 | + |
| 54 | + public function testSets() |
| 55 | + { |
| 56 | + service('auth')->login(1); |
| 57 | + |
| 58 | + preference('Food.fruit', 'Pineapple'); |
| 59 | + |
| 60 | + $this->seeInDatabase($this->table, [ |
| 61 | + 'class' => 'Tests\Support\Config\Food', |
| 62 | + 'key' => 'fruit', |
| 63 | + 'value' => 'Pineapple', |
| 64 | + 'type' => 'string', |
| 65 | + 'context' => 'user:1', |
| 66 | + ]); |
| 67 | + } |
| 68 | + |
| 69 | + public function testGets() |
| 70 | + { |
| 71 | + service('auth')->login(1); |
| 72 | + |
| 73 | + $this->hasInDatabase($this->table, [ |
| 74 | + 'class' => 'Tests\Support\Config\Food', |
| 75 | + 'key' => 'fruit', |
| 76 | + 'value' => 'Orange', |
| 77 | + 'created_at' => Time::now()->toDateTimeString(), |
| 78 | + 'updated_at' => Time::now()->toDateTimeString(), |
| 79 | + ]); |
| 80 | + |
| 81 | + $this->assertSame('Orange', preference('Food.fruit')); |
| 82 | + } |
| 83 | +/* |
| 84 | + public function testReturnsValueDotArray() |
| 85 | + { |
| 86 | + $this->hasInDatabase($this->table, [ |
| 87 | + 'class' => 'Foo', |
| 88 | + 'key' => 'bar', |
| 89 | + 'value' => 'baz', |
| 90 | + 'type' => 'string', |
| 91 | + 'created_at' => Time::now()->toDateTimeString(), |
| 92 | + 'updated_at' => Time::now()->toDateTimeString(), |
| 93 | + ]); |
| 94 | +
|
| 95 | + $this->assertSame('baz', setting('Foo.bar')); |
| 96 | + } |
| 97 | +
|
| 98 | + public function testSettingValueDotArray() |
| 99 | + { |
| 100 | + $this->hasInDatabase($this->table, [ |
| 101 | + 'class' => 'Foo', |
| 102 | + 'key' => 'bar', |
| 103 | + 'value' => 'baz', |
| 104 | + 'type' => 'string', |
| 105 | + 'created_at' => Time::now()->toDateTimeString(), |
| 106 | + 'updated_at' => Time::now()->toDateTimeString(), |
| 107 | + ]); |
| 108 | +
|
| 109 | + setting('Foo.bar', false); |
| 110 | +
|
| 111 | + $this->seeInDatabase($this->table, [ |
| 112 | + 'class' => 'Foo', |
| 113 | + 'key' => 'bar', |
| 114 | + 'value' => '0', |
| 115 | + 'type' => 'boolean', |
| 116 | + ]); |
| 117 | +
|
| 118 | + $this->assertFalse(setting('Foo.bar')); |
| 119 | + } |
| 120 | +*/ |
| 121 | +} |
0 commit comments