diff --git a/src/functions/common/functions_directories.php b/src/functions/common/functions_directories.php index 25fcef8..4d03440 100644 --- a/src/functions/common/functions_directories.php +++ b/src/functions/common/functions_directories.php @@ -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); @@ -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; + } } /** @@ -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; diff --git a/src/model/Structure.php b/src/model/Structure.php index 1cead30..312346d 100644 --- a/src/model/Structure.php +++ b/src/model/Structure.php @@ -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) { diff --git a/tests/TestSuites/Common/function_directories/__snapshots__/Common_GetDirectoryEntryTest__testCanOverrideRevision__2.json b/tests/TestSuites/Common/function_directories/__snapshots__/Common_GetDirectoryEntryTest__testCanOverrideRevision__2.json new file mode 100644 index 0000000..a71c597 --- /dev/null +++ b/tests/TestSuites/Common/function_directories/__snapshots__/Common_GetDirectoryEntryTest__testCanOverrideRevision__2.json @@ -0,0 +1,7 @@ +{ + "id": 10099, + "name": "Test 3", + "url": "test-3\/", + "revision": 10003, + "published": false +} diff --git a/tests/TestSuites/Common/function_directories/__snapshots__/Common_GetDirectoryEntryTest__testCanOverrideRevision__3.json b/tests/TestSuites/Common/function_directories/__snapshots__/Common_GetDirectoryEntryTest__testCanOverrideRevision__3.json new file mode 100644 index 0000000..ef43006 --- /dev/null +++ b/tests/TestSuites/Common/function_directories/__snapshots__/Common_GetDirectoryEntryTest__testCanOverrideRevision__3.json @@ -0,0 +1,7 @@ +{ + "id": 10098, + "name": "Test 3", + "url": "test-3\/", + "revision": 10002, + "published": true +} diff --git a/tests/TestSuites/Common/function_directories/__snapshots__/Common_GetEntryTest__testCanOverrideRevision__2.json b/tests/TestSuites/Common/function_directories/__snapshots__/Common_GetEntryTest__testCanOverrideRevision__2.json new file mode 100644 index 0000000..a71c597 --- /dev/null +++ b/tests/TestSuites/Common/function_directories/__snapshots__/Common_GetEntryTest__testCanOverrideRevision__2.json @@ -0,0 +1,7 @@ +{ + "id": 10099, + "name": "Test 3", + "url": "test-3\/", + "revision": 10003, + "published": false +} diff --git a/tests/TestSuites/Common/function_directories/__snapshots__/Common_GetEntryTest__testCanOverrideRevision__3.json b/tests/TestSuites/Common/function_directories/__snapshots__/Common_GetEntryTest__testCanOverrideRevision__3.json new file mode 100644 index 0000000..ef43006 --- /dev/null +++ b/tests/TestSuites/Common/function_directories/__snapshots__/Common_GetEntryTest__testCanOverrideRevision__3.json @@ -0,0 +1,7 @@ +{ + "id": 10098, + "name": "Test 3", + "url": "test-3\/", + "revision": 10002, + "published": true +} diff --git a/tests/TestSuites/Common/function_directories/common.get_directory_entry.test.php b/tests/TestSuites/Common/function_directories/common.get_directory_entry.test.php index db1c198..3730c48 100644 --- a/tests/TestSuites/Common/function_directories/common.get_directory_entry.test.php +++ b/tests/TestSuites/Common/function_directories/common.get_directory_entry.test.php @@ -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, @@ -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)); } } diff --git a/tests/TestSuites/Common/function_directories/common.get_entry.test.php b/tests/TestSuites/Common/function_directories/common.get_entry.test.php index 876b00b..8d4e1f7 100644 --- a/tests/TestSuites/Common/function_directories/common.get_entry.test.php +++ b/tests/TestSuites/Common/function_directories/common.get_entry.test.php @@ -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, @@ -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)); } } diff --git a/tests/TestSuites/Common/function_directories/common.get_entry_content_list.test.php b/tests/TestSuites/Common/function_directories/common.get_entry_content_list.test.php index 0dcd81a..f68fb2d 100644 --- a/tests/TestSuites/Common/function_directories/common.get_entry_content_list.test.php +++ b/tests/TestSuites/Common/function_directories/common.get_entry_content_list.test.php @@ -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'], diff --git a/tests/TestSuites/Common/function_directories/common.get_entry_variants.test.php b/tests/TestSuites/Common/function_directories/common.get_entry_variants.test.php index 7980037..0b09574 100644 --- a/tests/TestSuites/Common/function_directories/common.get_entry_variants.test.php +++ b/tests/TestSuites/Common/function_directories/common.get_entry_variants.test.php @@ -19,6 +19,7 @@ protected function setUp(): void public function testGetEntryVariants (): void { NF::$cache->mockItem('entry/10000', [ + 'published' => true, 'variants' => [ ['variant1'], ['variant2']