Completed
Push — master ( e6c518...18cdbd )
by Jeroen De
12s
created

testGivenValidPayPalData_redirectsToPayPal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
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\HttpKernel\Client;
10
use WMDE\Fundraising\Entities\Donation;
11
use WMDE\Fundraising\Frontend\Factories\FunFunFactory;
12
use WMDE\Fundraising\Frontend\Infrastructure\PageViewTracker;
13
use WMDE\Fundraising\Frontend\Tests\EdgeToEdge\WebRouteTestCase;
14
use WMDE\Fundraising\Frontend\Tests\Fixtures\FixedTokenGenerator;
15
16
/**
17
 * @licence GNU GPL v2+
18
 * @author Kai Nissen < [email protected] >
19
 * @author Gabriel Birke < [email protected] >
20
 */
21
class AddDonationRouteTest extends WebRouteTestCase {
22
23
	const SOME_TOKEN = 'SomeToken';
24
25
	public function setUp() {
26
		if ( !function_exists( 'lut_init' ) ) {
27
			$this->markTestSkipped( 'The konto_check needs to be installed!' );
28
		}
29
		parent::setUp();
30
	}
31
32
	public function testGivenValidRequest_donationGetsPersisted() {
33
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
34
35
			$client->setServerParameter( 'HTTP_REFERER', 'https://en.wikipedia.org/wiki/Karla_Kennichnich' );
36
			$client->followRedirects( false );
37
38
			$client->request(
39
				'POST',
40
				'/donation/add',
41
				$this->newValidFormInput()
42
			);
43
44
			$this->assertIsExpectedDonation( $this->getDonationFromDatabase( $factory ) );
45
		} );
46
	}
47
48
	public function testWhenDonationGetsPersisted_timestampIsStoredInCookie() {
49
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
0 ignored issues
show
Unused Code introduced by
The parameter $factory is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
51
			$client->followRedirects( true );
52
			$client->request(
53
				'POST',
54
				'/donation/add',
55
				$this->newValidFormInput()
56
			);
57
58
			$cookie = $client->getCookieJar()->get( 'donation_timestamp' );
59
			$this->assertNotNull( $cookie );
60
			$donationTimestamp = new \DateTime( $cookie->getValue() );
61
			$this->assertEquals( time(), $donationTimestamp->getTimestamp(), 'Timestamp should be not more than 5 seconds old', 5.0 );
62
63
		} );
64
	}
65
66
	public function testWhenMultipleDonationFormSubmissions_requestGetsRejected() {
67
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
0 ignored issues
show
Unused Code introduced by
The parameter $factory is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68
			$client->getCookieJar()->set( new Cookie( 'donation_timestamp', $this->getPastTimestamp() ) );
69
70
			$client->request(
71
				'POST',
72
				'/donation/add',
73
				$this->newValidFormInput()
74
			);
75
76
			$this->assertContains( 'Sie haben vor sehr kurzer Zeit bereits gespendet', $client->getResponse()->getContent() );
77
		} );
78
	}
79
80
	public function testWhenMultipleDonationsInAccordanceToTimeLimit_requestIsNotRejected() {
81
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
0 ignored issues
show
Unused Code introduced by
The parameter $factory is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
			$client->getCookieJar()->set(
83
				new Cookie(
84
					'donation_timestamp',
85
					$this->getPastTimestamp( 'PT35M' )
86
				)
87
			);
88
89
			$client->request(
90
				'POST',
91
				'/donation/add',
92
				$this->newValidFormInput()
93
			);
94
95
			$this->assertNotContains( 'Sie haben vor sehr kurzer Zeit bereits gespendet', $client->getResponse()->getContent() );
96
		} );
97
	}
98
99
	private function getPastTimestamp( string $interval = 'PT10S' ) {
100
		return ( new \DateTime() )->sub( new \DateInterval( $interval ) )->format( 'Y-m-d H:i:s' );
101
	}
