Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Listener/RelatedModelsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;
use Cake\ORM\Association;
use Cake\ORM\Association\BelongsToMany;
use Cake\Utility\Inflector;
use RuntimeException;

Expand Down Expand Up @@ -118,6 +119,12 @@ public function publishRelatedModels(?string $action = null, ?EntityInterface $e
*/
protected function _findOptions(Association $association): array
{
if ($association instanceof BelongsToMany) {
return [
'keyField' => $association->getPrimaryKey(),
];
}

return [
'keyField' => $association->getBindingKey(),
];
Expand Down
11 changes: 6 additions & 5 deletions tests/Fixture/BlogsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ class BlogsFixture extends TestFixture
'is_active' => ['type' => 'boolean', 'default' => true, 'null' => false],
'name' => ['type' => 'string', 'length' => 255, 'null' => false],
'body' => ['type' => 'text', 'null' => false],
'user_id' => ['type' => 'uuid', 'null' => false],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
];

public $records = [
['name' => '1st post', 'body' => '1st post body'],
['name' => '2nd post', 'body' => '2nd post body'],
['name' => '3rd post', 'body' => '3rd post body'],
['name' => '4th post', 'body' => '4th post body'],
['name' => '5th post', 'body' => '5th post body'],
['name' => '1st post', 'body' => '1st post body', 'user_id' => '0acad6f2-b47e-4fc1-9086-cbc906dc45fd'],
['name' => '2nd post', 'body' => '2nd post body', 'user_id' => '0acad6f2-b47e-4fc1-9086-cbc906dc45fd'],
['name' => '3rd post', 'body' => '3rd post body', 'user_id' => '0acad6f2-b47e-4fc1-9086-cbc906dc45fd'],
['name' => '4th post', 'body' => '4th post body', 'user_id' => '0acad6f2-b47e-4fc1-9086-cbc906dc45fd'],
['name' => '5th post', 'body' => '5th post body', 'user_id' => '0acad6f2-b47e-4fc1-9086-cbc906dc45fd'],
];
}
6 changes: 5 additions & 1 deletion tests/TestCase/Action/AddActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AddActionTest extends IntegrationTestCase
*
* @var array
*/
protected $fixtures = ['plugin.Crud.Blogs'];
protected $fixtures = ['plugin.Crud.Blogs', 'plugin.Crud.Users'];

/**
* Table class to mock on
Expand Down Expand Up @@ -111,6 +111,7 @@ function ($event) {
$this->post('/blogs/add', [
'name' => 'Hello World',
'body' => 'Pretty hot body',
'user_id' => '0acad6f2-b47e-4fc1-9086-cbc906dc45fd',
]);

$this->assertEvents(['beforeSave', 'afterSave', 'setFlash', 'beforeRedirect']);
Expand Down Expand Up @@ -157,6 +158,7 @@ function ($event) {
'name' => 'Hello World',
'body' => 'Pretty hot body',
'_add' => 1,
'user_id' => '0acad6f2-b47e-4fc1-9086-cbc906dc45fd',
]);

$this->assertEvents(['beforeSave', 'afterSave', 'setFlash', 'beforeRedirect']);
Expand Down Expand Up @@ -202,6 +204,7 @@ function ($event) {
'name' => 'Hello World',
'body' => 'Pretty hot body',
'_edit' => 1,
'user_id' => '0acad6f2-b47e-4fc1-9086-cbc906dc45fd',
]);

$this->assertEvents(['beforeSave', 'afterSave', 'setFlash', 'beforeRedirect']);
Expand Down Expand Up @@ -404,6 +407,7 @@ function ($event) {
$this->{$method}('/blogs/add.json', [
'name' => '6th blog post',
'body' => 'Amazing blog post',
'user_id' => '0acad6f2-b47e-4fc1-9086-cbc906dc45fd',
]);
$this->assertTrue($this->_subject->success);
$this->assertTrue($this->_subject->created);
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Action/DeleteActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DeleteActionTest extends IntegrationTestCase
*
* @var array
*/
protected $fixtures = ['plugin.Crud.Blogs'];
protected $fixtures = ['plugin.Crud.Blogs', 'plugin.Crud.Users'];

/**
* Table class to mock on
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Action/EditActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class EditActionTest extends IntegrationTestCase
*
* @var array
*/
protected $fixtures = ['plugin.Crud.Blogs'];
protected $fixtures = ['plugin.Crud.Blogs', 'plugin.Crud.Users'];

/**
* Table class to mock on
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Action/IndexActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class IndexActionTest extends IntegrationTestCase
*
* @var array
*/
protected $fixtures = ['plugin.Crud.Blogs'];
protected $fixtures = ['plugin.Crud.Blogs', 'plugin.Crud.Users'];

/**
* Data provider with all HTTP verbs
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Action/LookupActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LookupActionTest extends IntegrationTestCase
*
* @var array
*/
protected $fixtures = ['plugin.Crud.Blogs'];
protected $fixtures = ['plugin.Crud.Blogs', 'plugin.Crud.Users'];

/**
* Test with no extra options
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Action/ViewActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ViewActionTest extends IntegrationTestCase
*
* @var array
*/
protected $fixtures = ['plugin.Crud.Blogs'];
protected $fixtures = ['plugin.Crud.Blogs', 'plugin.Crud.Users'];

/**
* Data provider with all HTTP verbs
Expand Down
7 changes: 7 additions & 0 deletions tests/test_app/src/Model/Table/BlogsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ class BlogsTable extends Table
{
public $customOptions;

public function initialize(array $config): void
{
parent::initialize($config);

$this->belongsTo('Users');
}

/**
* findWithCustomOptions
*
Expand Down