|
13 | 13 |
|
14 | 14 | namespace CodeIgniter\Models; |
15 | 15 |
|
| 16 | +use CodeIgniter\Database\Exceptions\DataException; |
16 | 17 | use PHPUnit\Framework\Attributes\Group; |
17 | 18 | use Tests\Support\Models\UserModel; |
18 | 19 |
|
@@ -47,4 +48,113 @@ public function testReplaceRespectsUseTimestamps(): void |
47 | 48 | $this->assertStringContainsString('created_at', (string) $sql); |
48 | 49 | $this->assertStringContainsString('updated_at', (string) $sql); |
49 | 50 | } |
| 51 | + |
| 52 | + public function testReplaceProtectsFields(): void |
| 53 | + { |
| 54 | + $this->createModel(UserModel::class); |
| 55 | + |
| 56 | + $data = [ |
| 57 | + 'name' => 'Amanda Holmes', |
| 58 | + 'email' => 'amanda@holmes.com', |
| 59 | + 'country' => 'US', |
| 60 | + 'not_allowed' => 'hack', |
| 61 | + ]; |
| 62 | + |
| 63 | + $sql = $this->model->replace($data, true); |
| 64 | + $this->assertStringNotContainsString('not_allowed', (string) $sql); |
| 65 | + $this->assertStringContainsString('email', (string) $sql); |
| 66 | + } |
| 67 | + |
| 68 | + public function testReplacePreservesPrimaryKeyEvenIfNotInAllowedFields(): void |
| 69 | + { |
| 70 | + $this->createModel(UserModel::class); |
| 71 | + |
| 72 | + $data = [ |
| 73 | + 'id' => 1, |
| 74 | + 'name' => 'Amanda Holmes', |
| 75 | + 'email' => 'amanda@holmes.com', |
| 76 | + 'country' => 'US', |
| 77 | + 'not_allowed' => 'hack', |
| 78 | + ]; |
| 79 | + |
| 80 | + $sql = $this->model->replace($data, true); |
| 81 | + $this->assertStringContainsString('id', (string) $sql); |
| 82 | + $this->assertStringNotContainsString('not_allowed', (string) $sql); |
| 83 | + } |
| 84 | + |
| 85 | + public function testReplaceWithProtectFalseAllowsAllFields(): void |
| 86 | + { |
| 87 | + $this->createModel(UserModel::class); |
| 88 | + |
| 89 | + $data = [ |
| 90 | + 'name' => 'Amanda Holmes', |
| 91 | + 'email' => 'amanda@holmes.com', |
| 92 | + 'country' => 'US', |
| 93 | + 'not_allowed' => 'hack', |
| 94 | + ]; |
| 95 | + |
| 96 | + $sql = $this->model->protect(false)->replace($data, true); |
| 97 | + $this->assertStringContainsString('not_allowed', (string) $sql); |
| 98 | + } |
| 99 | + |
| 100 | + public function testReplaceThrowsDataExceptionWhenNoAllowedFields(): void |
| 101 | + { |
| 102 | + $this->createModel(UserModel::class); |
| 103 | + $this->setPrivateProperty($this->model, 'allowedFields', []); |
| 104 | + |
| 105 | + $this->expectException(DataException::class); |
| 106 | + $this->expectExceptionMessage('Allowed fields must be specified for model: "Tests\Support\Models\UserModel"'); |
| 107 | + |
| 108 | + $this->model->replace(['id' => 1, 'name' => 'Amanda']); |
| 109 | + } |
| 110 | + |
| 111 | + public function testReplaceThrowsDataExceptionWhenDatasetEmpty(): void |
| 112 | + { |
| 113 | + $this->createModel(UserModel::class); |
| 114 | + |
| 115 | + $this->expectException(DataException::class); |
| 116 | + $this->expectExceptionMessage('There is no data to replace.'); |
| 117 | + |
| 118 | + $this->model->replace([]); |
| 119 | + } |
| 120 | + |
| 121 | + public function testReplaceThrowsDataExceptionWhenAllFieldsProtected(): void |
| 122 | + { |
| 123 | + $this->createModel(UserModel::class); |
| 124 | + |
| 125 | + $this->expectException(DataException::class); |
| 126 | + $this->expectExceptionMessage('There is no data to replace.'); |
| 127 | + |
| 128 | + $this->model->replace(['not_allowed' => 'hack']); |
| 129 | + } |
| 130 | + |
| 131 | + public function testReplaceLiveExecutionWithUnallowedFields(): void |
| 132 | + { |
| 133 | + $this->createModel(UserModel::class); |
| 134 | + |
| 135 | + $data = [ |
| 136 | + 'name' => 'Amanda Holmes', |
| 137 | + 'email' => 'amanda@holmes.com', |
| 138 | + 'country' => 'US', |
| 139 | + ]; |
| 140 | + |
| 141 | + $id = $this->model->insert($data); |
| 142 | + |
| 143 | + $replaceData = [ |
| 144 | + 'id' => $id, |
| 145 | + 'name' => 'Amanda Holmes', |
| 146 | + 'email' => 'amanda_updated@holmes.com', |
| 147 | + 'country' => 'UK', |
| 148 | + 'not_allowed' => 'ignored', |
| 149 | + ]; |
| 150 | + |
| 151 | + $result = $this->model->replace($replaceData); |
| 152 | + $this->assertNotFalse($result); |
| 153 | + |
| 154 | + $this->seeInDatabase('user', [ |
| 155 | + 'id' => $id, |
| 156 | + 'email' => 'amanda_updated@holmes.com', |
| 157 | + 'country' => 'UK', |
| 158 | + ]); |
| 159 | + } |
50 | 160 | } |
0 commit comments