Completed
Pull Request — master (#975)
by wiese
61:13
created

testDonationReceiptOptOut_persistedInDonation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Tests\EdgeToEdge\Routes;
6
7
use Silex\Application;
8
use Symfony\Component\BrowserKit\Cookie;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\HttpKernel\Client;
12
use WMDE\Fundraising\Entities\Donation;
13
use WMDE\Fundraising\Frontend\App\RouteHandlers\ShowDonationConfirmationHandler;
14
use WMDE\Fundraising\Frontend\Factories\FunFunFactory;
15
use WMDE\Fundraising\Frontend\Infrastructure\PageViewTracker;
16
use WMDE\Fundraising\Frontend\Tests\EdgeToEdge\WebRouteTestCase;
17
use WMDE\Fundraising\Frontend\Tests\Fixtures\FixedTokenGenerator;
18
use WMDE\Fundraising\Frontend\PaymentContext\DataAccess\Sofort\Transfer\Client as SofortClient;
19
use WMDE\Fundraising\Frontend\PaymentContext\DataAccess\Sofort\Transfer\Response as SofortResponse;
20
21
/**
22
 * @licence GNU GPL v2+
23
 * @author Kai Nissen < [email protected] >
24
 * @author Gabriel Birke < [email protected] >
25
 *
26
 * @requires extension konto_check
27
 */
28
class AddDonationRouteTest extends WebRouteTestCase {
29
30
	const SOME_TOKEN = 'SomeToken';
31
32
	private const ADD_DONATION_PATH = '/donation/add';
33
34
	public function testGivenValidRequest_donationGetsPersisted(): void {
35
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
36
37
			$client->setServerParameter( 'HTTP_REFERER', 'https://en.wikipedia.org/wiki/Karla_Kennichnich' );
38
			$client->followRedirects( false );
39
40
			$client->request(
41
				'POST',
42
				'/donation/add',
43
				$this->newValidFormInput()
44
			);
45
46
			$this->assertIsExpectedDonation( $this->getDonationFromDatabase( $factory ) );
47
		} );
48
	}
49
50
	public function testWhenDonationGetsPersisted_timestampIsStoredInCookie(): void {
51
		$client = $this->createClient();
52
		$client->followRedirects( true );
53
		$client->request(
54
			'POST',
55
			'/donation/add',
56
			$this->newValidFormInput()
57
		);
58
59
		$cookie = $client->getCookieJar()->get( 'donation_timestamp' );
60
		$this->assertNotNull( $cookie );
61
		$donationTimestamp = new \DateTime( $cookie->getValue() );
62
		$this->assertEquals( time(), $donationTimestamp->getTimestamp(), 'Timestamp should be not more than 5 seconds old', 5.0 );
63
	}
64
65
	public function testWhenMultipleDonationFormSubmissions_requestGetsRejected(): void {
66
		$client = $this->createClient();
67
		$client->getCookieJar()->set( new Cookie( 'donation_timestamp', $this->getPastTimestamp() ) );
68
69
		$client->request(
70
			'POST',
71
			'/donation/add',
72
			$this->newValidFormInput()
73
		);
74
75
		$this->assertContains( 'donation_rejected_limit', $client->getResponse()->getContent() );
76
	}
77
78
	public function testWhenMultipleDonationsInAccordanceToTimeLimit_requestIsNotRejected(): void {
79
		$client = $this->createClient();
80
		$client->getCookieJar()->set(
81
			new Cookie(
82
				'donation_timestamp',
83
				$this->getPastTimestamp( 'PT35M' )
84
			)
85
		);
86
87
		$client->request(
88
			'POST',
89
			'/donation/add',
90
			$this->newValidFormInput()
91
		);
92
93
		$this->assertNotContains( 'donation_rejected_limit', $client->getResponse()->getContent() );
94
	}
95
96
	private function getPastTimestamp( string $interval = 'PT10S' ): string {
97
		return ( new \DateTime() )->sub( new \DateInterval( $interval ) )->format( 'Y-m-d H:i:s' );
98
	}
