Skip to content

Commit

Permalink
Merge pull request getkirby#4741 from getkirby/fix/4740-better-lock-c…
Browse files Browse the repository at this point in the history
…heck

More efficient lock check
  • Loading branch information
distantnative authored Oct 3, 2022
2 parents fb95486 + 39ca913 commit 6c723d5
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 25 deletions.
9 changes: 9 additions & 0 deletions config/api/routes/lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
* Content Lock Routes
*/
return [
[
'pattern' => '(:all)/lock',
'method' => 'GET',
'action' => function (string $path) {
return [
'lock' => $this->parent($path)->lock()?->toArray() ?? false
];
}
],
[
'pattern' => '(:all)/lock',
'method' => 'PATCH',
Expand Down
9 changes: 3 additions & 6 deletions panel/src/components/Forms/FormButtons.vue
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,9 @@ export default {
this.$events.$off("keydown.cmd.s", this.onSave);
},
methods: {
check() {
this.$reload({
navigate: false,
only: "$view.props.lock",
silent: true
});
async check() {
const { lock } = await this.$api.get(this.$view.path + "/lock");
this.$set(this.$view.props, "lock", lock);
},
async onLock(lock = true) {
const api = [this.$view.path + "/lock", null, null, true];
Expand Down
27 changes: 27 additions & 0 deletions src/Cms/ContentLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,33 @@ public function resolve(): bool
return $this->kirby()->locks()->set($this->model, $this->data);
}

/**
* Returns the state for the
* form buttons in the frontend
*/
public function state(): ?string
{
return match (true) {
$this->isUnlocked() => 'unlock',
$this->isLocked() => 'lock',
default => null
};
}

/**
* Returns a usable lock array
* for the frontend
*
* @return array
*/
public function toArray(): array
{
return [
'state' => $this->state(),
'data' => $this->get()
];
}

/**
* Removes current lock and adds lock user to unlock data
*
Expand Down
13 changes: 1 addition & 12 deletions src/Panel/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,18 +235,7 @@ public function isDisabledDropdownOption(string $action, array $options, array $
public function lock(): array|false
{
if ($lock = $this->model->lock()) {
if ($lock->isUnlocked() === true) {
return ['state' => 'unlock'];
}

if ($lock->isLocked() === true) {
return [
'state' => 'lock',
'data' => $lock->get()
];
}

return ['state' => null];
return $lock->toArray();
}

return false;
Expand Down
44 changes: 44 additions & 0 deletions tests/Cms/Api/routes/LockRoutesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Kirby\Cms;

use PHPUnit\Framework\TestCase;

class LockRoutesTest extends TestCase
{
protected $app;

public function setUp(): void
{
$this->app = new App([
'options' => [
'api.allowImpersonation' => true
],
'roots' => [
'index' => '/dev/null'
]
]);
}

public function testGet()
{
$app = $this->app->clone([
'site' => [
'children' => [
[
'slug' => 'a',
]
]
]
]);

$app->impersonate('kirby');

$response = $app->api()->call('pages/a/lock');
$expected = [
'lock' => false
];

$this->assertSame($expected, $response);
}
}
61 changes: 61 additions & 0 deletions tests/Cms/Content/ContentLockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,65 @@ public function testResolveWithRemainingUnlocks()
$app->impersonate('[email protected]');
$this->assertTrue($page->lock()->isUnlocked());
}

public function testState()
{
$app = $this->app;
$page = $app->page('test');

$app->impersonate('[email protected]');

$page->lock()->create();

$this->assertSame(null, $page->lock()->state());

$app->impersonate('[email protected]');

// state is locked
$this->assertSame('lock', $page->lock()->state());

// user force unlocks the lock
$page->lock()->unlock();

$app->impersonate('[email protected]');

// state is now unlock for the original user
$this->assertSame('unlock', $page->lock()->state());
}

public function testToArray()
{
$app = $this->app;
$page = $app->page('test');

$app->impersonate('[email protected]');

$page->lock()->create();

$expected = [
'state' => null,
'data' => false
];

$this->assertSame($expected, $page->lock()->toArray());

$app->impersonate('[email protected]');

$lockArray = $page->lock()->toArray();

// state is locked
$this->assertSame('lock', $lockArray['state']);
$this->assertSame('[email protected]', $lockArray['data']['email']);

// user force unlocks the lock
$page->lock()->unlock();

$app->impersonate('[email protected]');

$lockArray = $page->lock()->toArray();

// state is locked
$this->assertSame('unlock', $lockArray['state']);
$this->assertSame(false, $lockArray['data']);
}
}
6 changes: 3 additions & 3 deletions tests/Panel/Areas/SiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testPage(): void
$model = $props['model'];

$this->assertSame('default', $props['blueprint']);
$this->assertSame(['state' => null], $props['lock']);
$this->assertSame(['state' => null, 'data' => false], $props['lock']);

$this->assertArrayNotHasKey('tab', $props);
$this->assertSame([], $props['tabs']);
Expand Down Expand Up @@ -91,7 +91,7 @@ public function testPageFile(): void
$model = $props['model'];

$this->assertSame('image', $props['blueprint']);
$this->assertSame(['state' => null], $props['lock']);
$this->assertSame(['state' => null, 'data' => false], $props['lock']);

$this->assertArrayNotHasKey('tab', $props);
$this->assertSame([], $props['tabs']);
Expand Down Expand Up @@ -159,7 +159,7 @@ public function testSiteFile(): void
$model = $props['model'];

$this->assertSame('image', $props['blueprint']);
$this->assertSame(['state' => null], $props['lock']);
$this->assertSame(['state' => null, 'data' => false], $props['lock']);

$this->assertArrayNotHasKey('tab', $props);
$this->assertSame([], $props['tabs']);
Expand Down
15 changes: 11 additions & 4 deletions tests/Panel/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
namespace Kirby\Panel;

use Kirby\Cms\App;
use Kirby\Cms\ContentLock;
use Kirby\Cms\Page as ModelPage;
use Kirby\Cms\Site as ModelSite;
use Kirby\Filesystem\Asset;
use Kirby\Filesystem\Dir;
use PHPUnit\Framework\TestCase;

class CustomContentLockIsLocked
class CustomContentLockIsLocked extends ContentLock
{
public function __construct()
{
$this->model = new ModelPage(['slug' => 'test']);
}

public function get(): array
{
return ['email' => '[email protected]'];
Expand All @@ -26,7 +33,7 @@ public function isUnlocked(): bool
}
}

class CustomContentLockIsUnlocked
class CustomContentLockIsUnlocked extends CustomContentLockIslocked
{
public function isUnlocked(): bool
{
Expand Down Expand Up @@ -416,7 +423,7 @@ public function testLock()

// no lock or unlock
$site = new ModelSite();
$this->assertSame(['state' => null], $site->panel()->lock());
$this->assertSame(['state' => null, 'data' => false], $site->panel()->lock());

// lock
$site = new ModelSiteTestForceLocked();
Expand All @@ -426,7 +433,7 @@ public function testLock()

// unlock
$site = new ModelSiteTestForceUnlocked();
$this->assertSame(['state' => 'unlock'], $site->panel()->lock());
$this->assertSame('unlock', $site->panel()->lock()['state']);
}

/**
Expand Down

0 comments on commit 6c723d5

Please sign in to comment.