123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358 |
- <?php
- /**
- * ModelValidationTest file
- *
- * PHP 5
- *
- * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
- * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright 2005-2012, 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 MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
- /**
- * ModelValidationTest
- *
- * @package Cake.Test.Case.Model
- */
- class ModelValidationTest extends BaseModelTest {
- /**
- * 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($count, 0);
- $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 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($count, 0);
- $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->assertFalse(is_null($Author->id));
- $id = $Author->id;
- $count = $Author->find('count', array('conditions' => array('Author.id' => $id)));
- $this->assertSame($count, 1);
- $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');
- $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);
- $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->attach('Containable');
- $newList = array(
- 'contain',
- 'resetbindings',
- 'containments',
- 'fielddependencies',
- 'containmentsmap'
- );
- $this->assertEquals(array_merge($expected, $newList), array_keys($Validator->getMethods()));
- $TestModel->Behaviors->detach('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($result, $expected);
- }
- /**
- * 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($result, $expected);
- }
- }
|