99
100
	private function newValidFormInput(): array {
101
		return [
102
			'betrag' => '5,51',
103
			'zahlweise' => 'BEZ',
104
			'periode' => 0,
105
			'iban' => 'DE12500105170648489890',
106
			'bic' => 'INGDDEFFXXX',
107
			'konto' => '0648489890',
108
			'blz' => '50010517',
109
			'bankname' => 'ING-DiBa',
110
			'addressType' => 'person',
111
			'salutation' => 'Frau',
112
			'title' => 'Prof. Dr.',
113
			'company' => '',
114
			'firstName' => 'Karla',
115
			'lastName' => 'Kennichnich',
116
			'street' => 'Lehmgasse 12',
117
			'postcode' => '12345',
118
			'city' => 'Einort',
119
			'country' => 'DE',
120
			'email' => '[email protected]',
121
			'info' => '1',
122
			'piwik_campaign' => 'test',
123
			'piwik_kwd' => 'gelb',
124
			'impCount' => '3',
125
			'bImpCount' => '1',
126
			'layout' => 'Default',
127
			'color' => 'blue',
128
			'skin' => 'default',
129
		];
130
	}
131
132
	private function assertIsExpectedDonation( Donation $donation ): void {
133
		$data = $donation->getDecodedData();
134
		$this->assertSame( '5.51', $donation->getAmount() );
135
		$this->assertSame( 'BEZ', $donation->getPaymentType() );
136
		$this->assertSame( 0, $donation->getPaymentIntervalInMonths() );
137
		$this->assertSame( 'DE12500105170648489890', $data['iban'] );
138
		$this->assertSame( 'INGDDEFFXXX', $data['bic'] );
139
		$this->assertSame( '0648489890', $data['konto'] );
140
		$this->assertSame( '50010517', $data['blz'] );
141
		$this->assertSame( 'ING-DiBa', $data['bankname'] );
142
		$this->assertSame( 'person', $data['adresstyp'] );
143
		$this->assertSame( 'Frau', $data['anrede'] );
144
		$this->assertSame( 'Prof. Dr.', $data['titel'] );
145
		$this->assertSame( '', $data['firma'] );
146
		$this->assertSame( 'Karla', $data['vorname'] );
147
		$this->assertSame( 'Kennichnich', $data['nachname'] );
148
		$this->assertSame( 'Prof. Dr. Karla Kennichnich', $donation->getDonorFullName() );
149
		$this->assertSame( 'Lehmgasse 12', $data['strasse'] );
150
		$this->assertSame( '12345', $data['plz'] );
151
		$this->assertSame( 'Einort', $data['ort'] );
152
		$this->assertSame( 'Einort', $donation->getDonorCity() );
153
		$this->assertSame( 'DE', $data['country'] );
154
		$this->assertSame( '[email protected]', $data['email'] );
155
		$this->assertSame( '[email protected]', $donation->getDonorEmail() );
156
		$this->assertSame( 'test/gelb', $data['tracking'] );
157
		$this->assertSame( 3, $data['impCount'] );
158
		$this->assertSame( 1, $data['bImpCount'] );
159
		$this->assertSame( '', $data['layout'] );
160
		$this->assertSame( '', $data['color'] );
161
		$this->assertSame( '', $data['skin'] );
162
		$this->assertSame( 'en.wikipedia.org', $data['source'] );
163
		$this->assertSame( 'N', $donation->getStatus() );
164
		$this->assertTrue( $donation->getDonorOptsIntoNewsletter() );
165
		$this->assertTrue( $donation->getDonationReceipt() );
166
	}
167
168
	public function testGivenValidRequest_confirmationPageContainsEnteredData(): void {
169
		$client = $this->createClient();
170
		$client->request(
171
			'POST',
172
			'/donation/add',
173
			$this->newValidFormInput()
174
		);
175
		$client->followRedirect();
176
177
		$response = $client->getResponse()->getContent();
178
179
		$this->assertContains( '5,51 €', $response );
180
		$this->assertContains( 'donation.interval: 0', $response );
181
		$this->assertContains( 'DE12500105170648489890', $response );
182
		$this->assertContains( 'INGDDEFFXXX', $response );
183
		$this->assertContains( 'ING-DiBa', $response );
184
		$this->assertContains( 'Prof. Dr. Karla Kennichnich', $response );
185
		$this->assertContains( 'Lehmgasse 12', $response );
186
		$this->assertContains( '<span id="confirm-postcode">12345</span> <span id="confirm-city">Einort</span>', $response );
187
		$this->assertContains( '[email protected]', $response );
188
		$this->assertContains( 'send-info', $response );
189
	}
