about summary refs log tree commit diff stats
path: root/tests_integration/DefineHandlerTest.php
blob: c695f6bde997e313dd98bdd478acb1d59fd4a08f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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], '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: <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);

		// "hello­world" 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']);
	}
}