diff --git a/src/Mcp/Tools/ActiveThemeInfo.php b/src/Mcp/Tools/ActiveThemeInfo.php index c28b13f..86b5c9a 100644 --- a/src/Mcp/Tools/ActiveThemeInfo.php +++ b/src/Mcp/Tools/ActiveThemeInfo.php @@ -138,6 +138,9 @@ private function listBladeTemplates(string $viewsPath, string $prefix = ''): arr */ private function directories(string $path): array { - return array_values(array_filter(File::directories($path), is_string(...))); + /** @var list $directories */ + $directories = File::directories($path); + + return $directories; } } diff --git a/src/Mcp/Tools/DiscoveredComponents.php b/src/Mcp/Tools/DiscoveredComponents.php index 4bc407f..cf894db 100644 --- a/src/Mcp/Tools/DiscoveredComponents.php +++ b/src/Mcp/Tools/DiscoveredComponents.php @@ -56,17 +56,19 @@ public function handle(Request $request): Response $type = $request->get('type'); - if ($type && (! is_string($type) || ! isset(self::TYPE_MAP[$type]))) { - return Response::text('Invalid type. Valid types: '.implode(', ', array_keys(self::TYPE_MAP))); + $filteredMap = self::TYPE_MAP; + + if ($type) { + if (! is_string($type) || ! isset(self::TYPE_MAP[$type])) { + return Response::text('Invalid type. Valid types: '.implode(', ', array_keys(self::TYPE_MAP))); + } + + $filteredMap = [$type => self::TYPE_MAP[$type]]; } /** @var DiscoveryManager $discoveryManager */ $discoveryManager = app(DiscoveryManager::class); - $filteredMap = is_string($type) && $type !== '' && isset(self::TYPE_MAP[$type]) - ? [$type => self::TYPE_MAP[$type]] - : self::TYPE_MAP; - $components = []; foreach ($filteredMap as $typeName => $discoveryId) { @@ -168,8 +170,10 @@ private function formatRecurrence(mixed $recurrence): string return $this->formatRecurrence($recurrence->toScheduleArray()); } - return $recurrence === null || is_scalar($recurrence) || $recurrence instanceof Stringable - ? (string) $recurrence - : throw new UnexpectedValueException('The recurrence of the schedule has no text.'); + if ($recurrence !== null && ! is_scalar($recurrence) && ! $recurrence instanceof Stringable) { + throw new UnexpectedValueException('The recurrence of the schedule has no text.'); + } + + return Value::text($recurrence); } } diff --git a/tests/Feature/Support/ValueTest.php b/tests/Feature/Support/ValueTest.php new file mode 100644 index 0000000..b404290 --- /dev/null +++ b/tests/Feature/Support/ValueTest.php @@ -0,0 +1,52 @@ +toBe($expected); +})->with([ + 'string' => ['daily', 'daily'], + 'integer' => [42, '42'], + 'stringable' => [new class implements Stringable + { + public function __toString(): string + { + return 'as text'; + } + }, 'as text'], + 'null' => [null, ''], + 'array' => [['a'], ''], + 'plain object' => [new stdClass, ''], +]); + +it('reads the integer of a scalar, and zero from anything else', function (mixed $value, int $expected): void { + expect(Value::integer($value))->toBe($expected); +})->with([ + 'numeric string' => ['25', 25], + 'float' => [2.9, 2], + 'null' => [null, 0], + 'array' => [['25'], 0], + 'plain object' => [new stdClass, 0], +]); + +it('asks an object only for a method it has', function (): void { + expect(Value::ask(new NectarValueTarget, 'answer'))->toBe('answered') + ->and(Value::ask(new NectarValueTarget, 'missing'))->toBeNull(); +}); + +it('asks nothing of a class name, even one with a callable static method', function (): void { + expect(Value::ask(NectarValueTarget::class, 'answer'))->toBeNull(); +}); diff --git a/tests/Feature/Tools/UntypedDataTest.php b/tests/Feature/Tools/UntypedDataTest.php new file mode 100644 index 0000000..8fc6490 --- /dev/null +++ b/tests/Feature/Tools/UntypedDataTest.php @@ -0,0 +1,154 @@ + $arguments + */ +function untypedDataPayload(object $tool, array $arguments = []): mixed +{ + $content = (string) $tool->handle(new Request($arguments))->content(); + + return json_decode($content, true) ?? $content; +} + +/** + * The details discovered_components gives a schedule declared with this recurrence. + * + * @return array + */ +function untypedScheduleDetails(mixed $recurrence): array +{ + $attribute = Mockery::mock(); + $attribute->shouldReceive('newInstance')->andReturn((object) ['hook' => 'job', 'recurrence' => $recurrence]); + + app()->instance(DiscoveryManager::class, FakeDiscovery::manager(['schedules' => [['attribute' => $attribute]]])); + + return untypedDataPayload(new DiscoveredComponents, ['type' => 'schedule'])['components']['schedule'][0]['details']; +} + +afterEach(function (): void { + app()->forgetInstance(DiscoveryManager::class); + @unlink(base_path('composer.lock')); +}); + +it('shows a recurrence declared as an enum or a stringable', function (mixed $recurrence, string $expected): void { + expect(untypedScheduleDetails($recurrence))->toBe(['hook' => 'job', 'recurrence' => $expected]); +})->with([ + 'integer-backed enum' => [NectarUntypedSeconds::Hour, '3600'], + 'pure enum' => [NectarUntypedFrequency::Weekly, 'Weekly'], + 'stringable' => [new class implements Stringable + { + public function __toString(): string + { + return 'fortnightly'; + } + }, 'fortnightly'], +]); + +it('gives no details for a recurrence that has no text', function (): void { + expect(untypedScheduleDetails(new stdClass))->toBe([]); +}); + +it('lists every component type when the type filter is empty', function (): void { + app()->instance(DiscoveryManager::class, FakeDiscovery::manager()); + + expect(array_keys(untypedDataPayload(new DiscoveredComponents, ['type' => ''])['components'])) + ->toBe(['post_type', 'taxonomy', 'hook', 'schedule', 'rest_route']); +}); + +it('refuses a type filter that is not text', function (): void { + app()->instance(DiscoveryManager::class, FakeDiscovery::manager()); + + expect(untypedDataPayload(new DiscoveredComponents, ['type' => ['hook']]))->toStartWith('Invalid type.'); +}); + +it('reports no Pollora package from a composer.lock without a package list', function (string $lock): void { + file_put_contents(base_path('composer.lock'), $lock); + + $payload = untypedDataPayload(new PolloraStatus); + + expect($payload['pollora_version'])->toBe('unknown') + ->and($payload['pollora_packages'])->toBe([]); +})->with([ + 'no packages key' => ['{}'], + 'packages is not a list' => ['{"packages": "none"}'], + 'not an object' => ['"lock"'], + 'not JSON' => ['<<<<<<< HEAD'], +]); + +it('skips the composer.lock entries without a text name and version, and reads the others', function (): void { + file_put_contents(base_path('composer.lock'), json_encode(['packages' => [ + 'garbage', + ['version' => 'v1.0.0'], + ['name' => 'pollora/unversioned'], + ['name' => 'pollora/numbered', 'version' => 2], + ['name' => 'pollora/framework', 'version' => 'v13.4.0'], + ]])); + + $payload = untypedDataPayload(new PolloraStatus); + + expect($payload['pollora_version'])->toBe('v13.4.0') + ->and($payload['pollora_packages'])->toBe([['name' => 'pollora/framework', 'version' => 'v13.4.0']]); +}); + +it('reads a module without the expected methods as empty and unknown', function (): void { + app()->instance('modules', new class + { + /** + * @return list + */ + public function all(): array + { + return [new stdClass]; + } + }); + + expect(untypedDataPayload(new ModulesInfo)['modules'])->toBe([ + '' => ['name' => '', 'path' => '', 'enabled' => null], + ]); +}); + +it('lists no module when the module manager cannot list them', function (): void { + app()->instance('modules', new stdClass); + + expect(untypedDataPayload(new ModulesInfo))->toBe(['available' => true, 'count' => 0, 'modules' => []]); +}); + +it('refuses a WordPress security key even with spaces around its name', function (): void { + expect(untypedDataPayload(new WpOption, ['key' => ' nonce_key ']))->toBeString()->toContain('looks like a secret'); +}); + +it('reads a secret-looking option allowed by a single name, spaces and case aside', function (): void { + config()->set('nectar.wp_option.allow', ' My_Api_Key '); + + expect(untypedDataPayload(new WpOption, ['key' => ' my_api_key '])['key'])->toBe(' my_api_key '); +}); + +it('ignores the allow-list entries that are not an option name', function (): void { + config()->set('nectar.wp_option.allow', [null, 12, ' ', 'my_api_key']); + + expect(untypedDataPayload(new WpOption, ['key' => 'my_api_key'])['key'])->toBe('my_api_key'); +});