forked from KnpLabs/KnpUserBundle
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathFOSUserExtensionTest.php
542 lines (472 loc) · 18.8 KB
/
FOSUserExtensionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Tests\DependencyInjection;
use FOS\UserBundle\DependencyInjection\FOSUserExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Yaml\Parser;
class FOSUserExtensionTest extends TestCase
{
/** @var ContainerBuilder */
protected $configuration;
protected function tearDown()
{
$this->configuration = null;
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testUserLoadThrowsExceptionUnlessDatabaseDriverSet()
{
$loader = new FOSUserExtension();
$config = $this->getEmptyConfig();
unset($config['db_driver']);
$loader->load([$config], new ContainerBuilder());
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testUserLoadThrowsExceptionUnlessDatabaseDriverIsValid()
{
$loader = new FOSUserExtension();
$config = $this->getEmptyConfig();
$config['db_driver'] = 'foo';
$loader->load([$config], new ContainerBuilder());
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testUserLoadThrowsExceptionUnlessFirewallNameSet()
{
$loader = new FOSUserExtension();
$config = $this->getEmptyConfig();
unset($config['firewall_name']);
$loader->load([$config], new ContainerBuilder());
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testUserLoadThrowsExceptionUnlessGroupModelClassSet()
{
$loader = new FOSUserExtension();
$config = $this->getFullConfig();
unset($config['group']['group_class']);
$loader->load([$config], new ContainerBuilder());
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testUserLoadThrowsExceptionUnlessUserModelClassSet()
{
$loader = new FOSUserExtension();
$config = $this->getEmptyConfig();
unset($config['user_class']);
$loader->load([$config], new ContainerBuilder());
}
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testCustomDriverWithoutManager()
{
$loader = new FOSUserExtension();
$config = $this->getEmptyConfig();
$config['db_driver'] = 'custom';
$loader->load([$config], new ContainerBuilder());
}
public function testCustomDriver()
{
$this->configuration = new ContainerBuilder();
$loader = new FOSUserExtension();
$config = $this->getEmptyConfig();
$config['db_driver'] = 'custom';
$config['service']['user_manager'] = 'acme.user_manager';
$loader->load([$config], $this->configuration);
$this->assertNotHasDefinition('fos_user.user_manager.default');
$this->assertAlias('acme.user_manager', 'fos_user.user_manager');
$this->assertParameter('custom', 'fos_user.storage');
}
public function testDisableRegistration()
{
$this->configuration = new ContainerBuilder();
$loader = new FOSUserExtension();
$config = $this->getEmptyConfig();
$config['registration'] = false;
$loader->load([$config], $this->configuration);
$this->assertNotHasDefinition('fos_user.registration.form.factory');
$mailer = $this->configuration->getDefinition('fos_user.mailer.default');
$parameters = $this->configuration->getParameterBag()->resolveValue(
$mailer->getArgument(3)
);
$this->assertSame(
[
'confirmation' => ['[email protected]' => 'Acme Ltd'],
'resetting' => ['[email protected]' => 'Acme Corp'],
],
$parameters['from_email']
);
}
public function testDisableResetting()
{
$this->configuration = new ContainerBuilder();
$loader = new FOSUserExtension();
$config = $this->getEmptyConfig();
$config['resetting'] = false;
$loader->load([$config], $this->configuration);
$this->assertNotHasDefinition('fos_user.resetting.form.factory');
$mailer = $this->configuration->getDefinition('fos_user.mailer.default');
$parameters = $this->configuration->getParameterBag()->resolveValue(
$mailer->getArgument(3)
);
$this->assertSame(
[
'confirmation' => ['[email protected]' => 'Acme Corp'],
'resetting' => ['[email protected]' => 'Acme Ltd'],
],
$parameters['from_email']
);
}
public function testDisableProfile()
{
$this->configuration = new ContainerBuilder();
$loader = new FOSUserExtension();
$config = $this->getEmptyConfig();
$config['profile'] = false;
$loader->load([$config], $this->configuration);
$this->assertNotHasDefinition('fos_user.profile.form.factory');
}
public function testDisableChangePassword()
{
$this->configuration = new ContainerBuilder();
$loader = new FOSUserExtension();
$config = $this->getEmptyConfig();
$config['change_password'] = false;
$loader->load([$config], $this->configuration);
$this->assertNotHasDefinition('fos_user.change_password.form.factory');
}
/**
* @dataProvider providerEmailsDisabledFeature
*/
public function testEmailsDisabledFeature($testConfig, $registration, $resetting)
{
$this->configuration = new ContainerBuilder();
$loader = new FOSUserExtension();
$config = $this->getEmptyConfig();
$config = array_merge($config, $testConfig);
$loader->load([$config], $this->configuration);
$this->assertParameter($registration, 'fos_user.registration.confirmation.from_email');
$this->assertParameter($resetting, 'fos_user.resetting.email.from_email');
}
public function providerEmailsDisabledFeature()
{
$configBothFeaturesDisabled = ['registration' => false, 'resetting' => false];
$configResettingDisabled = ['resetting' => false];
$configRegistrationDisabled = ['registration' => false];
$configOverridenRegistrationEmail = [
'registration' => [
'confirmation' => [
'from_email' => ['address' => '[email protected]', 'sender_name' => 'Acme Ltd'],
],
],
];
$configOverridenResettingEmail = [
'resetting' => [
'email' => [
'from_email' => ['address' => '[email protected]', 'sender_name' => 'Acme Ltd'],
],
],
];
$default = ['[email protected]' => 'Acme Corp'];
$overriden = ['[email protected]' => 'Acme Ltd'];
return [
[$configBothFeaturesDisabled, ['[email protected]' => 'Acme Ltd'], ['[email protected]' => 'Acme Ltd']],
[$configResettingDisabled, $default, ['[email protected]' => 'Acme Ltd']],
[$configRegistrationDisabled, ['[email protected]' => 'Acme Ltd'], $default],
[$configOverridenRegistrationEmail, $overriden, $default],
[$configOverridenResettingEmail, $default, $overriden],
];
}
public function testUserLoadModelClassWithDefaults()
{
$this->createEmptyConfiguration();
$this->assertParameter('Acme\MyBundle\Document\User', 'fos_user.model.user.class');
}
public function testUserLoadModelClass()
{
$this->createFullConfiguration();
$this->assertParameter('Acme\MyBundle\Entity\User', 'fos_user.model.user.class');
}
public function testUserLoadManagerClassWithDefaults()
{
$this->createEmptyConfiguration();
$this->assertParameter('mongodb', 'fos_user.storage');
$this->assertParameter(null, 'fos_user.model_manager_name');
$this->assertAlias('fos_user.user_manager.default', 'fos_user.user_manager');
$this->assertNotHasDefinition('fos_user.group_manager');
}
public function testUserLoadManagerClass()
{
$this->createFullConfiguration();
$this->assertParameter('orm', 'fos_user.storage');
$this->assertParameter('custom', 'fos_user.model_manager_name');
$this->assertAlias('acme_my.user_manager', 'fos_user.user_manager');
$this->assertAlias('fos_user.group_manager.default', 'fos_user.group_manager');
}
public function testUserLoadFormClass()
{
$this->createFullConfiguration();
$this->assertParameter('acme_my_profile', 'fos_user.profile.form.type');
$this->assertParameter('acme_my_registration', 'fos_user.registration.form.type');
$this->assertParameter('acme_my_group', 'fos_user.group.form.type');
$this->assertParameter('acme_my_change_password', 'fos_user.change_password.form.type');
$this->assertParameter('acme_my_resetting', 'fos_user.resetting.form.type');
}
public function testUserLoadFormNameWithDefaults()
{
$this->createEmptyConfiguration();
$this->assertParameter('fos_user_profile_form', 'fos_user.profile.form.name');
$this->assertParameter('fos_user_registration_form', 'fos_user.registration.form.name');
$this->assertParameter('fos_user_change_password_form', 'fos_user.change_password.form.name');
$this->assertParameter('fos_user_resetting_form', 'fos_user.resetting.form.name');
}
public function testUserLoadFormName()
{
$this->createFullConfiguration();
$this->assertParameter('acme_profile_form', 'fos_user.profile.form.name');
$this->assertParameter('acme_registration_form', 'fos_user.registration.form.name');
$this->assertParameter('acme_group_form', 'fos_user.group.form.name');
$this->assertParameter('acme_change_password_form', 'fos_user.change_password.form.name');
$this->assertParameter('acme_resetting_form', 'fos_user.resetting.form.name');
}
public function testUserLoadFormServiceWithDefaults()
{
$this->createEmptyConfiguration();
$this->assertHasDefinition('fos_user.profile.form.factory');
$this->assertHasDefinition('fos_user.registration.form.factory');
$this->assertNotHasDefinition('fos_user.group.form.factory');
$this->assertHasDefinition('fos_user.change_password.form.factory');
$this->assertHasDefinition('fos_user.resetting.form.factory');
}
public function testUserLoadFormService()
{
$this->createFullConfiguration();
$this->assertHasDefinition('fos_user.profile.form.factory');
$this->assertHasDefinition('fos_user.registration.form.factory');
$this->assertHasDefinition('fos_user.group.form.factory');
$this->assertHasDefinition('fos_user.change_password.form.factory');
$this->assertHasDefinition('fos_user.resetting.form.factory');
}
public function testUserLoadConfirmationEmailWithDefaults()
{
$this->createEmptyConfiguration();
$this->assertParameter(false, 'fos_user.registration.confirmation.enabled');
$this->assertParameter(['[email protected]' => 'Acme Corp'], 'fos_user.registration.confirmation.from_email');
$this->assertParameter('@FOSUser/Registration/email.txt.twig', 'fos_user.registration.confirmation.template');
$this->assertParameter('@FOSUser/Resetting/email.txt.twig', 'fos_user.resetting.email.template');
$this->assertParameter(['[email protected]' => 'Acme Corp'], 'fos_user.resetting.email.from_email');
$this->assertParameter(86400, 'fos_user.resetting.token_ttl');
}
public function testUserLoadConfirmationEmail()
{
$this->createFullConfiguration();
$this->assertParameter(true, 'fos_user.registration.confirmation.enabled');
$this->assertParameter(['[email protected]' => 'Acme Corp'], 'fos_user.registration.confirmation.from_email');
$this->assertParameter('AcmeMyBundle:Registration:mail.txt.twig', 'fos_user.registration.confirmation.template');
$this->assertParameter('AcmeMyBundle:Resetting:mail.txt.twig', 'fos_user.resetting.email.template');
$this->assertParameter(['[email protected]' => 'Acme Corp'], 'fos_user.resetting.email.from_email');
$this->assertParameter(7200, 'fos_user.resetting.retry_ttl');
}
public function testUserLoadUtilServiceWithDefaults()
{
$this->createEmptyConfiguration();
$this->assertAlias('fos_user.mailer.default', 'fos_user.mailer');
$this->assertAlias('fos_user.util.canonicalizer.default', 'fos_user.util.email_canonicalizer');
$this->assertAlias('fos_user.util.canonicalizer.default', 'fos_user.util.username_canonicalizer');
}
public function testUserLoadUtilService()
{
$this->createFullConfiguration();
$this->assertAlias('acme_my.mailer', 'fos_user.mailer');
$this->assertAlias('acme_my.email_canonicalizer', 'fos_user.util.email_canonicalizer');
$this->assertAlias('acme_my.username_canonicalizer', 'fos_user.util.username_canonicalizer');
}
public function testUserLoadFlashesByDefault()
{
$this->createEmptyConfiguration();
$this->assertHasDefinition('fos_user.listener.flash');
}
public function testUserLoadFlashesCanBeDisabled()
{
$this->createFullConfiguration();
$this->assertNotHasDefinition('fos_user.listener.flash');
}
/**
* @dataProvider userManagerSetFactoryProvider
*
* @param $dbDriver
* @param $doctrineService
*/
public function testUserManagerSetFactory($dbDriver, $doctrineService)
{
$this->configuration = new ContainerBuilder();
$loader = new FOSUserExtension();
$config = $this->getEmptyConfig();
$config['db_driver'] = $dbDriver;
$loader->load([$config], $this->configuration);
$definition = $this->configuration->getDefinition('fos_user.object_manager');
$this->assertAlias($doctrineService, 'fos_user.doctrine_registry');
if (method_exists($definition, 'getFactory')) {
$factory = $definition->getFactory();
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $factory[0]);
$this->assertSame('fos_user.doctrine_registry', (string) $factory[0]);
$this->assertSame('getManager', $factory[1]);
} else {
$this->assertSame('fos_user.doctrine_registry', $definition->getFactoryService());
$this->assertSame('getManager', $definition->getFactoryMethod());
}
}
/**
* @return array
*/
public function userManagerSetFactoryProvider()
{
return [
['orm', 'doctrine'],
['couchdb', 'doctrine_couchdb'],
['mongodb', 'doctrine_mongodb'],
];
}
protected function createEmptyConfiguration()
{
$this->configuration = new ContainerBuilder();
$loader = new FOSUserExtension();
$config = $this->getEmptyConfig();
$loader->load([$config], $this->configuration);
$this->assertTrue($this->configuration instanceof ContainerBuilder);
}
protected function createFullConfiguration()
{
$this->configuration = new ContainerBuilder();
$loader = new FOSUserExtension();
$config = $this->getFullConfig();
$loader->load([$config], $this->configuration);
$this->assertTrue($this->configuration instanceof ContainerBuilder);
}
/**
* getEmptyConfig.
*
* @return array
*/
protected function getEmptyConfig()
{
$yaml = <<<EOF
db_driver: mongodb
firewall_name: fos_user
user_class: Acme\MyBundle\Document\User
from_email:
address: [email protected]
sender_name: Acme Corp
EOF;
$parser = new Parser();
return $parser->parse($yaml);
}
/**
* @return mixed
*/
protected function getFullConfig()
{
$yaml = <<<EOF
db_driver: orm
firewall_name: fos_user
use_listener: true
use_flash_notifications: false
user_class: Acme\MyBundle\Entity\User
model_manager_name: custom
from_email:
address: [email protected]
sender_name: Acme Corp
profile:
form:
type: acme_my_profile
name: acme_profile_form
validation_groups: [acme_profile]
change_password:
form:
type: acme_my_change_password
name: acme_change_password_form
validation_groups: [acme_change_password]
registration:
confirmation:
from_email:
address: [email protected]
sender_name: Acme Corp
enabled: true
template: AcmeMyBundle:Registration:mail.txt.twig
form:
type: acme_my_registration
name: acme_registration_form
validation_groups: [acme_registration]
resetting:
retry_ttl: 7200
token_ttl: 86400
email:
from_email:
address: [email protected]
sender_name: Acme Corp
template: AcmeMyBundle:Resetting:mail.txt.twig
form:
type: acme_my_resetting
name: acme_resetting_form
validation_groups: [acme_resetting]
service:
mailer: acme_my.mailer
email_canonicalizer: acme_my.email_canonicalizer
username_canonicalizer: acme_my.username_canonicalizer
user_manager: acme_my.user_manager
group:
group_class: Acme\MyBundle\Entity\Group
form:
type: acme_my_group
name: acme_group_form
validation_groups: [acme_group]
EOF;
$parser = new Parser();
return $parser->parse($yaml);
}
/**
* @param string $value
* @param string $key
*/
private function assertAlias($value, $key)
{
$this->assertSame($value, (string) $this->configuration->getAlias($key), sprintf('%s alias is correct', $key));
}
/**
* @param mixed $value
* @param string $key
*/
private function assertParameter($value, $key)
{
$this->assertSame($value, $this->configuration->getParameter($key), sprintf('%s parameter is correct', $key));
}
/**
* @param string $id
*/
private function assertHasDefinition($id)
{
$this->assertTrue(($this->configuration->hasDefinition($id) ?: $this->configuration->hasAlias($id)));
}
/**
* @param string $id
*/
private function assertNotHasDefinition($id)
{
$this->assertFalse(($this->configuration->hasDefinition($id) ?: $this->configuration->hasAlias($id)));
}
}