From af59b35937cef3dc86a1e7a83a6cab774a87d98c Mon Sep 17 00:00:00 2001 From: Roberto Butti Date: Thu, 6 Apr 2023 23:05:57 +0200 Subject: [PATCH 01/14] fix: resolve relations Use case: fixing resolving relation for nested stories with rels and not rel_uuids --- src/Storyblok/Client.php | 49 ++++++++++++++++++---- tests/Storyblok/Integration/ClientTest.php | 21 ++++++++++ 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/src/Storyblok/Client.php b/src/Storyblok/Client.php index da41df1..c30b008 100644 --- a/src/Storyblok/Client.php +++ b/src/Storyblok/Client.php @@ -741,7 +741,7 @@ public function enrichContent($data) { $enrichedContent = $data; if (isset($data['component'])) { - if (!isset($data['_stopResolving'])) { + if (!$this->isStopResolving($data)) { foreach ($data as $fieldName => $fieldValue) { $enrichedContent[$fieldName] = $this->insertRelations($data['component'], $fieldName, $fieldValue); $enrichedContent[$fieldName] = $this->insertLinks($enrichedContent[$fieldName]); @@ -749,10 +749,12 @@ public function enrichContent($data) } } } elseif (\is_array($data)) { - if (!isset($data['_stopResolving'])) { + if (!$this->isStopResolving($data)) { foreach ($data as $key => $value) { if (\is_string($value) && \array_key_exists($value, $this->resolvedRelations)) { - $enrichedContent[$key] = $this->resolvedRelations[$value]; + if ('uuid' !== $key) { + $enrichedContent[$key] = $this->resolvedRelations[$value]; + } } else { $enrichedContent[$key] = $this->enrichContent($value); } @@ -903,17 +905,27 @@ private function insertRelations($component, $field, $value) if (\is_string($value)) { if (isset($this->resolvedRelations[$value])) { $filteredNode = $this->resolvedRelations[$value]; - $filteredNode['_stopResolving'] = true; + $filteredNode['_stopResolving'] = $this->getLevelResolving($filteredNode) + 1; + // $this->settingStopResolving($filteredNode); + // $filteredNode['_stopResolving'] = true; } } elseif (\is_array($value)) { - $filteredNode = []; + $filteredNodeTemp = []; + $resolved = false; foreach ($value as $item) { if (\is_string($item) && isset($this->resolvedRelations[$item])) { + $resolved = true; $story = $this->resolvedRelations[$item]; - $story['_stopResolving'] = true; - $filteredNode[] = $story; + + // $story['_stopResolving'] = true; + // $this->settingStopResolving($story); + $story['_stopResolving'] = $this->getLevelResolving($story) + 1; + $filteredNodeTemp[] = $story; } } + if ($resolved) { + $filteredNode = $filteredNodeTemp; + } } } @@ -1072,4 +1084,27 @@ private function _getCacheKey($key = '') { return hash('sha256', $key); } + +/* + private function settingStopResolving(&$data) + { + $data['_stopResolving'] = $this->getLevelResolving($data) + 1; + } +*/ + private function isStopResolving($data) + { + return $this->getLevelResolving($data) > 2; + } + + private function getLevelResolving($data) + { + $level = 0; + if (!isset($data['_stopResolving'])) { + $level = 0; + } else { + $level = $data['_stopResolving']; + } + + return $level; + } } diff --git a/tests/Storyblok/Integration/ClientTest.php b/tests/Storyblok/Integration/ClientTest.php index d435c81..07f436e 100644 --- a/tests/Storyblok/Integration/ClientTest.php +++ b/tests/Storyblok/Integration/ClientTest.php @@ -154,6 +154,27 @@ expect($body['rels'])->toHaveLength(51); })->group('integration'); +test('Integration: get one story from Product', function () { + unset($_GET['_storyblok_published']); + $client = new Client('HMqBPn2a92FjXYI3tQGDVQtt'); + $slug = 'categories/products/bike-001'; + $key = 'stories/' . $slug; + $client->editMode(); + $options = $client->getApiParameters(); + $client->resolveRelations( + 'Product.ProductVariants' + ); + $responses = $client->getStoryBySlug($slug); + $body = $responses->getBody(); + expect($body)->toHaveKeys(['rels', 'story', 'cv', 'links']); + // print_r($body); + expect($body['story']['content']['productname'])->toEqual('Bike 001'); + expect($body['story']['content']['ProductVariants'])->toBeArray(); + expect($body['story']['content']['ProductVariants'])->toHaveLength(2); + expect($body['story']['content']['ProductVariants']['0']['name'])->toBeString(); + expect($body['story']['content']['ProductVariants']['0']['name'])->toEqual('Bike 001 L'); +})->group('integration'); + test('Integration: get one story with Resolved relations 2', function () { unset($_GET['_storyblok_published']); $client = new Client('HMqBPn2a92FjXYI3tQGDVQtt'); From fe309643dbcd7ef0ed5fa49eacc4d3654c29bf19 Mon Sep 17 00:00:00 2001 From: Roberto Butti Date: Thu, 6 Apr 2023 23:11:58 +0200 Subject: [PATCH 02/14] Update ClientTest.php --- tests/Storyblok/Integration/ClientTest.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/Storyblok/Integration/ClientTest.php b/tests/Storyblok/Integration/ClientTest.php index 07f436e..a41ec27 100644 --- a/tests/Storyblok/Integration/ClientTest.php +++ b/tests/Storyblok/Integration/ClientTest.php @@ -68,20 +68,17 @@ $body = $response->getBody(); expect($body)->toBeArray() ->toHaveKey('story') - ->toHaveCount(4) - ; + ->toHaveCount(4); $story = $body['story']; expect($story)->toBeArray() ->toHaveKey('content') - ->toHaveCount(22) - ; + ->toHaveCount(22); $content = $story['content']; expect($content)->toBeArray() ->toHaveKey('_uid') ->toHaveKey('body') ->toHaveKey('component') - ->toHaveCount(4) - ; + ->toHaveCount(4); expect($content['_uid'])->toBeString(); expect($content['component'])->toBeString(); })->group('integration'); From bb60345a5eb77f2c2fe85b30d35fe388c3ff7d74 Mon Sep 17 00:00:00 2001 From: Roberto Butti Date: Fri, 7 Apr 2023 09:18:08 +0200 Subject: [PATCH 03/14] adding a test for resolving less than 50 relations --- src/Storyblok/Client.php | 12 +++--------- tests/Storyblok/Integration/ClientTest.php | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/Storyblok/Client.php b/src/Storyblok/Client.php index c30b008..d4886d5 100644 --- a/src/Storyblok/Client.php +++ b/src/Storyblok/Client.php @@ -905,9 +905,7 @@ private function insertRelations($component, $field, $value) if (\is_string($value)) { if (isset($this->resolvedRelations[$value])) { $filteredNode = $this->resolvedRelations[$value]; - $filteredNode['_stopResolving'] = $this->getLevelResolving($filteredNode) + 1; - // $this->settingStopResolving($filteredNode); - // $filteredNode['_stopResolving'] = true; + $this->settingStopResolving($filteredNode); } } elseif (\is_array($value)) { $filteredNodeTemp = []; @@ -916,10 +914,7 @@ private function insertRelations($component, $field, $value) if (\is_string($item) && isset($this->resolvedRelations[$item])) { $resolved = true; $story = $this->resolvedRelations[$item]; - - // $story['_stopResolving'] = true; - // $this->settingStopResolving($story); - $story['_stopResolving'] = $this->getLevelResolving($story) + 1; + $this->settingStopResolving($story); $filteredNodeTemp[] = $story; } } @@ -1085,12 +1080,11 @@ private function _getCacheKey($key = '') return hash('sha256', $key); } -/* private function settingStopResolving(&$data) { $data['_stopResolving'] = $this->getLevelResolving($data) + 1; } -*/ + private function isStopResolving($data) { return $this->getLevelResolving($data) > 2; diff --git a/tests/Storyblok/Integration/ClientTest.php b/tests/Storyblok/Integration/ClientTest.php index a41ec27..4160e2b 100644 --- a/tests/Storyblok/Integration/ClientTest.php +++ b/tests/Storyblok/Integration/ClientTest.php @@ -151,6 +151,26 @@ expect($body['rels'])->toHaveLength(51); })->group('integration'); +test('Integration: get one story with few resolved relations', function () { + unset($_GET['_storyblok_published']); + $client = new Client('HMqBPn2a92FjXYI3tQGDVQtt'); + $slug = 'categories/category-shoe-001'; + $key = 'stories/' . $slug; + $client->editMode(); + $options = $client->getApiParameters(); + $client->resolveRelations( + 'ProductCategory.Products,Product.ProductVariants' + ); + $responses = $client->getStoryBySlug($slug); + $body = $responses->getBody(); + expect($body)->toHaveKeys(['rels', 'story', 'cv', 'links']); + expect($body['story']['content']['Products'])->toBeArray(); + expect($body['story']['content']['Products'])->toHaveCount(1); + expect($body['story']['content']['Products']['0']['content']['productname'])->toEqual('Shoe 001'); + expect($body['story']['content']['Products']['0']['content']['ProductVariants'])->toBeArray()->toHaveLength(3); + expect($body['rels'])->toHaveLength(4); +})->group('integration'); + test('Integration: get one story from Product', function () { unset($_GET['_storyblok_published']); $client = new Client('HMqBPn2a92FjXYI3tQGDVQtt'); From a3c82ad84858f01b643dd6c0e97c715ada258bb9 Mon Sep 17 00:00:00 2001 From: Roberto Butti Date: Fri, 7 Apr 2023 13:29:30 +0200 Subject: [PATCH 04/14] additional tests for second level relations --- tests/Storyblok/Integration/ClientTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/Storyblok/Integration/ClientTest.php b/tests/Storyblok/Integration/ClientTest.php index 4160e2b..b1b3d61 100644 --- a/tests/Storyblok/Integration/ClientTest.php +++ b/tests/Storyblok/Integration/ClientTest.php @@ -168,6 +168,10 @@ expect($body['story']['content']['Products'])->toHaveCount(1); expect($body['story']['content']['Products']['0']['content']['productname'])->toEqual('Shoe 001'); expect($body['story']['content']['Products']['0']['content']['ProductVariants'])->toBeArray()->toHaveLength(3); + expect($body['story']['content']['Products']['0']['content']['ProductVariants']['0'])->toBeArray(); + expect($body['story']['content']['Products']['0']['content']['ProductVariants']['0']['name'])->toEqual("Shoe 001 Blue"); + expect($body['story']['content']['Products']['0']['content']['ProductVariants']['0']['content'])->toBeArray(); + expect($body['story']['content']['Products']['0']['content']['ProductVariants']['0']['content']['VariantName'])->toEqual("Shoe 001 Blue");; expect($body['rels'])->toHaveLength(4); })->group('integration'); From 167b523cbafa06a057a7f10cb5ab566460002d69 Mon Sep 17 00:00:00 2001 From: Roberto Butti Date: Fri, 7 Apr 2023 13:31:33 +0200 Subject: [PATCH 05/14] Update ClientTest.php --- tests/Storyblok/Integration/ClientTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Storyblok/Integration/ClientTest.php b/tests/Storyblok/Integration/ClientTest.php index b1b3d61..7fa4f52 100644 --- a/tests/Storyblok/Integration/ClientTest.php +++ b/tests/Storyblok/Integration/ClientTest.php @@ -169,9 +169,9 @@ expect($body['story']['content']['Products']['0']['content']['productname'])->toEqual('Shoe 001'); expect($body['story']['content']['Products']['0']['content']['ProductVariants'])->toBeArray()->toHaveLength(3); expect($body['story']['content']['Products']['0']['content']['ProductVariants']['0'])->toBeArray(); - expect($body['story']['content']['Products']['0']['content']['ProductVariants']['0']['name'])->toEqual("Shoe 001 Blue"); + expect($body['story']['content']['Products']['0']['content']['ProductVariants']['0']['name'])->toEqual('Shoe 001 Blue'); expect($body['story']['content']['Products']['0']['content']['ProductVariants']['0']['content'])->toBeArray(); - expect($body['story']['content']['Products']['0']['content']['ProductVariants']['0']['content']['VariantName'])->toEqual("Shoe 001 Blue");; + expect($body['story']['content']['Products']['0']['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Shoe 001 Blue'); expect($body['rels'])->toHaveLength(4); })->group('integration'); From faea8acc74d97c534d2ecbd1f9bc9890f980a10c Mon Sep 17 00:00:00 2001 From: Roberto Butti Date: Fri, 7 Apr 2023 13:44:46 +0200 Subject: [PATCH 06/14] more test for second level (with > 50 rels and with < 50 rels) --- tests/Storyblok/Integration/ClientTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Storyblok/Integration/ClientTest.php b/tests/Storyblok/Integration/ClientTest.php index 7fa4f52..fc3974c 100644 --- a/tests/Storyblok/Integration/ClientTest.php +++ b/tests/Storyblok/Integration/ClientTest.php @@ -194,6 +194,7 @@ expect($body['story']['content']['ProductVariants'])->toHaveLength(2); expect($body['story']['content']['ProductVariants']['0']['name'])->toBeString(); expect($body['story']['content']['ProductVariants']['0']['name'])->toEqual('Bike 001 L'); + expect($body['story']['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Bike 001 L'); })->group('integration'); test('Integration: get one story with Resolved relations 2', function () { @@ -217,6 +218,7 @@ expect($body['story']['content']['MainProduct'])->toBeArray(); expect($body['story']['content']['MainProduct']['name'])->toEqual('Bike 001'); expect($body['story']['content']['MainProduct']['content']['ProductVariants'])->toBeArray()->toHaveLength(2); + expect($body['story']['content']['MainProduct']['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Bike 001 L'); })->group('integration'); test('Integration: get one story with Resolved relations 3', function () { From ed0be337ee0d5f12157a092779922a6f7fd427a7 Mon Sep 17 00:00:00 2001 From: Roberto Butti Date: Fri, 7 Apr 2023 15:26:10 +0200 Subject: [PATCH 07/14] Adding test for multiple stories with nested rrelations --- tests/Storyblok/Integration/ClientTest.php | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/Storyblok/Integration/ClientTest.php b/tests/Storyblok/Integration/ClientTest.php index fc3974c..39654f8 100644 --- a/tests/Storyblok/Integration/ClientTest.php +++ b/tests/Storyblok/Integration/ClientTest.php @@ -221,6 +221,32 @@ expect($body['story']['content']['MainProduct']['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Bike 001 L'); })->group('integration'); +test('Integration: get list of stories story with Resolved relations 2', function () { + unset($_GET['_storyblok_published']); + $client = new Client('HMqBPn2a92FjXYI3tQGDVQtt'); + + $key = 'stories/'; + $params = [ + 'starts_with' => 'categories/category-shoe', + 'content_type' => 'ProductCategory', + ]; + $client->editMode(); + $options = $client->getApiParameters(); + $client->resolveRelations( + 'ProductCategory.Products,Product.ProductVariants' + ); + $responses = $client->getStories($params); + $body = $responses->getBody(); + expect($body)->toHaveKeys(['rels', 'stories', 'cv', 'links']); + expect($body['stories'][0]['name'])->toEqual('Category Shoe 001'); + + expect($body['stories'][0]['content']['Products'])->toBeArray(); + expect($body['stories'][0]['content']['Products'])->toHaveLength(1); + expect($body['stories'][0]['content']['Products'][0]['name'])->toEqual('Shoe 001'); + expect($body['stories'][0]['content']['Products'][0]['content']['ProductVariants'])->toBeArray()->toHaveLength(3); + expect($body['stories'][0]['content']['Products'][0]['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Shoe 001 Blue'); +})->group('integration'); + test('Integration: get one story with Resolved relations 3', function () { unset($_GET['_storyblok_published']); $client = new Client('HMqBPn2a92FjXYI3tQGDVQtt'); From aea22658bebbcef0b257b2deaa1216f2c3fd721e Mon Sep 17 00:00:00 2001 From: Roberto Butti Date: Fri, 7 Apr 2023 17:34:03 +0200 Subject: [PATCH 08/14] add test for translation in nested relations --- tests/Storyblok/Integration/ClientTest.php | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/Storyblok/Integration/ClientTest.php b/tests/Storyblok/Integration/ClientTest.php index 39654f8..a1077b2 100644 --- a/tests/Storyblok/Integration/ClientTest.php +++ b/tests/Storyblok/Integration/ClientTest.php @@ -247,6 +247,33 @@ expect($body['stories'][0]['content']['Products'][0]['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Shoe 001 Blue'); })->group('integration'); +test('Integration: get list of stories -translations- story with Resolved relations 2', function () { + unset($_GET['_storyblok_published']); + $client = new Client('HMqBPn2a92FjXYI3tQGDVQtt'); + + $key = 'stories/'; + $params = [ + 'starts_with' => 'categories/category-shoe', + 'content_type' => 'ProductCategory', + ]; + $client->editMode(); + $client->language('it'); + $options = $client->getApiParameters(); + $client->resolveRelations( + 'ProductCategory.Products,Product.ProductVariants' + ); + $responses = $client->getStories($params); + $body = $responses->getBody(); + expect($body)->toHaveKeys(['rels', 'stories', 'cv', 'links']); + expect($body['stories'][0]['name'])->toEqual('Category Shoe 001'); + + expect($body['stories'][0]['content']['Products'])->toBeArray(); + expect($body['stories'][0]['content']['Products'])->toHaveLength(1); + expect($body['stories'][0]['content']['Products'][0]['name'])->toEqual('Shoe 001'); + expect($body['stories'][0]['content']['Products'][0]['content']['ProductVariants'])->toBeArray()->toHaveLength(3); + expect($body['stories'][0]['content']['Products'][0]['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Scarpa 001 Blu'); +})->group('integration'); + test('Integration: get one story with Resolved relations 3', function () { unset($_GET['_storyblok_published']); $client = new Client('HMqBPn2a92FjXYI3tQGDVQtt'); From 8e39c9d1f9e09f5078a4cbc33e91f80fc9e60fd7 Mon Sep 17 00:00:00 2001 From: Roberto Butti Date: Sat, 8 Apr 2023 09:54:13 +0200 Subject: [PATCH 09/14] Resolving nested relations, 2nd level - introducing level management for "enrich content" - adding tests: for < 50 rels (nested rels) - adding tests: (nested rels) - adding tests: stop resolving loop --- src/Storyblok/Client.php | 29 +++++++--------------- tests/Storyblok/Integration/ClientTest.php | 20 +++++++++++++++ 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/Storyblok/Client.php b/src/Storyblok/Client.php index d4886d5..c9872a9 100644 --- a/src/Storyblok/Client.php +++ b/src/Storyblok/Client.php @@ -734,29 +734,30 @@ function getResolvedLinks($data, array $queryString) * Enrich the Stories with resolved links and stories. * * @param array|\stdClass|string $data + * @param mixed $level * * @return array|string */ - public function enrichContent($data) + public function enrichContent($data, $level = 0) { $enrichedContent = $data; if (isset($data['component'])) { - if (!$this->isStopResolving($data)) { + if (!$this->isStopResolving($level)) { foreach ($data as $fieldName => $fieldValue) { $enrichedContent[$fieldName] = $this->insertRelations($data['component'], $fieldName, $fieldValue); $enrichedContent[$fieldName] = $this->insertLinks($enrichedContent[$fieldName]); - $enrichedContent[$fieldName] = $this->enrichContent($enrichedContent[$fieldName]); + $enrichedContent[$fieldName] = $this->enrichContent($enrichedContent[$fieldName], $level + 1); } } } elseif (\is_array($data)) { - if (!$this->isStopResolving($data)) { + if (!$this->isStopResolving($level)) { foreach ($data as $key => $value) { if (\is_string($value) && \array_key_exists($value, $this->resolvedRelations)) { if ('uuid' !== $key) { $enrichedContent[$key] = $this->resolvedRelations[$value]; } } else { - $enrichedContent[$key] = $this->enrichContent($value); + $enrichedContent[$key] = $this->enrichContent($value, $level + 1); } } } @@ -1082,23 +1083,11 @@ private function _getCacheKey($key = '') private function settingStopResolving(&$data) { - $data['_stopResolving'] = $this->getLevelResolving($data) + 1; + $data['_stopResolving'] = true; } - private function isStopResolving($data) + private function isStopResolving($level) { - return $this->getLevelResolving($data) > 2; - } - - private function getLevelResolving($data) - { - $level = 0; - if (!isset($data['_stopResolving'])) { - $level = 0; - } else { - $level = $data['_stopResolving']; - } - - return $level; + return $level > 3; } } diff --git a/tests/Storyblok/Integration/ClientTest.php b/tests/Storyblok/Integration/ClientTest.php index a1077b2..d658423 100644 --- a/tests/Storyblok/Integration/ClientTest.php +++ b/tests/Storyblok/Integration/ClientTest.php @@ -292,3 +292,23 @@ expect($body['story']['content']['ProductVariants']['0']['name'])->toBeString(); expect($body['story']['content']['ProductVariants']['1']['name'])->toBeString(); })->group('integration'); + +test('Integration: test stop resolving loop', function () { + unset($_GET['_storyblok_published']); + $client = new Client('HMqBPn2a92FjXYI3tQGDVQtt'); + $slug = 'testlevel'; + $key = 'stories/' . $slug; + $client->editMode(); + $options = $client->getApiParameters(); + $client->resolveRelations( + 'level-1.related,level-2.related' + ); + $responses = $client->getStoryBySlug($slug); + $body = $responses->getBody(); + expect($body)->toHaveKeys(['rels', 'story', 'cv', 'links']); + expect($body['story']['content']['related'])->toBeArray(); + expect($body['story']['content']['related']['name'])->toEqual('TestLevel2'); // FIRST LEVEL + expect($body['story']['content']['related']['_stopResolving'])->toEqual(1); + expect($body['story']['content']['related']['content']['related']['name'])->toEqual('TestLevel'); // SECOND LEVEL + expect($body['story']['content']['related']['content']['related']['content']['related'])->toBeString(); // NO RESOLVING THIRD LEVEL +})->group('integration'); From cda63f304ad88b5c8deea291c5cb43ba16a91d54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Gon=C3=A7alves?= Date: Tue, 11 Apr 2023 13:59:06 -0300 Subject: [PATCH 10/14] added rels and links to resolveRelations, increased level --- src/Storyblok/Client.php | 14 +++++++++++++- tests/Storyblok/Integration/ClientTest.php | 8 +++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Storyblok/Client.php b/src/Storyblok/Client.php index c9872a9..d2afc4a 100644 --- a/src/Storyblok/Client.php +++ b/src/Storyblok/Client.php @@ -887,6 +887,18 @@ private function enrichStories($data, $queryString) } $enrichedData['stories'] = $stories; } + + if (!empty($data['rels'])) { + foreach ($data['rels'] as $index => $rel) { + $enrichedData['rels'][$index] = $this->enrichContent($rel, -1); + } + } + + if (!empty($data['links'])) { + foreach ($data['links'] as $index => $rel) { + $enrichedData['links'][$index] = $this->enrichContent($rel, -1); + } + } return $enrichedData; } @@ -1088,6 +1100,6 @@ private function settingStopResolving(&$data) private function isStopResolving($level) { - return $level > 3; + return $level > 4; } } diff --git a/tests/Storyblok/Integration/ClientTest.php b/tests/Storyblok/Integration/ClientTest.php index d658423..c87585b 100644 --- a/tests/Storyblok/Integration/ClientTest.php +++ b/tests/Storyblok/Integration/ClientTest.php @@ -305,10 +305,12 @@ ); $responses = $client->getStoryBySlug($slug); $body = $responses->getBody(); + expect($body)->toHaveKeys(['rels', 'story', 'cv', 'links']); expect($body['story']['content']['related'])->toBeArray(); - expect($body['story']['content']['related']['name'])->toEqual('TestLevel2'); // FIRST LEVEL + expect($body['story']['content']['related']['name'])->toEqual('TestLevel2'); expect($body['story']['content']['related']['_stopResolving'])->toEqual(1); - expect($body['story']['content']['related']['content']['related']['name'])->toEqual('TestLevel'); // SECOND LEVEL - expect($body['story']['content']['related']['content']['related']['content']['related'])->toBeString(); // NO RESOLVING THIRD LEVEL + expect($body['story']['content']['related']['content']['related']['name'])->toEqual('TestLevel'); + expect($body['story']['content']['related']['content']['related']['content']['related'])->toBeArray(); + expect($body['story']['content']['related']['content']['related']['content']['related']['content']['related'])->toBeString(); })->group('integration'); From 215af729daa505c34d59bc058bb0764fc0b331d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Gon=C3=A7alves?= Date: Wed, 12 Apr 2023 09:53:12 -0300 Subject: [PATCH 11/14] syntax fix --- src/Storyblok/Client.php | 4 ++-- tests/Storyblok/Integration/ClientTest.php | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Storyblok/Client.php b/src/Storyblok/Client.php index d2afc4a..e140dda 100644 --- a/src/Storyblok/Client.php +++ b/src/Storyblok/Client.php @@ -887,13 +887,13 @@ private function enrichStories($data, $queryString) } $enrichedData['stories'] = $stories; } - + if (!empty($data['rels'])) { foreach ($data['rels'] as $index => $rel) { $enrichedData['rels'][$index] = $this->enrichContent($rel, -1); } } - + if (!empty($data['links'])) { foreach ($data['links'] as $index => $rel) { $enrichedData['links'][$index] = $this->enrichContent($rel, -1); diff --git a/tests/Storyblok/Integration/ClientTest.php b/tests/Storyblok/Integration/ClientTest.php index c87585b..ecbd7bc 100644 --- a/tests/Storyblok/Integration/ClientTest.php +++ b/tests/Storyblok/Integration/ClientTest.php @@ -68,17 +68,20 @@ $body = $response->getBody(); expect($body)->toBeArray() ->toHaveKey('story') - ->toHaveCount(4); + ->toHaveCount(4) + ; $story = $body['story']; expect($story)->toBeArray() ->toHaveKey('content') - ->toHaveCount(22); + ->toHaveCount(22) + ; $content = $story['content']; expect($content)->toBeArray() ->toHaveKey('_uid') ->toHaveKey('body') ->toHaveKey('component') - ->toHaveCount(4); + ->toHaveCount(4) + ; expect($content['_uid'])->toBeString(); expect($content['component'])->toBeString(); })->group('integration'); From c5d37dd695a7b46c8438e826515387638ba1d5b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Gon=C3=A7alves?= Date: Wed, 12 Apr 2023 20:01:34 -0300 Subject: [PATCH 12/14] fix singleOption inside multiOption --- src/Storyblok/Client.php | 23 +++++++++------- tests/Storyblok/Integration/ClientTest.php | 31 ++++++++++------------ 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/Storyblok/Client.php b/src/Storyblok/Client.php index e140dda..a7673ec 100644 --- a/src/Storyblok/Client.php +++ b/src/Storyblok/Client.php @@ -657,6 +657,7 @@ function getResolvedRelations($data, $queryString) $relations = $data['rels']; } elseif (isset($data['rel_uuids'])) { $relSize = \count($data['rel_uuids']); + $chunks = []; $chunkSize = 50; @@ -741,6 +742,7 @@ function getResolvedLinks($data, array $queryString) public function enrichContent($data, $level = 0) { $enrichedContent = $data; + if (isset($data['component'])) { if (!$this->isStopResolving($level)) { foreach ($data as $fieldName => $fieldValue) { @@ -752,10 +754,8 @@ public function enrichContent($data, $level = 0) } elseif (\is_array($data)) { if (!$this->isStopResolving($level)) { foreach ($data as $key => $value) { - if (\is_string($value) && \array_key_exists($value, $this->resolvedRelations)) { - if ('uuid' !== $key) { - $enrichedContent[$key] = $this->resolvedRelations[$value]; - } + if (\is_string($value) && \array_key_exists($value, $this->resolvedRelations) && 'uuid' !== $key) { + $enrichedContent[$key] = $this->resolvedRelations[$value]; } else { $enrichedContent[$key] = $this->enrichContent($value, $level + 1); } @@ -874,9 +874,9 @@ private function enrichStories($data, $queryString) $this->getResolvedLinks($data, $queryString); if (isset($data['story'])) { - if (isset($enrichedData['rel_uuids'])) { - $enrichedData['rels'] = $this->resolvedRelations; - } + // if (isset($enrichedData['rel_uuids'])) { + // $enrichedData['rels'] = $this->resolvedRelations; + // } $enrichedData['story']['content'] = $this->enrichContent($data['story']['content']); } elseif (isset($data['stories'])) { $stories = []; @@ -913,7 +913,6 @@ private function enrichStories($data, $queryString) private function insertRelations($component, $field, $value) { $filteredNode = $value; - if (isset($this->_relationsList[$component]) && \in_array($field, $this->_relationsList[$component], true)) { if (\is_string($value)) { if (isset($this->resolvedRelations[$value])) { @@ -923,6 +922,7 @@ private function insertRelations($component, $field, $value) } elseif (\is_array($value)) { $filteredNodeTemp = []; $resolved = false; + foreach ($value as $item) { if (\is_string($item) && isset($this->resolvedRelations[$item])) { $resolved = true; @@ -931,8 +931,13 @@ private function insertRelations($component, $field, $value) $filteredNodeTemp[] = $story; } } + if ($resolved) { - $filteredNode = $filteredNodeTemp; + if (1 === \count($filteredNodeTemp)) { + $filteredNode = $filteredNodeTemp[0]; + } else { + $filteredNode = $filteredNodeTemp; + } } } } diff --git a/tests/Storyblok/Integration/ClientTest.php b/tests/Storyblok/Integration/ClientTest.php index ecbd7bc..677ad27 100644 --- a/tests/Storyblok/Integration/ClientTest.php +++ b/tests/Storyblok/Integration/ClientTest.php @@ -142,7 +142,7 @@ ); $responses = $client->getStoryBySlug($slug); $body = $responses->getBody(); - expect($body)->toHaveKeys(['rels', 'story', 'cv', 'links']); + expect($body)->toHaveKeys(['rel_uuids', 'story', 'cv', 'links']); expect($body['story']['content']['FeaturedCategoryProducts'])->toBeArray(); expect($body['story']['content']['FeaturedCategoryProducts']['0']['name'])->toEqual('Category 001'); expect($body['story']['content']['FeaturedCategoryProducts']['1']['name'])->toEqual('Category A'); @@ -151,7 +151,7 @@ expect($body['story']['content']['MainProduct'])->toBeArray(); expect($body['story']['content']['MainProduct']['name'])->toEqual('Bike 001'); expect($body['story']['content']['MainProduct']['content']['ProductVariants'])->toBeArray()->toHaveLength(2); - expect($body['rels'])->toHaveLength(51); + expect($body['rel_uuids'])->toHaveLength(51); })->group('integration'); test('Integration: get one story with few resolved relations', function () { @@ -168,13 +168,12 @@ $body = $responses->getBody(); expect($body)->toHaveKeys(['rels', 'story', 'cv', 'links']); expect($body['story']['content']['Products'])->toBeArray(); - expect($body['story']['content']['Products'])->toHaveCount(1); - expect($body['story']['content']['Products']['0']['content']['productname'])->toEqual('Shoe 001'); - expect($body['story']['content']['Products']['0']['content']['ProductVariants'])->toBeArray()->toHaveLength(3); - expect($body['story']['content']['Products']['0']['content']['ProductVariants']['0'])->toBeArray(); - expect($body['story']['content']['Products']['0']['content']['ProductVariants']['0']['name'])->toEqual('Shoe 001 Blue'); - expect($body['story']['content']['Products']['0']['content']['ProductVariants']['0']['content'])->toBeArray(); - expect($body['story']['content']['Products']['0']['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Shoe 001 Blue'); + expect($body['story']['content']['Products']['content']['productname'])->toEqual('Shoe 001'); + expect($body['story']['content']['Products']['content']['ProductVariants'])->toBeArray()->toHaveLength(3); + expect($body['story']['content']['Products']['content']['ProductVariants']['0'])->toBeArray(); + expect($body['story']['content']['Products']['content']['ProductVariants']['0']['name'])->toEqual('Shoe 001 Blue'); + expect($body['story']['content']['Products']['content']['ProductVariants']['0']['content'])->toBeArray(); + expect($body['story']['content']['Products']['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Shoe 001 Blue'); expect($body['rels'])->toHaveLength(4); })->group('integration'); @@ -244,10 +243,9 @@ expect($body['stories'][0]['name'])->toEqual('Category Shoe 001'); expect($body['stories'][0]['content']['Products'])->toBeArray(); - expect($body['stories'][0]['content']['Products'])->toHaveLength(1); - expect($body['stories'][0]['content']['Products'][0]['name'])->toEqual('Shoe 001'); - expect($body['stories'][0]['content']['Products'][0]['content']['ProductVariants'])->toBeArray()->toHaveLength(3); - expect($body['stories'][0]['content']['Products'][0]['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Shoe 001 Blue'); + expect($body['stories'][0]['content']['Products']['name'])->toEqual('Shoe 001'); + expect($body['stories'][0]['content']['Products']['content']['ProductVariants'])->toBeArray()->toHaveLength(3); + expect($body['stories'][0]['content']['Products']['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Shoe 001 Blue'); })->group('integration'); test('Integration: get list of stories -translations- story with Resolved relations 2', function () { @@ -271,10 +269,9 @@ expect($body['stories'][0]['name'])->toEqual('Category Shoe 001'); expect($body['stories'][0]['content']['Products'])->toBeArray(); - expect($body['stories'][0]['content']['Products'])->toHaveLength(1); - expect($body['stories'][0]['content']['Products'][0]['name'])->toEqual('Shoe 001'); - expect($body['stories'][0]['content']['Products'][0]['content']['ProductVariants'])->toBeArray()->toHaveLength(3); - expect($body['stories'][0]['content']['Products'][0]['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Scarpa 001 Blu'); + expect($body['stories'][0]['content']['Products']['name'])->toEqual('Shoe 001'); + expect($body['stories'][0]['content']['Products']['content']['ProductVariants'])->toBeArray()->toHaveLength(3); + expect($body['stories'][0]['content']['Products']['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Scarpa 001 Blu'); })->group('integration'); test('Integration: get one story with Resolved relations 3', function () { From 9e383fe48c6b40512fe3481cb9e33538ca285826 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Gon=C3=A7alves?= Date: Thu, 13 Apr 2023 07:43:48 -0300 Subject: [PATCH 13/14] adjust on singleOption --- src/Storyblok/Client.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Storyblok/Client.php b/src/Storyblok/Client.php index a7673ec..5f50589 100644 --- a/src/Storyblok/Client.php +++ b/src/Storyblok/Client.php @@ -746,6 +746,10 @@ public function enrichContent($data, $level = 0) if (isset($data['component'])) { if (!$this->isStopResolving($level)) { foreach ($data as $fieldName => $fieldValue) { + if (isset($fieldValue['_stopResolving']) && $fieldValue['_stopResolving']) { + continue; + } + $enrichedContent[$fieldName] = $this->insertRelations($data['component'], $fieldName, $fieldValue); $enrichedContent[$fieldName] = $this->insertLinks($enrichedContent[$fieldName]); $enrichedContent[$fieldName] = $this->enrichContent($enrichedContent[$fieldName], $level + 1); @@ -754,8 +758,10 @@ public function enrichContent($data, $level = 0) } elseif (\is_array($data)) { if (!$this->isStopResolving($level)) { foreach ($data as $key => $value) { - if (\is_string($value) && \array_key_exists($value, $this->resolvedRelations) && 'uuid' !== $key) { - $enrichedContent[$key] = $this->resolvedRelations[$value]; + if (\is_string($value) && \array_key_exists($value, $this->resolvedRelations)) { + if ('uuid' !== $key) { + $enrichedContent[$key] = $this->resolvedRelations[$value]; + } } else { $enrichedContent[$key] = $this->enrichContent($value, $level + 1); } @@ -933,11 +939,7 @@ private function insertRelations($component, $field, $value) } if ($resolved) { - if (1 === \count($filteredNodeTemp)) { - $filteredNode = $filteredNodeTemp[0]; - } else { - $filteredNode = $filteredNodeTemp; - } + $filteredNode = $filteredNodeTemp; } } } From 33d17faefa32b9ac383fef0eff0367613abed286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Gon=C3=A7alves?= Date: Thu, 13 Apr 2023 07:49:51 -0300 Subject: [PATCH 14/14] adjusted tests and syntax --- src/Storyblok/Client.php | 2 +- tests/Storyblok/Integration/ClientTest.php | 27 ++++++++++++---------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/Storyblok/Client.php b/src/Storyblok/Client.php index 5f50589..56cd804 100644 --- a/src/Storyblok/Client.php +++ b/src/Storyblok/Client.php @@ -749,7 +749,7 @@ public function enrichContent($data, $level = 0) if (isset($fieldValue['_stopResolving']) && $fieldValue['_stopResolving']) { continue; } - + $enrichedContent[$fieldName] = $this->insertRelations($data['component'], $fieldName, $fieldValue); $enrichedContent[$fieldName] = $this->insertLinks($enrichedContent[$fieldName]); $enrichedContent[$fieldName] = $this->enrichContent($enrichedContent[$fieldName], $level + 1); diff --git a/tests/Storyblok/Integration/ClientTest.php b/tests/Storyblok/Integration/ClientTest.php index 677ad27..47fd96a 100644 --- a/tests/Storyblok/Integration/ClientTest.php +++ b/tests/Storyblok/Integration/ClientTest.php @@ -168,12 +168,13 @@ $body = $responses->getBody(); expect($body)->toHaveKeys(['rels', 'story', 'cv', 'links']); expect($body['story']['content']['Products'])->toBeArray(); - expect($body['story']['content']['Products']['content']['productname'])->toEqual('Shoe 001'); - expect($body['story']['content']['Products']['content']['ProductVariants'])->toBeArray()->toHaveLength(3); - expect($body['story']['content']['Products']['content']['ProductVariants']['0'])->toBeArray(); - expect($body['story']['content']['Products']['content']['ProductVariants']['0']['name'])->toEqual('Shoe 001 Blue'); - expect($body['story']['content']['Products']['content']['ProductVariants']['0']['content'])->toBeArray(); - expect($body['story']['content']['Products']['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Shoe 001 Blue'); + expect($body['story']['content']['Products'])->toHaveLength(1); + expect($body['story']['content']['Products'][0]['content']['productname'])->toEqual('Shoe 001'); + expect($body['story']['content']['Products'][0]['content']['ProductVariants'])->toBeArray()->toHaveLength(3); + expect($body['story']['content']['Products'][0]['content']['ProductVariants']['0'])->toBeArray(); + expect($body['story']['content']['Products'][0]['content']['ProductVariants']['0']['name'])->toEqual('Shoe 001 Blue'); + expect($body['story']['content']['Products'][0]['content']['ProductVariants']['0']['content'])->toBeArray(); + expect($body['story']['content']['Products'][0]['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Shoe 001 Blue'); expect($body['rels'])->toHaveLength(4); })->group('integration'); @@ -243,9 +244,10 @@ expect($body['stories'][0]['name'])->toEqual('Category Shoe 001'); expect($body['stories'][0]['content']['Products'])->toBeArray(); - expect($body['stories'][0]['content']['Products']['name'])->toEqual('Shoe 001'); - expect($body['stories'][0]['content']['Products']['content']['ProductVariants'])->toBeArray()->toHaveLength(3); - expect($body['stories'][0]['content']['Products']['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Shoe 001 Blue'); + expect($body['stories'][0]['content']['Products'])->toHaveLength(1); + expect($body['stories'][0]['content']['Products'][0]['name'])->toEqual('Shoe 001'); + expect($body['stories'][0]['content']['Products'][0]['content']['ProductVariants'])->toBeArray()->toHaveLength(3); + expect($body['stories'][0]['content']['Products'][0]['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Shoe 001 Blue'); })->group('integration'); test('Integration: get list of stories -translations- story with Resolved relations 2', function () { @@ -269,9 +271,10 @@ expect($body['stories'][0]['name'])->toEqual('Category Shoe 001'); expect($body['stories'][0]['content']['Products'])->toBeArray(); - expect($body['stories'][0]['content']['Products']['name'])->toEqual('Shoe 001'); - expect($body['stories'][0]['content']['Products']['content']['ProductVariants'])->toBeArray()->toHaveLength(3); - expect($body['stories'][0]['content']['Products']['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Scarpa 001 Blu'); + expect($body['stories'][0]['content']['Products'])->toHaveLength(1); + expect($body['stories'][0]['content']['Products'][0]['name'])->toEqual('Shoe 001'); + expect($body['stories'][0]['content']['Products'][0]['content']['ProductVariants'])->toBeArray()->toHaveLength(3); + expect($body['stories'][0]['content']['Products'][0]['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Scarpa 001 Blu'); })->group('integration'); test('Integration: get one story with Resolved relations 3', function () {