Completed
Pull Request — master (#971)
by wiese
94:17 queued 29:21
created

testAllPaymentTypesAreOffered()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
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\Response;
10
use Symfony\Component\HttpKernel\Client;
11
use WMDE\Fundraising\Entities\Donation;
12
use WMDE\Fundraising\Frontend\App\RouteHandlers\ShowDonationConfirmationHandler;
13
use WMDE\Fundraising\Frontend\Factories\FunFunFactory;
14
use WMDE\Fundraising\Frontend\Infrastructure\PageViewTracker;
15
use WMDE\Fundraising\Frontend\Tests\EdgeToEdge\WebRouteTestCase;
16
use WMDE\Fundraising\Frontend\Tests\Fixtures\FixedTokenGenerator;
17
use WMDE\Fundraising\Frontend\PaymentContext\DataAccess\Sofort\Transfer\Client as SofortClient;
18
use WMDE\Fundraising\Frontend\PaymentContext\DataAccess\Sofort\Transfer\Response as SofortResponse;
19
20
/**
21
 * @licence GNU GPL v2+
22
 * @author Kai Nissen < [email protected] >
23
 * @author Gabriel Birke < [email protected] >
24
 *
25
 * @requires extension konto_check
26
 */
27
class AddDonationRouteTest extends WebRouteTestCase {
28
29
	const SOME_TOKEN = 'SomeToken';
30
31
	public function testGivenValidRequest_donationGetsPersisted(): void {
32
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
33
34
			$client->setServerParameter( 'HTTP_REFERER', 'https://en.wikipedia.org/wiki/Karla_Kennichnich' );
35
			$client->followRedirects( false );
36
37
			$client->request(
38
				'POST',
39
				'/donation/add',
40
				$this->newValidFormInput()
41
			);
42
43
			$this->assertIsExpectedDonation( $this->getDonationFromDatabase( $factory ) );
44
		} );
45
	}
46
47
	public function testWhenDonationGetsPersisted_timestampIsStoredInCookie(): void {
48
		$client = $this->createClient();
49
		$client->followRedirects( true );
50
		$client->request(
51
			'POST',
52
			'/donation/add',
53
			$this->newValidFormInput()
54
		);
55
56
		$cookie = $client->getCookieJar()->get( 'donation_timestamp' );
57
		$this->assertNotNull( $cookie );
58
		$donationTimestamp = new \DateTime( $cookie->getValue() );
59
		$this->assertEquals( time(), $donationTimestamp->getTimestamp(), 'Timestamp should be not more than 5 seconds old', 5.0 );
60
	}
61
62
	public function testWhenMultipleDonationFormSubmissions_requestGetsRejected(): void {
63
		$client = $this->createClient();
64
		$client->getCookieJar()->set( new Cookie( 'donation_timestamp', $this->getPastTimestamp() ) );
65
66
		$client->request(
67
			'POST',
68
			'/donation/add',
69
			$this->newValidFormInput()
70
		);
71
72
		$this->assertContains( 'donation_rejected_limit', $client->getResponse()->getContent() );
73
	}
74
75
	public function testWhenMultipleDonationsInAccordanceToTimeLimit_requestIsNotRejected(): void {
76
		$client = $this->createClient();
77
		$client->getCookieJar()->set(
78
			new Cookie(
79
				'donation_timestamp',
80
				$this->getPastTimestamp( 'PT35M' )
81
			)
82
		);
83
84
		$client->request(
85
			'POST',
86
			'/donation/add',
87
			$this->newValidFormInput()
88
		);
89
90
		$this->assertNotContains( 'donation_rejected_limit', $client->getResponse()->getContent() );
91
	}
92
93
	private function getPastTimestamp( string $interval = 'PT10S' ): string {
94
		return ( new \DateTime() )->sub( new \DateInterval( $interval ) )->format( 'Y-m-d H:i:s' );
95
	}
