Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Schedule/ExecuteScheduleTest.php
7461 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client\Server\Schedule;34use Pterodactyl\Models\Task;5use Illuminate\Http\Response;6use Pterodactyl\Models\Schedule;7use Pterodactyl\Models\Permission;8use Illuminate\Support\Facades\Bus;9use Pterodactyl\Jobs\Schedule\RunTaskJob;10use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;1112class ExecuteScheduleTest extends ClientApiIntegrationTestCase13{14/**15* Test that a schedule can be executed and is updated in the database correctly.16*/17#[\PHPUnit\Framework\Attributes\DataProvider('permissionsDataProvider')]18public function testScheduleIsExecutedRightAway(array $permissions)19{20[$user, $server] = $this->generateTestAccount($permissions);2122Bus::fake();2324/** @var Schedule $schedule */25$schedule = Schedule::factory()->create([26'server_id' => $server->id,27]);2829$response = $this->actingAs($user)->postJson($this->link($schedule, '/execute'));30$response->assertStatus(Response::HTTP_BAD_REQUEST);31$response->assertJsonPath('errors.0.code', 'DisplayException');32$response->assertJsonPath('errors.0.detail', 'Cannot process schedule for task execution: no tasks are registered.');3334/** @var Task $task */35$task = Task::factory()->create([36'schedule_id' => $schedule->id,37'sequence_id' => 1,38'time_offset' => 2,39]);4041$this->actingAs($user)->postJson($this->link($schedule, '/execute'))->assertStatus(Response::HTTP_ACCEPTED);4243Bus::assertDispatched(function (RunTaskJob $job) use ($task) {44// A task executed right now should not have any job delay associated with it.45$this->assertNull($job->delay);46$this->assertSame($task->id, $job->task->id);4748return true;49});50}5152/**53* Test that a user without the schedule update permission cannot execute it.54*/55public function testUserWithoutScheduleUpdatePermissionCannotExecute()56{57[$user, $server] = $this->generateTestAccount([Permission::ACTION_SCHEDULE_CREATE]);5859/** @var Schedule $schedule */60$schedule = Schedule::factory()->create(['server_id' => $server->id]);6162$this->actingAs($user)->postJson($this->link($schedule, '/execute'))->assertForbidden();63}6465public static function permissionsDataProvider(): array66{67return [[[]], [[Permission::ACTION_SCHEDULE_UPDATE]]];68}69}707172