190
191
	public function testGivenValidBankTransferRequest_donationGetsPersisted(): void {
192
		/**
193
		 * @var FunFunFactory
194
		 */
195
		$factory = null;
0 ignored issues
show
Unused Code introduced by
$factory is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
196
197
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
198
199
			$client->setServerParameter( 'HTTP_REFERER', 'https://en.wikipedia.org/wiki/Karla_Kennichnich' );
200
			$client->followRedirects( false );
201
202
			$client->request(
203
				'POST',
204
				'/donation/add',
205
				$this->newValidBankTransferInput()
206
			);
207
208
			$donation = $this->getDonationFromDatabase( $factory );
209
210
			$data = $donation->getDecodedData();
211
			$this->assertSame( '12.34', $donation->getAmount() );
212
			$this->assertSame( 'UEB', $donation->getPaymentType() );
213
			$this->assertSame( 0, $donation->getPaymentIntervalInMonths() );
214
			$this->assertSame( 'person', $data['adresstyp'] );
215
			$this->assertSame( 'Frau', $data['anrede'] );
216
			$this->assertSame( 'Prof. Dr.', $data['titel'] );
217
			$this->assertSame( '', $data['firma'] );
218
			$this->assertSame( 'Karla', $data['vorname'] );
219
			$this->assertSame( 'Kennichnich', $data['nachname'] );
220
			$this->assertSame( 'Prof. Dr. Karla Kennichnich', $donation->getDonorFullName() );
221
			$this->assertSame( 'Lehmgasse 12', $data['strasse'] );
222
			$this->assertSame( '12345', $data['plz'] );
223
			$this->assertSame( 'Einort', $data['ort'] );
224
			$this->assertSame( 'Einort', $donation->getDonorCity() );
225
			$this->assertSame( 'DE', $data['country'] );
226
			$this->assertSame( '[email protected]', $data['email'] );
227
			$this->assertSame( '[email protected]', $donation->getDonorEmail() );
228
			$this->assertSame( 'test/gelb', $data['tracking'] );
229
			$this->assertSame( 3, $data['impCount'] );
230
			$this->assertSame( 1, $data['bImpCount'] );
231
			$this->assertSame( '', $data['layout'] );
232
			$this->assertSame( '', $data['color'] );
233
			$this->assertSame( '', $data['skin'] );
234
			$this->assertSame( 'en.wikipedia.org', $data['source'] );
235
			$this->assertSame( true, $donation->getDonorOptsIntoNewsletter() );
236
237
			$this->assertSame( 'Z', $donation->getStatus() );
238
			$this->assertRegExp( '/W-Q-[A-Z]{6}-[A-Z]/', $donation->getBankTransferCode() );
239
		} );
240
	}
241
242
	private function newValidBankTransferInput(): array {
243
		return [
244
			'betrag' => '12,34',
245
			'zahlweise' => 'UEB',
246
			'periode' => 0,
247
			'addressType' => 'person',
248
			'salutation' => 'Frau',
249
			'title' => 'Prof. Dr.',
250
			'company' => '',
251
			'firstName' => 'Karla',
252
			'lastName' => 'Kennichnich',
253
			'street' => 'Lehmgasse 12',
254
			'postcode' => '12345',
255
			'city' => 'Einort',
256
			'country' => 'DE',
257
			'email' => '[email protected]',
258
			'info' => '1',
259
			'piwik_campaign' => 'test',
260
			'piwik_kwd' => 'gelb',
261
			'impCount' => '3',
262
			'bImpCount' => '1',
263
			'layout' => 'Default',
264
			'color' => 'blue',
265
			'skin' => 'default',
266
		];
267
	}
268
269
	public function testGivenComplementableBankData_donationStillGetsPersisted(): void {
270
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
271
272
			$client->setServerParameter( 'HTTP_REFERER', 'https://en.wikipedia.org/wiki/Karla_Kennichnich' );
273
			$client->followRedirects( false );
274
275
			$client->request(
276
				'POST',
277
				'/donation/add',
278
				$this->newComplementableFormInput()
279
			);
280
281
			$donation = $this->getDonationFromDatabase( $factory );
282
283
			$data = $donation->getDecodedData();
284
			$this->assertSame( 'DE12500105170648489890', $data['iban'] );
285
			$this->assertSame( 'INGDDEFFXXX', $data['bic'] );
286
			$this->assertSame( '0648489890', $data['konto'] );
287
			$this->assertSame( '50010517', $data['blz'] );
288
			$this->assertSame( 'ING-DiBa', $data['bankname'] );
289
		} );
