Skip to content

Commit

Permalink
Restore 'association' option in beforeMarshal event.
Browse files Browse the repository at this point in the history
This was accidentally dropped in 3.2 because there were no tests around
the required keys of the beforeMarshal event.

Refs #10247
  • Loading branch information
markstory committed Feb 18, 2017
1 parent f100367 commit 3442438
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/ORM/Marshaller.php
Expand Up @@ -105,7 +105,7 @@ protected function _buildPropertyMap($data, $options)
}
if (isset($options['isMerge'])) {
$callback = function ($value, $entity) use ($assoc, $nested) {
$options = $nested + ['associated' => []];
$options = $nested + ['associated' => [], 'association' => $assoc];

return $this->_mergeAssociation($entity->get($assoc->getProperty()), $assoc, $value, $options);
};
Expand Down
61 changes: 43 additions & 18 deletions tests/TestCase/ORM/MarshallerTest.php
Expand Up @@ -3139,12 +3139,15 @@ public function testBeforeMarshalEvent()

$marshall = new Marshaller($this->articles);

$this->articles->eventManager()->attach(function ($e, $data, $options) {
$data['title'] = 'Modified title';
$data['user']['username'] = 'robert';

$options['associated'] = ['Users'];
}, 'Model.beforeMarshal');
$this->articles->eventManager()->on(
'Model.beforeMarshal',
function ($e, $data, $options) {
$this->assertArrayHasKey('validate', $options);
$data['title'] = 'Modified title';
$data['user']['username'] = 'robert';

$options['associated'] = ['Users'];
});

$entity = $marshall->one($data);

Expand Down Expand Up @@ -3181,21 +3184,43 @@ public function testBeforeMarshalEventOnAssociations()

$marshall = new Marshaller($this->articles);

$this->articles->users->eventManager()->attach(function ($e, $data) {
$data['secret'] = 'h45h3d';
}, 'Model.beforeMarshal');
// Assert event options are correct
$this->articles->users->eventManager()->on(
'Model.beforeMarshal',
function ($e, $data, $options) {
$this->assertArrayHasKey('validate', $options);
$this->assertTrue($options['validate']);

$this->assertArrayHasKey('associated', $options);
$this->assertSame([], $options['associated']);

$this->assertArrayHasKey('association', $options);
$this->assertInstanceOf('Cake\ORM\Association', $options['association']);
});

$this->articles->users->eventManager()->on(
'Model.beforeMarshal',
function ($e, $data, $options) {
$data['secret'] = 'h45h3d';
});

$this->articles->comments->eventManager()->attach(function ($e, $data) {
$data['comment'] .= ' (modified)';
}, 'Model.beforeMarshal');
$this->articles->comments->eventManager()->on(
'Model.beforeMarshal',
function ($e, $data) {
$data['comment'] .= ' (modified)';
});

$this->articles->tags->eventManager()->attach(function ($e, $data) {
$data['tag'] .= ' (modified)';
}, 'Model.beforeMarshal');
$this->articles->tags->eventManager()->on(
'Model.beforeMarshal',
function ($e, $data) {
$data['tag'] .= ' (modified)';
});

$this->articles->tags->junction()->eventManager()->attach(function ($e, $data) {
$data['modified_by'] = 1;
}, 'Model.beforeMarshal');
$this->articles->tags->junction()->eventManager()->on(
'Model.beforeMarshal',
function ($e, $data) {
$data['modified_by'] = 1;
});

$entity = $marshall->one($data, [
'associated' => ['Users', 'Comments', 'Tags']
Expand Down

0 comments on commit 3442438

Please sign in to comment.