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 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Models\Store;
  4. use App\StorePlan\PlanSchema;
  5. use App\StorePlan\PlanUpgrader;
  6. use App\StorePlan\PlanValidator;
  7. use App\StorePlan\SamplePlan;
  8. use Illuminate\Support\Arr;
  9. use Illuminate\Validation\ValidationException;
  10. use Tests\TestCase;
  11. class PlanValidatorTest extends TestCase
  12. {
  13. /**
  14. * @return array<string, mixed>
  15. */
  16. private function samplePlan(): array
  17. {
  18. return SamplePlan::for(new Store(['name' => 'Sambal Nenek']));
  19. }
  20. /**
  21. * @param array<string, mixed> $plan
  22. */
  23. private function assertInvalid(array $plan, string $errorKey): void
  24. {
  25. try {
  26. (new PlanValidator)->validate($plan);
  27. } catch (ValidationException $e) {
  28. $this->assertArrayHasKey($errorKey, $e->errors());
  29. return;
  30. }
  31. $this->fail("Expected the plan to be rejected for [$errorKey].");
  32. }
  33. public function test_the_sample_plan_is_valid_and_uses_every_section_type()
  34. {
  35. $plan = (new PlanValidator)->validate($this->samplePlan());
  36. $this->assertSame(PlanSchema::VERSION, $plan['version']);
  37. $this->assertSame('home', $plan['pages'][0]['id']);
  38. $this->assertArrayHasKey('logoText', $plan['header']['content']);
  39. $this->assertArrayHasKey('text', $plan['footer']['content']);
  40. // Video is left out on purpose: the AI must only use a video the merchant gives, so the example has none.
  41. $this->assertEqualsCanonicalizing(
  42. array_values(array_diff(array_keys(PlanSchema::pageSections()), ['video'])),
  43. array_column($plan['pages'][0]['sections'], 'type'),
  44. );
  45. }
  46. public function test_unknown_fields_are_removed()
  47. {
  48. $plan = $this->samplePlan();
  49. $plan['evil'] = 'x';
  50. $plan['theme']['colors']['extra'] = '#000000';
  51. $plan['pages'][0]['sections'][0]['content']['script'] = '<script>alert(1)</script>';
  52. $clean = (new PlanValidator)->validate($plan);
  53. $this->assertArrayNotHasKey('evil', $clean);
  54. $this->assertArrayNotHasKey('extra', $clean['theme']['colors']);
  55. $this->assertArrayNotHasKey('script', $clean['pages'][0]['sections'][0]['content']);
  56. }
  57. public function test_colours_must_be_hex_values()
  58. {
  59. $plan = $this->samplePlan();
  60. $plan['theme']['colors']['primary'] = 'red; background: url(x)';
  61. $this->assertInvalid($plan, 'theme.colors.primary');
  62. }
  63. public function test_every_theme_colour_is_required()
  64. {
  65. $plan = $this->samplePlan();
  66. unset($plan['theme']['colors']['border']);
  67. $this->assertInvalid($plan, 'theme.colors.border');
  68. }
  69. public function test_fonts_must_come_from_the_allowed_list()
  70. {
  71. $plan = $this->samplePlan();
  72. $plan['theme']['fonts']['heading'] = 'comic-sans';
  73. $this->assertInvalid($plan, 'theme.fonts.heading');
  74. }
  75. public function test_unknown_section_types_are_rejected()
  76. {
  77. $plan = $this->samplePlan();
  78. $plan['pages'][0]['sections'][0]['type'] = 'popup';
  79. $this->assertInvalid($plan, 'pages.0.sections.0.type');
  80. }
  81. public function test_a_variant_must_belong_to_its_section_type()
  82. {
  83. $plan = $this->samplePlan();
  84. $plan['pages'][0]['sections'][0]['variant'] = 'cards';
  85. $this->assertInvalid($plan, 'pages.0.sections.0.variant');
  86. }
  87. public function test_required_section_content_is_enforced()
  88. {
  89. $plan = $this->samplePlan();
  90. unset($plan['pages'][0]['sections'][0]['content']['headline']);
  91. $this->assertInvalid($plan, 'pages.0.sections.0.content.headline');
  92. }
  93. public function test_unsafe_links_are_rejected()
  94. {
  95. foreach (['javascript:alert(1)', 'http://insecure.example', 'data:text/html,x', '//evil.example'] as $href) {
  96. $plan = $this->samplePlan();
  97. $plan['pages'][0]['sections'][0]['content']['primaryCta']['href'] = $href;
  98. $this->assertInvalid($plan, 'pages.0.sections.0.content.primaryCta.href');
  99. }
  100. }
  101. public function test_safe_links_are_accepted()
  102. {
  103. foreach (['#products', '/products/sambal', 'https://example.com/x', 'mailto:hi@example.com', 'tel:+60123456789'] as $href) {
  104. $plan = $this->samplePlan();
  105. $plan['pages'][0]['sections'][0]['content']['primaryCta']['href'] = $href;
  106. $this->assertTrue((new PlanValidator)->passes($plan), "Expected [$href] to be accepted.");
  107. }
  108. }
  109. public function test_section_ids_must_be_unique()
  110. {
  111. $plan = $this->samplePlan();
  112. $plan['pages'][0]['sections'][1]['id'] = $plan['pages'][0]['sections'][0]['id'];
  113. $this->assertFalse((new PlanValidator)->passes($plan));
  114. }
  115. public function test_optional_parts_can_be_left_out()
  116. {
  117. $plan = $this->samplePlan();
  118. Arr::forget($plan, 'header.content.cta');
  119. Arr::forget($plan, 'pages.0.sections.0.content.secondaryCta');
  120. Arr::set($plan, 'pages.0.sections.0.content.eyebrow', null);
  121. $this->assertTrue((new PlanValidator)->passes($plan));
  122. }
  123. public function test_a_plan_needs_at_least_one_section()
  124. {
  125. $plan = $this->samplePlan();
  126. $plan['pages'][0]['sections'] = [];
  127. $this->assertInvalid($plan, 'pages.0.sections');
  128. }
  129. public function test_garbage_input_is_rejected_without_crashing()
  130. {
  131. $this->assertFalse((new PlanValidator)->passes([]));
  132. $this->assertFalse((new PlanValidator)->passes(['version' => 2, 'theme' => 'x', 'header' => 'h', 'pages' => 'y']));
  133. $this->assertFalse((new PlanValidator)->passes(['version' => 2, 'theme' => [], 'pages' => ['not-an-array', ['id' => 'home', 'sections' => ['x', ['type' => ['a']]]]]]));
  134. }
  135. public function test_the_header_and_footer_are_required_and_checked()
  136. {
  137. $plan = $this->samplePlan();
  138. unset($plan['header']);
  139. $this->assertInvalid($plan, 'header');
  140. $plan = $this->samplePlan();
  141. $plan['footer']['content']['links'][0]['href'] = 'javascript:alert(1)';
  142. $this->assertInvalid($plan, 'footer.content.links.0.href');
  143. $plan = $this->samplePlan();
  144. $plan['header']['variant'] = 'cards';
  145. $this->assertInvalid($plan, 'header.variant');
  146. }
  147. public function test_a_page_cannot_contain_a_header_or_footer()
  148. {
  149. $plan = $this->samplePlan();
  150. $plan['pages'][0]['sections'][0] = ['id' => 'top', 'type' => 'header', ...$plan['header']];
  151. $this->assertInvalid($plan, 'pages.0.sections.0.type');
  152. }
  153. public function test_the_first_page_must_be_home_and_page_ids_cannot_clash_with_built_in_pages()
  154. {
  155. $plan = $this->samplePlan();
  156. $plan['pages'][0]['id'] = 'landing';
  157. $this->assertInvalid($plan, 'pages.0.id');
  158. $plan = $this->samplePlan();
  159. $plan['pages'][] = ['id' => 'shop', 'title' => 'Shop', 'sections' => [['id' => 'x', ...$this->aboutSection()]]];
  160. $this->assertInvalid($plan, 'pages.1.id');
  161. }
  162. public function test_a_plan_can_have_more_pages_but_section_ids_stay_unique_across_them()
  163. {
  164. $plan = $this->samplePlan();
  165. $plan['pages'][] = ['id' => 'our-story', 'title' => 'Our story', 'sections' => [['id' => 'story-body', ...$this->aboutSection()]]];
  166. $this->assertTrue((new PlanValidator)->passes($plan));
  167. $plan['pages'][1]['sections'][0]['id'] = $plan['pages'][0]['sections'][0]['id'];
  168. $this->assertInvalid($plan, 'pages.1.sections.0.id');
  169. }
  170. public function test_links_to_pages_must_point_to_a_page_that_exists()
  171. {
  172. $plan = $this->samplePlan();
  173. $plan['header']['content']['links'][0]['href'] = '/about';
  174. $this->assertInvalid($plan, 'header.content.links.0.href');
  175. $plan['pages'][] = ['id' => 'about', 'title' => 'About', 'sections' => [['id' => 'about-main', ...$this->aboutSection()]]];
  176. $this->assertTrue((new PlanValidator)->passes($plan));
  177. // The store's own pages are always fine to link to.
  178. foreach (['/shop', '/cart', '/products/sambal', '/about#about-main'] as $href) {
  179. $plan['footer']['content']['links'][0]['href'] = $href;
  180. $this->assertTrue((new PlanValidator)->passes($plan), "Expected [$href] to be accepted.");
  181. }
  182. $plan['pages'][0]['sections'][0]['content']['primaryCta']['href'] = '/nowhere';
  183. $this->assertInvalid($plan, 'pages.0.sections.0.content.primaryCta.href');
  184. }
  185. public function test_old_format_plans_are_rejected_until_upgraded()
  186. {
  187. $old = [
  188. 'version' => 1,
  189. 'theme' => $this->samplePlan()['theme'],
  190. 'sections' => [
  191. ['id' => 'header', 'type' => 'header', ...$this->samplePlan()['header']],
  192. ...$this->samplePlan()['pages'][0]['sections'],
  193. ['id' => 'footer', 'type' => 'footer', ...$this->samplePlan()['footer']],
  194. ],
  195. ];
  196. $this->assertFalse((new PlanValidator)->passes($old));
  197. $this->assertEquals($this->samplePlan(), PlanUpgrader::upgrade($old));
  198. $this->assertSame($this->samplePlan(), PlanUpgrader::upgrade($this->samplePlan()));
  199. }
  200. /**
  201. * @return array<string, mixed>
  202. */
  203. private function aboutSection(): array
  204. {
  205. return [
  206. 'type' => 'about',
  207. 'variant' => 'simple',
  208. 'content' => ['heading' => 'Our story', 'body' => 'We started small.', 'stats' => []],
  209. ];
  210. }
  211. }