| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
-
- namespace Tests\Feature\Stores;
-
- use App\Models\Store;
- use App\StorePlan\PlanSchema;
- use App\StorePlan\PlanValidator;
- use App\StorePlan\SamplePlan;
- use Illuminate\Support\Arr;
- use Illuminate\Validation\ValidationException;
- use Tests\TestCase;
-
- class PlanValidatorTest extends TestCase
- {
- /**
- * @return array<string, mixed>
- */
- private function samplePlan(): array
- {
- return SamplePlan::for(new Store(['name' => 'Sambal Nenek']));
- }
-
- /**
- * @param array<string, mixed> $plan
- */
- private function assertInvalid(array $plan, string $errorKey): void
- {
- try {
- (new PlanValidator)->validate($plan);
- } catch (ValidationException $e) {
- $this->assertArrayHasKey($errorKey, $e->errors());
-
- return;
- }
-
- $this->fail("Expected the plan to be rejected for [$errorKey].");
- }
-
- public function test_the_sample_plan_is_valid_and_uses_every_section_type()
- {
- $plan = (new PlanValidator)->validate($this->samplePlan());
-
- $this->assertEqualsCanonicalizing(
- array_keys(PlanSchema::SECTIONS),
- array_column($plan['sections'], 'type'),
- );
- }
-
- public function test_unknown_fields_are_removed()
- {
- $plan = $this->samplePlan();
- $plan['evil'] = 'x';
- $plan['theme']['colors']['extra'] = '#000000';
- $plan['sections'][1]['content']['script'] = '<script>alert(1)</script>';
-
- $clean = (new PlanValidator)->validate($plan);
-
- $this->assertArrayNotHasKey('evil', $clean);
- $this->assertArrayNotHasKey('extra', $clean['theme']['colors']);
- $this->assertArrayNotHasKey('script', $clean['sections'][1]['content']);
- }
-
- public function test_colours_must_be_hex_values()
- {
- $plan = $this->samplePlan();
- $plan['theme']['colors']['primary'] = 'red; background: url(x)';
-
- $this->assertInvalid($plan, 'theme.colors.primary');
- }
-
- public function test_every_theme_colour_is_required()
- {
- $plan = $this->samplePlan();
- unset($plan['theme']['colors']['border']);
-
- $this->assertInvalid($plan, 'theme.colors.border');
- }
-
- public function test_fonts_must_come_from_the_allowed_list()
- {
- $plan = $this->samplePlan();
- $plan['theme']['fonts']['heading'] = 'comic-sans';
-
- $this->assertInvalid($plan, 'theme.fonts.heading');
- }
-
- public function test_unknown_section_types_are_rejected()
- {
- $plan = $this->samplePlan();
- $plan['sections'][0]['type'] = 'popup';
-
- $this->assertInvalid($plan, 'sections.0.type');
- }
-
- public function test_a_variant_must_belong_to_its_section_type()
- {
- $plan = $this->samplePlan();
- $plan['sections'][1]['variant'] = 'cards';
-
- $this->assertInvalid($plan, 'sections.1.variant');
- }
-
- public function test_required_section_content_is_enforced()
- {
- $plan = $this->samplePlan();
- unset($plan['sections'][1]['content']['headline']);
-
- $this->assertInvalid($plan, 'sections.1.content.headline');
- }
-
- public function test_unsafe_links_are_rejected()
- {
- foreach (['javascript:alert(1)', 'http://insecure.example', 'data:text/html,x', '//evil.example'] as $href) {
- $plan = $this->samplePlan();
- $plan['sections'][1]['content']['primaryCta']['href'] = $href;
-
- $this->assertInvalid($plan, 'sections.1.content.primaryCta.href');
- }
- }
-
- public function test_safe_links_are_accepted()
- {
- foreach (['#products', '/products/sambal', 'https://example.com/x', 'mailto:hi@example.com', 'tel:+60123456789'] as $href) {
- $plan = $this->samplePlan();
- $plan['sections'][1]['content']['primaryCta']['href'] = $href;
-
- $this->assertTrue((new PlanValidator)->passes($plan), "Expected [$href] to be accepted.");
- }
- }
-
- public function test_section_ids_must_be_unique()
- {
- $plan = $this->samplePlan();
- $plan['sections'][1]['id'] = $plan['sections'][0]['id'];
-
- $this->assertFalse((new PlanValidator)->passes($plan));
- }
-
- public function test_optional_parts_can_be_left_out()
- {
- $plan = $this->samplePlan();
- Arr::forget($plan, 'sections.0.content.cta');
- Arr::forget($plan, 'sections.1.content.secondaryCta');
- Arr::set($plan, 'sections.1.content.eyebrow', null);
-
- $this->assertTrue((new PlanValidator)->passes($plan));
- }
-
- public function test_a_plan_needs_at_least_one_section()
- {
- $plan = $this->samplePlan();
- $plan['sections'] = [];
-
- $this->assertInvalid($plan, 'sections');
- }
-
- public function test_garbage_input_is_rejected_without_crashing()
- {
- $this->assertFalse((new PlanValidator)->passes([]));
- $this->assertFalse((new PlanValidator)->passes(['version' => 1, 'theme' => 'x', 'sections' => 'y']));
- $this->assertFalse((new PlanValidator)->passes(['version' => 1, 'theme' => [], 'sections' => ['not-an-array', ['type' => ['a']]]]));
- }
- }
|