Files
pricebuddy/tests/Feature/Api/StoreScrapeStrategyValidationTest.php
Jeremy 482fe587ae fix: share one scrape strategy value rule across all three endpoints
A schema_org strategy could be tested but not saved. meta-extraction requires
`value` to be null for schema_org and non-empty for every other type, while the
write endpoints required it unconditionally (create) or whenever a type was
present (update). The extension's Tune tab defaults all three fields to
schema_org and omits `value`, so "Test all" succeeded and "Save to store" 422'd
on the identical strategy. No single payload satisfied both, so any store whose
fields are detected via schema.org could not be saved at all.

Extracts the meta-extraction closure into App\Rules\ScrapeStrategyValue and
points all three requests at it. The rule locates the sibling `type` relative to
the attribute under validation, so it works at any path without a configured
prefix, and is implicit so it still runs when `value` is absent - which is both
how a schema_org strategy is legitimately sent and how a selector with no
expression is caught. Which types need a value stays owned by
ScraperStrategyType::needsValue().

A saved schema_org strategy round-trips as `{"type": "schema_org"}` with no
value key at all - StandardStrategyDto drops null values rather than persisting
them - and that shape is valid input to all three endpoints. Covered by a test
that creates a store, reads it back and posts it to meta-extraction.

Also adds the stores_filter_domain capability to /api/client-config, so clients
stop inferring filter[domain] support from products_filter_url.
2026-08-08 12:29:02 +10:00

235 lines
8.3 KiB
PHP

