@@ -594,6 +594,60 @@ public function test_updating_a_resource_with_attaching_has_many_relation(): voi
594
594
);
595
595
}
596
596
597
+ public function test_updating_a_resource_with_attaching_has_many_relation_with_required_rules (): void
598
+ {
599
+ $ modelToUpdate = ModelFactory::new ()->createOne ();
600
+ $ hasManyRelationToAttach = HasManyRelationFactory::new ()
601
+ ->createOne ();
602
+
603
+ Gate::policy (Model::class, GreenPolicy::class);
604
+ Gate::policy (HasManyRelation::class, GreenPolicy::class);
605
+ Gate::policy (BelongsToManyRelation::class, GreenPolicy::class);
606
+
607
+ $ response = $ this ->post (
608
+ '/api/constrained/mutate ' ,
609
+ [
610
+ 'mutate ' => [
611
+ [
612
+ 'operation ' => 'update ' ,
613
+ 'key ' => $ modelToUpdate ->getKey (),
614
+ 'attributes ' => [
615
+ 'name ' => 'new name ' ,
616
+ 'number ' => 5001 ,
617
+ ],
618
+ 'relations ' => [
619
+ 'belongsToManyRelation ' => [
620
+ [
621
+ 'operation ' => 'create ' ,
622
+ 'attributes ' => [],
623
+ ],
624
+ ],
625
+ 'hasManyRelation ' => [
626
+ [
627
+ 'operation ' => 'attach ' ,
628
+ 'key ' => $ hasManyRelationToAttach ->getKey (),
629
+ ],
630
+ ],
631
+ ],
632
+ ],
633
+ ],
634
+ ],
635
+ ['Accept ' => 'application/json ' ]
636
+ );
637
+
638
+ $ this ->assertMutatedResponse (
639
+ $ response ,
640
+ [],
641
+ [$ modelToUpdate ],
642
+ );
643
+
644
+ // Here we test that the relation is correctly linked
645
+ $ this ->assertEquals (
646
+ Model::find ($ response ->json ('updated.0 ' ))->hasManyRelation ()->count (),
647
+ 1
648
+ );
649
+ }
650
+
597
651
public function test_updating_a_resource_with_detaching_has_many_relation (): void
598
652
{
599
653
$ modelToUpdate = ModelFactory::new ()->createOne ();
@@ -1191,6 +1245,60 @@ public function test_updating_a_resource_with_creating_belongs_to_many_relation_
1191
1245
);
1192
1246
}
1193
1247
1248
+ public function test_updating_a_resource_with_updating_belongs_to_many_relation_pivot_fields (): void
1249
+ {
1250
+ $ modelToUpdate = ModelFactory::new ()->createOne ();
1251
+
1252
+ $ belongsToManyToUpdate = BelongsToManyRelationFactory::new ()->createOne ();
1253
+
1254
+ Gate::policy (Model::class, GreenPolicy::class);
1255
+ Gate::policy (BelongsToManyRelation::class, GreenPolicy::class);
1256
+
1257
+ $ response = $ this ->post (
1258
+ '/api/models/mutate ' ,
1259
+ [
1260
+ 'mutate ' => [
1261
+ [
1262
+ 'operation ' => 'update ' ,
1263
+ 'key ' => $ modelToUpdate ->getKey (),
1264
+ 'attributes ' => [
1265
+ 'name ' => 'new name ' ,
1266
+ 'number ' => 5001 ,
1267
+ ],
1268
+ 'relations ' => [
1269
+ 'belongsToManyRelation ' => [
1270
+ [
1271
+ 'operation ' => 'update ' ,
1272
+ 'key ' => $ belongsToManyToUpdate ->getKey (),
1273
+ 'pivot ' => [
1274
+ 'number ' => 20 ,
1275
+ ],
1276
+ ],
1277
+ ],
1278
+ ],
1279
+ ],
1280
+ ],
1281
+ ],
1282
+ ['Accept ' => 'application/json ' ]
1283
+ );
1284
+
1285
+ $ this ->assertMutatedResponse (
1286
+ $ response ,
1287
+ [],
1288
+ [$ modelToUpdate ]
1289
+ );
1290
+
1291
+ // Here we test that the relation is correctly linked
1292
+ $ this ->assertEquals (
1293
+ Model::find ($ response ->json ('updated.0 ' ))->belongsToManyRelation ()->count (),
1294
+ 1
1295
+ );
1296
+ $ this ->assertEquals (
1297
+ Model::find ($ response ->json ('updated.0 ' ))->belongsToManyRelation [0 ]->belongs_to_many_pivot ->number ,
1298
+ 20
1299
+ );
1300
+ }
1301
+
1194
1302
public function test_updating_a_resource_with_creating_multiple_belongs_to_many_relation (): void
1195
1303
{
1196
1304
$ modelToUpdate = ModelFactory::new ()->createOne ();
0 commit comments