Completed
Push — master ( 530061...ece7ee )
by Jeroen De
82:37 queued 40:45
created

testWhenDonationTimestampCookieIsSet_itIsNotOverwritten()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Tests\EdgeToEdge\Routes;
6
7
use Symfony\Component\BrowserKit\Cookie;
8
use Symfony\Component\HttpKernel\Client;
9
use WMDE\Fundraising\Frontend\App\RouteHandlers\ShowDonationConfirmationHandler;
10
use WMDE\Fundraising\Frontend\DonationContext\Domain\Model\Donation;
11
use WMDE\Fundraising\Frontend\Factories\FunFunFactory;
12
use WMDE\Fundraising\Frontend\PaymentContext\Domain\Model\DirectDebitPayment;
13
use WMDE\Fundraising\Frontend\Presentation\DonationConfirmationPageSelector;
14
use WMDE\Fundraising\Frontend\Tests\Data\ValidDonation;
15
use WMDE\Fundraising\Frontend\Tests\EdgeToEdge\WebRouteTestCase;
16
use WMDE\Fundraising\Frontend\Tests\Fixtures\FixedTokenGenerator;
17
18
/**
19
 * @licence GNU GPL v2+
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
class ShowDonationConfirmationRouteTest extends WebRouteTestCase {
23
24
	const CORRECT_ACCESS_TOKEN = 'KindlyAllowMeAccess';
25
26
	const ACCESS_DENIED_TEXT = 'keine Berechtigung';
27
28
	public function testGivenValidRequest_confirmationPageContainsDonationData() {
29
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
30
			$factory->setDonationConfirmationPageSelector(
31
				new DonationConfirmationPageSelector( $this->newEmptyConfirmationPageConfig() )
32
			);
33
34
			$donation = $this->newStoredDonation( $factory );
35
36
			$responseContent = $this->retrieveDonationConfirmation( $client, $donation->getId() );
37
38
			$this->assertDonationDataInResponse( $donation, $responseContent );
39
			$this->assertContains( 'Template Name: DonationConfirmation.twig', $responseContent );
40
		} );
41
	}
42
43
	public function testGivenValidPostRequest_confirmationPageContainsDonationData() {
44
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
45
			$factory->setDonationConfirmationPageSelector(
46
				new DonationConfirmationPageSelector( $this->newEmptyConfirmationPageConfig() )
47
			);
48
49
			$donation = $this->newStoredDonation( $factory );
50
51
			$responseContent = $this->retrieveDonationConfirmation( $client, $donation->getId() );
52
53
			$this->assertDonationDataInResponse( $donation, $responseContent );
54
			$this->assertContains( 'Template Name: DonationConfirmation.twig', $responseContent );
55
		} );
56
	}
57
58
	public function testGivenValidPostRequest_embeddedMembershipFormContainsDonationData() {
59
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
60
			$factory->setDonationConfirmationPageSelector(
61
				new DonationConfirmationPageSelector( $this->newEmptyConfirmationPageConfig() )
62
			);
63
64
			$donation = $this->newStoredDonation( $factory );
65
66
			$responseContent = $this->retrieveDonationConfirmation( $client, $donation->getId() );
67
68
			$this->assertEmbeddedMembershipFormIsPrefilled( $donation, $responseContent );
69
			$this->assertContains( 'Template Name: DonationConfirmation.twig', $responseContent );
70
		} );
71
	}
72
73
	private function assertEmbeddedMembershipFormIsPrefilled( Donation $donation, string $responseContent ) {
74
		$personName = $donation->getDonor()->getName();
75
		$physicalAddress = $donation->getDonor()->getPhysicalAddress();
76
		/** @var DirectDebitPayment $paymentMethod */
77
		$paymentMethod = $donation->getPaymentMethod();
78
		$bankData = $paymentMethod->getBankData();
79
		$this->assertContains( 'initialFormValues.addressType: ' . $personName->getPersonType(), $responseContent );
80
		$this->assertContains( 'initialFormValues.salutation: ' . $personName->getSalutation(), $responseContent );
81
		$this->assertContains( 'initialFormValues.title: ' . $personName->getTitle(), $responseContent );
82
		$this->assertContains( 'initialFormValues.firstName: ' . $personName->getFirstName(), $responseContent );
83
		$this->assertContains( 'initialFormValues.lastName: ' . $personName->getLastName(), $responseContent );
84
		$this->assertContains( 'initialFormValues.companyName: ' . $personName->getCompanyName(), $responseContent );
85
		$this->assertContains( 'initialFormValues.street: ' . $physicalAddress->getStreetAddress(), $responseContent );
86
		$this->assertContains( 'initialFormValues.postcode: ' . $physicalAddress->getPostalCode(), $responseContent );
87
		$this->assertContains( 'initialFormValues.city: ' . $physicalAddress->getCity(), $responseContent );
