Unit testing cakephp models - php

So I'm having trouble with unit testing CakePHP models. Simple stuff like writing tests that my validation rules are catching errors etc.
To begin with, I have a model called NewsItem. Its defined in my MySQL database using the following schema
CREATE TABLE news_items (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(140) NOT NULL,
body TEXT NOT NULL,
modified DATETIME DEFAULT NULL,
created DATETIME DEFAULT NULL
);
The model is following
<?php
class NewsItem extends AppModel {
var $name = 'NewsItem';
var $validate = array(
'title' => array(
'titleRule-1' => array(
'rule' => array('maxLength', 140),
'message' => 'News item\'s title\'s length exceeds limit of 140 characters'
),
'titleRule-2' => array(
'rule' => 'alphaNumeric',
'required' => true,
'message' => 'Cannot save news item without a title'
)
)
);
}
?>
And in my test case I have
// Validation lets All data good through
function testValidationAllowsNormalData()
{
$this->assertTrue($this->NewsItem->save(array('NewsItem' => array('title' => 'A news item', 'body' => 'Some news'))));
}
However I'm my test case fails. Any ideas, suggestions, comments?

The alphaNumeric validation rule only allows, well, alphanumeric characters, i.e. no spaces. So your test fails correctly.

I'm not hugely familiar with cakePHP, but wouldn't save attempt to persist to the database? That should not be in a unit test if that is the case...

Related

Null values in FactoryMuffin define

I am using PhPUnit, FactoryMuffin and Faker for testing in Laravel, with a PostgreSQL db. In the previous version of FactoryMuffin (Zizaco\FactoryMuff) I could assign null values to columns both in the static factory array and when calling FactoryMuff::create.
However this no longer works - if I use the following define:
FactoryMuffin::define('MyModel', array(
'name' => 'word',
'empty' => null,
'another' => 'word'
));
when I call FactoryMuffin::create instead of passing NULL to the SQL INSERT statement it leaves the value blank, so I get:
INSERT INTO my_table ("name", "empty", "another") VALUES ('Ralph', , 'Someone');
which PGSQL doesn't allow. The same thing happens using
FactoryMuffin::create('MyModel', array('empty' => null));
Any ideas how to get round this, beyond instantiating the model and then assigning null to the field?
Since FactoryMuffin 2.1 (and 3.*) you can take advantage of the callback functionality, e.g.:
FactoryMuffin::define('MyModel', array(
'name' => 'word',
'empty' => null,
'another' => 'word'
))->setCallback(function ($object, $saved) {
$object->empty = null;
});
In FactoryMuffin 2.1 the callback is set as the third parameter of the define:
FactoryMuffin::define('MyModel', array(
'name' => 'word',
'empty' => null,
'another' => 'word'
), function ($object, $saved) {
$object->empty = null;
});

Yii migrations and custom Abstract Data Types

I'm working on a project and using Yii's Migration feature to keep the different production and test systems in sync. I must say i love this tool.
My question is there a way to create custom Abstract Data Types?
I know Yii's migration feature is made to allow table creation in multiple DBMS systems but my site is limited to MySQL so that should help things along.
What I would like to do is:
$this->createTable('test_table', array(
'key'=>'pk',
'active_YN'=>'yn',
));
instead of:
$this->createTable('test_table', array(
'key'=>'pk',
'active_YN'=>'TINYINT(1) NOT NULL DEFAULT \'1\'',
));
Im guessing that i would have to extend CDbMigration, possibly with a behavior?
Many Thanks.
If you still want to make the trick, here is what you can try.
MySQL uses the driver CMysqlSchema by default. You need to extend this class with your custom abstract column types.
class MyCustomMysqlSchema extends CMysqlSchema
{
/**
* #var array the abstract column types mapped to physical column types.
*/
public $columnTypes=array(
'pk' => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY',
'string' => 'varchar(255)',
'text' => 'text',
'integer' => 'int(11)',
'float' => 'float',
'decimal' => 'decimal',
'datetime' => 'datetime',
'timestamp' => 'timestamp',
'time' => 'time',
'date' => 'date',
'binary' => 'blob',
'boolean' => 'tinyint(1)',
'money' => 'decimal(19,4)',
// add your custom abstract column types here
'yn' => 'tinyint(1) UNSIGNED NOT NULL DEFAULT 1',
);
}
You need your connection to use this new driver. Modify your db configuration as follow.
'db'=>array(
// your initial db configuration here
'driverMap'=>array('mysql'=>'MyCustomMysqlSchema'),
),

