Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update entry URIs when collection route is changed #379

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Collections/CollectionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Collection as IlluminateCollection;
use Statamic\Contracts\Entries\Collection as CollectionContract;
use Statamic\Eloquent\Jobs\UpdateCollectionEntryUris;
use Statamic\Facades\Blink;
use Statamic\Stache\Repositories\CollectionRepository as StacheRepository;

Expand Down Expand Up @@ -33,8 +34,25 @@ public function findByHandle($handle): ?CollectionContract
public function save($entry)
{
$model = $entry->toModel();
$original = $model->getOriginal();

$model->save();

if (
$model->wasChanged('settings')
&& ($model->settings['routes'] ?? null) !== ($original['settings']['routes'] ?? null)
) {
$dispatch = UpdateCollectionEntryUris::dispatch($entry->handle());

$connection = config('statamic.eloquent-driver.collections.update_entry_uris_connection', 'default');

if ($connection != 'default') {
$dispatch->onConnection($connection);
}

$dispatch->onQueue(config('statamic.eloquent-driver.collections.update_entry_uris_queue', 'default'));
}

Blink::forget("eloquent-collection-{$model->handle}");
Blink::forget('eloquent-collections');

Expand Down
23 changes: 23 additions & 0 deletions src/Jobs/UpdateCollectionEntryUris.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Statamic\Eloquent\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Statamic\Facades\Entry;

class UpdateCollectionEntryUris implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable;

public function __construct(public string $collection) {}

public function handle()
{
Entry::query()
->where('collection', $this->collection)
->chunk(100, fn ($entries) => $entries->each->save());
}
}
68 changes: 68 additions & 0 deletions tests/Entries/CollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Entries;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Eloquent\Collections\Collection;
use Statamic\Eloquent\Collections\CollectionModel;
use Statamic\Eloquent\Jobs\UpdateCollectionEntryUris;
use Statamic\Facades\Collection as CollectionFacade;
use Statamic\Facades\Entry as EntryFacade;
use Tests\TestCase;

class CollectionTest extends TestCase
{
use RefreshDatabase;

#[Test]
public function it_finds_collection()
{
$model = CollectionModel::create([
'title' => 'Blog',
'handle' => 'blog',
'settings' => [],
]);

$find = CollectionFacade::find('blog');

$this->assertTrue($model->is($find->model()));
$this->assertEquals('blog', $find->handle());
$this->assertEquals('Blog', $find->title());
}

#[Test]
public function it_saves_to_collection_model()
{
$collection = (new Collection)->handle('test');

$this->assertDatabaseMissing('collections', ['handle' => 'test']);

$collection->save();

$this->assertDatabaseHas('collections', ['handle' => 'test']);
}

#[Test]
public function changing_a_collections_route_updates_entry_uris()
{
Queue::fake();

$collection = (new Collection)->handle('test');
$collection->save();

EntryFacade::make()->collection('test')->slug('one');
EntryFacade::make()->collection('test')->slug('two');
EntryFacade::make()->collection('test')->slug('three');

$collection->routes(['en' => '/blog/{slug}'])->save();
$collection->routes(['en' => '/{slug}'])->save();
$collection->routes(['en' => '/{slug}'])->save();
$collection->routes(['en' => null])->save();

// The job should only be dispatched three times.
// It shouldn't be dispatched when the route is null or hasn't changed since the last save.
Queue::assertPushed(UpdateCollectionEntryUris::class, 3);
}
}