mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 05:58:34 +01:00
Merge branch 'MDL-57859-master' of git://github.com/merrill-oakland/moodle
This commit is contained in:
commit
2e57a90e0e
@ -561,6 +561,22 @@ abstract class persistent {
|
||||
protected function after_update($result) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the record to the database.
|
||||
*
|
||||
* If this record has an ID, then {@link self::update()} is called, otherwise {@link self::create()} is called.
|
||||
* Before and after hooks for create() or update() will be called appropriately.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
final public function save() {
|
||||
if ($this->raw_get('id') <= 0) {
|
||||
$this->create();
|
||||
} else {
|
||||
$this->update();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to execute before a delete.
|
||||
*
|
||||
|
@ -325,6 +325,37 @@ class core_persistent_testcase extends advanced_testcase {
|
||||
$this->assertTrue($p->is_valid()); // Should always be valid after an update.
|
||||
}
|
||||
|
||||
public function test_save() {
|
||||
global $DB;
|
||||
$p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
|
||||
$this->assertFalse(isset($p->beforecreate));
|
||||
$this->assertFalse(isset($p->aftercreate));
|
||||
$this->assertFalse(isset($p->beforeupdate));
|
||||
$this->assertFalse(isset($p->beforeupdate));
|
||||
$p->save();
|
||||
$record = $DB->get_record(core_testable_persistent::TABLE, array('id' => $p->get('id')), '*', MUST_EXIST);
|
||||
$expected = $p->to_record();
|
||||
$this->assertTrue(isset($p->beforecreate));
|
||||
$this->assertTrue(isset($p->aftercreate));
|
||||
$this->assertFalse(isset($p->beforeupdate));
|
||||
$this->assertFalse(isset($p->beforeupdate));
|
||||
$this->assertEquals($expected->sortorder, $record->sortorder);
|
||||
$this->assertEquals($expected->idnumber, $record->idnumber);
|
||||
$this->assertEquals($expected->id, $record->id);
|
||||
$this->assertTrue($p->is_valid()); // Should always be valid after a save/create.
|
||||
|
||||
$p->set('idnumber', 'abcd');
|
||||
$p->save();
|
||||
$record = $DB->get_record(core_testable_persistent::TABLE, array('id' => $p->get('id')), '*', MUST_EXIST);
|
||||
$expected = $p->to_record();
|
||||
$this->assertTrue(isset($p->beforeupdate));
|
||||
$this->assertTrue(isset($p->beforeupdate));
|
||||
$this->assertEquals($expected->sortorder, $record->sortorder);
|
||||
$this->assertEquals($expected->idnumber, $record->idnumber);
|
||||
$this->assertEquals($expected->id, $record->id);
|
||||
$this->assertTrue($p->is_valid()); // Should always be valid after a save/update.
|
||||
}
|
||||
|
||||
public function test_read() {
|
||||
$p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc'));
|
||||
$p->create();
|
||||
|
Loading…
x
Reference in New Issue
Block a user