Skip to content

Commit

Permalink
[5.x] Implement NavCreating / NavSaving / NavCreated events (#10604)
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean authored Aug 9, 2024
1 parent 0b85403 commit bd3f907
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/Events/NavCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class NavCreated extends Event
{
public $nav;

public function __construct($nav)
{
$this->nav = $nav;
}
}
23 changes: 23 additions & 0 deletions src/Events/NavCreating.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Statamic\Events;

class NavCreating extends Event
{
public $nav;

public function __construct($nav)
{
$this->nav = $nav;
}

/**
* Dispatch the event with the given arguments, and halt on first non-null listener response.
*
* @return mixed
*/
public static function dispatch()
{
return event(new static(...func_get_args()), [], true);
}
}
23 changes: 23 additions & 0 deletions src/Events/NavSaving.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Statamic\Events;

class NavSaving extends Event
{
public $nav;

public function __construct($nav)
{
$this->nav = $nav;
}

/**
* Dispatch the event with the given arguments, and halt on first non-null listener response.
*
* @return mixed
*/
public static function dispatch()
{
return event(new static(...func_get_args()), [], true);
}
}
17 changes: 17 additions & 0 deletions src/Structures/Nav.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
use Statamic\Contracts\Structures\NavTreeRepository;
use Statamic\Data\ExistsAsFile;
use Statamic\Events\NavBlueprintFound;
use Statamic\Events\NavCreated;
use Statamic\Events\NavCreating;
use Statamic\Events\NavDeleted;
use Statamic\Events\NavDeleting;
use Statamic\Events\NavSaved;
use Statamic\Events\NavSaving;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Facades\Blueprint;
Expand All @@ -27,8 +30,22 @@ class Nav extends Structure implements Contract

public function save()
{
$isNew = ! Facades\Nav::find($this->handle());

if ($isNew && NavCreating::dispatch($this) === false) {
return false;
}

if (NavSaving::dispatch($this) === false) {
return false;
}

Facades\Nav::save($this);

if ($isNew) {
NavCreated::dispatch($this);
}

NavSaved::dispatch($this);

return true;
Expand Down
79 changes: 78 additions & 1 deletion tests/Data/Structures/NavTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
use Illuminate\Support\Facades\Event;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Contracts\Entries\Collection as StatamicCollection;
use Statamic\Events\NavCreated;
use Statamic\Events\NavCreating;
use Statamic\Events\NavDeleted;
use Statamic\Events\NavDeleting;
use Statamic\Events\NavSaved;
use Statamic\Events\NavSaving;
use Statamic\Facades;
use Statamic\Facades\Site;
use Statamic\Facades\User;
Expand Down Expand Up @@ -101,11 +105,84 @@ public function it_gets_and_sets_the_title()
#[Test]
public function it_saves_the_nav_through_the_api()
{
$nav = $this->structure();
$nav = $this->structure('test');

Facades\Nav::shouldReceive('find')->with('test');
Facades\Nav::shouldReceive('save')->with($nav)->once();

$this->assertTrue($nav->save());
}

#[Test]
public function it_dispatches_nav_creating()
{
Event::fake();

$nav = $this->structure('test');

Facades\Nav::shouldReceive('find')->with('test')->once();
Facades\Nav::shouldReceive('save')->with($nav)->once();

$this->assertTrue($nav->save());

Event::assertDispatched(NavCreating::class, function ($event) use ($nav) {
return $event->nav === $nav;
});
}

#[Test]
public function if_creating_event_returns_false_the_nav_doesnt_save()
{
Event::fake([NavCreated::class]);

Event::listen(NavCreating::class, function () {
return false;
});

$nav = $this->structure('test');

Facades\Nav::shouldReceive('find')->with('test')->once();
Facades\Nav::shouldReceive('save')->with($nav)->never();

$this->assertFalse($nav->save());

Event::assertNotDispatched(NavCreated::class);
}

#[Test]
public function it_dispatches_nav_saving()
{
Event::fake();

$nav = $this->structure('test');

Facades\Nav::shouldReceive('find')->with('test')->once();
Facades\Nav::shouldReceive('save')->with($nav)->once();

$this->assertTrue($nav->save());

Event::assertDispatched(NavSaving::class, function ($event) use ($nav) {
return $event->nav === $nav;
});
}

#[Test]
public function if_saving_event_returns_false_the_nav_doesnt_save()
{
Event::fake([NavSaved::class]);

Event::listen(NavSaving::class, function () {
return false;
});

$nav = $this->structure('test');

Facades\Nav::shouldReceive('find')->with('test')->once();
Facades\Nav::shouldReceive('save')->with($nav)->never();

$this->assertFalse($nav->save());

Event::assertNotDispatched(NavSaved::class);
}

#[Test]
Expand Down
1 change: 1 addition & 0 deletions tests/Stache/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ public function it_saves_structures()
{
$structure = Structure::find('footer');

NavRepository::shouldReceive('find')->with('footer');
NavRepository::shouldReceive('save')->with($structure)->once();

$structure->save();
Expand Down

0 comments on commit bd3f907

Please sign in to comment.