88
		$this->assertContains( 'initialFormValues.country: ' . $physicalAddress->getCountryCode(), $responseContent );
89
		$this->assertContains( 'initialFormValues.email: ' . $donation->getDonor()->getEmailAddress(), $responseContent );
90
		$this->assertContains( 'initialFormValues.iban: ' . $bankData->getIban()->toString(), $responseContent );
91
		$this->assertContains( 'initialFormValues.bic: ' . $bankData->getBic(), $responseContent );
92
		$this->assertContains( 'initialFormValues.accountNumber: ' . $bankData->getAccount(), $responseContent );
93
		$this->assertContains( 'initialFormValues.bankCode: ' . $bankData->getBankCode(), $responseContent );
94
		$this->assertContains( 'initialFormValues.bankname: ' . $bankData->getBankName(), $responseContent );
95
	}
96
97
	public function testGivenAlternativeConfirmationPageConfig_alternativeContentIsDisplayed() {
98
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
99
			$factory->setDonationConfirmationPageSelector(
100
				new DonationConfirmationPageSelector( $this->newConfirmationPageConfig() )
101
			);
102
			$donation = $this->newStoredDonation( $factory );
103
104
			$responseContent = $this->retrieveDonationConfirmation( $client, $donation->getId() );
105
106
			$this->assertContains( 'Alternative content', $responseContent );
107
			$this->assertContains( 'Template Name: DonationConfirmationAlternative.twig', $responseContent );
108
			$this->assertContains( 'Template Campaign: example', $responseContent );
109
		} );
110
	}
111
112
	public function testGivenAnonymousDonation_confirmationPageReflectsThat() {
113
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
114
			$donation = $this->newBookedAnonymousPayPalDonation( $factory );
115
116
			$responseContent = $this->retrieveDonationConfirmation( $client, $donation->getId() );
117
118
			$this->assertContains( 'Anonym', $responseContent );
119
		} );
120
	}
121
122
	public function testGivenAnonymousDonation_confirmationPageShowsStatusText() {
123
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
124
			$donation = $this->newBookedAnonymousPayPalDonation( $factory );
125
126
			$responseContent = $this->retrieveDonationConfirmation( $client, $donation->getId() );
127
128
			$this->assertContains( 'Ihre Spende per PayPal wurde gebucht', $responseContent );
129
		} );
130
	}
131
132
	private function retrieveDonationConfirmation( Client $client, int $donationId ) {
133
		$client->request(
134
			'GET',
135
			'show-donation-confirmation',
136
			[
137
				'id' => $donationId,
138
				'accessToken' => self::CORRECT_ACCESS_TOKEN
139
			]
140
		);
141
142
		return $client->getResponse()->getContent();
143
	}
144
145
	private function newStoredDonation( FunFunFactory $factory ): Donation {
146
		$factory->setTokenGenerator( new FixedTokenGenerator(
147
			self::CORRECT_ACCESS_TOKEN
148
		) );
149
150
		$donation = ValidDonation::newDirectDebitDonation();
151
152
		$factory->getDonationRepository()->storeDonation( $donation );
153
154
		return $donation;
155
	}
156
157
	private function newBookedAnonymousPayPalDonation( FunFunFactory $factory ): Donation {
158
		$factory->setTokenGenerator( new FixedTokenGenerator( self::CORRECT_ACCESS_TOKEN ) );
159
		$donation = ValidDonation::newBookedAnonymousPayPalDonation();
160
		$factory->getDonationRepository()->storeDonation( $donation );
161
162
		return $donation;
163
	}
164
165
	private function assertDonationDataInResponse( Donation $donation, string $responseContent ) {
166
		$donor = $donation->getDonor();
167
		$personName = $donor->getName();
168
		$physicalAddress = $donor->getPhysicalAddress();
169
		/** @var DirectDebitPayment $paymentMethod */
170
		$paymentMethod = $donation->getPaymentMethod();
171
172
		$this->assertContains( 'donation.id: ' . $donation->getId(), $responseContent );
173
		$this->assertContains( 'donation.status: ' . $donation->getStatus(), $responseContent );
174
		$this->assertContains( 'donation.amount: ' . $donation->getAmount()->getEuroString(), $responseContent );
175
		$this->assertContains( 'donation.interval: ' . $donation->getPaymentIntervalInMonths(), $responseContent );
176
		$this->assertContains( 'donation.paymentType: ' . $donation->getPaymentType(), $responseContent );
177
		$this->assertContains( 'donation.optsIntoNewsletter: ' . $donation->getOptsIntoNewsletter(), $responseContent );
178
		$this->assertContains( 'donation.updateToken: ' . self::CORRECT_ACCESS_TOKEN, $responseContent );
179
180
		$this->assertContains( 'person.salutation: ' . $personName->getSalutation(), $responseContent );
181
		$this->assertContains( 'person.fullName: ' . $personName->getFullName(), $responseContent );
182
		$this->assertContains( 'person.firstName: ' . $personName->getFirstName(), $responseContent );
183
		$this->assertContains( 'person.lastName: ' . $personName->getLastName(), $responseContent );
184
		$this->assertContains( 'person.streetAddress: ' . $physicalAddress->getStreetAddress(), $responseContent );
185
		$this->assertContains( 'person.postalCode: ' . $physicalAddress->getPostalCode(), $responseContent );
186
		$this->assertContains( 'person.city: ' . $physicalAddress->getCity(), $responseContent );
187
		$this->assertContains( 'person.email: ' . $donor->getEmailAddress(), $responseContent );
188
189
		$this->assertContains( 'bankData.iban: ' . $paymentMethod->getBankData()->getIban()->toString(), $responseContent );
190
		$this->assertContains( 'bankData.bic: ' . $paymentMethod->getBankData()->getBic(), $responseContent );
191
		$this->assertContains( 'bankData.bankname: ' . $paymentMethod->getBankData()->getBankName(), $responseContent );
192
	}
