*/ private function samplePlan(): array { return SamplePlan::for(new Store(['name' => 'Sambal Nenek'])); } /** * @param array $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'] = ''; $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']]]])); } }