96
97
	private function newValidFormInput(): array {
98
		return [
99
			'betrag' => '5,51',
100
			'zahlweise' => 'BEZ',
101
			'periode' => 0,
102
			'iban' => 'DE12500105170648489890',
103
			'bic' => 'INGDDEFFXXX',
104
			'konto' => '0648489890',
105
			'blz' => '50010517',
106
			'bankname' => 'ING-DiBa',
107
			'addressType' => 'person',
108
			'salutation' => 'Frau',
109
			'title' => 'Prof. Dr.',
110
			'company' => '',
111
			'firstName' => 'Karla',
112
			'lastName' => 'Kennichnich',
113
			'street' => 'Lehmgasse 12',
114
			'postcode' => '12345',
115
			'city' => 'Einort',
116
			'country' => 'DE',
117
			'email' => '[email protected]',
118
			'info' => '1',
119
			'piwik_campaign' => 'test',
120
			'piwik_kwd' => 'gelb',
121
			'impCount' => '3',
122
			'bImpCount' => '1',
123
			'layout' => 'Default',
124
			'color' => 'blue',
125
			'skin' => 'default',
126
		];
127
	}
128
129
	private function assertIsExpectedDonation( Donation $donation ): void {
130
		$data = $donation->getDecodedData();
131
		$this->assertSame( '5.51', $donation->getAmount() );
132
		$this->assertSame( 'BEZ', $donation->getPaymentType() );
133
		$this->assertSame( 0, $donation->getPaymentIntervalInMonths() );
134
		$this->assertSame( 'DE12500105170648489890', $data['iban'] );
135
		$this->assertSame( 'INGDDEFFXXX', $data['bic'] );
136
		$this->assertSame( '0648489890', $data['konto'] );
137
		$this->assertSame( '50010517', $data['blz'] );
138
		$this->assertSame( 'ING-DiBa', $data['bankname'] );
139
		$this->assertSame( 'person', $data['adresstyp'] );
140
		$this->assertSame( 'Frau', $data['anrede'] );
141
		$this->assertSame( 'Prof. Dr.', $data['titel'] );
142
		$this->assertSame( '', $data['firma'] );
143
		$this->assertSame( 'Karla', $data['vorname'] );
144
		$this->assertSame( 'Kennichnich', $data['nachname'] );
145
		$this->assertSame( 'Prof. Dr. Karla Kennichnich', $donation->getDonorFullName() );
146
		$this->assertSame( 'Lehmgasse 12', $data['strasse'] );
147
		$this->assertSame( '12345', $data['plz'] );
148
		$this->assertSame( 'Einort', $data['ort'] );
149
		$this->assertSame( 'Einort', $donation->getDonorCity() );
150
		$this->assertSame( 'DE', $data['country'] );
151
		$this->assertSame( '[email protected]', $data['email'] );
152
		$this->assertSame( '[email protected]', $donation->getDonorEmail() );
153
		$this->assertSame( 'test/gelb', $data['tracking'] );
154
		$this->assertSame( 3, $data['impCount'] );
155
		$this->assertSame( 1, $data['bImpCount'] );
156
		$this->assertSame( '', $data['layout'] );
157
		$this->assertSame( '', $data['color'] );
158
		$this->assertSame( '', $data['skin'] );
159
		$this->assertSame( 'en.wikipedia.org', $data['source'] );
160
		$this->assertSame( 'N', $donation->getStatus() );
161
		$this->assertTrue( $donation->getDonorOptsIntoNewsletter() );
162
	}
163
164
	public function testGivenValidRequest_confirmationPageContainsEnteredData(): void {
165
		$client = $this->createClient();
166
		$client->request(
167
			'POST',
168
			'/donation/add',
169
			$this->newValidFormInput()
170
		);
171
		$client->followRedirect();
172
173
		$response = $client->getResponse()->getContent();
174
175
		$this->assertContains( '5,51 €', $response );
176
		$this->assertContains( 'donation.interval: 0', $response );
177
		$this->assertContains( 'DE12500105170648489890', $response );
178
		$this->assertContains( 'INGDDEFFXXX', $response );
179
		$this->assertContains( 'ING-DiBa', $response );
180
		$this->assertContains( 'Prof. Dr. Karla Kennichnich', $response );
181
		$this->assertContains( 'Lehmgasse 12', $response );
182
		$this->assertContains( '<span id="confirm-postcode">12345</span> <span id="confirm-city">Einort</span>', $response );
183
		$this->assertContains( '[email protected]', $response );
184
		$this->assertContains( 'send-info', $response );
185
	}