102
103
	private function newValidFormInput() {
104
		return [
105
			'betrag' => '5,51',
106
			'zahlweise' => 'BEZ',
107
			'periode' => 0,
108
			'iban' => 'DE12500105170648489890',
109
			'bic' => 'INGDDEFFXXX',
110
			'konto' => '0648489890',
111
			'blz' => '50010517',
112
			'bankname' => 'ING-DiBa',
113
			'addressType' => 'person',
114
			'salutation' => 'Frau',
115
			'title' => 'Prof. Dr.',
116
			'company' => '',
117
			'firstName' => 'Karla',
118
			'lastName' => 'Kennichnich',
119
			'street' => 'Lehmgasse 12',
120
			'postcode' => '12345',
121
			'city' => 'Einort',
122
			'country' => 'DE',
123
			'email' => '[email protected]',
124
			'info' => '1',
125
			'piwik_campaign' => 'test',
126
			'piwik_kwd' => 'gelb',
127
			'impCount' => '3',
128
			'bImpCount' => '1',
129
			'layout' => 'Default',
130
			'color' => 'blue',
131
			'skin' => 'default',
132
		];
133
	}
134
135
	private function assertIsExpectedDonation( Donation $donation ) {
136
		$data = $donation->getDecodedData();
137
		$this->assertSame( '5.51', $donation->getAmount() );
138
		$this->assertSame( 'BEZ', $donation->getPaymentType() );
139
		$this->assertSame( 0, $donation->getPaymentIntervalInMonths() );
140
		$this->assertSame( 'DE12500105170648489890', $data['iban'] );
141
		$this->assertSame( 'INGDDEFFXXX', $data['bic'] );
142
		$this->assertSame( '0648489890', $data['konto'] );
143
		$this->assertSame( '50010517', $data['blz'] );
144
		$this->assertSame( 'ING-DiBa', $data['bankname'] );
145
		$this->assertSame( 'person', $data['adresstyp'] );
146
		$this->assertSame( 'Frau', $data['anrede'] );
147
		$this->assertSame( 'Prof. Dr.', $data['titel'] );
148
		$this->assertSame( '', $data['firma'] );
149
		$this->assertSame( 'Karla', $data['vorname'] );
150
		$this->assertSame( 'Kennichnich', $data['nachname'] );
151
		$this->assertSame( 'Prof. Dr. Karla Kennichnich', $donation->getDonorFullName() );
152
		$this->assertSame( 'Lehmgasse 12', $data['strasse'] );
153
		$this->assertSame( '12345', $data['plz'] );
154
		$this->assertSame( 'Einort', $data['ort'] );
155
		$this->assertSame( 'Einort', $donation->getDonorCity() );
156
		$this->assertSame( 'DE', $data['country'] );
157
		$this->assertSame( '[email protected]', $data['email'] );
158
		$this->assertSame( '[email protected]', $donation->getDonorEmail() );
159
		$this->assertSame( 'test/gelb', $data['tracking'] );
160
		$this->assertSame( 3, $data['impCount'] );
161
		$this->assertSame( 1, $data['bImpCount'] );
162
		$this->assertSame( '', $data['layout'] );
163
		$this->assertSame( '', $data['color'] );
164
		$this->assertSame( '', $data['skin'] );
165
		$this->assertSame( 'en.wikipedia.org', $data['source'] );
166
		$this->assertSame( 'N', $donation->getStatus() );
167
		$this->assertTrue( $donation->getDonorOptsIntoNewsletter() );
168
	}
169
170
	public function testGivenValidRequest_confirmationPageContainsEnteredData() {
171
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
0 ignored issues
show
Unused Code introduced by
The parameter $factory is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
172
173
			$client->request(
174
				'POST',
175
				'/donation/add',
176
				$this->newValidFormInput()
177
			);
178
			$client->followRedirect();
179
180
			$response = $client->getResponse()->getContent();
181
182
			$this->assertContains( '<strong>5,51 €</strong>', $response );
183
			$this->assertContains( 'per Lastschrift', $response );
184
			$this->assertContains( 'einmalig', $response );
185
			$this->assertContains( 'DE12500105170648489890', $response );
186
			$this->assertContains( 'INGDDEFFXXX', $response );
187
			$this->assertContains( 'ING-DiBa', $response );
188
			$this->assertContains( 'Prof. Dr. Karla Kennichnich', $response );
189
			$this->assertContains( 'Lehmgasse 12', $response );
190
			$this->assertContains( '<span id="confirm-postcode">12345</span> <span id="confirm-city">Einort</span>', $response );
191
			$this->assertContains( '[email protected]', $response );
192
			$this->assertContains( '<div id="send-info"', $response );
193
		} );
194
	}