290
	}
291
292
	private function newComplementableFormInput(): array {
293
		return [
294
			'betrag' => '5,51',
295
			'zahlweise' => 'BEZ',
296
			'periode' => 0,
297
			'iban' => 'DE12500105170648489890',
298
			'addressType' => 'person',
299
			'salutation' => 'Frau',
300
			'title' => 'Prof. Dr.',
301
			'firstName' => 'Karla',
302
			'lastName' => 'Kennichnich',
303
			'street' => 'Lehmgasse 12',
304
			'postcode' => '12345',
305
			'city' => 'Einort',
306
			'country' => 'DE',
307
			'email' => '[email protected]',
308
		];
309
	}
310
311
	private function getDonationFromDatabase( FunFunFactory $factory ): Donation {
312
		$donationRepo = $factory->getEntityManager()->getRepository( Donation::class );
313
		$donation = $donationRepo->find( 1 );
314
		$this->assertInstanceOf( Donation::class, $donation );
315
		return $donation;
316
	}
317
318
	public function testGivenValidPayPalData_redirectsToPayPal(): void {
319
		$client = $this->createClient();
320
		$client->followRedirects( false );
321
322
		$client->request(
323
			'POST',
324
			'/donation/add',
325
			$this->newValidPayPalInput()
326
		);
327
328
		$response = $client->getResponse();
329
		$this->assertSame( Response::HTTP_FOUND, $response->getStatusCode() );
330
		$this->assertContains( 'sandbox.paypal.com', $response->getContent() );
331
	}
332
333
	public function testWhenRedirectingToPayPal_translatedItemNameIsPassed(): void {
334
		$client = $this->createClient();
335
		$client->followRedirects( false );
336
337
		$client->request(
338
			'POST',
339
			'/donation/add',
340
			$this->newValidPayPalInput()
341
		);
342
343
		$response = $client->getResponse();
344
		$this->assertSame( Response::HTTP_FOUND, $response->getStatusCode() );
345
		$this->assertContains( 'item_name=item_name_donation', $response->getContent() );
346
	}
347
348
	private function newValidPayPalInput(): array {
349
		return [
350
			'betrag' => '12,34',
351
			'zahlweise' => 'PPL',
352
			'periode' => 3,
353
			'addressType' => 'anonym',
354
		];
355
	}
356
357
	public function testGivenValidCreditCardData_redirectsToPaymentProvider(): void {
358
		$client = $this->createClient();
359
		$client->request(
360
			'POST',
361
			'/donation/add',
362
			$this->newValidCreditCardInput()
363
		);
364
365
		$response = $client->getResponse();
366
		$this->assertSame( Response::HTTP_FOUND, $response->getStatusCode() );
367
		$this->assertTrue( $response->isRedirect() );
368
		$this->assertContains( 'amount=1234', $response->headers->get( 'Location' ) );
369
		$this->assertContains( 'thatother.paymentprovider.com', $response->headers->get( 'Location' ) );
370
	}
371
372
	public function testValidSofortInput_savesDonationAndRedirectsTo3rdPartyPage(): void {
373
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
374
			$response = new SofortResponse();
375
			$response->setPaymentUrl( 'https://bankingpin.please' );
376
377
			$sofortClient = $this->createMock( SofortClient::class );
378
			$sofortClient
379
				->method( 'get' )
380
				->willReturn( $response );
381
			$factory->setSofortClient( $sofortClient );
382
383
384
			$client->followRedirects( false );
385
			$client->request(
386
				'POST',
387
				'/donation/add',
388
				$this->newValidSofortInput()
389
			);
390
391
			$donation = $this->getDonationFromDatabase( $factory );
392
393
			$this->assertSame( 'Z', $donation->getStatus() );
394
			$this->assertRegExp( '/W-Q-[A-Z]{6}-[A-Z]/', $donation->getBankTransferCode() );
395
396
			$this->assertTrue( $client->getResponse()->isRedirect( 'https://bankingpin.please' ) );
397
		} );