186
187
	public function testGivenValidBankTransferRequest_donationGetsPersisted(): void {
188
		/**
189
		 * @var FunFunFactory
190
		 */
191
		$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...
192
193
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
194
195
			$client->setServerParameter( 'HTTP_REFERER', 'https://en.wikipedia.org/wiki/Karla_Kennichnich' );
196
			$client->followRedirects( false );
197
198
			$client->request(
199
				'POST',
200
				'/donation/add',
201
				$this->newValidBankTransferInput()
202
			);
203
204
			$donation = $this->getDonationFromDatabase( $factory );
205
206
			$data = $donation->getDecodedData();
207
			$this->assertSame( '12.34', $donation->getAmount() );
208
			$this->assertSame( 'UEB', $donation->getPaymentType() );
209
			$this->assertSame( 0, $donation->getPaymentIntervalInMonths() );
210
			$this->assertSame( 'person', $data['adresstyp'] );
211
			$this->assertSame( 'Frau', $data['anrede'] );
212
			$this->assertSame( 'Prof. Dr.', $data['titel'] );
213
			$this->assertSame( '', $data['firma'] );
214
			$this->assertSame( 'Karla', $data['vorname'] );
215
			$this->assertSame( 'Kennichnich', $data['nachname'] );
216
			$this->assertSame( 'Prof. Dr. Karla Kennichnich', $donation->getDonorFullName() );
217
			$this->assertSame( 'Lehmgasse 12', $data['strasse'] );
218
			$this->assertSame( '12345', $data['plz'] );
219
			$this->assertSame( 'Einort', $data['ort'] );
220
			$this->assertSame( 'Einort', $donation->getDonorCity() );
221
			$this->assertSame( 'DE', $data['country'] );
222
			$this->assertSame( '[email protected]', $data['email'] );
223
			$this->assertSame( '[email protected]', $donation->getDonorEmail() );
224
			$this->assertSame( 'test/gelb', $data['tracking'] );
225
			$this->assertSame( 3, $data['impCount'] );
226
			$this->assertSame( 1, $data['bImpCount'] );
227
			$this->assertSame( '', $data['layout'] );
228
			$this->assertSame( '', $data['color'] );
229
			$this->assertSame( '', $data['skin'] );
230
			$this->assertSame( 'en.wikipedia.org', $data['source'] );
231
			$this->assertSame( true, $donation->getDonorOptsIntoNewsletter() );
232
233
			$this->assertSame( 'Z', $donation->getStatus() );
234
			$this->assertRegExp( '/W-Q-[A-Z]{6}-[A-Z]/', $donation->getBankTransferCode() );
235
		} );
236
	}
237
238
	private function newValidBankTransferInput(): array {
239
		return [
240
			'betrag' => '12,34',
241
			'zahlweise' => 'UEB',
242
			'periode' => 0,
243
			'addressType' => 'person',
244
			'salutation' => 'Frau',
245
			'title' => 'Prof. Dr.',
246
			'company' => '',
247
			'firstName' => 'Karla',
248
			'lastName' => 'Kennichnich',
249
			'street' => 'Lehmgasse 12',
250
			'postcode' => '12345',
251
			'city' => 'Einort',
252
			'country' => 'DE',
253
			'email' => '[email protected]',
254
			'info' => '1',
255
			'piwik_campaign' => 'test',
256
			'piwik_kwd' => 'gelb',
257
			'impCount' => '3',
258
			'bImpCount' => '1',
259
			'layout' => 'Default',
260
			'color' => 'blue',
261
			'skin' => 'default',
262
		];
263
	}
264
265
	public function testGivenComplementableBankData_donationStillGetsPersisted(): void {
266
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
267
268
			$client->setServerParameter( 'HTTP_REFERER', 'https://en.wikipedia.org/wiki/Karla_Kennichnich' );
269
			$client->followRedirects( false );
270
271
			$client->request(
272
				'POST',
273
				'/donation/add',
274
				$this->newComplementableFormInput()
275
			);
276
277
			$donation = $this->getDonationFromDatabase( $factory );
278
279
			$data = $donation->getDecodedData();
280
			$this->assertSame( 'DE12500105170648489890', $data['iban'] );
281
			$this->assertSame( 'INGDDEFFXXX', $data['bic'] );
282
			$this->assertSame( '0648489890', $data['konto'] );
283
			$this->assertSame( '50010517', $data['blz'] );
284
			$this->assertSame( 'ING-DiBa', $data['bankname'] );
285
		} );
