From 8dd908198db8e25c96848ad593f369428c2bae4b Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 5 Aug 2022 15:21:03 +0100 Subject: [PATCH 01/46] Make Form submissions queryable and add facade --- config/stache.php | 5 + .../Forms/SubmissionQueryBuilder.php | 8 ++ src/Contracts/Forms/SubmissionRepository.php | 22 +++ src/Facades/FormSubmission.php | 29 ++++ src/Forms/Submission.php | 5 +- src/Providers/AppServiceProvider.php | 4 +- src/Stache/Query/SubmissionQueryBuilder.php | 135 ++++++++++++++++++ .../Repositories/SubmissionRepository.php | 75 ++++++++++ src/Stache/Stores/FormSubmissionStore.php | 57 ++++++++ src/Stache/Stores/SubmissionsStore.php | 28 ++++ 10 files changed, 366 insertions(+), 2 deletions(-) create mode 100644 src/Contracts/Forms/SubmissionQueryBuilder.php create mode 100644 src/Contracts/Forms/SubmissionRepository.php create mode 100644 src/Facades/FormSubmission.php create mode 100644 src/Stache/Query/SubmissionQueryBuilder.php create mode 100644 src/Stache/Repositories/SubmissionRepository.php create mode 100644 src/Stache/Stores/FormSubmissionStore.php create mode 100644 src/Stache/Stores/SubmissionsStore.php diff --git a/config/stache.php b/config/stache.php index 4f675ccfc5..7b4217c52a 100644 --- a/config/stache.php +++ b/config/stache.php @@ -84,6 +84,11 @@ 'directory' => base_path('users'), ], + 'form-submissions' => [ + 'class' => Stores\SubmissionsStore::class, + 'directory' => storage_path('statamic/forms'), + ], + ], /* diff --git a/src/Contracts/Forms/SubmissionQueryBuilder.php b/src/Contracts/Forms/SubmissionQueryBuilder.php new file mode 100644 index 0000000000..08e1fcfbd1 --- /dev/null +++ b/src/Contracts/Forms/SubmissionQueryBuilder.php @@ -0,0 +1,8 @@ + \Statamic\Stache\Repositories\NavigationRepository::class, \Statamic\Contracts\Assets\AssetRepository::class => \Statamic\Assets\AssetRepository::class, \Statamic\Contracts\Forms\FormRepository::class => \Statamic\Forms\FormRepository::class, + \Statamic\Contracts\Forms\SubmissionRepository::class => \Statamic\Stache\Repositories\SubmissionRepository::class, ])->each(function ($concrete, $abstract) { if (! $this->app->bound($abstract)) { Statamic::repository($abstract, $concrete); @@ -144,9 +145,10 @@ public function register() }); collect([ + 'assets' => fn () => Facades\Asset::query(), 'entries' => fn () => Facades\Entry::query(), + 'formsubmissions' => fn () => Facades\FormSubmission::query(), 'terms' => fn () => Facades\Term::query(), - 'assets' => fn () => Facades\Asset::query(), 'users' => fn () => Facades\User::query(), ])->each(function ($binding, $alias) { app()->bind('statamic.queries.'.$alias, $binding); diff --git a/src/Stache/Query/SubmissionQueryBuilder.php b/src/Stache/Query/SubmissionQueryBuilder.php new file mode 100644 index 0000000000..28597a2f63 --- /dev/null +++ b/src/Stache/Query/SubmissionQueryBuilder.php @@ -0,0 +1,135 @@ +forms[] = $operator; + + return $this; + } + + return parent::where($column, $operator, $value, $boolean); + } + + public function orWhere($column, $operator = null, $value = null) + { + return $this->where($column, $operator, $value, 'or'); + } + + public function whereIn($column, $values, $boolean = 'and') + { + if (in_array($column, ['form', 'forms'])) { + $this->forms = array_merge($this->forms ?? [], $values); + + return $this; + } + + return parent::whereIn($column, $values, $boolean); + } + + public function orWhereIn($column, $values) + { + return $this->whereIn($column, $values, 'or'); + } + + protected function collect($items = []) + { + return Collection::make($items); + } + + protected function getFilteredKeys() + { + $forms = empty($this->forms) + ? Facades\Form::all()->map->handle() + : $this->forms; + + return empty($this->wheres) + ? $this->getKeysFromForms($forms) + : $this->getKeysFromFormsWithWheres($forms, $this->wheres); + } + + protected function getKeysFromForms($forms) + { + return collect($forms)->flatMap(function ($form) { + $keys = $this->store->store($form)->paths()->keys(); + + return collect($keys)->map(function ($key) use ($form) { + return "{$form}::{$key}"; + }); + }); + } + + protected function getKeysFromFormsWithWheres($forms, $wheres) + { + return collect($wheres)->reduce(function ($ids, $where) use ($forms) { + $keys = $where['type'] == 'Nested' + ? $this->getKeysFromFormsWithWheres($forms, $where['query']->wheres) + : $this->getKeysFromFormsWithWhere($forms, $where); + + return $this->intersectKeysFromWhereClause($ids, $keys, $where); + }); + } + + protected function getKeysFromFormsWithWhere($forms, $where) + { + $items = collect($forms)->flatMap(function ($form) use ($where) { + return $this->getWhereColumnKeysFromStore($form, $where); + }); + + $method = 'filterWhere'.$where['type']; + + return $this->{$method}($items, $where)->keys(); + } + + protected function getOrderKeyValuesByIndex() + { + $forms = empty($this->forms) + ? Facades\Form::all()->map->handle() + : $this->forms; + + // First, we'll get the values from each index grouped by collection + $keys = collect($forms)->map(function ($form) { + $store = $this->store->store($form); + + return collect($this->orderBys)->mapWithKeys(function ($orderBy) use ($form, $store) { + $items = $store->index($orderBy->sort) + ->items() + ->mapWithKeys(function ($item, $key) use ($form) { + return ["{$form}::{$key}" => $item]; + })->all(); + + return [$orderBy->sort => $items]; + }); + }); + + // Then, we'll merge all the corresponding index values together from each collection. + return $keys->reduce(function ($carry, $form) { + foreach ($form as $sort => $values) { + $carry[$sort] = array_merge($carry[$sort] ?? [], $values); + } + + return $carry; + }, collect()); + } + + protected function getWhereColumnKeyValuesByIndex($column) + { + $forms = empty($this->forms) + ? Facades\Form::all()->map->handle() + : $this->forms; + + return collect($forms)->flatMap(function ($form) use ($column) { + return $this->getWhereColumnKeysFromStore($form, ['column' => $column]); + }); + } +} diff --git a/src/Stache/Repositories/SubmissionRepository.php b/src/Stache/Repositories/SubmissionRepository.php new file mode 100644 index 0000000000..c07f015f2d --- /dev/null +++ b/src/Stache/Repositories/SubmissionRepository.php @@ -0,0 +1,75 @@ +stache = $stache; + $this->store = $stache->store('form-submissions'); + } + + public function all(): Collection + { + return $this->query()->get(); + } + + public function whereForm(string $handle): Collection + { + return $this->query()->where('form', $handle)->get(); + } + + public function whereInForm(array $handles): Collection + { + return $this->query()->whereIn('form', $handles)->get(); + } + + public function find($id): ?Submission + { + return $this->query()->where('id', $id)->first(); + } + + public function save($submission) + { + $this->store + ->store($submission->form()) + ->save($submission); + } + + public function delete($submission) + { + $this->store + ->store($submission->form()) + ->delete($submission); + } + + public function query() + { + return new SubmissionQueryBuilder($this->store); + } + + public function make(): Submission + { + return app(Submission::class); + } + + public static function bindings(): array + { + return [ + Submission::class => \Statamic\Forms\Submission::class, + ]; + } +} diff --git a/src/Stache/Stores/FormSubmissionStore.php b/src/Stache/Stores/FormSubmissionStore.php new file mode 100644 index 0000000000..7468f6ec3e --- /dev/null +++ b/src/Stache/Stores/FormSubmissionStore.php @@ -0,0 +1,57 @@ +id(); + } + + // public function getItem($key) + // { + // dd($key); + // } + + public function getItemFilter(SplFileInfo $file) + { + $dir = str_finish($this->directory(), '/'); + $relative = str_after(Path::tidy($file->getPathname()), $dir); + + return $file->getExtension() === 'yaml' && substr_count($relative, '/') === 0; + } + + public function makeItemFromFile($path, $contents) + { + $handle = pathinfo($path, PATHINFO_FILENAME); + $data = YAML::file($path)->parse($contents); + + $form = pathinfo($path, PATHINFO_DIRNAME); + $form = str_after($form, $this->parent->directory()); + + $form = Form::find($form); + + $submission = FormSubmission::make() + ->id($handle) + ->form($form) + ->data($data); + + return $submission; + } +} diff --git a/src/Stache/Stores/SubmissionsStore.php b/src/Stache/Stores/SubmissionsStore.php new file mode 100644 index 0000000000..0ef9355534 --- /dev/null +++ b/src/Stache/Stores/SubmissionsStore.php @@ -0,0 +1,28 @@ +map(function ($form) { + return $this->store($form->handle()); + }); + } +} From 6adc349be44231b705b7991adc7faaf3aed1267d Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 5 Aug 2022 15:30:21 +0100 Subject: [PATCH 02/46] Remove extra line break --- src/Forms/Submission.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Forms/Submission.php b/src/Forms/Submission.php index ac2d4ec1a8..d45e8c00e9 100644 --- a/src/Forms/Submission.php +++ b/src/Forms/Submission.php @@ -19,7 +19,6 @@ use Statamic\Support\Arr; use Statamic\Support\Traits\FluentlyGetsAndSets; - class Submission implements SubmissionContract, Augmentable { use ContainsData, FluentlyGetsAndSets, HasAugmentedData, TracksQueriedColumns, TracksQueriedRelations; From 879c9d0764dacdbb2fda828265108b958c600d09 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 5 Aug 2022 16:07:20 +0100 Subject: [PATCH 03/46] Remove debug --- src/Stache/Stores/FormSubmissionStore.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Stache/Stores/FormSubmissionStore.php b/src/Stache/Stores/FormSubmissionStore.php index 7468f6ec3e..e42fb375b8 100644 --- a/src/Stache/Stores/FormSubmissionStore.php +++ b/src/Stache/Stores/FormSubmissionStore.php @@ -24,11 +24,6 @@ public function getItemKey($item) return $item->id(); } - // public function getItem($key) - // { - // dd($key); - // } - public function getItemFilter(SplFileInfo $file) { $dir = str_finish($this->directory(), '/'); From 33f052b3784a93f836721cf95ed95304da99a064 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 5 Aug 2022 16:57:33 +0100 Subject: [PATCH 04/46] Handle when $this->data() is a collection --- src/Data/ExistsAsFile.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Data/ExistsAsFile.php b/src/Data/ExistsAsFile.php index 7a549641c0..5bfeaca6b9 100644 --- a/src/Data/ExistsAsFile.php +++ b/src/Data/ExistsAsFile.php @@ -3,6 +3,7 @@ namespace Statamic\Data; use Illuminate\Support\Carbon; +use Illuminate\Support\Collection; use Statamic\Facades\File; use Statamic\Facades\YAML; use Statamic\Support\Arr; @@ -31,7 +32,12 @@ public function initialPath($path = null) public function fileData() { - return array_merge($this->data(), [ + $data = $this->data(); + if ($data instanceof Collection) { + $data = $data->all(); + } + + return array_merge($data, [ 'id' => $this->id(), ]); } From ce0344728a280bd5497c045a8a4d3dbc25a0b7f6 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 5 Aug 2022 16:57:54 +0100 Subject: [PATCH 05/46] Provide methods required by ExistsAsFile to Submission --- src/Forms/Submission.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Forms/Submission.php b/src/Forms/Submission.php index d45e8c00e9..04a231b547 100644 --- a/src/Forms/Submission.php +++ b/src/Forms/Submission.php @@ -6,6 +6,7 @@ use Statamic\Contracts\Data\Augmentable; use Statamic\Contracts\Forms\Submission as SubmissionContract; use Statamic\Data\ContainsData; +use Statamic\Data\ExistsAsFile; use Statamic\Data\HasAugmentedData; use Statamic\Data\TracksQueriedColumns; use Statamic\Data\TracksQueriedRelations; @@ -21,7 +22,7 @@ class Submission implements SubmissionContract, Augmentable { - use ContainsData, FluentlyGetsAndSets, HasAugmentedData, TracksQueriedColumns, TracksQueriedRelations; + use ContainsData, ExistsAsFile, FluentlyGetsAndSets, HasAugmentedData, TracksQueriedColumns, TracksQueriedRelations; /** * @var string @@ -187,6 +188,11 @@ public function delete() * @return string */ public function getPath() + { + return $this->path(); + } + + public function path() { return config('statamic.forms.submissions').'/'.$this->form()->handle().'/'.$this->id().'.yaml'; } From 1cc4320d7edb44d26f520ae18dfc95e201027666 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 5 Aug 2022 16:58:09 +0100 Subject: [PATCH 06/46] SubmissionStoreTest --- .../Stache/Stores/FormSubmissionStoreTest.php | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/Stache/Stores/FormSubmissionStoreTest.php diff --git a/tests/Stache/Stores/FormSubmissionStoreTest.php b/tests/Stache/Stores/FormSubmissionStoreTest.php new file mode 100644 index 0000000000..1963c9cd34 --- /dev/null +++ b/tests/Stache/Stores/FormSubmissionStoreTest.php @@ -0,0 +1,47 @@ +set('statamic.forms.submissions', __DIR__.'/../__fixtures__/content/submissions'); + + $this->parent = (new SubmissionsStore)->directory( + $this->directory = Path::tidy(__DIR__.'/../__fixtures__/content/submissions') + ); + + Stache::registerStore($this->parent); + + Stache::store('form-submissions')->directory($this->directory); + } + + /** @test */ + public function it_saves_to_disk() + { + $form = tap(Facades\Form::make('test_form'))->save(); + $submission = Facades\FormSubmission::make()->form($form); + $submission->set('title', 'Test'); + + $this->parent->store('test_form')->save($submission); + + $this->assertStringEqualsFile($path = $this->directory.'/test_form/'.$submission->id().'.yaml', $submission->fileContents()); + @unlink($path); + $this->assertFileNotExists($path); + + $this->assertEquals($path, $this->parent->store('test_form')->paths()->get($submission->id())); + } +} From 8bbe27817ea42b1434bf1b255ae53288628ddaab Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 5 Aug 2022 17:04:56 +0100 Subject: [PATCH 07/46] StyleCI --- src/Stache/Repositories/SubmissionRepository.php | 1 - src/Stache/Stores/FormSubmissionStore.php | 3 --- src/Stache/Stores/SubmissionsStore.php | 6 ------ 3 files changed, 10 deletions(-) diff --git a/src/Stache/Repositories/SubmissionRepository.php b/src/Stache/Repositories/SubmissionRepository.php index c07f015f2d..e513fb1f32 100644 --- a/src/Stache/Repositories/SubmissionRepository.php +++ b/src/Stache/Repositories/SubmissionRepository.php @@ -7,7 +7,6 @@ use Statamic\Contracts\Forms\SubmissionRepository as RepositoryContract; use Statamic\Stache\Query\SubmissionQueryBuilder; use Statamic\Stache\Stache; -use Statamic\Support\Str; class SubmissionRepository implements RepositoryContract { diff --git a/src/Stache/Stores/FormSubmissionStore.php b/src/Stache/Stores/FormSubmissionStore.php index e42fb375b8..14e8b157e5 100644 --- a/src/Stache/Stores/FormSubmissionStore.php +++ b/src/Stache/Stores/FormSubmissionStore.php @@ -5,10 +5,7 @@ use Statamic\Facades\Form; use Statamic\Facades\FormSubmission; use Statamic\Facades\Path; -use Statamic\Facades\Site; -use Statamic\Facades\Stache; use Statamic\Facades\YAML; -use Statamic\Stache\Indexes; use Statamic\Stache\Indexes\Value; use Symfony\Component\Finder\SplFileInfo; diff --git a/src/Stache/Stores/SubmissionsStore.php b/src/Stache/Stores/SubmissionsStore.php index 0ef9355534..a53f4c3a18 100644 --- a/src/Stache/Stores/SubmissionsStore.php +++ b/src/Stache/Stores/SubmissionsStore.php @@ -3,12 +3,6 @@ namespace Statamic\Stache\Stores; use Statamic\Facades\Form; -use Statamic\Facades\FormSubmission; -use Statamic\Facades\Path; -use Statamic\Facades\Site; -use Statamic\Facades\Stache; -use Statamic\Facades\YAML; -use Symfony\Component\Finder\SplFileInfo; class SubmissionsStore extends AggregateStore { From dd98c50d27f22c67980c9f048d2041bb34f9da2f Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 5 Aug 2022 18:39:25 +0100 Subject: [PATCH 08/46] Revert unintentional config change --- config/stache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/stache.php b/config/stache.php index 7b4217c52a..01a05014b8 100644 --- a/config/stache.php +++ b/config/stache.php @@ -86,7 +86,7 @@ 'form-submissions' => [ 'class' => Stores\SubmissionsStore::class, - 'directory' => storage_path('statamic/forms'), + 'directory' => storage_path('forms'), ], ], From ecd91afbedcb1810efa3969a84b6964e076106fb Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 5 Aug 2022 19:13:22 +0100 Subject: [PATCH 09/46] Update Submission class to use stache directory --- src/Forms/Submission.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Forms/Submission.php b/src/Forms/Submission.php index 04a231b547..3ca4aa626a 100644 --- a/src/Forms/Submission.php +++ b/src/Forms/Submission.php @@ -15,6 +15,7 @@ use Statamic\Events\SubmissionSaved; use Statamic\Events\SubmissionSaving; use Statamic\Facades\File; +use Statamic\Facades\Stache; use Statamic\Facades\YAML; use Statamic\Forms\Uploaders\AssetsUploader; use Statamic\Support\Arr; @@ -194,7 +195,11 @@ public function getPath() public function path() { - return config('statamic.forms.submissions').'/'.$this->form()->handle().'/'.$this->id().'.yaml'; + return vsprintf('%s/%s/%s.yaml', [ + rtrim(Stache::store('form-submissions')->directory(), '/'), + $this->form()->handle(), + $this->id(), + ]); } /** From 0ea42ca47cc7259789814f5af0d98ad84ba93b84 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 5 Aug 2022 19:13:47 +0100 Subject: [PATCH 10/46] SubmissionQueryBuilderTests I've left date tests in, but skipped, we should revisit --- tests/Forms/SubmissionQueryBuilderTest.php | 407 +++++++++++++++++++++ 1 file changed, 407 insertions(+) create mode 100644 tests/Forms/SubmissionQueryBuilderTest.php diff --git a/tests/Forms/SubmissionQueryBuilderTest.php b/tests/Forms/SubmissionQueryBuilderTest.php new file mode 100644 index 0000000000..4242ac16e9 --- /dev/null +++ b/tests/Forms/SubmissionQueryBuilderTest.php @@ -0,0 +1,407 @@ +save(); + FormSubmission::make()->form($form)->data(['a' => true])->save(); + FormSubmission::make()->form($form)->data(['b' => true])->save(); + FormSubmission::make()->form($form)->data(['c' => true])->save(); + + $submissions = FormSubmission::whereForm('test'); + $this->assertInstanceOf(Collection::class, $submissions); + $this->assertEveryItemIsInstanceOf(Submission::class, $submissions); + } + + /** @test */ + public function it_filters_using_wheres() + { + $form = tap(Form::make('test'))->save(); + FormSubmission::make()->form($form)->data(['id' => 'a', 'test' => 'foo'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'b', 'test' => 'bar'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'c', 'test' => 'foo'])->save(); + + $submissions = FormSubmission::query()->where('test', 'foo')->get(); + $this->assertEquals(['a', 'c'], $submissions->map->get('id')->sort()->values()->all()); + } + + /** @test */ + public function it_filters_using_or_wheres() + { + $form = tap(Form::make('test'))->save(); + FormSubmission::make()->form($form)->data(['id' => 'a', 'test' => 'foo'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'b', 'test' => 'bar'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'c', 'test' => 'baz'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'd', 'test' => 'foo'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'e', 'test' => 'raz'])->save(); + + $submissions = FormSubmission::query()->where('test', 'foo')->orWhere('test', 'bar')->get(); + $this->assertEquals(['a', 'd', 'b'], $submissions->map->get('id')->values()->all()); + } + + /** @test */ + public function it_filters_using_or_where_ins() + { + $form = tap(Form::make('test'))->save(); + FormSubmission::make()->form($form)->data(['id' => 'a', 'test' => 'foo'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'b', 'test' => 'bar'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'c', 'test' => 'baz'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'd', 'test' => 'foo'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'e', 'test' => 'raz'])->save(); + + $submissions = FormSubmission::query()->whereIn('test', ['foo', 'bar'])->orWhereIn('test', ['foo', 'raz'])->get(); + + $this->assertEquals(['a', 'b', 'd', 'e'], $submissions->map->get('id')->values()->all()); + } + + /** @test **/ + public function it_filters_using_or_where_not_ins() + { + $form = tap(Form::make('test'))->save(); + FormSubmission::make()->form($form)->data(['id' => 'a', 'test' => 'foo'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'b', 'test' => 'bar'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'c', 'test' => 'baz'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'd', 'test' => 'foo'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'e', 'test' => 'raz'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'f', 'test' => 'taz'])->save(); + + $submissions = FormSubmission::query()->whereNotIn('test', ['foo', 'bar'])->orWhereNotIn('test', ['foo', 'raz'])->get(); + + $this->assertEquals(['c', 'f'], $submissions->map->get('id')->values()->all()); + } + + /** @test */ + public function it_filters_using_nested_wheres() + { + $form = tap(Form::make('test'))->save(); + FormSubmission::make()->form($form)->data(['id' => 'a', 'test' => 'foo'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'b', 'test' => 'bar'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'c', 'test' => 'baz'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'd', 'test' => 'foo'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'e', 'test' => 'raz'])->save(); + + $submissions = FormSubmission::query() + ->where(function ($query) { + $query->where('test', 'foo'); + }) + ->orWhere(function ($query) { + $query->where('test', 'baz'); + }) + ->orWhere('test', 'raz') + ->get(); + + $this->assertCount(4, $submissions); + $this->assertEquals(['a', 'c', 'd', 'e'], $submissions->map->get('id')->sort()->values()->all()); + } + + /** @test */ + public function it_filters_using_nested_where_ins() + { + $form = tap(Form::make('test'))->save(); + FormSubmission::make()->form($form)->data(['id' => 'a', 'test' => 'foo'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'b', 'test' => 'bar'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'c', 'test' => 'baz'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'd', 'test' => 'foo'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'e', 'test' => 'raz'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'f', 'test' => 'chaz'])->save(); + + $submissions = FormSubmission::query() + ->where(function ($query) { + $query->where('test', 'foo'); + }) + ->orWhere(function ($query) { + $query->whereIn('test', ['baz', 'raz']); + }) + ->orWhere('test', 'chaz') + ->get(); + + $this->assertCount(5, $submissions); + $this->assertEquals(['a', 'c', 'd', 'e', 'f'], $submissions->map->get('id')->sort()->values()->all()); + } + + /** @test */ + public function it_filters_using_nested_where_not_ins() + { + $form = tap(Form::make('test'))->save(); + FormSubmission::make()->form($form)->data(['id' => 'a', 'test' => 'foo'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'b', 'test' => 'bar'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'c', 'test' => 'baz'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'd', 'test' => 'foo'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'e', 'test' => 'raz'])->save(); + + $submissions = FormSubmission::query() + ->where('test', 'foo') + ->orWhere(function ($query) { + $query->whereNotIn('test', ['baz', 'raz']); + }) + ->get(); + + $this->assertCount(3, $submissions); + $this->assertEquals(['a', 'b', 'd'], $submissions->map->get('id')->sort()->values()->all()); + } + + /** @test */ + public function it_sorts() + { + $form = tap(Form::make('test'))->save(); + FormSubmission::make()->form($form)->data(['id' => 'a', 'test' => 4])->save(); + FormSubmission::make()->form($form)->data(['id' => 'b', 'test' => 2])->save(); + FormSubmission::make()->form($form)->data(['id' => 'c', 'test' => 1])->save(); + FormSubmission::make()->form($form)->data(['id' => 'd', 'test' => 5])->save(); + FormSubmission::make()->form($form)->data(['id' => 'e', 'test' => 3])->save(); + + $submissions = FormSubmission::query()->orderBy('test')->get(); + $this->assertEquals(['c', 'b', 'e', 'a', 'd'], $submissions->map->get('id')->all()); + } + + /** @test **/ + public function submissions_are_found_using_where_column() + { + $form = tap(Form::make('test'))->save(); + FormSubmission::make()->form($form)->data(['id' => 'a', 'title' => 'Post 1', 'other_title' => 'Not Post 1'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'b', 'title' => 'Post 2', 'other_title' => 'Not Post 2'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'c', 'title' => 'Post 3', 'other_title' => 'Post 3'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'd', 'title' => 'Post 4', 'other_title' => 'Post 4'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'e', 'title' => 'Post 5', 'other_title' => 'Not Post 5'])->save(); + + $submissions = FormSubmission::query()->whereColumn('title', 'other_title')->get(); + + $this->assertCount(2, $submissions); + $this->assertEquals(['c', 'd'], $submissions->map->get('id')->all()); + + $submissions = FormSubmission::query()->whereColumn('title', '!=', 'other_title')->get(); + + $this->assertCount(3, $submissions); + $this->assertEquals(['a', 'b', 'e'], $submissions->map->get('id')->all()); + } + + /** @test **/ + public function entries_are_found_using_where_date() + { + $this->markTestSkipped(); // for now + + $this->createWhereDateTestTerms(); + + $entries = FormSubmission::query()->whereDate('test_date', '2021-11-15')->get(); + + $this->assertCount(2, $entries); + $this->assertEquals(['Post 1', 'Post 3'], $entries->map->title->all()); + + $entries = FormSubmission::query()->whereDate('test_date', 1637000264)->get(); + + $this->assertCount(2, $entries); + $this->assertEquals(['Post 1', 'Post 3'], $entries->map->title->all()); + + $entries = FormSubmission::query()->whereDate('test_date', '>=', '2021-11-15')->get(); + + $this->assertCount(2, $entries); + $this->assertEquals(['Post 1', 'Post 3'], $entries->map->title->all()); + } + + /** @test **/ + public function entries_are_found_using_where_month() + { + $this->markTestSkipped(); // for now + + $this->createWhereDateTestTerms(); + + $entries = FormSubmission::query()->whereMonth('test_date', 11)->get(); + + $this->assertCount(3, $entries); + $this->assertEquals(['Post 1', 'Post 2', 'Post 3'], $entries->map->title->all()); + + $entries = FormSubmission::query()->whereMonth('test_date', '<', 11)->get(); + + $this->assertCount(1, $entries); + $this->assertEquals(['Post 4'], $entries->map->title->all()); + } + + /** @test **/ + public function entries_are_found_using_where_day() + { + $this->markTestSkipped(); // for now + + $this->createWhereDateTestTerms(); + + $entries = FormSubmission::query()->whereDay('test_date', 15)->get(); + + $this->assertCount(2, $entries); + $this->assertEquals(['Post 1', 'Post 3'], $entries->map->title->all()); + + $entries = FormSubmission::query()->whereDay('test_date', '<', 15)->get(); + + $this->assertCount(2, $entries); + $this->assertEquals(['Post 2', 'Post 4'], $entries->map->title->all()); + } + + /** @test **/ + public function entries_are_found_using_where_year() + { + $this->markTestSkipped(); // for now + + $this->createWhereDateTestTerms(); + + $entries = FormSubmission::query()->whereYear('test_date', 2021)->get(); + + $this->assertCount(3, $entries); + $this->assertEquals(['Post 1', 'Post 2', 'Post 3'], $entries->map->title->all()); + + $entries = FormSubmission::query()->whereYear('test_date', '<', 2021)->get(); + + $this->assertCount(1, $entries); + $this->assertEquals(['Post 4'], $entries->map->title->all()); + } + + /** @test **/ + public function entries_are_found_using_where_time() + { + $this->markTestSkipped(); // for now + + $this->createWhereDateTestTerms(); + + $entries = FormSubmission::query()->whereTime('test_date', '09:00')->get(); + + $this->assertCount(1, $entries); + $this->assertEquals(['Post 2'], $entries->map->title->all()); + + $entries = FormSubmission::query()->whereTime('test_date', '>', '09:00')->get(); + + $this->assertCount(2, $entries); + $this->assertEquals(['Post 1', 'Post 4'], $entries->map->title->all()); + } + + private function createWhereDateTestTerms() + { + $blueprint = Blueprint::makeFromFields(['test_date' => ['type' => 'date', 'time_enabled' => true]]); + Blueprint::shouldReceive('in')->with('taxonomies/tags')->andReturn(collect(['tags' => $blueprint])); + + $form = tap(Form::make('test'))->save(); + FormSubmission::make()->form($form)->data(['title' => 'Post 1', 'test_date' => '2021-11-15 20:31:04'])->save(); + FormSubmission::make()->form($form)->data(['title' => 'Post 2', 'test_date' => '2021-11-14 09:00:00'])->save(); + FormSubmission::make()->form($form)->data(['title' => 'Post 3', 'test_date' => '2021-11-15 00:00:00'])->save(); + FormSubmission::make()->form($form)->data(['title' => 'Post 4', 'test_date' => '2020-09-13 14:44:24'])->save(); + FormSubmission::make()->form($form)->data(['title' => 'Post 5', 'test_date' => null])->save(); + } + + /** @test **/ + public function submissions_are_found_using_where_json_contains() + { + $form = tap(Form::make('test'))->save(); + FormSubmission::make()->form($form)->data(['id' => '1', 'test_taxonomy' => ['taxonomy-1', 'taxonomy-2']])->save(); + FormSubmission::make()->form($form)->data(['id' => '2', 'test_taxonomy' => ['taxonomy-3']])->save(); + FormSubmission::make()->form($form)->data(['id' => '3', 'test_taxonomy' => ['taxonomy-1', 'taxonomy-3']])->save(); + FormSubmission::make()->form($form)->data(['id' => '4', 'test_taxonomy' => ['taxonomy-3', 'taxonomy-4']])->save(); + FormSubmission::make()->form($form)->data(['id' => '5', 'test_taxonomy' => ['taxonomy-5']])->save(); + + $entries = FormSubmission::query()->whereJsonContains('test_taxonomy', ['taxonomy-1', 'taxonomy-5'])->get(); + + $this->assertCount(3, $entries); + $this->assertEquals(['1', '3', '5'], $entries->map->get('id')->all()); + + $entries = FormSubmission::query()->whereJsonContains('test_taxonomy', 'taxonomy-1')->get(); + + $this->assertCount(2, $entries); + $this->assertEquals(['1', '3'], $entries->map->get('id')->all()); + } + + /** @test **/ + public function submissions_are_found_using_where_json_doesnt_contain() + { + $form = tap(Form::make('test'))->save(); + FormSubmission::make()->form($form)->data(['id' => '1', 'test_taxonomy' => ['taxonomy-1', 'taxonomy-2']])->save(); + FormSubmission::make()->form($form)->data(['id' => '2', 'test_taxonomy' => ['taxonomy-3']])->save(); + FormSubmission::make()->form($form)->data(['id' => '3', 'test_taxonomy' => ['taxonomy-1', 'taxonomy-3']])->save(); + FormSubmission::make()->form($form)->data(['id' => '4', 'test_taxonomy' => ['taxonomy-3', 'taxonomy-4']])->save(); + FormSubmission::make()->form($form)->data(['id' => '5', 'test_taxonomy' => ['taxonomy-5']])->save(); + + $entries = FormSubmission::query()->whereJsonDoesntContain('test_taxonomy', ['taxonomy-1'])->get(); + + $this->assertCount(3, $entries); + $this->assertEquals(['2', '4', '5'], $entries->map->get('id')->all()); + + $entries = FormSubmission::query()->whereJsonDoesntContain('test_taxonomy', 'taxonomy-1')->get(); + + $this->assertCount(3, $entries); + $this->assertEquals(['2', '4', '5'], $entries->map->get('id')->all()); + } + + /** @test **/ + public function submissions_are_found_using_or_where_json_contains() + { + $form = tap(Form::make('test'))->save(); + FormSubmission::make()->form($form)->data(['id' => '1', 'test_taxonomy' => ['taxonomy-1', 'taxonomy-2']])->save(); + FormSubmission::make()->form($form)->data(['id' => '2', 'test_taxonomy' => ['taxonomy-3']])->save(); + FormSubmission::make()->form($form)->data(['id' => '3', 'test_taxonomy' => ['taxonomy-1', 'taxonomy-3']])->save(); + FormSubmission::make()->form($form)->data(['id' => '4', 'test_taxonomy' => ['taxonomy-3', 'taxonomy-4']])->save(); + FormSubmission::make()->form($form)->data(['id' => '5', 'test_taxonomy' => ['taxonomy-5']])->save(); + + $entries = FormSubmission::query()->whereJsonContains('test_taxonomy', ['taxonomy-1'])->orWhereJsonContains('test_taxonomy', ['taxonomy-5'])->get(); + + $this->assertCount(3, $entries); + $this->assertEquals(['1', '3', '5'], $entries->map->get('id')->all()); + } + + /** @test **/ + public function submissions_are_found_using_or_where_json_doesnt_contain() + { + $form = tap(Form::make('test'))->save(); + FormSubmission::make()->form($form)->data(['id' => '1', 'test_taxonomy' => ['taxonomy-1', 'taxonomy-2']])->save(); + FormSubmission::make()->form($form)->data(['id' => '2', 'test_taxonomy' => ['taxonomy-3']])->save(); + FormSubmission::make()->form($form)->data(['id' => '3', 'test_taxonomy' => ['taxonomy-1', 'taxonomy-3']])->save(); + FormSubmission::make()->form($form)->data(['id' => '4', 'test_taxonomy' => ['taxonomy-3', 'taxonomy-4']])->save(); + FormSubmission::make()->form($form)->data(['id' => '5', 'test_taxonomy' => ['taxonomy-5']])->save(); + + $entries = FormSubmission::query()->whereJsonContains('test_taxonomy', ['taxonomy-1'])->orWhereJsonDoesntContain('test_taxonomy', ['taxonomy-5'])->get(); + + $this->assertCount(4, $entries); + $this->assertEquals(['1', '3', '2', '4'], $entries->map->get('id')->all()); + } + + /** @test **/ + public function submissions_are_found_using_where_json_length() + { + $form = tap(Form::make('test'))->save(); + FormSubmission::make()->form($form)->data(['id' => '1', 'test_taxonomy' => ['taxonomy-1', 'taxonomy-2']])->save(); + FormSubmission::make()->form($form)->data(['id' => '2', 'test_taxonomy' => ['taxonomy-3']])->save(); + FormSubmission::make()->form($form)->data(['id' => '3', 'test_taxonomy' => ['taxonomy-1', 'taxonomy-3']])->save(); + FormSubmission::make()->form($form)->data(['id' => '4', 'test_taxonomy' => ['taxonomy-3', 'taxonomy-4']])->save(); + FormSubmission::make()->form($form)->data(['id' => '5', 'test_taxonomy' => ['taxonomy-5']])->save(); + + $entries = FormSubmission::query()->whereJsonLength('test_taxonomy', 1)->get(); + + $this->assertCount(2, $entries); + $this->assertEquals(['2', '5'], $entries->map->get('id')->all()); + } + + /** @test */ + public function submissions_are_found_using_offset() + { + $form = tap(Form::make('test'))->save(); + FormSubmission::make()->form($form)->data(['id' => 'a', ])->save(); + FormSubmission::make()->form($form)->data(['id' => 'b', ])->save(); + FormSubmission::make()->form($form)->data(['id' => 'c', ])->save(); + + $submissions = FormSubmission::query()->get(); + $this->assertEquals(['a', 'b', 'c'], $submissions->map->get('id')->all()); + + $submissions = FormSubmission::query()->offset(1)->get(); + $this->assertEquals(['b', 'c'], $submissions->map->get('id')->all()); + } +} From 653c5100582288a1fe09c5eb0ec445983b7fcda8 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 5 Aug 2022 19:14:14 +0100 Subject: [PATCH 11/46] Update TestCase to ensure tests run correctly --- tests/TestCase.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/TestCase.php b/tests/TestCase.php index 0523531c89..680c3504e4 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -75,6 +75,7 @@ protected function resolveApplicationConfiguration($app) $app['config']->set("statamic.$config", require(__DIR__."/../config/{$config}.php")); } + $app['config']->set('statamic.forms.submissions', __DIR__.'/__fixtures__/content/submissions'); $app['config']->set('statamic.antlers.version', 'runtime'); } @@ -104,6 +105,7 @@ protected function getEnvironmentSetUp($app) $app['config']->set('statamic.stache.stores.asset-containers.directory', __DIR__.'/__fixtures__/content/assets'); $app['config']->set('statamic.stache.stores.nav-trees.directory', __DIR__.'/__fixtures__/content/structures/navigation'); $app['config']->set('statamic.stache.stores.collection-trees.directory', __DIR__.'/__fixtures__/content/structures/collections'); + $app['config']->set('statamic.stache.stores.form-submissions.directory', __DIR__.'/__fixtures__/content/submissions'); $app['config']->set('statamic.api.enabled', true); $app['config']->set('statamic.graphql.enabled', true); From 1a9ef3fcd4149f6504bff24488de4c8e15f9fa97 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 5 Aug 2022 19:15:06 +0100 Subject: [PATCH 12/46] StyleCI --- tests/Forms/SubmissionQueryBuilderTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/Forms/SubmissionQueryBuilderTest.php b/tests/Forms/SubmissionQueryBuilderTest.php index 4242ac16e9..da6b04829c 100644 --- a/tests/Forms/SubmissionQueryBuilderTest.php +++ b/tests/Forms/SubmissionQueryBuilderTest.php @@ -6,10 +6,6 @@ use Statamic\Contracts\Forms\Submission; use Statamic\Facades\Form; use Statamic\Facades\FormSubmission; -use Statamic\Facades\Path; -use Statamic\Facades\Site; -use Statamic\Facades\Stache; -use Statamic\Stache\Stores\SubmissionsStore; use Tests\PreventSavingStacheItemsToDisk; use Tests\TestCase; From 5249af093a0a62691f0f463062adb1d8d0f214a8 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 5 Aug 2022 19:16:32 +0100 Subject: [PATCH 13/46] StyleCI --- tests/Forms/SubmissionQueryBuilderTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Forms/SubmissionQueryBuilderTest.php b/tests/Forms/SubmissionQueryBuilderTest.php index da6b04829c..e73be0fc42 100644 --- a/tests/Forms/SubmissionQueryBuilderTest.php +++ b/tests/Forms/SubmissionQueryBuilderTest.php @@ -390,9 +390,9 @@ public function submissions_are_found_using_where_json_length() public function submissions_are_found_using_offset() { $form = tap(Form::make('test'))->save(); - FormSubmission::make()->form($form)->data(['id' => 'a', ])->save(); - FormSubmission::make()->form($form)->data(['id' => 'b', ])->save(); - FormSubmission::make()->form($form)->data(['id' => 'c', ])->save(); + FormSubmission::make()->form($form)->data(['id' => 'a'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'b'])->save(); + FormSubmission::make()->form($form)->data(['id' => 'c'])->save(); $submissions = FormSubmission::query()->get(); $this->assertEquals(['a', 'b', 'c'], $submissions->map->get('id')->all()); From 08afa9456dfdf50a0f31f49899a85f3d859d6225 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 5 Aug 2022 19:43:56 +0100 Subject: [PATCH 14/46] Final tests --- .../Repositories/SubmissionRepository.php | 4 +- tests/Forms/SubmissionQueryBuilderTest.php | 10 ++-- tests/Stache/FeatureTest.php | 1 + .../Repositories/SubmissionRepositoryTest.php | 58 +++++++++++++++++++ .../contact_form/1631083591.2832.yaml | 5 ++ .../contact_form/1638174892.0304.yaml | 4 ++ .../contact_form/1638181789.4144.yaml | 5 ++ .../submissions/sign_up/1631087772.4503.yaml | 2 + 8 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 tests/Stache/Repositories/SubmissionRepositoryTest.php create mode 100644 tests/Stache/__fixtures__/content/submissions/contact_form/1631083591.2832.yaml create mode 100644 tests/Stache/__fixtures__/content/submissions/contact_form/1638174892.0304.yaml create mode 100644 tests/Stache/__fixtures__/content/submissions/contact_form/1638181789.4144.yaml create mode 100644 tests/Stache/__fixtures__/content/submissions/sign_up/1631087772.4503.yaml diff --git a/src/Stache/Repositories/SubmissionRepository.php b/src/Stache/Repositories/SubmissionRepository.php index e513fb1f32..a16fbfe50e 100644 --- a/src/Stache/Repositories/SubmissionRepository.php +++ b/src/Stache/Repositories/SubmissionRepository.php @@ -44,14 +44,14 @@ public function find($id): ?Submission public function save($submission) { $this->store - ->store($submission->form()) + ->store($submission->form()->handle()) ->save($submission); } public function delete($submission) { $this->store - ->store($submission->form()) + ->store($submission->form()->handle()) ->delete($submission); } diff --git a/tests/Forms/SubmissionQueryBuilderTest.php b/tests/Forms/SubmissionQueryBuilderTest.php index e73be0fc42..b9f6eb37e8 100644 --- a/tests/Forms/SubmissionQueryBuilderTest.php +++ b/tests/Forms/SubmissionQueryBuilderTest.php @@ -189,7 +189,7 @@ public function submissions_are_found_using_where_column() } /** @test **/ - public function entries_are_found_using_where_date() + public function submissions_are_found_using_where_date() { $this->markTestSkipped(); // for now @@ -212,7 +212,7 @@ public function entries_are_found_using_where_date() } /** @test **/ - public function entries_are_found_using_where_month() + public function submissions_are_found_using_where_month() { $this->markTestSkipped(); // for now @@ -230,7 +230,7 @@ public function entries_are_found_using_where_month() } /** @test **/ - public function entries_are_found_using_where_day() + public function submissions_are_found_using_where_day() { $this->markTestSkipped(); // for now @@ -248,7 +248,7 @@ public function entries_are_found_using_where_day() } /** @test **/ - public function entries_are_found_using_where_year() + public function submissions_are_found_using_where_year() { $this->markTestSkipped(); // for now @@ -266,7 +266,7 @@ public function entries_are_found_using_where_year() } /** @test **/ - public function entries_are_found_using_where_time() + public function submissions_are_found_using_where_time() { $this->markTestSkipped(); // for now diff --git a/tests/Stache/FeatureTest.php b/tests/Stache/FeatureTest.php index efd8bb31b9..e846e9c43b 100644 --- a/tests/Stache/FeatureTest.php +++ b/tests/Stache/FeatureTest.php @@ -37,6 +37,7 @@ public function setUp(): void $stache->store('collection-trees')->directory($dir.'/content/structures/collections'); $stache->store('nav-trees')->directory($dir.'/content/structures/navigation'); $stache->store('users')->directory($dir.'/users'); + $stache->store('form-submissions')->directory($dir.'/content/submissions'); }); } diff --git a/tests/Stache/Repositories/SubmissionRepositoryTest.php b/tests/Stache/Repositories/SubmissionRepositoryTest.php new file mode 100644 index 0000000000..92b060ac00 --- /dev/null +++ b/tests/Stache/Repositories/SubmissionRepositoryTest.php @@ -0,0 +1,58 @@ +sites(['en', 'fr']); + $this->app->instance(Stache::class, $stache); + $this->directory = __DIR__.'/../__fixtures__/content/submissions'; + $stache->registerStores([ + (new SubmissionsStore($stache, app('files')))->directory($this->directory), + ]); + + $this->repo = new SubmissionRepository($stache); + + $contact = Form::make('contact_form')->save(); + Form::make('sign_up')->save(); + } + + /** @test */ + public function it_gets_all_submissions() + { + $submissions = $this->repo->all(); + + $this->assertInstanceOf(Collection::class, $submissions); + $this->assertCount(4, $submissions); + $this->assertEveryItemIsInstanceOf(Submission::class, $submissions); + } + + /** @test */ + public function it_saves_a_submission_to_the_stache_and_to_a_file() + { + $submission = SubmissionAPI::make()->id('new'); + $submission->form(Form::find('contact_form')); + $submission->data(['foo' => 'bar']); + $this->assertNull($this->repo->find('new')); + + $this->repo->save($submission); + + $this->assertNotNull($item = $this->repo->find('new')); + $this->assertEquals(['foo' => 'bar'], $item->data()->all()); + $this->assertTrue(file_exists($this->directory.'/contact_form/new.yaml')); + @unlink($this->directory.'/contact_form/new.yaml'); + } +} diff --git a/tests/Stache/__fixtures__/content/submissions/contact_form/1631083591.2832.yaml b/tests/Stache/__fixtures__/content/submissions/contact_form/1631083591.2832.yaml new file mode 100644 index 0000000000..7383bc51e7 --- /dev/null +++ b/tests/Stache/__fixtures__/content/submissions/contact_form/1631083591.2832.yaml @@ -0,0 +1,5 @@ +name: Someone +email: someone@test.com +telephone: '079' +message: '123' +mailing_list: '1' diff --git a/tests/Stache/__fixtures__/content/submissions/contact_form/1638174892.0304.yaml b/tests/Stache/__fixtures__/content/submissions/contact_form/1638174892.0304.yaml new file mode 100644 index 0000000000..36eba24162 --- /dev/null +++ b/tests/Stache/__fixtures__/content/submissions/contact_form/1638174892.0304.yaml @@ -0,0 +1,4 @@ +name: 'Someone Else' +email: another@test.com +telephone: '079' +message: 'This is my message to you' diff --git a/tests/Stache/__fixtures__/content/submissions/contact_form/1638181789.4144.yaml b/tests/Stache/__fixtures__/content/submissions/contact_form/1638181789.4144.yaml new file mode 100644 index 0000000000..2025e05a7e --- /dev/null +++ b/tests/Stache/__fixtures__/content/submissions/contact_form/1638181789.4144.yaml @@ -0,0 +1,5 @@ +name: 'A third Person' +email: third@test.com +telephone: '028' +message: 'This is my message' +mailing_list: '1' diff --git a/tests/Stache/__fixtures__/content/submissions/sign_up/1631087772.4503.yaml b/tests/Stache/__fixtures__/content/submissions/sign_up/1631087772.4503.yaml new file mode 100644 index 0000000000..fea94286ba --- /dev/null +++ b/tests/Stache/__fixtures__/content/submissions/sign_up/1631087772.4503.yaml @@ -0,0 +1,2 @@ +name: 'Test' +email: test@test.com From cf4288297f3c19d75aad16027b3dd4235fabfd5b Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 5 Aug 2022 20:36:59 +0100 Subject: [PATCH 15/46] Revert test change as it was causing errors --- tests/TestCase.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/TestCase.php b/tests/TestCase.php index 680c3504e4..e8e091fc98 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -75,7 +75,6 @@ protected function resolveApplicationConfiguration($app) $app['config']->set("statamic.$config", require(__DIR__."/../config/{$config}.php")); } - $app['config']->set('statamic.forms.submissions', __DIR__.'/__fixtures__/content/submissions'); $app['config']->set('statamic.antlers.version', 'runtime'); } From a5eb6545615dec9dd361feb85863a6a71d3e678a Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 5 Aug 2022 20:46:33 +0100 Subject: [PATCH 16/46] A bit more test jigging --- tests/Tags/Form/FormSubmissionsTest.php | 2 ++ tests/TestCase.php | 1 + 2 files changed, 3 insertions(+) diff --git a/tests/Tags/Form/FormSubmissionsTest.php b/tests/Tags/Form/FormSubmissionsTest.php index 510939de9f..cfc464aee3 100644 --- a/tests/Tags/Form/FormSubmissionsTest.php +++ b/tests/Tags/Form/FormSubmissionsTest.php @@ -7,6 +7,8 @@ class FormSubmissionsTest extends FormTestCase /** @test */ public function it_renders_submissions() { + $this->app->make('stache')->store('form-submissions')->directory(__DIR__.'/../../__fixtures__/content/submissions'); + $this ->post('/!/forms/contact', [ 'email' => 'san@holo.com', diff --git a/tests/TestCase.php b/tests/TestCase.php index e8e091fc98..680c3504e4 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -75,6 +75,7 @@ protected function resolveApplicationConfiguration($app) $app['config']->set("statamic.$config", require(__DIR__."/../config/{$config}.php")); } + $app['config']->set('statamic.forms.submissions', __DIR__.'/__fixtures__/content/submissions'); $app['config']->set('statamic.antlers.version', 'runtime'); } From c4c161ecbc7802d45ad8dd38612958d6c1ea5e56 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 5 Aug 2022 20:52:52 +0100 Subject: [PATCH 17/46] And some more --- tests/Tags/Form/FormCreateTest.php | 6 ++++++ tests/Tags/Form/FormSubmissionsTest.php | 2 +- tests/Tags/Form/FormTestCase.php | 5 +++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/Tags/Form/FormCreateTest.php b/tests/Tags/Form/FormCreateTest.php index d3b0725362..cc48baca47 100644 --- a/tests/Tags/Form/FormCreateTest.php +++ b/tests/Tags/Form/FormCreateTest.php @@ -449,6 +449,8 @@ public function it_dynamically_renders_field_with_fallback_to_default_partial() /** @test */ public function it_wont_submit_form_and_renders_errors() { + $this->resetSubmissionStache(); + $this->assertEmpty(Form::find('contact')->submissions()); $this @@ -490,6 +492,8 @@ public function it_wont_submit_form_and_renders_errors() /** @test */ public function it_will_submit_form_and_render_success() { + $this->resetSubmissionStache(); + $this->assertEmpty(Form::find('contact')->submissions()); $this @@ -522,6 +526,8 @@ public function it_will_submit_form_and_render_success() /** @test */ public function it_will_submit_form_and_follow_custom_redirect_with_success() { + $this->resetSubmissionStache(); + $this->assertEmpty(Form::find('contact')->submissions()); $this diff --git a/tests/Tags/Form/FormSubmissionsTest.php b/tests/Tags/Form/FormSubmissionsTest.php index cfc464aee3..985f662328 100644 --- a/tests/Tags/Form/FormSubmissionsTest.php +++ b/tests/Tags/Form/FormSubmissionsTest.php @@ -7,7 +7,7 @@ class FormSubmissionsTest extends FormTestCase /** @test */ public function it_renders_submissions() { - $this->app->make('stache')->store('form-submissions')->directory(__DIR__.'/../../__fixtures__/content/submissions'); + $this->resetSubmissionStache(); $this ->post('/!/forms/contact', [ diff --git a/tests/Tags/Form/FormTestCase.php b/tests/Tags/Form/FormTestCase.php index 1b98540c81..432d6ca91a 100644 --- a/tests/Tags/Form/FormTestCase.php +++ b/tests/Tags/Form/FormTestCase.php @@ -120,4 +120,9 @@ protected function clearSubmissions() { Form::find('contact')->submissions()->each->delete(); } + + protected function resetSubmissionStache() + { + $this->app->make('stache')->store('form-submissions')->directory(__DIR__.'/../../__fixtures__/content/submissions'); + } } From 4d224e475729fe10aef10094f16f54c6e97a784f Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Sat, 6 Aug 2022 07:52:46 +0100 Subject: [PATCH 18/46] Remove unnecessary orwheres --- src/Stache/Query/SubmissionQueryBuilder.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/Stache/Query/SubmissionQueryBuilder.php b/src/Stache/Query/SubmissionQueryBuilder.php index 28597a2f63..149f96b7c5 100644 --- a/src/Stache/Query/SubmissionQueryBuilder.php +++ b/src/Stache/Query/SubmissionQueryBuilder.php @@ -21,11 +21,6 @@ public function where($column, $operator = null, $value = null, $boolean = 'and' return parent::where($column, $operator, $value, $boolean); } - public function orWhere($column, $operator = null, $value = null) - { - return $this->where($column, $operator, $value, 'or'); - } - public function whereIn($column, $values, $boolean = 'and') { if (in_array($column, ['form', 'forms'])) { @@ -37,11 +32,6 @@ public function whereIn($column, $values, $boolean = 'and') return parent::whereIn($column, $values, $boolean); } - public function orWhereIn($column, $values) - { - return $this->whereIn($column, $values, 'or'); - } - protected function collect($items = []) { return Collection::make($items); From 0ea591e28d18d6422fd6e1d9c456e08517b341f1 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Mon, 8 Aug 2022 09:52:01 +0100 Subject: [PATCH 19/46] Support for querying by date --- src/Stache/Indexes/Date.php | 20 +++++++ src/Stache/Stores/FormSubmissionStore.php | 1 + tests/Forms/SubmissionQueryBuilderTest.php | 67 +++++++++------------- 3 files changed, 48 insertions(+), 40 deletions(-) create mode 100644 src/Stache/Indexes/Date.php diff --git a/src/Stache/Indexes/Date.php b/src/Stache/Indexes/Date.php new file mode 100644 index 0000000000..bc7142576c --- /dev/null +++ b/src/Stache/Indexes/Date.php @@ -0,0 +1,20 @@ +store->getItemsFromFiles()->map(function ($item) { + return $this->getItemValue($item); + })->all(); + } + + public function getItemValue($item) + { + return $item->date(); + } +} diff --git a/src/Stache/Stores/FormSubmissionStore.php b/src/Stache/Stores/FormSubmissionStore.php index 14e8b157e5..7d8555fbc8 100644 --- a/src/Stache/Stores/FormSubmissionStore.php +++ b/src/Stache/Stores/FormSubmissionStore.php @@ -14,6 +14,7 @@ class FormSubmissionStore extends ChildStore protected $valueIndex = Value::class; protected $storeIndexes = [ 'form', + 'date' => \Statamic\Stache\Indexes\Date::class, ]; public function getItemKey($item) diff --git a/tests/Forms/SubmissionQueryBuilderTest.php b/tests/Forms/SubmissionQueryBuilderTest.php index b9f6eb37e8..8905c4ad01 100644 --- a/tests/Forms/SubmissionQueryBuilderTest.php +++ b/tests/Forms/SubmissionQueryBuilderTest.php @@ -4,6 +4,7 @@ use Illuminate\Support\Collection; use Statamic\Contracts\Forms\Submission; +use Statamic\Facades\Blueprint; use Statamic\Facades\Form; use Statamic\Facades\FormSubmission; use Tests\PreventSavingStacheItemsToDisk; @@ -191,109 +192,95 @@ public function submissions_are_found_using_where_column() /** @test **/ public function submissions_are_found_using_where_date() { - $this->markTestSkipped(); // for now - $this->createWhereDateTestTerms(); - $entries = FormSubmission::query()->whereDate('test_date', '2021-11-15')->get(); + $entries = FormSubmission::query()->whereDate('date', '2021-11-15')->get(); $this->assertCount(2, $entries); - $this->assertEquals(['Post 1', 'Post 3'], $entries->map->title->all()); + $this->assertEquals(['Post 1', 'Post 3'], $entries->map->title->sort()->values()->all()); - $entries = FormSubmission::query()->whereDate('test_date', 1637000264)->get(); + $entries = FormSubmission::query()->whereDate('date', 1637000264)->get(); $this->assertCount(2, $entries); - $this->assertEquals(['Post 1', 'Post 3'], $entries->map->title->all()); + $this->assertEquals(['Post 1', 'Post 3'], $entries->map->title->sort()->values()->all()); - $entries = FormSubmission::query()->whereDate('test_date', '>=', '2021-11-15')->get(); + $entries = FormSubmission::query()->whereDate('date', '>=', '2021-11-15')->get(); $this->assertCount(2, $entries); - $this->assertEquals(['Post 1', 'Post 3'], $entries->map->title->all()); + $this->assertEquals(['Post 1', 'Post 3'], $entries->map->title->sort()->values()->all()); } /** @test **/ public function submissions_are_found_using_where_month() { - $this->markTestSkipped(); // for now - $this->createWhereDateTestTerms(); - $entries = FormSubmission::query()->whereMonth('test_date', 11)->get(); + $entries = FormSubmission::query()->whereMonth('date', 11)->get(); $this->assertCount(3, $entries); - $this->assertEquals(['Post 1', 'Post 2', 'Post 3'], $entries->map->title->all()); + $this->assertEquals(['Post 1', 'Post 2', 'Post 3'], $entries->map->title->sort()->values()->all()); - $entries = FormSubmission::query()->whereMonth('test_date', '<', 11)->get(); + $entries = FormSubmission::query()->whereMonth('date', '<', 11)->get(); $this->assertCount(1, $entries); - $this->assertEquals(['Post 4'], $entries->map->title->all()); + $this->assertEquals(['Post 4'], $entries->map->title->sort()->values()->all()); } /** @test **/ public function submissions_are_found_using_where_day() { - $this->markTestSkipped(); // for now - $this->createWhereDateTestTerms(); - $entries = FormSubmission::query()->whereDay('test_date', 15)->get(); + $entries = FormSubmission::query()->whereDay('date', 15)->get(); $this->assertCount(2, $entries); - $this->assertEquals(['Post 1', 'Post 3'], $entries->map->title->all()); + $this->assertEquals(['Post 1', 'Post 3'], $entries->map->title->sort()->values()->all()); - $entries = FormSubmission::query()->whereDay('test_date', '<', 15)->get(); + $entries = FormSubmission::query()->whereDay('date', '<', 15)->get(); $this->assertCount(2, $entries); - $this->assertEquals(['Post 2', 'Post 4'], $entries->map->title->all()); + $this->assertEquals(['Post 2', 'Post 4'], $entries->map->title->sort()->values()->all()); } /** @test **/ public function submissions_are_found_using_where_year() { - $this->markTestSkipped(); // for now - $this->createWhereDateTestTerms(); - $entries = FormSubmission::query()->whereYear('test_date', 2021)->get(); + $entries = FormSubmission::query()->whereYear('date', 2021)->get(); $this->assertCount(3, $entries); - $this->assertEquals(['Post 1', 'Post 2', 'Post 3'], $entries->map->title->all()); + $this->assertEquals(['Post 1', 'Post 2', 'Post 3'], $entries->map->title->sort()->values()->all()); - $entries = FormSubmission::query()->whereYear('test_date', '<', 2021)->get(); + $entries = FormSubmission::query()->whereYear('date', '<', 2021)->get(); $this->assertCount(1, $entries); - $this->assertEquals(['Post 4'], $entries->map->title->all()); + $this->assertEquals(['Post 4'], $entries->map->title->sort()->values()->all()); } /** @test **/ public function submissions_are_found_using_where_time() { - $this->markTestSkipped(); // for now - $this->createWhereDateTestTerms(); - $entries = FormSubmission::query()->whereTime('test_date', '09:00')->get(); + $entries = FormSubmission::query()->whereTime('date', '09:00')->get(); $this->assertCount(1, $entries); - $this->assertEquals(['Post 2'], $entries->map->title->all()); + $this->assertEquals(['Post 2'], $entries->map->title->sort()->values()->all()); - $entries = FormSubmission::query()->whereTime('test_date', '>', '09:00')->get(); + $entries = FormSubmission::query()->whereTime('date', '>', '09:00')->get(); $this->assertCount(2, $entries); - $this->assertEquals(['Post 1', 'Post 4'], $entries->map->title->all()); + $this->assertEquals(['Post 1', 'Post 4'], $entries->map->title->sort()->values()->all()); } private function createWhereDateTestTerms() { - $blueprint = Blueprint::makeFromFields(['test_date' => ['type' => 'date', 'time_enabled' => true]]); - Blueprint::shouldReceive('in')->with('taxonomies/tags')->andReturn(collect(['tags' => $blueprint])); - $form = tap(Form::make('test'))->save(); - FormSubmission::make()->form($form)->data(['title' => 'Post 1', 'test_date' => '2021-11-15 20:31:04'])->save(); - FormSubmission::make()->form($form)->data(['title' => 'Post 2', 'test_date' => '2021-11-14 09:00:00'])->save(); - FormSubmission::make()->form($form)->data(['title' => 'Post 3', 'test_date' => '2021-11-15 00:00:00'])->save(); - FormSubmission::make()->form($form)->data(['title' => 'Post 4', 'test_date' => '2020-09-13 14:44:24'])->save(); - FormSubmission::make()->form($form)->data(['title' => 'Post 5', 'test_date' => null])->save(); + FormSubmission::make()->form($form)->data(['title' => 'Post 1'])->id(1637008264)->save(); + FormSubmission::make()->form($form)->data(['title' => 'Post 2'])->id(1636621200)->save(); + FormSubmission::make()->form($form)->data(['title' => 'Post 3'])->id(1636934400)->save(); + FormSubmission::make()->form($form)->data(['title' => 'Post 4'])->id(1600008264)->save(); } /** @test **/ From 40a2b7f8d5241d8c1ec674846b8929e955af42ad Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Mon, 8 Aug 2022 09:53:43 +0100 Subject: [PATCH 20/46] StyleCI --- src/Stache/Indexes/Date.php | 2 -- tests/Forms/SubmissionQueryBuilderTest.php | 1 - 2 files changed, 3 deletions(-) diff --git a/src/Stache/Indexes/Date.php b/src/Stache/Indexes/Date.php index bc7142576c..c88ed873fa 100644 --- a/src/Stache/Indexes/Date.php +++ b/src/Stache/Indexes/Date.php @@ -2,8 +2,6 @@ namespace Statamic\Stache\Indexes; -use Statamic\Query\ResolveValue; - class Date extends Index { public function getItems() diff --git a/tests/Forms/SubmissionQueryBuilderTest.php b/tests/Forms/SubmissionQueryBuilderTest.php index 8905c4ad01..58b0dcd07c 100644 --- a/tests/Forms/SubmissionQueryBuilderTest.php +++ b/tests/Forms/SubmissionQueryBuilderTest.php @@ -4,7 +4,6 @@ use Illuminate\Support\Collection; use Statamic\Contracts\Forms\Submission; -use Statamic\Facades\Blueprint; use Statamic\Facades\Form; use Statamic\Facades\FormSubmission; use Tests\PreventSavingStacheItemsToDisk; From 42acaea2fee146cf5c9384027be3689d05c13406 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 23 Jun 2023 06:38:46 +0100 Subject: [PATCH 21/46] Use FormSubmission facade to get and make submissions --- src/Forms/Form.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/Forms/Form.php b/src/Forms/Form.php index e1ad6ad137..c61e6cfa83 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -17,6 +17,7 @@ use Statamic\Facades\File; use Statamic\Facades\Folder; use Statamic\Facades\Form as FormFacade; +use Statamic\Facades\FormSubmission; use Statamic\Facades\YAML; use Statamic\Forms\Exceptions\BlueprintUndefinedException; use Statamic\Statamic; @@ -282,13 +283,7 @@ public function metrics($metrics = null) */ public function submissions() { - $path = config('statamic.forms.submissions').'/'.$this->handle(); - - return collect(Folder::getFilesByType($path, 'yaml'))->map(function ($file) { - return $this->makeSubmission() - ->id(pathinfo($file)['filename']) - ->data(YAML::parse(File::get($file))); - }); + return FormSubmission::whereForm($this->handle())->get(); } /** @@ -311,7 +306,7 @@ public function submission($id) */ public function makeSubmission() { - $submission = app(Submission::class); + $submission = FormSubmission::make(); $submission->form($this); From 053e9e4ccfdef4b626a3060ba6a08e17a557873f Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 23 Jun 2023 07:59:06 +0100 Subject: [PATCH 22/46] Bug fixes --- src/Forms/Form.php | 2 +- tests/Stache/Stores/FormSubmissionStoreTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Forms/Form.php b/src/Forms/Form.php index f7449f024b..6a823f4971 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -283,7 +283,7 @@ public function metrics($metrics = null) */ public function submissions() { - return FormSubmission::whereForm($this->handle())->get(); + return FormSubmission::whereForm($this->handle()); } /** diff --git a/tests/Stache/Stores/FormSubmissionStoreTest.php b/tests/Stache/Stores/FormSubmissionStoreTest.php index 1963c9cd34..5486c41010 100644 --- a/tests/Stache/Stores/FormSubmissionStoreTest.php +++ b/tests/Stache/Stores/FormSubmissionStoreTest.php @@ -40,7 +40,7 @@ public function it_saves_to_disk() $this->assertStringEqualsFile($path = $this->directory.'/test_form/'.$submission->id().'.yaml', $submission->fileContents()); @unlink($path); - $this->assertFileNotExists($path); + $this->assertFileDoesNotExist($path); $this->assertEquals($path, $this->parent->store('test_form')->paths()->get($submission->id())); } From 716129daf5596156a17fb4dd5843691a1d3fb62a Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 23 Jun 2023 17:18:42 +0100 Subject: [PATCH 23/46] Thank you Jason --- src/Forms/Submission.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Forms/Submission.php b/src/Forms/Submission.php index 3ca4aa626a..dff99de3ac 100644 --- a/src/Forms/Submission.php +++ b/src/Forms/Submission.php @@ -15,10 +15,9 @@ use Statamic\Events\SubmissionSaved; use Statamic\Events\SubmissionSaving; use Statamic\Facades\File; +use Statamic\Facades\FormSubmission; use Statamic\Facades\Stache; -use Statamic\Facades\YAML; use Statamic\Forms\Uploaders\AssetsUploader; -use Statamic\Support\Arr; use Statamic\Support\Traits\FluentlyGetsAndSets; class Submission implements SubmissionContract, Augmentable @@ -158,7 +157,7 @@ public function save() } } - File::put($this->getPath(), YAML::dump(Arr::removeNullValues($this->data()->all()))); + FormSubmission::save($this); foreach ($afterSaveCallbacks as $callback) { $callback($this); From c8b7d1029a490c960f5de209111c1d195b7306ce Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 23 Jun 2023 18:25:48 +0100 Subject: [PATCH 24/46] Overload fileData on the Submission class rather than changing the trait --- src/Data/ExistsAsFile.php | 8 +------- src/Forms/Submission.php | 5 +++++ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Data/ExistsAsFile.php b/src/Data/ExistsAsFile.php index 5bfeaca6b9..7a549641c0 100644 --- a/src/Data/ExistsAsFile.php +++ b/src/Data/ExistsAsFile.php @@ -3,7 +3,6 @@ namespace Statamic\Data; use Illuminate\Support\Carbon; -use Illuminate\Support\Collection; use Statamic\Facades\File; use Statamic\Facades\YAML; use Statamic\Support\Arr; @@ -32,12 +31,7 @@ public function initialPath($path = null) public function fileData() { - $data = $this->data(); - if ($data instanceof Collection) { - $data = $data->all(); - } - - return array_merge($data, [ + return array_merge($this->data(), [ 'id' => $this->id(), ]); } diff --git a/src/Forms/Submission.php b/src/Forms/Submission.php index dff99de3ac..e04b4fea87 100644 --- a/src/Forms/Submission.php +++ b/src/Forms/Submission.php @@ -236,6 +236,11 @@ public function blueprint() return $this->form->blueprint(); } + public function fileData() + { + return $this->data()->all(); + } + public function __get($key) { return $this->get($key); From 6a1bdab4f0b72911668400d099cf7e50f0ed0e49 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 23 Jun 2023 18:29:16 +0100 Subject: [PATCH 25/46] Also delete through the Facade --- src/Forms/Submission.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Forms/Submission.php b/src/Forms/Submission.php index e04b4fea87..1bdad06a2d 100644 --- a/src/Forms/Submission.php +++ b/src/Forms/Submission.php @@ -177,7 +177,7 @@ public function save() */ public function delete() { - File::delete($this->getPath()); + FormSubmission::delete($this); SubmissionDeleted::dispatch($this); } From b87bdfd9dd7a0bd1b786c89513d345edf1a5c7d1 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Sat, 24 Jun 2023 09:44:05 +0100 Subject: [PATCH 26/46] Defer to Submission respository for finding, allows it to be more efficient --- src/Forms/Form.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Forms/Form.php b/src/Forms/Form.php index 6a823f4971..1290f6cc6b 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -294,9 +294,7 @@ public function submissions() */ public function submission($id) { - return $this->submissions()->filter(function ($submission) use ($id) { - return $submission->id() === $id; - })->first(); + return FormSubmission::find($id); } /** From 6fd9b83647ea6c8facab36a36152206809e88dae Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Sat, 24 Jun 2023 10:35:13 +0100 Subject: [PATCH 27/46] Bind QueryBuilder to make it easier to override elsewhere --- src/Stache/Repositories/SubmissionRepository.php | 5 +++-- src/Stache/ServiceProvider.php | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Stache/Repositories/SubmissionRepository.php b/src/Stache/Repositories/SubmissionRepository.php index a16fbfe50e..d73cfaceb4 100644 --- a/src/Stache/Repositories/SubmissionRepository.php +++ b/src/Stache/Repositories/SubmissionRepository.php @@ -4,8 +4,8 @@ use Illuminate\Support\Collection; use Statamic\Contracts\Forms\Submission; +use Statamic\Contracts\Forms\SubmissionQueryBuilder; use Statamic\Contracts\Forms\SubmissionRepository as RepositoryContract; -use Statamic\Stache\Query\SubmissionQueryBuilder; use Statamic\Stache\Stache; class SubmissionRepository implements RepositoryContract @@ -57,7 +57,7 @@ public function delete($submission) public function query() { - return new SubmissionQueryBuilder($this->store); + return app(SubmissionQueryBuilder::class); } public function make(): Submission @@ -69,6 +69,7 @@ public static function bindings(): array { return [ Submission::class => \Statamic\Forms\Submission::class, + SubmissionQueryBuilder::class => \Statamic\Stache\Query\SubmissionQueryBuilder::class, ]; } } diff --git a/src/Stache/ServiceProvider.php b/src/Stache/ServiceProvider.php index 0179270977..493df591f4 100644 --- a/src/Stache/ServiceProvider.php +++ b/src/Stache/ServiceProvider.php @@ -7,6 +7,7 @@ use Statamic\Facades\File; use Statamic\Facades\Site; use Statamic\Stache\Query\EntryQueryBuilder; +use Statamic\Stache\Query\SubmissionQueryBuilder; use Symfony\Component\Lock\LockFactory; use Symfony\Component\Lock\Store\FlockStore; @@ -31,6 +32,10 @@ public function register() $this->app->bind(AssetQueryBuilder::class, function () { return new AssetQueryBuilder($this->app->make(Stache::class)->store('assets')); }); + + $this->app->bind(SubmissionQueryBuilder::class, function () { + return new SubmissionQueryBuilder($this->app->make(Stache::class)->store('form-submissions')); + }); } public function boot() From 7f143b6593a67ad1605cb6f2aa097c6150398dfe Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 6 Dec 2023 11:44:00 +0000 Subject: [PATCH 28/46] =?UTF-8?q?Run=20Pint=20=F0=9F=8D=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Forms/Form.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Forms/Form.php b/src/Forms/Form.php index 80582d0eba..16a8fafb3e 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -16,7 +16,6 @@ use Statamic\Events\FormSaving; use Statamic\Facades\Blueprint; use Statamic\Facades\File; -use Statamic\Facades\Folder; use Statamic\Facades\Form as FormFacade; use Statamic\Facades\FormSubmission; use Statamic\Facades\YAML; From 62b81bbe474a8f7169910f27feafb4d349fe6392 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Thu, 11 Jan 2024 14:55:21 +0000 Subject: [PATCH 29/46] fixes --- src/Forms/Form.php | 2 -- src/Stache/Stores/FormSubmissionStore.php | 10 +++++++++- tests/TestCase.php | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Forms/Form.php b/src/Forms/Form.php index 7749402fd0..de145c6ac1 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -3,7 +3,6 @@ namespace Statamic\Forms; use Illuminate\Contracts\Support\Arrayable; -use Illuminate\Support\Facades\Log; use Statamic\Contracts\Data\Augmentable; use Statamic\Contracts\Data\Augmented; use Statamic\Contracts\Forms\Form as FormContract; @@ -26,7 +25,6 @@ use Statamic\Statamic; use Statamic\Support\Arr; use Statamic\Support\Traits\FluentlyGetsAndSets; -use Statamic\Yaml\ParseException; class Form implements Arrayable, Augmentable, FormContract { diff --git a/src/Stache/Stores/FormSubmissionStore.php b/src/Stache/Stores/FormSubmissionStore.php index 7d8555fbc8..efdcfd8314 100644 --- a/src/Stache/Stores/FormSubmissionStore.php +++ b/src/Stache/Stores/FormSubmissionStore.php @@ -2,11 +2,13 @@ namespace Statamic\Stache\Stores; +use Illuminate\Support\Facades\Log; use Statamic\Facades\Form; use Statamic\Facades\FormSubmission; use Statamic\Facades\Path; use Statamic\Facades\YAML; use Statamic\Stache\Indexes\Value; +use Statamic\Yaml\ParseException; use Symfony\Component\Finder\SplFileInfo; class FormSubmissionStore extends ChildStore @@ -33,7 +35,13 @@ public function getItemFilter(SplFileInfo $file) public function makeItemFromFile($path, $contents) { $handle = pathinfo($path, PATHINFO_FILENAME); - $data = YAML::file($path)->parse($contents); + + try { + $data = YAML::parse(File::get($file)); + } catch (ParseException $e) { + $data = []; + Log::warning('Could not parse form submission file: '.$file); + } $form = pathinfo($path, PATHINFO_DIRNAME); $form = str_after($form, $this->parent->directory()); diff --git a/tests/TestCase.php b/tests/TestCase.php index 72a940316a..e696fb4036 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -74,7 +74,7 @@ protected function resolveApplicationConfiguration($app) ]; foreach ($configs as $config) { - $app['config']->set("statamic.$config", require (__DIR__."/../config/{$config}.php")); + $app['config']->set("statamic.$config", require(__DIR__."/../config/{$config}.php")); } $app['config']->set('statamic.forms.submissions', __DIR__.'/__fixtures__/content/submissions'); From df3caec80b34724facdff2adbaacff6319cd5650 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Thu, 11 Jan 2024 14:59:04 +0000 Subject: [PATCH 30/46] :beer: --- tests/TestCase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TestCase.php b/tests/TestCase.php index e696fb4036..72a940316a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -74,7 +74,7 @@ protected function resolveApplicationConfiguration($app) ]; foreach ($configs as $config) { - $app['config']->set("statamic.$config", require(__DIR__."/../config/{$config}.php")); + $app['config']->set("statamic.$config", require (__DIR__."/../config/{$config}.php")); } $app['config']->set('statamic.forms.submissions', __DIR__.'/__fixtures__/content/submissions'); From 5b001a921775e4eb9620c59e6f1024685a3c3203 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Thu, 11 Jan 2024 15:04:44 +0000 Subject: [PATCH 31/46] Fix test failure --- src/Stache/Stores/FormSubmissionStore.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Stache/Stores/FormSubmissionStore.php b/src/Stache/Stores/FormSubmissionStore.php index efdcfd8314..aa6c9f9ee6 100644 --- a/src/Stache/Stores/FormSubmissionStore.php +++ b/src/Stache/Stores/FormSubmissionStore.php @@ -37,7 +37,7 @@ public function makeItemFromFile($path, $contents) $handle = pathinfo($path, PATHINFO_FILENAME); try { - $data = YAML::parse(File::get($file)); + $data = YAML::file($path)->parse($contents); } catch (ParseException $e) { $data = []; Log::warning('Could not parse form submission file: '.$file); From c0088ca8fc7c13baea7c19ffc1ba19d93fe8ea30 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 3 Apr 2024 19:09:41 +0100 Subject: [PATCH 32/46] swap out string helpers --- src/Stache/Stores/FormSubmissionStore.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Stache/Stores/FormSubmissionStore.php b/src/Stache/Stores/FormSubmissionStore.php index aa6c9f9ee6..5b041a1f93 100644 --- a/src/Stache/Stores/FormSubmissionStore.php +++ b/src/Stache/Stores/FormSubmissionStore.php @@ -8,6 +8,7 @@ use Statamic\Facades\Path; use Statamic\Facades\YAML; use Statamic\Stache\Indexes\Value; +use Statamic\Support\Str; use Statamic\Yaml\ParseException; use Symfony\Component\Finder\SplFileInfo; @@ -26,8 +27,8 @@ public function getItemKey($item) public function getItemFilter(SplFileInfo $file) { - $dir = str_finish($this->directory(), '/'); - $relative = str_after(Path::tidy($file->getPathname()), $dir); + $dir = Str::finish($this->directory(), '/'); + $relative = Str::after(Path::tidy($file->getPathname()), $dir); return $file->getExtension() === 'yaml' && substr_count($relative, '/') === 0; } @@ -44,7 +45,7 @@ public function makeItemFromFile($path, $contents) } $form = pathinfo($path, PATHINFO_DIRNAME); - $form = str_after($form, $this->parent->directory()); + $form = Str::after($form, $this->parent->directory()); $form = Form::find($form); From d3b38ca41e9fbb2449d1e2d8881c2f962da86cbd Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 3 Apr 2024 19:14:14 +0100 Subject: [PATCH 33/46] =?UTF-8?q?=F0=9F=8D=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Forms/Submission.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Forms/Submission.php b/src/Forms/Submission.php index 664c2e8ecc..1637dc712e 100644 --- a/src/Forms/Submission.php +++ b/src/Forms/Submission.php @@ -21,7 +21,6 @@ use Statamic\Facades\Stache; use Statamic\Forms\Uploaders\AssetsUploader; use Statamic\Forms\Uploaders\FilesUploader; -use Statamic\Support\Arr; use Statamic\Support\Traits\FluentlyGetsAndSets; class Submission implements Augmentable, SubmissionContract From 190896f9c63ed8dd8c931b77954c962e731eb56d Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 4 Apr 2024 14:22:56 -0400 Subject: [PATCH 34/46] plural like the rest --- .../{FormSubmissionStore.php => FormSubmissionsStore.php} | 2 +- src/Stache/Stores/SubmissionsStore.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/Stache/Stores/{FormSubmissionStore.php => FormSubmissionsStore.php} (97%) diff --git a/src/Stache/Stores/FormSubmissionStore.php b/src/Stache/Stores/FormSubmissionsStore.php similarity index 97% rename from src/Stache/Stores/FormSubmissionStore.php rename to src/Stache/Stores/FormSubmissionsStore.php index 5b041a1f93..06c62c2930 100644 --- a/src/Stache/Stores/FormSubmissionStore.php +++ b/src/Stache/Stores/FormSubmissionsStore.php @@ -12,7 +12,7 @@ use Statamic\Yaml\ParseException; use Symfony\Component\Finder\SplFileInfo; -class FormSubmissionStore extends ChildStore +class FormSubmissionsStore extends ChildStore { protected $valueIndex = Value::class; protected $storeIndexes = [ diff --git a/src/Stache/Stores/SubmissionsStore.php b/src/Stache/Stores/SubmissionsStore.php index a53f4c3a18..7ff0a9dbf2 100644 --- a/src/Stache/Stores/SubmissionsStore.php +++ b/src/Stache/Stores/SubmissionsStore.php @@ -6,7 +6,7 @@ class SubmissionsStore extends AggregateStore { - protected $childStore = FormSubmissionStore::class; + protected $childStore = FormSubmissionsStore::class; public function key() { From 447c10802049e4cfb8b7647549f1a8120eaa4bd9 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 4 Apr 2024 14:28:53 -0400 Subject: [PATCH 35/46] move assets back, one less diff --- src/Providers/AppServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Providers/AppServiceProvider.php b/src/Providers/AppServiceProvider.php index 7b713abbe6..d14d968b73 100644 --- a/src/Providers/AppServiceProvider.php +++ b/src/Providers/AppServiceProvider.php @@ -155,10 +155,10 @@ public function register() }); collect([ - 'assets' => fn () => Facades\Asset::query(), 'entries' => fn () => Facades\Entry::query(), 'formsubmissions' => fn () => Facades\FormSubmission::query(), 'terms' => fn () => Facades\Term::query(), + 'assets' => fn () => Facades\Asset::query(), 'users' => fn () => Facades\User::query(), ])->each(function ($binding, $alias) { app()->bind('statamic.queries.'.$alias, $binding); From 7f096a0bda13a68f2308f1a5fbc06ac18afbc33b Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 4 Apr 2024 14:29:19 -0400 Subject: [PATCH 36/46] nitpick and update test --- src/Providers/AppServiceProvider.php | 2 +- tests/StatamicTest.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Providers/AppServiceProvider.php b/src/Providers/AppServiceProvider.php index d14d968b73..08fa4494f9 100644 --- a/src/Providers/AppServiceProvider.php +++ b/src/Providers/AppServiceProvider.php @@ -156,7 +156,7 @@ public function register() collect([ 'entries' => fn () => Facades\Entry::query(), - 'formsubmissions' => fn () => Facades\FormSubmission::query(), + 'form-submissions' => fn () => Facades\FormSubmission::query(), 'terms' => fn () => Facades\Term::query(), 'assets' => fn () => Facades\Asset::query(), 'users' => fn () => Facades\User::query(), diff --git a/tests/StatamicTest.php b/tests/StatamicTest.php index 54a445b37a..e8f637966b 100644 --- a/tests/StatamicTest.php +++ b/tests/StatamicTest.php @@ -159,6 +159,7 @@ public function native_query_builder_aliases_are_bound() 'terms' => \Statamic\Stache\Query\TermQueryBuilder::class, 'assets' => \Statamic\Assets\QueryBuilder::class, 'users' => \Statamic\Stache\Query\UserQueryBuilder::class, + 'form-submissions' => \Statamic\Stache\Query\SubmissionQueryBuilder::class, ]; foreach ($aliases as $alias => $class) { From d6dadd011af2ebe30b934056ba83dc773daf3a01 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 4 Apr 2024 18:40:16 -0400 Subject: [PATCH 37/46] copypasta --- src/Stache/Query/SubmissionQueryBuilder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Stache/Query/SubmissionQueryBuilder.php b/src/Stache/Query/SubmissionQueryBuilder.php index 149f96b7c5..4a60afcc66 100644 --- a/src/Stache/Query/SubmissionQueryBuilder.php +++ b/src/Stache/Query/SubmissionQueryBuilder.php @@ -87,7 +87,7 @@ protected function getOrderKeyValuesByIndex() ? Facades\Form::all()->map->handle() : $this->forms; - // First, we'll get the values from each index grouped by collection + // First, we'll get the values from each index grouped by form $keys = collect($forms)->map(function ($form) { $store = $this->store->store($form); @@ -102,7 +102,7 @@ protected function getOrderKeyValuesByIndex() }); }); - // Then, we'll merge all the corresponding index values together from each collection. + // Then, we'll merge all the corresponding index values together from each form. return $keys->reduce(function ($carry, $form) { foreach ($form as $sort => $values) { $carry[$sort] = array_merge($carry[$sort] ?? [], $values); From 103620a4f8b05bc200c06b25d5afa3bfc738904b Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 4 Apr 2024 19:09:44 -0400 Subject: [PATCH 38/46] not needed --- src/Stache/Indexes/Date.php | 18 ------------------ src/Stache/Stores/FormSubmissionsStore.php | 1 - 2 files changed, 19 deletions(-) delete mode 100644 src/Stache/Indexes/Date.php diff --git a/src/Stache/Indexes/Date.php b/src/Stache/Indexes/Date.php deleted file mode 100644 index c88ed873fa..0000000000 --- a/src/Stache/Indexes/Date.php +++ /dev/null @@ -1,18 +0,0 @@ -store->getItemsFromFiles()->map(function ($item) { - return $this->getItemValue($item); - })->all(); - } - - public function getItemValue($item) - { - return $item->date(); - } -} diff --git a/src/Stache/Stores/FormSubmissionsStore.php b/src/Stache/Stores/FormSubmissionsStore.php index 06c62c2930..dae3adf6f8 100644 --- a/src/Stache/Stores/FormSubmissionsStore.php +++ b/src/Stache/Stores/FormSubmissionsStore.php @@ -17,7 +17,6 @@ class FormSubmissionsStore extends ChildStore protected $valueIndex = Value::class; protected $storeIndexes = [ 'form', - 'date' => \Statamic\Stache\Indexes\Date::class, ]; public function getItemKey($item) From 6589b88082ac3012a73aae774c2e3dc0bbdbc56c Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 4 Apr 2024 19:09:46 -0400 Subject: [PATCH 39/46] fix var --- src/Stache/Stores/FormSubmissionsStore.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Stache/Stores/FormSubmissionsStore.php b/src/Stache/Stores/FormSubmissionsStore.php index dae3adf6f8..0116a47c57 100644 --- a/src/Stache/Stores/FormSubmissionsStore.php +++ b/src/Stache/Stores/FormSubmissionsStore.php @@ -40,7 +40,7 @@ public function makeItemFromFile($path, $contents) $data = YAML::file($path)->parse($contents); } catch (ParseException $e) { $data = []; - Log::warning('Could not parse form submission file: '.$file); + Log::warning('Could not parse form submission file: '.$path); } $form = pathinfo($path, PATHINFO_DIRNAME); From 6ce621c0ac4612afd151de7ce78f90f8146dbf35 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 4 Apr 2024 19:12:41 -0400 Subject: [PATCH 40/46] keep date so it gets warmed --- src/Stache/Stores/FormSubmissionsStore.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Stache/Stores/FormSubmissionsStore.php b/src/Stache/Stores/FormSubmissionsStore.php index 0116a47c57..41b82471bc 100644 --- a/src/Stache/Stores/FormSubmissionsStore.php +++ b/src/Stache/Stores/FormSubmissionsStore.php @@ -17,6 +17,7 @@ class FormSubmissionsStore extends ChildStore protected $valueIndex = Value::class; protected $storeIndexes = [ 'form', + 'date', ]; public function getItemKey($item) From 3e5c7b723e2d1e5694ab34d59b8e0481788d2859 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 4 Apr 2024 19:12:48 -0400 Subject: [PATCH 41/46] copy pasta --- src/Stache/Repositories/SubmissionRepository.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Stache/Repositories/SubmissionRepository.php b/src/Stache/Repositories/SubmissionRepository.php index d73cfaceb4..f10396aaef 100644 --- a/src/Stache/Repositories/SubmissionRepository.php +++ b/src/Stache/Repositories/SubmissionRepository.php @@ -12,8 +12,6 @@ class SubmissionRepository implements RepositoryContract { protected $stache; protected $store; - protected $substitutionsById = []; - protected $substitutionsByUri = []; public function __construct(Stache $stache) { From 134442b0d1ba65e8bc39cb452d41c1551593504a Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 5 Apr 2024 10:13:18 -0400 Subject: [PATCH 42/46] not needed --- tests/Tags/Form/FormCreateTest.php | 6 ------ tests/Tags/Form/FormSubmissionsTest.php | 2 -- tests/TestCase.php | 2 -- 3 files changed, 10 deletions(-) diff --git a/tests/Tags/Form/FormCreateTest.php b/tests/Tags/Form/FormCreateTest.php index f53bf4eed4..805ad4d43c 100644 --- a/tests/Tags/Form/FormCreateTest.php +++ b/tests/Tags/Form/FormCreateTest.php @@ -557,8 +557,6 @@ public function it_renders_section_instructions_without_cascading_into_field_ins /** @test */ public function it_wont_submit_form_and_renders_errors() { - $this->resetSubmissionStache(); - $this->assertEmpty(Form::find('contact')->submissions()); $this @@ -600,8 +598,6 @@ public function it_wont_submit_form_and_renders_errors() /** @test */ public function it_will_submit_form_and_render_success() { - $this->resetSubmissionStache(); - $this->assertEmpty(Form::find('contact')->submissions()); $this @@ -634,8 +630,6 @@ public function it_will_submit_form_and_render_success() /** @test */ public function it_will_submit_form_and_follow_custom_redirect_with_success() { - $this->resetSubmissionStache(); - $this->assertEmpty(Form::find('contact')->submissions()); $this diff --git a/tests/Tags/Form/FormSubmissionsTest.php b/tests/Tags/Form/FormSubmissionsTest.php index 985f662328..510939de9f 100644 --- a/tests/Tags/Form/FormSubmissionsTest.php +++ b/tests/Tags/Form/FormSubmissionsTest.php @@ -7,8 +7,6 @@ class FormSubmissionsTest extends FormTestCase /** @test */ public function it_renders_submissions() { - $this->resetSubmissionStache(); - $this ->post('/!/forms/contact', [ 'email' => 'san@holo.com', diff --git a/tests/TestCase.php b/tests/TestCase.php index 0e79862580..35e47189e6 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -76,8 +76,6 @@ protected function resolveApplicationConfiguration($app) foreach ($configs as $config) { $app['config']->set("statamic.$config", require (__DIR__."/../config/{$config}.php")); } - - $app['config']->set('statamic.forms.submissions', __DIR__.'/__fixtures__/content/submissions'); } protected function getEnvironmentSetUp($app) From 4b0c50af7132b19a04a2fe2c8d623e74b5d66d2d Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 5 Apr 2024 10:13:35 -0400 Subject: [PATCH 43/46] prevent saving new files to disk --- tests/Forms/SubmissionTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Forms/SubmissionTest.php b/tests/Forms/SubmissionTest.php index 70a5279d3b..730d3a4c7f 100644 --- a/tests/Forms/SubmissionTest.php +++ b/tests/Forms/SubmissionTest.php @@ -10,10 +10,13 @@ use Statamic\Events\SubmissionSaving; use Statamic\Facades\Blueprint; use Statamic\Facades\Form; +use Tests\PreventSavingStacheItemsToDisk; use Tests\TestCase; class SubmissionTest extends TestCase { + use PreventSavingStacheItemsToDisk; + /** @test */ public function the_id_is_generated_the_first_time_but_can_be_overridden() { From ac1ef0cdb123da36e0426fe78d5ce38b67870456 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 5 Apr 2024 10:14:09 -0400 Subject: [PATCH 44/46] this either --- tests/Tags/Form/FormTestCase.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/Tags/Form/FormTestCase.php b/tests/Tags/Form/FormTestCase.php index d22dcc4d84..83541322f0 100644 --- a/tests/Tags/Form/FormTestCase.php +++ b/tests/Tags/Form/FormTestCase.php @@ -124,9 +124,4 @@ protected function clearSubmissions() { Form::find('contact')->submissions()->each->delete(); } - - protected function resetSubmissionStache() - { - $this->app->make('stache')->store('form-submissions')->directory(__DIR__.'/../../__fixtures__/content/submissions'); - } } From 7c81812608542ae472cb48a0886aa2db73e81b8e Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 5 Apr 2024 10:41:28 -0400 Subject: [PATCH 45/46] test creation --- tests/Stache/Stores/FormSubmissionStoreTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/Stache/Stores/FormSubmissionStoreTest.php b/tests/Stache/Stores/FormSubmissionStoreTest.php index 5486c41010..ab757b1d13 100644 --- a/tests/Stache/Stores/FormSubmissionStoreTest.php +++ b/tests/Stache/Stores/FormSubmissionStoreTest.php @@ -2,6 +2,8 @@ namespace Tests\Stache\Stores; +use Illuminate\Support\Carbon; +use Statamic\Contracts\Forms\Submission; use Statamic\Facades; use Statamic\Facades\Path; use Statamic\Facades\Stache; @@ -29,6 +31,21 @@ public function setUp(): void Stache::store('form-submissions')->directory($this->directory); } + /** @test */ + public function it_makes_entry_instances_from_files() + { + $item = $this->parent->store('contact_form')->makeItemFromFile( + Path::tidy($this->directory).'/contact_form/1631083591.2832.yaml', + "name: John Smith\nmessage: Hello" + ); + + $this->assertInstanceOf(Submission::class, $item); + $this->assertEquals('1631083591.2832', $item->id()); + $this->assertEquals('John Smith', $item->get('name')); + $this->assertEquals(['name' => 'John Smith', 'message' => 'Hello'], $item->data()->all()); + $this->assertTrue(Carbon::createFromFormat('Y-m-d H:i:s', '2021-09-08 06:46:31')->eq($item->date()->startOfSecond())); + } + /** @test */ public function it_saves_to_disk() { From 3ceadf7966882d7d2a4b8c4f1bf70dead9a5d7b6 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 5 Apr 2024 10:43:36 -0400 Subject: [PATCH 46/46] not needed --- tests/Stache/Stores/FormSubmissionStoreTest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/Stache/Stores/FormSubmissionStoreTest.php b/tests/Stache/Stores/FormSubmissionStoreTest.php index ab757b1d13..b55c801039 100644 --- a/tests/Stache/Stores/FormSubmissionStoreTest.php +++ b/tests/Stache/Stores/FormSubmissionStoreTest.php @@ -19,9 +19,6 @@ public function setUp(): void { parent::setUp(); - $app = app(); - $app['config']->set('statamic.forms.submissions', __DIR__.'/../__fixtures__/content/submissions'); - $this->parent = (new SubmissionsStore)->directory( $this->directory = Path::tidy(__DIR__.'/../__fixtures__/content/submissions') );