398
	}
399
400
	private function newValidCreditCardInput(): array {
401
		return [
402
			'betrag' => '12,34',
403
			'zahlweise' => 'MCP',
404
			'periode' => 3,
405
			'addressType' => 'anonym',
406
		];
407
	}
408
409
	private function newValidSofortInput(): array {
410
		return [
411
			'betrag' => '100,00',
412
			'zahlweise' => 'SUB',
413
			'periode' => 0,
414
			'addressType' => 'anonym',
415
		];
416
	}
417
418
	public function testGivenInvalidRequest_formIsReloadedAndPrefilled(): void {
419
		$client = $this->createClient();
420
		$client->request(
421
			'POST',
422
			'/donation/add',
423
			$this->newInvalidFormInput()
424
		);
425
426
		$response = $client->getResponse()->getContent();
427
428
		$this->assertContains( 'Amount: 0,00', $response );
429
		$this->assertContains( 'Payment type: BEZ', $response );
430
		$this->assertContains( 'Interval: 3', $response );
431
		$this->assertContains( 'IBAN: DE12500105170648489890', $response );
432
		$this->assertContains( 'BIC: INGDDEFFXXX', $response );
433
		$this->assertContains( 'Bank name: ING-DiBa', $response );
434
		$this->assertContains( 'Address type: person', $response );
435
		$this->assertContains( 'Salutation: Frau', $response );
436
		$this->assertContains( 'Title: Prof. Dr.', $response );
437
		$this->assertContains( 'Company: ', $response );
438
		$this->assertContains( 'First name: Karla', $response );
439
		$this->assertContains( 'Last name: Kennichnich', $response );
440
		$this->assertContains( 'Street: Lehmgasse 12', $response );
441
		$this->assertContains( 'Postal code: 12345', $response );
442
		$this->assertContains( 'City: Einort', $response );
443
		$this->assertContains( 'Country code: DE', $response );
444
		$this->assertContains( 'Email address: [email protected]', $response );
445
	}
446
447
	public function testGivenInvalidRequest_formStillContainsBannerTrackingData(): void {
448
		$client = $this->createClient();
449
		$client->request(
450
			'POST',
451
			'/donation/add',
452
			[
453
				'impCount' => 12,
454
				'bImpCount' => 3
455
			]
456
		);
457
458
		$response = $client->getResponse()->getContent();
459
460
		$this->assertContains( 'Impression Count: 12', $response );
461
		$this->assertContains( 'Banner Impression Count: 3', $response );
462
	}
463
464
	public function testGivenNegativeDonationAmount_formIsReloadedAndPrefilledWithZero(): void {
465
		$client = $this->createClient();
466
467
		$formValues = $this->newInvalidFormInput();
468
		$formValues['betrag'] = '-5,00';
469
470
		$client->request(
471
			'POST',
472
			'/donation/add',
473
			$formValues
474
		);
475
476
		$response = $client->getResponse()->getContent();
477
478
		$this->assertContains( 'Amount: 0,00', $response );
479
	}
480
481
	private function newInvalidFormInput(): array {
482
		return [
483
			'betrag' => '0',
484
			'zahlweise' => 'BEZ',
485
			'periode' => 3,
486
			'iban' => 'DE12500105170648489890',
487
			'bic' => 'INGDDEFFXXX',
488
			'konto' => '0648489890',
489
			'blz' => '50010517',
490
			'bankname' => 'ING-DiBa',
491
			'addressType' => 'person',
492
			'salutation' => 'Frau',
493
			'title' => 'Prof. Dr.',
494
			'company' => '',
495
			'firstName' => 'Karla',
496
			'lastName' => 'Kennichnich',
497
			'street' => 'Lehmgasse 12',
498
			'postcode' => '12345',
499
			'city' => 'Einort',
500
			'country' => 'DE',
501
			'email' => '[email protected]',
502
			'info' => '1',
503
			'piwik_campaign' => 'test',
504
			'piwik_kwd' => 'gelb',
505
			'impCount' => '3',
506
			'bImpCount' => '1',
507
			'layout' => 'Default',
508
			'color' => 'blue',
509
			'skin' => 'default',
510
		];
511
	}
