diff --git a/src/Traits/MongoSyncTrait.php b/src/Traits/MongoSyncTrait.php index 2ee8e9f..113c76f 100644 --- a/src/Traits/MongoSyncTrait.php +++ b/src/Traits/MongoSyncTrait.php @@ -115,15 +115,20 @@ public function processAllRelationships(Request $request, string $event, string $modelTarget = $relation['modelTarget']; $methodOnTarget = $relation['methodOnTarget']; $modelOnTarget = $relation['modelOnTarget']; + $typeOnTarget = Arr::has($relation, 'typeOnTarget') ? Arr::get($relation, 'typeOnTarget') : 'EmbedsMany'; } else { $modelTarget = ''; $methodOnTarget = ''; $modelOnTarget = ''; + $typeOnTarget = ''; } $is_EO = is_EO($type); $is_EM = is_EM($type); + $is_EM_target = is_EM($typeOnTarget); + $is_EO_target = is_EO($typeOnTarget); + $key = $parent.$method.$counter; $is_skippable = $this->getIsSkippable($request->has($key), $hasTarget); @@ -147,7 +152,7 @@ public function processAllRelationships(Request $request, string $event, string //Delete EmbedsMany or EmbedsOne on Target - TODO: check if it is necessary to run deleteTargetObj method if ($hasTarget) { - $this->deleteTargetObj($method, $modelTarget, $methodOnTarget, $is_EO); + $this->deleteTargetObj($method, $modelTarget, $methodOnTarget, $is_EO, $is_EO_target, $is_EM_target); } //Delete EmbedsMany or EmbedsOne on current object if ($is_EM) { @@ -163,7 +168,7 @@ public function processAllRelationships(Request $request, string $event, string $i = 0; foreach ($objs as $obj) { - $this->processOneEmbededRelationship( + $this->processOneEmbeddedRelationship( $request, $obj, $type, @@ -175,6 +180,8 @@ public function processAllRelationships(Request $request, string $event, string $hasTarget, $is_EO, $is_EM, + $is_EO_target, + $is_EM_target, $i, $is_embeds_has_to_be_updated, $options); @@ -195,14 +202,21 @@ public function processAllRelationships(Request $request, string $event, string /** * @param $mini_model * @param string $method_on_target + * @param bool $is_EO_target + * @param bool $is_EM_target */ - public function updateRelationWithSync($mini_model, string $method_on_target) + public function updateRelationWithSync($mini_model, string $method_on_target, $is_EO_target, $is_EM_target) { - $new_values = []; - foreach ($this->$method_on_target as $temp) { - $new_values[] = $temp->attributes; + if ($is_EM_target) { + $new_values = []; + foreach ($this->$method_on_target as $temp) { + $new_values[] = $temp->attributes; + } + $new_values[] = $mini_model->attributes; + } else { + $new_values = $mini_model->attributes; } - $new_values[] = $mini_model->attributes; + $this->$method_on_target = $new_values; $this->save(); } @@ -218,21 +232,23 @@ public function updateRelationWithSync($mini_model, string $method_on_target) * @param $modelOnTarget * @param $event * @param $hasTarget - * @param $is_EO - * @param $is_EM + * @param bool $is_EO + * @param bool $is_EM + * @param bool $is_EO_target + * @param bool $is_EM_target * @param $i * @param bool $is_embeds_has_to_be_updated * @param $options * @throws Exception */ - public function processOneEmbededRelationship(Request $request, $obj, $type, $model, $method, $modelTarget, $methodOnTarget, $modelOnTarget, $event, $hasTarget, $is_EO, $is_EM, $i, $is_embeds_has_to_be_updated, $options) + public function processOneEmbeddedRelationship(Request $request, $obj, $type, $model, $method, $modelTarget, $methodOnTarget, $modelOnTarget, $event, $hasTarget, $is_EO, $is_EM, $is_EO_target, $is_EM_target, $i, $is_embeds_has_to_be_updated, $options) { if ($is_embeds_has_to_be_updated) { $this->processEmbedOnCurrentCollection($request, $obj, $type, $model, $method, $event, $is_EO, $is_EM, $i, $options); } if ($hasTarget) { - $this->processEmbedOnTargetCollection($modelTarget, $obj, $methodOnTarget, $modelOnTarget); + $this->processEmbedOnTargetCollection($modelTarget, $obj, $methodOnTarget, $modelOnTarget, $is_EO_target, $is_EM_target); } } @@ -260,19 +276,21 @@ public function updateWithSync(Request $request, array $additionalData = [], arr * @param $method * @param $modelTarget * @param $methodOnTarget - * @param $is_EO + * @param bool $is_EO + * @param bool $is_EO_target + * @param bool $is_EM_target */ - public function deleteTargetObj($method, $modelTarget, $methodOnTarget, $is_EO) + public function deleteTargetObj($method, $modelTarget, $methodOnTarget, $is_EO, $is_EO_target, $is_EM_target) { if ($is_EO) { $embedObj = $this->$method; if (! is_null($embedObj)) { $target_id = $embedObj->ref_id; - $this->handleSubTarget($target_id, $modelTarget, $methodOnTarget); + $this->handleSubTarget($target_id, $modelTarget, $methodOnTarget, $is_EO_target, $is_EM_target); } } else { foreach ($this->$method as $target) { - $this->handleSubTarget($target->ref_id, $modelTarget, $methodOnTarget); + $this->handleSubTarget($target->ref_id, $modelTarget, $methodOnTarget, $is_EO_target, $is_EM_target); } } } @@ -281,21 +299,24 @@ public function deleteTargetObj($method, $modelTarget, $methodOnTarget, $is_EO) * @param $target_id * @param $modelTarget * @param $methodOnTarget + * @param bool $is_EO_target + * @param bool $is_EM_target */ - public function handleSubTarget($target_id, $modelTarget, $methodOnTarget) + public function handleSubTarget($target_id, $modelTarget, $methodOnTarget, $is_EO_target, $is_EM_target) { - $id = $this->getId(); - $target = new $modelTarget; - $target = $target->all()->where('id', $target_id)->first(); - if (! is_null($target)) { - $new_values = []; - foreach ($target->$methodOnTarget as $temp) { - if ($temp->ref_id !== $id) { - $new_values[] = $temp->attributes; + if ($is_EM_target) { + $target = new $modelTarget; + $target = $target->all()->where('id', $target_id)->first(); + if (! is_null($target)) { + $new_values = []; + foreach ($target->$methodOnTarget as $temp) { + if ($temp->ref_id !== $this->getId()) { + $new_values[] = $temp->attributes; + } } + $target->$methodOnTarget = $new_values; + $target->save(); } - $target->$methodOnTarget = $new_values; - $target->save(); } } @@ -482,14 +503,16 @@ private function processEmbedOnCurrentCollection(Request $request, $obj, $type, * @param $obj * @param $methodOnTarget * @param $modelOnTarget + * @param bool $is_EO_target + * @param bool $is_EM_target * @throws Exception */ - private function processEmbedOnTargetCollection($modelTarget, $obj, $methodOnTarget, $modelOnTarget) + private function processEmbedOnTargetCollection($modelTarget, $obj, $methodOnTarget, $modelOnTarget, $is_EO_target, $is_EM_target) { $modelToBeSync = $this->getModelTobeSync($modelTarget, $obj); if (! is_null($modelToBeSync)) { $miniModel = $this->getEmbedModel($modelOnTarget); - $modelToBeSync->updateRelationWithSync($miniModel, $methodOnTarget); + $modelToBeSync->updateRelationWithSync($miniModel, $methodOnTarget, $is_EO_target, $is_EM_target); //TODO:Sync target on level > 1 //$modelToBeSync->processAllRelationships($request, $event, $methodOnTarget, $methodOnTarget . "-"); } diff --git a/tests/StoreWIthSyncTest.php b/tests/StoreWIthSyncTest.php index 36b0a86..0271877 100644 --- a/tests/StoreWIthSyncTest.php +++ b/tests/StoreWIthSyncTest.php @@ -3,11 +3,80 @@ namespace Tests; use Faker\Factory; +use Illuminate\Support\Facades\Date; use OfflineAgency\MongoAutoSync\Extensions\MongoCollection; use Tests\Models\Navigation; +use Tests\Models\SubItem; class StoreWIthSyncTest extends SyncTestCase { + public function test_store_with_embeds_one_on_target() + { + //Create a SubItem and test if the data is store correctly + $sub_item = $this->createSubItems( + [ + 'text' => 'example sub item test', + 'code' => 'HFGRT12345', + 'href' => 'https://google.com', + ] + ); + + $this->assertEquals('example sub item test', getTranslatedContent($sub_item->text)); + $this->assertEquals('HFGRT12345', $sub_item->code); + $this->assertEquals('https://google.com', $sub_item->href); + + //Create a mini Sub Item to associate to the new navigation + $sub_items = json_encode( + [ + (object) [ + 'ref_id' => $sub_item->id, + 'text' => getTranslatedContent($sub_item->text), + 'code' => $sub_item->code, + 'href' => $sub_item->href, + ], + ] + ); + + $date = Date::now(); + + //Create a navigation and test if the data is store correctly + $navigation = $this->createNavigation( + [ + 'text' => 'example navigation text', + 'code' => '1234ABHFGRT5', + 'href' => 'https://www.netflix.com/browse', + 'date' => $date, + 'target' => '_blank', + 'title' => 'Random title', + 'sub_items' => $sub_items, + ] + ); + + $this->assertTrue($this->isNavigationCreated($navigation)); + $this->assertIsString($navigation->text); + $this->assertIsArray($navigation->title); + + $this->assertEquals('example navigation text', $navigation->text); + $this->assertEquals('1234ABHFGRT5', $navigation->code); + $this->assertEquals('https://www.netflix.com/browse', $navigation->href); + //$this->assertEquals($date, $navigation->date); TODO: fix precision date + $this->assertEquals('_blank', $navigation->target); + $this->assertEquals('Random title', getTranslatedContent($navigation->title)); + $this->assertInstanceOf(MongoCollection::class, $navigation->sub_items); + + //Check if the subitem target is updated correctly + $sub_item = SubItem::find($sub_item->id); + $mini_navigation = $sub_item->navigation; + $this->assertNotNull($mini_navigation); + $this->assertEquals($navigation->id, $mini_navigation->ref_id); + $this->assertEquals('1234ABHFGRT5', $mini_navigation->code); + $this->assertEquals('Random title', getTranslatedContent($mini_navigation->title)); + $this->assertEquals('example navigation text', $navigation->text); + + //Clean all data that has been stored + $this->cleanUp($navigation, $sub_item); + } + public function test_store_with_embeds_many_on_target() { //Create a navigation and test if the data is store correctly diff --git a/tests/UpdateWithSyncTest.php b/tests/UpdateWithSyncTest.php index b81ed29..51e7cfd 100644 --- a/tests/UpdateWithSyncTest.php +++ b/tests/UpdateWithSyncTest.php @@ -8,10 +8,79 @@ use OfflineAgency\MongoAutoSync\Extensions\MongoCollection; use Tests\Models\MiniNavigation; use Tests\Models\Navigation; +use Tests\Models\SubItem; use Tests\SyncTestCase; class UpdateWithSyncTest extends SyncTestCase { + public function test_update_with_embeds_one_on_target() + { + //Sub Item Test + $sub_item = $this->createSubItems( + [ + 'text' => 'example sub item test', + 'code' => 'HFGRT12345', + 'href' => 'https://google.com', + ] + ); + $this->assertEquals('example sub item test', getTranslatedContent($sub_item->text)); + $this->assertEquals('HFGRT12345', $sub_item->code); + $this->assertEquals('https://google.com', $sub_item->href); + + //Navigation Test + $sub_items = json_encode( + [ + (object) [ + 'ref_id' => $sub_item->id, + 'text' => getTranslatedContent($sub_item->text), + 'code' => $sub_item->code, + 'href' => $sub_item->href, + ], + ] + ); + + $navigation = new Navigation; + + $date = Date::now(); + + $navigation = $this->createNavigation( + [ + 'text' => 'example navigation text', + 'code' => '1234ABHFGRT5', + 'href' => 'https://www.netflix.com/browse', + 'date' => $date, + 'target' => '_blank', + 'title' => 'Random title', + 'sub_items' => $sub_items, + ] + ); + + $this->assertTrue($this->isNavigationCreated($navigation)); + $this->assertIsString($navigation->text); + + $this->assertEquals('example navigation text', $navigation->text); + $this->assertEquals('1234ABHFGRT5', $navigation->code); + $this->assertEquals('https://www.netflix.com/browse', $navigation->href); + //$this->assertEquals($date, $navigation->date); TODO: Precision to be fixed + $this->assertEquals('_blank', $navigation->target); + $this->assertEquals('Random title', getTranslatedContent($navigation->title)); + $this->assertInstanceOf(MongoCollection::class, $navigation->sub_items); + + //Check target + $sub_item = SubItem::find($sub_item->id); + $mini_navigation = $sub_item->navigation; + $this->assertNotNull($mini_navigation); + + $this->assertEquals($navigation->id, $mini_navigation->ref_id); + $this->assertEquals('1234ABHFGRT5', $mini_navigation->code); + $this->assertEquals('Random title', getTranslatedContent($mini_navigation->title)); + $this->assertEquals('example navigation text', $navigation->text); + + //clean data + $navigation->delete(); + $sub_item->delete(); + } + public function test_update_with_embeds_many_on_target() { $faker = Factory::create(); @@ -62,6 +131,7 @@ public function test_update_with_embeds_many_on_target() //Check target $navigation = Navigation::find($navigation->id); + $sub_item_mini = $navigation->sub_items[0]; $this->assertNotEmpty($navigation->sub_items);