Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Schedule/CreateServerScheduleTest.php
7461 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client\Server\Schedule;34use Illuminate\Http\Response;5use Pterodactyl\Models\Schedule;6use Pterodactyl\Models\Permission;7use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;89class CreateServerScheduleTest extends ClientApiIntegrationTestCase10{11/**12* Test that a schedule can be created for the server.13*/14#[\PHPUnit\Framework\Attributes\DataProvider('permissionsDataProvider')]15public function testScheduleCanBeCreatedForServer(array $permissions)16{17[$user, $server] = $this->generateTestAccount($permissions);1819$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/schedules", [20'name' => 'Test Schedule',21'is_active' => false,22'minute' => '0',23'hour' => '*/2',24'day_of_week' => '2',25'month' => '1',26'day_of_month' => '*',27]);2829$response->assertOk();3031$this->assertNotNull($id = $response->json('attributes.id'));3233/** @var Schedule $schedule */34$schedule = Schedule::query()->findOrFail($id);35$this->assertFalse($schedule->is_active);36$this->assertFalse($schedule->is_processing);37$this->assertSame('0', $schedule->cron_minute);38$this->assertSame('*/2', $schedule->cron_hour);39$this->assertSame('2', $schedule->cron_day_of_week);40$this->assertSame('1', $schedule->cron_month);41$this->assertSame('*', $schedule->cron_day_of_month);42$this->assertSame('Test Schedule', $schedule->name);4344$this->assertJsonTransformedWith($response->json('attributes'), $schedule);45$response->assertJsonCount(0, 'attributes.relationships.tasks.data');46}4748/**49* Test that the validation rules for scheduling work as expected.50*/51public function testScheduleValidationRules()52{53[$user, $server] = $this->generateTestAccount();5455$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/schedules", []);5657$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);58foreach (['name', 'minute', 'hour', 'day_of_month', 'day_of_week'] as $i => $field) {59$response->assertJsonPath("errors.$i.code", 'ValidationException');60$response->assertJsonPath("errors.$i.meta.rule", 'required');61$response->assertJsonPath("errors.$i.meta.source_field", $field);62}6364$this->actingAs($user)65->postJson("/api/client/servers/$server->uuid/schedules", [66'name' => 'Testing',67'is_active' => 'no',68'minute' => '*',69'hour' => '*',70'day_of_month' => '*',71'month' => '*',72'day_of_week' => '*',73])74->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)75->assertJsonPath('errors.0.meta.rule', 'boolean');76}7778/**79* Test that a subuser without required permissions cannot create a schedule.80*/81public function testSubuserCannotCreateScheduleWithoutPermissions()82{83[$user, $server] = $this->generateTestAccount([Permission::ACTION_SCHEDULE_UPDATE]);8485$this->actingAs($user)86->postJson("/api/client/servers/$server->uuid/schedules", [])87->assertForbidden();88}8990public static function permissionsDataProvider(): array91{92return [[[]], [[Permission::ACTION_SCHEDULE_CREATE]]];93}94}959697