Files
animethemes-server/tests/Feature/Http/Wiki/Video/Script/ScriptTest.php
T
2025-06-27 02:52:13 -03:00

109 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Http\Wiki\Video\Script;
use App\Constants\Config\VideoConstants;
use App\Enums\Auth\SpecialPermission;
use App\Features\AllowScriptDownloading;
use App\Models\Auth\User;
use App\Models\Wiki\Video\VideoScript;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\Testing\File;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Storage;
use Laravel\Pennant\Feature;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
/**
* Class ScriptTest.
*/
class ScriptTest extends TestCase
{
use WithFaker;
/**
* If script downloading is disabled through the Allow Script Downloading feature,
* the user shall receive a forbidden exception.
*
* @return void
*/
public function test_script_downloading_not_allowed_forbidden(): void
{
Feature::deactivate(AllowScriptDownloading::class);
$script = VideoScript::factory()->createOne();
$response = $this->get(route('videoscript.show', ['videoscript' => $script]));
$response->assertForbidden();
}
/**
* Users with the bypass feature flag permission shall be permitted to download scripts
* even if the Allow Script Downloading feature is disabled.
*
* @return void
*/
public function test_video_streaming_permitted_for_bypass(): void
{
Feature::activate(AllowScriptDownloading::class, $this->faker->boolean());
$fs = Storage::fake(Config::get(VideoConstants::SCRIPT_DISK_QUALIFIED));
$file = File::fake()->create($this->faker->word().'.txt');
$fsFile = $fs->putFile('', $file);
$script = VideoScript::factory()->createOne([
VideoScript::ATTRIBUTE_PATH => $fsFile,
]);
$user = User::factory()->withPermissions(SpecialPermission::BYPASS_FEATURE_FLAGS->value)->createOne();
Sanctum::actingAs($user);
$response = $this->get(route('videoscript.show', ['videoscript' => $script]));
$response->assertDownload($script->path);
}
/**
* If the script is soft-deleted, the user shall receive a not found exception.
*
* @return void
*/
public function test_cannot_stream_soft_deleted_video(): void
{
Feature::activate(AllowScriptDownloading::class);
$script = VideoScript::factory()->trashed()->createOne();
$response = $this->get(route('videoscript.show', ['videoscript' => $script]));
$response->assertNotFound();
}
/**
* If script downloading is enabled, the script is downloaded from storage through the response.
*
* @return void
*/
public function test_downloaded_through_response(): void
{
Feature::activate(AllowScriptDownloading::class);
$fs = Storage::fake(Config::get(VideoConstants::SCRIPT_DISK_QUALIFIED));
$file = File::fake()->create($this->faker->word().'.txt');
$fsFile = $fs->putFile('', $file);
$script = VideoScript::factory()->createOne([
VideoScript::ATTRIBUTE_PATH => $fsFile,
]);
$response = $this->get(route('videoscript.show', ['videoscript' => $script]));
$response->assertDownload($script->path);
}
}