AllowEmpty vs NotEmpty

New to CakePHP here - I'm going through the documentation on the site, trying to muster up some basic data validation for a model I'm creating. This will likely be the first of many questions I have about CakePHP.
In the CakePHP Book, the validation rules seem to specify two different methods for making sure that a field isn't empty - AllowEmpty, and NotEmpty.
Question - is there a tangible difference between these two? CakePHP states that validation rules should occur in your model or controller - is one better suited for a model, and the other for a controller? The Book doesn't say anything about this. I'm guessing that one is an older method that's simply still around?
What gives? Should I use a specific one, or both, or does it not matter?
Edit: I decided to check the CakePHP 1.3 class documentation for it (to check the default value of the allowEmpty attribute), but it doesn't even show up. It's not in the source code either...is there something I'm missing?
Welcome to Cake. I hope you enjoy it.
This is definitely one of the stranger aspects of Cake.
notEmpty is a rule in and of itself. You can define it in your $validation attribute. You can assign a message for when this validation fails. You can treat this as if it is any other validation rule.
allowEmpty is an option of another validation rule, normally not notEmpty. It is not a validation rule in-and-of-itself. This would allow, for example, you to define that a varchar field allows an empty string, '', or a string with no more than 20 characters.
Edit:
Here's some code
// model validation using 'notEmpty'
$validation = array(
'fieldName' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'This value may not be left empty!'
),
... // other rules can go here
),
... // other fieldName can go here
);
// model validation using 'allowEmpty' to create an optional field
$validation = array(
'fieldName' => array(
'maxLength' => array(
'rule' => array('maxLength', 20),
'message' => 'This field may only contain 20 characters!',
'allowEmpty' => true // we'll also accept an empty string
),
... // other rules can go here
)
... // other fieldName can go here
);
I found a case where I had to use 'allowEmpty' => false instead of rule => 'notEmpty'. I had a form with an upload input (type='file') that had a validation rule of notEmpty, and it kept failing validation, even though the debugger showed the file[] array loaded. When I removed the 'notEmpty' rule and set allowEmpty => false, it worked, throwing an error when no file was chosen and accepting it when one was selected.
It must have something to do with the value being an array rather than a text value.
Its very simply to make server side validation in cakephp
Here is code for both validation (noEmpty, maxlength) for the same field.
'fieldName' => array(
'rule' => array('maxLength', 20),
'message' => 'fieldName should be less than 20 characters',
'allowEmpty' => true
),
'fieldName' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Please enter field name',
),
),

Using MeioUpload with CakePHP 1.3

