Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Schedule/UpdateServerScheduleTest.php
7461 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client\Server\Schedule;34use Pterodactyl\Models\Schedule;5use Pterodactyl\Helpers\Utilities;6use Pterodactyl\Models\Permission;7use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;89class UpdateServerScheduleTest extends ClientApiIntegrationTestCase10{11/**12* The data to use when updating a schedule.13*/14private array $updateData = [15'name' => 'Updated Schedule Name',16'minute' => '5',17'hour' => '*',18'day_of_week' => '*',19'month' => '*',20'day_of_month' => '*',21'is_active' => false,22];2324/**25* Test that a schedule can be updated.26*/27#[\PHPUnit\Framework\Attributes\DataProvider('permissionsDataProvider')]28public function testScheduleCanBeUpdated(array $permissions)29{30[$user, $server] = $this->generateTestAccount($permissions);3132/** @var Schedule $schedule */33$schedule = Schedule::factory()->create(['server_id' => $server->id]);34$expected = Utilities::getScheduleNextRunDate('5', '*', '*', '*', '*');3536$response = $this->actingAs($user)37->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}", $this->updateData);3839$schedule = $schedule->refresh();4041$response->assertOk();42$this->assertSame('Updated Schedule Name', $schedule->name);43$this->assertFalse($schedule->is_active);44$this->assertJsonTransformedWith($response->json('attributes'), $schedule);4546$this->assertSame($expected->toAtomString(), $schedule->next_run_at->toAtomString());47}4849/**50* Test that an error is returned if the schedule exists but does not belong to this51* specific server instance.52*/53public function testErrorIsReturnedIfScheduleDoesNotBelongToServer()54{55[$user, $server] = $this->generateTestAccount();56$server2 = $this->createServerModel(['owner_id' => $user->id]);5758$schedule = Schedule::factory()->create(['server_id' => $server2->id]);5960$this->actingAs($user)61->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}")62->assertNotFound();63}6465/**66* Test that an error is returned if the subuser does not have permission to modify a67* server schedule.68*/69public function testErrorIsReturnedIfSubuserDoesNotHavePermissionToModifySchedule()70{71[$user, $server] = $this->generateTestAccount([Permission::ACTION_SCHEDULE_CREATE]);7273$schedule = Schedule::factory()->create(['server_id' => $server->id]);7475$this->actingAs($user)76->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}")77->assertForbidden();78}7980/**81* Test that the "is_processing" field gets reset to false when the schedule is enabled82* or disabled so that an invalid state can be more easily fixed.83*84* @see https://github.com/pterodactyl/panel/issues/242585*/86public function testScheduleIsProcessingIsSetToFalseWhenActiveStateChanges()87{88[$user, $server] = $this->generateTestAccount();8990/** @var Schedule $schedule */91$schedule = Schedule::factory()->create([92'server_id' => $server->id,93'is_active' => true,94'is_processing' => true,95]);9697$this->assertTrue($schedule->is_active);98$this->assertTrue($schedule->is_processing);99100$response = $this->actingAs($user)101->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}", $this->updateData);102103$schedule = $schedule->refresh();104105$response->assertOk();106$this->assertFalse($schedule->is_active);107$this->assertFalse($schedule->is_processing);108}109110public static function permissionsDataProvider(): array111{112return [[[]], [[Permission::ACTION_SCHEDULE_UPDATE]]];113}114}115116117