diff --git a/src/Listener/RelatedModelsListener.php b/src/Listener/RelatedModelsListener.php index a8c130e2c..72764f2b4 100644 --- a/src/Listener/RelatedModelsListener.php +++ b/src/Listener/RelatedModelsListener.php @@ -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; @@ -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(), ]; diff --git a/tests/Fixture/BlogsFixture.php b/tests/Fixture/BlogsFixture.php index a06d86d7f..a08693f3a 100644 --- a/tests/Fixture/BlogsFixture.php +++ b/tests/Fixture/BlogsFixture.php @@ -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'], ]; } diff --git a/tests/TestCase/Action/AddActionTest.php b/tests/TestCase/Action/AddActionTest.php index a34631d9f..c4395e8a3 100644 --- a/tests/TestCase/Action/AddActionTest.php +++ b/tests/TestCase/Action/AddActionTest.php @@ -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 @@ -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']); @@ -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']); @@ -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']); @@ -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); diff --git a/tests/TestCase/Action/DeleteActionTest.php b/tests/TestCase/Action/DeleteActionTest.php index 567d50901..ba3aac8ba 100644 --- a/tests/TestCase/Action/DeleteActionTest.php +++ b/tests/TestCase/Action/DeleteActionTest.php @@ -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 diff --git a/tests/TestCase/Action/EditActionTest.php b/tests/TestCase/Action/EditActionTest.php index cd80acd9e..3844f4e24 100644 --- a/tests/TestCase/Action/EditActionTest.php +++ b/tests/TestCase/Action/EditActionTest.php @@ -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 diff --git a/tests/TestCase/Action/IndexActionTest.php b/tests/TestCase/Action/IndexActionTest.php index c57a571d9..93ed40987 100644 --- a/tests/TestCase/Action/IndexActionTest.php +++ b/tests/TestCase/Action/IndexActionTest.php @@ -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 diff --git a/tests/TestCase/Action/LookupActionTest.php b/tests/TestCase/Action/LookupActionTest.php index 3731faf36..d5b57d20e 100644 --- a/tests/TestCase/Action/LookupActionTest.php +++ b/tests/TestCase/Action/LookupActionTest.php @@ -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 diff --git a/tests/TestCase/Action/ViewActionTest.php b/tests/TestCase/Action/ViewActionTest.php index 8507924ee..7390a0f25 100644 --- a/tests/TestCase/Action/ViewActionTest.php +++ b/tests/TestCase/Action/ViewActionTest.php @@ -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 diff --git a/tests/test_app/src/Model/Table/BlogsTable.php b/tests/test_app/src/Model/Table/BlogsTable.php index 5dbf9a229..793bbc528 100644 --- a/tests/test_app/src/Model/Table/BlogsTable.php +++ b/tests/test_app/src/Model/Table/BlogsTable.php @@ -10,6 +10,13 @@ class BlogsTable extends Table { public $customOptions; + public function initialize(array $config): void + { + parent::initialize($config); + + $this->belongsTo('Users'); + } + /** * findWithCustomOptions *