| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426 |
- <?php
- /**
- * ModelValidationTest file
- *
- * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
- * @package Cake.Test.Case.Model
- * @since CakePHP(tm) v 1.2.0.4206
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
- /**
- * ModelValidationTest
- *
- * @package Cake.Test.Case.Model
- */
- class ModelValidationTest extends BaseModelTest {
- /**
- * override locale to the default (eng).
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- Configure::write('Config.language', 'eng');
- }
- /**
- * Tests validation parameter order in custom validation methods
- *
- * @return void
- */
- public function testValidationParams() {
- $TestModel = new ValidationTest1();
- $TestModel->validate['title'] = array(
- 'rule' => 'customValidatorWithParams',
- 'required' => true
- );
- $TestModel->create(array('title' => 'foo'));
- $TestModel->invalidFields();
- $expected = array(
- 'data' => array(
- 'title' => 'foo'
- ),
- 'validator' => array(
- 'rule' => 'customValidatorWithParams',
- 'on' => null,
- 'last' => true,
- 'allowEmpty' => false,
- 'required' => true,
- 'message' => null
- ),
- 'or' => true,
- 'ignoreOnSame' => 'id'
- );
- $this->assertEquals($expected, $TestModel->validatorParams);
- $TestModel->validate['title'] = array(
- 'rule' => 'customValidatorWithMessage',
- 'required' => true
- );
- $expected = array(
- 'title' => array('This field will *never* validate! Muhahaha!')
- );
- $this->assertEquals($expected, $TestModel->invalidFields());
- $TestModel->validate['title'] = array(
- 'rule' => array('customValidatorWithSixParams', 'one', 'two', null, 'four'),
- 'required' => true
- );
- $TestModel->create(array('title' => 'foo'));
- $TestModel->invalidFields();
- $expected = array(
- 'data' => array(
- 'title' => 'foo'
- ),
- 'one' => 'one',
- 'two' => 'two',
- 'three' => null,
- 'four' => 'four',
- 'five' => array(
- 'rule' => array(1 => 'one', 2 => 'two', 3 => null, 4 => 'four'),
- 'on' => null,
- 'last' => true,
- 'allowEmpty' => false,
- 'required' => true,
- 'message' => null
- ),
- 'six' => 6
- );
- $this->assertEquals($expected, $TestModel->validatorParams);
- $TestModel->validate['title'] = array(
- 'rule' => array('customValidatorWithSixParams', 'one', array('two'), null, 'four', array('five' => 5)),
- 'required' => true
- );
- $TestModel->create(array('title' => 'foo'));
- $TestModel->invalidFields();
- $expected = array(
- 'data' => array(
- 'title' => 'foo'
- ),
- 'one' => 'one',
- 'two' => array('two'),
- 'three' => null,
- 'four' => 'four',
- 'five' => array('five' => 5),
- 'six' => array(
- 'rule' => array(1 => 'one', 2 => array('two'), 3 => null, 4 => 'four', 5 => array('five' => 5)),
- 'on' => null,
- 'last' => true,
- 'allowEmpty' => false,
- 'required' => true,
- 'message' => null
- )
- );
- $this->assertEquals($expected, $TestModel->validatorParams);
- }
- /**
- * Tests validation parameter fieldList in invalidFields
- *
- * @return void
- */
- public function testInvalidFieldsWithFieldListParams() {
- $TestModel = new ValidationTest1();
- $TestModel->validate = $validate = array(
- 'title' => array(
- 'rule' => 'alphaNumeric',
- 'required' => true
- ),
- 'name' => array(
- 'rule' => 'alphaNumeric',
- 'required' => true
- ));
- $TestModel->set(array('title' => '$$', 'name' => '##'));
- $TestModel->invalidFields(array('fieldList' => array('title')));
- $expected = array(
- 'title' => array('This field cannot be left blank')
- );
- $this->assertEquals($expected, $TestModel->validationErrors);
- $TestModel->validationErrors = array();
- $TestModel->invalidFields(array('fieldList' => array('name')));
- $expected = array(
- 'name' => array('This field cannot be left blank')
- );
- $this->assertEquals($expected, $TestModel->validationErrors);
- $TestModel->validationErrors = array();
- $TestModel->invalidFields(array('fieldList' => array('name', 'title')));
- $expected = array(
- 'name' => array('This field cannot be left blank'),
- 'title' => array('This field cannot be left blank')
- );
- $this->assertEquals($expected, $TestModel->validationErrors);
- $TestModel->validationErrors = array();
- $TestModel->whitelist = array('name');
- $TestModel->invalidFields();
- $expected = array('name' => array('This field cannot be left blank'));
- $this->assertEquals($expected, $TestModel->validationErrors);
- $this->assertEquals($TestModel->validate, $validate);
- }
- /**
- * Test that invalidFields() integrates well with save(). And that fieldList can be an empty type.
- *
- * @return void
- */
- public function testInvalidFieldsWhitelist() {
- $TestModel = new ValidationTest1();
- $TestModel->validate = array(
- 'title' => array(
- 'rule' => 'alphaNumeric',
- 'required' => true
- ),
- 'name' => array(
- 'rule' => 'alphaNumeric',
- 'required' => true
- ));
- $TestModel->whitelist = array('name');
- $TestModel->save(array('name' => '#$$#', 'title' => '$$$$'));
- $expected = array('name' => array('This field cannot be left blank'));
- $this->assertEquals($expected, $TestModel->validationErrors);
- }
- /**
- * testValidates method
- *
- * @return void
- */
- public function testValidates() {
- $TestModel = new TestValidate();
- $TestModel->validate = array(
- 'user_id' => 'numeric',
- 'title' => array('allowEmpty' => false, 'rule' => 'notEmpty'),
- 'body' => 'notEmpty'
- );
- $data = array('TestValidate' => array(
- 'user_id' => '1',
- 'title' => '',
- 'body' => 'body'
- ));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertFalse($result);
- $data = array('TestValidate' => array(
- 'user_id' => '1',
- 'title' => 'title',
- 'body' => 'body'
- ));
- $result = $TestModel->create($data) && $TestModel->validates();
- $this->assertTrue($result);
- $data = array('TestValidate' => array(
- 'user_id' => '1',
- 'title' => '0',
- 'body' => 'body'
- ));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertTrue($result);
- $data = array('TestValidate' => array(
- 'user_id' => '1',
- 'title' => 0,
- 'body' => 'body'
- ));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertTrue($result);
- $TestModel->validate['modified'] = array('allowEmpty' => true, 'rule' => 'date');
- $data = array('TestValidate' => array(
- 'user_id' => '1',
- 'title' => 0,
- 'body' => 'body',
- 'modified' => ''
- ));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertTrue($result);
- $data = array('TestValidate' => array(
- 'user_id' => '1',
- 'title' => 0,
- 'body' => 'body',
- 'modified' => '2007-05-01'
- ));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertTrue($result);
- $data = array('TestValidate' => array(
- 'user_id' => '1',
- 'title' => 0,
- 'body' => 'body',
- 'modified' => 'invalid-date-here'
- ));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertFalse($result);
- $data = array('TestValidate' => array(
- 'user_id' => '1',
- 'title' => 0,
- 'body' => 'body',
- 'modified' => 0
- ));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertFalse($result);
- $data = array('TestValidate' => array(
- 'user_id' => '1',
- 'title' => 0,
- 'body' => 'body',
- 'modified' => '0'
- ));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertFalse($result);
- $TestModel->validate['modified'] = array('allowEmpty' => false, 'rule' => 'date');
- $data = array('TestValidate' => array('modified' => null));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertFalse($result);
- $data = array('TestValidate' => array('modified' => false));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertFalse($result);
- $data = array('TestValidate' => array('modified' => ''));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertFalse($result);
- $data = array('TestValidate' => array(
- 'modified' => '2007-05-01'
- ));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertTrue($result);
- $TestModel->validate['slug'] = array('allowEmpty' => false, 'rule' => array('maxLength', 45));
- $data = array('TestValidate' => array(
- 'user_id' => '1',
- 'title' => 0,
- 'body' => 'body',
- 'slug' => ''
- ));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertFalse($result);
- $data = array('TestValidate' => array(
- 'user_id' => '1',
- 'title' => 0,
- 'body' => 'body',
- 'slug' => 'slug-right-here'
- ));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertTrue($result);
- $data = array('TestValidate' => array(
- 'user_id' => '1',
- 'title' => 0,
- 'body' => 'body',
- 'slug' => 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'
- ));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertFalse($result);
- $TestModel->validate = array(
- 'number' => array(
- 'rule' => 'validateNumber',
- 'min' => 3,
- 'max' => 5
- ),
- 'title' => array(
- 'allowEmpty' => false,
- 'rule' => 'notEmpty'
- ));
- $data = array('TestValidate' => array(
- 'title' => 'title',
- 'number' => '0'
- ));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertFalse($result);
- $data = array('TestValidate' => array(
- 'title' => 'title',
- 'number' => 0
- ));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertFalse($result);
- $data = array('TestValidate' => array(
- 'title' => 'title',
- 'number' => '3'
- ));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertTrue($result);
- $data = array('TestValidate' => array(
- 'title' => 'title',
- 'number' => 3
- ));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertTrue($result);
- $TestModel->validate = array(
- 'number' => array(
- 'rule' => 'validateNumber',
- 'min' => 5,
- 'max' => 10
- ),
- 'title' => array(
- 'allowEmpty' => false,
- 'rule' => 'notEmpty'
- ));
- $data = array('TestValidate' => array(
- 'title' => 'title',
- 'number' => '3'
- ));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertFalse($result);
- $data = array('TestValidate' => array(
- 'title' => 'title',
- 'number' => 3
- ));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertFalse($result);
- $TestModel->validate = array(
- 'title' => array(
- 'allowEmpty' => false,
- 'rule' => 'validateTitle'
- ));
- $data = array('TestValidate' => array('title' => ''));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertFalse($result);
- $data = array('TestValidate' => array('title' => 'new title'));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertFalse($result);
- $data = array('TestValidate' => array('title' => 'title-new'));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertTrue($result);
- $TestModel->validate = array('title' => array(
- 'allowEmpty' => true,
- 'rule' => 'validateTitle'
- ));
- $data = array('TestValidate' => array('title' => ''));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertTrue($result);
- $TestModel->validate = array(
- 'title' => array(
- 'length' => array(
- 'allowEmpty' => true,
- 'rule' => array('maxLength', 10)
- )));
- $data = array('TestValidate' => array('title' => ''));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertTrue($result);
- $TestModel->validate = array(
- 'title' => array(
- 'rule' => array('userDefined', 'Article', 'titleDuplicate')
- ));
- $data = array('TestValidate' => array('title' => 'My Article Title'));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertFalse($result);
- $data = array('TestValidate' => array(
- 'title' => 'My Article With a Different Title'
- ));
- $result = $TestModel->create($data);
- $this->assertEquals($data, $result);
- $result = $TestModel->validates();
- $this->assertTrue($result);
- $TestModel->validate = array(
- 'title' => array(
- 'tooShort' => array('rule' => array('minLength', 50)),
- 'onlyLetters' => array('rule' => '/^[a-z]+$/i')
- ),
- );
- $data = array('TestValidate' => array(
- 'title' => 'I am a short string'
- ));
- $TestModel->create($data);
- $result = $TestModel->validates();
- $this->assertFalse($result);
- $result = $TestModel->validationErrors;
- $expected = array(
- 'title' => array('tooShort')
- );
- $this->assertEquals($expected, $result);
- $TestModel->validate = array(
- 'title' => array(
- 'tooShort' => array(
- 'rule' => array('minLength', 50),
- 'last' => false
- ),
- 'onlyLetters' => array('rule' => '/^[a-z]+$/i')
- ),
- );
- $data = array('TestValidate' => array(
- 'title' => 'I am a short string'
- ));
- $TestModel->create($data);
- $result = $TestModel->validates();
- $this->assertFalse($result);
- $result = $TestModel->validationErrors;
- $expected = array(
- 'title' => array('tooShort', 'onlyLetters')
- );
- $this->assertEquals($expected, $result);
- $result = $TestModel->validationErrors;
- $this->assertEquals($expected, $result);
- }
- /**
- * test that validates() checks all the 'with' associations as well for validation
- * as this can cause partial/wrong data insertion.
- *
- * @return void
- */
- public function testValidatesWithAssociations() {
- $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
- $data = array(
- 'Something' => array(
- 'id' => 5,
- 'title' => 'Extra Fields',
- 'body' => 'Extra Fields Body',
- 'published' => '1'
- ),
- 'SomethingElse' => array(
- array('something_else_id' => 1, 'doomed' => '')
- )
- );
- $Something = new Something();
- $JoinThing = $Something->JoinThing;
- $JoinThing->validate = array('doomed' => array('rule' => 'notEmpty'));
- $expectedError = array('doomed' => array('This field cannot be left blank'));
- $Something->create();
- $result = $Something->save($data);
- $this->assertFalse($result, 'Save occurred even when with models failed. %s');
- $this->assertEquals($expectedError, $JoinThing->validationErrors);
- $count = $Something->find('count', array('conditions' => array('Something.id' => $data['Something']['id'])));
- $this->assertSame(0, $count);
- $data = array(
- 'Something' => array(
- 'id' => 5,
- 'title' => 'Extra Fields',
- 'body' => 'Extra Fields Body',
- 'published' => '1'
- ),
- 'SomethingElse' => array(
- array('something_else_id' => 1, 'doomed' => 1),
- array('something_else_id' => 1, 'doomed' => '')
- )
- );
- $Something->create();
- $result = $Something->save($data);
- $this->assertFalse($result, 'Save occurred even when with models failed. %s');
- $joinRecords = $JoinThing->find('count', array(
- 'conditions' => array('JoinThing.something_id' => $data['Something']['id'])
- ));
- $this->assertEquals(0, $joinRecords, 'Records were saved on the join table. %s');
- }
- /**
- * Test that if a behavior modifies the model's whitelist validation gets triggered
- * properly for those fields.
- *
- * @return void
- */
- public function testValidateWithFieldListAndBehavior() {
- $TestModel = new ValidationTest1();
- $TestModel->validate = array(
- 'title' => array(
- 'rule' => 'notEmpty',
- ),
- 'name' => array(
- 'rule' => 'notEmpty',
- ));
- $TestModel->Behaviors->attach('ValidationRule', array('fields' => array('name')));
- $data = array(
- 'title' => '',
- 'name' => '',
- );
- $result = $TestModel->save($data, array('fieldList' => array('title')));
- $this->assertFalse($result);
- $expected = array('title' => array('This field cannot be left blank'), 'name' => array('This field cannot be left blank'));
- $this->assertEquals($expected, $TestModel->validationErrors);
- }
- /**
- * test that saveAll and with models with validation interact well
- *
- * @return void
- */
- public function testValidatesWithModelsAndSaveAll() {
- $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
- $data = array(
- 'Something' => array(
- 'id' => 5,
- 'title' => 'Extra Fields',
- 'body' => 'Extra Fields Body',
- 'published' => '1'
- ),
- 'SomethingElse' => array(
- array('something_else_id' => 1, 'doomed' => '')
- )
- );
- $Something = new Something();
- $JoinThing = $Something->JoinThing;
- $JoinThing->validate = array('doomed' => array('rule' => 'notEmpty'));
- $expectedError = array('doomed' => array('This field cannot be left blank'));
- $Something->create();
- $result = $Something->saveAll($data, array('validate' => 'only'));
- $this->assertFalse($result);
- $result = $Something->validateAssociated($data);
- $this->assertFalse($result);
- $this->assertEquals($expectedError, $JoinThing->validationErrors);
- $result = $Something->validator()->validateAssociated($data);
- $this->assertFalse($result);
- $Something->create();
- $result = $Something->saveAll($data, array('validate' => 'first'));
- $this->assertFalse($result);
- $this->assertEquals($expectedError, $JoinThing->validationErrors);
- $count = $Something->find('count', array('conditions' => array('Something.id' => $data['Something']['id'])));
- $this->assertSame(0, $count);
- $joinRecords = $JoinThing->find('count', array(
- 'conditions' => array('JoinThing.something_id' => $data['Something']['id'])
- ));
- $this->assertEquals(0, $joinRecords, 'Records were saved on the join table. %s');
- }
- /**
- * test that saveAll and with models at initial insert (no id has set yet)
- * with validation interact well
- *
- * @return void
- */
- public function testValidatesWithModelsAndSaveAllWithoutId() {
- $this->loadFixtures('Post', 'Author');
- $data = array(
- 'Author' => array(
- 'name' => 'Foo Bar',
- ),
- 'Post' => array(
- array('title' => 'Hello'),
- array('title' => 'World'),
- )
- );
- $Author = new Author();
- $Post = $Author->Post;
- $Post->validate = array('author_id' => array('rule' => 'numeric'));
- $Author->create();
- $result = $Author->saveAll($data, array('validate' => 'only'));
- $this->assertTrue($result);
- $result = $Author->validateAssociated($data);
- $this->assertTrue($result);
- $this->assertTrue($result);
- $Author->create();
- $result = $Author->saveAll($data, array('validate' => 'first'));
- $this->assertTrue($result);
- $this->assertNotNull($Author->id);
- $id = $Author->id;
- $count = $Author->find('count', array('conditions' => array('Author.id' => $id)));
- $this->assertSame(1, $count);
- $count = $Post->find('count', array(
- 'conditions' => array('Post.author_id' => $id)
- ));
- $this->assertEquals($count, count($data['Post']));
- }
- /**
- * Test that missing validation methods trigger errors in development mode.
- * Helps to make development easier.
- *
- * @expectedException PHPUnit_Framework_Error
- * @return void
- */
- public function testMissingValidationErrorTriggering() {
- Configure::write('debug', 2);
- $TestModel = new ValidationTest1();
- $TestModel->create(array('title' => 'foo'));
- $TestModel->validate = array(
- 'title' => array(
- 'rule' => array('thisOneBringsThePain'),
- 'required' => true
- )
- );
- $TestModel->invalidFields(array('fieldList' => array('title')));
- }
- /**
- * Test placeholder replacement when validation message is an array
- *
- * @return void
- */
- public function testValidationMessageAsArray() {
- $TestModel = new ValidationTest1();
- $TestModel->validate = array(
- 'title' => array(
- 'minLength' => array(
- 'rule' => array('minLength', 6),
- 'required' => true,
- 'message' => 'Minimum length allowed is %d chars',
- 'last' => false
- ),
- 'between' => array(
- 'rule' => array('between', 5, 15),
- 'message' => array('You may enter up to %s chars (minimum is %s chars)', 14, 6)
- )
- )
- );
- $TestModel->create();
- $expected = array(
- 'title' => array(
- 'Minimum length allowed is 6 chars',
- )
- );
- $TestModel->invalidFields();
- $this->assertEquals($expected, $TestModel->validationErrors);
- $TestModel->create(array('title' => 'foo'));
- $expected = array(
- 'title' => array(
- 'Minimum length allowed is 6 chars',
- 'You may enter up to 14 chars (minimum is 6 chars)'
- )
- );
- $TestModel->invalidFields();
- $this->assertEquals($expected, $TestModel->validationErrors);
- }
- /**
- * Test validation message translation
- *
- * @return void
- */
- public function testValidationMessageTranslation() {
- $lang = Configure::read('Config.language');
- Configure::write('Config.language', 'en');
- App::build(array(
- 'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS),
- ), App::RESET);
- $TestModel = new ValidationTest1();
- $TestModel->validationDomain = 'validation_messages';
- $TestModel->validate = array(
- 'title' => array(
- array(
- 'rule' => array('customValidationMethod', 'arg1'),
- 'required' => true,
- 'message' => 'Validation failed: %s'
- )
- )
- );
- $TestModel->create();
- $expected = array(
- 'title' => array(
- 'Translated validation failed: Translated arg1',
- )
- );
- $TestModel->invalidFields();
- $this->assertEquals($expected, $TestModel->validationErrors);
- $TestModel->validationDomain = 'default';
- Configure::write('Config.language', $lang);
- App::build();
- }
- /**
- * Test for 'on' => [create|update] in validation rules.
- *
- * @return void
- */
- public function testStateValidation() {
- $this->loadFixtures('Article');
- $Article = new Article();
- $data = array(
- 'Article' => array(
- 'title' => '',
- 'body' => 'Extra Fields Body',
- 'published' => '1'
- )
- );
- $Article->validate = array(
- 'title' => array(
- 'notempty' => array(
- 'rule' => 'notEmpty',
- 'on' => 'create'
- )
- )
- );
- $Article->create($data);
- $this->assertFalse($Article->validates());
- $Article->save(null, array('validate' => false));
- $data['Article']['id'] = $Article->id;
- $Article->set($data);
- $this->assertTrue($Article->validates());
- unset($data['Article']['id']);
- $Article->validate = array(
- 'title' => array(
- 'notempty' => array(
- 'rule' => 'notEmpty',
- 'on' => 'update'
- )
- )
- );
- $Article->create($data);
- $this->assertTrue($Article->validates());
- $Article->save(null, array('validate' => false));
- $data['Article']['id'] = $Article->id;
- $Article->set($data);
- $this->assertFalse($Article->validates());
- }
- /**
- * Test for 'required' => [create|update] in validation rules.
- *
- * @return void
- */
- public function testStateRequiredValidation() {
- $this->loadFixtures('Article');
- $Article = new Article();
- // no title field present
- $data = array(
- 'Article' => array(
- 'body' => 'Extra Fields Body',
- 'published' => '1'
- )
- );
- $Article->validate = array(
- 'title' => array(
- 'notempty' => array(
- 'rule' => 'notEmpty',
- 'required' => 'create'
- )
- )
- );
- $Article->create($data);
- $this->assertFalse($Article->validates());
- $Article->save(null, array('validate' => false));
- $data['Article']['id'] = $Article->id;
- $Article->set($data);
- $this->assertTrue($Article->validates());
- unset($data['Article']['id']);
- $Article->validate = array(
- 'title' => array(
- 'notempty' => array(
- 'rule' => 'notEmpty',
- 'required' => 'update'
- )
- )
- );
- $Article->create($data);
- $this->assertTrue($Article->validates());
- $Article->save(null, array('validate' => false));
- $data['Article']['id'] = $Article->id;
- $Article->set($data);
- $this->assertFalse($Article->validates());
- }
- /**
- * Test that 'required' and 'on' are not conflicting
- *
- * @return void
- */
- public function testOnRequiredConflictValidation() {
- $this->loadFixtures('Article');
- $Article = new Article();
- // no title field present
- $data = array(
- 'Article' => array(
- 'body' => 'Extra Fields Body',
- 'published' => '1'
- )
- );
- $Article->validate = array(
- 'title' => array(
- 'notempty' => array(
- 'rule' => 'notEmpty',
- 'required' => 'create',
- 'on' => 'create'
- )
- )
- );
- $Article->create($data);
- $this->assertFalse($Article->validates());
- $Article->validate = array(
- 'title' => array(
- 'notempty' => array(
- 'rule' => 'notEmpty',
- 'required' => 'update',
- 'on' => 'create'
- )
- )
- );
- $Article->create($data);
- $this->assertTrue($Article->validates());
- $Article->validate = array(
- 'title' => array(
- 'notempty' => array(
- 'rule' => 'notEmpty',
- 'required' => 'create',
- 'on' => 'update'
- )
- )
- );
- $Article->create($data);
- $this->assertTrue($Article->validates());
- $Article->validate = array(
- 'title' => array(
- 'notempty' => array(
- 'rule' => 'notEmpty',
- 'required' => 'update',
- 'on' => 'update'
- )
- )
- );
- $Article->create($data);
- $this->assertTrue($Article->validates());
- $Article->validate = array(
- 'title' => array(
- 'notempty' => array(
- 'rule' => 'notEmpty',
- 'required' => 'create',
- 'on' => 'create'
- )
- )
- );
- $Article->save(null, array('validate' => false));
- $data['Article']['id'] = $Article->id;
- $Article->set($data);
- $this->assertTrue($Article->validates());
- $Article->validate = array(
- 'title' => array(
- 'notempty' => array(
- 'rule' => 'notEmpty',
- 'required' => 'update',
- 'on' => 'create'
- )
- )
- );
- $Article->set($data);
- $this->assertTrue($Article->validates());
- $Article->validate = array(
- 'title' => array(
- 'notempty' => array(
- 'rule' => 'notEmpty',
- 'required' => 'create',
- 'on' => 'update'
- )
- )
- );
- $Article->set($data);
- $this->assertTrue($Article->validates());
- $Article->validate = array(
- 'title' => array(
- 'notempty' => array(
- 'rule' => 'notEmpty',
- 'required' => 'update',
- 'on' => 'update'
- )
- )
- );
- $Article->set($data);
- $this->assertFalse($Article->validates());
- }
- /**
- * testSaveAllDeepValidateOnly
- * tests the validate methods with deeper recursive data
- *
- * @return void
- */
- public function testSaveAllDeepValidateOnly() {
- $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
- $TestModel = new Article();
- $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
- $TestModel->hasAndBelongsToMany = array();
- $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
- $TestModel->Comment->validate['comment'] = 'notEmpty';
- $data = array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- );
- $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => true));
- $this->assertTrue($result);
- $result = $TestModel->validateAssociated($data, array('deep' => true));
- $this->assertTrue($result);
- $data = array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- );
- $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => true));
- $this->assertFalse($result);
- $result = $TestModel->validateAssociated($data, array('deep' => true));
- $this->assertFalse($result);
- $data = array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- );
- $expected = array(
- 'Article' => true,
- 'Comment' => array(
- true,
- true
- )
- );
- $result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
- $this->assertSame($expected, $result);
- $result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => true));
- $this->assertSame($expected, $result);
- $data = array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- );
- $expected = array(
- 'Article' => true,
- 'Comment' => array(
- false,
- true
- )
- );
- $result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
- $this->assertSame($expected, $result);
- $result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => true));
- $this->assertSame($expected, $result);
- $data = array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
- )
- );
- $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => true));
- $this->assertTrue($result);
- $result = $TestModel->validateAssociated($data, array('deep' => true));
- $this->assertTrue($result);
- $data = array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
- )
- );
- $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => true));
- $this->assertFalse($result);
- $result = $TestModel->validateAssociated($data, array('deep' => true));
- $this->assertFalse($result);
- $data = array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsave'))
- )
- );
- $expected = array(
- 'Article' => true,
- 'Comment' => array(
- true,
- true
- )
- );
- $result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
- $this->assertSame($expected, $result);
- $result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => true));
- $this->assertSame($expected, $result);
- $data = array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
- )
- );
- $expected = array(
- 'Article' => true,
- 'Comment' => array(
- true,
- false
- )
- );
- $result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
- $this->assertSame($expected, $result);
- $result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => true));
- $this->assertSame($expected, $result);
- $expected = array(
- 'Comment' => array(
- 1 => array(
- 'Attachment' => array(
- 'attachment' => array('This field cannot be left blank')
- )
- )
- )
- );
- $result = $TestModel->validationErrors;
- $this->assertSame($expected, $result);
- $data = array(
- 'Attachment' => array(
- 'attachment' => 'deepsave insert',
- ),
- 'Comment' => array(
- 'comment' => 'First comment deepsave insert',
- 'published' => 'Y',
- 'user_id' => 5,
- 'Article' => array(
- 'title' => 'First Article deepsave insert',
- 'body' => 'First Article Body deepsave insert',
- 'User' => array(
- 'user' => 'deepsave',
- 'password' => 'magic'
- ),
- ),
- )
- );
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
- $this->assertTrue($result);
- $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => true));
- $this->assertTrue($result);
- $expected = array(
- 'Attachment' => true,
- 'Comment' => true
- );
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
- $this->assertSame($expected, $result);
- $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
- $this->assertSame($expected, $result);
- $data = array(
- 'Attachment' => array(
- 'attachment' => 'deepsave insert',
- ),
- 'Comment' => array(
- 'comment' => 'First comment deepsave insert',
- 'published' => 'Y',
- 'user_id' => 5,
- 'Article' => array(
- 'title' => 'First Article deepsave insert',
- 'body' => 'First Article Body deepsave insert',
- 'User' => array(
- 'user' => '',
- 'password' => 'magic'
- ),
- ),
- )
- );
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
- $this->assertFalse($result);
- $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => true));
- $this->assertFalse($result);
- $result = $TestModel->Comment->Attachment->validationErrors;
- $expected = array(
- 'Comment' => array(
- 'Article' => array(
- 'User' => array(
- 'user' => array('This field cannot be left blank')
- )
- )
- )
- );
- $this->assertSame($expected, $result);
- $expected = array(
- 'Attachment' => true,
- 'Comment' => false
- );
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
- $this->assertEquals($expected, $result);
- $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
- $this->assertEquals($expected, $result);
- $data['Comment']['Article']['body'] = '';
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
- $this->assertFalse($result);
- $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => true));
- $this->assertFalse($result);
- $result = $TestModel->Comment->Attachment->validationErrors;
- $expected = array(
- 'Comment' => array(
- 'Article' => array(
- 'body' => array('This field cannot be left blank'),
- 'User' => array(
- 'user' => array('This field cannot be left blank')
- )
- )
- )
- );
- $this->assertSame($expected, $result);
- $expected = array(
- 'Attachment' => true,
- 'Comment' => false
- );
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
- $this->assertEquals($expected, $result);
- $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
- $this->assertEquals($expected, $result);
- $data['Comment']['comment'] = '';
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
- $this->assertFalse($result);
- $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => true));
- $this->assertFalse($result);
- $result = $TestModel->Comment->Attachment->validationErrors;
- $expected = array(
- 'Comment' => array(
- 'comment' => array('This field cannot be left blank'),
- 'Article' => array(
- 'body' => array('This field cannot be left blank'),
- 'User' => array(
- 'user' => array('This field cannot be left blank')
- )
- )
- )
- );
- $this->assertSame($expected, $result);
- $expected = array(
- 'Attachment' => true,
- 'Comment' => false
- );
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
- $this->assertEquals($expected, $result);
- $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
- $this->assertEquals($expected, $result);
- $data['Attachment']['attachment'] = '';
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
- $this->assertFalse($result);
- $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => true));
- $this->assertFalse($result);
- $result = $TestModel->Comment->Attachment->validationErrors;
- $expected = array(
- 'attachment' => array('This field cannot be left blank'),
- 'Comment' => array(
- 'comment' => array('This field cannot be left blank'),
- 'Article' => array(
- 'body' => array('This field cannot be left blank'),
- 'User' => array(
- 'user' => array('This field cannot be left blank')
- )
- )
- )
- );
- $this->assertSame($expected, $result);
- $result = $TestModel->Comment->validationErrors;
- $expected = array(
- 'comment' => array('This field cannot be left blank'),
- 'Article' => array(
- 'body' => array('This field cannot be left blank'),
- 'User' => array(
- 'user' => array('This field cannot be left blank')
- )
- )
- );
- $this->assertSame($expected, $result);
- $expected = array(
- 'Attachment' => false,
- 'Comment' => false
- );
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
- $this->assertEquals($expected, $result);
- $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
- $this->assertEquals($expected, $result);
- }
- /**
- * testSaveAllNotDeepValidateOnly
- * tests the validate methods to not validate deeper recursive data
- *
- * @return void
- */
- public function testSaveAllNotDeepValidateOnly() {
- $this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
- $TestModel = new Article();
- $TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
- $TestModel->hasAndBelongsToMany = array();
- $TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
- $TestModel->Comment->validate['comment'] = 'notEmpty';
- $data = array(
- 'Article' => array('id' => 2, 'body' => ''),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- );
- $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => false));
- $this->assertFalse($result);
- $result = $TestModel->validateAssociated($data, array('deep' => false));
- $this->assertFalse($result);
- $expected = array('body' => array('This field cannot be left blank'));
- $result = $TestModel->validationErrors;
- $this->assertSame($expected, $result);
- $data = array(
- 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- );
- $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => false));
- $this->assertTrue($result);
- $result = $TestModel->validateAssociated($data, array('deep' => false));
- $this->assertTrue($result);
- $data = array(
- 'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
- 'Comment' => array(
- array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
- array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
- )
- );
- $expected = array(
- 'Article' => true,
- 'Comment' => array(
- true,
- true
- )
- );
- $result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
- $this->assertSame($expected, $result);
- $result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => false));
- $this->assertSame($expected, $result);
- $data = array(
- 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
- )
- );
- $result = $TestModel->saveAll($data, array('validate' => 'only', 'deep' => false));
- $this->assertTrue($result);
- $result = $TestModel->validateAssociated($data, array('deep' => false));
- $this->assertTrue($result);
- $data = array(
- 'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
- 'Comment' => array(
- array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
- array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
- )
- );
- $expected = array(
- 'Article' => true,
- 'Comment' => array(
- true,
- true
- )
- );
- $result = $TestModel->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
- $this->assertSame($expected, $result);
- $result = $TestModel->validateAssociated($data, array('atomic' => false, 'deep' => false));
- $this->assertSame($expected, $result);
- $expected = array();
- $result = $TestModel->validationErrors;
- $this->assertSame($expected, $result);
- $data = array(
- 'Attachment' => array(
- 'attachment' => 'deepsave insert',
- ),
- 'Comment' => array(
- 'comment' => 'First comment deepsave insert',
- 'published' => 'Y',
- 'user_id' => 5,
- 'Article' => array(
- 'title' => 'First Article deepsave insert ignored',
- 'body' => 'First Article Body deepsave insert',
- 'User' => array(
- 'user' => '',
- 'password' => 'magic'
- ),
- ),
- )
- );
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
- $this->assertTrue($result);
- $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => false));
- $this->assertTrue($result);
- $result = $TestModel->Comment->Attachment->validationErrors;
- $expected = array();
- $this->assertSame($expected, $result);
- $expected = array(
- 'Attachment' => true,
- 'Comment' => true
- );
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
- $this->assertEquals($expected, $result);
- $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => false));
- $this->assertEquals($expected, $result);
- $data['Comment']['Article']['body'] = '';
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
- $this->assertTrue($result);
- $result = $TestModel->Comment->Attachment->validateAssociated($data, array('deep' => false));
- $this->assertTrue($result);
- $result = $TestModel->Comment->Attachment->validationErrors;
- $expected = array();
- $this->assertSame($expected, $result);
- $expected = array(
- 'Attachment' => true,
- 'Comment' => true
- );
- $result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
- $this->assertEquals($expected, $result);
- $result = $TestModel->Comment->Attachment->validateAssociated($data, array('atomic' => false, 'deep' => false));
- $this->assertEquals($expected, $result);
- }
- /**
- * testValidateAssociated method
- *
- * @return void
- */
- public function testValidateAssociated() {
- $this->loadFixtures('Comment', 'Attachment', 'Article', 'User');
- $TestModel = new Comment();
- $TestModel->Attachment->validate = array('attachment' => 'notEmpty');
- $data = array(
- 'Comment' => array(
- 'comment' => 'This is the comment'
- ),
- 'Attachment' => array(
- 'attachment' => ''
- )
- );
- $result = $TestModel->saveAll($data, array('validate' => 'only'));
- $this->assertFalse($result);
- $result = $TestModel->validateAssociated($data);
- $this->assertFalse($result);
- $fieldList = array(
- 'Attachment' => array('comment_id')
- );
- $result = $TestModel->saveAll($data, array(
- 'fieldList' => $fieldList, 'validate' => 'only'
- ));
- $this->assertTrue($result);
- $this->assertEmpty($TestModel->validationErrors);
- $result = $TestModel->validateAssociated($data, array('fieldList' => $fieldList));
- $this->assertTrue($result);
- $this->assertEmpty($TestModel->validationErrors);
- $TestModel->validate = array('comment' => 'notEmpty');
- $record = array(
- 'Comment' => array(
- 'user_id' => 1,
- 'article_id' => 1,
- 'comment' => '',
- ),
- 'Attachment' => array(
- 'attachment' => ''
- )
- );
- $result = $TestModel->saveAll($record, array('validate' => 'only'));
- $this->assertFalse($result);
- $result = $TestModel->validateAssociated($record);
- $this->assertFalse($result);
- $fieldList = array(
- 'Comment' => array('id', 'article_id', 'user_id'),
- 'Attachment' => array('comment_id')
- );
- $result = $TestModel->saveAll($record, array(
- 'fieldList' => $fieldList, 'validate' => 'only'
- ));
- $this->assertTrue($result);
- $this->assertEmpty($TestModel->validationErrors);
- $result = $TestModel->validateAssociated($record, array('fieldList' => $fieldList));
- $this->assertTrue($result);
- $this->assertEmpty($TestModel->validationErrors);
- $TestModel = new Article();
- $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
- $TestModel->Comment->validate = array('comment' => 'notEmpty');
- $data = array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array(
- 'id' => 1,
- 'comment' => '',
- 'published' => 'Y',
- 'user_id' => 1,
- ),
- array(
- 'id' => 2,
- 'comment' =>
- 'comment',
- 'published' => 'Y',
- 'user_id' => 1
- ),
- array(
- 'id' => 3,
- 'comment' => '',
- 'published' => 'Y',
- 'user_id' => 1
- )));
- $result = $TestModel->saveAll($data, array('validate' => 'only'));
- $this->assertFalse($result);
- $result = $TestModel->validateAssociated($data);
- $this->assertFalse($result);
- $expected = array(
- 'Article' => true,
- 'Comment' => array(false, true, false)
- );
- $result = $TestModel->saveAll($data, array('atomic' => false, 'validate' => 'only'));
- $this->assertSame($expected, $result);
- $result = $TestModel->validateAssociated($data, array('atomic' => false));
- $this->assertSame($expected, $result);
- $expected = array('Comment' => array(
- 0 => array('comment' => array('This field cannot be left blank')),
- 2 => array('comment' => array('This field cannot be left blank'))
- ));
- $this->assertEquals($expected['Comment'], $TestModel->Comment->validationErrors);
- $model = new Comment();
- $model->deleteAll(true);
- $model->validate = array('comment' => 'notEmpty');
- $model->Attachment->validate = array('attachment' => 'notEmpty');
- $model->Attachment->bindModel(array('belongsTo' => array('Comment')));
- $expected = array(
- 'comment' => array('This field cannot be left blank'),
- 'Attachment' => array(
- 'attachment' => array('This field cannot be left blank')
- )
- );
- $data = array(
- 'Comment' => array('comment' => '', 'article_id' => 1, 'user_id' => 1),
- 'Attachment' => array('attachment' => '')
- );
- $result = $model->saveAll($data, array('validate' => 'only'));
- $this->assertFalse($result);
- $result = $model->validateAssociated($data);
- $this->assertFalse($result);
- $this->assertEquals($expected, $model->validationErrors);
- $this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
- }
- /**
- * testValidateMany method
- *
- * @return void
- */
- public function testValidateMany() {
- $TestModel = new Article();
- $TestModel->validate = array('title' => 'notEmpty');
- $data = array(
- 0 => array('title' => ''),
- 1 => array('title' => 'title 1'),
- 2 => array('title' => 'title 2'),
- );
- $expected = array(
- 0 => array('title' => array('This field cannot be left blank')),
- );
- $result = $TestModel->saveAll($data, array('validate' => 'only'));
- $this->assertFalse($result);
- $this->assertEquals($expected, $TestModel->validationErrors);
- $result = $TestModel->validateMany($data);
- $this->assertFalse($result);
- $this->assertEquals($expected, $TestModel->validationErrors);
- $data = array(
- 0 => array('title' => 'title 0'),
- 1 => array('title' => ''),
- 2 => array('title' => 'title 2'),
- );
- $expected = array(
- 1 => array('title' => array('This field cannot be left blank')),
- );
- $result = $TestModel->saveAll($data, array('validate' => 'only'));
- $this->assertFalse($result);
- $this->assertEquals($expected, $TestModel->validationErrors);
- $result = $TestModel->validateMany($data);
- $this->assertFalse($result);
- $this->assertEquals($expected, $TestModel->validationErrors);
- }
- /**
- * testGetMethods method
- *
- * @return void
- */
- public function testGetMethods() {
- $this->loadFixtures('Article', 'Comment');
- $TestModel = new Article();
- $Validator = $TestModel->validator();
- $result = $Validator->getMethods();
- $expected = array_map('strtolower', get_class_methods('Article'));
- $this->assertEquals($expected, array_keys($result));
- }
- /**
- * Tests that methods are refreshed when the list of behaviors change
- *
- * @return void
- */
- public function testGetMethodsRefresh() {
- $this->loadFixtures('Article', 'Comment');
- $TestModel = new Article();
- $Validator = $TestModel->validator();
- $result = $Validator->getMethods();
- $expected = array_map('strtolower', get_class_methods('Article'));
- $this->assertEquals($expected, array_keys($result));
- $TestModel->Behaviors->load('Containable');
- $newList = array(
- 'contain',
- 'resetbindings',
- 'containments',
- 'fielddependencies',
- 'containmentsmap'
- );
- $this->assertEquals(array_merge($expected, $newList), array_keys($Validator->getMethods()));
- $TestModel->Behaviors->unload('Containable');
- $this->assertEquals($expected, array_keys($Validator->getMethods()));
- }
- /**
- * testSetValidationDomain method
- *
- * @return void
- */
- public function testSetValidationDomain() {
- $this->loadFixtures('Article', 'Comment');
- $TestModel = new Article();
- $Validator = $TestModel->validator();
- $result = $Validator->setValidationDomain('default');
- $this->assertEquals('default', $TestModel->validationDomain);
- $result = $Validator->setValidationDomain('other');
- $this->assertEquals('other', $TestModel->validationDomain);
- }
- /**
- * testGetModel method
- *
- * @return void
- */
- public function testGetModel() {
- $TestModel = new Article();
- $Validator = $TestModel->validator();
- $result = $Validator->getModel();
- $this->assertInstanceOf('Article', $result);
- }
- /**
- * Tests it is possible to get validation sets for a field using an array inteface
- *
- * @return void
- */
- public function testArrayAccessGet() {
- $TestModel = new Article();
- $Validator = $TestModel->validator();
- $titleValidator = $Validator['title'];
- $this->assertEquals('title', $titleValidator->field);
- $this->assertCount(1, $titleValidator->getRules());
- $rule = current($titleValidator->getRules());
- $this->assertEquals('notEmpty', $rule->rule);
- $titleValidator = $Validator['body'];
- $this->assertEquals('body', $titleValidator->field);
- $this->assertCount(1, $titleValidator->getRules());
- $rule = current($titleValidator->getRules());
- $this->assertEquals('notEmpty', $rule->rule);
- $titleValidator = $Validator['user_id'];
- $this->assertEquals('user_id', $titleValidator->field);
- $this->assertCount(1, $titleValidator->getRules());
- $rule = current($titleValidator->getRules());
- $this->assertEquals('numeric', $rule->rule);
- }
- /**
- * Tests it is possible to check for validation sets for a field using an array inteface
- *
- * @return void
- */
- public function testArrayAccessExists() {
- $TestModel = new Article();
- $Validator = $TestModel->validator();
- $this->assertTrue(isset($Validator['title']));
- $this->assertTrue(isset($Validator['body']));
- $this->assertTrue(isset($Validator['user_id']));
- $this->assertFalse(isset($Validator['other']));
- }
- /**
- * Tests it is possible to set validation rules for a field using an array inteface
- *
- * @return void
- */
- public function testArrayAccessSet() {
- $TestModel = new Article();
- $Validator = $TestModel->validator();
- $set = array(
- 'numeric' => array('rule' => 'numeric', 'allowEmpty' => false),
- 'range' => array('rule' => array('between', 1, 5), 'allowEmpty' => false),
- );
- $Validator['other'] = $set;
- $rules = $Validator['other'];
- $this->assertEquals('other', $rules->field);
- $validators = $rules->getRules();
- $this->assertCount(2, $validators);
- $this->assertEquals('numeric', $validators['numeric']->rule);
- $this->assertEquals(array('between', 1, 5), $validators['range']->rule);
- $Validator['new'] = new CakeValidationSet('new', $set, array());
- $rules = $Validator['new'];
- $this->assertEquals('new', $rules->field);
- $validators = $rules->getRules();
- $this->assertCount(2, $validators);
- $this->assertEquals('numeric', $validators['numeric']->rule);
- $this->assertEquals(array('between', 1, 5), $validators['range']->rule);
- }
- /**
- * Tests it is possible to unset validation rules
- *
- * @return void
- */
- public function testArrayAccessUset() {
- $TestModel = new Article();
- $Validator = $TestModel->validator();
- $this->assertTrue(isset($Validator['title']));
- unset($Validator['title']);
- $this->assertFalse(isset($Validator['title']));
- }
- /**
- * Tests it is possible to iterate a validation object
- *
- * @return void
- */
- public function testIterator() {
- $TestModel = new Article();
- $Validator = $TestModel->validator();
- $i = 0;
- foreach ($Validator as $field => $rules) {
- if ($i === 0) {
- $this->assertEquals('user_id', $field);
- }
- if ($i === 1) {
- $this->assertEquals('title', $field);
- }
- if ($i === 2) {
- $this->assertEquals('body', $field);
- }
- $this->assertInstanceOf('CakeValidationSet', $rules);
- $i++;
- }
- $this->assertEquals(3, $i);
- }
- /**
- * Tests countable interface in ModelValidator
- *
- * @return void
- */
- public function testCount() {
- $TestModel = new Article();
- $Validator = $TestModel->validator();
- $this->assertCount(3, $Validator);
- $set = array(
- 'numeric' => array('rule' => 'numeric', 'allowEmpty' => false),
- 'range' => array('rule' => array('between', 1, 5), 'allowEmpty' => false),
- );
- $Validator['other'] = $set;
- $this->assertCount(4, $Validator);
- unset($Validator['title']);
- $this->assertCount(3, $Validator);
- unset($Validator['body']);
- $this->assertCount(2, $Validator);
- }
- /**
- * Tests it is possible to add validation rules
- *
- * @return void
- */
- public function testAddRule() {
- $TestModel = new Article();
- $Validator = $TestModel->validator();
- $Validator->add('other', 'numeric', array('rule' => 'numeric', 'allowEmpty' => false));
- $Validator->add('other', 'range', array('rule' => array('between', 1, 5), 'allowEmpty' => false));
- $rules = $Validator['other'];
- $this->assertEquals('other', $rules->field);
- $validators = $rules->getRules();
- $this->assertCount(2, $validators);
- $this->assertEquals('numeric', $validators['numeric']->rule);
- $this->assertEquals(array('between', 1, 5), $validators['range']->rule);
- }
- /**
- * Tests it is possible to remove validation rules
- *
- * @return void
- */
- public function testRemoveRule() {
- $TestModel = new Article();
- $Validator = $TestModel->validator();
- $this->assertTrue(isset($Validator['title']));
- $Validator->remove('title');
- $this->assertFalse(isset($Validator['title']));
- $Validator->add('other', 'numeric', array('rule' => 'numeric', 'allowEmpty' => false));
- $Validator->add('other', 'range', array('rule' => array('between', 1, 5), 'allowEmpty' => false));
- $this->assertTrue(isset($Validator['other']));
- $Validator->remove('other', 'numeric');
- $this->assertTrue(isset($Validator['other']));
- $this->assertFalse(isset($Validator['other']['numeric']));
- $this->assertTrue(isset($Validator['other']['range']));
- }
- /**
- * Tests validation callbacks are triggered
- *
- * @return void
- */
- public function testValidateCallbacks() {
- $TestModel = $this->getMock('Article', array('beforeValidate', 'afterValidate'));
- $TestModel->expects($this->once())->method('beforeValidate');
- $TestModel->expects($this->once())->method('afterValidate');
- $TestModel->set(array('title' => '', 'body' => 'body'));
- $TestModel->validates();
- }
- /**
- * Tests that altering data in a beforeValidate callback will lead to saving those
- * values in database
- *
- * @return void
- */
- public function testValidateFirstWithBeforeValidate() {
- $this->loadFixtures('Article', 'User');
- $model = new CustomArticle();
- $model->validate = array(
- 'title' => array(
- 'notempty' => array(
- 'rule' => 'notEmpty',
- 'required' => true,
- 'allowEmpty' => false
- )
- )
- );
- $data = array(
- 'CustomArticle' => array(
- 'body' => 'foo0'
- )
- );
- $result = $model->saveAll($data, array('validate' => 'first'));
- $this->assertTrue($result);
- $this->assertFalse($model->findMethods['unPublished'], 'beforeValidate was run twice');
- $model->findMethods['unPublished'] = true;
- $data = array(
- 'CustomArticle' => array(
- 'body' => 'foo1'
- )
- );
- $result = $model->saveAll($data, array('validate' => 'first', 'deep' => true));
- $this->assertTrue($result);
- $title = $model->field('title', array('body' => 'foo1'));
- $this->assertEquals('foo', $title);
- $this->assertFalse($model->findMethods['unPublished'], 'beforeValidate was run twice');
- $data = array(
- array('body' => 'foo2'),
- array('body' => 'foo3'),
- array('body' => 'foo4')
- );
- $result = $model->saveAll($data, array('validate' => 'first', 'deep' => true));
- $this->assertTrue($result);
- $this->assertEquals('foo', $model->field('title', array('body' => 'foo2')));
- $this->assertEquals('foo', $model->field('title', array('body' => 'foo3')));
- $this->assertEquals('foo', $model->field('title', array('body' => 'foo4')));
- }
- /**
- * Tests that altering data in a beforeValidate callback will lead to saving those
- * values in database
- *
- * @return void
- */
- public function testValidateFirstAssociatedWithBeforeValidate() {
- $this->loadFixtures('Article', 'User');
- $model = new CustomArticle();
- $model->validate = array(
- 'title' => array(
- 'notempty' => array(
- 'rule' => 'notEmpty',
- 'required' => true
- )
- )
- );
- $articles = array(
- array('body' => 'foo1'),
- array('body' => 'foo2'),
- array('body' => 'foo3')
- );
- $user = new User();
- $user->bindModel(array('hasMany' => array('CustomArticle')));
- $data = array(
- 'User' => array('user' => 'foo', 'password' => 'bar'),
- 'CustomArticle' => $articles
- );
- $result = $user->saveAll($data, array('validate' => 'first'));
- $this->assertTrue($result);
- $this->assertEquals('foo', $model->field('title', array('body' => 'foo1')));
- $this->assertEquals('foo', $model->field('title', array('body' => 'foo2')));
- $this->assertEquals('foo', $model->field('title', array('body' => 'foo3')));
- }
- /**
- * testValidateFirstWithDefaults method
- *
- * @return void
- */
- public function testFirstWithDefaults() {
- $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
- $TestModel = new Article();
- $result = $TestModel->find('first', array(
- 'conditions' => array('Article.id' => 1)
- ));
- $expected = array(
- 'Article' => array(
- 'id' => 1,
- 'user_id' => 1,
- 'title' => 'First Article',
- 'body' => 'First Article Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:39:23'
- ),
- );
- unset($result['Article']['updated']);
- $this->assertEquals($expected['Article'], $result['Article']);
- $data = array(
- 'Article' => array(
- 'id' => 1,
- 'title' => 'First Article (modified)'
- ),
- 'Comment' => array(
- array('comment' => 'Article comment', 'user_id' => 1)
- )
- );
- $result = $TestModel->saveAll($data, array('validate' => 'first'));
- $this->assertTrue($result);
- $result = $TestModel->find('first', array(
- 'conditions' => array('Article.id' => 1)
- ));
- $expected['Article']['title'] = 'First Article (modified)';
- unset($result['Article']['updated']);
- $this->assertEquals($expected['Article'], $result['Article']);
- }
- public function testAddMultipleRules() {
- $TestModel = new Article();
- $Validator = $TestModel->validator();
- $set = array(
- 'numeric' => array('rule' => 'numeric', 'allowEmpty' => false),
- 'range' => array('rule' => array('between', 1, 5), 'allowEmpty' => false),
- );
- $Validator->add('other', $set);
- $rules = $Validator['other'];
- $this->assertEquals('other', $rules->field);
- $validators = $rules->getRules();
- $this->assertCount(2, $validators);
- $this->assertEquals('numeric', $validators['numeric']->rule);
- $this->assertEquals(array('between', 1, 5), $validators['range']->rule);
- $set = new CakeValidationSet('other', array(
- 'a' => array('rule' => 'numeric', 'allowEmpty' => false),
- 'b' => array('rule' => array('between', 1, 5), 'allowEmpty' => false),
- ));
- $Validator->add('other', $set);
- $this->assertSame($set, $Validator->getField('other'));
- }
- /**
- * Test that rules are parsed correctly when calling getField()
- *
- * @return void
- */
- public function testValidator() {
- $TestModel = new Article();
- $Validator = $TestModel->validator();
- $result = $Validator->getField();
- $expected = array('user_id', 'title', 'body');
- $this->assertEquals($expected, array_keys($result));
- $this->assertTrue($result['user_id'] instanceof CakeValidationSet);
- $result = $TestModel->validator()->getField('title');
- $this->assertTrue($result instanceof CakeValidationSet);
- }
- /**
- * Test that validator override works as expected
- *
- * @return void
- */
- public function testValidatorOverride() {
- $TestModel = new Article();
- $ValidatorA = new ModelValidator($TestModel);
- $ValidatorB = new ModelValidator($TestModel);
- $TestModel->validator($ValidatorA);
- $TestModel->validator($ValidatorB);
- $this->assertSame($ValidatorB, $TestModel->validator());
- $this->assertNotSame($ValidatorA, $TestModel->validator());
- }
- /**
- * Test that type hint exception is thrown
- *
- * @expectedException PHPUnit_Framework_Error
- * @return void
- */
- public function testValidatorTypehintException() {
- new ModelValidator('asdasds');
- }
- /**
- * Tests that altering data in a beforeValidate callback will lead to saving those
- * values in database, this time with belongsTo associations
- *
- * @return void
- */
- public function testValidateFirstAssociatedWithBeforeValidate2() {
- $this->loadFixtures('Article', 'User');
- $model = new CustomArticle();
- $model->validate = array(
- 'title' => array(
- 'notempty' => array(
- 'rule' => 'notEmpty',
- 'required' => true
- )
- )
- );
- $data = array(
- 'User' => array('user' => 'foo', 'password' => 'bar'),
- 'CustomArticle' => array(
- 'body' => 'a test'
- )
- );
- $result = $model->saveAll($data, array('validate' => 'first'));
- $this->assertTrue($result);
- $this->assertEquals('foo', $model->field('title', array('body' => 'a test')));
- }
- /**
- * Testing you can dynamically add rules to a field, added this to dispel doubts
- * after a presentation made to show off this new feature
- *
- * @return void
- */
- public function testDynamicValidationRuleBuilding() {
- $model = new Article;
- $validator = $model->validator();
- $validator->add('body', 'isSpecial', array('rule' => 'special'));
- $rules = $validator['body']->getRules();
- $this->assertCount(2, $rules);
- $this->assertEquals('special', $rules['isSpecial']->rule);
- $validator['body']->setRule('isAwesome', array('rule' => 'awesome'));
- $rules = $validator['body']->getRules();
- $this->assertCount(3, $rules);
- $this->assertEquals('awesome', $rules['isAwesome']->rule);
- }
- /**
- * Test to ensure custom validation methods work with CakeValidationSet
- *
- * @return void
- */
- public function testCustomMethodsWithCakeValidationSet() {
- $TestModel = new TestValidate();
- $Validator = $TestModel->validator();
- $Validator->add('title', 'validateTitle', array(
- 'rule' => 'validateTitle',
- 'message' => 'That aint right',
- ));
- $data = array('title' => 'notatitle');
- $result = $Validator->getField('title')->validate($data);
- $expected = array(0 => 'That aint right');
- $this->assertEquals($expected, $result);
- $data = array('title' => 'title-is-good');
- $result = $Validator->getField('title')->validate($data);
- $expected = array();
- $this->assertEquals($expected, $result);
- }
- public function testCustomMethodWithEmptyValue() {
- $this->loadFixtures('Article');
- $model = $this->getMock('Article', array('isLegit'));
- $model->validate = array(
- 'title' => array(
- 'custom' => array(
- 'rule' => array('isLegit'),
- 'message' => 'is no good'
- )
- )
- );
- $model->expects($this->once())
- ->method('isLegit')
- ->will($this->returnValue(false));
- $model->set(array('title' => ''));
- $this->assertFalse($model->validates());
- }
- /**
- * Test validateAssociated with atomic=false & deep=true
- *
- * @return void
- */
- public function testValidateAssociatedAtomicFalseDeepTrueWithErrors() {
- $this->loadFixtures('Comment', 'Article', 'User', 'Attachment');
- $Attachment = ClassRegistry::init('Attachment');
- $Attachment->Comment->validator()->add('comment', array(
- array('rule' => 'notEmpty')
- ));
- $Attachment->Comment->User->bindModel(array(
- 'hasMany' => array(
- 'Article',
- 'Comment'
- )),
- false
- );
- $data = array(
- 'Attachment' => array(
- 'attachment' => 'text',
- 'Comment' => array(
- 'comment' => '',
- 'published' => 'N',
- 'User' => array(
- 'user' => 'Foo',
- 'password' => 'mypassword',
- 'Comment' => array(
- array(
- 'comment' => ''
- )
- )
- )
- )
- )
- );
- $result = $Attachment->validateAssociated($data, array('atomic' => false, 'deep' => true));
- $result = $Attachment->validationErrors;
- $expected = array(
- 'Comment' => array(
- 'comment' => array(
- 0 => 'This field cannot be left blank',
- ),
- 'User' => array(
- 'Comment' => array(
- 0 => array(
- 'comment' => array(
- 0 => 'This field cannot be left blank',
- ),
- ),
- ),
- ),
- ),
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * Test validateMany with atomic=false & deep=true
- *
- * @return void
- */
- public function testValidateManyAtomicFalseDeepTrueWithErrors() {
- $this->loadFixtures('Comment', 'Article', 'User');
- $Article = ClassRegistry::init('Article');
- $Article->Comment->validator()->add('comment', array(
- array('rule' => 'notEmpty')
- ));
- $data = array(
- array(
- 'Article' => array(
- 'user_id' => 1,
- 'title' => 'Foo',
- 'body' => 'text',
- 'published' => 'N'
- ),
- 'Comment' => array(
- array(
- 'user_id' => 1,
- 'comment' => 'Baz',
- 'published' => 'N',
- )
- ),
- ),
- array(
- 'Article' => array(
- 'user_id' => 1,
- 'title' => 'Bar',
- 'body' => 'text',
- 'published' => 'N'
- ),
- 'Comment' => array(
- array(
- 'user_id' => 1,
- 'comment' => '',
- 'published' => 'N',
- )
- ),
- ),
- );
- $Article->validateMany($data, array('atomic' => false, 'deep' => true));
- $result = $Article->validationErrors;
- $expected = array(
- 1 => array(
- 'Comment' => array(
- 0 => array(
- 'comment' => array(
- 0 => 'This field cannot be left blank',
- ),
- ),
- ),
- ),
- );
- $this->assertEquals($expected, $result);
- }
- }
- /**
- * Behavior for testing validation rules.
- */
- class ValidationRuleBehavior extends ModelBehavior {
- public function setup(Model $Model, $config = array()) {
- $this->settings[$Model->alias] = $config;
- }
- public function beforeValidate(Model $Model, $options = array()) {
- $fields = $this->settings[$Model->alias]['fields'];
- foreach ($fields as $field) {
- $Model->whitelist[] = $field;
- }
- }
- }
|