I'm trying to implement some fairly simple image uploading using the MeioUpload behaviour in CakePHP 1.3, but I can't for the life of me get it to work. When I try to save $this->data in my controller, it tries to save a regular file array (for lack of a better word) rather than just the filename.
Here's what I'm doing:
I've put meio_upload.php into /app/models/behaviors
In my model, I'm doing the following:
var $actsAs = array(
'MeioUpload.MeioUpload' => array(
'filename' => array(
'dir' => 'img{DS}upload{DS}brawlers',
'allowedMime' => array('image/png'),
'allowedExt' => array('.png', '.PNG'),
'zoomCrop' => false,
'thumbsizes' => array(
'normal' => array(
'width' => 150,
'height' => 150
)
),
'default' => 'default.png',
'length' => array(
'minWidth' => 100,
'minHeight' => 100,
'maxWidth' => 150,
'maxHeight' => 150
)
)
)
);
In my view, I've got the following form:
<?php
echo $this->Form->create('Brawler', array('type' => 'file'));
echo $this->Form->input('name', array(
'label' => 'Name',
'maxLength' => '45'
)
);
echo $this->Form->input('comment', array(
'label' => 'Description',
'rows' => '3'
)
);
echo $this->Form->input('author', array(
'label' => 'Your name)',
'maxLength' => '45'
)
);
echo $this->Form->input('email', array(
'label' => 'Email (will not be shown)',
'maxLength' => '45'
)
);
echo $this->Form->input(
'filename',
array(
'between'=>'<br />',
'type'=>'file',
'label' => 'Image (Max 2mb, 150x150 pixels, .png)'
)
);
echo $this->Form->end('Submit');
?>
And finally my add action in the associated controller looks like this:
function add() {
if (!empty($this->data)) {
$this->Brawler->create();
if($this->Brawler->save($this->data)) {
$this->Session->setFlash('The brawler has been saved', true);
$this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash('The Brawler could not be saved. Please try again.', true);
debug($this->data);
debug($this->validationErrors);
die();
//$this->redirect(array('action'=>'add'));
}
}
}
For posterity, here's my table design:
delimiter $$
CREATE TABLE `brawlers` (
`id` int(11) NOT NULL,
`name` varchar(45) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`comment` text,
`email` varchar(45) NOT NULL,
`author` varchar(45) NOT NULL,
`filename` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
) ENGINE=MyISAM DEFAULT CHARSET=utf8$$
When I try to submit my form, this is the output I get:
app/controllers/brawlers_controller.php (line 37)
Array
(
[Brawler] => Array
(
[name] => Viking
[comment] => Herp. This is a description.
[author] => Me
[email] => me#gmail.com
[filename] => Array
(
[name] => 5.png
[type] => image/png
[tmp_name] => /storage/configuration/upload_tmp_dir/phpEF2okD
[error] => 0
[size] => 15863
)
)
)
app/controllers/brawlers_controller.php (line 37)
Obviously, this fails when it tries to save an array to the filename field. The image is never saved in the specified upload directory either. It seems like the meioupload behavior is never actually used. How can I verify this?
You'll have to excuse the mass of code that I've posted, but I figure it's better that I show you everything than to have you guess at what I may be doing. If someone can spot the error, that would save me many hours of pulling my hair.
Hey i have answer for ur Query
That is u have to make anther variable Say
$d1 and copy $this->date array into that array like below
$d1['brawlers']['name'] = $this->data['brawlers']['name'];
all ur variables
$d1['brawlers']['filename'] = $this->data['brawlers']['filename']['name'];
and then save it like $this->brawlers->save($d1);
I'm NOT PHP guy, but I had to use the CakePHP and Meio.Upload some time ago.
AFAIK you need 4 fields in your database for image:
filename (you can change name of this one in
settings)
dir
mimetype
filesize
Judging on your error and db schema I would say you are missing some of the fields.
Edit:
See the documentation
I had the same problem, the MeioUpload version your are using is incompatible with your version of Cake. I ended using version 2.3 of MeioUpload.

Zend Form Edit and Zend_Validate_Db_NoRecordExists