286
	}
287
288
	private function newComplementableFormInput(): array {
289
		return [
290
			'betrag' => '5,51',
291
			'zahlweise' => 'BEZ',
292
			'periode' => 0,
293
			'iban' => 'DE12500105170648489890',
294
			'addressType' => 'person',
295
			'salutation' => 'Frau',
296
			'title' => 'Prof. Dr.',
297
			'firstName' => 'Karla',
298
			'lastName' => 'Kennichnich',
299
			'street' => 'Lehmgasse 12',
300
			'postcode' => '12345',
301
			'city' => 'Einort',
302
			'country' => 'DE',
303
			'email' => '[email protected]',
304
		];
305
	}
306
307
	private function getDonationFromDatabase( FunFunFactory $factory ): Donation {
308
		$donationRepo = $factory->getEntityManager()->getRepository( Donation::class );
309
		$donation = $donationRepo->find( 1 );
310
		$this->assertInstanceOf( Donation::class, $donation );
311
		return $donation;
312
	}
313
314
	public function testGivenValidPayPalData_redirectsToPayPal(): void {
315
		$client = $this->createClient();
316
		$client->followRedirects( false );
317
318
		$client->request(
319
			'POST',
320
			'/donation/add',
321
			$this->newValidPayPalInput()
322
		);
323
324
		$response = $client->getResponse();
325
		$this->assertSame( Response::HTTP_FOUND, $response->getStatusCode() );
326
		$this->assertContains( 'sandbox.paypal.com', $response->getContent() );
327
	}
328
329
	public function testWhenRedirectingToPayPal_translatedItemNameIsPassed(): void {
330
		$client = $this->createClient();
331
		$client->followRedirects( false );
332
333
		$client->request(
334
			'POST',
335
			'/donation/add',
336
			$this->newValidPayPalInput()
337
		);
338
339
		$response = $client->getResponse();
340
		$this->assertSame( Response::HTTP_FOUND, $response->getStatusCode() );
341
		$this->assertContains( 'item_name=item_name_donation', $response->getContent() );
342
	}
343
344
	private function newValidPayPalInput(): array {
345
		return [
346
			'betrag' => '12,34',
347
			'zahlweise' => 'PPL',
348
			'periode' => 3,
349
			'addressType' => 'anonym',
350
		];
351
	}
352
353
	public function testGivenValidCreditCardData_redirectsToPaymentProvider(): void {
354
		$client = $this->createClient();
355
		$client->request(
356
			'POST',
357
			'/donation/add',
358
			$this->newValidCreditCardInput()
359
		);
360
361
		$response = $client->getResponse();
362
		$this->assertSame( Response::HTTP_FOUND, $response->getStatusCode() );
363
		$this->assertTrue( $response->isRedirect() );
364
		$this->assertContains( 'amount=1234', $response->headers->get( 'Location' ) );
365
		$this->assertContains( 'thatother.paymentprovider.com', $response->headers->get( 'Location' ) );
366
	}
367
368
	public function testValidSofortInput_savesDonationAndRedirectsTo3rdPartyPage(): void {
369
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
370
			$response = new SofortResponse();
371
			$response->setPaymentUrl( 'https://bankingpin.please' );
372
373
			$sofortClient = $this->createMock( SofortClient::class );
374
			$sofortClient
375
				->method( 'get' )
376
				->willReturn( $response );
377
			$factory->setSofortClient( $sofortClient );
378
379
380
			$client->followRedirects( false );
381
			$client->request(
382
				'POST',
383
				'/donation/add',
384
				$this->newValidSofortInput()
385
			);
386
387
			$donation = $this->getDonationFromDatabase( $factory );
388
389
			$this->assertSame( 'Z', $donation->getStatus() );
390
			$this->assertRegExp( '/W-Q-[A-Z]{6}-[A-Z]/', $donation->getBankTransferCode() );
391
392
			$this->assertTrue( $client->getResponse()->isRedirect( 'https://bankingpin.please' ) );
393
		} );
