Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Entry retrieval issues #42

Merged
merged 4 commits into from
Jun 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/functions/common/functions_directories.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
* Get entry data
*
* @param int $id
* @return array
* @return array|null
*/
function get_directory_entry($id)
{
global $entry_override;
global $revision_override;

$id = convert_to_safe_string($id, 'int');
$entrydata = $revision_override ? null : NF::$cache->fetch("entry/$id");
$entrydata = (isset($entry_override) && $entry_override == $id && isset($revision_override)) ? null : NF::$cache->fetch("entry/$id");

if ($entrydata == null) {
$url = 'builder/structures/entry/' . $id;
$url .= $revision_override ? ('/revision/' . $revision_override) : '';
if (isset($entry_override) && $entry_override == $id && isset($revision_override)) {
$url .= '/revision/' . $revision_override;
}

try {
$entrydata = json_decode(NF::$capi->get($url)->getBody(), true);
Expand Down Expand Up @@ -54,7 +57,9 @@ function get_directory_entry($id)
NF::debug($entrydata, 'entry ' . $id . ' from memory');
}

return $entrydata;
if ($entrydata && ($entrydata['published']) || ($entry_override == $id && isset($revision_override))) {
return $entrydata;
}
}

/**
Expand Down Expand Up @@ -235,8 +240,10 @@ function get_entry_content_list($entry_id, $area, $content_type)
$data = $entry[$area];
$contentList = [];

foreach ($data as $item) {
$contentList[] = $item[$content_type];
if ($entry) {
foreach ($data as $item) {
$contentList[] = $item[$content_type];
}
}

return $contentList;
Expand Down
6 changes: 5 additions & 1 deletion src/model/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,11 @@ public static function find($id)
}

if (!$data) {
$response = NF::$capi->get('builder/structures/entry/' . $id . (isset($entry_override) ? "/revision/$revision_override" : ""));
$url = 'builder/structures/entry/' . $id;
if (isset($entry_override) && $entry_override == $id && isset($revision_override)) {
$url .= '/revision/' . $revision_override;
}
$response = NF::$capi->get($url);
$data = json_decode($response->getBody(), true);

if (!$data || $data['directory_id'] != $structureId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": 10099,
"name": "Test 3",
"url": "test-3\/",
"revision": 10003,
"published": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": 10098,
"name": "Test 3",
"url": "test-3\/",
"revision": 10002,
"published": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": 10099,
"name": "Test 3",
"url": "test-3\/",
"revision": 10003,
"published": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": 10098,
"name": "Test 3",
"url": "test-3\/",
"revision": 10002,
"published": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function testHandlesNotFound (): void
public function testCanOverrideRevision (): void
{
global $revision_override;
global $entry_override;

NF::$cache->mockItem('entry/10003', [
'id' => 10003,
Expand All @@ -69,9 +70,45 @@ public function testCanOverrideRevision (): void
'published' => false
])));

$entry_override = 10003;
$revision_override = 10001;

$this->assertMatchesJsonSnapshot(get_directory_entry(10003));

NF::$cache->mockItem('entry/10098', [
'id' => 10098,
'name' => 'Test 3',
'url' => 'test-3/',
'revision' => 10002,
'published' => true
]);

NF::$capi->mockResponse(new Response(200, ['Content-Type' => 'application/json'], json_encode([
'id' => 10099,
'name' => 'Test 3',
'url' => 'test-3/',
'revision' => 10003,
'published' => false
])));

$entry_override = 10099;
$revision_override = 10003;

$this->assertMatchesJsonSnapshot(get_directory_entry(10099));
$this->assertMatchesJsonSnapshot(get_directory_entry(10098));
}

public function testRespectsPublishedAttribute (): void
{
NF::$capi->mockResponse(new Response(200, ['Content-Type' => 'application/json'], json_encode([
'id' => 10004,
'name' => 'Test 4',
'url' => 'test-4/',
'revision' => 10000,
'published' => false
])));

$this->assertNull(get_directory_entry(10004));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function testHandlesNotFound (): void
public function testCanOverrideRevision (): void
{
global $revision_override;
global $entry_override;

NF::$cache->mockItem('entry/10003', [
'id' => 10003,
Expand All @@ -69,9 +70,45 @@ public function testCanOverrideRevision (): void
'published' => false
])));

$entry_override = 10003;
$revision_override = 10001;

$this->assertMatchesJsonSnapshot(get_entry(10003));

NF::$cache->mockItem('entry/10098', [
'id' => 10098,
'name' => 'Test 3',
'url' => 'test-3/',
'revision' => 10002,
'published' => true
]);

NF::$capi->mockResponse(new Response(200, ['Content-Type' => 'application/json'], json_encode([
'id' => 10099,
'name' => 'Test 3',
'url' => 'test-3/',
'revision' => 10003,
'published' => false
])));

$entry_override = 10099;
$revision_override = 10003;

$this->assertMatchesJsonSnapshot(get_entry(10099));
$this->assertMatchesJsonSnapshot(get_entry(10098));
}

public function testRespectsPublishedAttribute (): void
{
NF::$capi->mockResponse(new Response(200, ['Content-Type' => 'application/json'], json_encode([
'id' => 10004,
'name' => 'Test 4',
'url' => 'test-4/',
'revision' => 10000,
'published' => false
])));

$this->assertNull(get_entry(10004));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ protected function setUp(): void
public function testGetEntryContentList (): void
{
NF::$cache->mockItem('entry/10000', [
'published' => true,
'gallery' => [
['image' => 'image-1.png'],
['image' => 'image-2.png'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ protected function setUp(): void
public function testGetEntryVariants (): void
{
NF::$cache->mockItem('entry/10000', [
'published' => true,
'variants' => [
['variant1'],
['variant2']
Expand Down