/** * Constructor. * * @param sfUser A sfUser instance * @param array An array of options * @param string A CSRF secret (false to disable CSRF protection, null to use the global CSRF secret) * * @see sfForm */ public function __construct(sfUser $user, $options = array(), $CSRFSecret = null) { $this->user = $user; if (!isset($options['languages'])) { throw new RuntimeException(sprintf('%s requires a "languages" option.', get_class($this))); } parent::__construct(array('language' => $user->getCulture()), $options, $CSRFSecret); }
/** * Returns user parameters as an array. * * @param sfUser $user A sfUser instance * * @return array The user parameters */ public static function userAsArray(sfUser $user = null) { if (!$user) { return array(); } $data = array('options' => $user->getOptions(), 'attributeHolder' => self::flattenParameterHolder($user->getAttributeHolder(), true), 'culture' => $user->getCulture()); if ($user instanceof sfBasicSecurityUser) { $data = array_merge($data, array('authenticated' => $user->isAuthenticated(), 'credentials' => $user->getCredentials(), 'lastRequest' => $user->getLastRequestTime())); } return $data; }
$_SERVER['session_id'] = 'test'; $dispatcher = new sfEventDispatcher(); $sessionPath = sys_get_temp_dir() . '/sessions_' . rand(11111, 99999); $storage = new sfSessionTestStorage(array('session_path' => $sessionPath)); $user = new sfUser($dispatcher, $storage); // ->initialize() $t->diag('->initialize()'); $t->is($user->getCulture(), 'en', '->initialize() sets the culture to "en" by default'); $user->setCulture(null); $user->initialize($dispatcher, $storage, array('default_culture' => 'de')); user_flush($dispatcher, $user, $storage); $t->is($user->getCulture(), 'de', '->initialize() sets the culture to the value of default_culture if available'); user_flush($dispatcher, $user, $storage); $t->is($user->getCulture(), 'de', '->initialize() reads the culture from the session data if available'); $userBis = new sfUser($dispatcher, $storage); $t->is($userBis->getCulture(), 'de', '->initialize() serializes the culture to the session data'); // ->setCulture() ->getCulture() $t->diag('->setCulture() ->getCulture()'); $user->setCulture('fr'); $t->is($user->getCulture(), 'fr', '->setCulture() changes the current user culture'); // ->setFlash() ->getFlash() ->hasFlash() $t->diag('->setFlash() ->getFlash() ->hasFlash()'); $user->initialize($dispatcher, $storage, array('use_flash' => true)); $user->setFlash('foo', 'bar'); $t->is($user->getFlash('foo'), 'bar', '->setFlash() sets a flash variable'); $t->is($user->hasFlash('foo'), true, '->hasFlash() returns true if the flash variable exists'); user_flush($dispatcher, $user, $storage, array('use_flash' => true)); $userBis = new sfUser($dispatcher, $storage, array('use_flash' => true)); $t->is($userBis->getFlash('foo'), 'bar', '->getFlash() returns a flash previously set'); $t->is($userBis->hasFlash('foo'), true, '->hasFlash() returns true if the flash variable exists'); user_flush($dispatcher, $user, $storage, array('use_flash' => true));
/** * Returns user parameters as an array. * * @param sfUser $user A sfUser instance * * @return array The user parameters */ public static function userAsArray(sfUser $user = null) { if (!$user) { return array(); } return array('options' => $user->getOptions(), 'attributeHolder' => self::flattenParameterHolder($user->getAttributeHolder()), 'culture' => $user->getCulture()); }
new sfFormLanguage($user); $t->fail('__construct() throws a RuntimeException if you don\'t pass a "languages" option'); } catch (RuntimeException $e) { $t->pass('__construct() throws a RuntimeException if you don\'t pass a "languages" option'); } $form = new sfFormLanguage($user, array('languages' => array('en', 'fr'))); $t->is($form->getDefault('language'), 'en', '__construct() sets the default language value to the user language'); $w = $form->getWidgetSchema(); $t->is($w['language']->getOption('languages'), array('en', 'fr'), '__construct() uses the "languages" option for the select form widget'); $v = $form->getValidatorSchema(); $t->is($v['language']->getOption('languages'), array('en', 'fr'), '__construct() uses the "languages" option for the validator'); // ->process() $t->diag('->process()'); // with CSRF disabled $t->diag('with CSRF disabled'); sfForm::disableCSRFProtection(); $form = new sfFormLanguage($user, array('languages' => array('en', 'fr'))); $request->setParameter('language', 'fr'); $t->is($form->process($request), true, '->process() returns true if the form is valid'); $t->is($user->getCulture(), 'fr', '->process() changes the user culture'); $request->setParameter('language', 'es'); $t->is($form->process($request), false, '->process() returns true if the form is not valid'); $t->is($form['language']->getError()->getCode(), 'invalid', '->process() throws an error if the language is not in the languages option'); sfToolkit::clearDirectory($sessionPath); // with CSRF enabled $t->diag('with CSRF enabled'); sfForm::enableCSRFProtection('secret'); $form = new sfFormLanguage($user, array('languages' => array('en', 'fr'))); $request->setParameter('language', 'fr'); $request->setParameter('_csrf_token', $form->getCSRFToken('secret')); $t->is($form->process($request), true, '->process() returns true if the form is valid');