diff --git a/customfield/classes/category.php b/customfield/classes/category.php index 0a7dff99851..4f37247cb9e 100644 --- a/customfield/classes/category.php +++ b/customfield/classes/category.php @@ -14,24 +14,14 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Customfield category persistent class - * - * @package core_customfield - * @copyright 2018 Toni Barbera - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - namespace core_customfield; use core\persistent; -defined('MOODLE_INTERNAL') || die; - /** - * Class category + * Customfield category persistent class * - * @package core_customfield + * @package core_customfield * @copyright 2018 Toni Barbera * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -60,7 +50,8 @@ class category extends persistent { 'descriptionformat' => [ 'type' => PARAM_INT, 'default' => FORMAT_MOODLE, - 'optional' => true + 'optional' => true, + 'null' => NULL_ALLOWED, ], 'component' => [ 'type' => PARAM_COMPONENT @@ -80,7 +71,8 @@ class category extends persistent { 'sortorder' => [ 'type' => PARAM_INT, 'optional' => true, - 'default' => -1 + 'default' => -1, + 'null' => NULL_ALLOWED, ], ); } diff --git a/customfield/classes/field.php b/customfield/classes/field.php index 7ba3ffced34..d763ee8b869 100644 --- a/customfield/classes/field.php +++ b/customfield/classes/field.php @@ -14,24 +14,14 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Field persistent class - * - * @package core_customfield - * @copyright 2018 Toni Barbera - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - namespace core_customfield; use core\persistent; -defined('MOODLE_INTERNAL') || die; - /** - * Class field + * Customfield field persistent class * - * @package core_customfield + * @package core_customfield * @copyright 2018 Toni Barbera * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -67,12 +57,14 @@ class field extends persistent { 'descriptionformat' => [ 'type' => PARAM_INT, 'default' => FORMAT_MOODLE, - 'optional' => true + 'optional' => true, + 'null' => NULL_ALLOWED, ], 'sortorder' => [ 'type' => PARAM_INT, 'optional' => true, 'default' => -1, + 'null' => NULL_ALLOWED, ], 'categoryid' => [ 'type' => PARAM_INT diff --git a/customfield/tests/category_controller_test.php b/customfield/tests/category_controller_test.php index 7da1bdf817d..13397e17a06 100644 --- a/customfield/tests/category_controller_test.php +++ b/customfield/tests/category_controller_test.php @@ -61,6 +61,29 @@ final class category_controller_test extends \advanced_testcase { $this->assertTrue($c instanceof category_controller); } + /** + * Test creation of category instance from pre-defined object + */ + public function test_constructor_from_record(): void { + $this->resetAfterTest(); + + // Create field object that matches the persistent/schema definition. + $category = category_controller::create(0, (object) [ + 'name' => 'Test', + 'description' => null, + 'descriptionformat' => null, + 'component' => 'core_course', + 'area' => 'course', + 'itemid' => 0, + 'sortorder' => null, + ]); + + // Saving the category will validate the persistent internally. + $category->save(); + + $this->assertInstanceOf(category_controller::class, $category); + } + /** * Test for function \core_customfield\field_controller::create() in case of wrong parameters */ diff --git a/customfield/tests/field_controller_test.php b/customfield/tests/field_controller_test.php index f95253bad66..ee51af79d42 100644 --- a/customfield/tests/field_controller_test.php +++ b/customfield/tests/field_controller_test.php @@ -93,6 +93,30 @@ final class field_controller_test extends \advanced_testcase { ); } + /** + * Test creation of field instance from pre-defined object + */ + public function test_constructor_from_record(): void { + $this->resetAfterTest(); + + // Create field object that matches the persistent/schema definition. + $category = $this->get_generator()->create_category(); + $field = field_controller::create(0, (object) [ + 'name' => 'Test', + 'shortname' => 'test', + 'type' => 'text', + 'description' => null, + 'descriptionformat' => null, + 'sortorder' => null, + 'configdata' => null, + ], $category); + + // Saving the field will validate the persistent internally. + $field->save(); + + $this->assertInstanceOf(\customfield_text\field_controller::class, $field); + } + /** * Test for function \core_customfield\field_controller::create() in case of wrong parameters */