Skip to content

Commit 6aa3fd0

Browse files
committed
tests
1 parent 9e4158d commit 6aa3fd0

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Feature\Assets;
4+
5+
use Illuminate\Http\UploadedFile;
6+
use PHPUnit\Framework\Attributes\Test;
7+
use Statamic\Facades\AssetContainer;
8+
use Statamic\Facades\User;
9+
use Tests\FakesRoles;
10+
use Tests\PreventSavingStacheItemsToDisk;
11+
use Tests\TestCase;
12+
13+
class DownloadAssetTest extends TestCase
14+
{
15+
use FakesRoles;
16+
use PreventSavingStacheItemsToDisk;
17+
18+
private $tempDir;
19+
20+
public function setUp(): void
21+
{
22+
parent::setUp();
23+
24+
config(['filesystems.disks.test' => [
25+
'driver' => 'local',
26+
'root' => $this->tempDir = __DIR__.'/tmp',
27+
]]);
28+
}
29+
30+
public function tearDown(): void
31+
{
32+
app('files')->deleteDirectory($this->tempDir);
33+
34+
parent::tearDown();
35+
}
36+
37+
#[Test]
38+
public function it_downloads()
39+
{
40+
$container = AssetContainer::make('test')->disk('test')->save();
41+
$container
42+
->makeAsset('one.txt')
43+
->upload(UploadedFile::fake()->create('one.txt'));
44+
45+
$this->setTestRoles(['test' => ['access cp', 'view test assets']]);
46+
$user = User::make()->assignRole('test')->save();
47+
48+
$this
49+
->actingAs($user)
50+
->getJson('/cp/assets/'.base64_encode('test::one.txt').'/download')
51+
->assertSuccessful()
52+
->assertDownload('one.txt');
53+
}
54+
55+
#[Test]
56+
public function it_404s_when_the_asset_doesnt_exist()
57+
{
58+
$container = AssetContainer::make('test')->disk('test')->save();
59+
60+
$this->setTestRoles(['test' => ['access cp', 'view test assets']]);
61+
$user = User::make()->assignRole('test')->save();
62+
63+
$this
64+
->actingAs($user)
65+
->getJson('/cp/assets/'.base64_encode('test::unknown.txt').'/download')
66+
->assertNotFound();
67+
}
68+
69+
#[Test]
70+
public function it_denies_access_without_permission_to_view_asset()
71+
{
72+
$container = AssetContainer::make('test')->disk('test')->save();
73+
$container
74+
->makeAsset('one.txt')
75+
->upload(UploadedFile::fake()->create('one.txt'));
76+
77+
$this->setTestRoles(['test' => ['access cp']]);
78+
$user = User::make()->assignRole('test')->save();
79+
80+
$this
81+
->actingAs($user)
82+
->getJson('/cp/assets/'.base64_encode('test::one.txt').'/download')
83+
->assertForbidden();
84+
}
85+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Tests\Feature\Assets;
4+
5+
use Illuminate\Http\UploadedFile;
6+
use PHPUnit\Framework\Attributes\Test;
7+
use Statamic\Facades\AssetContainer;
8+
use Statamic\Facades\User;
9+
use Tests\FakesRoles;
10+
use Tests\PreventSavingStacheItemsToDisk;
11+
use Tests\TestCase;
12+
13+
class ShowAssetTest extends TestCase
14+
{
15+
use FakesRoles;
16+
use PreventSavingStacheItemsToDisk;
17+
18+
private $tempDir;
19+
20+
public function setUp(): void
21+
{
22+
parent::setUp();
23+
24+
config(['filesystems.disks.test' => [
25+
'driver' => 'local',
26+
'root' => $this->tempDir = __DIR__.'/tmp',
27+
]]);
28+
}
29+
30+
public function tearDown(): void
31+
{
32+
app('files')->deleteDirectory($this->tempDir);
33+
34+
parent::tearDown();
35+
}
36+
37+
#[Test]
38+
public function it_returns_json()
39+
{
40+
$container = AssetContainer::make('test')->disk('test')->save();
41+
$container
42+
->makeAsset('one.txt')
43+
->upload(UploadedFile::fake()->create('one.txt'));
44+
45+
$this->setTestRoles(['test' => ['access cp', 'view test assets']]);
46+
$user = User::make()->assignRole('test')->save();
47+
48+
$this
49+
->actingAs($user)
50+
->getJson('/cp/assets/'.base64_encode('test::one.txt'))
51+
->assertSuccessful()
52+
->assertJson(['data' => ['id' => 'test::one.txt']]);
53+
}
54+
55+
#[Test]
56+
public function it_404s_when_the_asset_doesnt_exist()
57+
{
58+
$container = AssetContainer::make('test')->disk('test')->save();
59+
60+
$this->setTestRoles(['test' => ['access cp', 'view test assets']]);
61+
$user = User::make()->assignRole('test')->save();
62+
63+
$this
64+
->actingAs($user)
65+
->getJson('/cp/assets/'.base64_encode('test::unknown.txt'))
66+
->assertNotFound();
67+
}
68+
69+
#[Test]
70+
public function it_denies_access_without_permission_to_view_asset()
71+
{
72+
$container = AssetContainer::make('test')->disk('test')->save();
73+
$container
74+
->makeAsset('one.txt')
75+
->upload(UploadedFile::fake()->create('one.txt'));
76+
77+
$this->setTestRoles(['test' => ['access cp']]);
78+
$user = User::make()->assignRole('test')->save();
79+
80+
$this
81+
->actingAs($user)
82+
->getJson('/cp/assets/'.base64_encode('test::one.txt'))
83+
->assertForbidden();
84+
}
85+
}

0 commit comments

Comments
 (0)