394
	}
395
396
	private function newValidCreditCardInput(): array {
397
		return [
398
			'betrag' => '12,34',
399
			'zahlweise' => 'MCP',
400
			'periode' => 3,
401
			'addressType' => 'anonym',
402
		];
403
	}
404
405
	private function newValidSofortInput(): array {
406
		return [
407
			'betrag' => '100,00',
408
			'zahlweise' => 'SUB',
409
			'periode' => 0,
410
			'addressType' => 'anonym',
411
		];
412
	}
413
414
	public function testGivenInvalidRequest_formIsReloadedAndPrefilled(): void {
415
		$client = $this->createClient();
416
		$client->request(
417
			'POST',
418
			'/donation/add',
419
			$this->newInvalidFormInput()
420
		);
421
422
		$response = $client->getResponse()->getContent();
423
424
		$this->assertContains( 'Amount: 0,00', $response );
425
		$this->assertContains( 'Payment type: BEZ', $response );
426
		$this->assertContains( 'Interval: 3', $response );
427
		$this->assertContains( 'IBAN: DE12500105170648489890', $response );
428
		$this->assertContains( 'BIC: INGDDEFFXXX', $response );
429
		$this->assertContains( 'Bank name: ING-DiBa', $response );
430
		$this->assertContains( 'Address type: person', $response );
431
		$this->assertContains( 'Salutation: Frau', $response );
432
		$this->assertContains( 'Title: Prof. Dr.', $response );
433
		$this->assertContains( 'Company: ', $response );
434
		$this->assertContains( 'First name: Karla', $response );
435
		$this->assertContains( 'Last name: Kennichnich', $response );
436
		$this->assertContains( 'Street: Lehmgasse 12', $response );
437
		$this->assertContains( 'Postal code: 12345', $response );
438
		$this->assertContains( 'City: Einort', $response );
439
		$this->assertContains( 'Country code: DE', $response );
440
		$this->assertContains( 'Email address: [email protected]', $response );
441
	}
442
443
	public function testGivenInvalidRequest_formStillContainsBannerTrackingData(): void {
444
		$client = $this->createClient();
445
		$client->request(
446
			'POST',
447
			'/donation/add',
448
			[
449
				'impCount' => 12,
450
				'bImpCount' => 3
451
			]
452
		);
453
454
		$response = $client->getResponse()->getContent();
455
456
		$this->assertContains( 'Impression Count: 12', $response );
457
		$this->assertContains( 'Banner Impression Count: 3', $response );
458
	}
459
460
	public function testGivenNegativeDonationAmount_formIsReloadedAndPrefilledWithZero(): void {
461
		$client = $this->createClient();
462
463
		$formValues = $this->newInvalidFormInput();
464
		$formValues['betrag'] = '-5,00';
465
466
		$client->request(
467
			'POST',
468
			'/donation/add',
469
			$formValues
470
		);
471
472
		$response = $client->getResponse()->getContent();
473
474
		$this->assertContains( 'Amount: 0,00', $response );
475
	}
476
477
	private function newInvalidFormInput(): array {
478
		return [
479
			'betrag' => '0',
480
			'zahlweise' => 'BEZ',
481
			'periode' => 3,
482
			'iban' => 'DE12500105170648489890',
483
			'bic' => 'INGDDEFFXXX',
484
			'konto' => '0648489890',
485
			'blz' => '50010517',
486
			'bankname' => 'ING-DiBa',
487
			'addressType' => 'person',
488
			'salutation' => 'Frau',
489
			'title' => 'Prof. Dr.',
490
			'company' => '',
491
			'firstName' => 'Karla',
492
			'lastName' => 'Kennichnich',
493
			'street' => 'Lehmgasse 12',
494
			'postcode' => '12345',
495
			'city' => 'Einort',
496
			'country' => 'DE',
497
			'email' => '[email protected]',
498
			'info' => '1',
499
			'piwik_campaign' => 'test',
500
			'piwik_kwd' => 'gelb',
501
			'impCount' => '3',
502
			'bImpCount' => '1',
503
			'layout' => 'Default',
504
			'color' => 'blue',
505
			'skin' => 'default',
506
		];
507
	}
