PluginProbe ʕ •ᴥ•ʔ
Reviews Feed – Add Testimonials and Customer Reviews From Google Reviews, Yelp, TripAdvisor, and More / 2.11.0
Reviews Feed – Add Testimonials and Customer Reviews From Google Reviews, Yelp, TripAdvisor, and More v2.11.0
2.11.0 2.10.0 2.9.0 2.8.0 2.7.0 2.6.7 2.6.8 2.6.5 2.6.4 2.6.3 2.6.2 2.6.0 2.5.5 2.5.4 2.5.3 2.5.2 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1 1.1.1 1.1.2 1.2.0 2.0 2.1.0 2.1.1 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1
reviews-feed / tests / Unit / SourceIdLookupTest.php
reviews-feed / tests / Unit Last commit date
Doubles 6 days ago Providers 6 days ago BulkRearmOnGrowthTest.php 6 days ago BulkReviewsUpdateStuckStateTest.php 6 days ago ClearCacheRelayResetTest.php 6 days ago DeleteSourceRelayFailureTest.php 6 days ago ErrorHandlerFalsyOptionTest.php 6 days ago FeedCacheUpdateServiceTest.php 6 days ago FeedMalformedPayloadTest.php 6 days ago ForceKeylessRefetchTest.php 6 days ago LicenseDeactivateStaleStateTest.php 6 days ago MediaFinderMemoTest.php 6 days ago MultiSourceAggregationTest.php 6 days ago ReconcileMigratedLicenseRoutineTest.php 6 days ago ReconcileRemovalTest.php 6 days ago RegisterWebsiteRoutineTest.php 6 days ago RelaySlowEndpointsTest.php 6 days ago RemoteRequestMemoTest.php 6 days ago ReviewAlertHeaderTotalsTest.php 6 days ago ReviewAlertPageTargetingTest.php 6 days ago ReviewAlertStarFillTest.php 6 days ago ShortcodeNeutralizationTest.php 6 days ago SiteMigrationRecoveryTest.php 6 days ago Smash1130UsageTrackingHardeningTest.php 6 days ago Smash1130UsageTrackingHooksTest.php 6 days ago Smash1583HeaderParityTest.php 6 days ago Smash1631MultiLanguageBulkTest.php 6 days ago Smash1631UpdateSingleLangScopeTest.php 6 days ago Smash1706TripAdvisorPlaceIdTest.php 6 days ago Smash1756SchemaServiceTest.php 6 days ago Smash1785AvatarLocalUrlGuardTest.php 6 days ago Smash1785AvatarReHealTest.php 6 days ago Smash1795ReviewTextXssTest.php 6 days ago Smash1835TripAdvisorKeyShapeTest.php 6 days ago Smash1973WordpressOrgPlaceIdNullTest.php 6 days ago Smash1987NestedSourceErrorShapeTest.php 6 days ago Smash782BookingHeaderRatingTest.php 6 days ago Smash782CountryFlagEmojiTest.php 6 days ago Smash782ExternalRefreshCronTest.php 6 days ago Smash782ExtrasTemplateTest.php 6 days ago Smash782ReviewAlertProviderDataTest.php 6 days ago SourceIdLookupTest.php 6 days ago WpmlGetCurrentLanguageTest.php 6 days ago WpmlLanguageMappingTest.php 6 days ago
SourceIdLookupTest.php
443 lines
1 <?php
2
3 namespace SmashBalloon\Reviews\Tests\Unit;
4
5 use PHPUnit\Framework\TestCase;
6
7 /**
8 * Unit tests for source_id lookup functionality in sb-reviews plugin.
9 *
10 * These tests verify:
11 * 1. relay_source_id is captured from API responses
12 * 2. source_id is used when available in API calls
13 * 3. Falls back to place_id for backward compatibility
14 * 4. Danish character handling works correctly
15 */
16 class SourceIdLookupTest extends TestCase
17 {
18 /*
19 |--------------------------------------------------------------------------
20 | RemoteRequest::fetch() - source_id Preference Tests
21 |--------------------------------------------------------------------------
22 */
23
24 /**
25 * Test that source_id is preferred when relay_source_id is available.
26 *
27 * This simulates the behavior in RemoteRequest::fetch()
28 */
29 public function test_source_id_is_used_when_relay_source_id_available(): void
30 {
31 $args = [
32 'business' => 'test_place_123',
33 'info' => [
34 'relay_source_id' => 456,
35 'id' => 'test_place_123',
36 ],
37 ];
38
39 $result = $this->buildApiArgs($args);
40
41 $this->assertArrayHasKey('source_id', $result);
42 $this->assertEquals(456, $result['source_id']);
43 $this->assertArrayNotHasKey('place_id', $result);
44 }
45
46 /**
47 * Test that place_id is used as fallback when no relay_source_id.
48 */
49 public function test_place_id_used_as_fallback_when_no_relay_source_id(): void
50 {
51 $args = [
52 'business' => 'test_place_123',
53 'info' => [
54 'id' => 'test_place_123',
55 // No relay_source_id
56 ],
57 ];
58
59 $result = $this->buildApiArgs($args);
60
61 $this->assertArrayHasKey('place_id', $result);
62 $this->assertEquals('test_place_123', $result['place_id']);
63 $this->assertArrayNotHasKey('source_id', $result);
64 }
65
66 /**
67 * Test that empty relay_source_id falls back to place_id.
68 */
69 public function test_empty_relay_source_id_falls_back_to_place_id(): void
70 {
71 $args = [
72 'business' => 'test_place_123',
73 'info' => [
74 'relay_source_id' => '', // Empty
75 'id' => 'test_place_123',
76 ],
77 ];
78
79 $result = $this->buildApiArgs($args);
80
81 $this->assertArrayHasKey('place_id', $result);
82 $this->assertArrayNotHasKey('source_id', $result);
83 }
84
85 /**
86 * Test that zero relay_source_id falls back to place_id.
87 */
88 public function test_zero_relay_source_id_falls_back_to_place_id(): void
89 {
90 $args = [
91 'business' => 'test_place_123',
92 'info' => [
93 'relay_source_id' => 0,
94 'id' => 'test_place_123',
95 ],
96 ];
97
98 $result = $this->buildApiArgs($args);
99
100 $this->assertArrayHasKey('place_id', $result);
101 $this->assertArrayNotHasKey('source_id', $result);
102 }
103
104 /**
105 * Test that source_id is cast to integer.
106 */
107 public function test_source_id_is_cast_to_integer(): void
108 {
109 $args = [
110 'business' => 'test_place_123',
111 'info' => [
112 'relay_source_id' => '789', // String
113 ],
114 ];
115
116 $result = $this->buildApiArgs($args);
117
118 $this->assertArrayHasKey('source_id', $result);
119 $this->assertIsInt($result['source_id']);
120 $this->assertEquals(789, $result['source_id']);
121 }
122
123 /*
124 |--------------------------------------------------------------------------
125 | relay_source_id Capture Tests
126 |--------------------------------------------------------------------------
127 */
128
129 /**
130 * Test that relay_source_id is captured from API response.
131 */
132 public function test_relay_source_id_captured_from_response(): void
133 {
134 $apiResponse = [
135 'info' => [
136 'id' => 'Løgstør-test',
137 'name' => 'Test Business',
138 'rating' => 4.5,
139 ],
140 'source_id' => 12345,
141 ];
142
143 $processedInfo = $this->processApiResponse($apiResponse);
144
145 $this->assertArrayHasKey('relay_source_id', $processedInfo);
146 $this->assertEquals(12345, $processedInfo['relay_source_id']);
147 }
148
149 /**
150 * Test that original info is preserved when capturing relay_source_id.
151 */
152 public function test_original_info_preserved_with_relay_source_id(): void
153 {
154 $apiResponse = [
155 'info' => [
156 'id' => 'test_place',
157 'name' => 'Original Name',
158 'rating' => 4.5,
159 'url' => 'https://example.com',
160 ],
161 'source_id' => 999,
162 ];
163
164 $processedInfo = $this->processApiResponse($apiResponse);
165
166 $this->assertEquals('test_place', $processedInfo['id']);
167 $this->assertEquals('Original Name', $processedInfo['name']);
168 $this->assertEquals(4.5, $processedInfo['rating']);
169 $this->assertEquals('https://example.com', $processedInfo['url']);
170 $this->assertEquals(999, $processedInfo['relay_source_id']);
171 }
172
173 /**
174 * Test handling when source_id is missing from response (backward compat).
175 */
176 public function test_handles_missing_source_id_in_response(): void
177 {
178 $apiResponse = [
179 'info' => [
180 'id' => 'test_place',
181 'name' => 'Test Name',
182 ],
183 // No source_id - old API response format
184 ];
185
186 $processedInfo = $this->processApiResponse($apiResponse);
187
188 $this->assertArrayNotHasKey('relay_source_id', $processedInfo);
189 $this->assertEquals('test_place', $processedInfo['id']);
190 }
191
192 /*
193 |--------------------------------------------------------------------------
194 | Danish Character Encoding Tests
195 |--------------------------------------------------------------------------
196 */
197
198 /**
199 * Test that Danish characters in place_id don't affect source_id lookup.
200 */
201 public function test_danish_place_id_with_source_id_lookup(): void
202 {
203 $args = [
204 'business' => 'Løgstør-æøå-café',
205 'info' => [
206 'relay_source_id' => 123,
207 'id' => 'Løgstør-æøå-café',
208 ],
209 ];
210
211 $result = $this->buildApiArgs($args);
212
213 // Should use source_id, not the problematic place_id
214 $this->assertArrayHasKey('source_id', $result);
215 $this->assertEquals(123, $result['source_id']);
216 $this->assertArrayNotHasKey('place_id', $result);
217 }
218
219 /**
220 * Test that URL-encoded place_id is used correctly when no source_id.
221 */
222 public function test_danish_place_id_fallback_encoding(): void
223 {
224 $danishPlaceId = 'Løgstør-æøå';
225
226 $args = [
227 'business' => $danishPlaceId,
228 'info' => [
229 'id' => $danishPlaceId,
230 // No relay_source_id - must use place_id
231 ],
232 ];
233
234 $result = $this->buildApiArgs($args);
235
236 $this->assertArrayHasKey('place_id', $result);
237 $this->assertEquals($danishPlaceId, $result['place_id']);
238 }
239
240 /**
241 * Test various Danish character combinations.
242 *
243 * @dataProvider danishCharactersProvider
244 */
245 public function test_various_danish_characters(string $placeId): void
246 {
247 $args = [
248 'business' => $placeId,
249 'info' => [
250 'relay_source_id' => 100,
251 'id' => $placeId,
252 ],
253 ];
254
255 $result = $this->buildApiArgs($args);
256
257 // With source_id, the problematic place_id should not be in the request
258 $this->assertArrayHasKey('source_id', $result);
259 $this->assertArrayNotHasKey('place_id', $result);
260 }
261
262 public static function danishCharactersProvider(): array
263 {
264 return [
265 'lowercase æ' => ['test-æ-place'],
266 'lowercase ø' => ['test-ø-place'],
267 'lowercase å' => ['test-å-place'],
268 'uppercase Æ' => ['test-Æ-place'],
269 'uppercase Ø' => ['test-Ø-place'],
270 'uppercase �
271 ' => ['test-�
272 -place'],
273 'all lowercase' => ['Løgstør-æøå'],
274 'mixed case' => ['�
275 LBORG-Næstved'],
276 'with spaces' => ['Rødovre Station'],
277 'with numbers' => ['�
278 lborg123'],
279 ];
280 }
281
282 /*
283 |--------------------------------------------------------------------------
284 | Edge Cases
285 |--------------------------------------------------------------------------
286 */
287
288 /**
289 * Test handling of null info array.
290 */
291 public function test_handles_null_info(): void
292 {
293 $args = [
294 'business' => 'test_place',
295 'info' => null,
296 ];
297
298 $result = $this->buildApiArgs($args);
299
300 $this->assertArrayHasKey('place_id', $result);
301 $this->assertArrayNotHasKey('source_id', $result);
302 }
303
304 /**
305 * Test handling of missing info key.
306 */
307 public function test_handles_missing_info_key(): void
308 {
309 $args = [
310 'business' => 'test_place',
311 ];
312
313 $result = $this->buildApiArgs($args);
314
315 $this->assertArrayHasKey('place_id', $result);
316 $this->assertArrayNotHasKey('source_id', $result);
317 }
318
319 /**
320 * Test that negative source_id is handled (shouldn't happen but be defensive).
321 */
322 public function test_handles_negative_source_id(): void
323 {
324 $args = [
325 'business' => 'test_place',
326 'info' => [
327 'relay_source_id' => -1, // Invalid
328 ],
329 ];
330
331 $result = $this->buildApiArgs($args);
332
333 // Should still use source_id (the API will reject invalid IDs)
334 $this->assertArrayHasKey('source_id', $result);
335 }
336
337 /**
338 * Test handling of very large source_id.
339 */
340 public function test_handles_large_source_id(): void
341 {
342 $largeId = PHP_INT_MAX;
343
344 $args = [
345 'business' => 'test_place',
346 'info' => [
347 'relay_source_id' => $largeId,
348 ],
349 ];
350
351 $result = $this->buildApiArgs($args);
352
353 $this->assertArrayHasKey('source_id', $result);
354 $this->assertEquals($largeId, $result['source_id']);
355 }
356
357 /*
358 |--------------------------------------------------------------------------
359 | JSON Serialization Tests (for storage in info field)
360 |--------------------------------------------------------------------------
361 */
362
363 /**
364 * Test that relay_source_id survives JSON encode/decode cycle.
365 */
366 public function test_relay_source_id_survives_json_cycle(): void
367 {
368 $originalInfo = [
369 'id' => 'Løgstør-test',
370 'name' => 'Test æøå',
371 'relay_source_id' => 12345,
372 ];
373
374 $encoded = json_encode($originalInfo);
375 $decoded = json_decode($encoded, true);
376
377 $this->assertEquals(12345, $decoded['relay_source_id']);
378 $this->assertEquals('Løgstør-test', $decoded['id']);
379 }
380
381 /**
382 * Test that special characters in info survive storage.
383 */
384 public function test_danish_characters_survive_json_cycle(): void
385 {
386 $originalInfo = [
387 'id' => 'Løgstør-æøå-ÆØ�
388 ',
389 'name' => 'Café Rødovre �
390 lborg',
391 'relay_source_id' => 999,
392 ];
393
394 $encoded = json_encode($originalInfo, JSON_UNESCAPED_UNICODE);
395 $decoded = json_decode($encoded, true);
396
397 $this->assertEquals('Løgstør-æøå-ÆØ�
398 ', $decoded['id']);
399 $this->assertEquals('Café Rødovre �
400 lborg', $decoded['name']);
401 }
402
403 /*
404 |--------------------------------------------------------------------------
405 | Helper Methods (Simulating RemoteRequest and Feed Saver logic)
406 |--------------------------------------------------------------------------
407 */
408
409 /**
410 * Simulates the arg building logic from RemoteRequest::fetch()
411 */
412 private function buildApiArgs(array $requestArgs): array
413 {
414 $business = $requestArgs['business'] ?? '';
415
416 // This is the logic from RemoteRequest::fetch()
417 if (!empty($requestArgs['info']['relay_source_id'])) {
418 return [
419 'source_id' => (int) $requestArgs['info']['relay_source_id'],
420 ];
421 }
422
423 return [
424 'place_id' => $business,
425 ];
426 }
427
428 /**
429 * Simulates the relay_source_id capture from SBR_Feed_Saver_Manager
430 */
431 private function processApiResponse(array $response): array
432 {
433 $info = $response['info'] ?? [];
434
435 // This is the logic from SBR_Feed_Saver_Manager::process_source_apikey()
436 if (isset($response['source_id'])) {
437 $info['relay_source_id'] = $response['source_id'];
438 }
439
440 return $info;
441 }
442 }
443