171 lines
6.2 KiB
PHP
171 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Domain\Catalog\ArticleService;
|
|
use App\Domain\Catalog\ContentService;
|
|
use App\Domain\Catalog\DomainRuleException;
|
|
use App\Domain\Catalog\LicenseArticle;
|
|
use App\Domain\Catalog\Offer;
|
|
use App\Domain\Catalog\OfferService;
|
|
use App\Domain\Catalog\OfferStatus;
|
|
use App\Domain\Catalog\OfferVersion;
|
|
use App\Domain\Catalog\RightsProfileVersion;
|
|
use App\Domain\Commerce\Checkout;
|
|
use App\Domain\Entitlement\Acquisition;
|
|
use App\Domain\Entitlement\Grant;
|
|
use App\Domain\Entitlement\GrantIssuer;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class OfferVersioningTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_a_second_activation_leaves_one_active_version_and_keeps_the_first_intact(): void
|
|
{
|
|
[$article, $rights, $offer] = $this->offerWithArticle();
|
|
$offers = app(OfferService::class);
|
|
|
|
$first = $offers->activate($offer, [
|
|
['target' => $article, 'rights' => $rights],
|
|
], ['grant_kind' => 'kauf', 'price_cents' => 500]);
|
|
|
|
$second = $offers->activate($offer, [
|
|
['target' => $article, 'rights' => $rights],
|
|
], ['grant_kind' => 'kauf', 'price_cents' => 900]);
|
|
|
|
$first->refresh();
|
|
$second->refresh();
|
|
|
|
$this->assertSame(OfferStatus::Beendet, $first->status);
|
|
$this->assertSame(500, $first->price_cents);
|
|
$this->assertSame(OfferStatus::Aktiv, $second->status);
|
|
$this->assertSame(1, OfferVersion::query()->where('offer_id', $offer->id)->where('status', OfferStatus::Aktiv)->count());
|
|
}
|
|
|
|
public function test_an_active_version_cannot_change_except_to_beendet(): void
|
|
{
|
|
[, $rights, $offer] = $this->offerWithArticle();
|
|
$version = app(OfferService::class)->activate($offer, [
|
|
['target' => $this->publishedArticle('preis', 'Preis'), 'rights' => $rights],
|
|
], ['grant_kind' => 'kauf', 'price_cents' => 100]);
|
|
|
|
$version->price_cents = 200;
|
|
|
|
$this->expectException(DomainRuleException::class);
|
|
$version->save();
|
|
}
|
|
|
|
public function test_registration_uses_only_the_highest_active_duplicate_version(): void
|
|
{
|
|
[, $rights, $offer] = $this->offerWithArticle();
|
|
$offers = app(OfferService::class);
|
|
$low = $offers->activate($offer, [
|
|
['target' => $this->publishedArticle('reg-a', 'Reg A'), 'rights' => $rights],
|
|
], ['grant_kind' => 'bei_registrierung']);
|
|
|
|
$high = OfferVersion::query()->create([
|
|
'offer_id' => $offer->id,
|
|
'number' => $low->number + 1,
|
|
'status' => OfferStatus::Aktiv,
|
|
'term_rule' => $low->term_rule,
|
|
'update_promise' => $low->update_promise,
|
|
'dynamic' => false,
|
|
'grant_kind' => 'bei_registrierung',
|
|
'holder_kind' => 'person',
|
|
'activated_at' => now(),
|
|
]);
|
|
|
|
$user = User::factory()->create();
|
|
app(GrantIssuer::class)->grantRegistrationOffers($user);
|
|
|
|
$acquisitions = Acquisition::query()
|
|
->where('party_id', $user->party_id)
|
|
->where('reference_type', 'offer')
|
|
->where('reference_id', $offer->id)
|
|
->get();
|
|
|
|
$this->assertCount(1, $acquisitions);
|
|
$this->assertSame($high->id, (int) ($acquisitions->first()->conditions['offer_version_id'] ?? 0));
|
|
|
|
app(GrantIssuer::class)->grantRegistrationOffers($user);
|
|
$this->assertSame(1, Acquisition::query()->where('party_id', $user->party_id)->where('reference_id', $offer->id)->count());
|
|
}
|
|
|
|
public function test_order_line_stores_offer_name_and_keeps_it_when_the_offer_is_renamed(): void
|
|
{
|
|
[, $rights, $offer] = $this->offerWithArticle();
|
|
$offers = app(OfferService::class);
|
|
$offers->activate($offer, [
|
|
['target' => $this->publishedArticle('kauf', 'Kauf'), 'rights' => $rights],
|
|
], ['grant_kind' => 'kauf', 'price_cents' => 1200]);
|
|
|
|
$member = User::factory()->create();
|
|
$order = app(Checkout::class)->place($member, $offer);
|
|
$line = $order->lines()->first();
|
|
|
|
$this->assertSame('Testangebot', $line->offer_name);
|
|
|
|
$offer->update(['name' => 'Umbenannt']);
|
|
|
|
$response = $this->actingAs($member)->get(route('member.order', $order));
|
|
|
|
$response->assertOk()->assertSee('Testangebot')->assertDontSee('Umbenannt');
|
|
}
|
|
|
|
public function test_an_existing_grant_keeps_snapshot_and_release_reference(): void
|
|
{
|
|
[$article, $rights, $offer] = $this->offerWithArticle();
|
|
app(OfferService::class)->activate($offer, [
|
|
['target' => $article, 'rights' => $rights],
|
|
], ['grant_kind' => 'auf_abruf']);
|
|
|
|
$member = User::factory()->create();
|
|
app(GrantIssuer::class)->claim($member, $offer);
|
|
$grant = Grant::query()->first();
|
|
$snapshot = $grant->rights_snapshot;
|
|
$releaseId = $grant->release_at_grant_id;
|
|
|
|
app(OfferService::class)->activate($offer, [
|
|
['target' => $article, 'rights' => $rights],
|
|
], ['grant_kind' => 'auf_abruf']);
|
|
|
|
$grant->refresh();
|
|
$this->assertSame($snapshot, $grant->rights_snapshot);
|
|
$this->assertSame($releaseId, $grant->release_at_grant_id);
|
|
}
|
|
|
|
/**
|
|
* @return array{0: LicenseArticle, 1: RightsProfileVersion, 2: Offer}
|
|
*/
|
|
private function offerWithArticle(): array
|
|
{
|
|
$article = $this->publishedArticle('basis', 'Basis');
|
|
$rights = $this->rights();
|
|
$offer = app(OfferService::class)->create('Testangebot');
|
|
|
|
return [$article, $rights, $offer];
|
|
}
|
|
|
|
private function publishedArticle(string $code, string $title): LicenseArticle
|
|
{
|
|
$content = app(ContentService::class);
|
|
$version = $content->publishElementVersion($content->createElement('text', 'text', $title), $title);
|
|
|
|
return app(ArticleService::class)->publishRelease(
|
|
app(ArticleService::class)->create($code),
|
|
$version,
|
|
'Testartikel.',
|
|
)->article->fresh();
|
|
}
|
|
|
|
private function rights(): RightsProfileVersion
|
|
{
|
|
return app(OfferService::class)->createRightsProfile('Lesen', 'ehrenamtlich', [
|
|
'text' => ['ansehen'],
|
|
]);
|
|
}
|
|
}
|