I am slowly building up my Zend skills by building some utility websites for my own use. I have been using Zend Forms and Form validation and so far have been happy that I have been understanding the Zend way of doing things. However I am a bit confused with how to use Zend_Validate_Db_NoRecordExists() in the context of an edit form and a field that maps to database column that has to be unique.
For example using this simple table
TABLE Test
(
ID INT AUTO_INCREMENT,
Data INT UNIQUE
);
If I was simply adding a new row to the Table Test, I could add a validator to the Zend Form element for the Data field as such:
$data = new Zend_Form_Element_Text('Data');
$data->addValidator( new Zend_Validate_Db_NoRecordExists('Test', 'Data') )
At form validation this validator will check that the contents of the Data element does not already exist in the table. Thus the insert into Test can go ahead without violating the Data fields UNIQUE qualifier.
However the situation is different when editing an existing row of the Test table. In that case the validator needs to check that the element value meets one of two mutually exclusive conditions conditions:
The user has changed the element value, and the new value does not currently
exist in the table.
The user has Not changed the element value. Thus the value does currently exist in the table (and this is OK).
The Zend Validation Docs talk about adding a parameter to the NoRecordExists() validator for the purpose of excluding records from the validation process. The idea being to "validate the table looking for any matching rows, but ignore any hits where the a field has this specific value". Such a use case is what is needed for the validating the element when editing a table. The pseudo code to do this in 1.9 is like so (actually I got this from the 1.9 source code - I think the current docs may be wrong):
$data = new Zend_Form_Element_Text('Data');
$data->addValidator( new Zend_Validate_Db_NoRecordExists('Test', 'Data',
array ('field'=>'Data', 'Value'=> $Value) );
The problem is that the value that is to be excluded ($Value) is bound to the validator at the time it is instantiated (also when the form is instantiated). But when the form is editing a record, that value needs to be bound to the contents of the $data field when the form was initially populated with data - IE the Data value initially read from the Test table row. But in typical Zend patterns a form is instantiated and populated in two separate steps which precludes binding the exclude value to the desired element value.
The following Zend psuedo code marks where I would like the binding of $Value to the NoRecordExists() validator to occur (and note that this is a common Zend controller pattern):
$form = new Form()
if (is Post) {
$formData = GetPostData()
if ($form->isValid($formData)) {
Update Table with $formData
Redirect out of here
} else {
$form->populate($formData)
}
} else {
$RowData = Get Data from Table
$form->populate($RowData) <=== This is where I want ('value' => $Value) bound
}
I could sub-class Zend_Form and override the populate() method to do a one-shot insertion of the NoRecordExists() validator on initial form population, but that seems like a huge hack to me. So I wanted to know what other people think and is there some pattern already written down that solves this problem?
Edit 2009-02-04
I've been thinking that the only decent solution to this problem is to write a custom validator and forget about the Zend version. My form has the record ID as hidden field, so that given the table and column names I could craft some SQL to test for uniqueness and exclude the row with an ID of such an such. Of course this started me thinking about how I would be tying the form to the dB layer that the Model is supposed to hide!
This is how it's done:
I your FORM, you add this validator (for example email field):
$email->addValidator('Db_NoRecordExists', true, array('table' => 'user', 'field' => 'email'));
Don't add custom error message for this since after that it didn't work for me, e.g.:
$email->getValidator('Db_NoRecordExists')->setMessage('This email is already registered.');
In your Controller add this:
/* Don't check for Db_NoRecordExists if editing the same field */
$form->getElement('email')
->addValidator('Db_NoRecordExists',
false,
array('table' => 'user',
'field' => 'email',
'exclude' => array ('field' => 'id', 'value' => $this->request->get('id'))));
And after this you do verifications, e.g.:
if ($this->getRequest()->isPost())
{
if($form->isValid($this->getRequest()->getPost()))
{
....
That's it!
This will also work :
$this->addElement('text', 'email', array(
'label' => 'Your email address:',
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
'EmailAddress',
array('Db_NoRecordExists', true, array(
'table' => 'guestbook',
'field' => 'email',
'messages' => array(
'recordFound' => 'Email already taken'
)
)
)
)
));
After reviewing the overwhelming response I've decided that I'm going with a custom validator
Look at this one:
Answer raised by me and well-solved by Dickie
private $_id;
public function setId($id=null)
{
$this->_id=$id;
}
public function init()
{
.....
if(isset($this->_id)){
$email->addValidator('Db_NoRecordExists', false, array('table' => 'user', 'field' => 'email','exclude' => array ('field' => 'id', 'value' => $this->_id) ));
$email->getValidator('Db_NoRecordExists')->setMessage('This email is already registered.');
}
Now u can use:
$form = new Form_Test(array('id'=>$id));
You could just call $form->getElement('input')->removeValidator('Zend_Validator_Db_NoRecordExists'); instead of supplying the exclusion.
I have just tried this example for email address uniqueness and it works perfectly with below stuffs :
1] In my form:
// Add an email element
$this->addElement('text', 'email', array(
'label' => 'Email :',
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
'EmailAddress',
)
));
Here's something special that I needed to add for unique email address to work:
$email = new Zend_Form_Element_Text('email');
$email->addValidator('Db_NoRecordExists', true, array('table' => 'guestbook', 'field' => 'email'));
2] In my controller:
$form->getElement('email')
->addValidator('Db_NoRecordExists',
false,
array('table' => 'guestbook',
'field' => 'email',
'exclude' => array ('field' => 'id', 'value' => $request->get('id'))));
if ($this->getRequest()->isPost()) {
if ($form->isValid($request->getPost())) {
Hope it helps you people !
Thanks

Categories