| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
-
- namespace Tests\Feature\Stores;
-
- use App\Models\Store;
- use App\Models\User;
- use App\StorePlan\SamplePlan;
- use App\StorePlan\SectionEditor;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Inertia\Testing\AssertableInertia as Assert;
- use Tests\TestCase;
-
- class DesignSectionBoxesTest extends TestCase
- {
- use RefreshDatabase;
-
- private User $user;
-
- private Store $store;
-
- protected function setUp(): void
- {
- parent::setUp();
-
- $this->user = User::factory()->create();
- $this->store = Store::factory()->for($this->user)->create();
- $plan = SamplePlan::for($this->store);
- // Start without the sample's bento, so adding one is clear.
- $this->store->plan = SectionEditor::remove($plan, 'highlights');
- $this->store->save();
- $this->actingAs($this->user);
- }
-
- /**
- * @return list<string>
- */
- private function homeIds(): array
- {
- return array_column($this->store->fresh()->plan['pages'][0]['sections'], 'id');
- }
-
- /**
- * @return array<string, mixed>
- */
- private function section(string $id): array
- {
- return collect($this->store->fresh()->plan['pages'][0]['sections'])->firstWhere('id', $id);
- }
-
- public function test_a_video_can_be_added_after_a_section_changed_and_removed()
- {
- $this->post(route('stores.videos.store', $this->store->slug), [
- 'video_url' => 'https://youtu.be/otej7WLdPh0?si=eYqNO3QE14avO4VL',
- 'heading' => 'How we make it',
- 'text' => '',
- 'variant' => 'wide',
- 'after' => 'about',
- ])->assertSessionHasNoErrors();
-
- $ids = $this->homeIds();
- $this->assertSame('video', $ids[array_search('about', $ids, true) + 1]);
- $this->assertSame('https://youtu.be/otej7WLdPh0?si=eYqNO3QE14avO4VL', $this->section('video')['content']['videoUrl']);
-
- $this->get(route('stores.show', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->where('videoSections.0.section_id', 'video')
- ->where('videoSections.0.heading', 'How we make it')
- ->where('homeSections.0.id', 'hero'));
-
- $this->put(route('stores.videos.update', [$this->store->slug, 'video']), [
- 'video_url' => 'https://vimeo.com/76979871', 'heading' => 'Our kitchen', 'text' => 'A day with Nenek.', 'variant' => 'split',
- ])->assertSessionHasNoErrors();
-
- $video = $this->section('video');
- $this->assertSame('split', $video['variant']);
- $this->assertSame('A day with Nenek.', $video['content']['text']);
-
- // A second video gets its own id.
- $this->post(route('stores.videos.store', $this->store->slug), [
- 'video_url' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', 'heading' => 'Second', 'variant' => 'wide', 'after' => 'hero',
- ])->assertSessionHasNoErrors();
- $this->assertContains('video-2', $this->homeIds());
-
- $this->delete(route('stores.videos.destroy', [$this->store->slug, 'video']))->assertSessionHasNoErrors();
- $this->assertNotContains('video', $this->homeIds());
- }
-
- public function test_video_links_and_sections_are_checked()
- {
- $url = route('stores.videos.store', $this->store->slug);
-
- $this->post($url, ['video_url' => 'https://evil.example/v.mp4', 'heading' => 'X', 'variant' => 'wide'])->assertSessionHasErrors('video_url');
- $this->post($url, ['video_url' => 'https://youtu.be/otej7WLdPh0', 'heading' => '', 'variant' => 'wide'])->assertSessionHasErrors('heading');
- $this->post($url, ['video_url' => 'https://youtu.be/otej7WLdPh0', 'heading' => 'X', 'variant' => 'wide', 'after' => 'nowhere'])->assertSessionHasErrors('after');
-
- // Only video sections can be edited here.
- $this->put(route('stores.videos.update', [$this->store->slug, 'about']), [
- 'video_url' => 'https://youtu.be/otej7WLdPh0', 'heading' => 'X', 'variant' => 'wide',
- ])->assertNotFound();
-
- $this->actingAs(User::factory()->create())
- ->post($url, ['video_url' => 'https://youtu.be/otej7WLdPh0', 'heading' => 'X', 'variant' => 'wide'])
- ->assertNotFound();
- }
-
- public function test_bento_tiles_start_filled_and_can_be_edited()
- {
- $product = $this->store->products()->create(['name' => 'Sambal', 'price' => 1000, 'stock' => 3, 'status' => 'active']);
- $product->images()->create(['store_id' => $this->store->id, 'path' => 'products/1/s.jpg', 'position' => 1]);
- $collection = $this->store->collections()->create(['name' => 'Gift sets']);
-
- $this->post(route('stores.bento.store', $this->store->slug), ['after' => 'trust'])->assertSessionHasNoErrors();
-
- $ids = $this->homeIds();
- $this->assertSame('highlights', $ids[array_search('trust', $ids, true) + 1]);
- $tiles = $this->section('highlights')['content']['tiles'];
- $this->assertSame(['/shop', '/collections/'.$collection->slug, '#about'], array_column($tiles, 'href'));
- $this->assertSame('product:'.$product->slug, $tiles[0]['image']);
-
- $this->get(route('stores.show', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->where('bentoSections.0.tiles.1.title', 'Gift sets')
- ->where('linkSuggestions.1.href', '/collections/gift-sets'));
-
- $this->put(route('stores.bento.update', [$this->store->slug, 'highlights']), [
- 'heading' => 'Start here',
- 'variant' => 'even',
- 'tiles' => [
- ['title' => 'Everything', 'text' => '', 'href' => '/shop', 'image' => null, 'tone' => 'primary'],
- ['title' => 'Gifts', 'text' => 'Ready to give', 'href' => '/collections/gift-sets', 'image' => 'product:'.$product->slug, 'tone' => 'accent'],
- ['title' => 'Story', 'href' => '#about', 'tone' => 'dark'],
- ['title' => 'FAQ', 'href' => '#faq', 'tone' => 'surface'],
- ],
- ])->assertSessionHasNoErrors();
-
- $bento = $this->section('highlights');
- $this->assertSame('even', $bento['variant']);
- $this->assertSame('Start here', $bento['content']['heading']);
- $this->assertCount(4, $bento['content']['tiles']);
- $this->assertNull($bento['content']['tiles'][0]['text']);
-
- $this->delete(route('stores.bento.destroy', [$this->store->slug, 'highlights']))->assertSessionHasNoErrors();
- $this->assertNotContains('highlights', $this->homeIds());
- }
-
- public function test_bento_tiles_are_checked()
- {
- $this->post(route('stores.bento.store', $this->store->slug), ['after' => 'hero']);
- $url = route('stores.bento.update', [$this->store->slug, 'highlights']);
- $tile = ['title' => 'Shop', 'href' => '/shop', 'tone' => 'primary'];
- $before = $this->section('highlights');
-
- $this->put($url, ['variant' => 'mosaic', 'tiles' => [$tile, $tile]])->assertSessionHasErrors('tiles');
- $this->put($url, ['variant' => 'mosaic', 'tiles' => [$tile, $tile, [...$tile, 'href' => 'javascript:alert(1)']]])->assertSessionHasErrors('tiles.2.href');
- $this->put($url, ['variant' => 'mosaic', 'tiles' => [$tile, $tile, [...$tile, 'image' => 'product:not-ours']]])->assertSessionHasErrors('section');
- // A link to a page the store does not have is caught by the plan checker.
- $this->put($url, ['variant' => 'mosaic', 'tiles' => [$tile, $tile, [...$tile, 'href' => '/no-such-page']]])->assertSessionHasErrors('section');
-
- $this->assertSame($before, $this->section('highlights'));
- }
- }
|