512
513
	public function testGivenInvalidAnonymousRequest_formIsReloadedAndPrefilled(): void {
514
		$client = $this->createClient();
515
		$client->request(
516
			'POST',
517
			'/donation/add',
518
			$this->newAnonymousFormInput()
519
		);
520
521
		$response = $client->getResponse()->getContent();
522
523
		$this->assertContains( 'Amount: 0', $response );
524
		$this->assertContains( 'Payment type: UEB', $response );
525
		$this->assertContains( 'Interval: 1', $response );
526
		$this->assertContains( 'Value of field "amount" violates rule: Amount too low', $response );
527
	}
528
529
	private function newAnonymousFormInput(): array {
530
		return [
531
			'betrag' => '0',
532
			'zahlweise' => 'UEB',
533
			'periode' => 1,
534
			'addressType' => 'anonym'
535
		];
536
	}
537
538
	public function testGivenValidRequest_tokensAreReturned(): void {
539
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
540
			$factory->setTokenGenerator( new FixedTokenGenerator( self::SOME_TOKEN ) );
541
542
			$client->setServerParameter( 'HTTP_REFERER', 'https://en.wikipedia.org/wiki/Karla_Kennichnich' );
543
			$client->followRedirects( false );
544
545
			$client->request(
546
				'POST',
547
				'/donation/add',
548
				$this->newValidCreditCardInput()
549
			);
550
551
			$response = $client->getResponse()->getContent();
552
553
			$this->assertContains( self::SOME_TOKEN, $response );
554
		} );
555
	}
556
557
	public function testGivenValidRequest_clientIsRedirected(): void {
558
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
559
			$factory->setTokenGenerator( new FixedTokenGenerator( self::SOME_TOKEN ) );
560
			$client->followRedirects( false );
561
562
			$client->request(
563
				'POST',
564
				'/donation/add',
565
				$this->newValidFormInput()
566
			);
567
568
			$this->assertTrue( $client->getResponse()->isRedirect() );
569
		} );
570
	}
571
572
	public function testWhenTrackingCookieExists_valueIsPersisted(): void {
573
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
574
			$client->getCookieJar()->set( new Cookie( 'spenden_tracking', 'test/blue' ) );
575
576
			$client->request(
577
				'POST',
578
				'/donation/add',
579
				$this->newComplementableFormInput()
580
			);
581
582
			$donation = $this->getDonationFromDatabase( $factory );
583
			$data = $donation->getDecodedData();
584
585
			$this->assertSame( 'test/blue', $data['tracking'] );
586
		} );
587
	}
588
589
	public function testWhenTrackableInputDataIsSubmitted_theyAreStoredInSession(): void {
590
		$this->createAppEnvironment( [], function ( Client $client, FunFunFactory $factory, Application $app ): void {
591
592
			$client->request(
593
				'GET',
594
				'/',
595
				[
596
					'betrag' => '5,00',
597
					'periode' => 3,
598
					'zahlweise' => 'BEZ'
599
				]
600
			);
601
602
			$piwikTracking = $app['session']->get( 'piwikTracking' );
603
			$this->assertSame( 'BEZ', $piwikTracking['paymentType'] );
604
			$this->assertSame( 3, $piwikTracking['paymentInterval'] );
605
			$this->assertSame( '5,00', $piwikTracking['paymentAmount'] );
606
		} );
607
	}
608
609
	public function testWhenTolstojNovelIsPassed_isIsNotStoredInSession(): void {
610
		$this->createAppEnvironment( [], function ( Client $client, FunFunFactory $factory, Application $app ): void {
611
612
			$client->request(
613
				'GET',
614
				'/',
615
				[
616
					'betrag' => '5,00',
617
					'periode' => 3,
618
					'zahlweise' => 'Eh bien, mon prince. Gênes et Lucques ne sont plus que des apanages, des поместья, de la ' .
619
						'famille Buonaparte. Non, je vous préviens que si vous ne me dites pas que nous avons la guerre, si ' .
620
						'vous vous permettez encore de pallier toutes les infamies, toutes les atrocités de cet Antichrist ' .
621
						'(ma parole, j’y crois) — je ne vous connais plus, vous n’êtes plus mon ami, vous n’êtes plus мой ' .
622
						'верный раб, comme vous dites. Ну, здравствуйте,' .
623
						'здравствуйте. Je vois que je vous fais peur, ' .
624
						'садитесь и рассказывайте.'
625
				]
626
			);
627
628
			$piwikTracking = $app['session']->get( 'piwikTracking' );
629
			$this->assertArrayNotHasKey( 'paymentType', $piwikTracking );
630
			$this->assertSame( 3, $piwikTracking['paymentInterval'] );
631
			$this->assertSame( '5,00', $piwikTracking['paymentAmount'] );
632
		} );
633
	}
