Skip to content
Permalink
Browse files

Addin test to show how to change the options array in beforeSave

  • Loading branch information...
lorenzo committed Oct 22, 2013
1 parent 7b398bf commit 9a9fa54f14a2eb88110dfcdc4ce40716f5d438e6
Showing with 31 additions and 1 deletion.
  1. +31 −1 Cake/Test/TestCase/ORM/TableTest.php
@@ -1008,7 +1008,7 @@ public function testSaveWithFieldList() {
*
* @return void
*/
public function testbeforeSaveModifyData() {
public function testBeforeSaveModifyData() {
$table = Table::build('users');
$data = new \Cake\ORM\Entity([
'username' => 'superuser',
@@ -1026,4 +1026,34 @@ public function testbeforeSaveModifyData() {
$this->assertEquals('foo', $row->get('password'));
}
/**
* Tests that it is possible to modify the options array in beforeSave
*
* @return void
*/
public function testBeforeSaveModifyOptions() {
$table = Table::build('users');
$data = new \Cake\ORM\Entity([
'username' => 'superuser',
'password' => 'foo',
'created' => new \DateTime('2013-10-10 00:00'),
'updated' => new \DateTime('2013-10-10 00:00')
]);
$listener1 = function($e, $entity, $options) {
$options['fieldList'][] = 'created';
};
$listener2 = function($e, $entity, $options) {
$options['fieldList'][] = 'updated';
};
$table->getEventManager()->attach($listener1, 'Model.beforeSave');
$table->getEventManager()->attach($listener2, 'Model.beforeSave');
$this->assertSame($data, $table->save($data));
$this->assertEquals($data->id, 5);
$row = $table->find('all')->where(['id' => 5])->first();
$data->set('username', null);
$data->set('password', null);
$this->assertEquals($data->toArray(), $row->toArray());
}
}

0 comments on commit 9a9fa54

Please sign in to comment.
You can’t perform that action at this time.