seedShop(); } /** * Ein Artikel aus genau einem Medium samt Angebot mit den gewünschten Rechten. * * @param array> $capabilities * @return array{0: LicenseArticle, 1: Offer} */ private function singleMedium(string $class, string $title, string $file, string $body, array $capabilities): array { Storage::disk('media_derived')->put($file, $body); $content = app(ContentService::class); $element = $content->createElement($class, $class, $title); $element->description = 'Beschreibung '.$title; $element->save(); $version = $content->publishElementVersion($element, null, ['file' => $file]); $articles = app(ArticleService::class); $article = $articles->create('einzel-'.$class.'-'.count($capabilities).'-'.md5($title), 'Testkauf'); $article->working_title = $title; $article->save(); $articles->publishRelease($article, $version, 'Einzelmedium.'); $offers = app(OfferService::class); $rights = $offers->createRightsProfile('Rechte '.$title, 'ehrenamtlich', $capabilities); $offer = $offers->create('Einzelkauf '.$title); $offers->activate($offer, [['target' => $article->fresh(), 'rights' => $rights]], [ 'grant_kind' => 'kauf', 'price_cents' => 1200, 'holder_kind' => 'person', 'licence_scope' => 'einzel', ]); return [$article->fresh(), $offer]; } private function buy(Offer $offer): void { $order = $this->orderOf($this->tina(), [$offer]); app(Checkout::class)->confirmTestPayment($order, 'test-dl-'.$offer->id); } public function test_a_single_film_is_in_the_library_and_downloads_with_its_title_as_file_name(): void { [$article, $offer] = $this->singleMedium('film', 'Lara im Park', 'inhalte/einzel/film.mp4', 'FILMINHALT', ['film' => ['streamen', 'laden']]); $this->buy($offer); $this->actingAs($this->tina())->get(route('member.library'))->assertOk()->assertSee('Lara im Park')->assertSee(route('member.article', $article), false); $this->get(route('member.article', $article))->assertOk()->assertSee('Herunterladen'); $link = $this->downloadUrl($article); $this->assertNotSame('', $link, 'Der Film bietet „Herunterladen“.'); $download = $this->get($link); $download->assertOk(); $this->assertStringContainsString('attachment', (string) $download->headers->get('Content-Disposition')); $this->assertStringContainsString('Lara im Park.mp4', rawurldecode((string) $download->headers->get('Content-Disposition'))); $this->assertSame('FILMINHALT', $download->streamedContent()); } public function test_a_single_film_with_download_only_rights_offers_the_download_and_no_dead_end(): void { [$article, $offer] = $this->singleMedium('film', 'Nur Download', 'inhalte/einzel/nur.mp4', 'NURDOWNLOAD', ['film' => ['laden']]); $this->buy($offer); $this->actingAs($this->tina())->get(route('member.article', $article)) ->assertOk() ->assertSee('Herunterladen') ->assertDontSee('Nicht enthalten. Für diesen Teil liegt keine Freigabe vor.'); } public function test_a_companion_pdf_downloads_as_a_real_pdf_with_view_and_download_decided_separately(): void { [$article, $offer] = $this->singleMedium('pdf', 'Begleitmaterial Abstand', 'inhalte/einzel/material.pdf', "%PDF-1.4\n%Abstand\n%%EOF\n", ['pdf' => ['ansehen', 'laden']]); $this->buy($offer); $page = $this->actingAs($this->tina())->get(route('member.article', $article))->assertOk()->assertSee('Ansehen')->assertSee('Herunterladen'); preg_match_all('/href="([^"]+)"/', $page->getContent(), $links); $download = collect($links[1])->map(fn (string $href): string => html_entity_decode($href))->first(fn (string $href): bool => str_contains($href, '/laden?')); $this->assertNotNull($download, 'Das PDF bietet einen Downloadlink.'); $response = $this->get($download)->assertOk(); $this->assertStringContainsString('attachment', (string) $response->headers->get('Content-Disposition')); $this->assertStringContainsString('Begleitmaterial Abstand.pdf', rawurldecode((string) $response->headers->get('Content-Disposition'))); $this->assertStringStartsWith('%PDF-1.4', $response->streamedContent()); } public function test_a_pdf_without_the_download_right_shows_no_download(): void { [$article, $offer] = $this->singleMedium('pdf', 'Nur ansehen', 'inhalte/einzel/ansehen.pdf', "%PDF-1.4\n%A\n%%EOF\n", ['pdf' => ['ansehen']]); $this->buy($offer); $this->actingAs($this->tina())->get(route('member.article', $article))->assertOk()->assertSee('Ansehen')->assertDontSee('Herunterladen'); } public function test_the_download_needs_payment_and_the_buying_account(): void { [$article, $offer] = $this->singleMedium('film', 'Gesperrt', 'inhalte/einzel/gesperrt.mp4', 'GESPERRT', ['film' => ['streamen', 'laden']]); $order = $this->orderOf($this->tina(), [$offer]); $this->actingAs($this->tina())->get(route('member.article', $article))->assertOk()->assertDontSee('Herunterladen'); app(Checkout::class)->confirmTestPayment($order, 'test-lock'); $this->get(route('member.article', $article))->assertSee('Herunterladen'); $url = $this->downloadUrl($article); auth()->logout(); $this->actingAs($this->max())->get($url)->assertForbidden(); $this->actingAs($this->max())->get(route('member.article', $article))->assertDontSee('Herunterladen'); } private function downloadUrl(LicenseArticle $article): string { $page = $this->get(route('member.article', $article)); preg_match_all('/href="([^"]+)"/', $page->getContent(), $links); return (string) collect($links[1])->map(fn (string $href): string => html_entity_decode($href))->first(fn (string $href): bool => str_contains($href, '/laden?')); } }