Files
unsupervised-scheduler/tests/Unit/Availability/AvailabilityControllerTest.php
T
thatguygriffandClaude Fable 5 266f572884
CI / Tests (PHP 8.2) (pull_request) Successful in 38s
CI / Tests (PHP 8.1) (pull_request) Successful in 48s
CI / No Debug Code (pull_request) Successful in 3s
CI / Coding Standards (pull_request) Successful in 2m49s
CI / PHPStan (pull_request) Successful in 2m53s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m38s
CI / Build Plugin Zip (pull_request) Skipped
Default the availability admin page to the week view too
Follow-up demo feedback: the My Availability page now opens in its weekly
calendar (usc_view=list opts back into the table, which keeps the bulk-delete
form), matching the new lessons defaults.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-07-22 11:09:23 -03:00

142 lines
5.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit\Availability;
use Brain\Monkey\Functions;
use Mockery;
use Unsupervised\Schedular\Availability\AvailabilityController;
use Unsupervised\Schedular\Availability\AvailabilityRepository;
use Unsupervised\Schedular\Availability\AvailabilitySlot;
use Unsupervised\Schedular\Offering\OfferingRepository;
use Unsupervised\Schedular\Tests\Unit\TestCase;
class AvailabilityControllerTest extends TestCase
{
private AvailabilityRepository&Mockery\MockInterface $repository;
private OfferingRepository&Mockery\MockInterface $offerings;
private AvailabilityController $controller;
protected function setUp(): void
{
parent::setUp();
$this->repository = Mockery::mock(AvailabilityRepository::class);
$this->offerings = Mockery::mock(OfferingRepository::class);
$this->controller = new AvailabilityController($this->repository, $this->offerings);
$_POST = [];
$_GET = [];
Functions\when('current_user_can')->justReturn(true);
Functions\when('get_current_user_id')->justReturn(3);
Functions\when('check_admin_referer')->justReturn(true);
Functions\when('wp_unslash')->returnArg();
Functions\when('sanitize_text_field')->returnArg();
Functions\when('sanitize_key')->alias(
static fn ($key) => strtolower((string) preg_replace('/[^a-zA-Z0-9_\-]/', '', (string) $key))
);
Functions\when('absint')->alias(static fn ($value) => abs((int) $value));
Functions\when('get_option')->justReturn(1);
Functions\when('current_time')->justReturn('2026-07-06');
Functions\when('admin_url')->justReturn('admin.php?page=us-availability');
Functions\when('add_query_arg')->justReturn('admin.php?page=us-availability&usc_view=week');
Functions\when('wp_nonce_field')->justReturn('');
Functions\when('submit_button')->justReturn('');
Functions\when('mysql2date')->alias(
static fn (string $format, string $date) => date($format, (int) strtotime($date))
);
$this->offerings->shouldReceive('findAll')->andReturn([]);
}
public function testBulkDeleteRemovesOnlyOwnedExistingSlots(): void
{
$_POST = [
'usc_action' => 'bulk_delete',
'slot_ids' => ['5', 'junk', '7', '9'],
];
$owned = new AvailabilitySlot(instructorId: 3, startDt: '2026-07-08 09:00:00', endDt: '2026-07-08 10:00:00', id: 5);
$other = new AvailabilitySlot(instructorId: 4, startDt: '2026-07-08 09:00:00', endDt: '2026-07-08 10:00:00', id: 7);
$this->repository->shouldReceive('findById')->once()->with(5)->andReturn($owned);
$this->repository->shouldReceive('findById')->once()->with(7)->andReturn($other);
$this->repository->shouldReceive('findById')->once()->with(9)->andReturn(null);
$this->repository->shouldReceive('delete')->once()->with(5)->andReturn(true);
$this->repository->shouldReceive('findByInstructor')->once()->with(3)->andReturn([]);
$this->render();
}
public function testBulkDeleteIgnoresNonArrayInput(): void
{
$_POST = [
'usc_action' => 'bulk_delete',
'slot_ids' => '5',
];
$this->repository->shouldNotReceive('findById');
$this->repository->shouldNotReceive('delete');
$this->repository->shouldReceive('findByInstructor')->once()->with(3)->andReturn([]);
$this->render();
}
public function testSingleDeleteChecksOwnership(): void
{
$_POST = [
'usc_action' => 'delete',
'slot_id' => '7',
];
$other = new AvailabilitySlot(instructorId: 4, startDt: '2026-07-08 09:00:00', endDt: '2026-07-08 10:00:00', id: 7);
$this->repository->shouldReceive('findById')->once()->with(7)->andReturn($other);
$this->repository->shouldNotReceive('delete');
$this->repository->shouldReceive('findByInstructor')->once()->with(3)->andReturn([]);
$this->render();
}
public function testDefaultsToWeekView(): void
{
$slot = new AvailabilitySlot(instructorId: 3, startDt: '2026-07-08 09:00:00', endDt: '2026-07-08 10:00:00', id: 5);
$this->repository->shouldReceive('findByInstructor')->once()->with(3)->andReturn([$slot]);
$html = $this->render();
// Week of Monday 2026-07-06 (start_of_week = 1, today = 2026-07-06).
self::assertStringContainsString('Week of', $html);
// The list table (with its bulk-delete form) is not rendered by default.
self::assertStringNotContainsString('id="usc-bulk-delete-form"', $html);
}
public function testListViewRendersBulkCheckboxesOnlyForUnbookedSlots(): void
{
$_GET['usc_view'] = 'list';
$unbooked = new AvailabilitySlot(instructorId: 3, startDt: '2026-07-08 09:00:00', endDt: '2026-07-08 10:00:00', id: 5);
$booked = new AvailabilitySlot(instructorId: 3, startDt: '2026-07-08 10:00:00', endDt: '2026-07-08 11:00:00', isBooked: true, id: 6);
$this->repository->shouldReceive('findByInstructor')->once()->with(3)->andReturn([$unbooked, $booked]);
$html = $this->render();
self::assertStringContainsString('id="usc-bulk-delete-form"', $html);
self::assertStringContainsString('value="bulk_delete"', $html);
self::assertStringContainsString('Delete selected', $html);
self::assertStringContainsString('name="slot_ids[]" form="usc-bulk-delete-form" value="5"', $html);
self::assertStringNotContainsString('name="slot_ids[]" form="usc-bulk-delete-form" value="6"', $html);
}
private function render(): string
{
ob_start();
$this->controller->renderPage();
return (string) ob_get_clean();
}
}