post($this->apiUrl, [
'op' => 'define',
'word' => 'cat',
]);
$this->assertEquals(401, $status);
$this->assertArrayHasKey('error', $body);
}
// ── Disabled dict server ─────────────────────────────────────────────────
public function test_define_with_disabled_dict_server(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
// When DICT_SERVER is empty, handler returns disabled message
// (This test verifies the early-return path)
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'define',
'word' => 'cat',
]);
// If dict server is not configured, returns disabled message
// If it is configured (mock), returns definitions
$this->assertEquals(200, $status);
$this->assertArrayHasKey('result', $body);
// Either disabled message or definitions
$isDisabled = isset($body['result'][0]) && $body['result'][0] === 'Dictionary lookups are disabled.';
$isDefined = isset($body['result'][0]) && str_contains($body['result'][0], 'cat:');
$this->assertTrue($isDisabled || $isDefined, 'Expected either disabled message or definitions');
}
// ── With mock dict server ────────────────────────────────────────────────
public function test_define_returns_definitions_for_known_word(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'define',
'word' => 'cat',
]);
$this->assertEquals(200, $status);
$this->assertArrayHasKey('result', $body);
$this->assertNotEmpty($body['result']);
// First result should be the word with :
$this->assertEquals('cat:', $body['result'][0]);
// Followed by definition(s)
$this->assertGreaterThan(1, count($body['result']));
}
public function test_define_truncates_word_until_match(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
// "cats" → handler tries "cats" → not found → tries "cat" → found
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'define',
'word' => 'cats',
]);
$this->assertEquals(200, $status);
$this->assertArrayHasKey('result', $body);
$this->assertNotEmpty($body['result']);
// Should match "cat" definition
$this->assertEquals('cats:', $body['result'][0]);
}
public function test_define_returns_no_results_for_unknown_word(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'define',
'word' => 'xyznonexistent',
]);
$this->assertEquals(200, $status);
$this->assertArrayHasKey('result', $body);
$this->assertNotEmpty($body['result']);
// Should return "No results for: xyznonexistent"
$this->assertStringContainsString('No results for:', $body['result'][0]);
$this->assertStringContainsString('xyznonexistent', $body['result'][0]);
}
public function test_define_strips_html_tags_from_word(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'define',
'word' => '',
]);
$this->assertEquals(200, $status);
$this->assertArrayHasKey('result', $body);
$this->assertNotEmpty($body['result']);
// Word should be stripped to just "cat"
$this->assertStringNotContainsString('