/** * Test merge with validation and create or update validation rules * * @return void */ public function testMergeWithCreate() { $data = ['title' => 'My title', 'author_id' => 'foo']; $marshall = new Marshaller($this->articles); $entity = new Entity(['title' => 'Foo', 'body' => 'My Content', 'author_id' => 1]); $entity->accessible('*', true); $entity->isNew(true); $entity->clean(); $this->articles->validator()->requirePresence('thing', 'update')->add('author_id', 'numeric', ['rule' => 'numeric', 'on' => 'update']); $expected = clone $entity; $result = $marshall->merge($expected, $data, []); $this->assertEmpty($result->errors('author_id')); $this->assertEmpty($result->errors('thing')); $entity->clean(); $entity->isNew(false); $result = $marshall->merge($entity, $data, []); $this->assertNotEmpty($result->errors('author_id')); $this->assertNotEmpty($result->errors('thing')); }
/** * testModifiedPresent * * @return void * @triggers Model.beforeSave */ public function testModifiedPresent() { $table = $this->getMock('Cake\\ORM\\Table'); $this->Behavior = new TimestampBehavior($table); $ts = new \DateTime('2000-01-01'); $this->Behavior->timestamp($ts); $event = new Event('Model.beforeSave'); $existingValue = new \DateTime('2011-11-11'); $entity = new Entity(['name' => 'Foo', 'modified' => $existingValue]); $entity->clean(); $entity->isNew(false); $return = $this->Behavior->handleEvent($event, $entity); $this->assertTrue($return, 'Handle Event is expected to always return true'); $this->assertInstanceOf('Cake\\I18n\\Time', $entity->modified); $this->assertSame($ts->format('c'), $entity->modified->format('c'), 'Modified timestamp is expected to be updated'); }
/** * Restore original (unencrypted) values after saving to DB * @param Event $event Event object * @param Entity $entity Entity object * @param ArrayObject $options Save options array * @return void */ public function afterSave(Event $event, Entity $entity, ArrayObject $options) { if ($entity->has('_cyphered') && is_array($entity->_cyphered)) { foreach ($entity->_cyphered as $field => $value) { $entity->set($field, $value); $entity->clean(); } unset($entity->_cyphered); } }
/** * Tests that marking an entity as clean will remove errors too * * @return void */ public function testCleanRemovesErrors() { $entity = new Entity(['a' => 'b']); $entity->errors('a', 'is not good'); $entity->clean(); $this->assertEmpty($entity->errors()); }
/** * Tests that only the properties marked as dirty are actually saved * to the database * * @group save * @return void */ public function testSaveOnlyDirtyProperties() { $entity = new \Cake\ORM\Entity(['username' => 'superuser', 'password' => 'root', 'created' => new Time('2013-10-10 00:00'), 'updated' => new Time('2013-10-10 00:00')]); $entity->clean(); $entity->dirty('username', true); $entity->dirty('created', true); $entity->dirty('updated', true); $table = TableRegistry::get('users'); $this->assertSame($entity, $table->save($entity)); $this->assertEquals($entity->id, self::$nextUserId); $row = $table->find('all')->where(['id' => self::$nextUserId])->first(); $entity->set('password', null); $this->assertEquals($entity->toArray(), $row->toArray()); }
/** * Tests that it is possible to pass a fieldList option to the merge method * * @return void */ public function testMergeWithFieldList() { $data = ['title' => 'My title', 'author_id' => 1]; $marshall = new Marshaller($this->articles); $entity = new Entity(['title' => 'Foo', 'body' => 'My Content', 'author_id' => 2]); $entity->accessible('*', false); $entity->isNew(false); $entity->clean(); $result = $marshall->merge($entity, $data, ['fieldList' => ['title', 'body']]); $expected = ['title' => 'My title', 'body' => 'My Content', 'author_id' => 2]; $this->assertSame($entity, $result); $this->assertEquals($expected, $result->toArray()); $this->assertFalse($entity->accessible('*')); }
/** * Test iteration after serialization * * @return void */ public function testIteratorAfterSerializationHydrated() { $query = $this->table->find('all'); $results = unserialize(serialize($query->all())); // Use a loop to test Iterator implementation foreach ($results as $i => $row) { $expected = new \Cake\ORM\Entity($this->fixtureData[$i]); $expected->isNew(false); $expected->source($this->table->alias()); $expected->clean(); $this->assertEquals($expected, $row, "Row {$i} does not match"); } }
/** * Tests that fields with the same value are not marked as dirty * * @return void */ public function testMergeDirty() { $marshall = new Marshaller($this->articles); $entity = new Entity(['title' => 'Foo', 'author_id' => 1]); $data = ['title' => 'Foo', 'author_id' => 1, 'crazy' => true]; $entity->accessible('*', true); $entity->clean(); $result = $marshall->merge($entity, $data, []); $expected = ['title' => 'Foo', 'author_id' => 1, 'crazy' => true]; $this->assertEquals($expected, $result->toArray()); $this->assertFalse($entity->dirty('title')); $this->assertFalse($entity->dirty('author_id')); $this->assertTrue($entity->dirty('crazy')); }
/** * Test what happens when everything works and a modifier_id value for the * Entity is already set. * * @return void * @triggers Model.beforeSave */ public function testModifierIdPresent() { $existingValue = "54108b70-a178-4590-9df4-1a900a00020f"; $event = new Event('Model.beforeSave'); $entity = new Entity(['name' => 'Foo', 'modifier_id' => $existingValue]); $entity->clean(); $entity->isNew(false); $return = $this->Behavior->handleEvent($event, $entity); $this->assertTrue($return, 'Handle Event is expected to always return true'); $this->assertInternalType('string', $entity->modifier_id); $this->assertSame($this->mockedUserUUID, $entity->modifier_id, 'Modifier_id Field Equals The Mocked Value as it Should be edited'); }