|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\Models; |
| 15 | + |
| 16 | +use CodeIgniter\Database\Exceptions\DataException; |
| 17 | +use PHPUnit\Framework\Attributes\Group; |
| 18 | +use Tests\Support\Entity\User; |
| 19 | +use Tests\Support\Models\UserModel; |
| 20 | +use Tests\Support\Models\ValidModel; |
| 21 | + |
| 22 | +/** |
| 23 | + * @internal |
| 24 | + */ |
| 25 | +#[Group('DatabaseLive')] |
| 26 | +final class InsertOnlyFieldsModelTest extends LiveModelTestCase |
| 27 | +{ |
| 28 | + public function testInsertAllowsInsertOnlyFields(): void |
| 29 | + { |
| 30 | + $this->createModel(UserModel::class)->setInsertOnlyFields(['email'])->insert([ |
| 31 | + 'name' => 'Insert Only', |
| 32 | + 'email' => 'insert-only@example.com', |
| 33 | + 'country' => 'US', |
| 34 | + ]); |
| 35 | + |
| 36 | + $this->seeInDatabase('user', [ |
| 37 | + 'email' => 'insert-only@example.com', |
| 38 | + ]); |
| 39 | + } |
| 40 | + |
| 41 | + public function testInsertBatchAllowsInsertOnlyFields(): void |
| 42 | + { |
| 43 | + $result = $this->createModel(UserModel::class)->setInsertOnlyFields(['email'])->insertBatch([ |
| 44 | + [ |
| 45 | + 'name' => 'Insert Only Batch', |
| 46 | + 'email' => 'insert-only-batch@example.com', |
| 47 | + 'country' => 'US', |
| 48 | + ], |
| 49 | + ]); |
| 50 | + |
| 51 | + $this->assertSame(1, $result); |
| 52 | + $this->seeInDatabase('user', [ |
| 53 | + 'email' => 'insert-only-batch@example.com', |
| 54 | + ]); |
| 55 | + } |
| 56 | + |
| 57 | + public function testUpdateThrowsOnInsertOnlyFields(): void |
| 58 | + { |
| 59 | + $this->expectException(DataException::class); |
| 60 | + $this->expectExceptionMessage('Fields cannot be updated for model "Tests\Support\Models\UserModel": email'); |
| 61 | + |
| 62 | + $this->createModel(UserModel::class)->setInsertOnlyFields(['email'])->update(1, [ |
| 63 | + 'name' => 'Insert Only Update', |
| 64 | + 'email' => 'insert-only-update@example.com', |
| 65 | + ]); |
| 66 | + } |
| 67 | + |
| 68 | + public function testSaveUpdateThrowsOnInsertOnlyFields(): void |
| 69 | + { |
| 70 | + $this->expectException(DataException::class); |
| 71 | + $this->expectExceptionMessage('Fields cannot be updated for model "Tests\Support\Models\UserModel": email'); |
| 72 | + |
| 73 | + $this->createModel(UserModel::class)->setInsertOnlyFields(['email'])->save([ |
| 74 | + 'id' => 1, |
| 75 | + 'name' => 'Insert Only Save', |
| 76 | + 'email' => 'insert-only-save@example.com', |
| 77 | + ]); |
| 78 | + } |
| 79 | + |
| 80 | + public function testSetUpdateThrowsOnInsertOnlyFields(): void |
| 81 | + { |
| 82 | + $this->expectException(DataException::class); |
| 83 | + $this->expectExceptionMessage('Fields cannot be updated for model "Tests\Support\Models\UserModel": email'); |
| 84 | + |
| 85 | + $this->createModel(UserModel::class)->setInsertOnlyFields(['email']) |
| 86 | + ->where('id', 1) |
| 87 | + ->set('email', 'insert-only-set@example.com') |
| 88 | + ->update(null, ['name' => 'Insert Only Set']); |
| 89 | + } |
| 90 | + |
| 91 | + public function testUpdateBatchThrowsOnInsertOnlyFields(): void |
| 92 | + { |
| 93 | + $this->expectException(DataException::class); |
| 94 | + $this->expectExceptionMessage('Fields cannot be updated for model "Tests\Support\Models\UserModel": email'); |
| 95 | + |
| 96 | + $this->createModel(UserModel::class)->setInsertOnlyFields(['email'])->updateBatch([ |
| 97 | + [ |
| 98 | + 'id' => 1, |
| 99 | + 'name' => 'Insert Only Batch', |
| 100 | + 'email' => 'insert-only-update-batch@example.com', |
| 101 | + ], |
| 102 | + ], 'id'); |
| 103 | + } |
| 104 | + |
| 105 | + public function testUpdateBatchAllowsInsertOnlyFieldAsIndex(): void |
| 106 | + { |
| 107 | + $result = $this->createModel(UserModel::class)->setInsertOnlyFields(['email'])->updateBatch([ |
| 108 | + [ |
| 109 | + 'email' => 'derek@world.com', |
| 110 | + 'name' => 'Insert Only Batch Index', |
| 111 | + ], |
| 112 | + ], 'email'); |
| 113 | + |
| 114 | + $this->assertSame(1, $result); |
| 115 | + $this->seeInDatabase('user', [ |
| 116 | + 'email' => 'derek@world.com', |
| 117 | + 'name' => 'Insert Only Batch Index', |
| 118 | + ]); |
| 119 | + } |
| 120 | + |
| 121 | + public function testEntityUpdateThrowsOnChangedInsertOnlyFields(): void |
| 122 | + { |
| 123 | + $model = new class ($this->db) extends UserModel { |
| 124 | + protected $returnType = User::class; |
| 125 | + protected $insertOnlyFields = ['email']; |
| 126 | + }; |
| 127 | + |
| 128 | + $user = $model->find(1); |
| 129 | + $this->assertInstanceOf(User::class, $user); |
| 130 | + |
| 131 | + $user->email = 'insert-only-entity@example.com'; |
| 132 | + |
| 133 | + $this->expectException(DataException::class); |
| 134 | + $this->expectExceptionMessage('Fields cannot be updated for model "' . $model::class . '": email'); |
| 135 | + |
| 136 | + $model->update($user->id, $user); |
| 137 | + } |
| 138 | + |
| 139 | + public function testEntityUpdateAllowsUnchangedInsertOnlyFields(): void |
| 140 | + { |
| 141 | + $model = new class ($this->db) extends UserModel { |
| 142 | + protected $returnType = User::class; |
| 143 | + protected $insertOnlyFields = ['email']; |
| 144 | + }; |
| 145 | + |
| 146 | + $user = $model->find(1); |
| 147 | + $this->assertInstanceOf(User::class, $user); |
| 148 | + |
| 149 | + $user->name = 'Insert Only Entity'; |
| 150 | + |
| 151 | + $this->assertTrue($model->update($user->id, $user)); |
| 152 | + $this->seeInDatabase('user', [ |
| 153 | + 'id' => 1, |
| 154 | + 'name' => 'Insert Only Entity', |
| 155 | + ]); |
| 156 | + } |
| 157 | + |
| 158 | + public function testProtectFalseBypassesInsertOnlyFields(): void |
| 159 | + { |
| 160 | + $result = $this->createModel(UserModel::class)->setInsertOnlyFields(['email'])->protect(false)->update(1, [ |
| 161 | + 'email' => 'insert-only-disabled@example.com', |
| 162 | + ]); |
| 163 | + |
| 164 | + $this->assertTrue($result); |
| 165 | + $this->seeInDatabase('user', [ |
| 166 | + 'id' => 1, |
| 167 | + 'email' => 'insert-only-disabled@example.com', |
| 168 | + ]); |
| 169 | + } |
| 170 | + |
| 171 | + public function testValidationRunsBeforeInsertOnlyFields(): void |
| 172 | + { |
| 173 | + $model = $this->createModel(ValidModel::class)->setInsertOnlyFields(['description']); |
| 174 | + $this->setPrivateProperty($model, 'cleanValidationRules', false); |
| 175 | + |
| 176 | + $this->assertFalse($model->update(1, [ |
| 177 | + 'description' => 'Insert only description', |
| 178 | + ])); |
| 179 | + $this->assertArrayHasKey('name', $model->errors()); |
| 180 | + } |
| 181 | +} |
0 commit comments