508
509
	public function testGivenInvalidAnonymousRequest_formIsReloadedAndPrefilled(): void {
510
		$client = $this->createClient();
511
		$client->request(
512
			'POST',
513
			'/donation/add',
514
			$this->newAnonymousFormInput()
515
		);
516
517
		$response = $client->getResponse()->getContent();
518
519
		$this->assertContains( 'Amount: 0', $response );
520
		$this->assertContains( 'Payment type: UEB', $response );
521
		$this->assertContains( 'Interval: 1', $response );
522
		$this->assertContains( 'Value of field "amount" violates rule: Amount too low', $response );
523
	}
524
525
	private function newAnonymousFormInput(): array {
526
		return [
527
			'betrag' => '0',
528
			'zahlweise' => 'UEB',
529
			'periode' => 1,
530
			'addressType' => 'anonym'
531
		];
532
	}
533
534
	public function testGivenValidRequest_tokensAreReturned(): void {
535
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
536
			$factory->setTokenGenerator( new FixedTokenGenerator( self::SOME_TOKEN ) );
537
538
			$client->setServerParameter( 'HTTP_REFERER', 'https://en.wikipedia.org/wiki/Karla_Kennichnich' );
539
			$client->followRedirects( false );
540
541
			$client->request(
542
				'POST',
543
				'/donation/add',
544
				$this->newValidCreditCardInput()
545
			);
546
547
			$response = $client->getResponse()->getContent();
548
549
			$this->assertContains( self::SOME_TOKEN, $response );
550
		} );
551
	}
552
553
	public function testGivenValidRequest_clientIsRedirected(): void {
554
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
555
			$factory->setTokenGenerator( new FixedTokenGenerator( self::SOME_TOKEN ) );
556
			$client->followRedirects( false );
557
558
			$client->request(
559
				'POST',
560
				'/donation/add',
561
				$this->newValidFormInput()
562
			);
563
564
			$this->assertTrue( $client->getResponse()->isRedirect() );
565
		} );
566
	}
567
568
	public function testWhenTrackingCookieExists_valueIsPersisted(): void {
569
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
570
			$client->getCookieJar()->set( new Cookie( 'spenden_tracking', 'test/blue' ) );
571
572
			$client->request(
573
				'POST',
574
				'/donation/add',
575
				$this->newComplementableFormInput()
576
			);
577
578
			$donation = $this->getDonationFromDatabase( $factory );
579
			$data = $donation->getDecodedData();
580
581
			$this->assertSame( 'test/blue', $data['tracking'] );
582
		} );
583
	}
584
585
	public function testWhenTrackableInputDataIsSubmitted_theyAreStoredInSession(): void {
586
		$this->createAppEnvironment( [], function ( Client $client, FunFunFactory $factory, Application $app ): void {
587
588
			$client->request(
589
				'GET',
590
				'/',
591
				[
592
					'betrag' => '5,00',
593
					'periode' => 3,
594
					'zahlweise' => 'BEZ'
595
				]
596
			);
597
598
			$piwikTracking = $app['session']->get( 'piwikTracking' );
599
			$this->assertSame( 'BEZ', $piwikTracking['paymentType'] );
600
			$this->assertSame( 3, $piwikTracking['paymentInterval'] );
601
			$this->assertSame( '5,00', $piwikTracking['paymentAmount'] );
602
		} );
603
	}
