Skip to content

Commit

Permalink
Merge pull request #87 from storyblok/fix/INT-859-level
Browse files Browse the repository at this point in the history
Fix/int 859 level
  • Loading branch information
joaokamun authored Apr 13, 2023
2 parents 5ad38c9 + 33d17fa commit f681de0
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 16 deletions.
65 changes: 51 additions & 14 deletions src/Storyblok/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ function getResolvedRelations($data, $queryString)
$relations = $data['rels'];
} elseif (isset($data['rel_uuids'])) {
$relSize = \count($data['rel_uuids']);

$chunks = [];
$chunkSize = 50;

Expand Down Expand Up @@ -734,27 +735,35 @@ 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 (!isset($data['_stopResolving'])) {
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]);
$enrichedContent[$fieldName] = $this->enrichContent($enrichedContent[$fieldName], $level + 1);
}
}
} elseif (\is_array($data)) {
if (!isset($data['_stopResolving'])) {
if (!$this->isStopResolving($level)) {
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);
$enrichedContent[$key] = $this->enrichContent($value, $level + 1);
}
}
}
Expand Down Expand Up @@ -871,9 +880,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 = [];
Expand All @@ -885,6 +894,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;
}

Expand All @@ -898,22 +919,28 @@ 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])) {
$filteredNode = $this->resolvedRelations[$value];
$filteredNode['_stopResolving'] = true;
$this->settingStopResolving($filteredNode);
}
} 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;
$this->settingStopResolving($story);
$filteredNodeTemp[] = $story;
}
}

if ($resolved) {
$filteredNode = $filteredNodeTemp;
}
}
}

Expand Down Expand Up @@ -1072,4 +1099,14 @@ private function _getCacheKey($key = '')
{
return hash('sha256', $key);
}

private function settingStopResolving(&$data)
{
$data['_stopResolving'] = true;
}

private function isStopResolving($level)
{
return $level > 4;
}
}
126 changes: 124 additions & 2 deletions tests/Storyblok/Integration/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -151,7 +151,53 @@
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 () {
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'])->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');

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');
expect($body['story']['content']['ProductVariants']['0']['content']['VariantName'])->toEqual('Bike 001 L');
})->group('integration');

test('Integration: get one story with Resolved relations 2', function () {
Expand All @@ -175,6 +221,60 @@
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 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 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 () {
Expand All @@ -195,3 +295,25 @@
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');
expect($body['story']['content']['related']['_stopResolving'])->toEqual(1);
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');

0 comments on commit f681de0

Please sign in to comment.