195
196
	public function testGivenValidBankTransferRequest_donationGetsPersisted() {
197
		/**
198
		 * @var FunFunFactory
199
		 */
200
		$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...
201
202
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
203
204
			$client->setServerParameter( 'HTTP_REFERER', 'https://en.wikipedia.org/wiki/Karla_Kennichnich' );
205
			$client->followRedirects( false );
206
207
			$client->request(
208
				'POST',
209
				'/donation/add',
210
				$this->newValidBankTransferInput()
211
			);
212
213
			$donation = $this->getDonationFromDatabase( $factory );
214
215
			$data = $donation->getDecodedData();
216
			$this->assertSame( '12.34', $donation->getAmount() );
217
			$this->assertSame( 'UEB', $donation->getPaymentType() );
218
			$this->assertSame( 0, $donation->getPaymentIntervalInMonths() );
219
			$this->assertSame( 'person', $data['adresstyp'] );
220
			$this->assertSame( 'Frau', $data['anrede'] );
221
			$this->assertSame( 'Prof. Dr.', $data['titel'] );
222
			$this->assertSame( '', $data['firma'] );
223
			$this->assertSame( 'Karla', $data['vorname'] );
224
			$this->assertSame( 'Kennichnich', $data['nachname'] );
225
			$this->assertSame( 'Prof. Dr. Karla Kennichnich', $donation->getDonorFullName() );
226
			$this->assertSame( 'Lehmgasse 12', $data['strasse'] );
227
			$this->assertSame( '12345', $data['plz'] );
228
			$this->assertSame( 'Einort', $data['ort'] );
229
			$this->assertSame( 'Einort', $donation->getDonorCity() );
230
			$this->assertSame( 'DE', $data['country'] );
231
			$this->assertSame( '[email protected]', $data['email'] );
232
			$this->assertSame( '[email protected]', $donation->getDonorEmail() );
233
			$this->assertSame( 'test/gelb', $data['tracking'] );
234
			$this->assertSame( 3, $data['impCount'] );
235
			$this->assertSame( 1, $data['bImpCount'] );
236
			$this->assertSame( '', $data['layout'] );
237
			$this->assertSame( '', $data['color'] );
238
			$this->assertSame( '', $data['skin'] );
239
			$this->assertSame( 'en.wikipedia.org', $data['source'] );
240
			$this->assertSame( true, $donation->getDonorOptsIntoNewsletter() );
241
242
			$this->assertSame( 'Z', $donation->getStatus() );
243
			$this->assertRegExp( '/W-Q-[A-Z]{6}-[A-Z]/', $donation->getBankTransferCode() );
244
		} );
245
	}
246
247
	private function newValidBankTransferInput() {
248
		return [
249
			'betrag' => '12,34',
250
			'zahlweise' => 'UEB',
251
			'periode' => 0,
252
			'addressType' => 'person',
253
			'salutation' => 'Frau',
254
			'title' => 'Prof. Dr.',
255
			'company' => '',
256
			'firstName' => 'Karla',
257
			'lastName' => 'Kennichnich',
258
			'street' => 'Lehmgasse 12',
259
			'postcode' => '12345',
260
			'city' => 'Einort',
261
			'country' => 'DE',
262
			'email' => '[email protected]',
263
			'info' => '1',
264
			'piwik_campaign' => 'test',
265
			'piwik_kwd' => 'gelb',
266
			'impCount' => '3',
267
			'bImpCount' => '1',
268
			'layout' => 'Default',
269
			'color' => 'blue',
270
			'skin' => 'default',
271
		];
272
	}
273
274
	public function testGivenComplementableBankData_donationStillGetsPersisted() {
275
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
276
277
			$client->setServerParameter( 'HTTP_REFERER', 'https://en.wikipedia.org/wiki/Karla_Kennichnich' );
278
			$client->followRedirects( false );
279
280
			$client->request(
281
				'POST',
282
				'/donation/add',
283
				$this->newComplementableFormInput()
284
			);
285
286
			$donation = $this->getDonationFromDatabase( $factory );
287
288
			$data = $donation->getDecodedData();
289
			$this->assertSame( 'DE12500105170648489890', $data['iban'] );
290
			$this->assertSame( 'INGDDEFFXXX', $data['bic'] );
291
			$this->assertSame( '0648489890', $data['konto'] );
292
			$this->assertSame( '50010517', $data['blz'] );
293
			$this->assertSame( 'ING-DiBa', $data['bankname'] );
294
		} );
