AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PlanValidatorTest.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Models\Store;
  4. use App\StorePlan\PlanSchema;
  5. use App\StorePlan\PlanValidator;
  6. use App\StorePlan\SamplePlan;
  7. use Illuminate\Support\Arr;
  8. use Illuminate\Validation\ValidationException;
  9. use Tests\TestCase;
  10. class PlanValidatorTest extends TestCase
  11. {
  12. /**
  13. * @return array<string, mixed>
  14. */
  15. private function samplePlan(): array
  16. {
  17. return SamplePlan::for(new Store(['name' => 'Sambal Nenek']));
  18. }
  19. /**
  20. * @param array<string, mixed> $plan
  21. */
  22. private function assertInvalid(array $plan, string $errorKey): void
  23. {
  24. try {
  25. (new PlanValidator)->validate($plan);
  26. } catch (ValidationException $e) {
  27. $this->assertArrayHasKey($errorKey, $e->errors());
  28. return;
  29. }
  30. $this->fail("Expected the plan to be rejected for [$errorKey].");
  31. }
  32. public function test_the_sample_plan_is_valid_and_uses_every_section_type()
  33. {
  34. $plan = (new PlanValidator)->validate($this->samplePlan());
  35. $this->assertEqualsCanonicalizing(
  36. array_keys(PlanSchema::SECTIONS),
  37. array_column($plan['sections'], 'type'),
  38. );
  39. }
  40. public function test_unknown_fields_are_removed()
  41. {
  42. $plan = $this->samplePlan();
  43. $plan['evil'] = 'x';
  44. $plan['theme']['colors']['extra'] = '#000000';
  45. $plan['sections'][1]['content']['script'] = '<script>alert(1)</script>';
  46. $clean = (new PlanValidator)->validate($plan);
  47. $this->assertArrayNotHasKey('evil', $clean);
  48. $this->assertArrayNotHasKey('extra', $clean['theme']['colors']);
  49. $this->assertArrayNotHasKey('script', $clean['sections'][1]['content']);
  50. }
  51. public function test_colours_must_be_hex_values()
  52. {
  53. $plan = $this->samplePlan();
  54. $plan['theme']['colors']['primary'] = 'red; background: url(x)';
  55. $this->assertInvalid($plan, 'theme.colors.primary');
  56. }
  57. public function test_every_theme_colour_is_required()
  58. {
  59. $plan = $this->samplePlan();
  60. unset($plan['theme']['colors']['border']);
  61. $this->assertInvalid($plan, 'theme.colors.border');
  62. }
  63. public function test_fonts_must_come_from_the_allowed_list()
  64. {
  65. $plan = $this->samplePlan();
  66. $plan['theme']['fonts']['heading'] = 'comic-sans';
  67. $this->assertInvalid($plan, 'theme.fonts.heading');
  68. }
  69. public function test_unknown_section_types_are_rejected()
  70. {
  71. $plan = $this->samplePlan();
  72. $plan['sections'][0]['type'] = 'popup';
  73. $this->assertInvalid($plan, 'sections.0.type');
  74. }
  75. public function test_a_variant_must_belong_to_its_section_type()
  76. {
  77. $plan = $this->samplePlan();
  78. $plan['sections'][1]['variant'] = 'cards';
  79. $this->assertInvalid($plan, 'sections.1.variant');
  80. }
  81. public function test_required_section_content_is_enforced()
  82. {
  83. $plan = $this->samplePlan();
  84. unset($plan['sections'][1]['content']['headline']);
  85. $this->assertInvalid($plan, 'sections.1.content.headline');
  86. }
  87. public function test_unsafe_links_are_rejected()
  88. {
  89. foreach (['javascript:alert(1)', 'http://insecure.example', 'data:text/html,x', '//evil.example'] as $href) {
  90. $plan = $this->samplePlan();
  91. $plan['sections'][1]['content']['primaryCta']['href'] = $href;
  92. $this->assertInvalid($plan, 'sections.1.content.primaryCta.href');
  93. }
  94. }
  95. public function test_safe_links_are_accepted()
  96. {
  97. foreach (['#products', '/products/sambal', 'https://example.com/x', 'mailto:hi@example.com', 'tel:+60123456789'] as $href) {
  98. $plan = $this->samplePlan();
  99. $plan['sections'][1]['content']['primaryCta']['href'] = $href;
  100. $this->assertTrue((new PlanValidator)->passes($plan), "Expected [$href] to be accepted.");
  101. }
  102. }
  103. public function test_section_ids_must_be_unique()
  104. {
  105. $plan = $this->samplePlan();
  106. $plan['sections'][1]['id'] = $plan['sections'][0]['id'];
  107. $this->assertFalse((new PlanValidator)->passes($plan));
  108. }
  109. public function test_optional_parts_can_be_left_out()
  110. {
  111. $plan = $this->samplePlan();
  112. Arr::forget($plan, 'sections.0.content.cta');
  113. Arr::forget($plan, 'sections.1.content.secondaryCta');
  114. Arr::set($plan, 'sections.1.content.eyebrow', null);
  115. $this->assertTrue((new PlanValidator)->passes($plan));
  116. }
  117. public function test_a_plan_needs_at_least_one_section()
  118. {
  119. $plan = $this->samplePlan();
  120. $plan['sections'] = [];
  121. $this->assertInvalid($plan, 'sections');
  122. }
  123. public function test_garbage_input_is_rejected_without_crashing()
  124. {
  125. $this->assertFalse((new PlanValidator)->passes([]));
  126. $this->assertFalse((new PlanValidator)->passes(['version' => 1, 'theme' => 'x', 'sections' => 'y']));
  127. $this->assertFalse((new PlanValidator)->passes(['version' => 1, 'theme' => [], 'sections' => ['not-an-array', ['type' => ['a']]]]));
  128. }
  129. }