Functional Testcases for Form using Symfony - php

I write Simple code for contqact Form,which contain fields name, email and message. I want to write test cases for it to check user enter fields value are match with the expected value or not. But i m not getting user enter value while I execute 'contactTest.php' file using command "./symfony test:functional frontend contact"
Code:
enter code here
<?php
include('/home/APP/test/bootstrap/functional.php');
$browser = new sfTestFunctional(new sfBrowser());
$browser->
get('/Contact/thankyou')->
with('request')->begin()->
isParameter('module', 'Contact')->
isParameter('action', 'thankyou')->
end();
$browser->
get('/Contact/index')->
click('Submit', array(
'name' => 'Test',
'email' => 'not.an.email',
'message' => 'message'
))->
isRedirected()->
followRedirect()->
with('response')->checkElement('#content ul li','Testa');
get('/Contact/thankyou', array('name' => 'Test'));
?>
Please help me to write testcase for checking user enter value in the form and expected value. And also suggest how to run & test?

It looks to me that you forgot to provide your form object.
Can you share your form template?
You probably want something like this :
get('/Contact/index')->
click('Submit', array(
contact => array(
'name' => 'Test',
'email' => 'not.an.email',
'message' => 'message'
)
))->

Related

Getting JSON data

Im working with an api which stores data into a JSON file. This data is gathered from a form that the users fill in my website. The way its inserted goes as follow:
$pers_payload = array(
'gender' => 'Unknown', //or Male / Female
'first_name' => $_POST['billing_first_name'],
'family_name' => $_POST ['billing_last_name'],
'email' => $_POST['billing_email'],
'linked_as_contact_to_organization' => array(
array(
'organization_id' => $organization_id, // add the person as a contact to the newly created organization
'work_email' => $_POST['billing_email'],
'work_phone' => $_POST['billing_phone']
)
),
'visiting_address' => array(
'country_code' => 'NL'
), // can be extented with other address data
'postal_address' => array(
'country_code' => $_POST['billing_country']
) // can be extented with other address data
);
And then:
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
Now instead of post i want to get the data. I tried getting data like this:
$SimplicateApi->makeApiCall('GET','/crm/organization?q[name]=*my name*');
I dont know if this is the right way, well it didn't work so obviously its not.
Anyways what im trying to achieve is with PHP i want to gather the name value of an existing person. this data is stored in /api/v2/crm/person.json
Api documentation (which i read but didn't understand to well) http://api.simplicate.nl/
It's been a while but i'm trying to answer all my open questions without an answer which i ended up solving on my own.
So for this.
You have to create a variable which makes the get request like this:
$test = $SimplicateApi->makeApiCall('GET','/crm/organization?q[name]=My name');
Now you can for example do a var_dump($test);
And as output you will get all the data inside
/crm/organization?q[name]=My name

Check if data exists

I'm working with an API.
With an array I collect data like this:
$org_payload = array(
'name' => $_POST['billing_company'],
'phone' => $_POST['billing_phone'],
'email' => $_POST['billing_email'],
'note' => $_POST['order_comments'],
'relation_type' => array(
'id'=>'relationtype:c1ec3ae77036842d' //provide the relationtypeid, f.e. relationtype:796ce0d318a2f5db515efc18bba82b90
),
'visiting_address' => array(
'country_code' => 'NL',
'line_1' => $_POST['billing_address_1'],
'postal_code' => $_POST['billing_postcode'],
'locality' => $_POST['billing_city'],
'country' => $_POST['billing_country']
), // can be extented with other address data
'postal_address' => array(
'country_code' => 'NL'
) // can be extented with other address data
);
At one point i send this data to the program i'm working with. I achieve this with this code:
$organization = $SimplicateApi->makeApiCall('POST','/crm/organization',json_encode($org_payload));
I gather this data from a form on my website. This data gets posted in the program.
I am trying to achieve that when data gathered from my form matches existing data in the program then don't add it. I would like a hint in the right direction for this, been looking on the internet without any luck.
What I would suggest is to have one extra call to the API.
Like you said in the comments - the company name and the phone number is unique.
If there is some call to get a user by those values and check what you got from the form, would be enough.
If they are unique - send them,
if not - show to the user or whatever you want to do here.
No need to keep one more database on your system as well.

CakePHP validation not detecting form field

I have a form where users can upload images, and I'm printing it to the page like so:
<?php echo $this->Form->label('file', 'Image file', array('class' => 'col-lg-1 control-label')); ?>
Then, in the model I'm setting up validation like so:
public $validate = array(
'file' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'You must select an image to upload'
),
'extension' => array(
'rule' => array('extension', array('png')),
'message' => 'Images must be in PNG format'
),
'size' => array(
'rule' => array('fileSize', '<', '1MB'),
'message' => 'Images must be no larger than 1MB'
),
'goodUpload' => array(
'rule' => 'uploadError',
'message' => 'Something went wrong with the upload, please try again'
)
)
);
However, Cake doesn't seem to be associating the form field with the validation rule, as if I select an image to upload I always get "You must select an image to upload" as a form error. I've made sure the form has enctype="multipart/form-data".
Is this happening because file isn't a database field? How can I make cake run some validation on file?
Edit: Here's my entire form, as requested: http://pastebin.com/SbSbtDP9
You can validate fields that are not in the database, long as you have the correct field name in the correct model.
From what I can see in your code it seems your outputting a label instead of an actual input, for the image upload I would try
echo $this->Form->create('Model', array('type'=>'file'));
echo $this->Form->input('file', array('type'=>'file'));
echo $this->Form->submit('Upload Image'):
echo $this->Form->end();
For the validation I would try something like with the rest of your validate options (size, etc...) CakePHP usually throws an error on notEmpty on File Uploads. So just checking for the extension type is usually good enough.
public $validate = array(
'file' => array(
'rule' => array(
'extension', array('jpeg', 'jpg')
'message' => 'You must supply a file.'
)
)
);
Majority of time in CakePHP for Image Uploading I resort to a plugin such as https://github.com/josegonzalez/cakephp-upload it does validation and upload handling all in one.
Managed to figure it out. Turns out having the notEmpty validation on a file fieldnever works, it always thinks there's nothing there and so always throws that validation message.
Worked around it by writing my own validation method.

