1d6ac46ba3
CI / No Debug Code (pull_request) Successful in 3s
CI / Tests (PHP 8.2) (pull_request) Successful in 48s
CI / Tests (PHP 8.3) (pull_request) Successful in 52s
CI / Coding Standards (pull_request) Successful in 57s
CI / Tests (PHP 8.1) (pull_request) Successful in 1m1s
CI / PHPStan (pull_request) Successful in 1m11s
CI / Build Plugin Zip (pull_request) Has been skipped
- Bump phpstan/phpstan ^2.0 and szepeviktor/phpstan-wordpress ^2.0 - Move the analysis level into phpstan.neon (single source) and raise it to 10 - Add Val, a runtime coercion helper that narrows untyped WordPress boundary values (wpdb rows, REST params, superglobals, options) with explicit checks instead of blind casts, plus unit tests - Type value-object fromRow() params as stdClass (what wpdb returns) and map columns through Val so unexpected shapes degrade safely - Use %i identifier placeholders for table names in all wpdb::prepare() calls so every query string is a literal and identifiers are escaped by WordPress; raises the minimum WordPress version to 6.2 where %i was introduced - Guard wpdb::prepare() null result before wpdb::query() in updateTax() - Fix nullable get_permalink()/strtotime() handling, list types at REST and capability call sites, dead null-coalescing on checked superglobals, and narrow get_users() results before mapping - Register Val method names with the ValidatedSanitizedInput sniff so it validates the real sanitizer around each superglobal read - Update repository unit tests for the %i placeholder arguments Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
216 lines
7.0 KiB
PHP
216 lines
7.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Unsupervised\Schedular\Tests\Unit\Availability;
|
|
|
|
use Brain\Monkey\Functions;
|
|
use Mockery;
|
|
use Unsupervised\Schedular\Availability\AvailabilityRepository;
|
|
use Unsupervised\Schedular\Availability\AvailabilitySlot;
|
|
use Unsupervised\Schedular\Tests\Unit\TestCase;
|
|
|
|
class AvailabilityRepositoryTest extends TestCase
|
|
{
|
|
private \wpdb $db;
|
|
private AvailabilityRepository $repo;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->db = Mockery::mock(\wpdb::class);
|
|
$this->db->prefix = 'wp_';
|
|
$this->repo = new AvailabilityRepository($this->db);
|
|
}
|
|
|
|
public function testInsertCallsWpdbInsertAndReturnsId(): void
|
|
{
|
|
Functions\expect('current_time')->with('mysql')->andReturn('2026-04-01 12:00:00');
|
|
|
|
$this->db->shouldReceive('insert')
|
|
->once()
|
|
->with(
|
|
'wp_us_availability',
|
|
Mockery::on(static function (array $data): bool {
|
|
return $data['instructor_id'] === 5
|
|
&& $data['start_dt'] === '2026-04-01 09:00:00'
|
|
&& $data['duration_minutes'] === 30
|
|
&& $data['offering_id'] === 8
|
|
&& $data['is_booked'] === 0;
|
|
}),
|
|
['%d', '%d', '%s', '%s', '%d', '%d', '%d', '%s']
|
|
);
|
|
|
|
$this->db->insert_id = 42;
|
|
|
|
$slot = new AvailabilitySlot(5, '2026-04-01 09:00:00', '2026-04-01 10:00:00', 30, 8);
|
|
$result = $this->repo->insert($slot);
|
|
|
|
self::assertSame(42, $result);
|
|
}
|
|
|
|
public function testCreateWeeklySeriesInsertsWeeklyAndSharesGroup(): void
|
|
{
|
|
Functions\when('current_time')->justReturn('2026-04-07 12:00:00');
|
|
|
|
$captured = [];
|
|
$ids = [10, 11, 12];
|
|
|
|
$this->db->shouldReceive('insert')
|
|
->times(3)
|
|
->andReturnUsing(function (string $table, array $data) use (&$captured, &$ids): void {
|
|
$captured[] = $data['start_dt'];
|
|
$this->db->insert_id = array_shift($ids);
|
|
});
|
|
|
|
// The first row is back-filled with its own id as the recurrence group.
|
|
$this->db->shouldReceive('update')
|
|
->once()
|
|
->with('wp_us_availability', ['recurrence_group' => 10], ['id' => 10], ['%d'], ['%d']);
|
|
|
|
$first = new AvailabilitySlot(5, '2026-04-07 09:00:00', '2026-04-07 10:00:00', 60);
|
|
$result = $this->repo->createWeeklySeries($first, 3);
|
|
|
|
self::assertSame([10, 11, 12], $result);
|
|
self::assertSame(
|
|
['2026-04-07 09:00:00', '2026-04-14 09:00:00', '2026-04-21 09:00:00'],
|
|
$captured
|
|
);
|
|
}
|
|
|
|
public function testFindByIdReturnsNullWhenNotFound(): void
|
|
{
|
|
$this->db->shouldReceive('prepare')
|
|
->once()
|
|
->andReturn('SELECT * FROM wp_us_availability WHERE id = 99');
|
|
|
|
$this->db->shouldReceive('get_row')
|
|
->once()
|
|
->andReturn(null);
|
|
|
|
$result = $this->repo->findById(99);
|
|
|
|
self::assertNull($result);
|
|
}
|
|
|
|
public function testFindByIdReturnsSlotWhenFound(): void
|
|
{
|
|
$row = (object) [
|
|
'id' => '10',
|
|
'instructor_id' => '5',
|
|
'offering_id' => null,
|
|
'start_dt' => '2026-04-01 09:00:00',
|
|
'end_dt' => '2026-04-01 10:00:00',
|
|
'duration_minutes' => '60',
|
|
'is_booked' => '0',
|
|
'recurrence_group' => null,
|
|
];
|
|
|
|
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
|
|
$this->db->shouldReceive('get_row')->andReturn($row);
|
|
|
|
$slot = $this->repo->findById(10);
|
|
|
|
self::assertInstanceOf(AvailabilitySlot::class, $slot);
|
|
self::assertSame(10, $slot->id);
|
|
self::assertSame(5, $slot->instructorId);
|
|
}
|
|
|
|
public function testClaimReturnsTrueWhenSlotWasUnbooked(): void
|
|
{
|
|
$this->db->shouldReceive('update')
|
|
->once()
|
|
->with('wp_us_availability', ['is_booked' => 1], ['id' => 7, 'is_booked' => 0], ['%d'], ['%d', '%d'])
|
|
->andReturn(1);
|
|
|
|
self::assertTrue($this->repo->claim(7));
|
|
}
|
|
|
|
public function testClaimReturnsFalseWhenSlotAlreadyBooked(): void
|
|
{
|
|
// The is_booked = 0 guard matches no row once the slot is taken.
|
|
$this->db->shouldReceive('update')
|
|
->once()
|
|
->with('wp_us_availability', ['is_booked' => 1], ['id' => 7, 'is_booked' => 0], ['%d'], ['%d', '%d'])
|
|
->andReturn(0);
|
|
|
|
self::assertFalse($this->repo->claim(7));
|
|
}
|
|
|
|
public function testDeleteReturnsFalseWhenRowNotDeleted(): void
|
|
{
|
|
$this->db->shouldReceive('delete')
|
|
->once()
|
|
->with('wp_us_availability', ['id' => 1, 'is_booked' => 0], ['%d', '%d'])
|
|
->andReturn(0);
|
|
|
|
self::assertFalse($this->repo->delete(1));
|
|
}
|
|
|
|
public function testFindAvailableWithNoFiltersPreparesTableOnly(): void
|
|
{
|
|
$this->db->shouldReceive('prepare')
|
|
->once()
|
|
->with(Mockery::pattern('/WHERE is_booked = 0/'), ['wp_us_availability'])
|
|
->andReturn('SELECT ...');
|
|
|
|
$this->db->shouldReceive('get_results')
|
|
->once()
|
|
->with('SELECT ...')
|
|
->andReturn([]);
|
|
|
|
$result = $this->repo->findAvailable();
|
|
|
|
self::assertSame([], $result);
|
|
}
|
|
|
|
public function testFindAvailableWithInstructorFilterPreparesQuery(): void
|
|
{
|
|
$this->db->shouldReceive('prepare')
|
|
->once()
|
|
->with(Mockery::pattern('/instructor_id = %d/'), Mockery::any())
|
|
->andReturn('SELECT ...');
|
|
|
|
$this->db->shouldReceive('get_results')->andReturn([]);
|
|
|
|
$this->repo->findAvailable(instructorId: 3);
|
|
}
|
|
|
|
public function testFindAvailableWithOfferingAndDurationFilters(): void
|
|
{
|
|
$this->db->shouldReceive('prepare')
|
|
->once()
|
|
->with(
|
|
Mockery::pattern('/offering_id = %d AND duration_minutes = %d/'),
|
|
Mockery::on(static fn (array $p): bool => $p === ['wp_us_availability', 8, 30])
|
|
)
|
|
->andReturn('SELECT ...');
|
|
|
|
$this->db->shouldReceive('get_results')->andReturn([]);
|
|
|
|
$this->repo->findAvailable(offeringId: 8, durationMinutes: 30);
|
|
}
|
|
|
|
public function testFindByInstructorReturnsSlots(): void
|
|
{
|
|
$row = (object) [
|
|
'id' => '5',
|
|
'instructor_id' => '3',
|
|
'offering_id' => null,
|
|
'start_dt' => '2026-04-01 09:00:00',
|
|
'end_dt' => '2026-04-01 10:00:00',
|
|
'duration_minutes' => '60',
|
|
'is_booked' => '0',
|
|
'recurrence_group' => null,
|
|
];
|
|
|
|
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
|
|
$this->db->shouldReceive('get_results')->andReturn([$row]);
|
|
|
|
$slots = $this->repo->findByInstructor(3);
|
|
|
|
self::assertCount(1, $slots);
|
|
self::assertInstanceOf(AvailabilitySlot::class, $slots[0]);
|
|
}
|
|
}
|