AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PlanValidatorTest.php 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. $this->assertEqualsCanonicalizing(
  41. array_keys(PlanSchema::pageSections()),
  42. array_column($plan['pages'][0]['sections'], 'type'),
  43. );
  44. }
  45. public function test_unknown_fields_are_removed()
  46. {
  47. $plan = $this->samplePlan();
  48. $plan['evil'] = 'x';
  49. $plan['theme']['colors']['extra'] = '#000000';
  50. $plan['pages'][0]['sections'][0]['content']['script'] = '<script>alert(1)</script>';
  51. $clean = (new PlanValidator)->validate($plan);
  52. $this->assertArrayNotHasKey('evil', $clean);
  53. $this->assertArrayNotHasKey('extra', $clean['theme']['colors']);
  54. $this->assertArrayNotHasKey('script', $clean['pages'][0]['sections'][0]['content']);
  55. }
  56. public function test_colours_must_be_hex_values()
  57. {
  58. $plan = $this->samplePlan();
  59. $plan['theme']['colors']['primary'] = 'red; background: url(x)';
  60. $this->assertInvalid($plan, 'theme.colors.primary');
  61. }
  62. public function test_every_theme_colour_is_required()
  63. {
  64. $plan = $this->samplePlan();
  65. unset($plan['theme']['colors']['border']);
  66. $this->assertInvalid($plan, 'theme.colors.border');
  67. }
  68. public function test_fonts_must_come_from_the_allowed_list()
  69. {
  70. $plan = $this->samplePlan();
  71. $plan['theme']['fonts']['heading'] = 'comic-sans';
  72. $this->assertInvalid($plan, 'theme.fonts.heading');
  73. }
  74. public function test_unknown_section_types_are_rejected()
  75. {
  76. $plan = $this->samplePlan();
  77. $plan['pages'][0]['sections'][0]['type'] = 'popup';
  78. $this->assertInvalid($plan, 'pages.0.sections.0.type');
  79. }
  80. public function test_a_variant_must_belong_to_its_section_type()
  81. {
  82. $plan = $this->samplePlan();
  83. $plan['pages'][0]['sections'][0]['variant'] = 'cards';
  84. $this->assertInvalid($plan, 'pages.0.sections.0.variant');
  85. }
  86. public function test_required_section_content_is_enforced()
  87. {
  88. $plan = $this->samplePlan();
  89. unset($plan['pages'][0]['sections'][0]['content']['headline']);
  90. $this->assertInvalid($plan, 'pages.0.sections.0.content.headline');
  91. }
  92. public function test_unsafe_links_are_rejected()
  93. {
  94. foreach (['javascript:alert(1)', 'http://insecure.example', 'data:text/html,x', '//evil.example'] as $href) {
  95. $plan = $this->samplePlan();
  96. $plan['pages'][0]['sections'][0]['content']['primaryCta']['href'] = $href;
  97. $this->assertInvalid($plan, 'pages.0.sections.0.content.primaryCta.href');
  98. }
  99. }
  100. public function test_safe_links_are_accepted()
  101. {
  102. foreach (['#products', '/products/sambal', 'https://example.com/x', 'mailto:hi@example.com', 'tel:+60123456789'] as $href) {
  103. $plan = $this->samplePlan();
  104. $plan['pages'][0]['sections'][0]['content']['primaryCta']['href'] = $href;
  105. $this->assertTrue((new PlanValidator)->passes($plan), "Expected [$href] to be accepted.");
  106. }
  107. }
  108. public function test_section_ids_must_be_unique()
  109. {
  110. $plan = $this->samplePlan();
  111. $plan['pages'][0]['sections'][1]['id'] = $plan['pages'][0]['sections'][0]['id'];
  112. $this->assertFalse((new PlanValidator)->passes($plan));
  113. }
  114. public function test_optional_parts_can_be_left_out()
  115. {
  116. $plan = $this->samplePlan();
  117. Arr::forget($plan, 'header.content.cta');
  118. Arr::forget($plan, 'pages.0.sections.0.content.secondaryCta');
  119. Arr::set($plan, 'pages.0.sections.0.content.eyebrow', null);
  120. $this->assertTrue((new PlanValidator)->passes($plan));
  121. }
  122. public function test_a_plan_needs_at_least_one_section()
  123. {
  124. $plan = $this->samplePlan();
  125. $plan['pages'][0]['sections'] = [];
  126. $this->assertInvalid($plan, 'pages.0.sections');
  127. }
  128. public function test_garbage_input_is_rejected_without_crashing()
  129. {
  130. $this->assertFalse((new PlanValidator)->passes([]));
  131. $this->assertFalse((new PlanValidator)->passes(['version' => 2, 'theme' => 'x', 'header' => 'h', 'pages' => 'y']));
  132. $this->assertFalse((new PlanValidator)->passes(['version' => 2, 'theme' => [], 'pages' => ['not-an-array', ['id' => 'home', 'sections' => ['x', ['type' => ['a']]]]]]));
  133. }
  134. public function test_the_header_and_footer_are_required_and_checked()
  135. {
  136. $plan = $this->samplePlan();
  137. unset($plan['header']);
  138. $this->assertInvalid($plan, 'header');
  139. $plan = $this->samplePlan();
  140. $plan['footer']['content']['links'][0]['href'] = 'javascript:alert(1)';
  141. $this->assertInvalid($plan, 'footer.content.links.0.href');
  142. $plan = $this->samplePlan();
  143. $plan['header']['variant'] = 'cards';
  144. $this->assertInvalid($plan, 'header.variant');
  145. }
  146. public function test_a_page_cannot_contain_a_header_or_footer()
  147. {
  148. $plan = $this->samplePlan();
  149. $plan['pages'][0]['sections'][0] = ['id' => 'top', 'type' => 'header', ...$plan['header']];
  150. $this->assertInvalid($plan, 'pages.0.sections.0.type');
  151. }
  152. public function test_the_first_page_must_be_home_and_page_ids_cannot_clash_with_built_in_pages()
  153. {
  154. $plan = $this->samplePlan();
  155. $plan['pages'][0]['id'] = 'landing';
  156. $this->assertInvalid($plan, 'pages.0.id');
  157. $plan = $this->samplePlan();
  158. $plan['pages'][] = ['id' => 'shop', 'title' => 'Shop', 'sections' => [['id' => 'x', ...$this->aboutSection()]]];
  159. $this->assertInvalid($plan, 'pages.1.id');
  160. }
  161. public function test_a_plan_can_have_more_pages_but_section_ids_stay_unique_across_them()
  162. {
  163. $plan = $this->samplePlan();
  164. $plan['pages'][] = ['id' => 'our-story', 'title' => 'Our story', 'sections' => [['id' => 'story-body', ...$this->aboutSection()]]];
  165. $this->assertTrue((new PlanValidator)->passes($plan));
  166. $plan['pages'][1]['sections'][0]['id'] = $plan['pages'][0]['sections'][0]['id'];
  167. $this->assertInvalid($plan, 'pages.1.sections.0.id');
  168. }
  169. public function test_old_format_plans_are_rejected_until_upgraded()
  170. {
  171. $old = [
  172. 'version' => 1,
  173. 'theme' => $this->samplePlan()['theme'],
  174. 'sections' => [
  175. ['id' => 'header', 'type' => 'header', ...$this->samplePlan()['header']],
  176. ...$this->samplePlan()['pages'][0]['sections'],
  177. ['id' => 'footer', 'type' => 'footer', ...$this->samplePlan()['footer']],
  178. ],
  179. ];
  180. $this->assertFalse((new PlanValidator)->passes($old));
  181. $this->assertEquals($this->samplePlan(), PlanUpgrader::upgrade($old));
  182. $this->assertSame($this->samplePlan(), PlanUpgrader::upgrade($this->samplePlan()));
  183. }
  184. /**
  185. * @return array<string, mixed>
  186. */
  187. private function aboutSection(): array
  188. {
  189. return [
  190. 'type' => 'about',
  191. 'variant' => 'simple',
  192. 'content' => ['heading' => 'Our story', 'body' => 'We started small.', 'stats' => []],
  193. ];
  194. }
  195. }