1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-12 16:14:16 +02:00

feat(fields): Add new field fetch for Entries API #492

This commit is contained in:
Awilum
2020-12-14 12:50:33 +03:00
parent 2ecb53811f
commit 6922e1b453
19 changed files with 188 additions and 8 deletions

View File

@@ -10,15 +10,13 @@ afterEach(function (): void {
filesystem()->directory(PATH['project'] . '/entries')->delete();
});
test('test fetchField', function () {
test('test fetchField for catalog', function () {
// Create catalog
flextype('entries')->create('catalog', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/catalog/entry.md')->get()));
flextype('entries')->create('catalog/bikes', ['title' => 'Bikes']);
flextype('entries')->create('catalog/bikes/gt', ['title' => 'GT', 'brand' => 'gt']);
flextype('entries')->create('catalog/bikes/norco', ['title' => 'Norco', 'brand' => 'norco']);
flextype('entries')->create('catalog/bikes', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/catalog/bikes/entry.md')->get()));
flextype('entries')->create('catalog/bikes/gt', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/catalog/bikes/gt/entry.md')->get()));
flextype('entries')->create('catalog/bikes/norco', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/catalog/bikes/norco/entry.md')->get()));
flextype('entries')->create('catalog/bikes/foo', ['title' => 'foo']);
flextype('entries')->create('catalog/bikes/foo/bar', ['title' => 'bar']);
@@ -31,7 +29,8 @@ test('test fetchField', function () {
flextype('entries')->create('banner', ['title' => 'Banner']);
$catalogSingle = flextype('entries')->fetch('catalog');
$this->assertEquals(15, $catalogSingle->count());
$this->assertEquals(16, $catalogSingle->count());
$this->assertEquals('Catalog', $catalogSingle['title']);
$this->assertEquals('catalog', $catalogSingle['id']);
$this->assertEquals(1, $catalogSingle['bikes']->count());
@@ -42,7 +41,7 @@ test('test fetchField', function () {
$this->assertEquals('30% off', $catalogSingle['discounts']['discounts/30-off']['title']);
$catalogSingleWithCollectionFalse = flextype('entries')->fetch('catalog', 'single');
$this->assertEquals(15, $catalogSingleWithCollectionFalse->count());
$this->assertEquals(16, $catalogSingleWithCollectionFalse->count());
$this->assertEquals('Catalog', $catalogSingleWithCollectionFalse['title']);
$this->assertEquals('catalog', $catalogSingleWithCollectionFalse['id']);
$this->assertEquals(1, $catalogSingleWithCollectionFalse['bikes']->count());
@@ -64,3 +63,42 @@ test('test fetchField', function () {
$this->assertEquals('Banner', $banner['title']);
$this->assertEquals('banner', $banner['id']);
});
test('test fetchField for blog', function () {
flextype('entries')->create('blog', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/blog/entry.md')->get()));
flextype('entries')->create('blog/post-1', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/blog/post-1/entry.md')->get()));
flextype('entries')->create('blog/post-2', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/blog/post-2/entry.md')->get()));
$blog = flextype('entries')->fetch('blog')->sortKeys();
$this->assertEquals(14, $blog->count());
});
test('test fetchField for albmus', function () {
flextype('entries')->create('root', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/root/entry.md')->get()));
flextype('entries')->create('albums', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/root/albums/entry.md')->get()));
flextype('entries')->create('albums/category-1', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/root/albums/category-1/entry.md')->get()));
flextype('entries')->create('albums/category-1/album-1', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/root/albums/category-1/album-1/entry.md')->get()));
flextype('entries')->create('banners', ['title' => 'Banners']);
flextype('entries')->create('banners/1', ['title' => 'Banner1']);
flextype('entries')->create('banners/2', ['title' => 'Banner2']);
});
test('test fetchField for long nested entries', function () {
flextype('entries')->create('level1', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/level1/entry.md')->get()));
flextype('entries')->create('level1/level2', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/level1/level2/entry.md')->get()));
flextype('entries')->create('level1/level2/level3', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/level1/level2/level3/entry.md')->get()));
flextype('entries')->create('level1/level2/level3/level4', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/level1/level2/level3/level4/entry.md')->get()));
$level = flextype('entries')->fetch('level1')->sortKeys();
$this->assertEquals(14, $level->count());
$this->assertEquals('level1/level2', $level['root']['id']);
$this->assertEquals('level1/level2/level3', $level['root']['root']['id']);
$this->assertEquals('level1/level2/level3/level4', $level['root']['root']['root']['id']);
});

View File

@@ -0,0 +1,7 @@
---
title: blog
fetch:
posts:
from: collection
id: blog
---

View File

@@ -0,0 +1,3 @@
---
title: Post 1
---

View File

@@ -0,0 +1,3 @@
---
title: Post 2
---

View File

@@ -0,0 +1,3 @@
---
title: Bikes
---

View File

@@ -0,0 +1,27 @@
---
title: GT
brand: gt
fetch:
discounts_available:
id: discounts
from: collection
options:
filter:
where:
-
key: category
operator: eq
value: bikes
label1:
from: single
id: discounts/50-off
options:
filter:
limit: 3
label2:
from: single
id: discounts/30-off
options:
filter:
limit: 2
---

View File

@@ -0,0 +1,13 @@
---
title: Norco
brand: norco
fetch:
label:
from: single
id: discounts/30-off
options: []
discounts:
from: collection
id: discounts
options: []
---

View File

@@ -2,6 +2,12 @@
title: Catalog
visibility: draft
fetch:
label1:
from: single
id: discounts/50-off
options:
filter:
limit: 4
bikes:
id: catalog/bikes
from: collection

View File

@@ -0,0 +1,7 @@
---
title: level1
fetch:
root:
from: single
id: level1/level2
---

View File

@@ -0,0 +1,7 @@
---
title: level2
fetch:
root:
from: single
id: level1/level2/level3
---

View File

@@ -0,0 +1,7 @@
---
title: level3
fetch:
root:
from: single
id: level1/level2/level3/level4
---

View File

@@ -0,0 +1,3 @@
---
title: level4
---

View File

@@ -0,0 +1,7 @@
---
title: blog
fetch:
collection:
id: blog
from: collection
---

View File

@@ -0,0 +1,3 @@
---
title: Post 1
---

View File

@@ -0,0 +1,3 @@
---
title: Post 2
---

View File

@@ -0,0 +1,21 @@
---
title: Album 1
fetch:
banner-single:
from: single
id: banners
banners-collection:
from: collection
id: banners
banners-collection-2:
from: collection
id: banners
options:
filter:
limit: 1
find:
depth: ">0"
banner-single-2:
from: single
id: banners/2
---

View File

@@ -0,0 +1,3 @@
---
title: Category1
---

View File

@@ -0,0 +1,3 @@
---
title: Albums
---

View File

@@ -0,0 +1,16 @@
---
title: Root
fetch:
single:
from: single
id: albums
collection:
from: collection
id: albums
collectionWithDepth:
from: collection
id: albums
options:
find:
depth: '>0'
---