634
635
	public function testWhenParameterIsOmitted_itIsNotStoredInSession(): void {
636
		$this->createAppEnvironment( [], function ( Client $client, FunFunFactory $factory, Application $app ): void {
637
638
			$client->request(
639
				'GET',
640
				'/',
641
				[
642
					'betrag' => '5,00',
643
					'zahlweise' => 'BEZ'
644
				]
645
			);
646
647
			$piwikTracking = $app['session']->get( 'piwikTracking' );
648
			$this->assertSame( 'BEZ', $piwikTracking['paymentType'] );
649
			$this->assertSame( '5,00', $piwikTracking['paymentAmount'] );
650
			$this->assertArrayNotHasKey( 'paymentInterval', $piwikTracking );
651
		} );
652
	}
653
654
	public function testWhenInitiallyIntendedPaymentOptionsDifferFromActual_itIsReflectedInPiwikTrackingEvents(): void {
655
		$client = $this->createClient( [] );
656
		$client->request(
657
			'GET',
658
			'/',
659
			[
660
				'betrag' => '5.00',
661
				'zahlweise' => 'BEZ',
662
				'periode' => 12
663
			]
664
		);
665
666
		$client->request(
667
			'POST',
668
			'/donation/add',
669
			[
670
				'addressType' => 'anonym',
671
				'betrag' => '12,34',
672
				'periode' => '0',
673
				'zahlweise' => 'UEB'
674
			]
675
		);
676
		$client->followRedirect();
677
678
		$responseContent = $client->getResponse()->getContent();
679
		$this->assertContains( 'BEZ/UEB', $responseContent );
680
		$this->assertContains( '5.00/12.34', $responseContent );
681
		$this->assertContains( '12/0', $responseContent );
682
	}
683
684
	public function testWhenMobileTrackingIsRequested_piwikTrackerIsCalledForPaypalPayment(): void {
685
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
686
			$factory->setNullMessenger();
687
			$client->followRedirects( false );
688
689
			$tracker = $this->getMockBuilder( PageViewTracker::class )->disableOriginalConstructor()->getMock();
690
			$tracker->expects( $this->once() )
691
				->method( 'trackPaypalRedirection' )
692
				->with( 'test', 'gelb', '10.1.2.3' );
693
			$factory->setPageViewTracker( $tracker );
694
695
			$client->request(
696
				'POST',
697
				'/donation/add',
698
				$this->newValidMobilePayPalInput(),
699
				[],
700
				['REMOTE_ADDR' => '10.1.2.3']
701
			);
702
703
			$client->getResponse();
704
		} );
705
	}
706
707
	private function newValidMobilePayPalInput(): array {
708
		return [
709
			'betrag' => '12,34',
710
			'zahlweise' => 'PPL',
711
			'periode' => 3,
712
			'addressType' => 'anonym',
713
			'piwik_campaign' => 'test',
714
			'piwik_kwd' => 'gelb',
715
			'mbt' => '1' // mobile tracking param
716
		];
717
	}
718
719
	public function testWhenMobileTrackingIsRequested_piwikTrackerIsNotCalledForNonPaypalPayment(): void {
720
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
721
			$factory->setNullMessenger();
722
			$client->followRedirects( false );
723
724
			$tracker = $this->getMockBuilder( PageViewTracker::class )->disableOriginalConstructor()->getMock();
725
			$tracker->expects( $this->never() )
726
				->method( 'trackPaypalRedirection' );
727
			$factory->setPageViewTracker( $tracker );
728
729
			$client->request(
730
				'POST',
731
				'/donation/add',
732
				array_merge(
733
					$this->newValidCreditCardInput(),
734
					['mbt' => '1']
735
				)
736
			);
737
738
			$client->getResponse();
739
		} );
740
	}