604
605
	public function testWhenTolstojNovelIsPassed_isIsNotStoredInSession(): void {
606
		$this->createAppEnvironment( [], function ( Client $client, FunFunFactory $factory, Application $app ): void {
607
608
			$client->request(
609
				'GET',
610
				'/',
611
				[
612
					'betrag' => '5,00',
613
					'periode' => 3,
614
					'zahlweise' => 'Eh bien, mon prince. Gênes et Lucques ne sont plus que des apanages, des поместья, de la ' .
615
						'famille Buonaparte. Non, je vous préviens que si vous ne me dites pas que nous avons la guerre, si ' .
616
						'vous vous permettez encore de pallier toutes les infamies, toutes les atrocités de cet Antichrist ' .
617
						'(ma parole, j’y crois) — je ne vous connais plus, vous n’êtes plus mon ami, vous n’êtes plus мой ' .
618
						'верный раб, comme vous dites. Ну, здравствуйте,' .
619
						'здравствуйте. Je vois que je vous fais peur, ' .
620
						'садитесь и рассказывайте.'
621
				]
622
			);
623
624
			$piwikTracking = $app['session']->get( 'piwikTracking' );
625
			$this->assertArrayNotHasKey( 'paymentType', $piwikTracking );
626
			$this->assertSame( 3, $piwikTracking['paymentInterval'] );
627
			$this->assertSame( '5,00', $piwikTracking['paymentAmount'] );
628
		} );
629
	}
630
631
	public function testWhenParameterIsOmitted_itIsNotStoredInSession(): void {
632
		$this->createAppEnvironment( [], function ( Client $client, FunFunFactory $factory, Application $app ): void {
633
634
			$client->request(
635
				'GET',
636
				'/',
637
				[
638
					'betrag' => '5,00',
639
					'zahlweise' => 'BEZ'
640
				]
641
			);
642
643
			$piwikTracking = $app['session']->get( 'piwikTracking' );
644
			$this->assertSame( 'BEZ', $piwikTracking['paymentType'] );
645
			$this->assertSame( '5,00', $piwikTracking['paymentAmount'] );
646
			$this->assertArrayNotHasKey( 'paymentInterval', $piwikTracking );
647
		} );
648
	}
649
650
	public function testWhenInitiallyIntendedPaymentOptionsDifferFromActual_itIsReflectedInPiwikTrackingEvents(): void {
651
		$client = $this->createClient( [] );
652
		$client->request(
653
			'GET',
654
			'/',
655
			[
656
				'betrag' => '5.00',
657
				'zahlweise' => 'BEZ',
658
				'periode' => 12
659
			]
660
		);
661
662
		$client->request(
663
			'POST',
664
			'/donation/add',
665
			[
666
				'addressType' => 'anonym',
667
				'betrag' => '12,34',
668
				'periode' => '0',
669
				'zahlweise' => 'UEB'
670
			]
671
		);
672
		$client->followRedirect();
673
674
		$responseContent = $client->getResponse()->getContent();
675
		$this->assertContains( 'BEZ/UEB', $responseContent );
676
		$this->assertContains( '5.00/12.34', $responseContent );
677
		$this->assertContains( '12/0', $responseContent );
678
	}
679
680
	public function testWhenMobileTrackingIsRequested_piwikTrackerIsCalledForPaypalPayment(): void {
681
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
682
			$factory->setNullMessenger();
683
			$client->followRedirects( false );
684
685
			$tracker = $this->getMockBuilder( PageViewTracker::class )->disableOriginalConstructor()->getMock();
686
			$tracker->expects( $this->once() )
687
				->method( 'trackPaypalRedirection' )
688
				->with( 'test', 'gelb', '10.1.2.3' );
689
			$factory->setPageViewTracker( $tracker );
690
691
			$client->request(
692
				'POST',
693
				'/donation/add',
694
				$this->newValidMobilePayPalInput(),
695
				[],
696
				['REMOTE_ADDR' => '10.1.2.3']
697
			);
698
699
			$client->getResponse();
700
		} );
701
	}
702
703
	private function newValidMobilePayPalInput(): array {
704
		return [
705
			'betrag' => '12,34',
706
			'zahlweise' => 'PPL',
707
			'periode' => 3,
708
			'addressType' => 'anonym',
709
			'piwik_campaign' => 'test',
710
			'piwik_kwd' => 'gelb',
711
			'mbt' => '1' // mobile tracking param
712
		];
713
	}
714
715
	public function testWhenMobileTrackingIsRequested_piwikTrackerIsNotCalledForNonPaypalPayment(): void {
716
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
717
			$factory->setNullMessenger();
718
			$client->followRedirects( false );
719
720
			$tracker = $this->getMockBuilder( PageViewTracker::class )->disableOriginalConstructor()->getMock();
721
			$tracker->expects( $this->never() )
722
				->method( 'trackPaypalRedirection' );
723
			$factory->setPageViewTracker( $tracker );
724
725
			$client->request(
726
				'POST',
727
				'/donation/add',
728
				array_merge(
729
					$this->newValidCreditCardInput(),
730
					['mbt' => '1']
731
				)
732
			);
733
734
			$client->getResponse();
735
		} );