295
	}
296
297
	private function newComplementableFormInput() {
298
		return [
299
			'betrag' => '5,51',
300
			'zahlweise' => 'BEZ',
301
			'periode' => 0,
302
			'iban' => 'DE12500105170648489890',
303
			'addressType' => 'person',
304
			'salutation' => 'Frau',
305
			'title' => 'Prof. Dr.',
306
			'firstName' => 'Karla',
307
			'lastName' => 'Kennichnich',
308
			'street' => 'Lehmgasse 12',
309
			'postcode' => '12345',
310
			'city' => 'Einort',
311
			'country' => 'DE',
312
			'email' => '[email protected]',
313
		];
314
	}
315
316
	private function getDonationFromDatabase( FunFunFactory $factory ): Donation {
317
		$donationRepo = $factory->getEntityManager()->getRepository( Donation::class );
318
		$donation = $donationRepo->find( 1 );
319
		$this->assertInstanceOf( Donation::class, $donation );
320
		return $donation;
321
	}
322
323
	public function testGivenValidPayPalData_redirectsToPayPal() {
324
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
0 ignored issues
show
Unused Code introduced by
The parameter $factory is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
325
			$client->followRedirects( false );
326
327
			$client->request(
328
				'POST',
329
				'/donation/add',
330
				$this->newValidPayPalInput()
331
			);
332
333
			$response = $client->getResponse();
334
			$this->assertSame( 302, $response->getStatusCode() );
335
			$this->assertContains( 'sandbox.paypal.com', $response->getContent() );
336
		} );
337
	}
338
339
	private function newValidPayPalInput() {
340
		return [
341
			'betrag' => '12,34',
342
			'zahlweise' => 'PPL',
343
			'periode' => 3,
344
			'addressType' => 'anonym',
345
		];
346
	}
347
348
	public function testGivenValidCreditCardData_showsIframeEmbeddingPage() {
349
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
0 ignored issues
show
Unused Code introduced by
The parameter $factory is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
350
351
			$client->request(
352
				'POST',
353
				'/donation/add',
354
				$this->newValidCreditCardInput()
355
			);
356
357
			$response = $client->getResponse();
358
			$this->assertSame( 200, $response->getStatusCode() );
359
			$this->assertContains( 'Ich spende vierteljährlich 12,34 € per Kreditkarte.', $response->getContent() );
360
			$this->assertContains( 'thatother.paymentprovider.com', $response->getContent() );
361
		} );
362
	}
363
364
	private function newValidCreditCardInput() {
365
		return [
366
			'betrag' => '12,34',
367
			'zahlweise' => 'MCP',
368
			'periode' => 3,
369
			'addressType' => 'anonym',
370
		];
371
	}
372
373
	public function testGivenInvalidRequest_formIsReloadedAndPrefilled() {
374
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
0 ignored issues
show
Unused Code introduced by
The parameter $factory is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
375
376
			$client->request(
377
				'POST',
378
				'/donation/add',
379
				$this->newInvalidFormInput()
380
			);
381
382
			$response = $client->getResponse()->getContent();
383
384
			$this->assertContains( 'Amount: 0,00', $response );
385
			$this->assertContains( 'Payment type: BEZ', $response );
386
			$this->assertContains( 'Interval: 3', $response );
387
			$this->assertContains( 'IBAN: DE12500105170648489890', $response );
388
			$this->assertContains( 'BIC: INGDDEFFXXX', $response );
389
			$this->assertContains( 'Bank name: ING-DiBa', $response );
390
			$this->assertContains( 'Address type: person', $response );
391
			$this->assertContains( 'Salutation: Frau', $response );
392
			$this->assertContains( 'Title: Prof. Dr.', $response );
393
			$this->assertContains( 'Company: ', $response );
394
			$this->assertContains( 'First name: Karla', $response );
395
			$this->assertContains( 'Last name: Kennichnich', $response );
396
			$this->assertContains( 'Street: Lehmgasse 12', $response );
397
			$this->assertContains( 'Postal code: 12345', $response );
398
			$this->assertContains( 'City: Einort', $response );
399
			$this->assertContains( 'Country code: DE', $response );
400
			$this->assertContains( 'Email address: [email protected]', $response );
401
		} );