193
194
	public function testGivenWrongToken_accessIsDenied() {
195
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
196
			$factory->setDonationConfirmationPageSelector(
197
				new DonationConfirmationPageSelector( $this->newEmptyConfirmationPageConfig() )
198
			);
199
			$donation = $this->newStoredDonation( $factory );
200
201
			$client->request(
202
				'GET',
203
				'show-donation-confirmation',
204
				[
205
					'donationId' => $donation->getId(),
206
					'accessToken' => 'WrongAccessToken'
207
				]
208
			);
209
210
			$this->assertDonationIsNotShown( $donation, $client->getResponse()->getContent() );
211
		} );
212
	}
213
214
	private function assertDonationIsNotShown( Donation $donation, string $responseContent ) {
215
		$this->assertNotContains( $donation->getDonor()->getName()->getFirstName(), $responseContent );
216
		$this->assertNotContains( $donation->getDonor()->getName()->getLastName(), $responseContent );
217
218
		$this->assertContains( self::ACCESS_DENIED_TEXT, $responseContent );
219
	}
220
221
	public function testGivenWrongId_accessIsDenied() {
222
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
223
			$factory->setDonationConfirmationPageSelector(
224
				new DonationConfirmationPageSelector( $this->newEmptyConfirmationPageConfig() )
225
			);
226
227
			$donation = $this->newStoredDonation( $factory );
228
229
			$responseContent = $this->retrieveDonationConfirmation( $client, $donation->getId() + 1 );
230
231
			$this->assertDonationIsNotShown( $donation, $responseContent );
232
		} );
233
	}
234
235
	public function testWhenNoDonation_accessIsDenied() {
236
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
237
			$factory->setDonationConfirmationPageSelector(
238
				new DonationConfirmationPageSelector( $this->newEmptyConfirmationPageConfig() )
239
			);
240
241
			$responseContent = $this->retrieveDonationConfirmation( $client, 1 );
242
			$this->assertContains( self::ACCESS_DENIED_TEXT, $responseContent );
243
		} );
244
	}
245
246
	private function newConfirmationPageConfig() {
247
		return [
248
			'default' => 'DonationConfirmation.twig',
249
			'campaigns' => [
250
				[
251
					'code' => 'example',
252
					'active' => true,
253
					'startDate' => '1970-01-01 00:00:00',
254
					'endDate' => '2038-12-31 23:59:59',
255
					'templates' => [ 'DonationConfirmationAlternative.twig' ]
256
				]
257
			]
258
		];
259
	}
260
261
	private function newEmptyConfirmationPageConfig() {
262
		return [
263
			'default' => 'DonationConfirmation.twig',
264
			'campaigns' => [
265
				[
266
					'code' => 'example',
267
					'active' => false,
268
					'startDate' => '1970-01-01 00:00:00',
269
					'endDate' => '1970-12-31 23:59:59',
270
					'templates' => []
271
				]
272
			]
273
		];
274
	}
275
276
	public function testWhenDonationTimestampCookieIsSet_itIsNotOverwritten() {
277
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
278
			$donation = $this->newStoredDonation( $factory );
279
280
			$client->getCookieJar()->set(
281
				new Cookie( ShowDonationConfirmationHandler::SUBMISSION_COOKIE_NAME, 'some value' )
282
			);
283
			$client->request(
284
				'GET',
285
				'show-donation-confirmation',
286
				[
287
					'id' => $donation->getId(),
288
					'accessToken' => self::CORRECT_ACCESS_TOKEN
289
				]
290
			);
291
292
			$this->assertSame(
293
				'some value',
294
				$client->getCookieJar()->get( ShowDonationConfirmationHandler::SUBMISSION_COOKIE_NAME )->getValue()
295
			);
296
		} );
297
	}
298
299
}
300