<?php
namespace Tests\Feature\Api;
use App\Enums\ScraperService;
use App\Models\Store;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use Tests\Traits\ScraperTrait;
/**
* The `value` of a scrape strategy is conditional on its `type`: schema_org reads the
* page's embedded metadata and takes no value, every other type requires one. All three
* endpoints that accept a strategy must agree, otherwise a strategy that meta-extraction
* accepts cannot be saved (or vice versa).
*/
class StoreScrapeStrategyValidationTest extends TestCase
{
use RefreshDatabase;
use ScraperTrait;
private User $user;
protected function setUp(): void
{
parent::setUp();
$this->user = User::factory()->create();
$token = $this->user->createToken('test-token')->plainTextToken;
$this->withHeaders(['Authorization' => 'Bearer '.$token]);
}
public function test_create_accepts_schema_org_strategies_without_a_value(): void
{
$this->postJson('/api/stores', $this->createPayload($this->schemaOrgStrategies()))
->assertCreated();
$store = Store::query()->firstOrFail();
$this->assertSame('schema_org', $store->scrape_strategy->title?->type->value);
$this->assertNull($store->scrape_strategy->title?->value);
}
public function test_create_rejects_a_value_on_a_schema_org_strategy(): void
{
$strategies = $this->schemaOrgStrategies();
$strategies['title']['value'] = 'h1';
$this->postJson('/api/stores', $this->createPayload($strategies))
->assertUnprocessable()
->assertJsonValidationErrors(['scrape_strategy.title.value']);
}
public function test_create_rejects_a_selector_strategy_without_a_value(): void
{
$strategies = $this->schemaOrgStrategies();
$strategies['price'] = ['type' => 'selector'];
$this->postJson('/api/stores', $this->createPayload($strategies))
->assertUnprocessable()
->assertJsonValidationErrors(['scrape_strategy.price.value']);
}
public function test_create_accepts_a_selector_strategy_with_a_value(): void
{
$strategies = $this->schemaOrgStrategies();
$strategies['price'] = ['type' => 'selector', 'value' => '.price'];
$this->postJson('/api/stores', $this->createPayload($strategies))
->assertCreated();
}
public function test_create_rejects_a_blank_value_on_a_selector_strategy(): void
{
$strategies = $this->schemaOrgStrategies();
$strategies['price'] = ['type' => 'selector', 'value' => ' '];
$this->postJson('/api/stores', $this->createPayload($strategies))
->assertUnprocessable()
->assertJsonValidationErrors(['scrape_strategy.price.value']);
}
public function test_update_accepts_schema_org_strategies_without_a_value(): void
{
$store = Store::factory()->create(['user_id' => $this->user->id]);
$this->putJson("/api/stores/{$store->id}", ['scrape_strategy' => $this->schemaOrgStrategies()])
->assertSuccessful();
$store->refresh();
$this->assertSame('schema_org', $store->scrape_strategy->image?->type->value);
$this->assertNull($store->scrape_strategy->image?->value);
}
public function test_update_rejects_a_value_on_a_schema_org_strategy(): void
{
$store = Store::factory()->create(['user_id' => $this->user->id]);
$strategies = $this->schemaOrgStrategies();
$strategies['image']['value'] = 'img';
$this->putJson("/api/stores/{$store->id}", ['scrape_strategy' => $strategies])
->assertUnprocessable()
->assertJsonValidationErrors(['scrape_strategy.image.value']);
}
public function test_update_rejects_a_selector_strategy_without_a_value(): void
{
$store = Store::factory()->create(['user_id' => $this->user->id]);
$strategies = $this->schemaOrgStrategies();
$strategies['image'] = ['type' => 'selector'];
$this->putJson("/api/stores/{$store->id}", ['scrape_strategy' => $strategies])
->assertUnprocessable()
->assertJsonValidationErrors(['scrape_strategy.image.value']);
}
public function test_update_accepts_a_selector_strategy_with_a_value(): void
{
$store = Store::factory()->create(['user_id' => $this->user->id]);
$strategies = $this->schemaOrgStrategies();
$strategies['image'] = ['type' => 'selector', 'value' => 'img.product'];
$this->putJson("/api/stores/{$store->id}", ['scrape_strategy' => $strategies])
->assertSuccessful();
$this->assertSame('img.product', $store->refresh()->scrape_strategy->image?->value);
}
public function test_a_saved_schema_org_strategy_is_stored_and_returned_without_a_value_key(): void
{
$this->postJson('/api/stores', $this->createPayload($this->schemaOrgStrategies()))
->assertCreated();
$store = Store::query()->firstOrFail();
// The DTO drops null values entirely rather than persisting `"value": null`.
$stored = json_decode($store->getRawOriginal('scrape_strategy'), true);
$this->assertSame(['type' => 'schema_org'], $stored['title']);
$this->getJson("/api/stores/{$store->id}")
->assertSuccessful()
->assertJsonPath('data.scrape_strategy.title', ['type' => 'schema_org']);
}
public function test_a_saved_schema_org_store_round_trips_through_meta_extraction(): void
{
$this->postJson('/api/stores', $this->createPayload($this->schemaOrgStrategies()))
->assertCreated();
$store = Store::query()->firstOrFail();
$read = $this->getJson("/api/stores/{$store->id}")->assertSuccessful()->json('data');
$this->mockScrapeSchema('42.50', 'Round trip product', 'https://example.com/rt.jpg');
// Exactly what a client reads back, sent straight into the endpoint that made the
// original strategy testable.
$this->postJson('/api/meta-extraction', [
'url' => 'https://teststore.com/product/1',
'store' => [
'name' => $read['name'],
'domains' => $read['domains'],
'settings' => $read['settings'],
'scrape_strategy' => $read['scrape_strategy'],
],
])
->assertOk()
->assertJsonPath('data.title', 'Round trip product')
->assertJsonPath('data.price', 42.5);
}
public function test_meta_extraction_still_rejects_a_value_on_a_schema_org_strategy(): void
{
$this->postJson('/api/meta-extraction', [
'url' => 'https://teststore.com/product/1',
'store' => [
'scrape_strategy' => [
'title' => ['type' => 'schema_org', 'value' => 'h1'],
],
],
])
->assertUnprocessable()
->assertJsonValidationErrors(['store.scrape_strategy.title.value']);
}
public function test_meta_extraction_still_requires_a_value_on_a_selector_strategy(): void
{
$this->postJson('/api/meta-extraction', [
'url' => 'https://teststore.com/product/1',
'store' => [
'scrape_strategy' => [
'title' => ['type' => 'selector'],
],
],
])
->assertUnprocessable()
->assertJsonValidationErrors(['store.scrape_strategy.title.value']);
}
/**
* @return array<string, array<string, string>>
*/
private function schemaOrgStrategies(): array
{
return [
'title' => ['type' => 'schema_org'],
'price' => ['type' => 'schema_org'],
'image' => ['type' => 'schema_org'],
];
}
/**
* @param array<string, array<string, string>> $strategies
* @return array<string, mixed>
*/
private function createPayload(array $strategies): array
{
return [
'name' => 'Test Store',
'domains' => [
['domain' => 'teststore.com'],
],
'scrape_strategy' => $strategies,
'settings' => [
'scraper_service' => ScraperService::Http->value,
'scraper_service_settings' => '',
],
];
}
}