402
	}
403
404
	public function testGivenInvalidRequest_formStillContainsBannerTrackingData() {
405
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
0 ignored issues
show
Unused Code introduced by
The parameter $factory is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
406
407
			$client->request(
408
				'POST',
409
				'/donation/add',
410
				[
411
					'impCount' => 12,
412
					'bImpCount' => 3
413
				]
414
			);
415
416
			$response = $client->getResponse()->getContent();
417
418
			$this->assertContains( 'Impression Count: 12', $response );
419
			$this->assertContains( 'Banner Impression Count: 3', $response );
420
		} );
421
	}
422
423
	public function testGivenNegativeDonationAmount_formIsReloadedAndPrefilledWithZero() {
424
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
0 ignored issues
show
Unused Code introduced by
The parameter $factory is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
425
426
			$formValues = $this->newInvalidFormInput();
427
			$formValues['betrag'] = '-5,00';
428
429
			$client->request(
430
				'POST',
431
				'/donation/add',
432
				$formValues
433
			);
434
435
			$response = $client->getResponse()->getContent();
436
437
			$this->assertContains( 'Amount: 0,00', $response );
438
		} );
439
	}
440
441
	private function newInvalidFormInput() {
442
		return [
443
			'betrag' => '0',
444
			'zahlweise' => 'BEZ',
445
			'periode' => 3,
446
			'iban' => 'DE12500105170648489890',
447
			'bic' => 'INGDDEFFXXX',
448
			'konto' => '0648489890',
449
			'blz' => '50010517',
450
			'bankname' => 'ING-DiBa',
451
			'addressType' => 'person',
452
			'salutation' => 'Frau',
453
			'title' => 'Prof. Dr.',
454
			'company' => '',
455
			'firstName' => 'Karla',
456
			'lastName' => 'Kennichnich',
457
			'street' => 'Lehmgasse 12',
458
			'postcode' => '12345',
459
			'city' => 'Einort',
460
			'country' => 'DE',
461
			'email' => '[email protected]',
462
			'info' => '1',
463
			'piwik_campaign' => 'test',
464
			'piwik_kwd' => 'gelb',
465
			'impCount' => '3',
466
			'bImpCount' => '1',
467
			'layout' => 'Default',
468
			'color' => 'blue',
469
			'skin' => 'default',
470
		];
471
	}
472
473
	public function testGivenInvalidAnonymousRequest_formIsReloadedAndPrefilled() {
474
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
0 ignored issues
show
Unused Code introduced by
The parameter $factory is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
475
476
			$client->request(
477
				'POST',
478
				'/donation/add',
479
				$this->newAnonymousFormInput()
480
			);
481
482
			$response = $client->getResponse()->getContent();
483
484
			$this->assertContains( 'Amount: 0', $response );
485
			$this->assertContains( 'Payment type: UEB', $response );
486
			$this->assertContains( 'Interval: 1', $response );
487
			$this->assertContains( 'Value of field "amount" violates rule: Amount too low', $response );
488
		} );
489
	}
490
491
	private function newAnonymousFormInput() {
492
		return [
493
			'betrag' => '0',
494
			'zahlweise' => 'UEB',
495
			'periode' => 1,
496
			'addressType' => 'anonym'
497
		];
498
	}
499
500
	public function testGivenValidRequest_tokensAreReturned() {
501
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
502
			$factory->setTokenGenerator( new FixedTokenGenerator( self::SOME_TOKEN ) );
503
504
			$client->setServerParameter( 'HTTP_REFERER', 'https://en.wikipedia.org/wiki/Karla_Kennichnich' );
505
			$client->followRedirects( false );
506
507
			$client->request(
508
				'POST',
509
				'/donation/add',
510
				$this->newValidCreditCardInput()
511
			);
512
513
			$response = $client->getResponse()->getContent();
514
515
			$this->assertContains( self::SOME_TOKEN, $response );
516
		} );
517
	}
518
519
	public function testGivenValidRequest_clientIsRedirected() {
520
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
521
			$factory->setTokenGenerator( new FixedTokenGenerator( self::SOME_TOKEN ) );
522
			$client->followRedirects( false );
523
524
			$client->request(
525
				'POST',
526
				'/donation/add',
527
				$this->newValidFormInput()
528
			);
529
530
			$this->assertTrue( $client->getResponse()->isRedirect() );
531
		} );
