-
-
Notifications
You must be signed in to change notification settings - Fork 627
Expand file tree
/
Copy pathFormSubmissionStoreTest.php
More file actions
77 lines (62 loc) · 2.72 KB
/
FormSubmissionStoreTest.php
File metadata and controls
77 lines (62 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace Tests\Stache\Stores;
use Illuminate\Support\Carbon;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Contracts\Forms\Submission;
use Statamic\Facades;
use Statamic\Facades\Path;
use Statamic\Facades\Stache;
use Statamic\Stache\Stores\SubmissionsStore;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;
class FormSubmissionStoreTest extends TestCase
{
use PreventSavingStacheItemsToDisk;
public function setUp(): void
{
parent::setUp();
$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_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_sanitizes_non_utf8_data()
{
$item = $this->parent->store('contact_form')->makeItemFromFile(
Path::tidy($this->directory).'/contact_form/1631083591.2832.yaml',
"name: 'Test User'\nmessage: !!binary dGVzdCBtZXNzYWdlIHdpdGggYmFkIGJ5dGVzOiDtoL3tsYk="
);
$this->assertInstanceOf(Submission::class, $item);
$this->assertEquals('Test User', $item->get('name'));
$this->assertStringContainsString('test message with bad bytes:', $item->get('message'));
$this->assertTrue(mb_check_encoding($item->get('message'), 'UTF-8'));
$this->assertNotNull(json_encode($item->data()->all()));
}
#[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->assertFileDoesNotExist($path);
$this->assertEquals($path, $this->parent->store('test_form')->paths()->get($submission->id()));
}
}