Cakephp testing login

I want to test login function if it works propperly and only lets valid and active users in.
My user fixture contains:
array(
'password' => '*emptyPasswordHash*', // empty password
'username' => 'Lorem',
'balance' => 0,
'currency' => 'USD',
'id' => 1,
'user_group_id' => 3, //Customer
'active' => 1,
'hash' => 'LoremHash'
),
My test function looks like this:
function testLogin() {
//test valid login
$result = $this->testAction('/users/login', array(
'data' => array(
'User' => array(
'username' => 'Lorem',
'pass' => '',
'remember' => true
)),
'method' => 'post',
'return' => 'view'
));
debug($result);
}
The login form has 3 inputs: username, password and remember
I have set $this->Auth->autoRedirect = false; in the UsersController::beforeFilter and I am doing some cookie setting stuff
when I debug($this->data); in UsersController::login() it shows the exact same data when testing and when logging normaly. But while testing the login fails and I get $this->Auth->loginError message instead of a login.
How do I test login action propperly?
if you use cakes Auth component and dont hack it up you dont need to...
https://github.com/cakephp/cakephp/blob/master/cake/tests/cases/libs/controller/components/auth.test.php#L545
and if you really want to, look at how the pro's do it :)
Did you also set your custom function, like described in The CakePHP manual:
Normally, the AuthComponent will
attempt to verify that the login
credentials you've entered are
accurate by comparing them to what's
been stored in your user model.
However, there are times where you
might want to do some additional work
in determining proper credentials. By
setting this variable to one of
several different values, you can do
different things.

Validate field in model with no table (CakePHP)

I've got a model in CakePHP that doesn't have a table, called Upload. I've got a validation in this Model for a field called source_id.
I've got a form that builds a nice looking $this-data, giving me a well formated set, including:
$this->data['Upload']['source_id']
However, the validation rule I have set doesn't seem to run at all. I copied this validation rule from another model where it does work, so I'm confident that it works:
var $validate = array(
'source_id' => array(
rule' => 'numeric',
'required' => true,
'allowEmpty' => false,
'message' => 'Error!.'
)
);
Can you not validate fields for a model that lacks a database table?
The form uses the Upload model, and submits to another controller action method.
CakePHP 1.2, PHP/MySQL 5, XAMPP.
I'm dumb. You have to trigger a validation check, either with a save() or
$this->Upload->set($this->data);
$this->Upload->validates();
Working now.
You can also fake the database structure by setting the $_schema array, like so:
var $useTable = false;
var $_schema = array(
'name' =>array('type'=>'string', 'length'=>100),
'email' =>array('type'=>'string', 'length'=>255),
'phone' =>array('type'=>'string', 'length'=>20),
'subject' =>array('type'=>'string', 'length'=>255),
'message' =>array('type'=>'text')
);

Categories