532
	}
533
534
	public function testWhenTrackingCookieExists_valueIsPersisted() {
535
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
536
			$client->getCookieJar()->set( new Cookie( 'spenden_tracking', 'test/blue' ) );
537
538
			$client->request(
539
				'POST',
540
				'/donation/add',
541
				$this->newComplementableFormInput()
542
			);
543
544
			$donation = $this->getDonationFromDatabase( $factory );
545
			$data = $donation->getDecodedData();
546
547
			$this->assertSame( 'test/blue', $data['tracking'] );
548
		} );
549
	}
550
551
	public function testWhenTrackableInputDataIsSubmitted_theyAreStoredInSession() {
552
		$this->createAppEnvironment( [], function ( Client $client, FunFunFactory $factory, Application $app ) {
553
554
			$client->request(
555
				'GET',
556
				'/',
557
				[
558
					'betrag' => '5,00',
559
					'periode' => 3,
560
					'zahlweise' => 'BEZ'
561
				]
562
			);
563
564
			$piwikTracking = $app['session']->get( 'piwikTracking' );
565
			$this->assertSame( 'BEZ', $piwikTracking['paymentType'] );
566
			$this->assertSame( 3, $piwikTracking['paymentInterval'] );
567
			$this->assertSame( '5,00', $piwikTracking['paymentAmount'] );
568
		} );
569
	}
570
571
	public function testWhenTolstojNovelIsPassed_isIsNotStoredInSession() {
572
		$this->createAppEnvironment( [], function ( Client $client, FunFunFactory $factory, Application $app ) {
573
574
			$client->request(
575
				'GET',
576
				'/',
577
				[
578
					'betrag' => '5,00',
579
					'periode' => 3,
580
					'zahlweise' => 'Eh bien, mon prince. Gênes et Lucques ne sont plus que des apanages, des поместья, de la ' .
581
						'famille Buonaparte. Non, je vous préviens que si vous ne me dites pas que nous avons la guerre, si ' .
582
						'vous vous permettez encore de pallier toutes les infamies, toutes les atrocités de cet Antichrist ' .
583
						'(ma parole, j’y crois) — je ne vous connais plus, vous n’êtes plus mon ami, vous n’êtes plus мой ' .
584
						'верный раб, comme vous dites. Ну, здравствуйте,' .
585
						'здравствуйте. Je vois que je vous fais peur, ' .
586
						'садитесь и рассказывайте.'
587
				]
588
			);
589
590
			$piwikTracking = $app['session']->get( 'piwikTracking' );
591
			$this->assertArrayNotHasKey( 'paymentType', $piwikTracking );
592
			$this->assertSame( 3, $piwikTracking['paymentInterval'] );
593
			$this->assertSame( '5,00', $piwikTracking['paymentAmount'] );
594
		} );
595
	}
596
597
	public function testWhenParameterIsOmitted_itIsNotStoredInSession() {
598
		$this->createAppEnvironment( [], function ( Client $client, FunFunFactory $factory, Application $app ) {
599
600
			$client->request(
601
				'GET',
602
				'/',
603
				[
604
					'betrag' => '5,00',
605
					'zahlweise' => 'BEZ'
606
				]
607
			);
608
609
			$piwikTracking = $app['session']->get( 'piwikTracking' );
610
			$this->assertSame( 'BEZ', $piwikTracking['paymentType'] );
611
			$this->assertSame( '5,00', $piwikTracking['paymentAmount'] );
612
			$this->assertArrayNotHasKey( 'paymentInterval', $piwikTracking );
613
		} );
614
	}
615
616
	public function testWhenInitiallyIntendedPaymentOptionsDifferFromActual_itIsReflectedInPiwikTrackingEvents() {
617
		$client = $this->createClient( [] );
618
		$client->request(
619
			'GET',
620
			'/',
621
			[
622
				'betrag' => '5.00',
623
				'zahlweise' => 'BEZ',
624
				'periode' => 12
625
			]
626
		);
627
628
		$client->request(
629
			'POST',
630
			'/donation/add',
631
			[
632
				'addressType' => 'anonym',
633
				'betrag' => '12,34',
634
				'periode' => '0',
635
				'zahlweise' => 'UEB'
636
			]
637
		);
638
		$client->followRedirect();
639
640
		$responseContent = $client->getResponse()->getContent();
641
		$this->assertContains( 'BEZ/UEB', $responseContent );
642
		$this->assertContains( '5.00/12.34', $responseContent );
643
		$this->assertContains( '12/0', $responseContent );
644
	}