741
742
	public function testGivenCommasInStreetInput_donationGetsPersisted(): void {
743
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
744
745
			$client->setServerParameter( 'HTTP_REFERER', 'https://en.wikipedia.org/wiki/Karla_Kennichnich' );
746
			$client->followRedirects( false );
747
748
			$formInput = $this->newValidFormInput();
749
			$formInput['street'] = ',Lehmgasse, 12,';
750
751
			$client->request(
752
				'POST',
753
				'/donation/add',
754
				$formInput
755
			);
756
757
			$this->assertIsExpectedDonation( $this->getDonationFromDatabase( $factory ) );
758
		} );
759
	}
760
761
	public function testGivenSufficientForeignBankData_donationGetsPersisted(): void {
762
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
763
			$formInput = $this->newValidFormInput();
764
			$formInput['iban'] = 'AT022050302101023600';
765
			$formInput['bic'] = 'SPIHAT22XXX';
766
			$formInput['konto'] = '';
767
			$formInput['blz'] = '';
768
			$formInput['bankname'] = '';
769
			$client->request(
770
				'POST',
771
				'/donation/add',
772
				$formInput
773
			);
774
775
			$donation = $this->getDonationFromDatabase( $factory );
776
			$data = $donation->getDecodedData();
777
778
			$this->assertSame( 'AT022050302101023600', $data['iban'] );
779
			$this->assertSame( 'SPIHAT22XXX', $data['bic'] );
780
			$this->assertSame( '', $data['konto'] );
781
			$this->assertSame( '', $data['blz'] );
782
			$this->assertSame( '', $data['bankname'] );
783
		} );
784
	}
785
786
	public function testCookieFlagsSecureAndHttpOnlyAreSet(): void {
787
		$client = new Client(
788
			$this->createSilexApplication(),
789
			[ 'HTTPS' => true ]
790
		);
791
		$client->followRedirects( true );
792
793
		$client->request(
794
			'POST',
795
			'/donation/add',
796
			$this->newValidFormInput()
797
		);
798
799
		$cookieJar = $client->getCookieJar();
800
		$cookieJar->updateFromResponse( $client->getInternalResponse() );
0 ignored issues
show
Bug introduced by
It seems like $client->getInternalResponse() can be null; however, updateFromResponse() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
801
		$cookie = $cookieJar->get( ShowDonationConfirmationHandler::SUBMISSION_COOKIE_NAME );
802
		$this->assertTrue( $cookie->isSecure() );
803
		$this->assertTrue( $cookie->isHttpOnly() );
804
	}
805
806
	public function testAllPaymentTypesAreOffered(): void {
807
		$client = $this->createClient( [ 'skin' => [ 'default' => '10h16' ] ] );
808
		$client->request(
809
			'GET',
810
			'/donation/new'
811
		);
812
		$crawler = $client->getCrawler();
813
814
		$this->assertSame( 1, $crawler->filter( '#donation-payment input[name="zahlweise"][value="BEZ"]' )->count() );
815
		$this->assertSame( 1, $crawler->filter( '#donation-payment input[name="zahlweise"][value="UEB"]' )->count() );
816
		$this->assertSame( 1, $crawler->filter( '#donation-payment input[name="zahlweise"][value="MCP"]' )->count() );
817
		$this->assertSame( 1, $crawler->filter( '#donation-payment input[name="zahlweise"][value="PPL"]' )->count() );
818
		$this->assertSame( 1, $crawler->filter( '#donation-payment input[name="zahlweise"][value="SUB"]' )->count() );
819
	}
820
821
	public function testSofortPaymentTypeCanByDisabledViaQuery(): void {
822
		$client = $this->createClient( [ 'skin' => [ 'default' => '10h16' ] ] );
823
		$client->request(
824
			'GET',
825
			'/donation/new',
826
			[ 'pmt' => '0' ]
827
		);
828
		$crawler = $client->getCrawler();
829
830
		$this->assertSame( 0, $crawler->filter( '#donation-payment input[name="zahlweise"][value="SUB"]' )->count() );
831
	}
832
833
	public function testDonationReceiptOptOut_persistedInDonation(): void {
834
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
835
			$parameters = $this->newValidFormInput();
836
			$parameters['donationReceipt'] = '0';
837
838
			$client->request( Request::METHOD_POST, self::ADD_DONATION_PATH, $parameters );
839
840
			$this->assertFalse( $this->getDonationFromDatabase( $factory )->getDonationReceipt() );
841
		} );
842
	}
843
}
844