PhpUnit form does not submit, no error - forms

I'm doing a functional test in PhpUnit to test simple modal window functionality which has only one field and two buttons (i have tried various ways so the code may be not clean, I'm pasting just to show the idea):
$form = $crawler
// find all buttons with the text "Pridėti"
->filter('button:contains("Pridėti")')
->eq(0)
->form()// select the first button in the list
;
$form['appbundle_classinfo[name]'] = '5a';
$crawler = $client->submit($form);
//It doesn't even save to database
$container = self::$kernel->getContainer();
$em = $container->get('doctrine')->getManager();
$classinfo = $em->getRepository('AppBundle:ClassInfo');
//Echoes modal window
echo $client->getResponse()->getContent() ;die;
I also tried to var_dump the form, it shows that the value is added to form:
string(25) "appbundle_classinfo[name]"
["value":protected]=>
string(2) "5a"
So the form is not submitted. Can you help me find out why?

<?php
namespace AppBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class PostTest extends WebTestCase
{
public function testShowPost()
{
//returns a Client, your browser use your web site
$client = static::createClient();
// The request() method (read more about the request method) returns a Crawler object which can be used to select elements in the response, click on links and submit forms.
$crawler = $client->request('GET', '/your/route/controller/action');
// select button by name or id
$form = $crawler->selectButton('Login')->submit();
//submit the form
$crawler->submit($form);
$this->assertGreaterThan(
0,
$crawler->filter('html:contains("hello world")')->count()
);
// get HTML content returned
dump($client->getResponse()->getContent());
}
http://symfony.com/doc/current/testing.html#functional-tests
hope that were useful, let me know.

Related

Form not passing through hidden field value correctly in Facebook, but ok elsewhere

I have a script that first checks if the form page is referred from a GET parameter
$page['is_referred'] = isset($_GET['rfr']) ? 1 : 0;
Then that set value is used as the value of a hidden field in a Symfony2 form
$form = $app['form.factory']->createBuilder('form', $data)
//....
->add('referred', 'hidden', array(
'data' => $page['is_referred'],
))
->getForm();
then
if ('POST' === $request->getMethod()) {
$form->bindRequest($request);
if ($form->isValid()) {
//...
$referred = $form->get('referred')->getData();
$msg_referer = "Thanks from Referrer";
$msg_noreferer = "Thanks";
if( 1 == $referred ){
$page['thanks'] = $msg_referer;
}else{
$page['thanks'] = $msg_noreferer;
}
//..
}
}
The hidden field is read okay on the main site when ?rfr is in the url, but when iframed in Facebook and ?rfr is present, the $msg_noreferer message is displayed instead. Any ideas as to why this happens?
EDIT: I should also mention that the field value is correctly set in Facebook when I check the source, but it just doesn't seem to post the data through correctly
I should also mention that the field value is correctly set in Facebook when I check the source, but it just doesn't seem to post the data through correctly
I’d say, the value is posted alright, but the form validation does not accept it, because the hidden field does not get added to the form on validation.
Have you checked this, if the code adding the hidden field to the form is actually being executed when handling the POST request received from the browser …?

Set values to form builds by zend framework

I use Zend framework to build the forms, I want to make the edit action, since the user click on edit, the form appears with users data, how can I set the data to the form which is built dynamically??
$form->populate($data);
where $data is an array of key value pairs containing your data.
$form = new Zend_Form;
if ($this->_request->isPost()) {
//to just populate
$form->populate($this->_getAllParams());
//or auto populate during validation
if ($form->isValid($this->_getAllParams()) {
//do stuff if valid
}
}
I know that ZF maunal is pretty messy and not everything is clear, but I think forms are explained pretty nice (with examples). You should research more by yourself.
http://framework.zend.com/manual/en/zend.form.html
$Menu = new Admin_Model_DbTable_Menu();
$row = $Menu->fetchRow($Menu->select()->where('id = ?', $id));
$Addmenu = new Admin_Form_Addmenu();
$Addmenu->populate($row->toArray());

Trying to copy values into multiple embedded forms

I have a Symfony 1.4 application to allow users to enter data to a electrical appliance testing database. The page in questions consists of multiple embedded "new" forms so the user can submit many tests in one go. The form validates and saves correctly, but feedback is that it will be tedious to use.
As much of the data may be the same in each test (e.g. same date, same result, same person doing the testing), I would like the user to be able to fill in values in the top row, then click a button to fill the same information in the rows below. I'm pretty sure this would require javascript, but I don't have much experience.
I would appreciate any suggestions.
Many Thanks.
Well, I managed to figure it out without using javascript.
I put a button on the page
<input type="submit" name="copy_values" value="duplicate">
In the action for the page I included the code...
elseif (isset($_POST['copy_values'])) {
// get values from first embedded test
$newTests = $testList['new_tests'];
$testDate = $newTests[0]['et_date_tested'];
$testedBy = $newTests[0]['et_tester_id'];
$formOptions = array('test_date'=>$testDate, 'tester'=>$testedBy);
$this->form = new MultiTestForm(null, $formOptions);
}
$this->setTemplate('multiAdd');
... which takes the values from the widgets in the top row of the form and creates an array. This is passed as the options array to create a new form.
In the top level form class..
public function configure()
{
...
$subform = new sfForm();
for($i = 0;$i < sfConfig::get('app_new_test_rows'); $i ++)
{
$formToAdd = new TestsForMultiAddForm(null,$this->getOptions());
$subform->embedForm($i, $formToAdd);
}
$this->embedForm('new_tests', $subform);
}
...and in the embedded form class...
public function configure()
{
...
if ($this->getOption('test_date')) {
$this->setDefault('et_date_tested', $this->getOption('test_date'));
}
if ($this->getOption('tester')) {
$this->setDefault('et_tester_id', $this->getOption('tester'));
}
...
}
Not sure if this is the conventional way to approach the problem, but it works!

In Yii, how best to POST both search results selection & previously entered form model back to form controller?

To complete a Yii form field, users often need to search for a referenced model record (like searching for a friend's profile in a social app). I'm sure other Yii apps are doing this elegantly. But in my dirty approach, in the search results page, I use a CHtml::submitButton to POST two models back to the form containing:
the "found" record (a user id associated with one of the profiles from the search results)
the previously entered form field contents (relationship characterization fields)
Alternatively, the autocomplete widget works well, but doesn't do the detailed search that I need (e.g. search based on a partial name and city or state or other user profile content).
Alternatively, you'd think that within the search results view I might be able to modify the form member to contain the found record (new friend's user id) and just POST/submit the modified model from the search results page . But for that to work each of the search results in the list needs a unique user id populated in that form field, and I can't figure out how to duplicate the form model before modifying that one member server-side for each of the search results' "submit" or "select" buttons, and it just doesn't seem right to create all those form models.
So what seems to work is to submit two separate models using subforms (within the search results view) , with the submitButton POSTing a model and the extra parameter (user id) separately.
Is there a better way? ...to link to and from a search results page and a form field, retaining already-entered data and populating the searched for field with a selected record from the search results.
Here's controllers/SiteController:
public function actionBefriend() {
$model=new BefriendForm;
if(isset($_POST['BefriendForm'])) {
$model->attributes=$_POST['BefriendForm'];
if ($model->validate()) {
$model->createFriendship();
$this->redirect('Index'); }
else
$er=$model->getErrors(); }
if(isset($_POST['idfriend'])) {
$model->idfriend=$_POST['idfriend']; }
if(isset($model->idfriend)) {
$model->friend_name=Bio::model()->findByPk($model->idfriend)->name; }
$this->render('newFrienship', array('model' =>$model)); // newFriendship is the form view }
Here's controllers/Bio.php (Profile)
public function actionIndex() {
$criteria = new CDbCriteria();
$model=new BefriendForm;
if(isset($_GET['q']))
$q = $_GET['q'];
elseif (isset($_POST['BefriendForm'])) {
$model->attributes=$_POST['BefriendForm'];
$q = $model['friend_name']; }
if(isset($q)) {
$criteria->compare('name', $q, true, 'OR');
$criteria->compare('city', $q, true, 'OR');
$criteria->compare('state', $q, true, 'OR');
$criteria->compare('bio_text', $q, true, 'OR'); }
else
$q = '';
$dataProvider=new CActiveDataProvider('Bio', array('criteria'=>$criteria));
$this->render('index',array('dataProvider'=>$dataProvider, 'q'=>$q, 'model'=>$model )); }
Here's the start of views/site/newFriendship (form view)
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'newFriendship-BefriendForm',
'enableAjaxValidation'=>true,)); ?>
Here's the core of views/bio/index.php (search results index page):
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'viewData'=>array('model'=>$model) )); ?>
Here's the search result row in views/bio/_view.php that links back to BefriendForm (SiteController) that sends the id of the user to populate the friend field in the form (which gets a name from the id):
<form method="POST">
<input type="hidden" name="idfriend" value="<?php echo $data->idfriend ?>" />
// Here's that submit button that I can't get to send both the model
//and the idfriend back to the form to repopulate it
// without manually writing HTML to submit all the fields individually
// or creating 2 subforms to submit together with a signle submitButton.
<?php echo CHtml::submitButton('Befriend', array('submit' => array('site/Befriend'),'model'=$model);
</form>
The best alternative I can see is to imbibe the searcher widget within the form.
Turns out you can just replace the form field containing the primary key with the appropriate value before POSTing back to the NewFriendship form when the user clicks the "Select" or "Befriend" button. So only one model is posted back to the original form from the search results page.
Replace the section from views/bio/_view.php in the question with...
<?php
foreach($model->attributeNames() as $name)
if($name != 'friend_id')
echo CHtml::activeHiddenField($model,$name);
else
echo CHtml::activeHiddenField($model,$name,array('value'=>$data->getPrimaryKey()));
echo CHtml::submitButton('Befriend', array('submit' => array('site/Befriend')));
?>

How to paginate search results?

I want to paginate search results using Zend_Paginator. So I pass my data to a paginator instance:
$paginator = new Zend_Paginator (
new Zend_Paginator_Adapter_DbSelect ( $data )
);
Data is returned this way
public function getData($idArray){
$db = Zend_Db_Table::getDefaultAdapter();
$selectProgramme = new Zend_Db_Select($db);
$selectProgramme->from('programme')
->order('id DESC')
->where('id IN(?)', $idArray);
return $selectProgramme;
}
$idArray is provided by my search implementations. This all works great and I get the correct data and pagination links displayed.
However I can't paginate the result because the pagination links are not valid. So normal pagination would have following link:
mysite.de/home/index/page/1
in search I now have
mysite.de/home/search/page/1
This does not work. Any suggestions how to implement search pagination?
EDIT: I have a HomeController with two actions, index and search action. IndexAction displays all data and I can paginate it.
public function indexAction(){
//...
$paginator = new Zend_Paginator(
new Zend_Paginator_Adapter_DbSelect($data)
);
$paginator->setItemCountPerPage(16)
->setPageRange(20)
->setCurrentPageNumber($this->_getParam('page', 1));
$this->view->data = $paginator;
}
The searchActions handles the search process:
public function searchAction(){
$response = $solr->search($this->getRequest()->getParam('search', null));
//...if items found get the data exactly the same way as in the
// index action, using Zend_Paginator_Adapter_DbSelect
$paginator = new Zend_Paginator(
new Zend_Paginator_Adapter_DbSelect($data)
);
$paginator->setItemCountPerPage(16)
->setPageRange(20)
->setCurrentPageNumber($this->_getParam('page', 1));
$this->view->data = $paginator;
}
So like you see in the search action there is a problem with the search process when I paginate. I need to decide somehow if to search or to paginate. Any suggestions on that?
Since search required the search parameter pagination will fail because when paginating the the search parameter is null.
$sreq = $this->getRequest()->getParam('search', null);
So we need to pass this parameter whenever we paginate our search. I solve this using Zend_Session:
//get search param
$sreq = $this->getRequest()->getParam('search', null);
//store search param in session for pagination
$search = new Zend_Session_Namespace('PSearch');
if($sreq != null){
$search->psearch = $sreq;
}else{
$sreq = $search->psearch;
}
I have this at the top of my searchAction and everything works.
Not sure I understand, but is your problem that the page parameter from the url is not making it's way to the Paginator - e.g. regardless of what page you are on, it is always showing the first 20 results?
If so, have you tried manually setting the page on the paginator:
$page = $this->_getParam('page', 1);
$paginator->setCurrentPageNumber($page);
public function search()
Are you sure that you didn't mistyped here? Should be
public function searchAction()
You put your search data into $response but create paginator instance using $data (which is null)