645
646
	public function testWhenMobileTrackingIsRequested_piwikTrackerIsCalledForPaypalPayment() {
647
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
648
			$factory->setNullMessenger();
649
			$client->followRedirects( false );
650
651
			$tracker = $this->getMockBuilder( PageViewTracker::class )->disableOriginalConstructor()->getMock();
652
			$tracker->expects( $this->once() )
653
				->method( 'trackPaypalRedirection' )
654
				->with( 'test', 'gelb', '10.1.2.3' );
655
			$factory->setPageViewTracker( $tracker );
656
657
			$client->request(
658
				'POST',
659
				'/donation/add',
660
				$this->newValidMobilePayPalInput(),
661
				[],
662
				[ 'REMOTE_ADDR' => '10.1.2.3' ]
663
			);
664
665
			$client->getResponse();
0 ignored issues
show
Unused Code introduced by
The call to the method Symfony\Component\HttpKernel\Client::getResponse() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
666
		} );
667
	}
668
669
	private function newValidMobilePayPalInput() {
670
		return [
671
			'betrag' => '12,34',
672
			'zahlweise' => 'PPL',
673
			'periode' => 3,
674
			'addressType' => 'anonym',
675
			'piwik_campaign' => 'test',
676
			'piwik_kwd' => 'gelb',
677
			'mbt' => '1' // mobile tracking param
678
		];
679
	}
680
681
	public function testWhenMobileTrackingIsRequested_piwikTrackerIsNotCalledForNonPaypalPayment() {
682
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
683
			$factory->setNullMessenger();
684
			$client->followRedirects( false );
685
686
			$tracker = $this->getMockBuilder( PageViewTracker::class )->disableOriginalConstructor()->getMock();
687
			$tracker->expects( $this->never() )
688
				->method( 'trackPaypalRedirection' );
689
			$factory->setPageViewTracker( $tracker );
690
691
			$client->request(
692
				'POST',
693
				'/donation/add',
694
				array_merge(
695
					$this->newValidCreditCardInput(),
696
					[ 'mbt' => '1' ]
697
				)
698
			);
699
700
			$client->getResponse();
0 ignored issues
show
Unused Code introduced by
The call to the method Symfony\Component\HttpKernel\Client::getResponse() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
701
		} );
702
	}
703
704
	public function testGivenCommasInStreetInput_donationGetsPersisted() {
705
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
706
707
			$client->setServerParameter( 'HTTP_REFERER', 'https://en.wikipedia.org/wiki/Karla_Kennichnich' );
708
			$client->followRedirects( false );
709
710
			$formInput = $this->newValidFormInput();
711
			$formInput['street'] = ',Lehmgasse, 12,';
712
713
			$client->request(
714
				'POST',
715
				'/donation/add',
716
				$formInput
717
			);
718
719
			$this->assertIsExpectedDonation( $this->getDonationFromDatabase( $factory ) );
720
		} );
721
	}
722
723
	public function testGivenSufficientForeignBankData_donationGetsPersisted() {
724
		$this->createEnvironment( [], function ( Client $client, FunFunFactory $factory ) {
725
			$formInput = $this->newValidFormInput();
726
			$formInput['iban'] = 'AT022050302101023600';
727
			$formInput['bic'] = 'SPIHAT22XXX';
728
			$formInput['konto'] = '';
729
			$formInput['blz'] = '';
730
			$formInput['bankname'] = '';
731
			$client->request(
732
				'POST',
733
				'/donation/add',
734
				$formInput
735
			);
736
737
			$donation = $this->getDonationFromDatabase( $factory );
738
			$data = $donation->getDecodedData();
739
740
			$this->assertSame( 'AT022050302101023600', $data['iban'] );
741
			$this->assertSame( 'SPIHAT22XXX', $data['bic'] );
742
			$this->assertSame( '', $data['konto'] );
743
			$this->assertSame( '', $data['blz'] );
744
			$this->assertSame( '', $data['bankname'] );
745
		} );
746
	}
747
748
}
749