예제 #1
0
 /**
  * Test Model.beforeMarshal event on associated tables.
  *
  * @return void
  */
 public function testBeforeMarshalEventOnAssociations()
 {
     $data = ['title' => 'My title', 'body' => 'My content', 'author_id' => 1, 'user' => ['username' => 'mark', 'password' => 'secret'], 'comments' => [['comment' => 'First post', 'user_id' => 2], ['comment' => 'Second post', 'user_id' => 2]], 'tags' => [['tag' => 'news', '_joinData' => ['active' => 1]], ['tag' => 'cakephp', '_joinData' => ['active' => 0]]]];
     $marshall = new Marshaller($this->articles);
     $this->articles->users->eventManager()->attach(function ($e, $data) {
         $data['secret'] = 'h45h3d';
     }, 'Model.beforeMarshal');
     $this->articles->comments->eventManager()->attach(function ($e, $data) {
         $data['comment'] .= ' (modified)';
     }, 'Model.beforeMarshal');
     $this->articles->tags->eventManager()->attach(function ($e, $data) {
         $data['tag'] .= ' (modified)';
     }, 'Model.beforeMarshal');
     $this->articles->tags->junction()->eventManager()->attach(function ($e, $data) {
         $data['modified_by'] = 1;
     }, 'Model.beforeMarshal');
     $entity = $marshall->one($data, ['associated' => ['Users', 'Comments', 'Tags']]);
     $this->assertEquals('h45h3d', $entity->user->secret);
     $this->assertEquals('First post (modified)', $entity->comments[0]->comment);
     $this->assertEquals('Second post (modified)', $entity->comments[1]->comment);
     $this->assertEquals('news (modified)', $entity->tags[0]->tag);
     $this->assertEquals('cakephp (modified)', $entity->tags[1]->tag);
     $this->assertEquals(1, $entity->tags[0]->_joinData->modified_by);
     $this->assertEquals(1, $entity->tags[1]->_joinData->modified_by);
 }
예제 #2
0
 /**
  * Test generating a list of entities from a list of composite ids
  *
  * @return void
  */
 public function testOneGenerateBelongsToManyEntitiesFromIds()
 {
     $articles = TableRegistry::get('SiteArticles');
     $articles->entityClass(__NAMESPACE__ . '\\OpenArticleEntity');
     $tags = TableRegistry::get('SiteTags');
     $junction = TableRegistry::get('SiteArticlesTags');
     $articles->belongsToMany('SiteTags', ['targetTable' => $tags, 'propertyName' => 'tags', 'through' => 'SiteArticlesTags', 'foreignKey' => ['article_id', 'site_id'], 'targetForeignKey' => ['tag_id', 'site_id']]);
     $data = ['title' => 'Haz tags', 'body' => 'Some content here', 'tags' => ['_ids' => [[1, 1], [2, 2], [3, 1]]]];
     $marshall = new Marshaller($articles);
     $result = $marshall->one($data, ['associated' => ['SiteTags']]);
     $this->assertCount(3, $result->tags);
     $this->assertInstanceOf('Cake\\ORM\\Entity', $result->tags[0]);
     $this->assertInstanceOf('Cake\\ORM\\Entity', $result->tags[1]);
     $this->assertInstanceOf('Cake\\ORM\\Entity', $result->tags[2]);
     $data = ['title' => 'Haz tags', 'body' => 'Some content here', 'tags' => ['_ids' => [1, 2, 3]]];
     $marshall = new Marshaller($articles);
     $result = $marshall->one($data, ['associated' => ['SiteTags']]);
     $this->assertEmpty($result->tags);
 }
예제 #3
0
 /**
  * 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());
 }
예제 #4
0
 /**
  * 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'));
 }
예제 #5
0
 /**
  * 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'));
 }
예제 #6
0
 public function testEnsurePrimaryKeyBeingReadFromTableWhenLoadingBelongsToManyRecordsByPrimaryKey()
 {
     $data = ['tags' => [['id' => 1], ['id' => 2]]];
     $tags = TableRegistry::get('Tags');
     $tags->schema()->dropConstraint('primary');
     $tags->primaryKey('id');
     $marshall = new Marshaller($this->articles);
     $result = $marshall->one($data, ['associated' => ['Tags']]);
     $expected = ['tags' => [['id' => 1, 'name' => 'tag1', 'description' => 'A big description'], ['id' => 2, 'name' => 'tag2', 'description' => 'Another big description']]];
     $this->assertEquals($expected, $result->toArray());
 }