From 97668add7e43d85794c8b484f77f921e5f5803eb Mon Sep 17 00:00:00 2001 From: philipp lang Date: Fri, 27 Oct 2023 00:48:50 +0200 Subject: [PATCH] Fix: Allow umlauts in Contribution generator --- app/Contribution/Actions/GenerateAction.php | 2 +- app/Rules/JsonBase64Rule.php | 6 +- resources/js/views/contribution/VIndex.vue | 66 +++++++------------ tests/Feature/Contribution/StoreTest.php | 22 ++----- .../ContributionRequestFactory.php | 6 +- 5 files changed, 36 insertions(+), 66 deletions(-) diff --git a/app/Contribution/Actions/GenerateAction.php b/app/Contribution/Actions/GenerateAction.php index 08d4d36d..41333bcc 100644 --- a/app/Contribution/Actions/GenerateAction.php +++ b/app/Contribution/Actions/GenerateAction.php @@ -49,6 +49,6 @@ class GenerateAction */ private function payload(ActionRequest $request): array { - return json_decode(base64_decode($request->input('payload', '')), true); + return json_decode(urldecode(base64_decode($request->input('payload', ''))), true); } } diff --git a/app/Rules/JsonBase64Rule.php b/app/Rules/JsonBase64Rule.php index a444733b..ddf5a281 100644 --- a/app/Rules/JsonBase64Rule.php +++ b/app/Rules/JsonBase64Rule.php @@ -33,11 +33,13 @@ class JsonBase64Rule implements Rule return false; } - if (base64_encode(base64_decode($value, true)) !== $value) { + $decoded = urldecode(base64_decode($value, true)); + + if (base64_encode(urlencode($decoded)) !== $value) { return false; } - if (!is_array(json_decode(base64_decode($value), true))) { + if (!is_array(json_decode($decoded, true))) { return false; } diff --git a/resources/js/views/contribution/VIndex.vue b/resources/js/views/contribution/VIndex.vue index e5a34c60..b838fa10 100644 --- a/resources/js/views/contribution/VIndex.vue +++ b/resources/js/views/contribution/VIndex.vue @@ -1,49 +1,31 @@ @@ -52,6 +34,11 @@ import debounce from 'lodash/debounce'; export default { + props: { + data: {}, + countries: {}, + compilers: {}, + }, data: function () { return { search: { @@ -70,11 +57,6 @@ export default { }, }; }, - props: { - data: {}, - countries: {}, - compilers: {}, - }, computed: { searchText: { get() { @@ -99,7 +81,7 @@ export default { async submit() { try { await this.axios.post('/contribution-validate', this.values); - var payload = btoa(JSON.stringify(this.values)); + var payload = btoa(encodeURIComponent(JSON.stringify(this.values))); window.open(`/contribution-generate?payload=${payload}`); } catch (e) { this.errorsFromException(e); diff --git a/tests/Feature/Contribution/StoreTest.php b/tests/Feature/Contribution/StoreTest.php index fe469c2a..c3a3286f 100644 --- a/tests/Feature/Contribution/StoreTest.php +++ b/tests/Feature/Contribution/StoreTest.php @@ -36,20 +36,18 @@ class StoreTest extends TestCase $this->withoutExceptionHandling(); Tex::spy(); $this->login()->loginNami(); - $country = Country::factory()->create(); $member1 = Member::factory()->defaults()->for(Gender::factory())->create(['address' => 'Maxstr 44', 'zip' => '42719', 'firstname' => 'Max', 'lastname' => 'Muster']); $member2 = Member::factory()->defaults()->for(Gender::factory())->create(['address' => 'Maxstr 44', 'zip' => '42719', 'firstname' => 'Jane', 'lastname' => 'Muster']); $response = $this->call('GET', '/contribution-generate', [ - 'payload' => base64_encode(json_encode([ - 'country' => $country->id, + 'payload' => ContributionRequestFactory::new()->type($type)->state([ 'dateFrom' => '1991-06-15', 'dateUntil' => '1991-06-16', 'eventName' => 'Super tolles Lager', 'members' => [$member1->id, $member2->id], 'type' => $type, 'zipLocation' => '42777 SG', - ])), + ])->toBase64(), ]); $response->assertSessionDoesntHaveErrors(); @@ -61,22 +59,10 @@ class StoreTest extends TestCase { $this->withoutExceptionHandling()->login()->loginNami(); Tex::spy(); - $member = Member::factory()->defaults()->for(Gender::factory())->create(); - - InvoiceSettings::fake([ - 'from_long' => 'Stamm BiPi', - ]); + InvoiceSettings::fake(['from_long' => 'Stamm BiPi']); $this->call('GET', '/contribution-generate', [ - 'payload' => base64_encode(json_encode([ - 'country' => Country::factory()->create()->id, - 'dateFrom' => '1991-06-15', - 'dateUntil' => '1991-06-16', - 'eventName' => 'Super tolles Lager', - 'members' => [$member->id], - 'type' => CitySolingenDocument::class, - 'zipLocation' => '42777 SG', - ])), + 'payload' => ContributionRequestFactory::new()->type(CitySolingenDocument::class)->toBase64(), ]); Tex::assertCompiled(CitySolingenDocument::class, fn ($document) => $document->hasAllContent(['Stamm BiPi'])); diff --git a/tests/RequestFactories/ContributionRequestFactory.php b/tests/RequestFactories/ContributionRequestFactory.php index 883c70b5..d4dd5a5a 100644 --- a/tests/RequestFactories/ContributionRequestFactory.php +++ b/tests/RequestFactories/ContributionRequestFactory.php @@ -15,11 +15,11 @@ class ContributionRequestFactory extends RequestFactory $compilers = collect(app(ContributionFactory::class)->compilerSelect())->pluck('class'); return [ - 'country' => $this->faker->randomElement(Country::get())->id, + 'country' => Country::factory()->create()->id, 'dateFrom' => $this->faker->date(), 'dateUntil' => $this->faker->date(), 'eventName' => $this->faker->words(3, true), - 'members' => [$this->faker->randomElement(Member::get())->id], + 'members' => [Member::factory()->defaults()->create()->id], 'type' => $this->faker->randomElement($compilers), 'zipLocation' => $this->faker->city, ]; @@ -27,7 +27,7 @@ class ContributionRequestFactory extends RequestFactory public function toBase64(): string { - return base64_encode(json_encode($this->create())); + return base64_encode(urlencode(json_encode($this->create()))); } /**