Skip to content
Draft
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
9 changes: 9 additions & 0 deletions src/Tokens/TokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@

namespace Statamic\Eloquent\Tokens;

use Illuminate\Support\Collection;
use Statamic\Contracts\Tokens\Token as TokenContract;
use Statamic\Tokens\TokenRepository as Repository;

class TokenRepository extends Repository
{
public function all(): Collection
{
return app('statamic.eloquent.tokens.model')::query()
->get()
->map(fn ($model) => Token::fromModel($model))
->values();
}

public function find(string $token): ?Token
{
$model = app('statamic.eloquent.tokens.model')::whereToken($token)->first();
Expand Down
31 changes: 15 additions & 16 deletions tests/Entries/EntryRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public function it_updates_the_order_of_all_entries_in_a_collection()
(new Entry)->id(3)->collection($collection)->slug('charlie')->save();
(new Entry)->id(4)->collection($collection)->slug('delta')->save();

$orders = $this->entryOrders();

$collection->structure()->in('en')->tree([
['entry' => 4],
['entry' => 2],
Expand All @@ -103,12 +105,7 @@ public function it_updates_the_order_of_all_entries_in_a_collection()

// Assert that the order is unchanged, to make sure that saving
// the structure isn't what caused the order to be updated.
$this->assertEquals([
1 => 1,
2 => 1,
3 => 1,
4 => 1,
], EntryModel::all()->mapWithKeys(fn ($e) => [$e->id => $e->order])->all());
$this->assertEquals($orders, $this->entryOrders());

(new EntryRepository(new Stache))->updateOrders($collection);

Expand All @@ -117,7 +114,7 @@ public function it_updates_the_order_of_all_entries_in_a_collection()
2 => 2,
3 => 4,
4 => 1,
], EntryModel::all()->mapWithKeys(fn ($e) => [$e->id => $e->order])->all());
], $this->entryOrders());
}

#[Test]
Expand All @@ -134,6 +131,8 @@ public function it_updates_the_order_of_specific_entries_in_a_collection()
(new Entry)->id(3)->collection($collection)->slug('charlie')->save();
(new Entry)->id(4)->collection($collection)->slug('delta')->save();

$orders = $this->entryOrders();

$collection->structure()->in('en')->tree([
['entry' => 4],
['entry' => 2],
Expand All @@ -143,21 +142,16 @@ public function it_updates_the_order_of_specific_entries_in_a_collection()

// Assert that the order is unchanged, to make sure that saving
// the structure isn't what caused the order to be updated.
$this->assertEquals([
1 => 1,
2 => 1,
3 => 1,
4 => 1,
], EntryModel::all()->mapWithKeys(fn ($e) => [$e->id => $e->order])->all());
$this->assertEquals($orders, $this->entryOrders());

(new EntryRepository(new Stache))->updateOrders($collection, [2, 3]);

$this->assertEquals([
1 => 1,
1 => $orders[1],
2 => 2,
3 => 4,
4 => 1,
], EntryModel::all()->mapWithKeys(fn ($e) => [$e->id => $e->order])->all());
4 => $orders[4],
], $this->entryOrders());
}

#[Test]
Expand Down Expand Up @@ -310,4 +304,9 @@ public function it_skips_missing_entires_when_finding_by_ids()

$this->assertEquals([$expected->id()], $actual->map->id()->all());
}

private function entryOrders(): array
{
return EntryModel::all()->mapWithKeys(fn ($e) => [$e->id => $e->order])->all();
}
}
12 changes: 12 additions & 0 deletions tests/Repositories/TokenRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ public function it_gets_a_token()
$this->assertNull($this->repo->find('unknown'));
}

#[Test]
public function it_gets_all_tokens()
{
$this->repo->make('expired', 'ExampleHandler')->expireAt(now()->subMinute())->save();
$this->repo->make('fresh', 'ExampleHandler')->expireAt(now()->addHour())->save();

$this->assertEquals(
['abc', 'expired', 'fresh'],
$this->repo->all()->map->token()->sort()->values()->all()
);
}

#[Test]
public function it_saves_a_token_to_the_database()
{
Expand Down