diff options
Diffstat (limited to 'tests_integration/DefineHandlerTest.php')
| -rw-r--r-- | tests_integration/DefineHandlerTest.php | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/tests_integration/DefineHandlerTest.php b/tests_integration/DefineHandlerTest.php new file mode 100644 index 0000000..6dcce49 --- /dev/null +++ b/tests_integration/DefineHandlerTest.php @@ -0,0 +1,162 @@ +<?php +use PHPUnit\Framework\TestCase; + +final class DefineHandlerTest extends ApiTestCase +{ + // ── Authentication ─────────────────────────────────────────────────────── + + public function test_define_requires_authentication(): void + { + [$status, $body] = $this->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], '<strong>'); + $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 <strong> tags + $this->assertEquals('<strong>cat</strong>', $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('<strong>cats</strong>', $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: <b>xyznonexistent</b>" + $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' => '<script>cat</script>', + ]); + + $this->assertEquals(200, $status); + $this->assertArrayHasKey('result', $body); + $this->assertNotEmpty($body['result']); + // Word should be stripped to just "cat" + $this->assertStringNotContainsString('<script>', $body['result'][0]); + } + + public function test_define_handles_soft_hyphens(): void + { + // Login + [$status, $body] = $this->post($this->apiUrl, [ + 'op' => 'perform-login', + 'user' => 'test', + 'password' => 'test', + ]); + $this->assertEquals(200, $status); + + // "helloworld" contains a soft hyphen () which should be stripped + [$status, $body] = $this->post($this->apiUrl, [ + 'op' => 'define', + 'word' => "hello\u{00AD}world", + ]); + + $this->assertEquals(200, $status); + $this->assertArrayHasKey('result', $body); + $this->assertNotEmpty($body['result']); + } +} |
