Some times your code depend on a user attribute, so you want set the user attribute in your functional test.
The Problem is that the doCall method in the sfBrowser class rebuild the context and in the end calls theshutdown method from user and storage object to write the session data.
So you must create your own Browser class and override the doCall function and simple move the shutdown calls to the beginning.
Browser Class:
class myBrowser extends sfBrowser
{
/**
* Calls a request to a uri.
*/
protected function doCall()
{
// manually shutdown user to save current session data
if (isset($this->context) AND $this->context->getUser())
{
$this->context->getUser()->shutdown();
$this->context->getStorage()->shutdown();
}
// recycle our context object
$this->context = $this->getContext(true);
sfConfig::set('sf_test', true);
// we register a fake rendering filter
sfConfig::set('sf_rendering_filter', array('sfFakeRenderingFilter', null));
$this->resetCurrentException();
// dispatch our request
ob_start();
$this->context->getController()->dispatch();
$retval = ob_get_clean();
// append retval to the response content
$this->context->getResponse()->setContent($retval);
}
}
Test code:
include dirname(__FILE__).'/../../bootstrap/functional.php';
$browser = new sfTestFunctional(new myBrowser());
$browser->
get('/site-one');
// override the session data
$browser->getUser()->setAttribute('key', $value);
$browser->
get('/site-one');
When you would restrict the display of a field or fieldset group on user credential. you must do the following
1. open the action class and add
public function preExecute()
{
parent::preExecute();
$this->configuration->setUser($this->getUser());
}
2. open the *GeneratorConfiguration class and add
protected $user;
public function setUser($user)
{
$this->user = $user;
}
public function getUser()
{
return $this->user;
}
public function getEditDisplay()
{
$fieldsets = parent::getEditDisplay();
if (!$this->getUser()->hasCredential('admin'))
{
unset(
$fieldsets['Auth'][2], // field
$fieldsets['Rights'] // field group
);
}
return $fieldsets;
}
public function getNewDisplay()
{
... // see getEditDisplay
}
public function getFormOptions()
{
return array('user' => $this->getUser());
}
3. last remove the fields from the form, open the *Form class and add
public function configure()
{
...
if (!$this->getOption('user')->hasCredential('admin'))
{
unset($this['is_super_admin'], $this['groups_list'], $this['permissions_list']);
}
...
}
When you validate you XML Xliff file you have often the problem that a trans-unit node has no or a already existing id. So i have wrote a small script for that problem.
fix_id.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| <?php
$doc = new DOMDocument();
$doc->load($argv[1]);
$xpath = new DOMXPath($doc);
$query = '//trans-unit';
$entries = $xpath->query($query);
foreach ($entries as $i => $entry) {
$entry->setAttribute('id', $i+1);
}
$doc->save($argv[1]); |
Execute the following command in your symfony1 root directory
for FILE in `find apps/*/i18n -name *\.xml`; do php fix_id.php $FILE; done;
You have a register form with a field account_type and two embed forms Company and Address.
You want remove the embed Company form, when the field account_type is sfGuardUserProfile::TYPE_PRIVATE otherwise you want remove the embed Address form.
The solution is to override the bind method in your form.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| public function bind(array $taintedValues = null, array $taintedFiles = null)
{
// unset request param and validator
if($taintedValues['account_type'] == sfGuardUserProfile::TYPE_PRIVATE)
{
unset(
$taintedValues['Company'],
$this->validatorSchema['Company']
);
}
else
{
unset(
$taintedValues['Address'],
$this->validatorSchema['Address']
);
}
parent::bind($taintedValues, $taintedFiles);
} |
create file lib/gfFormHerlp.class.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
| <?php
/**
* Some usefull form helper
*
* @package symfony
* @subpackage helper
* @author Gordon Franke <gfranke@nevalon.de>
* @link http://www.nevalon.de
*/
class gfFormHelper
{
/**
* Add * to required field labels
*
* @param sfForm $form
* @param string $symbol
* @param string $title
*
* @return void
*/
public static function addRequiredToLabel(sfForm $form, $symbol = '*', $title = 'This field is mandatory.')
{
$widgetSchema = $form->getWidgetSchema();
$validatorSchema = $form->getValidatorSchema();
foreach($form->getFormFieldSchema()->getWidget()->getFields() as $key => $object)
{
$label = $form->getFormFieldSchema()->offsetGet($key)->renderLabelName();
if(isset($validatorSchema[$key]) and $validatorSchema[$key]->getOption('required') == true) {
$label .= '<sup title="' . $widgetSchema->getFormFormatter()->translate($title) . '">' . $symbol . '</sup>';
}
$widgetSchema->setLabel($key, $label);
}
}
} |
add at the end of your form setup or better when you have subforms into the configure method this line
1
| gfFormHelper::addRequiredToLabel($this); |
Use this in your testcode to create the user object
1
2
3
4
5
6
7
8
| $_SERVER['session_id'] = 'test';
$dispatcher = new sfEventDispatcher();
$sessionPath = sfToolkit::getTmpDir().'/sessions_'.rand(11111, 99999);
$storage = new sfSessionTestStorage(array('session_path' => $sessionPath));
$myUser = new myUser($dispatcher, $storage);
$myUser->setAttribute('user_id', 1, 'sfGuardSecurityUser'); |
Recent Comments