Skip to content

Commit 340b345

Browse files
committed
Add tests for Model::setAllowedFields
1 parent 2a4818d commit 340b345

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

system/Model.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,9 +1578,10 @@ public function errors(bool $forceDB = false)
15781578

15791579
return $error['message'] ?? null;
15801580
}
1581-
1581+
1582+
//--------------------------------------------------------------------
1583+
15821584
/**
1583-
* Allows to set allowed fields.
15841585
* It could be used when you have to change default or override current allowed fields.
15851586
*
15861587
* @param array $allowedFields
@@ -1589,7 +1590,7 @@ public function errors(bool $forceDB = false)
15891590
*/
15901591
public function setAllowedFields(array $allowedFields)
15911592
{
1592-
$this->$allowedFields = $allowedFields;
1593+
$this->allowedFields = $allowedFields;
15931594
}
15941595

15951596
//--------------------------------------------------------------------

tests/system/Database/Live/ModelTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php namespace CodeIgniter\Database\Live;
1+
<?php
2+
3+
namespace CodeIgniter\Database\Live;
24

35
use BadMethodCallException;
46
use CodeIgniter\Config\Config;
@@ -2579,4 +2581,27 @@ public function testcountAllResultsFalseWithDeletedFalseUseSoftDeletesFalse()
25792581
$this->assertFalse($this->getPrivateProperty($model, 'tempUseSoftDeletes'));
25802582
}
25812583

2584+
public function testSetAllowedFields()
2585+
{
2586+
$allowed1 = [
2587+
'id',
2588+
'created_at',
2589+
];
2590+
$allowed2 = [
2591+
'id',
2592+
'updated_at',
2593+
];
2594+
2595+
$model = new class extends Model {
2596+
protected $allowedFields = [
2597+
'id',
2598+
'created_at',
2599+
];
2600+
};
2601+
2602+
$this->assertSame($allowed1, $this->getPrivateProperty($model, 'allowedFields'));
2603+
2604+
$model->setAllowedFields($allowed2);
2605+
$this->assertSame($allowed2, $this->getPrivateProperty($model, 'allowedFields'));
2606+
}
25822607
}

0 commit comments

Comments
 (0)