736
	}
737
738
	public function testGivenCommasInStreetInput_donationGetsPersisted(): void {
739
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
740
741
			$client->setServerParameter( 'HTTP_REFERER', 'https://en.wikipedia.org/wiki/Karla_Kennichnich' );
742
			$client->followRedirects( false );
743
744
			$formInput = $this->newValidFormInput();
745
			$formInput['street'] = ',Lehmgasse, 12,';
746
747
			$client->request(
748
				'POST',
749
				'/donation/add',
750
				$formInput
751
			);
752
753
			$this->assertIsExpectedDonation( $this->getDonationFromDatabase( $factory ) );
754
		} );
755
	}
756
757
	public function testGivenSufficientForeignBankData_donationGetsPersisted(): void {
758
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ): void {
759
			$formInput = $this->newValidFormInput();
760
			$formInput['iban'] = 'AT022050302101023600';
761
			$formInput['bic'] = 'SPIHAT22XXX';
762
			$formInput['konto'] = '';
763
			$formInput['blz'] = '';
764
			$formInput['bankname'] = '';
765
			$client->request(
766
				'POST',
767
				'/donation/add',
768
				$formInput
769
			);
770
771
			$donation = $this->getDonationFromDatabase( $factory );
772
			$data = $donation->getDecodedData();
773
774
			$this->assertSame( 'AT022050302101023600', $data['iban'] );
775
			$this->assertSame( 'SPIHAT22XXX', $data['bic'] );
776
			$this->assertSame( '', $data['konto'] );
777
			$this->assertSame( '', $data['blz'] );
778
			$this->assertSame( '', $data['bankname'] );
779
		} );
780
	}
781
782
	public function testCookieFlagsSecureAndHttpOnlyAreSet(): void {
783
		$client = new Client(
784
			$this->createSilexApplication(),
785
			[ 'HTTPS' => true ]
786
		);
787
		$client->followRedirects( true );
788
789
		$client->request(
790
			'POST',
791
			'/donation/add',
792
			$this->newValidFormInput()
793
		);
794
795
		$cookieJar = $client->getCookieJar();
796
		$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...
797
		$cookie = $cookieJar->get( ShowDonationConfirmationHandler::SUBMISSION_COOKIE_NAME );
798
		$this->assertTrue( $cookie->isSecure() );
799
		$this->assertTrue( $cookie->isHttpOnly() );
800
	}
801
802
	public function testAllPaymentTypesAreOffered(): void {
803
		$client = $this->createClient( [ 'skin' => [ 'default' => '10h16' ] ] );
804
		$client->request(
805
			'GET',
806
			'/donation/new'
807
		);
808
		$crawler = $client->getCrawler();
809
810
		$this->assertSame( 1, $crawler->filter( '#donation-payment input[name="zahlweise"][value="BEZ"]' )->count() );
811
		$this->assertSame( 1, $crawler->filter( '#donation-payment input[name="zahlweise"][value="UEB"]' )->count() );
812
		$this->assertSame( 1, $crawler->filter( '#donation-payment input[name="zahlweise"][value="MCP"]' )->count() );
813
		$this->assertSame( 1, $crawler->filter( '#donation-payment input[name="zahlweise"][value="PPL"]' )->count() );
814
		$this->assertSame( 1, $crawler->filter( '#donation-payment input[name="zahlweise"][value="SUB"]' )->count() );
815
	}
816
817
	public function testSofortPaymentTypeCanByDisabledViaQuery(): void {
818
		$client = $this->createClient( [ 'skin' => [ 'default' => '10h16' ] ] );
819
		$client->request(
820
			'GET',
821
			'/donation/new',
822
			[ 'pmt' => '0' ]
823
		);
824
		$crawler = $client->getCrawler();
825
826
		$this->assertSame( 0, $crawler->filter( '#donation-payment input[name="zahlweise"][value="SUB"]' )->count() );
827
	}
828
829
}
830