Skip to content

Commit

Permalink
workaround for a change to isNewForSite
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot committed Oct 1, 2024
1 parent 477d32b commit a47b0f8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"slug": "foo-bar",
"isDraft": false,
"isRevision": false,
"isNewForSite": true,
"isNewForSite": false,
"isUnpublishedDraft": false,
"enabled": true,
"archived": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"slug": "foo-bar",
"isDraft": false,
"isRevision": false,
"isNewForSite": true,
"isNewForSite": false,
"isUnpublishedDraft": false,
"enabled": true,
"archived": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"slug": "foo-bar",
"isDraft": false,
"isRevision": false,
"isNewForSite": true,
"isNewForSite": false,
"isUnpublishedDraft": false,
"enabled": true,
"archived": false,
Expand Down
69 changes: 40 additions & 29 deletions tests/SnapshotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,6 @@
->renderTemplate('variable', ['foo' => 'bar'])
->assertMatchesSnapshot();

it('includes postDate in snapshots')
->expect(fn () => Entry::factory()
->section('posts')
->postDate('2022-01-01 00:00:00')
->title('foo bar')
->create())
->toSnapshot(['postDate'])->toMatchSnapshot();

it('includes postDate in snapshot assertions', function () {
$entry = Entry::factory()
->section('posts')
->postDate('2022-01-01 00:00:00')
->title('foo bar')
->create();

$entry->assertMatchesSnapshot(['postDate']);
});

it('matches entry snapshots', function () {
$entry = Entry::factory()
->section('posts')
->title('foo bar')
->textField('foo')
->dropdownField('one')
->create();

expect($entry)->toMatchSnapshot();
});

it('matches nested entry snapshots', function () {
$child = Entry::factory()
->section('posts')
Expand All @@ -91,3 +62,43 @@
$snapshots = collect($entries->map->toSnapshotArray())->sortBy('title')->values()->all();
expect(json_encode($snapshots))->toMatchSnapshot();
});

// Before 5.3.3 Craft reported isNewForSite as `true` after a save but thanks to
// https://github.com/craftcms/cms/issues/15517 it switched to `false. So, to avoid splitting our
// tests we don't actually check snapshots on newly created elements. We re-fetch them from
// the database to be sure we're not running in to differences with isNewForSite between
// Craft versions
it('includes postDate in snapshots', function () {
$entry = Entry::factory()
->section('posts')
->postDate('2022-01-01 00:00:00')
->title('foo bar')
->create();

expect(\craft\elements\Entry::find()->id($entry->id)->one())
->toSnapshot(['postDate'])
->toMatchSnapshot();
});

it('includes postDate in snapshot assertions', function () {
$entry = Entry::factory()
->section('posts')
->postDate('2022-01-01 00:00:00')
->title('foo bar')
->create();

expect(\craft\elements\Entry::find()->id($entry->id)->one())
->assertMatchesSnapshot(['postDate']);
});

it('matches entry snapshots', function () {
$entry = Entry::factory()
->section('posts')
->title('foo bar')
->textField('foo')
->dropdownField('one')
->create();

expect(\craft\elements\Entry::find()->id($entry->id)->one())
->toMatchSnapshot();
});

0 comments on commit a47b0f8

Please sign in to comment.