FOSUserExtensionTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. /*
  3. * This file is part of the FOSUserBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace FOS\UserBundle\Tests\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use FOS\UserBundle\DependencyInjection\FOSUserExtension;
  13. use Symfony\Component\Yaml\Parser;
  14. class FOSUserExtensionTest extends \PHPUnit_Framework_TestCase
  15. {
  16. protected $configuration;
  17. /**
  18. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  19. */
  20. public function testUserLoadThrowsExceptionUnlessDatabaseDriverSet()
  21. {
  22. $loader = new FOSUserExtension();
  23. $config = $this->getEmptyConfig();
  24. unset($config['db_driver']);
  25. $loader->load(array($config), new ContainerBuilder());
  26. }
  27. /**
  28. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  29. */
  30. public function testUserLoadThrowsExceptionUnlessDatabaseDriverIsValid()
  31. {
  32. $loader = new FOSUserExtension();
  33. $config = $this->getEmptyConfig();
  34. $config['db_driver'] = 'foo';
  35. $loader->load(array($config), new ContainerBuilder());
  36. }
  37. /**
  38. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  39. */
  40. public function testUserLoadThrowsExceptionUnlessFirewallNameSet()
  41. {
  42. $loader = new FOSUserExtension();
  43. $config = $this->getEmptyConfig();
  44. unset($config['firewall_name']);
  45. $loader->load(array($config), new ContainerBuilder());
  46. }
  47. /**
  48. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  49. */
  50. public function testUserLoadThrowsExceptionUnlessGroupModelClassSet()
  51. {
  52. $loader = new FOSUserExtension();
  53. $config = $this->getFullConfig();
  54. unset($config['group']['group_class']);
  55. $loader->load(array($config), new ContainerBuilder());
  56. }
  57. /**
  58. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  59. */
  60. public function testUserLoadThrowsExceptionUnlessUserModelClassSet()
  61. {
  62. $loader = new FOSUserExtension();
  63. $config = $this->getEmptyConfig();
  64. unset($config['user_class']);
  65. $loader->load(array($config), new ContainerBuilder());
  66. }
  67. /**
  68. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  69. */
  70. public function testCustomDriverWithoutManager()
  71. {
  72. $loader = new FOSUserExtension();
  73. $config = $this->getEmptyConfig();
  74. $config['db_driver'] = 'custom';
  75. $loader->load(array($config), new ContainerBuilder());
  76. }
  77. public function testCustomDriver()
  78. {
  79. $this->configuration = new ContainerBuilder();
  80. $loader = new FOSUserExtension();
  81. $config = $this->getEmptyConfig();
  82. $config['db_driver'] = 'custom';
  83. $config['service']['user_manager'] = 'acme.user_manager';
  84. $loader->load(array($config), $this->configuration);
  85. $this->assertNotHasDefinition('fos_user.user_manager.default');
  86. $this->assertAlias('acme.user_manager', 'fos_user.user_manager');
  87. $this->assertParameter('custom', 'fos_user.storage');
  88. }
  89. public function testDisableRegistration()
  90. {
  91. $this->configuration = new ContainerBuilder();
  92. $loader = new FOSUserExtension();
  93. $config = $this->getEmptyConfig();
  94. $config['registration'] = false;
  95. $loader->load(array($config), $this->configuration);
  96. $this->assertNotHasDefinition('fos_user.registration.form');
  97. }
  98. public function testDisableResetting()
  99. {
  100. $this->configuration = new ContainerBuilder();
  101. $loader = new FOSUserExtension();
  102. $config = $this->getEmptyConfig();
  103. $config['resetting'] = false;
  104. $loader->load(array($config), $this->configuration);
  105. $this->assertNotHasDefinition('fos_user.resetting.form');
  106. }
  107. public function testDisableProfile()
  108. {
  109. $this->configuration = new ContainerBuilder();
  110. $loader = new FOSUserExtension();
  111. $config = $this->getEmptyConfig();
  112. $config['profile'] = false;
  113. $loader->load(array($config), $this->configuration);
  114. $this->assertNotHasDefinition('fos_user.profile.form');
  115. }
  116. public function testDisableChangePassword()
  117. {
  118. $this->configuration = new ContainerBuilder();
  119. $loader = new FOSUserExtension();
  120. $config = $this->getEmptyConfig();
  121. $config['change_password'] = false;
  122. $loader->load(array($config), $this->configuration);
  123. $this->assertNotHasDefinition('fos_user.change_password.form');
  124. }
  125. public function testUserLoadModelClassWithDefaults()
  126. {
  127. $this->createEmptyConfiguration();
  128. $this->assertParameter('Acme\MyBundle\Document\User', 'fos_user.model.user.class');
  129. }
  130. public function testUserLoadModelClass()
  131. {
  132. $this->createFullConfiguration();
  133. $this->assertParameter('Acme\MyBundle\Entity\User', 'fos_user.model.user.class');
  134. }
  135. public function testUserLoadManagerClassWithDefaults()
  136. {
  137. $this->createEmptyConfiguration();
  138. $this->assertParameter('mongodb', 'fos_user.storage');
  139. $this->assertParameter(null, 'fos_user.model_manager_name');
  140. $this->assertAlias('fos_user.user_manager.default', 'fos_user.user_manager');
  141. $this->assertNotHasDefinition('fos_user.group_manager');
  142. }
  143. public function testUserLoadManagerClass()
  144. {
  145. $this->createFullConfiguration();
  146. $this->assertParameter('orm', 'fos_user.storage');
  147. $this->assertParameter('custom', 'fos_user.model_manager_name');
  148. $this->assertAlias('acme_my.user_manager', 'fos_user.user_manager');
  149. $this->assertAlias('fos_user.group_manager.default', 'fos_user.group_manager');
  150. }
  151. public function testUserLoadFormClassWithDefaults()
  152. {
  153. $this->createEmptyConfiguration();
  154. $this->assertParameter('fos_user_profile', 'fos_user.profile.form.type');
  155. $this->assertParameter('fos_user_registration', 'fos_user.registration.form.type');
  156. $this->assertParameter('fos_user_change_password', 'fos_user.change_password.form.type');
  157. $this->assertParameter('fos_user_resetting', 'fos_user.resetting.form.type');
  158. }
  159. public function testUserLoadFormClass()
  160. {
  161. $this->createFullConfiguration();
  162. $this->assertParameter('acme_my_profile', 'fos_user.profile.form.type');
  163. $this->assertParameter('acme_my_registration', 'fos_user.registration.form.type');
  164. $this->assertParameter('acme_my_group', 'fos_user.group.form.type');
  165. $this->assertParameter('acme_my_change_password', 'fos_user.change_password.form.type');
  166. $this->assertParameter('acme_my_resetting', 'fos_user.resetting.form.type');
  167. }
  168. public function testUserLoadFormNameWithDefaults()
  169. {
  170. $this->createEmptyConfiguration();
  171. $this->assertParameter('fos_user_profile_form', 'fos_user.profile.form.name');
  172. $this->assertParameter('fos_user_registration_form', 'fos_user.registration.form.name');
  173. $this->assertParameter('fos_user_change_password_form', 'fos_user.change_password.form.name');
  174. $this->assertParameter('fos_user_resetting_form', 'fos_user.resetting.form.name');
  175. }
  176. public function testUserLoadFormName()
  177. {
  178. $this->createFullConfiguration();
  179. $this->assertParameter('acme_profile_form', 'fos_user.profile.form.name');
  180. $this->assertParameter('acme_registration_form', 'fos_user.registration.form.name');
  181. $this->assertParameter('acme_group_form', 'fos_user.group.form.name');
  182. $this->assertParameter('acme_change_password_form', 'fos_user.change_password.form.name');
  183. $this->assertParameter('acme_resetting_form', 'fos_user.resetting.form.name');
  184. }
  185. public function testUserLoadFormServiceWithDefaults()
  186. {
  187. $this->createEmptyConfiguration();
  188. $this->assertHasDefinition('fos_user.profile.form');
  189. $this->assertHasDefinition('fos_user.registration.form');
  190. $this->assertNotHasDefinition('fos_user.group.form');
  191. $this->assertHasDefinition('fos_user.change_password.form');
  192. $this->assertHasDefinition('fos_user.resetting.form');
  193. }
  194. public function testUserLoadFormService()
  195. {
  196. $this->createFullConfiguration();
  197. $this->assertHasDefinition('fos_user.profile.form');
  198. $this->assertHasDefinition('fos_user.registration.form');
  199. $this->assertHasDefinition('fos_user.group.form');
  200. $this->assertHasDefinition('fos_user.change_password.form');
  201. $this->assertHasDefinition('fos_user.resetting.form');
  202. }
  203. public function testUserLoadConfirmationEmailWithDefaults()
  204. {
  205. $this->createEmptyConfiguration();
  206. $this->assertParameter(false, 'fos_user.registration.confirmation.enabled');
  207. $this->assertParameter(array('webmaster@example.com' => 'webmaster'), 'fos_user.registration.confirmation.from_email');
  208. $this->assertParameter('FOSUserBundle:Registration:email.txt.twig', 'fos_user.registration.confirmation.template');
  209. $this->assertParameter('FOSUserBundle:Resetting:email.txt.twig', 'fos_user.resetting.email.template');
  210. $this->assertParameter(array('webmaster@example.com' => 'webmaster'), 'fos_user.resetting.email.from_email');
  211. $this->assertParameter(86400, 'fos_user.resetting.token_ttl');
  212. }
  213. public function testUserLoadConfirmationEmail()
  214. {
  215. $this->createFullConfiguration();
  216. $this->assertParameter(true, 'fos_user.registration.confirmation.enabled');
  217. $this->assertParameter(array('register@acme.org' => 'Acme Corp'), 'fos_user.registration.confirmation.from_email');
  218. $this->assertParameter('AcmeMyBundle:Registration:mail.txt.twig', 'fos_user.registration.confirmation.template');
  219. $this->assertParameter('AcmeMyBundle:Resetting:mail.txt.twig', 'fos_user.resetting.email.template');
  220. $this->assertParameter(array('reset@acme.org' => 'Acme Corp'), 'fos_user.resetting.email.from_email');
  221. $this->assertParameter(1800, 'fos_user.resetting.token_ttl');
  222. }
  223. public function testUserLoadTemplateConfigWithDefaults()
  224. {
  225. $this->createEmptyConfiguration();
  226. $this->assertParameter('twig', 'fos_user.template.engine');
  227. }
  228. public function testUserLoadTemplateConfig()
  229. {
  230. $this->createFullConfiguration();
  231. $this->assertParameter('php', 'fos_user.template.engine');
  232. }
  233. public function testUserLoadUtilServiceWithDefaults()
  234. {
  235. $this->createEmptyConfiguration();
  236. $this->assertAlias('fos_user.mailer.default', 'fos_user.mailer');
  237. $this->assertAlias('fos_user.util.canonicalizer.default', 'fos_user.util.email_canonicalizer');
  238. $this->assertAlias('fos_user.util.canonicalizer.default', 'fos_user.util.username_canonicalizer');
  239. }
  240. public function testUserLoadUtilService()
  241. {
  242. $this->createFullConfiguration();
  243. $this->assertAlias('acme_my.mailer', 'fos_user.mailer');
  244. $this->assertAlias('acme_my.email_canonicalizer', 'fos_user.util.email_canonicalizer');
  245. $this->assertAlias('acme_my.username_canonicalizer', 'fos_user.util.username_canonicalizer');
  246. }
  247. protected function createEmptyConfiguration()
  248. {
  249. $this->configuration = new ContainerBuilder();
  250. $loader = new FOSUserExtension();
  251. $config = $this->getEmptyConfig();
  252. $loader->load(array($config), $this->configuration);
  253. $this->assertTrue($this->configuration instanceof ContainerBuilder);
  254. }
  255. protected function createFullConfiguration()
  256. {
  257. $this->configuration = new ContainerBuilder();
  258. $loader = new FOSUserExtension();
  259. $config = $this->getFullConfig();
  260. $loader->load(array($config), $this->configuration);
  261. $this->assertTrue($this->configuration instanceof ContainerBuilder);
  262. }
  263. /**
  264. * getEmptyConfig
  265. *
  266. * @return array
  267. */
  268. protected function getEmptyConfig()
  269. {
  270. $yaml = <<<EOF
  271. db_driver: mongodb
  272. firewall_name: fos_user
  273. user_class: Acme\MyBundle\Document\User
  274. EOF;
  275. $parser = new Parser();
  276. return $parser->parse($yaml);
  277. }
  278. protected function getFullConfig()
  279. {
  280. $yaml = <<<EOF
  281. db_driver: orm
  282. firewall_name: fos_user
  283. use_listener: true
  284. user_class: Acme\MyBundle\Entity\User
  285. model_manager_name: custom
  286. from_email:
  287. address: admin@acme.org
  288. sender_name: Acme Corp
  289. profile:
  290. form:
  291. type: acme_my_profile
  292. handler: acme_my.form.handler.profile
  293. name: acme_profile_form
  294. validation_groups: [acme_profile]
  295. change_password:
  296. form:
  297. type: acme_my_change_password
  298. handler: acme_my.form.handler.change_password
  299. name: acme_change_password_form
  300. validation_groups: [acme_change_password]
  301. registration:
  302. confirmation:
  303. from_email:
  304. address: register@acme.org
  305. sender_name: Acme Corp
  306. enabled: true
  307. template: AcmeMyBundle:Registration:mail.txt.twig
  308. form:
  309. type: acme_my_registration
  310. handler: acme_my.form.handler.registration
  311. name: acme_registration_form
  312. validation_groups: [acme_registration]
  313. resetting:
  314. token_ttl: 1800
  315. email:
  316. from_email:
  317. address: reset@acme.org
  318. sender_name: Acme Corp
  319. template: AcmeMyBundle:Resetting:mail.txt.twig
  320. form:
  321. type: acme_my_resetting
  322. handler: acme_my.form.handler.resetting
  323. name: acme_resetting_form
  324. validation_groups: [acme_resetting]
  325. service:
  326. mailer: acme_my.mailer
  327. email_canonicalizer: acme_my.email_canonicalizer
  328. username_canonicalizer: acme_my.username_canonicalizer
  329. user_manager: acme_my.user_manager
  330. template:
  331. engine: php
  332. group:
  333. group_class: Acme\MyBundle\Entity\Group
  334. form:
  335. type: acme_my_group
  336. handler: acme_my.form.handler.group
  337. name: acme_group_form
  338. validation_groups: [acme_group]
  339. EOF;
  340. $parser = new Parser();
  341. return $parser->parse($yaml);
  342. }
  343. /**
  344. * @param string $value
  345. * @param string $key
  346. */
  347. private function assertAlias($value, $key)
  348. {
  349. $this->assertEquals($value, (string) $this->configuration->getAlias($key), sprintf('%s alias is correct', $key));
  350. }
  351. /**
  352. * @param mixed $value
  353. * @param string $key
  354. */
  355. private function assertParameter($value, $key)
  356. {
  357. $this->assertEquals($value, $this->configuration->getParameter($key), sprintf('%s parameter is correct', $key));
  358. }
  359. /**
  360. * @param string $id
  361. */
  362. private function assertHasDefinition($id)
  363. {
  364. $this->assertTrue(($this->configuration->hasDefinition($id) ?: $this->configuration->hasAlias($id)));
  365. }
  366. /**
  367. * @param string $id
  368. */
  369. private function assertNotHasDefinition($id)
  370. {
  371. $this->assertFalse(($this->configuration->hasDefinition($id) ?: $this->configuration->hasAlias($id)));
  372. }
  373. protected function tearDown()
  374. {
  375. unset($this->configuration);
  376. }
  377. }