/** * 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')); }
/** * Test merging the _joinData entity for belongstomany associations * while passing a whitelist * * @return void */ public function testMergeJoinDataWithFieldList() { $data = ['title' => 'My title', 'body' => 'My content', 'author_id' => 1, 'tags' => [['id' => 1, 'tag' => 'news', '_joinData' => ['active' => 0]], ['id' => 2, 'tag' => 'cakephp', '_joinData' => ['active' => 0]]]]; $options = ['associated' => ['Tags' => ['associated' => ['_joinData']]]]; $marshall = new Marshaller($this->articles); $entity = $marshall->one($data, $options); $entity->accessible('*', true); $data = ['title' => 'Haz data', 'tags' => [['id' => 1, 'tag' => 'Cake', '_joinData' => ['foo' => 'bar', 'crazy' => 'something']], ['tag' => 'new tag', '_joinData' => ['active' => 1, 'foo' => 'baz']]]]; $tag1 = $entity->tags[0]; $result = $marshall->merge($entity, $data, ['associated' => ['Tags._joinData' => ['fieldList' => ['foo']]]]); $this->assertEquals($data['title'], $result->title); $this->assertEquals('My content', $result->body); $this->assertSame($tag1, $entity->tags[0]); $this->assertSame($tag1->_joinData, $entity->tags[0]->_joinData); $this->assertSame(['active' => 0, 'foo' => 'bar'], $entity->tags[0]->_joinData->toArray()); $this->assertSame(['foo' => 'baz'], $entity->tags[1]->_joinData->toArray()); $this->assertEquals('new tag', $entity->tags[1]->tag); $this->assertTrue($entity->tags[0]->dirty('_joinData')); $this->assertTrue($entity->tags[1]->dirty('_joinData')); }
/** * Tests that patching an association resulting in no changes, will * not mark the parent entity as dirty * * @return void */ public function testAssociationNoChanges() { $options = ['markClean' => true, 'isNew' => false]; $entity = new Entity(['title' => 'My Title', 'user' => new Entity(['username' => 'mark', 'password' => 'not a secret'], $options)], $options); $data = ['body' => 'My Content', 'user' => ['username' => 'mark', 'password' => 'not a secret']]; $marshall = new Marshaller($this->articles); $marshall->merge($entity, $data, ['associated' => ['Users']]); $this->assertEquals('My Content', $entity->body); $this->assertInstanceOf('Cake\\ORM\\Entity', $entity->user); $this->assertEquals('mark', $entity->user->username); $this->assertEquals('not a secret', $entity->user->password); $this->assertFalse($entity->dirty('user')); $this->assertTrue($entity->user->isNew()); }
/** * Tests merge with data types that need to be marshalled * * @return void */ public function testMergeComplexType() { $entity = new Entity(['comment' => 'My Comment text'], ['markNew' => false, 'markClean' => true]); $data = ['created' => ['year' => '2014', 'month' => '2', 'day' => 14]]; $marshall = new Marshaller($this->comments); $result = $marshall->merge($entity, $data); $this->assertInstanceOf('DateTime', $entity->created); $this->assertEquals('2014-02-14', $entity->created->format('Y-m-d')); }