*/ 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->assertSame(PlanSchema::VERSION, $plan['version']); $this->assertSame('home', $plan['pages'][0]['id']); $this->assertArrayHasKey('logoText', $plan['header']['content']); $this->assertArrayHasKey('text', $plan['footer']['content']); $this->assertEqualsCanonicalizing( array_keys(PlanSchema::pageSections()), array_column($plan['pages'][0]['sections'], 'type'), ); } public function test_unknown_fields_are_removed() { $plan = $this->samplePlan(); $plan['evil'] = 'x'; $plan['theme']['colors']['extra'] = '#000000'; $plan['pages'][0]['sections'][0]['content']['script'] = ''; $clean = (new PlanValidator)->validate($plan); $this->assertArrayNotHasKey('evil', $clean); $this->assertArrayNotHasKey('extra', $clean['theme']['colors']); $this->assertArrayNotHasKey('script', $clean['pages'][0]['sections'][0]['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['pages'][0]['sections'][0]['type'] = 'popup'; $this->assertInvalid($plan, 'pages.0.sections.0.type'); } public function test_a_variant_must_belong_to_its_section_type() { $plan = $this->samplePlan(); $plan['pages'][0]['sections'][0]['variant'] = 'cards'; $this->assertInvalid($plan, 'pages.0.sections.0.variant'); } public function test_required_section_content_is_enforced() { $plan = $this->samplePlan(); unset($plan['pages'][0]['sections'][0]['content']['headline']); $this->assertInvalid($plan, 'pages.0.sections.0.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['pages'][0]['sections'][0]['content']['primaryCta']['href'] = $href; $this->assertInvalid($plan, 'pages.0.sections.0.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['pages'][0]['sections'][0]['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['pages'][0]['sections'][1]['id'] = $plan['pages'][0]['sections'][0]['id']; $this->assertFalse((new PlanValidator)->passes($plan)); } public function test_optional_parts_can_be_left_out() { $plan = $this->samplePlan(); Arr::forget($plan, 'header.content.cta'); Arr::forget($plan, 'pages.0.sections.0.content.secondaryCta'); Arr::set($plan, 'pages.0.sections.0.content.eyebrow', null); $this->assertTrue((new PlanValidator)->passes($plan)); } public function test_a_plan_needs_at_least_one_section() { $plan = $this->samplePlan(); $plan['pages'][0]['sections'] = []; $this->assertInvalid($plan, 'pages.0.sections'); } public function test_garbage_input_is_rejected_without_crashing() { $this->assertFalse((new PlanValidator)->passes([])); $this->assertFalse((new PlanValidator)->passes(['version' => 2, 'theme' => 'x', 'header' => 'h', 'pages' => 'y'])); $this->assertFalse((new PlanValidator)->passes(['version' => 2, 'theme' => [], 'pages' => ['not-an-array', ['id' => 'home', 'sections' => ['x', ['type' => ['a']]]]]])); } public function test_the_header_and_footer_are_required_and_checked() { $plan = $this->samplePlan(); unset($plan['header']); $this->assertInvalid($plan, 'header'); $plan = $this->samplePlan(); $plan['footer']['content']['links'][0]['href'] = 'javascript:alert(1)'; $this->assertInvalid($plan, 'footer.content.links.0.href'); $plan = $this->samplePlan(); $plan['header']['variant'] = 'cards'; $this->assertInvalid($plan, 'header.variant'); } public function test_a_page_cannot_contain_a_header_or_footer() { $plan = $this->samplePlan(); $plan['pages'][0]['sections'][0] = ['id' => 'top', 'type' => 'header', ...$plan['header']]; $this->assertInvalid($plan, 'pages.0.sections.0.type'); } public function test_the_first_page_must_be_home_and_page_ids_cannot_clash_with_built_in_pages() { $plan = $this->samplePlan(); $plan['pages'][0]['id'] = 'landing'; $this->assertInvalid($plan, 'pages.0.id'); $plan = $this->samplePlan(); $plan['pages'][] = ['id' => 'shop', 'title' => 'Shop', 'sections' => [['id' => 'x', ...$this->aboutSection()]]]; $this->assertInvalid($plan, 'pages.1.id'); } public function test_a_plan_can_have_more_pages_but_section_ids_stay_unique_across_them() { $plan = $this->samplePlan(); $plan['pages'][] = ['id' => 'our-story', 'title' => 'Our story', 'sections' => [['id' => 'story-body', ...$this->aboutSection()]]]; $this->assertTrue((new PlanValidator)->passes($plan)); $plan['pages'][1]['sections'][0]['id'] = $plan['pages'][0]['sections'][0]['id']; $this->assertInvalid($plan, 'pages.1.sections.0.id'); } public function test_old_format_plans_are_rejected_until_upgraded() { $old = [ 'version' => 1, 'theme' => $this->samplePlan()['theme'], 'sections' => [ ['id' => 'header', 'type' => 'header', ...$this->samplePlan()['header']], ...$this->samplePlan()['pages'][0]['sections'], ['id' => 'footer', 'type' => 'footer', ...$this->samplePlan()['footer']], ], ]; $this->assertFalse((new PlanValidator)->passes($old)); $this->assertEquals($this->samplePlan(), PlanUpgrader::upgrade($old)); $this->assertSame($this->samplePlan(), PlanUpgrader::upgrade($this->samplePlan())); } /** * @return array */ private function aboutSection(): array { return [ 'type' => 'about', 'variant' => 'simple', 'content' => ['heading' => 'Our story', 'body' => 'We started small.', 'stats' => []], ]; } }