Completed
Push — master ( 919a96...568a86 )
by wiese
73:55 queued 09:12
created

AddDonationRouteTest::newValidSofortInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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