Paket 1: eine aktive Angebotsfassung und Bestellbezeichnung
Deploy Test / test (push) Failing after 10s
Deploy Test / deploy (push) Skipped

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-11-16 06:54:43 +01:00
1 parent 8cd382c357
commit 7820bececb
13 files changed
+529 -25

No files matched your search

@@ -0,0 +1,107 @@
<?php
namespace Tests\Feature;
use App\Domain\Catalog\ArticleService;
use App\Domain\Catalog\ContentService;
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 Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Str;
use Tests\TestCase;
class OfferActivationParallelTest extends TestCase
{
private ?string $databasePath = null;
protected function setUp(): void
{
parent::setUp();
$this->databasePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'luf_parallel_'.Str::uuid()->toString().'.sqlite';
touch($this->databasePath);
config(['database.connections.sqlite.database' => $this->databasePath]);
DB::purge('sqlite');
DB::reconnect('sqlite');
Artisan::call('migrate', ['--force' => true]);
}
protected function tearDown(): void
{
if ($this->databasePath !== null && is_file($this->databasePath)) {
@unlink($this->databasePath);
}
config(['database.connections.sqlite.database' => ':memory:']);
DB::purge('sqlite');
parent::tearDown();
}
public function test_overlapping_activations_leave_exactly_one_active_version(): void
{
[$article, $rights, $offer] = $this->offerWithArticle();
$baseEnv = [
'APP_ENV' => 'testing',
'APP_KEY' => config('app.key'),
'DB_CONNECTION' => 'sqlite',
'DB_DATABASE' => $this->databasePath,
'CACHE_STORE' => 'array',
'QUEUE_CONNECTION' => 'sync',
'SESSION_DRIVER' => 'array',
];
$worker = base_path('tests/Support/offer_activate_worker.php');
$results = Process::pool(function ($pool) use ($baseEnv, $worker, $offer, $article, $rights): void {
$pool->as('a')
->path(base_path())
->env($baseEnv)
->timeout(120)
->command([PHP_BINARY, $worker, $this->databasePath, (string) $offer->id, (string) $article->id, (string) $rights->id, '1000', '80']);
$pool->as('b')
->path(base_path())
->env($baseEnv)
->timeout(120)
->command([PHP_BINARY, $worker, $this->databasePath, (string) $offer->id, (string) $article->id, (string) $rights->id, '2000', '0']);
})->start()->wait();
$this->assertTrue($results['a']->successful(), $results['a']->errorOutput());
$this->assertTrue($results['b']->successful(), $results['b']->errorOutput());
$active = OfferVersion::query()->where('offer_id', $offer->id)->where('status', OfferStatus::Aktiv)->get();
$this->assertCount(1, $active);
$versions = OfferVersion::query()->where('offer_id', $offer->id)->orderBy('number')->get();
$this->assertCount(2, $versions);
$this->assertSame(OfferStatus::Beendet, $versions->first()->status);
$this->assertContains($versions->last()->price_cents, [1000, 2000]);
$this->assertNotSame($versions->first()->price_cents, $versions->last()->price_cents);
}
/**
* @return array{0: LicenseArticle, 1: RightsProfileVersion, 2: Offer}
*/
private function offerWithArticle(): array
{
$content = app(ContentService::class);
$version = $content->publishElementVersion($content->createElement('text', 'text', 'Basis'), 'Basis');
$article = app(ArticleService::class)->publishRelease(
app(ArticleService::class)->create('parallel-basis'),
$version,
'Paralleltest.',
)->article->fresh();
$rights = app(OfferService::class)->createRightsProfile('Lesen', 'ehrenamtlich', ['text' => ['ansehen']]);
$offer = app(OfferService::class)->create('Parallelangebot');
return [$article, $rights, $offer];
}
}
+170
View File
@@ -0,0 +1,170 @@
<?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'],
]);
}
}