Skip to content

Commit

Permalink
First tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianallgeier committed Feb 27, 2025
1 parent ab81807 commit 6b24726
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
8 changes: 6 additions & 2 deletions src/Cms/FileActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kirby\Cms;

use Closure;
use Kirby\Content\ImmutableMemoryStorage;
use Kirby\Content\MemoryStorage;
use Kirby\Content\VersionCache;
use Kirby\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -152,6 +153,9 @@ protected function commit(
): mixed {
$kirby = $this->kirby();

$old = $this->clone();
$old->changeStorage(ImmutableMemoryStorage::class);

// check file rules
$this->rules()->$action(...array_values($arguments));

Expand Down Expand Up @@ -180,8 +184,8 @@ protected function commit(
// determine arguments for `after` hook depending on the action
$argumentsAfter = match ($action) {
'create' => ['file' => $result],
'delete' => ['status' => $result, 'file' => $this],
default => ['newFile' => $result, 'oldFile' => $this]
'delete' => ['status' => $result, 'file' => $old],
default => ['newFile' => $result, 'oldFile' => $old]
};

// run `after` hook and apply return to action result
Expand Down
10 changes: 2 additions & 8 deletions src/Cms/ModelWithContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,20 +431,14 @@ public function save(
string|null $languageCode = null,
bool $overwrite = false
): static {
// create a clone to avoid modifying the original
$clone = $this->clone();

// move the old model into memory
$this->changeStorage(ImmutableMemoryStorage::class);

// update the clone
$clone->version()->save(
$this->version()->save(
$data ?? [],
$languageCode ?? 'default',
$overwrite
);

return $clone;
return $this;
}

/**
Expand Down
10 changes: 7 additions & 3 deletions src/Cms/PageActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kirby\Cms;

use Closure;
use Kirby\Content\ImmutableMemoryStorage;
use Kirby\Content\MemoryStorage;
use Kirby\Content\VersionCache;
use Kirby\Content\VersionId;
Expand Down Expand Up @@ -331,6 +332,9 @@ protected function commit(
): mixed {
$kirby = $this->kirby();

$old = $this->clone();
$old->changeStorage(ImmutableMemoryStorage::class);

// check page rules
$this->rules()->$action(...array_values($arguments));

Expand Down Expand Up @@ -359,9 +363,9 @@ protected function commit(
// determine arguments for `after` hook depending on the action
$argumentsAfter = match ($action) {
'create' => ['page' => $result],
'duplicate' => ['duplicatePage' => $result, 'originalPage' => $this],
'delete' => ['status' => $result, 'page' => $this],
default => ['newPage' => $result, 'oldPage' => $this]
'duplicate' => ['duplicatePage' => $result, 'originalPage' => $old],
'delete' => ['status' => $result, 'page' => $old],
default => ['newPage' => $result, 'oldPage' => $old]
};

// run `after` hook and apply return to action result
Expand Down
6 changes: 5 additions & 1 deletion src/Cms/SiteActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kirby\Cms;

use Closure;
use Kirby\Content\ImmutableMemoryStorage;

/**
* SiteActions
Expand Down Expand Up @@ -31,6 +32,9 @@ protected function commit(
): mixed {
$kirby = $this->kirby();

$old = $this->clone();
$old->changeStorage(ImmutableMemoryStorage::class);

// check site rules
$this->rules()->$action(...array_values($arguments));

Expand Down Expand Up @@ -59,7 +63,7 @@ protected function commit(
// (first argument, usually the new model) if anything returned
$result = $kirby->apply(
'site.' . $action . ':after',
['newSite' => $result, 'oldSite' => $this],
['newSite' => $result, 'oldSite' => $old],
'newSite'
);

Expand Down
8 changes: 6 additions & 2 deletions src/Cms/UserActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kirby\Cms;

use Closure;
use Kirby\Content\ImmutableMemoryStorage;
use Kirby\Content\MemoryStorage;
use Kirby\Data\Data;
use Kirby\Data\Json;
Expand Down Expand Up @@ -157,6 +158,9 @@ protected function commit(

$kirby = $this->kirby();

$old = $this->clone();
$old->changeStorage(ImmutableMemoryStorage::class);

// check user rules
$this->rules()->$action(...array_values($arguments));

Expand Down Expand Up @@ -185,8 +189,8 @@ protected function commit(
// determine arguments for `after` hook depending on the action
$argumentsAfter = match ($action) {
'create' => ['user' => $result],
'delete' => ['status' => $result, 'user' => $this],
default => ['newUser' => $result, 'oldUser' => $this]
'delete' => ['status' => $result, 'user' => $old],
default => ['newUser' => $result, 'oldUser' => $old]
};

// run `after` hook and apply return to action result
Expand Down

0 comments on commit 6b24726

Please sign in to comment.