FormHelperTest.php 164 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @since CakePHP(tm) v 1.2.0.4206
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\View\Helper;
  18. use Cake\Controller\Controller;
  19. use Cake\Core\App;
  20. use Cake\Core\Configure;
  21. use Cake\Core\Plugin;
  22. use Cake\Network\Request;
  23. use Cake\ORM\Entity;
  24. use Cake\ORM\Table;
  25. use Cake\ORM\TableRegistry;
  26. use Cake\Routing\Router;
  27. use Cake\TestSuite\TestCase;
  28. use Cake\Utility\Security;
  29. use Cake\View\Helper\FormHelper;
  30. use Cake\View\Helper\HtmlHelper;
  31. use Cake\View\View;
  32. /**
  33. * Test stub.
  34. */
  35. class Article extends Entity {
  36. }
  37. /**
  38. * Contact class
  39. *
  40. */
  41. class ContactsTable extends Table {
  42. /**
  43. * Default schema
  44. *
  45. * @var array
  46. */
  47. protected $_schema = array(
  48. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  49. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  50. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  51. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  52. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  53. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  54. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  55. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  56. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null),
  57. '_constraints' => array('primary' => ['type' => 'primary', 'columns' => ['id']])
  58. );
  59. /**
  60. * Initializes the schema
  61. *
  62. * @return void
  63. */
  64. public function initialize(array $config) {
  65. $this->schema($this->_schema);
  66. }
  67. }
  68. /**
  69. * ValidateUser class
  70. *
  71. */
  72. class ValidateUsersTable extends Table {
  73. /**
  74. * schema method
  75. *
  76. * @var array
  77. */
  78. protected $_schema = array(
  79. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  80. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  82. 'balance' => array('type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2),
  83. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3),
  84. 'ratio' => array('type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6),
  85. 'population' => array('type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0),
  86. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  87. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  88. '_constraints' => array('primary' => ['type' => 'primary', 'columns' => ['id']])
  89. );
  90. /**
  91. * Initializes the schema
  92. *
  93. * @return void
  94. */
  95. public function initialize(array $config) {
  96. $this->schema($this->_schema);
  97. }
  98. }
  99. /**
  100. * FormHelperTest class
  101. *
  102. * @property FormHelper $Form
  103. */
  104. class FormHelperTest extends TestCase {
  105. /**
  106. * Fixtures to be used
  107. *
  108. * @var array
  109. */
  110. public $fixtures = array('core.article', 'core.comment');
  111. /**
  112. * Do not load the fixtures by default
  113. *
  114. * @var boolean
  115. */
  116. public $autoFixtures = false;
  117. /**
  118. * setUp method
  119. *
  120. * @return void
  121. */
  122. public function setUp() {
  123. parent::setUp();
  124. Configure::write('Config.language', 'eng');
  125. Configure::write('App.base', '');
  126. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  127. Configure::delete('Asset');
  128. $this->View = new View(null);
  129. $this->Form = new FormHelper($this->View);
  130. $this->Form->Html = new HtmlHelper($this->View);
  131. $this->Form->request = new Request('articles/add');
  132. $this->Form->request->here = '/articles/add';
  133. $this->Form->request['controller'] = 'articles';
  134. $this->Form->request['action'] = 'add';
  135. $this->Form->request->webroot = '';
  136. $this->Form->request->base = '';
  137. $this->dateRegex = array(
  138. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  139. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  140. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  141. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  142. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  143. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  144. );
  145. $this->article = [
  146. 'schema' => [
  147. 'id' => ['type' => 'integer'],
  148. 'author_id' => ['type' => 'integer', 'null' => true],
  149. 'title' => ['type' => 'string', 'null' => true],
  150. 'body' => 'text',
  151. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  152. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  153. ],
  154. 'required' => [
  155. 'author_id' => true,
  156. 'title' => true,
  157. ]
  158. ];
  159. Configure::write('Security.salt', 'foo!');
  160. Router::connect('/:controller', array('action' => 'index'));
  161. Router::connect('/:controller/:action/*');
  162. }
  163. /**
  164. * tearDown method
  165. *
  166. * @return void
  167. */
  168. public function tearDown() {
  169. parent::tearDown();
  170. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  171. TableRegistry::clear();
  172. }
  173. /**
  174. * Test registering a new widget class and rendering it.
  175. *
  176. * @return void
  177. */
  178. public function testAddWidgetAndRenderWidget() {
  179. $data = [
  180. 'val' => 1
  181. ];
  182. $mock = $this->getMock('Cake\View\Widget\WidgetInterface');
  183. $this->assertNull($this->Form->addWidget('test', $mock));
  184. $mock->expects($this->once())
  185. ->method('render')
  186. ->with($data)
  187. ->will($this->returnValue('HTML'));
  188. $result = $this->Form->widget('test', $data);
  189. $this->assertEquals('HTML', $result);
  190. }
  191. /**
  192. * Test registering an invalid widget class.
  193. *
  194. * @expectedException \RuntimeException
  195. * @return void
  196. */
  197. public function testAddWidgetInvalid() {
  198. $mock = new \StdClass();
  199. $this->Form->addWidget('test', $mock);
  200. $this->Form->widget('test');
  201. }
  202. /**
  203. * Test adding a new context class.
  204. *
  205. * @return void
  206. */
  207. public function testAddContextProvider() {
  208. $context = 'My data';
  209. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  210. $this->assertInstanceOf('Cake\Network\Request', $request);
  211. $this->assertEquals($context, $data['entity']);
  212. return $this->getMock('Cake\View\Form\ContextInterface');
  213. });
  214. $this->Form->create($context);
  215. }
  216. /**
  217. * Test adding an invalid context class.
  218. *
  219. * @expectedException RuntimeException
  220. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  221. * @return void
  222. */
  223. public function testAddContextProviderInvalid() {
  224. $context = 'My data';
  225. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  226. return new \StdClass();
  227. });
  228. $this->Form->create($context);
  229. }
  230. /**
  231. * Provides context options for create().
  232. *
  233. * @return array
  234. */
  235. public function contextSelectionProvider() {
  236. $entity = new Article();
  237. $collection = $this->getMock('Cake\Collection\Collection', ['extract'], [[$entity]]);
  238. $data = [
  239. 'schema' => [
  240. 'title' => ['type' => 'string']
  241. ]
  242. ];
  243. return [
  244. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  245. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  246. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  247. 'none' => [null, 'Cake\View\Form\NullContext'],
  248. 'false' => [false, 'Cake\View\Form\NullContext'],
  249. ];
  250. }
  251. /**
  252. * Test default context selection in create()
  253. *
  254. * @dataProvider contextSelectionProvider
  255. * @return void
  256. */
  257. public function testCreateContextSelectionBuiltIn($data, $class) {
  258. $this->loadFixtures('Article');
  259. $this->Form->create($data);
  260. $this->assertInstanceOf($class, $this->Form->context());
  261. }
  262. /**
  263. * Data provider for type option.
  264. *
  265. * @return array
  266. */
  267. public static function requestTypeProvider() {
  268. return [
  269. // type, method, override
  270. ['post', 'post', 'POST'],
  271. ['put', 'post', 'PUT'],
  272. ['patch', 'post', 'PATCH'],
  273. ['delete', 'post', 'DELETE'],
  274. ];
  275. }
  276. /**
  277. * Test creating file forms.
  278. *
  279. * @return void
  280. */
  281. public function testCreateFile() {
  282. $encoding = strtolower(Configure::read('App.encoding'));
  283. $result = $this->Form->create(false, array('type' => 'file'));
  284. $expected = array(
  285. 'form' => array(
  286. 'method' => 'post', 'action' => '/articles/add',
  287. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  288. ),
  289. 'div' => array('style' => 'display:none;'),
  290. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  291. '/div'
  292. );
  293. $this->assertTags($result, $expected);
  294. }
  295. /**
  296. * Test creating GET forms.
  297. *
  298. * @return void
  299. */
  300. public function testCreateGet() {
  301. $encoding = strtolower(Configure::read('App.encoding'));
  302. $result = $this->Form->create(false, array('type' => 'get'));
  303. $expected = array('form' => array(
  304. 'method' => 'get', 'action' => '/articles/add',
  305. 'accept-charset' => $encoding
  306. ));
  307. $this->assertTags($result, $expected);
  308. }
  309. /**
  310. * test the create() method
  311. *
  312. * @dataProvider requestTypeProvider
  313. * @return void
  314. */
  315. public function testCreateTypeOptions($type, $method, $override) {
  316. $encoding = strtolower(Configure::read('App.encoding'));
  317. $result = $this->Form->create(false, array('type' => $type));
  318. $expected = array(
  319. 'form' => array(
  320. 'method' => $method, 'action' => '/articles/add',
  321. 'accept-charset' => $encoding
  322. ),
  323. 'div' => array('style' => 'display:none;'),
  324. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => $override),
  325. '/div'
  326. );
  327. $this->assertTags($result, $expected);
  328. }
  329. /**
  330. * Test opening a form for an update operation.
  331. *
  332. * @return void
  333. */
  334. public function testCreateUpdateForm() {
  335. $encoding = strtolower(Configure::read('App.encoding'));
  336. $this->Form->request->here = '/articles/edit/1';
  337. $this->Form->request['action'] = 'edit';
  338. $this->article['defaults']['id'] = 1;
  339. $result = $this->Form->create($this->article);
  340. $expected = array(
  341. 'form' => array(
  342. 'method' => 'post', 'action' => '/articles/edit/1',
  343. 'accept-charset' => $encoding
  344. ),
  345. 'div' => array('style' => 'display:none;'),
  346. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  347. '/div'
  348. );
  349. $this->assertTags($result, $expected);
  350. }
  351. /**
  352. * Test the onsubmit option for create()
  353. *
  354. * @return void
  355. */
  356. public function testCreateOnSubmit() {
  357. $this->Form->request->data = [];
  358. $this->Form->request['controller'] = 'articles';
  359. $result = $this->Form->create($this->article, ['url' => ['action' => 'index', 'param'], 'default' => false]);
  360. $expected = array(
  361. 'form' => array(
  362. 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/articles/index/param',
  363. 'accept-charset' => 'utf-8'
  364. ),
  365. 'div' => array('style' => 'display:none;'),
  366. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  367. '/div'
  368. );
  369. $this->assertTags($result, $expected);
  370. $this->Form->request->data = [];
  371. $this->Form->request['controller'] = 'articles';
  372. $result = $this->Form->create($this->article, array(
  373. 'url' => array('action' => 'index', 'param'),
  374. 'default' => false,
  375. 'onsubmit' => 'someFunction();'
  376. ));
  377. $expected = array(
  378. 'form' => array(
  379. 'method' => 'post',
  380. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  381. 'action' => '/articles/index/param',
  382. 'accept-charset' => 'utf-8'
  383. ),
  384. 'div' => array('style' => 'display:none;'),
  385. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  386. '/div'
  387. );
  388. $this->assertTags($result, $expected);
  389. }
  390. /**
  391. * test create() with automatic url generation
  392. *
  393. * @return void
  394. */
  395. public function testCreateAutoUrl() {
  396. $encoding = strtolower(Configure::read('App.encoding'));
  397. $this->Form->request['action'] = 'delete';
  398. $this->Form->request->here = '/articles/delete/10';
  399. $this->Form->request->base = '';
  400. $result = $this->Form->create($this->article);
  401. $expected = array(
  402. 'form' => array(
  403. 'method' => 'post', 'action' => '/articles/delete/10',
  404. 'accept-charset' => $encoding
  405. ),
  406. 'div' => array('style' => 'display:none;'),
  407. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  408. '/div'
  409. );
  410. $this->assertTags($result, $expected);
  411. $this->article['defaults'] = ['id' => 1];
  412. $this->Form->request->here = '/articles/edit/1';
  413. $this->Form->request['action'] = 'delete';
  414. $result = $this->Form->create($this->article, ['action' => 'edit']);
  415. $expected = array(
  416. 'form' => array(
  417. 'method' => 'post',
  418. 'action' => '/articles/edit/1',
  419. 'accept-charset' => $encoding
  420. ),
  421. 'div' => array('style' => 'display:none;'),
  422. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  423. '/div'
  424. );
  425. $this->assertTags($result, $expected);
  426. $this->Form->request['action'] = 'add';
  427. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  428. $expected = array(
  429. 'form' => array(
  430. 'method' => 'post',
  431. 'action' => '/articles/publish/1',
  432. 'accept-charset' => $encoding
  433. ),
  434. 'div' => array('style' => 'display:none;'),
  435. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  436. '/div'
  437. );
  438. $this->assertTags($result, $expected);
  439. $result = $this->Form->create($this->article, array('url' => '/articles/publish'));
  440. $expected = array(
  441. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  442. 'div' => array('style' => 'display:none;'),
  443. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  444. '/div'
  445. );
  446. $this->assertTags($result, $expected);
  447. $this->Form->request['controller'] = 'pages';
  448. $result = $this->Form->create($this->article, array('action' => 'signup'));
  449. $expected = array(
  450. 'form' => array(
  451. 'method' => 'post', 'action' => '/pages/signup/1',
  452. 'accept-charset' => $encoding
  453. ),
  454. 'div' => array('style' => 'display:none;'),
  455. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  456. '/div'
  457. );
  458. $this->assertTags($result, $expected);
  459. }
  460. /**
  461. * test create() with a custom route
  462. *
  463. * @return void
  464. */
  465. public function testCreateCustomRoute() {
  466. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  467. $encoding = strtolower(Configure::read('App.encoding'));
  468. $this->Form->request['controller'] = 'users';
  469. $result = $this->Form->create(false, array('action' => 'login'));
  470. $expected = array(
  471. 'form' => array(
  472. 'method' => 'post', 'action' => '/login',
  473. 'accept-charset' => $encoding
  474. ),
  475. 'div' => array('style' => 'display:none;'),
  476. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  477. '/div'
  478. );
  479. $this->assertTags($result, $expected);
  480. }
  481. /**
  482. * test automatic accept-charset overriding
  483. *
  484. * @return void
  485. */
  486. public function testCreateWithAcceptCharset() {
  487. $result = $this->Form->create($this->article, array(
  488. 'type' => 'post', 'action' => 'index', 'encoding' => 'iso-8859-1'
  489. )
  490. );
  491. $expected = array(
  492. 'form' => array(
  493. 'method' => 'post', 'action' => '/articles',
  494. 'accept-charset' => 'iso-8859-1'
  495. ),
  496. 'div' => array('style' => 'display:none;'),
  497. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  498. '/div'
  499. );
  500. $this->assertTags($result, $expected);
  501. }
  502. /**
  503. * Test base form URL when url param is passed with multiple parameters (&)
  504. *
  505. */
  506. public function testCreateQuerystringrequest() {
  507. $encoding = strtolower(Configure::read('App.encoding'));
  508. $result = $this->Form->create($this->article, array(
  509. 'type' => 'post',
  510. 'escape' => false,
  511. 'url' => array(
  512. 'controller' => 'controller',
  513. 'action' => 'action',
  514. '?' => array('param1' => 'value1', 'param2' => 'value2')
  515. )
  516. ));
  517. $expected = array(
  518. 'form' => array(
  519. 'method' => 'post',
  520. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  521. 'accept-charset' => $encoding
  522. ),
  523. 'div' => array('style' => 'display:none;'),
  524. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  525. '/div'
  526. );
  527. $this->assertTags($result, $expected);
  528. $result = $this->Form->create($this->article, array(
  529. 'type' => 'post',
  530. 'url' => array(
  531. 'controller' => 'controller',
  532. 'action' => 'action',
  533. '?' => array('param1' => 'value1', 'param2' => 'value2')
  534. )
  535. ));
  536. $this->assertTags($result, $expected);
  537. }
  538. /**
  539. * test that create() doesn't cause errors by multiple id's being in the primary key
  540. * as could happen with multiple select or checkboxes.
  541. *
  542. * @return void
  543. */
  544. public function testCreateWithMultipleIdInData() {
  545. $encoding = strtolower(Configure::read('App.encoding'));
  546. $this->Form->request->data['Article']['id'] = array(1, 2);
  547. $result = $this->Form->create($this->article);
  548. $expected = array(
  549. 'form' => array(
  550. 'method' => 'post',
  551. 'action' => '/articles/add',
  552. 'accept-charset' => $encoding
  553. ),
  554. 'div' => array('style' => 'display:none;'),
  555. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  556. '/div'
  557. );
  558. $this->assertTags($result, $expected);
  559. }
  560. /**
  561. * test that create() doesn't add in extra passed params.
  562. *
  563. * @return void
  564. */
  565. public function testCreatePassedArgs() {
  566. $encoding = strtolower(Configure::read('App.encoding'));
  567. $this->Form->request->data['Article']['id'] = 1;
  568. $result = $this->Form->create($this->article, array(
  569. 'type' => 'post',
  570. 'escape' => false,
  571. 'url' => array(
  572. 'action' => 'edit',
  573. 'myparam'
  574. )
  575. ));
  576. $expected = array(
  577. 'form' => array(
  578. 'method' => 'post',
  579. 'action' => '/articles/edit/myparam',
  580. 'accept-charset' => $encoding
  581. ),
  582. 'div' => array('style' => 'display:none;'),
  583. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  584. '/div'
  585. );
  586. $this->assertTags($result, $expected);
  587. }
  588. /**
  589. * test creating a get form, and get form inputs.
  590. *
  591. * @return void
  592. */
  593. public function testGetFormCreate() {
  594. $encoding = strtolower(Configure::read('App.encoding'));
  595. $result = $this->Form->create($this->article, array('type' => 'get'));
  596. $this->assertTags($result, array('form' => array(
  597. 'method' => 'get', 'action' => '/articles/add',
  598. 'accept-charset' => $encoding
  599. )));
  600. $result = $this->Form->text('title');
  601. $this->assertTags($result, array('input' => array(
  602. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  603. )));
  604. $result = $this->Form->password('password');
  605. $this->assertTags($result, array('input' => array(
  606. 'name' => 'password', 'type' => 'password'
  607. )));
  608. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  609. $result = $this->Form->text('user_form');
  610. $this->assertTags($result, array('input' => array(
  611. 'name' => 'user_form', 'type' => 'text'
  612. )));
  613. }
  614. /**
  615. * test get form, and inputs when the model param is false
  616. *
  617. * @return void
  618. */
  619. public function testGetFormWithFalseModel() {
  620. $encoding = strtolower(Configure::read('App.encoding'));
  621. $this->Form->request['controller'] = 'contact_test';
  622. $result = $this->Form->create(false, array(
  623. 'type' => 'get', 'url' => array('controller' => 'contact_test')
  624. ));
  625. $expected = array('form' => array(
  626. 'method' => 'get', 'action' => '/contact_test/add',
  627. 'accept-charset' => $encoding
  628. ));
  629. $this->assertTags($result, $expected);
  630. $result = $this->Form->text('reason');
  631. $expected = array(
  632. 'input' => array('type' => 'text', 'name' => 'reason')
  633. );
  634. $this->assertTags($result, $expected);
  635. }
  636. /**
  637. * testFormCreateWithSecurity method
  638. *
  639. * Test form->create() with security key.
  640. *
  641. * @return void
  642. */
  643. public function testCreateWithSecurity() {
  644. $this->Form->request->params['_csrfToken'] = 'testKey';
  645. $encoding = strtolower(Configure::read('App.encoding'));
  646. $result = $this->Form->create($this->article, [
  647. 'url' => '/articles/publish',
  648. ]);
  649. $expected = array(
  650. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  651. 'div' => array('style' => 'display:none;'),
  652. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  653. array('input' => array(
  654. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  655. )),
  656. '/div'
  657. );
  658. $this->assertTags($result, $expected);
  659. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  660. $expected['form']['id'] = 'MyForm';
  661. $this->assertTags($result, $expected);
  662. }
  663. /**
  664. * testFormCreateGetNoSecurity method
  665. *
  666. * Test form->create() with no security key as its a get form
  667. *
  668. * @return void
  669. */
  670. public function testCreateEndGetNoSecurity() {
  671. $this->Form->request->params['_csrfToken'] = 'testKey';
  672. $encoding = strtolower(Configure::read('App.encoding'));
  673. $article = new Article();
  674. $result = $this->Form->create($article, [
  675. 'type' => 'get',
  676. 'url' => '/contacts/add'
  677. ]);
  678. $this->assertNotContains('testKey', $result);
  679. $result = $this->Form->end('Save');
  680. $this->assertNotContains('testKey', $result);
  681. }
  682. /**
  683. * test that create() clears the fields property so it starts fresh
  684. *
  685. * @return void
  686. */
  687. public function testCreateClearingFields() {
  688. $this->Form->fields = array('model_id');
  689. $this->Form->create($this->article);
  690. $this->assertEquals(array(), $this->Form->fields);
  691. }
  692. /**
  693. * Tests form hash generation with model-less data
  694. *
  695. * @return void
  696. */
  697. public function testValidateHashNoModel() {
  698. $this->Form->request->params['_Token'] = 'foo';
  699. $result = $this->Form->secure(array('anything'));
  700. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  701. }
  702. /**
  703. * Tests that hidden fields generated for checkboxes don't get locked
  704. *
  705. * @return void
  706. */
  707. public function testNoCheckboxLocking() {
  708. $this->Form->request->params['_Token'] = 'foo';
  709. $this->assertSame([], $this->Form->fields);
  710. $this->Form->checkbox('check', array('value' => '1'));
  711. $this->assertSame($this->Form->fields, array('check'));
  712. }
  713. /**
  714. * testFormSecurityFields method
  715. *
  716. * Test generation of secure form hash generation.
  717. *
  718. * @return void
  719. */
  720. public function testFormSecurityFields() {
  721. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  722. $this->Form->request->params['_Token'] = 'testKey';
  723. $result = $this->Form->secure($fields);
  724. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  725. $hash .= ':' . 'Model.valid';
  726. $hash = urlencode($hash);
  727. $expected = array(
  728. 'div' => array('style' => 'display:none;'),
  729. array('input' => array(
  730. 'type' => 'hidden',
  731. 'name' => '_Token[fields]',
  732. 'value' => $hash
  733. )),
  734. array('input' => array(
  735. 'type' => 'hidden',
  736. 'name' => '_Token[unlocked]',
  737. 'value' => '',
  738. )),
  739. '/div'
  740. );
  741. $this->assertTags($result, $expected);
  742. }
  743. /**
  744. * Tests correct generation of number fields for double and float fields
  745. *
  746. * @return void
  747. */
  748. public function testTextFieldGenerationForFloats() {
  749. $this->article['schema'] = [
  750. 'foo' => [
  751. 'type' => 'float',
  752. 'null' => false,
  753. 'default' => null,
  754. 'length' => 10
  755. ]
  756. ];
  757. $this->Form->create($this->article);
  758. $result = $this->Form->input('foo');
  759. $expected = array(
  760. 'div' => array('class' => 'input number'),
  761. 'label' => array('for' => 'foo'),
  762. 'Foo',
  763. '/label',
  764. array('input' => array(
  765. 'type' => 'number',
  766. 'name' => 'foo',
  767. 'id' => 'foo',
  768. 'step' => 'any'
  769. )),
  770. '/div'
  771. );
  772. $this->assertTags($result, $expected);
  773. $result = $this->Form->input('foo', array('step' => 0.5));
  774. $expected = array(
  775. 'div' => array('class' => 'input number'),
  776. 'label' => array('for' => 'foo'),
  777. 'Foo',
  778. '/label',
  779. array('input' => array(
  780. 'type' => 'number',
  781. 'name' => 'foo',
  782. 'id' => 'foo',
  783. 'step' => '0.5'
  784. )),
  785. '/div'
  786. );
  787. $this->assertTags($result, $expected);
  788. }
  789. /**
  790. * Tests correct generation of number fields for integer fields
  791. *
  792. * @return void
  793. */
  794. public function testTextFieldTypeNumberGenerationForIntegers() {
  795. TableRegistry::get('Contacts', [
  796. 'className' => __NAMESPACE__ . '\ContactsTable'
  797. ]);
  798. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  799. $result = $this->Form->input('age');
  800. $expected = array(
  801. 'div' => array('class' => 'input number'),
  802. 'label' => array('for' => 'age'),
  803. 'Age',
  804. '/label',
  805. array('input' => array(
  806. 'type' => 'number', 'name' => 'age',
  807. 'id' => 'age'
  808. )),
  809. '/div'
  810. );
  811. $this->assertTags($result, $expected);
  812. }
  813. /**
  814. * Tests correct generation of file upload fields for binary fields
  815. *
  816. * @return void
  817. */
  818. public function testFileUploadFieldTypeGenerationForBinaries() {
  819. $table = TableRegistry::get('Contacts', [
  820. 'className' => __NAMESPACE__ . '\ContactsTable'
  821. ]);
  822. $table->schema(array('foo' => array(
  823. 'type' => 'binary',
  824. 'null' => false,
  825. 'default' => null,
  826. 'length' => 1024
  827. )));
  828. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  829. $result = $this->Form->input('foo');
  830. $expected = array(
  831. 'div' => array('class' => 'input file'),
  832. 'label' => array('for' => 'foo'),
  833. 'Foo',
  834. '/label',
  835. array('input' => array(
  836. 'type' => 'file', 'name' => 'foo',
  837. 'id' => 'foo'
  838. )),
  839. '/div'
  840. );
  841. $this->assertTags($result, $expected);
  842. }
  843. /**
  844. * testFormSecurityMultipleFields method
  845. *
  846. * Test secure() with multiple row form. Ensure hash is correct.
  847. *
  848. * @return void
  849. */
  850. public function testFormSecurityMultipleFields() {
  851. $this->Form->request->params['_Token'] = 'foo';
  852. $fields = array(
  853. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  854. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  855. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  856. );
  857. $result = $this->Form->secure($fields);
  858. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  859. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  860. $expected = array(
  861. 'div' => array('style' => 'display:none;'),
  862. array('input' => array(
  863. 'type' => 'hidden', 'name' => '_Token[fields]',
  864. 'value' => $hash
  865. )),
  866. array('input' => array(
  867. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  868. 'value' => ''
  869. )),
  870. '/div'
  871. );
  872. $this->assertTags($result, $expected);
  873. }
  874. /**
  875. * testFormSecurityMultipleSubmitButtons
  876. *
  877. * test form submit generation and ensure that _Token is only created on end()
  878. *
  879. * @return void
  880. */
  881. public function testFormSecurityMultipleSubmitButtons() {
  882. $this->Form->request->params['_Token'] = 'testKey';
  883. $this->Form->create($this->article);
  884. $this->Form->text('Address.title');
  885. $this->Form->text('Address.first_name');
  886. $result = $this->Form->submit('Save', array('name' => 'save'));
  887. $expected = array(
  888. 'div' => array('class' => 'submit'),
  889. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  890. '/div',
  891. );
  892. $this->assertTags($result, $expected);
  893. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  894. $expected = array(
  895. 'div' => array('class' => 'submit'),
  896. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  897. '/div',
  898. );
  899. $this->assertTags($result, $expected);
  900. $result = $this->Form->end();
  901. $expected = array(
  902. 'div' => array('style' => 'display:none;'),
  903. array('input' => array(
  904. 'type' => 'hidden',
  905. 'name' => '_Token[fields]',
  906. 'value'
  907. )),
  908. array('input' => array(
  909. 'type' => 'hidden',
  910. 'name' => '_Token[unlocked]',
  911. 'value' => 'cancel%7Csave'
  912. )),
  913. '/div'
  914. );
  915. $this->assertTags($result, $expected);
  916. }
  917. /**
  918. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  919. *
  920. * @return void
  921. */
  922. public function testSecurityButtonNestedNamed() {
  923. $key = 'testKey';
  924. $this->Form->request->params['_csrfToken'] = $key;
  925. $this->Form->create('Addresses');
  926. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  927. $result = $this->Form->unlockField();
  928. $this->assertEquals(array('Address.button'), $result);
  929. }
  930. /**
  931. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  932. *
  933. * @return void
  934. */
  935. public function testSecuritySubmitNestedNamed() {
  936. $this->Form->request->params['_Token'] = 'testKey';
  937. $this->Form->create($this->article);
  938. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  939. $result = $this->Form->unlockField();
  940. $this->assertEquals(array('Address.button'), $result);
  941. }
  942. /**
  943. * Test that the correct fields are unlocked for image submits with no names.
  944. *
  945. * @return void
  946. */
  947. public function testSecuritySubmitImageNoName() {
  948. $key = 'testKey';
  949. $this->Form->request->params['_Token'] = 'testKey';
  950. $this->Form->create(false);
  951. $result = $this->Form->submit('save.png');
  952. $expected = array(
  953. 'div' => array('class' => 'submit'),
  954. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  955. '/div'
  956. );
  957. $this->assertTags($result, $expected);
  958. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  959. }
  960. /**
  961. * Test that the correct fields are unlocked for image submits with names.
  962. *
  963. * @return void
  964. */
  965. public function testSecuritySubmitImageName() {
  966. $this->Form->request->params['_Token'] = 'testKey';
  967. $this->Form->create(null);
  968. $result = $this->Form->submit('save.png', array('name' => 'test'));
  969. $expected = array(
  970. 'div' => array('class' => 'submit'),
  971. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  972. '/div'
  973. );
  974. $this->assertTags($result, $expected);
  975. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  976. }
  977. /**
  978. * testFormSecurityMultipleInputFields method
  979. *
  980. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  981. *
  982. * @return void
  983. */
  984. public function testFormSecurityMultipleInputFields() {
  985. $this->Form->request->params['_Token'] = 'testKey';
  986. $this->Form->create();
  987. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  988. $this->Form->input('Addresses.0.title');
  989. $this->Form->input('Addresses.0.first_name');
  990. $this->Form->input('Addresses.0.last_name');
  991. $this->Form->input('Addresses.0.address');
  992. $this->Form->input('Addresses.0.city');
  993. $this->Form->input('Addresses.0.phone');
  994. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  995. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  996. $this->Form->input('Addresses.1.title');
  997. $this->Form->input('Addresses.1.first_name');
  998. $this->Form->input('Addresses.1.last_name');
  999. $this->Form->input('Addresses.1.address');
  1000. $this->Form->input('Addresses.1.city');
  1001. $this->Form->input('Addresses.1.phone');
  1002. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  1003. $result = $this->Form->secure($this->Form->fields);
  1004. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  1005. $expected = array(
  1006. 'div' => array('style' => 'display:none;'),
  1007. array('input' => array(
  1008. 'type' => 'hidden', 'name' => '_Token[fields]',
  1009. 'value' => $hash
  1010. )),
  1011. array('input' => array(
  1012. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1013. 'value' => ''
  1014. )),
  1015. '/div'
  1016. );
  1017. $this->assertTags($result, $expected);
  1018. }
  1019. /**
  1020. * Test form security with Model.field.0 style inputs
  1021. *
  1022. * @return void
  1023. */
  1024. public function testFormSecurityArrayFields() {
  1025. $this->Form->request->params['_Token'] = 'testKey';
  1026. $this->Form->create();
  1027. $this->Form->text('Address.primary.1');
  1028. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1029. $this->Form->text('Address.secondary.1.0');
  1030. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1031. }
  1032. /**
  1033. * testFormSecurityMultipleInputDisabledFields method
  1034. *
  1035. * test secure form generation with multiple records and disabled fields.
  1036. *
  1037. * @return void
  1038. */
  1039. public function testFormSecurityMultipleInputDisabledFields() {
  1040. $this->Form->request->params['_Token'] = array(
  1041. 'unlockedFields' => array('first_name', 'address')
  1042. );
  1043. $this->Form->create();
  1044. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1045. $this->Form->text('Addresses.0.title');
  1046. $this->Form->text('Addresses.0.first_name');
  1047. $this->Form->text('Addresses.0.last_name');
  1048. $this->Form->text('Addresses.0.address');
  1049. $this->Form->text('Addresses.0.city');
  1050. $this->Form->text('Addresses.0.phone');
  1051. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1052. $this->Form->text('Addresses.1.title');
  1053. $this->Form->text('Addresses.1.first_name');
  1054. $this->Form->text('Addresses.1.last_name');
  1055. $this->Form->text('Addresses.1.address');
  1056. $this->Form->text('Addresses.1.city');
  1057. $this->Form->text('Addresses.1.phone');
  1058. $result = $this->Form->secure($this->Form->fields);
  1059. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1060. $expected = array(
  1061. 'div' => array('style' => 'display:none;'),
  1062. array('input' => array(
  1063. 'type' => 'hidden',
  1064. 'name' => '_Token[fields]',
  1065. 'value' => $hash
  1066. )),
  1067. array('input' => array(
  1068. 'type' => 'hidden',
  1069. 'name' => '_Token[unlocked]',
  1070. 'value' => 'address%7Cfirst_name',
  1071. )),
  1072. '/div'
  1073. );
  1074. $this->assertTags($result, $expected);
  1075. }
  1076. /**
  1077. * testFormSecurityInputDisabledFields method
  1078. *
  1079. * Test single record form with disabled fields.
  1080. *
  1081. * @return void
  1082. */
  1083. public function testFormSecurityInputUnlockedFields() {
  1084. $this->Form->request['_Token'] = array(
  1085. 'unlockedFields' => array('first_name', 'address')
  1086. );
  1087. $this->Form->create();
  1088. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1089. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1090. $this->Form->text('Addresses.title');
  1091. $this->Form->text('Addresses.first_name');
  1092. $this->Form->text('Addresses.last_name');
  1093. $this->Form->text('Addresses.address');
  1094. $this->Form->text('Addresses.city');
  1095. $this->Form->text('Addresses.phone');
  1096. $result = $this->Form->fields;
  1097. $expected = array(
  1098. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1099. 'Addresses.city', 'Addresses.phone'
  1100. );
  1101. $this->assertEquals($expected, $result);
  1102. $result = $this->Form->secure($expected);
  1103. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1104. $expected = array(
  1105. 'div' => array('style' => 'display:none;'),
  1106. array('input' => array(
  1107. 'type' => 'hidden',
  1108. 'name' => '_Token[fields]',
  1109. 'value' => $hash
  1110. )),
  1111. array('input' => array(
  1112. 'type' => 'hidden',
  1113. 'name' => '_Token[unlocked]',
  1114. 'value' => 'address%7Cfirst_name',
  1115. )),
  1116. '/div'
  1117. );
  1118. $this->assertTags($result, $expected);
  1119. }
  1120. /**
  1121. * test securing inputs with custom name attributes.
  1122. *
  1123. * @return void
  1124. */
  1125. public function testFormSecureWithCustomNameAttribute() {
  1126. $this->Form->request->params['_Token'] = 'testKey';
  1127. $this->Form->text('UserForm.published', array('name' => 'User[custom]'));
  1128. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1129. $this->Form->text('UserForm.published', array('name' => 'User[custom][another][value]'));
  1130. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1131. }
  1132. /**
  1133. * testFormSecuredInput method
  1134. *
  1135. * Test generation of entire secure form, assertions made on input() output.
  1136. *
  1137. * @return void
  1138. */
  1139. public function testFormSecuredInput() {
  1140. $this->Form->request->params['_csrfToken'] = 'testKey';
  1141. $this->Form->request->params['_Token'] = 'stuff';
  1142. $this->article['schema'] = [
  1143. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1144. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1145. ];
  1146. $result = $this->Form->create($this->article, array('url' => '/articles/add'));
  1147. $encoding = strtolower(Configure::read('App.encoding'));
  1148. $expected = array(
  1149. 'form' => array('method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding),
  1150. 'div' => array('style' => 'display:none;'),
  1151. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1152. array('input' => array(
  1153. 'type' => 'hidden',
  1154. 'name' => '_csrfToken',
  1155. 'value' => 'testKey'
  1156. )),
  1157. '/div'
  1158. );
  1159. $this->assertTags($result, $expected);
  1160. $result = $this->Form->input('ratio');
  1161. $expected = array(
  1162. 'div' => array('class'),
  1163. 'label' => array('for'),
  1164. 'Ratio',
  1165. '/label',
  1166. 'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
  1167. '/div',
  1168. );
  1169. $this->assertTags($result, $expected);
  1170. $result = $this->Form->input('population');
  1171. $expected = array(
  1172. 'div' => array('class'),
  1173. 'label' => array('for'),
  1174. 'Population',
  1175. '/label',
  1176. 'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
  1177. '/div',
  1178. );
  1179. $this->assertTags($result, $expected);
  1180. $result = $this->Form->input('published', array('type' => 'text'));
  1181. $expected = array(
  1182. 'div' => array('class' => 'input text'),
  1183. 'label' => array('for' => 'published'),
  1184. 'Published',
  1185. '/label',
  1186. array('input' => array(
  1187. 'type' => 'text',
  1188. 'name' => 'published',
  1189. 'id' => 'published'
  1190. )),
  1191. '/div'
  1192. );
  1193. $this->assertTags($result, $expected);
  1194. $result = $this->Form->input('other', array('type' => 'text'));
  1195. $expected = array(
  1196. 'div' => array('class' => 'input text'),
  1197. 'label' => array('for' => 'other'),
  1198. 'Other',
  1199. '/label',
  1200. array('input' => array(
  1201. 'type' => 'text',
  1202. 'name' => 'other',
  1203. 'id',
  1204. )),
  1205. '/div'
  1206. );
  1207. $this->assertTags($result, $expected);
  1208. $result = $this->Form->hidden('stuff');
  1209. $expected = array(
  1210. 'input' => array(
  1211. 'type' => 'hidden',
  1212. 'name' => 'stuff',
  1213. ));
  1214. $this->assertTags($result, $expected);
  1215. $result = $this->Form->hidden('hidden', array('value' => '0'));
  1216. $expected = array('input' => array(
  1217. 'type' => 'hidden',
  1218. 'name' => 'hidden',
  1219. 'value' => '0'
  1220. ));
  1221. $this->assertTags($result, $expected);
  1222. $result = $this->Form->input('something', array('type' => 'checkbox'));
  1223. $expected = array(
  1224. 'div' => array('class' => 'input checkbox'),
  1225. array('input' => array(
  1226. 'type' => 'hidden',
  1227. 'name' => 'something',
  1228. 'value' => '0'
  1229. )),
  1230. array('input' => array(
  1231. 'type' => 'checkbox',
  1232. 'name' => 'something',
  1233. 'value' => '1',
  1234. 'id' => 'something'
  1235. )),
  1236. 'label' => array('for' => 'something'),
  1237. 'Something',
  1238. '/label',
  1239. '/div'
  1240. );
  1241. $this->assertTags($result, $expected);
  1242. $result = $this->Form->fields;
  1243. $expected = array(
  1244. 'ratio', 'population', 'published', 'other',
  1245. 'stuff' => '',
  1246. 'hidden' => '0',
  1247. 'something'
  1248. );
  1249. $this->assertEquals($expected, $result);
  1250. $result = $this->Form->secure($this->Form->fields);
  1251. $expected = array(
  1252. 'div' => array('style' => 'display:none;'),
  1253. array('input' => array(
  1254. 'type' => 'hidden',
  1255. 'name' => '_Token[fields]',
  1256. 'value'
  1257. )),
  1258. array('input' => array(
  1259. 'type' => 'hidden',
  1260. 'name' => '_Token[unlocked]',
  1261. 'value' => ''
  1262. )),
  1263. '/div'
  1264. );
  1265. $this->assertTags($result, $expected);
  1266. }
  1267. /**
  1268. * Test secured inputs with custom names.
  1269. *
  1270. * @return void
  1271. */
  1272. public function testSecuredInputCustomName() {
  1273. $this->Form->request->params['_Token'] = 'testKey';
  1274. $this->assertEquals(array(), $this->Form->fields);
  1275. $this->Form->text('text_input', array(
  1276. 'name' => 'Option[General.default_role]',
  1277. ));
  1278. $expected = array('Option.General.default_role');
  1279. $this->assertEquals($expected, $this->Form->fields);
  1280. $this->Form->select('select_box', [1, 2], [
  1281. 'name' => 'Option[General.select_role]',
  1282. ]);
  1283. $expected = ['Option.General.default_role', 'Option.General.select_role'];
  1284. $this->assertEquals($expected, $this->Form->fields);
  1285. }
  1286. /**
  1287. * Tests that the correct keys are added to the field hash index
  1288. *
  1289. * @return void
  1290. */
  1291. public function testFormSecuredFileInput() {
  1292. $this->assertEquals(array(), $this->Form->fields);
  1293. $this->Form->file('Attachment.file');
  1294. $expected = array(
  1295. 'Attachment.file.name', 'Attachment.file.type',
  1296. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1297. 'Attachment.file.size'
  1298. );
  1299. $this->assertEquals($expected, $this->Form->fields);
  1300. }
  1301. /**
  1302. * test that multiple selects keys are added to field hash
  1303. *
  1304. * @return void
  1305. */
  1306. public function testFormSecuredMultipleSelect() {
  1307. $this->Form->request->params['_csrfToken'] = 'testKey';
  1308. $this->assertEquals(array(), $this->Form->fields);
  1309. $options = array('1' => 'one', '2' => 'two');
  1310. $this->Form->select('Model.select', $options);
  1311. $expected = array('Model.select');
  1312. $this->assertEquals($expected, $this->Form->fields);
  1313. $this->Form->fields = array();
  1314. $this->Form->select('Model.select', $options, array('multiple' => true));
  1315. $this->assertEquals($expected, $this->Form->fields);
  1316. }
  1317. /**
  1318. * testFormSecuredRadio method
  1319. *
  1320. * @return void
  1321. */
  1322. public function testFormSecuredRadio() {
  1323. $this->Form->request->params['_Token'] = 'testKey';
  1324. $this->assertEquals(array(), $this->Form->fields);
  1325. $options = array('1' => 'option1', '2' => 'option2');
  1326. $this->Form->radio('Test.test', $options);
  1327. $expected = array('Test.test');
  1328. $this->assertEquals($expected, $this->Form->fields);
  1329. }
  1330. /**
  1331. * Test that when disabled is in a list based attribute array it works.
  1332. *
  1333. * @return void
  1334. */
  1335. public function testFormSecuredAndDisabledNotAssoc() {
  1336. $this->Form->request->params['_Token'] = 'testKey';
  1337. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1338. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1339. $this->Form->text('Model.text', array('disabled'));
  1340. $this->Form->textarea('Model.textarea', array('disabled'));
  1341. $this->Form->password('Model.password', array('disabled'));
  1342. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1343. $expected = array(
  1344. 'Model.radio' => ''
  1345. );
  1346. $this->assertEquals($expected, $this->Form->fields);
  1347. }
  1348. /**
  1349. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1350. * hashing.
  1351. *
  1352. * @return void
  1353. */
  1354. public function testFormSecuredAndDisabled() {
  1355. $this->Form->request->params['_Token'] = 'testKey';
  1356. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1357. $this->Form->text('Model.text', array('disabled' => true));
  1358. $this->Form->password('Model.text', array('disabled' => true));
  1359. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1360. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1361. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1362. $this->Form->year('Model.year', array('disabled' => true));
  1363. $this->Form->month('Model.month', array('disabled' => true));
  1364. $this->Form->day('Model.day', array('disabled' => true));
  1365. $this->Form->hour('Model.hour', array('disabled' => true));
  1366. $this->Form->minute('Model.minute', array('disabled' => true));
  1367. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1368. $expected = array(
  1369. 'Model.radio' => ''
  1370. );
  1371. $this->assertEquals($expected, $this->Form->fields);
  1372. }
  1373. /**
  1374. * testDisableSecurityUsingForm method
  1375. *
  1376. * @return void
  1377. */
  1378. public function testDisableSecurityUsingForm() {
  1379. $this->Form->request['_Token'] = [
  1380. 'disabledFields' => []
  1381. ];
  1382. $this->Form->create();
  1383. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1384. $this->Form->text('Addresses.title');
  1385. $this->Form->text('Addresses.first_name', ['secure' => false]);
  1386. $this->Form->textarea('Addresses.city', ['secure' => false]);
  1387. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  1388. $result = $this->Form->fields;
  1389. $expected = [
  1390. 'Addresses.id' => '123456', 'Addresses.title',
  1391. ];
  1392. $this->assertEquals($expected, $result);
  1393. }
  1394. /**
  1395. * test disableField
  1396. *
  1397. * @return void
  1398. */
  1399. public function testUnlockFieldAddsToList() {
  1400. $this->Form->request['_Token'] = array(
  1401. 'unlockedFields' => array()
  1402. );
  1403. $this->Form->unlockField('Contact.name');
  1404. $this->Form->text('Contact.name');
  1405. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1406. $this->assertEquals(array(), $this->Form->fields);
  1407. }
  1408. /**
  1409. * test unlockField removing from fields array.
  1410. *
  1411. * @return void
  1412. */
  1413. public function testUnlockFieldRemovingFromFields() {
  1414. $this->Form->request['_Token'] = array(
  1415. 'unlockedFields' => array()
  1416. );
  1417. $this->Form->create($this->article);
  1418. $this->Form->hidden('Article.id', array('value' => 1));
  1419. $this->Form->text('Article.title');
  1420. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  1421. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  1422. $this->Form->unlockField('Article.title');
  1423. $this->Form->unlockField('Article.id');
  1424. $this->assertEquals(array(), $this->Form->fields);
  1425. }
  1426. /**
  1427. * test error message display
  1428. *
  1429. * @return void
  1430. */
  1431. public function testErrorMessageDisplay() {
  1432. $this->article['errors'] = [
  1433. 'Article' => ['title' => 'error message']
  1434. ];
  1435. $this->Form->create($this->article);
  1436. $result = $this->Form->input('Article.title');
  1437. $expected = [
  1438. 'div' => ['class' => 'input text error'],
  1439. 'label' => ['for' => 'article-title'],
  1440. 'Title',
  1441. '/label',
  1442. 'input' => [
  1443. 'type' => 'text', 'name' => 'Article[title]',
  1444. 'id' => 'article-title', 'class' => 'form-error'
  1445. ],
  1446. ['div' => ['class' => 'error-message']],
  1447. 'error message',
  1448. '/div',
  1449. '/div'
  1450. ];
  1451. $this->assertTags($result, $expected);
  1452. $result = $this->Form->input('Article.title', [
  1453. 'templates' => [
  1454. 'groupContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  1455. ]
  1456. ]);
  1457. $expected = [
  1458. 'div' => ['class' => 'input text error'],
  1459. 'label' => ['for' => 'article-title'],
  1460. 'Title',
  1461. '/label',
  1462. 'input' => [
  1463. 'type' => 'text', 'name' => 'Article[title]',
  1464. 'id' => 'article-title', 'class' => 'form-error'
  1465. ],
  1466. '/div'
  1467. ];
  1468. $this->assertTags($result, $expected);
  1469. }
  1470. /**
  1471. * Test validation errors, when validation message is an empty string.
  1472. *
  1473. * @return void
  1474. */
  1475. public function testEmptyErrorValidation() {
  1476. $this->article['errors'] = [
  1477. 'Article' => ['title' => '']
  1478. ];
  1479. $this->Form->create($this->article);
  1480. $result = $this->Form->input('Article.title');
  1481. $expected = [
  1482. 'div' => ['class' => 'input text error'],
  1483. 'label' => ['for' => 'article-title'],
  1484. 'Title',
  1485. '/label',
  1486. 'input' => [
  1487. 'type' => 'text', 'name' => 'Article[title]',
  1488. 'id' => 'article-title', 'class' => 'form-error'
  1489. ],
  1490. ['div' => ['class' => 'error-message']],
  1491. [],
  1492. '/div',
  1493. '/div'
  1494. ];
  1495. $this->assertTags($result, $expected);
  1496. }
  1497. /**
  1498. * Test validation errors, when calling input() overriding validation message by an empty string.
  1499. *
  1500. * @return void
  1501. */
  1502. public function testEmptyInputErrorValidation() {
  1503. $this->article['errors'] = [
  1504. 'Article' => ['title' => 'error message']
  1505. ];
  1506. $this->Form->create($this->article);
  1507. $result = $this->Form->input('Article.title', array('error' => ''));
  1508. $expected = [
  1509. 'div' => ['class' => 'input text error'],
  1510. 'label' => ['for' => 'article-title'],
  1511. 'Title',
  1512. '/label',
  1513. 'input' => [
  1514. 'type' => 'text', 'name' => 'Article[title]',
  1515. 'id' => 'article-title', 'class' => 'form-error'
  1516. ],
  1517. ['div' => ['class' => 'error-message']],
  1518. [],
  1519. '/div',
  1520. '/div'
  1521. ];
  1522. $this->assertTags($result, $expected);
  1523. }
  1524. /**
  1525. * Tests displaying errors for nested entities
  1526. *
  1527. * @return void
  1528. */
  1529. public function testFormValidationAssociated() {
  1530. $nested = new Entity(['foo' => 'bar']);
  1531. $nested->errors('foo', ['not a valid bar']);
  1532. $entity = new Entity(['nested' => $nested]);
  1533. $this->Form->create($entity);
  1534. $result = $this->Form->error('nested.foo');
  1535. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  1536. }
  1537. /**
  1538. * testFormValidationAssociatedSecondLevel method
  1539. *
  1540. * test form error display with associated model.
  1541. *
  1542. * @return void
  1543. */
  1544. public function testFormValidationAssociatedSecondLevel() {
  1545. $inner = new Entity(['bar' => 'baz']);
  1546. $nested = new Entity(['foo' => $inner]);
  1547. $entity = new Entity(['nested' => $nested]);
  1548. $inner->errors('bar', ['not a valid one']);
  1549. $this->Form->create($entity);
  1550. $result = $this->Form->error('nested.foo.bar');
  1551. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  1552. }
  1553. /**
  1554. * testFormValidationMultiRecord method
  1555. *
  1556. * test form error display with multiple records.
  1557. *
  1558. * @return void
  1559. */
  1560. public function testFormValidationMultiRecord() {
  1561. $one = new Entity;
  1562. $two = new Entity;
  1563. TableRegistry::get('Contacts', [
  1564. 'className' => __NAMESPACE__ . '\ContactsTable'
  1565. ]);
  1566. $one->errors('email', ['invalid email']);
  1567. $two->errors('name', ['This is wrong']);
  1568. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  1569. $result = $this->Form->input('Contacts.0.email');
  1570. $expected = array(
  1571. 'div' => array('class' => 'input email error'),
  1572. 'label' => array('for' => 'contacts-0-email'),
  1573. 'Email',
  1574. '/label',
  1575. 'input' => array(
  1576. 'type' => 'text', 'name' => 'Contacts[0][email]', 'id' => 'contacts-0-email',
  1577. 'class' => 'form-error', 'maxlength' => 255
  1578. ),
  1579. array('div' => array('class' => 'error-message')),
  1580. 'invalid email',
  1581. '/div',
  1582. '/div'
  1583. );
  1584. $result = $this->Form->input('Contacts.1.name');
  1585. $expected = array(
  1586. 'div' => array('class' => 'input text error'),
  1587. 'label' => array('for' => 'contacts-1-name'),
  1588. 'Name',
  1589. '/label',
  1590. 'input' => array(
  1591. 'type' => 'text', 'name' => 'Contacts[1][name]', 'id' => 'contacts-1-name',
  1592. 'class' => 'form-error', 'maxlength' => 255
  1593. ),
  1594. array('div' => array('class' => 'error-message')),
  1595. 'This is wrong',
  1596. '/div',
  1597. '/div'
  1598. );
  1599. $this->assertTags($result, $expected);
  1600. }
  1601. /**
  1602. * testInput method
  1603. *
  1604. * Test various incarnations of input().
  1605. *
  1606. * @return void
  1607. */
  1608. public function testInput() {
  1609. TableRegistry::get('ValidateUsers', [
  1610. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  1611. ]);
  1612. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  1613. $result = $this->Form->input('ValidateUsers.balance');
  1614. $expected = array(
  1615. 'div' => array('class'),
  1616. 'label' => array('for'),
  1617. 'Balance',
  1618. '/label',
  1619. 'input' => array('name', 'type' => 'number', 'id', 'step'),
  1620. '/div',
  1621. );
  1622. $this->assertTags($result, $expected);
  1623. $result = $this->Form->input('ValidateUser.cost_decimal');
  1624. $expected = array(
  1625. 'div' => array('class'),
  1626. 'label' => array('for'),
  1627. 'Cost Decimal',
  1628. '/label',
  1629. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  1630. '/div',
  1631. );
  1632. $this->assertTags($result, $expected);
  1633. }
  1634. /**
  1635. * Tests the input method and passing custom options
  1636. *
  1637. * @return void
  1638. */
  1639. public function testInputCustomization() {
  1640. TableRegistry::get('Contacts', [
  1641. 'className' => __NAMESPACE__ . '\ContactsTable'
  1642. ]);
  1643. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1644. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1645. $expected = array(
  1646. 'div' => array('class' => 'input email'),
  1647. 'label' => array('for' => 'custom'),
  1648. 'Email',
  1649. '/label',
  1650. array('input' => array(
  1651. 'type' => 'email', 'name' => 'Contact[email]',
  1652. 'id' => 'custom', 'maxlength' => 255
  1653. )),
  1654. '/div'
  1655. );
  1656. $this->assertTags($result, $expected);
  1657. $result = $this->Form->input('Contact.email', array(
  1658. 'templates' => ['groupContainer' => '<div>{{content}}</div>']
  1659. ));
  1660. $expected = array(
  1661. '<div',
  1662. 'label' => array('for' => 'contact-email'),
  1663. 'Email',
  1664. '/label',
  1665. array('input' => array(
  1666. 'type' => 'email', 'name' => 'Contact[email]',
  1667. 'id' => 'contact-email', 'maxlength' => 255
  1668. )),
  1669. '/div'
  1670. );
  1671. $this->assertTags($result, $expected);
  1672. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1673. $expected = array(
  1674. 'div' => array('class' => 'input text'),
  1675. 'label' => array('for' => 'contact-email'),
  1676. 'Email',
  1677. '/label',
  1678. array('input' => array(
  1679. 'type' => 'text', 'name' => 'Contact[email]',
  1680. 'id' => 'contact-email', 'maxlength' => '255'
  1681. )),
  1682. '/div'
  1683. );
  1684. $this->assertTags($result, $expected);
  1685. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1686. $expected = array(
  1687. 'div' => array('class' => 'input text'),
  1688. 'label' => array('for' => 'contact-5-email'),
  1689. 'Email',
  1690. '/label',
  1691. array('input' => array(
  1692. 'type' => 'text', 'name' => 'Contact[5][email]',
  1693. 'id' => 'contact-5-email', 'maxlength' => '255'
  1694. )),
  1695. '/div'
  1696. );
  1697. $this->assertTags($result, $expected);
  1698. $result = $this->Form->input('Contact.password');
  1699. $expected = array(
  1700. 'div' => array('class' => 'input password'),
  1701. 'label' => array('for' => 'contact-password'),
  1702. 'Password',
  1703. '/label',
  1704. array('input' => array(
  1705. 'type' => 'password', 'name' => 'Contact[password]',
  1706. 'id' => 'contact-password'
  1707. )),
  1708. '/div'
  1709. );
  1710. $this->assertTags($result, $expected);
  1711. $result = $this->Form->input('Contact.email', array(
  1712. 'type' => 'file', 'class' => 'textbox'
  1713. ));
  1714. $expected = array(
  1715. 'div' => array('class' => 'input file'),
  1716. 'label' => array('for' => 'contact-email'),
  1717. 'Email',
  1718. '/label',
  1719. array('input' => array(
  1720. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  1721. 'id' => 'contact-email'
  1722. )),
  1723. '/div'
  1724. );
  1725. $this->assertTags($result, $expected);
  1726. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  1727. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  1728. $result = $this->Form->input('Contact.phone');
  1729. $expected = array(
  1730. 'div' => array('class' => 'input tel'),
  1731. 'label' => array('for' => 'contact-phone'),
  1732. 'Phone',
  1733. '/label',
  1734. array('input' => array(
  1735. 'type' => 'tel', 'name' => 'Contact[phone]',
  1736. 'value' => 'Hello &amp; World &gt; weird chars',
  1737. 'id' => 'contact-phone', 'maxlength' => 255
  1738. )),
  1739. '/div'
  1740. );
  1741. $this->assertTags($result, $expected);
  1742. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1743. $this->Form->create();
  1744. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1745. $expected = array(
  1746. 'div' => array('class' => 'input text'),
  1747. 'label' => array('for' => 'myId'),
  1748. 'Field',
  1749. '/label',
  1750. 'input' => array(
  1751. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  1752. 'value' => 'My value', 'id' => 'myId'
  1753. ),
  1754. '/div'
  1755. );
  1756. $this->assertTags($result, $expected);
  1757. unset($this->Form->request->data);
  1758. $entity->errors('field', 'Badness!');
  1759. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  1760. $result = $this->Form->input('Contact.field');
  1761. $expected = array(
  1762. 'div' => array('class' => 'input text error'),
  1763. 'label' => array('for' => 'contact-field'),
  1764. 'Field',
  1765. '/label',
  1766. 'input' => array(
  1767. 'type' => 'text', 'name' => 'Contact[field]',
  1768. 'id' => 'contact-field', 'class' => 'form-error'
  1769. ),
  1770. array('div' => array('class' => 'error-message')),
  1771. 'Badness!',
  1772. '/div',
  1773. '/div'
  1774. );
  1775. $this->assertTags($result, $expected);
  1776. $result = $this->Form->input('Contact.field', array(
  1777. 'templates' => [
  1778. 'groupContainerError' => '{{content}}{{error}}',
  1779. 'error' => '<span class="error-message">{{content}}</span>'
  1780. ]
  1781. ));
  1782. $expected = array(
  1783. 'label' => array('for' => 'contact-field'),
  1784. 'Field',
  1785. '/label',
  1786. 'input' => array(
  1787. 'type' => 'text', 'name' => 'Contact[field]',
  1788. 'id' => 'contact-field', 'class' => 'form-error'
  1789. ),
  1790. array('span' => array('class' => 'error-message')),
  1791. 'Badness!',
  1792. '/span'
  1793. );
  1794. $this->assertTags($result, $expected);
  1795. $entity->errors('field', ['minLength']);
  1796. $result = $this->Form->input('Contact.field', array(
  1797. 'error' => array(
  1798. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1799. 'maxLength' => 'login too large'
  1800. )
  1801. ));
  1802. $expected = array(
  1803. 'div' => array('class' => 'input text error'),
  1804. 'label' => array('for' => 'contact-field'),
  1805. 'Field',
  1806. '/label',
  1807. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'contact-field', 'class' => 'form-error'),
  1808. array('div' => array('class' => 'error-message')),
  1809. 'Le login doit contenir au moins 2 caractères',
  1810. '/div',
  1811. '/div'
  1812. );
  1813. $this->assertTags($result, $expected);
  1814. $entity->errors('field', ['maxLength']);
  1815. $result = $this->Form->input('Contact.field', array(
  1816. 'error' => array(
  1817. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1818. 'maxLength' => 'login too large',
  1819. )
  1820. ));
  1821. $expected = array(
  1822. 'div' => array('class' => 'input text error'),
  1823. 'label' => array('for' => 'contact-field'),
  1824. 'Field',
  1825. '/label',
  1826. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'contact-field', 'class' => 'form-error'),
  1827. array('div' => array('class' => 'error-message')),
  1828. 'login too large',
  1829. '/div',
  1830. '/div'
  1831. );
  1832. $this->assertTags($result, $expected);
  1833. }
  1834. /**
  1835. * Test that inputs with 0 can be created.
  1836. *
  1837. * @return void
  1838. */
  1839. public function testInputZero() {
  1840. TableRegistry::get('Contacts', [
  1841. 'className' => __NAMESPACE__ . '\ContactsTable'
  1842. ]);
  1843. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1844. $result = $this->Form->input('0');
  1845. $expected = array(
  1846. 'div' => array('class' => 'input text'),
  1847. 'label' => array('for' => '0'), '/label',
  1848. 'input' => array('type' => 'text', 'name' => '0', 'id' => '0'),
  1849. '/div'
  1850. );
  1851. $this->assertTags($result, $expected);
  1852. }
  1853. /**
  1854. * test input() with checkbox creation
  1855. *
  1856. * @return void
  1857. */
  1858. public function testInputCheckbox() {
  1859. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1860. $expected = array(
  1861. 'div' => array('class' => 'input checkbox'),
  1862. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  1863. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  1864. '/div'
  1865. );
  1866. $this->assertTags($result, $expected);
  1867. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1868. $expected = array(
  1869. 'div' => array('class' => 'input checkbox'),
  1870. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  1871. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  1872. '/div'
  1873. );
  1874. $this->assertTags($result, $expected);
  1875. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1876. $expected = array(
  1877. 'div' => array('class' => 'input checkbox'),
  1878. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  1879. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  1880. '/div'
  1881. );
  1882. $this->assertTags($result, $expected);
  1883. $result = $this->Form->input('User.disabled', array(
  1884. 'label' => 'Disabled',
  1885. 'type' => 'checkbox',
  1886. 'data-foo' => 'disabled'
  1887. ));
  1888. $expected = array(
  1889. 'div' => array('class' => 'input checkbox'),
  1890. 'input' => array('type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'),
  1891. array('input' => array(
  1892. 'type' => 'checkbox',
  1893. 'name' => 'User[disabled]',
  1894. 'value' => '1',
  1895. 'id' => 'user-disabled',
  1896. 'data-foo' => 'disabled'
  1897. )),
  1898. 'label' => array('for' => 'user-disabled'),
  1899. 'Disabled',
  1900. '/label',
  1901. '/div'
  1902. );
  1903. $this->assertTags($result, $expected);
  1904. }
  1905. /**
  1906. * test form->input() with datetime
  1907. *
  1908. * @return void
  1909. */
  1910. public function testInputDatetime() {
  1911. $this->Form = $this->getMock(
  1912. 'Cake\View\Helper\FormHelper',
  1913. ['datetime'],
  1914. [new View(null)]
  1915. );
  1916. $this->Form->expects($this->once())->method('datetime')
  1917. ->with('prueba', [
  1918. 'type' => 'datetime',
  1919. 'timeFormat' => 24,
  1920. 'minYear' => 2008,
  1921. 'maxYear' => 2011,
  1922. 'interval' => 15,
  1923. 'options' => null,
  1924. 'empty' => false,
  1925. 'id' => 'prueba',
  1926. 'required' => false,
  1927. ])
  1928. ->will($this->returnValue('This is it!'));
  1929. $result = $this->Form->input('prueba', array(
  1930. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  1931. 'maxYear' => 2011, 'interval' => 15
  1932. ));
  1933. $expected = array(
  1934. 'div' => array('class' => 'input datetime'),
  1935. 'label' => array('for' => 'prueba'),
  1936. 'Prueba',
  1937. '/label',
  1938. 'This is it!',
  1939. '/div'
  1940. );
  1941. $this->assertTags($result, $expected);
  1942. }
  1943. /**
  1944. * Test generating checkboxes with disabled elements.
  1945. *
  1946. * @return void
  1947. */
  1948. public function testInputCheckboxWithDisabledElements() {
  1949. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  1950. $result = $this->Form->input('Contact.multiple', array(
  1951. 'multiple' => 'checkbox',
  1952. 'disabled' => 'disabled',
  1953. 'options' => $options
  1954. ));
  1955. $expected = array(
  1956. array('div' => array('class' => 'input select')),
  1957. array('label' => array('for' => "contact-multiple")),
  1958. 'Multiple',
  1959. '/label',
  1960. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '')),
  1961. array('div' => array('class' => 'checkbox')),
  1962. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1")),
  1963. array('label' => array('for' => "contact-multiple-1")),
  1964. 'One',
  1965. '/label',
  1966. '/div',
  1967. array('div' => array('class' => 'checkbox')),
  1968. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2")),
  1969. array('label' => array('for' => "contact-multiple-2")),
  1970. 'Two',
  1971. '/label',
  1972. '/div',
  1973. array('div' => array('class' => 'checkbox')),
  1974. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3")),
  1975. array('label' => array('for' => "contact-multiple-3")),
  1976. 'Three',
  1977. '/label',
  1978. '/div',
  1979. '/div'
  1980. );
  1981. $this->assertTags($result, $expected);
  1982. // make sure 50 does only disable 50, and not 50f5c0cf
  1983. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  1984. $disabled = array(50);
  1985. $expected = array(
  1986. array('div' => array('class' => 'input select')),
  1987. array('label' => array('for' => "contact-multiple")),
  1988. 'Multiple',
  1989. '/label',
  1990. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '')),
  1991. array('div' => array('class' => 'checkbox')),
  1992. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50")),
  1993. array('label' => array('for' => "contact-multiple-50")),
  1994. 'Fifty',
  1995. '/label',
  1996. '/div',
  1997. array('div' => array('class' => 'checkbox')),
  1998. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf")),
  1999. array('label' => array('for' => "contact-multiple-50f5c0cf")),
  2000. 'Stringy',
  2001. '/label',
  2002. '/div',
  2003. '/div'
  2004. );
  2005. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2006. $this->assertTags($result, $expected);
  2007. }
  2008. /**
  2009. * test input name with leading integer, ensure attributes are generated correctly.
  2010. *
  2011. * @return void
  2012. */
  2013. public function testInputWithLeadingInteger() {
  2014. $result = $this->Form->text('0.Node.title');
  2015. $expected = array(
  2016. 'input' => array('name' => '0[Node][title]', 'type' => 'text')
  2017. );
  2018. $this->assertTags($result, $expected);
  2019. }
  2020. /**
  2021. * test form->input() with select type inputs.
  2022. *
  2023. * @return void
  2024. */
  2025. public function testInputSelectType() {
  2026. $result = $this->Form->input('email', array(
  2027. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2028. );
  2029. $expected = array(
  2030. 'div' => array('class' => 'input select'),
  2031. 'label' => array('for' => 'email'),
  2032. 'Email',
  2033. '/label',
  2034. array('select' => array('name' => 'email', 'id' => 'email')),
  2035. array('option' => array('value' => '')),
  2036. '/option',
  2037. array('option' => array('value' => 'è')),
  2038. 'Firést',
  2039. '/option',
  2040. array('option' => array('value' => 'é')),
  2041. 'Secoènd',
  2042. '/option',
  2043. '/select',
  2044. '/div'
  2045. );
  2046. $this->assertTags($result, $expected);
  2047. $result = $this->Form->input('email', array(
  2048. 'options' => array('First', 'Second'), 'empty' => true)
  2049. );
  2050. $expected = array(
  2051. 'div' => array('class' => 'input select'),
  2052. 'label' => array('for' => 'email'),
  2053. 'Email',
  2054. '/label',
  2055. array('select' => array('name' => 'email', 'id' => 'email')),
  2056. array('option' => array('value' => '')),
  2057. '/option',
  2058. array('option' => array('value' => '0')),
  2059. 'First',
  2060. '/option',
  2061. array('option' => array('value' => '1')),
  2062. 'Second',
  2063. '/option',
  2064. '/select',
  2065. '/div'
  2066. );
  2067. $this->assertTags($result, $expected);
  2068. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2069. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2070. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2071. $expected = array(
  2072. 'div' => array('class' => 'input select'),
  2073. 'label' => array('for' => 'model-user-id'),
  2074. 'User',
  2075. '/label',
  2076. 'select' => array('name' => 'Model[user_id]', 'id' => 'model-user-id'),
  2077. array('option' => array('value' => '')),
  2078. '/option',
  2079. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2080. 'good',
  2081. '/option',
  2082. array('option' => array('value' => 'other')),
  2083. 'bad',
  2084. '/option',
  2085. '/select',
  2086. '/div'
  2087. );
  2088. $this->assertTags($result, $expected);
  2089. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2090. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2091. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2092. $expected = array(
  2093. 'div' => array('class' => 'input select'),
  2094. 'label' => array('for' => 'thing-user-id'),
  2095. 'User',
  2096. '/label',
  2097. 'select' => array('name' => 'Thing[user_id]', 'id' => 'thing-user-id'),
  2098. array('option' => array('value' => '')),
  2099. 'Some Empty',
  2100. '/option',
  2101. array('option' => array('value' => 'value')),
  2102. 'good',
  2103. '/option',
  2104. array('option' => array('value' => 'other')),
  2105. 'bad',
  2106. '/option',
  2107. '/select',
  2108. '/div'
  2109. );
  2110. $this->assertTags($result, $expected);
  2111. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2112. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2113. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2114. $expected = array(
  2115. 'div' => array('class' => 'input select'),
  2116. 'label' => array('for' => 'thing-user-id'),
  2117. 'User',
  2118. '/label',
  2119. 'select' => array('name' => 'Thing[user_id]', 'id' => 'thing-user-id'),
  2120. array('option' => array('value' => '')),
  2121. 'Some Empty',
  2122. '/option',
  2123. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2124. 'good',
  2125. '/option',
  2126. array('option' => array('value' => 'other')),
  2127. 'bad',
  2128. '/option',
  2129. '/select',
  2130. '/div'
  2131. );
  2132. $this->assertTags($result, $expected);
  2133. $this->Form->data = array();
  2134. $result = $this->Form->input('Publisher.id', array(
  2135. 'label' => 'Publisher',
  2136. 'type' => 'select',
  2137. 'multiple' => 'checkbox',
  2138. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2139. ));
  2140. $expected = array(
  2141. array('div' => array('class' => 'input select')),
  2142. array('label' => array('for' => 'publisher-id')),
  2143. 'Publisher',
  2144. '/label',
  2145. 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''),
  2146. array('div' => array('class' => 'checkbox')),
  2147. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1')),
  2148. array('label' => array('for' => 'publisher-id-value-1')),
  2149. 'Label 1',
  2150. '/label',
  2151. '/div',
  2152. array('div' => array('class' => 'checkbox')),
  2153. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2')),
  2154. array('label' => array('for' => 'publisher-id-value-2')),
  2155. 'Label 2',
  2156. '/label',
  2157. '/div',
  2158. '/div'
  2159. );
  2160. $this->assertTags($result, $expected);
  2161. }
  2162. /**
  2163. * test that input() and a non standard primary key makes a hidden input by default.
  2164. *
  2165. * @return void
  2166. */
  2167. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2168. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  2169. $this->Form->create($this->article);
  2170. $result = $this->Form->input('title');
  2171. $expected = array(
  2172. 'input' => array('type' => 'hidden', 'name' => 'title', 'id' => 'title'),
  2173. );
  2174. $this->assertTags($result, $expected);
  2175. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  2176. $this->Form->create($this->article);
  2177. $result = $this->Form->input('title');
  2178. $expected = array(
  2179. 'input' => array('type' => 'hidden', 'name' => 'title', 'id' => 'title'),
  2180. );
  2181. $this->assertTags($result, $expected);
  2182. $result = $this->Form->input('body');
  2183. $expected = array(
  2184. 'input' => array('type' => 'hidden', 'name' => 'body', 'id' => 'body'),
  2185. );
  2186. $this->assertTags($result, $expected);
  2187. }
  2188. /**
  2189. * test that overriding the magic select type widget is possible
  2190. *
  2191. * @return void
  2192. */
  2193. public function testInputOverridingMagicSelectType() {
  2194. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2195. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2196. $expected = array(
  2197. 'div' => array('class' => 'input text'),
  2198. 'label' => array('for' => 'model-user-id'), 'User', '/label',
  2199. 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'),
  2200. '/div'
  2201. );
  2202. $this->assertTags($result, $expected);
  2203. //Check that magic types still work for plural/singular vars
  2204. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2205. $result = $this->Form->input('Model.type');
  2206. $expected = array(
  2207. 'div' => array('class' => 'input select'),
  2208. 'label' => array('for' => 'model-type'), 'Type', '/label',
  2209. 'select' => array('name' => 'Model[type]', 'id' => 'model-type'),
  2210. array('option' => array('value' => 'value')), 'good', '/option',
  2211. array('option' => array('value' => 'other')), 'bad', '/option',
  2212. '/select',
  2213. '/div'
  2214. );
  2215. $this->assertTags($result, $expected);
  2216. }
  2217. /**
  2218. * Test that inferred types do not override developer input
  2219. *
  2220. * @return void
  2221. */
  2222. public function testInputMagicTypeDoesNotOverride() {
  2223. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2224. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2225. $expected = array(
  2226. 'div' => array('class' => 'input checkbox'),
  2227. array('input' => array(
  2228. 'type' => 'hidden',
  2229. 'name' => 'Model[user]',
  2230. 'value' => 0,
  2231. )),
  2232. array('input' => array(
  2233. 'name' => 'Model[user]',
  2234. 'type' => 'checkbox',
  2235. 'id' => 'model-user',
  2236. 'value' => 1
  2237. )),
  2238. 'label' => array('for' => 'model-user'), 'User', '/label',
  2239. '/div'
  2240. );
  2241. $this->assertTags($result, $expected);
  2242. }
  2243. /**
  2244. * Test that magic input() selects are created for type=number
  2245. *
  2246. * @return void
  2247. */
  2248. public function testInputMagicSelectForTypeNumber() {
  2249. TableRegistry::get('ValidateUsers', [
  2250. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2251. ]);
  2252. $entity = new Entity(['balance' => 1]);
  2253. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  2254. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2255. $result = $this->Form->input('ValidateUser.balance');
  2256. $expected = array(
  2257. 'div' => array('class' => 'input select'),
  2258. 'label' => array('for' => 'validateuser-balance'),
  2259. 'Balance',
  2260. '/label',
  2261. 'select' => array('name' => 'ValidateUser[balance]', 'id' => 'validateuser-balance'),
  2262. array('option' => array('value' => '0')),
  2263. 'nothing',
  2264. '/option',
  2265. array('option' => array('value' => '1', 'selected' => 'selected')),
  2266. 'some',
  2267. '/option',
  2268. array('option' => array('value' => '100')),
  2269. 'a lot',
  2270. '/option',
  2271. '/select',
  2272. '/div'
  2273. );
  2274. $this->assertTags($result, $expected);
  2275. }
  2276. /**
  2277. * Test that magic input() selects can easily be converted into radio types without error.
  2278. *
  2279. * @return void
  2280. */
  2281. public function testInputMagicSelectChangeToRadio() {
  2282. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2283. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2284. $this->assertRegExp('/input type="radio"/', $result);
  2285. }
  2286. /**
  2287. * testFormInputs method
  2288. *
  2289. * test correct results from form::inputs().
  2290. *
  2291. * @return void
  2292. */
  2293. public function testFormInputsLegendFieldset() {
  2294. $this->Form->create($this->article);
  2295. $result = $this->Form->inputs('The Legend');
  2296. $expected = array(
  2297. '<fieldset',
  2298. '<legend',
  2299. 'The Legend',
  2300. '/legend',
  2301. '*/fieldset',
  2302. );
  2303. $this->assertTags($result, $expected);
  2304. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => true));
  2305. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  2306. $this->assertContains('<fieldset>', $result);
  2307. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => true));
  2308. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  2309. $this->assertContains('<fieldset>', $result);
  2310. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => false));
  2311. $this->assertNotContains('<legend>', $result);
  2312. $this->assertNotContains('<fieldset>', $result);
  2313. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2314. $this->assertNotContains('<legend>', $result);
  2315. $this->assertNotContains('<fieldset>', $result);
  2316. $this->Form->create($this->article);
  2317. $this->Form->request->params['prefix'] = 'admin';
  2318. $this->Form->request->params['action'] = 'admin_edit';
  2319. $this->Form->request->params['controller'] = 'articles';
  2320. $result = $this->Form->inputs();
  2321. $expected = [
  2322. '<fieldset',
  2323. '<legend',
  2324. 'New Article',
  2325. '/legend',
  2326. '*/fieldset',
  2327. ];
  2328. $this->assertTags($result, $expected);
  2329. }
  2330. /**
  2331. * Test the inputs() method.
  2332. *
  2333. * @return void
  2334. */
  2335. public function testFormInputs() {
  2336. $this->Form->create($this->article);
  2337. $result = $this->Form->inputs(['id', 'title', 'body']);
  2338. $expected = array(
  2339. '<fieldset',
  2340. '<legend', 'New Article', '/legend',
  2341. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2342. array('div' => array('class' => 'input text required')),
  2343. '*/div',
  2344. array('div' => array('class' => 'input text')),
  2345. '*/div',
  2346. '/fieldset',
  2347. );
  2348. $this->assertTags($result, $expected);
  2349. $this->Form->create($this->article);
  2350. $result = $this->Form->inputs('Hello');
  2351. $expected = array(
  2352. 'fieldset' => array(),
  2353. 'legend' => array(),
  2354. 'Hello',
  2355. '/legend',
  2356. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2357. array('div' => array('class' => 'input select required')),
  2358. '*/div',
  2359. array('div' => array('class' => 'input text required')),
  2360. '*/div',
  2361. array('div' => array('class' => 'input text')),
  2362. '*/div',
  2363. array('div' => array('class' => 'input text')),
  2364. '*/div',
  2365. '/fieldset'
  2366. );
  2367. $this->assertTags($result, $expected);
  2368. $this->Form->create(false);
  2369. $expected = array(
  2370. 'fieldset' => array(),
  2371. array('div' => array('class' => 'input text')),
  2372. 'label' => array('for' => 'foo'),
  2373. 'Foo',
  2374. '/label',
  2375. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  2376. '*/div',
  2377. '/fieldset'
  2378. );
  2379. $result = $this->Form->inputs(
  2380. array('foo' => array('type' => 'text')),
  2381. array(),
  2382. array('legend' => false)
  2383. );
  2384. $this->assertTags($result, $expected);
  2385. }
  2386. /**
  2387. * testSelectAsCheckbox method
  2388. *
  2389. * test multi-select widget with checkbox formatting.
  2390. *
  2391. * @return void
  2392. */
  2393. public function testSelectAsCheckbox() {
  2394. $result = $this->Form->select(
  2395. 'Model.multi_field',
  2396. array('first', 'second', 'third'),
  2397. array('multiple' => 'checkbox', 'value' => array(0, 1))
  2398. );
  2399. $expected = array(
  2400. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  2401. array('div' => array('class' => 'checkbox')),
  2402. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0')),
  2403. array('label' => array('for' => 'model-multi-field-0', 'class' => 'selected')),
  2404. 'first',
  2405. '/label',
  2406. '/div',
  2407. array('div' => array('class' => 'checkbox')),
  2408. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1')),
  2409. array('label' => array('for' => 'model-multi-field-1', 'class' => 'selected')),
  2410. 'second',
  2411. '/label',
  2412. '/div',
  2413. array('div' => array('class' => 'checkbox')),
  2414. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  2415. array('label' => array('for' => 'model-multi-field-2')),
  2416. 'third',
  2417. '/label',
  2418. '/div',
  2419. );
  2420. $this->assertTags($result, $expected);
  2421. $result = $this->Form->select(
  2422. 'Model.multi_field',
  2423. array('1/2' => 'half'),
  2424. array('multiple' => 'checkbox')
  2425. );
  2426. $expected = array(
  2427. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  2428. array('div' => array('class' => 'checkbox')),
  2429. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2')),
  2430. array('label' => array('for' => 'model-multi-field-1-2')),
  2431. 'half',
  2432. '/label',
  2433. '/div',
  2434. );
  2435. $this->assertTags($result, $expected);
  2436. }
  2437. /**
  2438. * testLabel method
  2439. *
  2440. * test label generation.
  2441. *
  2442. * @return void
  2443. */
  2444. public function testLabel() {
  2445. $result = $this->Form->label('Person.name');
  2446. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  2447. $result = $this->Form->label('Person.name');
  2448. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  2449. $result = $this->Form->label('Person.first_name');
  2450. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'First Name', '/label'));
  2451. $result = $this->Form->label('Person.first_name', 'Your first name');
  2452. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'Your first name', '/label'));
  2453. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2454. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class'), 'Your first name', '/label'));
  2455. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2456. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2457. $result = $this->Form->label('Person.first_name', '');
  2458. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), '/label'));
  2459. $result = $this->Form->label('Person.2.name', '');
  2460. $this->assertTags($result, array('label' => array('for' => 'person-2-name'), '/label'));
  2461. }
  2462. /**
  2463. * testTextbox method
  2464. *
  2465. * test textbox element generation
  2466. *
  2467. * @return void
  2468. */
  2469. public function testTextbox() {
  2470. $result = $this->Form->text('Model.field');
  2471. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]')));
  2472. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2473. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]')));
  2474. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2475. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID')));
  2476. }
  2477. /**
  2478. * Test that text() hooks up with request data and error fields.
  2479. *
  2480. * @return void
  2481. */
  2482. public function testTextBoxDataAndError() {
  2483. $this->article['errors'] = [
  2484. 'Contact' => ['text' => 'wrong']
  2485. ];
  2486. $this->Form->create($this->article);
  2487. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2488. $result = $this->Form->text('Model.text');
  2489. $expected = [
  2490. 'input' => [
  2491. 'type' => 'text',
  2492. 'name' => 'Model[text]',
  2493. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  2494. ]
  2495. ];
  2496. $this->assertTags($result, $expected);
  2497. $this->Form->request->data['Contact']['text'] = 'test';
  2498. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  2499. $expected = [
  2500. 'input' => [
  2501. 'type' => 'text',
  2502. 'name' => 'Contact[text]',
  2503. 'value' => 'test',
  2504. 'id' => 'theID',
  2505. 'class' => 'form-error'
  2506. ]
  2507. ];
  2508. $this->assertTags($result, $expected);
  2509. }
  2510. /**
  2511. * testDefaultValue method
  2512. *
  2513. * Test default value setting
  2514. *
  2515. * @return void
  2516. */
  2517. public function testTextDefaultValue() {
  2518. $this->Form->request->data['Model']['field'] = 'test';
  2519. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2520. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  2521. $this->assertTags($result, $expected);
  2522. unset($this->Form->request->data['Model']['field']);
  2523. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2524. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  2525. $this->assertTags($result, $expected);
  2526. }
  2527. /**
  2528. * testError method
  2529. *
  2530. * Test field error generation
  2531. *
  2532. * @return void
  2533. */
  2534. public function testError() {
  2535. $this->article['errors'] = [
  2536. 'Article' => ['field' => 'email']
  2537. ];
  2538. $this->Form->create($this->article);
  2539. $result = $this->Form->error('Article.field');
  2540. $expected = [
  2541. ['div' => ['class' => 'error-message']],
  2542. 'email',
  2543. '/div',
  2544. ];
  2545. $this->assertTags($result, $expected);
  2546. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  2547. $expected = [
  2548. ['div' => ['class' => 'error-message']],
  2549. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  2550. '/div',
  2551. ];
  2552. $this->assertTags($result, $expected);
  2553. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  2554. $expected = [
  2555. ['div' => ['class' => 'error-message']],
  2556. '<strong', 'Badness!', '/strong',
  2557. '/div',
  2558. ];
  2559. $this->assertTags($result, $expected);
  2560. }
  2561. /**
  2562. * Test error with nested lists.
  2563. *
  2564. * @return void
  2565. */
  2566. public function testErrorMessages() {
  2567. $this->article['errors'] = [
  2568. 'Article' => ['field' => 'email']
  2569. ];
  2570. $this->Form->create($this->article);
  2571. $result = $this->Form->error('Article.field', array(
  2572. 'email' => 'No good!'
  2573. ));
  2574. $expected = array(
  2575. 'div' => array('class' => 'error-message'),
  2576. 'No good!',
  2577. '/div'
  2578. );
  2579. $this->assertTags($result, $expected);
  2580. }
  2581. /**
  2582. * test error() with multiple messages.
  2583. *
  2584. * @return void
  2585. */
  2586. public function testErrorMultipleMessages() {
  2587. $this->article['errors'] = [
  2588. 'field' => ['notEmpty', 'email', 'Something else']
  2589. ];
  2590. $this->Form->create($this->article);
  2591. $result = $this->Form->error('field', array(
  2592. 'notEmpty' => 'Cannot be empty',
  2593. 'email' => 'No good!'
  2594. ));
  2595. $expected = array(
  2596. 'div' => array('class' => 'error-message'),
  2597. 'ul' => array(),
  2598. '<li', 'Cannot be empty', '/li',
  2599. '<li', 'No good!', '/li',
  2600. '<li', 'Something else', '/li',
  2601. '/ul',
  2602. '/div'
  2603. );
  2604. $this->assertTags($result, $expected);
  2605. }
  2606. /**
  2607. * testPassword method
  2608. *
  2609. * Test password element generation
  2610. *
  2611. * @return void
  2612. */
  2613. public function testPassword() {
  2614. $this->article['errors'] = [
  2615. 'Contact' => [
  2616. 'passwd' => 1
  2617. ]
  2618. ];
  2619. $this->Form->create($this->article);
  2620. $result = $this->Form->password('Contact.field');
  2621. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]')));
  2622. $this->Form->request->data['Contact']['passwd'] = 'test';
  2623. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2624. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2625. }
  2626. /**
  2627. * testRadio method
  2628. *
  2629. * Test radio element set generation
  2630. *
  2631. * @return void
  2632. */
  2633. public function testRadio() {
  2634. $result = $this->Form->radio('Model.field', array('option A'));
  2635. $expected = array(
  2636. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  2637. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  2638. 'label' => array('for' => 'model-field-0'),
  2639. 'option A',
  2640. '/label'
  2641. );
  2642. $this->assertTags($result, $expected);
  2643. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2644. $expected = array(
  2645. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  2646. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  2647. array('label' => array('for' => 'model-field-0')),
  2648. 'option A',
  2649. '/label',
  2650. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1')),
  2651. array('label' => array('for' => 'model-field-1')),
  2652. 'option B',
  2653. '/label',
  2654. );
  2655. $this->assertTags($result, $expected);
  2656. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  2657. $expected = array(
  2658. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''),
  2659. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male')),
  2660. array('label' => array('for' => 'employee-gender-male')),
  2661. 'Male',
  2662. '/label',
  2663. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female')),
  2664. array('label' => array('for' => 'employee-gender-female')),
  2665. 'Female',
  2666. '/label',
  2667. );
  2668. $this->assertTags($result, $expected);
  2669. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  2670. $expected = array(
  2671. array('input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '')),
  2672. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0')),
  2673. array('label' => array('for' => 'model-custom-0')),
  2674. 'option A',
  2675. '/label',
  2676. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1')),
  2677. array('label' => array('for' => 'model-custom-1')),
  2678. 'option B',
  2679. '/label',
  2680. );
  2681. $this->assertTags($result, $expected);
  2682. }
  2683. /**
  2684. * test generating radio input inside label ala twitter bootstrap
  2685. *
  2686. * @return void
  2687. */
  2688. public function testRadioInputInsideLabel() {
  2689. $this->Form->templates([
  2690. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  2691. 'radioContainer' => '{{label}}'
  2692. ]);
  2693. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  2694. $expected = [
  2695. ['input' => [
  2696. 'type' => 'hidden',
  2697. 'name' => 'Model[field]',
  2698. 'value' => ''
  2699. ]],
  2700. ['label' => ['for' => 'model-field-0']],
  2701. ['input' => [
  2702. 'type' => 'radio',
  2703. 'name' => 'Model[field]',
  2704. 'value' => '0',
  2705. 'id' => 'model-field-0'
  2706. ]],
  2707. 'option A',
  2708. '/label',
  2709. ['label' => ['for' => 'model-field-1']],
  2710. ['input' => [
  2711. 'type' => 'radio',
  2712. 'name' => 'Model[field]',
  2713. 'value' => '1',
  2714. 'id' => 'model-field-1'
  2715. ]],
  2716. 'option B',
  2717. '/label',
  2718. ];
  2719. $this->assertTags($result, $expected);
  2720. }
  2721. /**
  2722. * test disabling the hidden input for radio buttons
  2723. *
  2724. * @return void
  2725. */
  2726. public function testRadioHiddenInputDisabling() {
  2727. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  2728. $expected = array(
  2729. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'),
  2730. 'label' => array('for' => 'model-1-field-0'),
  2731. 'option A',
  2732. '/label'
  2733. );
  2734. $this->assertTags($result, $expected);
  2735. }
  2736. /**
  2737. * testSelect method
  2738. *
  2739. * Test select element generation.
  2740. *
  2741. * @return void
  2742. */
  2743. public function testSelect() {
  2744. $result = $this->Form->select('Model.field', array());
  2745. $expected = array(
  2746. 'select' => array('name' => 'Model[field]'),
  2747. '/select'
  2748. );
  2749. $this->assertTags($result, $expected);
  2750. $this->Form->request->data = array('Model' => array('field' => 'value'));
  2751. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  2752. $expected = array(
  2753. 'select' => array('name' => 'Model[field]'),
  2754. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2755. 'good',
  2756. '/option',
  2757. array('option' => array('value' => 'other')),
  2758. 'bad',
  2759. '/option',
  2760. '/select'
  2761. );
  2762. $this->assertTags($result, $expected);
  2763. $this->Form->request->data = array();
  2764. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  2765. $expected = array(
  2766. 'select' => array('name' => 'Model[field]'),
  2767. array('option' => array('value' => 'value')),
  2768. 'good',
  2769. '/option',
  2770. array('option' => array('value' => 'other')),
  2771. 'bad',
  2772. '/option',
  2773. '/select'
  2774. );
  2775. $this->assertTags($result, $expected);
  2776. $result = $this->Form->select(
  2777. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  2778. array('empty' => false)
  2779. );
  2780. $expected = array(
  2781. 'select' => array('name' => 'Model[field]'),
  2782. array('option' => array('value' => 'first')),
  2783. 'first &quot;html&quot; &lt;chars&gt;',
  2784. '/option',
  2785. array('option' => array('value' => 'second')),
  2786. 'value',
  2787. '/option',
  2788. '/select'
  2789. );
  2790. $this->assertTags($result, $expected);
  2791. $result = $this->Form->select(
  2792. 'Model.field',
  2793. array('first' => 'first "html" <chars>', 'second' => 'value'),
  2794. array('escape' => false, 'empty' => false)
  2795. );
  2796. $expected = array(
  2797. 'select' => array('name' => 'Model[field]'),
  2798. array('option' => array('value' => 'first')),
  2799. 'first "html" <chars>',
  2800. '/option',
  2801. array('option' => array('value' => 'second')),
  2802. 'value',
  2803. '/option',
  2804. '/select'
  2805. );
  2806. $this->assertTags($result, $expected);
  2807. $options = array(
  2808. array('value' => 'first', 'text' => 'First'),
  2809. array('value' => 'first', 'text' => 'Another First'),
  2810. );
  2811. $result = $this->Form->select(
  2812. 'Model.field',
  2813. $options,
  2814. array('escape' => false, 'empty' => false)
  2815. );
  2816. $expected = array(
  2817. 'select' => array('name' => 'Model[field]'),
  2818. array('option' => array('value' => 'first')),
  2819. 'First',
  2820. '/option',
  2821. array('option' => array('value' => 'first')),
  2822. 'Another First',
  2823. '/option',
  2824. '/select'
  2825. );
  2826. $this->assertTags($result, $expected);
  2827. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  2828. $result = $this->Form->select(
  2829. 'Model.contact_id',
  2830. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  2831. array('escape' => false, 'empty' => 'pick something')
  2832. );
  2833. $expected = array(
  2834. 'select' => array('name' => 'Model[contact_id]'),
  2835. array('option' => array('value' => '')), 'pick something', '/option',
  2836. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  2837. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  2838. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  2839. '/select'
  2840. );
  2841. $this->assertTags($result, $expected);
  2842. $this->Form->request->data['Model']['field'] = 0;
  2843. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  2844. $expected = array(
  2845. 'select' => array('name' => 'Model[field]'),
  2846. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  2847. array('option' => array('value' => '1')), 'Yes', '/option',
  2848. '/select'
  2849. );
  2850. $this->assertTags($result, $expected);
  2851. }
  2852. /**
  2853. * test select() with required and disabled attributes.
  2854. *
  2855. * @return void
  2856. */
  2857. public function testSelectRequired() {
  2858. $this->article['required'] = [
  2859. 'user_id' => true
  2860. ];
  2861. $this->Form->create($this->article);
  2862. $result = $this->Form->select('user_id', array('option A'));
  2863. $expected = array(
  2864. 'select' => array(
  2865. 'name' => 'user_id',
  2866. 'required' => 'required'
  2867. ),
  2868. array('option' => array('value' => '0')), 'option A', '/option',
  2869. '/select'
  2870. );
  2871. $this->assertTags($result, $expected);
  2872. $result = $this->Form->select('user_id', array('option A'), array('disabled' => true));
  2873. $expected = array(
  2874. 'select' => array(
  2875. 'name' => 'user_id',
  2876. 'disabled' => 'disabled'
  2877. ),
  2878. array('option' => array('value' => '0')), 'option A', '/option',
  2879. '/select'
  2880. );
  2881. $this->assertTags($result, $expected);
  2882. }
  2883. /**
  2884. * testNestedSelect method
  2885. *
  2886. * test select element generation with optgroups
  2887. *
  2888. * @return void
  2889. */
  2890. public function testNestedSelect() {
  2891. $result = $this->Form->select(
  2892. 'Model.field',
  2893. array(1 => 'One', 2 => 'Two', 'Three' => array(
  2894. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  2895. )), array('empty' => false)
  2896. );
  2897. $expected = array(
  2898. 'select' => array('name' => 'Model[field]'),
  2899. array('option' => array('value' => 1)),
  2900. 'One',
  2901. '/option',
  2902. array('option' => array('value' => 2)),
  2903. 'Two',
  2904. '/option',
  2905. array('optgroup' => array('label' => 'Three')),
  2906. array('option' => array('value' => 3)),
  2907. 'Three',
  2908. '/option',
  2909. array('option' => array('value' => 4)),
  2910. 'Four',
  2911. '/option',
  2912. array('option' => array('value' => 5)),
  2913. 'Five',
  2914. '/option',
  2915. '/optgroup',
  2916. '/select'
  2917. );
  2918. $this->assertTags($result, $expected);
  2919. }
  2920. /**
  2921. * testSelectMultiple method
  2922. *
  2923. * test generation of multiple select elements
  2924. *
  2925. * @return void
  2926. */
  2927. public function testSelectMultiple() {
  2928. $options = array('first', 'second', 'third');
  2929. $result = $this->Form->select(
  2930. 'Model.multi_field',
  2931. $options,
  2932. array('multiple' => true)
  2933. );
  2934. $expected = array(
  2935. 'input' => array(
  2936. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2937. ),
  2938. 'select' => array(
  2939. 'name' => 'Model[multi_field][]',
  2940. 'multiple' => 'multiple'
  2941. ),
  2942. array('option' => array('value' => '0')),
  2943. 'first',
  2944. '/option',
  2945. array('option' => array('value' => '1')),
  2946. 'second',
  2947. '/option',
  2948. array('option' => array('value' => '2')),
  2949. 'third',
  2950. '/option',
  2951. '/select'
  2952. );
  2953. $this->assertTags($result, $expected);
  2954. $result = $this->Form->select(
  2955. 'Model.multi_field',
  2956. $options,
  2957. array('multiple' => 'multiple')
  2958. );
  2959. $this->assertTags($result, $expected);
  2960. }
  2961. /**
  2962. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  2963. *
  2964. * @return void
  2965. */
  2966. public function testCheckboxZeroValue() {
  2967. $result = $this->Form->input('User.get_spam', array(
  2968. 'type' => 'checkbox',
  2969. 'value' => '0',
  2970. 'hiddenField' => '1',
  2971. ));
  2972. $expected = array(
  2973. 'div' => array('class' => 'input checkbox'),
  2974. array('input' => array(
  2975. 'type' => 'hidden', 'name' => 'User[get_spam]',
  2976. 'value' => '1'
  2977. )),
  2978. array('input' => array(
  2979. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  2980. 'value' => '0', 'id' => 'user-get-spam'
  2981. )),
  2982. 'label' => array('for' => 'user-get-spam'),
  2983. 'Get Spam',
  2984. '/label',
  2985. '/div'
  2986. );
  2987. $this->assertTags($result, $expected);
  2988. }
  2989. /**
  2990. * test generation of habtm select boxes.
  2991. *
  2992. * @return void
  2993. */
  2994. public function testHabtmSelectBox() {
  2995. $this->loadFixtures('Article');
  2996. $options = array(
  2997. 1 => 'blue',
  2998. 2 => 'red',
  2999. 3 => 'green'
  3000. );
  3001. $tags = [
  3002. new Entity(['id' => 1, 'name' => 'blue']),
  3003. new Entity(['id' => 3, 'name' => 'green'])
  3004. ];
  3005. $article = new Article(['tags' => $tags]);
  3006. $this->Form->create($article);
  3007. $result = $this->Form->input('tags._ids', ['options' => $options]);
  3008. $expected = array(
  3009. 'div' => array('class' => 'input select'),
  3010. 'label' => array('for' => 'tags-ids'),
  3011. 'Tags',
  3012. '/label',
  3013. 'input' => array('type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''),
  3014. 'select' => array(
  3015. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  3016. 'multiple' => 'multiple'
  3017. ),
  3018. array('option' => array('value' => '1', 'selected' => 'selected')),
  3019. 'blue',
  3020. '/option',
  3021. array('option' => array('value' => '2')),
  3022. 'red',
  3023. '/option',
  3024. array('option' => array('value' => '3', 'selected' => 'selected')),
  3025. 'green',
  3026. '/option',
  3027. '/select',
  3028. '/div'
  3029. );
  3030. $this->assertTags($result, $expected);
  3031. // make sure only 50 is selected, and not 50f5c0cf
  3032. $options = array(
  3033. '1' => 'blue',
  3034. '50f5c0cf' => 'red',
  3035. '50' => 'green'
  3036. );
  3037. $tags = [
  3038. new Entity(['id' => 1, 'name' => 'blue']),
  3039. new Entity(['id' => 50, 'name' => 'green'])
  3040. ];
  3041. $article = new Article(['tags' => $tags]);
  3042. $this->Form->create($article);
  3043. $result = $this->Form->input('tags._ids', ['options' => $options]);
  3044. $expected = array(
  3045. 'div' => array('class' => 'input select'),
  3046. 'label' => array('for' => 'tags-ids'),
  3047. 'Tags',
  3048. '/label',
  3049. 'input' => array('type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''),
  3050. 'select' => array(
  3051. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  3052. 'multiple' => 'multiple'
  3053. ),
  3054. array('option' => array('value' => '1', 'selected' => 'selected')),
  3055. 'blue',
  3056. '/option',
  3057. array('option' => array('value' => '50f5c0cf')),
  3058. 'red',
  3059. '/option',
  3060. array('option' => array('value' => '50', 'selected' => 'selected')),
  3061. 'green',
  3062. '/option',
  3063. '/select',
  3064. '/div'
  3065. );
  3066. $this->assertTags($result, $expected);
  3067. }
  3068. /**
  3069. * test generation of multi select elements in checkbox format
  3070. *
  3071. * @return void
  3072. */
  3073. public function testSelectMultipleCheckboxes() {
  3074. $result = $this->Form->select(
  3075. 'Model.multi_field',
  3076. array('first', 'second', 'third'),
  3077. array('multiple' => 'checkbox')
  3078. );
  3079. $expected = array(
  3080. 'input' => array(
  3081. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3082. ),
  3083. array('div' => array('class' => 'checkbox')),
  3084. array('input' => array(
  3085. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3086. 'value' => '0', 'id' => 'model-multi-field-0'
  3087. )),
  3088. array('label' => array('for' => 'model-multi-field-0')),
  3089. 'first',
  3090. '/label',
  3091. '/div',
  3092. array('div' => array('class' => 'checkbox')),
  3093. array('input' => array(
  3094. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3095. 'value' => '1', 'id' => 'model-multi-field-1'
  3096. )),
  3097. array('label' => array('for' => 'model-multi-field-1')),
  3098. 'second',
  3099. '/label',
  3100. '/div',
  3101. array('div' => array('class' => 'checkbox')),
  3102. array('input' => array(
  3103. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3104. 'value' => '2', 'id' => 'model-multi-field-2'
  3105. )),
  3106. array('label' => array('for' => 'model-multi-field-2')),
  3107. 'third',
  3108. '/label',
  3109. '/div'
  3110. );
  3111. $this->assertTags($result, $expected);
  3112. $result = $this->Form->select(
  3113. 'Model.multi_field',
  3114. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  3115. array('multiple' => 'checkbox')
  3116. );
  3117. $expected = array(
  3118. 'input' => array(
  3119. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3120. ),
  3121. array('div' => array('class' => 'checkbox')),
  3122. array('input' => array(
  3123. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3124. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  3125. )),
  3126. array('label' => array('for' => 'model-multi-field-a+')),
  3127. 'first',
  3128. '/label',
  3129. '/div',
  3130. array('div' => array('class' => 'checkbox')),
  3131. array('input' => array(
  3132. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3133. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  3134. )),
  3135. array('label' => array('for' => 'model-multi-field-a++')),
  3136. 'second',
  3137. '/label',
  3138. '/div',
  3139. array('div' => array('class' => 'checkbox')),
  3140. array('input' => array(
  3141. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3142. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  3143. )),
  3144. array('label' => array('for' => 'model-multi-field-a+++')),
  3145. 'third',
  3146. '/label',
  3147. '/div'
  3148. );
  3149. $this->assertTags($result, $expected);
  3150. $result = $this->Form->select(
  3151. 'Model.multi_field',
  3152. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  3153. array('multiple' => 'checkbox')
  3154. );
  3155. $expected = array(
  3156. 'input' => array(
  3157. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3158. ),
  3159. array('div' => array('class' => 'checkbox')),
  3160. array('input' => array(
  3161. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3162. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  3163. )),
  3164. array('label' => array('for' => 'model-multi-field-a-b')),
  3165. 'first',
  3166. '/label',
  3167. '/div',
  3168. array('div' => array('class' => 'checkbox')),
  3169. array('input' => array(
  3170. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3171. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  3172. )),
  3173. array('label' => array('for' => 'model-multi-field-a-b1')),
  3174. 'second',
  3175. '/label',
  3176. '/div',
  3177. array('div' => array('class' => 'checkbox')),
  3178. array('input' => array(
  3179. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3180. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  3181. )),
  3182. array('label' => array('for' => 'model-multi-field-a-b2')),
  3183. 'third',
  3184. '/label',
  3185. '/div'
  3186. );
  3187. $this->assertTags($result, $expected);
  3188. }
  3189. /**
  3190. * Ensure that multiCheckbox reads from the request data.
  3191. *
  3192. * @return void
  3193. */
  3194. public function testSelectMultipleCheckboxRequestData() {
  3195. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3196. $result = $this->Form->select(
  3197. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3198. );
  3199. $expected = array(
  3200. 'input' => array(
  3201. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  3202. ),
  3203. array('div' => array('class' => 'checkbox')),
  3204. array('input' => array(
  3205. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3206. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  3207. )),
  3208. array('label' => array('for' => 'model-tags-1', 'class' => 'selected')),
  3209. 'first',
  3210. '/label',
  3211. '/div',
  3212. array('div' => array('class' => 'checkbox')),
  3213. array('input' => array(
  3214. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3215. 'value' => 'Array', 'id' => 'model-tags-array'
  3216. )),
  3217. array('label' => array('for' => 'model-tags-array')),
  3218. 'Array',
  3219. '/label',
  3220. '/div'
  3221. );
  3222. $this->assertTags($result, $expected);
  3223. }
  3224. /**
  3225. * Checks the security hash array generated for multiple-input checkbox elements
  3226. *
  3227. * @return void
  3228. */
  3229. public function testSelectMultipleCheckboxSecurity() {
  3230. $this->Form->request->params['_Token'] = 'testKey';
  3231. $this->assertEquals(array(), $this->Form->fields);
  3232. $result = $this->Form->select(
  3233. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3234. array('multiple' => 'checkbox')
  3235. );
  3236. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3237. $result = $this->Form->secure($this->Form->fields);
  3238. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3239. $this->assertRegExp('/"' . $key . '"/', $result);
  3240. }
  3241. /**
  3242. * Multiple select elements should always be secured as they always participate
  3243. * in the POST data.
  3244. *
  3245. * @return void
  3246. */
  3247. public function testSelectMultipleSecureWithNoOptions() {
  3248. $this->assertEquals(array(), $this->Form->fields);
  3249. $this->Form->select(
  3250. 'Model.select',
  3251. array(),
  3252. array('multiple' => true)
  3253. );
  3254. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3255. }
  3256. /**
  3257. * When a select box has no options it should not be added to the fields list
  3258. * as it always fail post validation.
  3259. *
  3260. * @return void
  3261. */
  3262. public function testSelectNoSecureWithNoOptions() {
  3263. $this->Form->request->params['_Token'] = 'testkey';
  3264. $this->assertEquals([], $this->Form->fields);
  3265. $this->Form->select(
  3266. 'Model.select',
  3267. []
  3268. );
  3269. $this->assertEquals([], $this->Form->fields);
  3270. $this->Form->select(
  3271. 'Model.user_id',
  3272. [],
  3273. ['empty' => true]
  3274. );
  3275. $this->assertEquals(array('Model.user_id'), $this->Form->fields);
  3276. }
  3277. /**
  3278. * testInputMultipleCheckboxes method
  3279. *
  3280. * test input() resulting in multi select elements being generated.
  3281. *
  3282. * @return void
  3283. */
  3284. public function testInputMultipleCheckboxes() {
  3285. $result = $this->Form->input('Model.multi_field', array(
  3286. 'options' => array('first', 'second', 'third'),
  3287. 'multiple' => 'checkbox'
  3288. ));
  3289. $expected = array(
  3290. array('div' => array('class' => 'input select')),
  3291. array('label' => array('for' => 'model-multi-field')),
  3292. 'Multi Field',
  3293. '/label',
  3294. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3295. array('div' => array('class' => 'checkbox')),
  3296. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0')),
  3297. array('label' => array('for' => 'model-multi-field-0')),
  3298. 'first',
  3299. '/label',
  3300. '/div',
  3301. array('div' => array('class' => 'checkbox')),
  3302. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1')),
  3303. array('label' => array('for' => 'model-multi-field-1')),
  3304. 'second',
  3305. '/label',
  3306. '/div',
  3307. array('div' => array('class' => 'checkbox')),
  3308. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  3309. array('label' => array('for' => 'model-multi-field-2')),
  3310. 'third',
  3311. '/label',
  3312. '/div',
  3313. '/div'
  3314. );
  3315. $this->assertTags($result, $expected);
  3316. $result = $this->Form->input('Model.multi_field', array(
  3317. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3318. 'multiple' => 'checkbox'
  3319. ));
  3320. $expected = array(
  3321. array('div' => array('class' => 'input select')),
  3322. array('label' => array('for' => 'model-multi-field')),
  3323. 'Multi Field',
  3324. '/label',
  3325. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3326. array('div' => array('class' => 'checkbox')),
  3327. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a')),
  3328. array('label' => array('for' => 'model-multi-field-a')),
  3329. 'first',
  3330. '/label',
  3331. '/div',
  3332. array('div' => array('class' => 'checkbox')),
  3333. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b')),
  3334. array('label' => array('for' => 'model-multi-field-b')),
  3335. 'second',
  3336. '/label',
  3337. '/div',
  3338. array('div' => array('class' => 'checkbox')),
  3339. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c')),
  3340. array('label' => array('for' => 'model-multi-field-c')),
  3341. 'third',
  3342. '/label',
  3343. '/div',
  3344. '/div'
  3345. );
  3346. $this->assertTags($result, $expected);
  3347. }
  3348. /**
  3349. * testSelectHiddenFieldOmission method
  3350. *
  3351. * test that select() with 'hiddenField' => false omits the hidden field
  3352. *
  3353. * @return void
  3354. */
  3355. public function testSelectHiddenFieldOmission() {
  3356. $result = $this->Form->select('Model.multi_field',
  3357. array('first', 'second'),
  3358. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  3359. );
  3360. $this->assertNotContains('type="hidden"', $result);
  3361. }
  3362. /**
  3363. * test that select() with multiple = checkbox works with overriding name attribute.
  3364. *
  3365. * @return void
  3366. */
  3367. public function testSelectCheckboxMultipleOverrideName() {
  3368. $result = $this->Form->select('category', ['1', '2'], [
  3369. 'multiple' => 'checkbox',
  3370. 'name' => 'fish',
  3371. ]);
  3372. $expected = array(
  3373. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => ''),
  3374. array('div' => array('class' => 'checkbox')),
  3375. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0')),
  3376. array('label' => array('for' => 'fish-0')), '1', '/label',
  3377. '/div',
  3378. array('div' => array('class' => 'checkbox')),
  3379. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1')),
  3380. array('label' => array('for' => 'fish-1')), '2', '/label',
  3381. '/div'
  3382. );
  3383. $this->assertTags($result, $expected);
  3384. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  3385. 'name' => 'fish',
  3386. ]);
  3387. $this->assertTags($result, $expected);
  3388. }
  3389. /**
  3390. * testCheckbox method
  3391. *
  3392. * Test generation of checkboxes
  3393. *
  3394. * @return void
  3395. */
  3396. public function testCheckbox() {
  3397. $result = $this->Form->checkbox('Model.field');
  3398. $expected = array(
  3399. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  3400. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'))
  3401. );
  3402. $this->assertTags($result, $expected);
  3403. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  3404. $expected = array(
  3405. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  3406. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => 'myvalue', 'id' => 'theID'))
  3407. );
  3408. $this->assertTags($result, $expected);
  3409. }
  3410. /**
  3411. * testCheckboxDefaultValue method
  3412. *
  3413. * Test default value setting on checkbox() method
  3414. *
  3415. * @return void
  3416. */
  3417. public function testCheckboxDefaultValue() {
  3418. $this->Form->request->data['Model']['field'] = false;
  3419. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3420. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  3421. unset($this->Form->request->data['Model']['field']);
  3422. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3423. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  3424. $this->Form->request->data['Model']['field'] = true;
  3425. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3426. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  3427. unset($this->Form->request->data['Model']['field']);
  3428. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3429. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  3430. }
  3431. /**
  3432. * Test checkbox being checked or having errors.
  3433. *
  3434. * @return void
  3435. */
  3436. public function testCheckboxCheckedAndError() {
  3437. $this->article['errors'] = [
  3438. 'published' => true
  3439. ];
  3440. $this->Form->request->data['published'] = 'myvalue';
  3441. $this->Form->create($this->article);
  3442. $result = $this->Form->checkbox('published', array('id' => 'theID', 'value' => 'myvalue'));
  3443. $expected = array(
  3444. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  3445. array('input' => array(
  3446. 'type' => 'checkbox',
  3447. 'name' => 'published',
  3448. 'value' => 'myvalue',
  3449. 'id' => 'theID',
  3450. 'checked' => 'checked',
  3451. 'class' => 'form-error'
  3452. ))
  3453. );
  3454. $this->assertTags($result, $expected);
  3455. $this->Form->request->data['published'] = '';
  3456. $result = $this->Form->checkbox('published');
  3457. $expected = array(
  3458. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  3459. array('input' => array('type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error'))
  3460. );
  3461. $this->assertTags($result, $expected);
  3462. }
  3463. /**
  3464. * test checkbox() with a custom name attribute
  3465. *
  3466. * @return void
  3467. */
  3468. public function testCheckboxCustomNameAttribute() {
  3469. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  3470. $expected = array(
  3471. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0'),
  3472. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1'))
  3473. );
  3474. $this->assertTags($result, $expected);
  3475. }
  3476. /**
  3477. * Test that the hidden input for checkboxes can be omitted or set to a
  3478. * specific value.
  3479. *
  3480. * @return void
  3481. */
  3482. public function testCheckboxHiddenField() {
  3483. $result = $this->Form->checkbox('UserForm.something', array(
  3484. 'hiddenField' => false
  3485. ));
  3486. $expected = array(
  3487. 'input' => array(
  3488. 'type' => 'checkbox',
  3489. 'name' => 'UserForm[something]',
  3490. 'value' => '1'
  3491. ),
  3492. );
  3493. $this->assertTags($result, $expected);
  3494. $result = $this->Form->checkbox('UserForm.something', array(
  3495. 'value' => 'Y',
  3496. 'hiddenField' => 'N',
  3497. ));
  3498. $expected = array(
  3499. array('input' => array(
  3500. 'type' => 'hidden', 'name' => 'UserForm[something]',
  3501. 'value' => 'N'
  3502. )),
  3503. array('input' => array(
  3504. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  3505. 'value' => 'Y'
  3506. )),
  3507. );
  3508. $this->assertTags($result, $expected);
  3509. }
  3510. /**
  3511. * testDateTime method
  3512. *
  3513. * Test generation of date/time select elements
  3514. *
  3515. * @return void
  3516. */
  3517. public function testDateTime() {
  3518. extract($this->dateRegex);
  3519. $result = $this->Form->dateTime('Contact.date', array('empty' => false));
  3520. $now = strtotime('now');
  3521. $expected = array(
  3522. array('select' => array('name' => 'Contact[date][month]')),
  3523. $monthsRegex,
  3524. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  3525. date('F', $now),
  3526. '/option',
  3527. '*/select',
  3528. array('select' => array('name' => 'Contact[date][day]')),
  3529. $daysRegex,
  3530. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  3531. date('j', $now),
  3532. '/option',
  3533. '*/select',
  3534. array('select' => array('name' => 'Contact[date][year]')),
  3535. $yearsRegex,
  3536. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  3537. date('Y', $now),
  3538. '/option',
  3539. '*/select',
  3540. array('select' => array('name' => 'Contact[date][hour]')),
  3541. $hoursRegex,
  3542. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  3543. date('g', $now),
  3544. '/option',
  3545. '*/select',
  3546. array('select' => array('name' => 'Contact[date][minute]')),
  3547. $minutesRegex,
  3548. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  3549. date('i', $now),
  3550. '/option',
  3551. '*/select',
  3552. array('select' => array('name' => 'Contact[date][meridian]')),
  3553. $meridianRegex,
  3554. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  3555. date('a', $now),
  3556. '/option',
  3557. '*/select'
  3558. );
  3559. $this->assertTags($result, $expected);
  3560. }
  3561. /**
  3562. * Test empty defaulting to true for datetime.
  3563. *
  3564. * @return void
  3565. */
  3566. public function testDatetimeEmpty() {
  3567. extract($this->dateRegex);
  3568. $now = strtotime('now');
  3569. $result = $this->Form->dateTime('Contact.date', array(
  3570. 'empty' => true,
  3571. ));
  3572. $expected = array(
  3573. array('select' => array('name' => 'Contact[date][month]')),
  3574. $monthsRegex,
  3575. array('option' => array('value' => '')),
  3576. '/option',
  3577. '*/select',
  3578. array('select' => array('name' => 'Contact[date][day]')),
  3579. $daysRegex,
  3580. array('option' => array('value' => '')),
  3581. '/option',
  3582. '*/select',
  3583. array('select' => array('name' => 'Contact[date][year]')),
  3584. $yearsRegex,
  3585. array('option' => array('value' => '')),
  3586. '/option',
  3587. '*/select',
  3588. array('select' => array('name' => 'Contact[date][hour]')),
  3589. $hoursRegex,
  3590. array('option' => array('value' => '')),
  3591. '/option',
  3592. '*/select',
  3593. array('select' => array('name' => 'Contact[date][minute]')),
  3594. $minutesRegex,
  3595. array('option' => array('value' => '')),
  3596. '/option',
  3597. '*/select',
  3598. array('select' => array('name' => 'Contact[date][meridian]')),
  3599. $meridianRegex,
  3600. array('option' => array('value' => '')),
  3601. '/option',
  3602. '*/select'
  3603. );
  3604. $this->assertTags($result, $expected);
  3605. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  3606. }
  3607. /**
  3608. * Test datetime with interval option.
  3609. *
  3610. * @return void
  3611. */
  3612. public function testDatetimeMinuteInterval() {
  3613. extract($this->dateRegex);
  3614. $now = strtotime('now');
  3615. $result = $this->Form->dateTime('Contact.date', array(
  3616. 'interval' => 5,
  3617. 'value' => ''
  3618. ));
  3619. $expected = array(
  3620. array('select' => array('name' => 'Contact[date][month]')),
  3621. $monthsRegex,
  3622. array('option' => array('selected' => 'selected', 'value' => '')),
  3623. '/option',
  3624. '*/select',
  3625. array('select' => array('name' => 'Contact[date][day]')),
  3626. $daysRegex,
  3627. array('option' => array('selected' => 'selected', 'value' => '')),
  3628. '/option',
  3629. '*/select',
  3630. array('select' => array('name' => 'Contact[date][year]')),
  3631. $yearsRegex,
  3632. array('option' => array('selected' => 'selected', 'value' => '')),
  3633. '/option',
  3634. '*/select',
  3635. array('select' => array('name' => 'Contact[date][hour]')),
  3636. $hoursRegex,
  3637. array('option' => array('selected' => 'selected', 'value' => '')),
  3638. '/option',
  3639. '*/select',
  3640. array('select' => array('name' => 'Contact[date][minute]')),
  3641. $minutesRegex,
  3642. array('option' => array('selected' => 'selected', 'value' => '')),
  3643. '/option',
  3644. array('option' => array('value' => '00')),
  3645. '00',
  3646. '/option',
  3647. array('option' => array('value' => '05')),
  3648. '05',
  3649. '/option',
  3650. array('option' => array('value' => '10')),
  3651. '10',
  3652. '/option',
  3653. '*/select',
  3654. );
  3655. $this->assertTags($result, $expected);
  3656. }
  3657. /**
  3658. * Test dateTime with rounding
  3659. *
  3660. * @return void
  3661. */
  3662. public function testDateTimeRounding() {
  3663. $this->Form->request->data['Contact'] = array(
  3664. 'date' => array(
  3665. 'day' => '13',
  3666. 'month' => '12',
  3667. 'year' => '2010',
  3668. 'hour' => '04',
  3669. 'minute' => '19',
  3670. 'meridian' => 'AM'
  3671. )
  3672. );
  3673. $result = $this->Form->dateTime('Contact.date', array('interval' => 15));
  3674. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  3675. $result = $this->Form->dateTime('Contact.date', array('interval' => 15, 'round' => 'up'));
  3676. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  3677. $result = $this->Form->dateTime('Contact.date', array('interval' => 5, 'round' => 'down'));
  3678. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  3679. }
  3680. /**
  3681. * test that datetime() and default values work.
  3682. *
  3683. * @return void
  3684. */
  3685. public function testDatetimeWithDefault() {
  3686. $result = $this->Form->dateTime('Contact.updated', array('value' => '2009-06-01 11:15:30'));
  3687. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  3688. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  3689. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  3690. $result = $this->Form->dateTime('Contact.updated', array(
  3691. 'default' => '2009-06-01 11:15:30'
  3692. ));
  3693. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  3694. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  3695. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  3696. }
  3697. /**
  3698. * testDateTime all zeros
  3699. *
  3700. * @return void
  3701. */
  3702. public function testDateTimeAllZeros() {
  3703. $result = $this->Form->dateTime('Contact.date', array(
  3704. 'timeFormat' => false,
  3705. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  3706. 'value' => '0000-00-00'
  3707. ));
  3708. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  3709. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  3710. }
  3711. /**
  3712. * testDateTimeEmptyAsArray
  3713. *
  3714. * @return void
  3715. */
  3716. public function testDateTimeEmptyAsArray() {
  3717. $result = $this->Form->dateTime('Contact.date', array(
  3718. 'empty' => array(
  3719. 'day' => 'DAY',
  3720. 'month' => 'MONTH',
  3721. 'year' => 'YEAR',
  3722. 'hour' => 'HOUR',
  3723. 'minute' => 'MINUTE',
  3724. 'meridian' => false
  3725. )
  3726. ));
  3727. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  3728. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  3729. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  3730. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  3731. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  3732. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  3733. $result = $this->Form->dateTime('Contact.date', array(
  3734. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  3735. ));
  3736. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  3737. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  3738. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  3739. }
  3740. /**
  3741. * testFormDateTimeMulti method
  3742. *
  3743. * test multiple datetime element generation
  3744. *
  3745. * @return void
  3746. */
  3747. public function testFormDateTimeMulti() {
  3748. extract($this->dateRegex);
  3749. $result = $this->Form->dateTime('Contact.1.updated');
  3750. $this->assertContains('Contact[1][updated][month]', $result);
  3751. $this->assertContains('Contact[1][updated][day]', $result);
  3752. $this->assertContains('Contact[1][updated][year]', $result);
  3753. $this->assertContains('Contact[1][updated][hour]', $result);
  3754. $this->assertContains('Contact[1][updated][minute]', $result);
  3755. $this->assertContains('Contact[1][updated][meridian]', $result);
  3756. }
  3757. /**
  3758. * When changing the date format, the label should always focus the first select box when
  3759. * clicked.
  3760. *
  3761. * @return void
  3762. */
  3763. public function testDateTimeLabelIdMatchesFirstInput() {
  3764. $this->markTestIncomplete('Need to revisit once models work again.');
  3765. $result = $this->Form->input('Model.date', array('type' => 'date'));
  3766. $this->assertContains('label for="ModelDateMonth"', $result);
  3767. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  3768. $this->assertContains('label for="ModelDateDay"', $result);
  3769. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  3770. $this->assertContains('label for="ModelDateYear"', $result);
  3771. }
  3772. /**
  3773. * testMonth method
  3774. *
  3775. * @return void
  3776. */
  3777. public function testMonth() {
  3778. $result = $this->Form->month('Model.field', ['value' => '']);
  3779. $expected = array(
  3780. array('select' => array('name' => 'Model[field][month]')),
  3781. array('option' => array('value' => '', 'selected' => 'selected')),
  3782. '/option',
  3783. array('option' => array('value' => '01')),
  3784. date('F', strtotime('2008-01-01 00:00:00')),
  3785. '/option',
  3786. array('option' => array('value' => '02')),
  3787. date('F', strtotime('2008-02-01 00:00:00')),
  3788. '/option',
  3789. '*/select',
  3790. );
  3791. $this->assertTags($result, $expected);
  3792. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  3793. $expected = array(
  3794. array('select' => array('name' => 'Model[field][month]')),
  3795. array('option' => array('selected' => 'selected', 'value' => '')),
  3796. '/option',
  3797. array('option' => array('value' => '01')),
  3798. date('F', strtotime('2008-01-01 00:00:00')),
  3799. '/option',
  3800. array('option' => array('value' => '02')),
  3801. date('F', strtotime('2008-02-01 00:00:00')),
  3802. '/option',
  3803. '*/select',
  3804. );
  3805. $this->assertTags($result, $expected);
  3806. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  3807. $expected = array(
  3808. array('select' => array('name' => 'Model[field][month]')),
  3809. array('option' => array('selected' => 'selected', 'value' => '')),
  3810. '/option',
  3811. array('option' => array('value' => '01')),
  3812. '1',
  3813. '/option',
  3814. array('option' => array('value' => '02')),
  3815. '2',
  3816. '/option',
  3817. '*/select',
  3818. );
  3819. $this->assertTags($result, $expected);
  3820. $monthNames = [
  3821. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  3822. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  3823. ];
  3824. $result = $this->Form->month('Model.field', array('value' => '1', 'monthNames' => $monthNames));
  3825. $expected = array(
  3826. array('select' => array('name' => 'Model[field][month]')),
  3827. array('option' => array('value' => '')),
  3828. '/option',
  3829. array('option' => array('value' => '01', 'selected' => 'selected')),
  3830. 'Jan',
  3831. '/option',
  3832. array('option' => array('value' => '02')),
  3833. 'Feb',
  3834. '/option',
  3835. '*/select',
  3836. );
  3837. $this->assertTags($result, $expected);
  3838. $this->Form->request->data['Project']['release'] = '2050-02-10';
  3839. $result = $this->Form->month('Project.release');
  3840. $expected = array(
  3841. array('select' => array('name' => 'Project[release][month]')),
  3842. array('option' => array('value' => '')),
  3843. '/option',
  3844. array('option' => array('value' => '01')),
  3845. 'January',
  3846. '/option',
  3847. array('option' => array('value' => '02', 'selected' => 'selected')),
  3848. 'February',
  3849. '/option',
  3850. '*/select',
  3851. );
  3852. $this->assertTags($result, $expected);
  3853. }
  3854. /**
  3855. * testDay method
  3856. *
  3857. * @return void
  3858. */
  3859. public function testDay() {
  3860. extract($this->dateRegex);
  3861. $result = $this->Form->day('Model.field', array('value' => false));
  3862. $expected = array(
  3863. array('select' => array('name' => 'Model[field][day]')),
  3864. array('option' => array('selected' => 'selected', 'value' => '')),
  3865. '/option',
  3866. array('option' => array('value' => '01')),
  3867. '1',
  3868. '/option',
  3869. array('option' => array('value' => '02')),
  3870. '2',
  3871. '/option',
  3872. $daysRegex,
  3873. '/select',
  3874. );
  3875. $this->assertTags($result, $expected);
  3876. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  3877. $result = $this->Form->day('Model.field');
  3878. $expected = array(
  3879. array('select' => array('name' => 'Model[field][day]')),
  3880. array('option' => array('value' => '')),
  3881. '/option',
  3882. array('option' => array('value' => '01')),
  3883. '1',
  3884. '/option',
  3885. array('option' => array('value' => '02')),
  3886. '2',
  3887. '/option',
  3888. $daysRegex,
  3889. array('option' => array('value' => '10', 'selected' => 'selected')),
  3890. '10',
  3891. '/option',
  3892. $daysRegex,
  3893. '/select',
  3894. );
  3895. $this->assertTags($result, $expected);
  3896. $this->Form->request->data['Model']['field'] = '';
  3897. $result = $this->Form->day('Model.field', array('value' => '10'));
  3898. $expected = array(
  3899. array('select' => array('name' => 'Model[field][day]')),
  3900. array('option' => array('value' => '')),
  3901. '/option',
  3902. array('option' => array('value' => '01')),
  3903. '1',
  3904. '/option',
  3905. array('option' => array('value' => '02')),
  3906. '2',
  3907. '/option',
  3908. $daysRegex,
  3909. array('option' => array('value' => '10', 'selected' => 'selected')),
  3910. '10',
  3911. '/option',
  3912. $daysRegex,
  3913. '/select',
  3914. );
  3915. $this->assertTags($result, $expected);
  3916. $this->Form->request->data['Project']['release'] = '2050-10-10';
  3917. $result = $this->Form->day('Project.release');
  3918. $expected = array(
  3919. array('select' => array('name' => 'Project[release][day]')),
  3920. array('option' => array('value' => '')),
  3921. '/option',
  3922. array('option' => array('value' => '01')),
  3923. '1',
  3924. '/option',
  3925. array('option' => array('value' => '02')),
  3926. '2',
  3927. '/option',
  3928. $daysRegex,
  3929. array('option' => array('value' => '10', 'selected' => 'selected')),
  3930. '10',
  3931. '/option',
  3932. $daysRegex,
  3933. '/select',
  3934. );
  3935. $this->assertTags($result, $expected);
  3936. }
  3937. /**
  3938. * testMinute method
  3939. *
  3940. * @return void
  3941. */
  3942. public function testMinute() {
  3943. extract($this->dateRegex);
  3944. $result = $this->Form->minute('Model.field', ['value' => '']);
  3945. $expected = array(
  3946. array('select' => array('name' => 'Model[field][minute]')),
  3947. array('option' => array('selected' => 'selected', 'value' => '')),
  3948. '/option',
  3949. array('option' => array('value' => '00')),
  3950. '00',
  3951. '/option',
  3952. array('option' => array('value' => '01')),
  3953. '01',
  3954. '/option',
  3955. array('option' => array('value' => '02')),
  3956. '02',
  3957. '/option',
  3958. $minutesRegex,
  3959. '/select',
  3960. );
  3961. $this->assertTags($result, $expected);
  3962. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  3963. $result = $this->Form->minute('Model.field');
  3964. $expected = array(
  3965. array('select' => array('name' => 'Model[field][minute]')),
  3966. array('option' => array('value' => '')),
  3967. '/option',
  3968. array('option' => array('value' => '00')),
  3969. '00',
  3970. '/option',
  3971. array('option' => array('value' => '01')),
  3972. '01',
  3973. '/option',
  3974. array('option' => array('value' => '02')),
  3975. '02',
  3976. '/option',
  3977. $minutesRegex,
  3978. array('option' => array('value' => '12', 'selected' => 'selected')),
  3979. '12',
  3980. '/option',
  3981. $minutesRegex,
  3982. '/select',
  3983. );
  3984. $this->assertTags($result, $expected);
  3985. $this->Form->request->data['Model']['field'] = '';
  3986. $result = $this->Form->minute('Model.field', array('interval' => 5));
  3987. $expected = array(
  3988. array('select' => array('name' => 'Model[field][minute]')),
  3989. array('option' => array('selected' => 'selected', 'value' => '')),
  3990. '/option',
  3991. array('option' => array('value' => '00')),
  3992. '00',
  3993. '/option',
  3994. array('option' => array('value' => '05')),
  3995. '05',
  3996. '/option',
  3997. array('option' => array('value' => '10')),
  3998. '10',
  3999. '/option',
  4000. $minutesRegex,
  4001. '/select',
  4002. );
  4003. $this->assertTags($result, $expected);
  4004. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  4005. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4006. $expected = array(
  4007. array('select' => array('name' => 'Model[field][minute]')),
  4008. array('option' => array('value' => '')),
  4009. '/option',
  4010. array('option' => array('value' => '00')),
  4011. '00',
  4012. '/option',
  4013. array('option' => array('value' => '05')),
  4014. '05',
  4015. '/option',
  4016. array('option' => array('value' => '10', 'selected' => 'selected')),
  4017. '10',
  4018. '/option',
  4019. $minutesRegex,
  4020. '/select',
  4021. );
  4022. $this->assertTags($result, $expected);
  4023. }
  4024. /**
  4025. * Test generating an input for the meridian.
  4026. *
  4027. * @return void
  4028. */
  4029. public function testMeridian() {
  4030. extract($this->dateRegex);
  4031. $now = time();
  4032. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  4033. $expected = [
  4034. array('select' => array('name' => 'Model[field][meridian]')),
  4035. array('option' => array('value' => '')),
  4036. '/option',
  4037. $meridianRegex,
  4038. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4039. date('a', $now),
  4040. '/option',
  4041. '*/select'
  4042. ];
  4043. $this->assertTags($result, $expected);
  4044. }
  4045. /**
  4046. * testHour method
  4047. *
  4048. * @return void
  4049. */
  4050. public function testHour() {
  4051. extract($this->dateRegex);
  4052. $result = $this->Form->hour('Model.field', ['value' => '']);
  4053. $expected = array(
  4054. array('select' => array('name' => 'Model[field][hour]')),
  4055. array('option' => array('selected' => 'selected', 'value' => '')),
  4056. '/option',
  4057. array('option' => array('value' => '01')),
  4058. '1',
  4059. '/option',
  4060. array('option' => array('value' => '02')),
  4061. '2',
  4062. '/option',
  4063. $hoursRegex,
  4064. '/select',
  4065. );
  4066. $this->assertTags($result, $expected);
  4067. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4068. $result = $this->Form->hour('Model.field', ['format' => 12]);
  4069. $expected = array(
  4070. array('select' => array('name' => 'Model[field][hour]')),
  4071. array('option' => array('value' => '')),
  4072. '/option',
  4073. array('option' => array('value' => '01')),
  4074. '1',
  4075. '/option',
  4076. array('option' => array('value' => '02')),
  4077. '2',
  4078. '/option',
  4079. $hoursRegex,
  4080. array('option' => array('value' => '12', 'selected' => 'selected')),
  4081. '12',
  4082. '/option',
  4083. '/select',
  4084. );
  4085. $this->assertTags($result, $expected);
  4086. $this->Form->request->data['Model']['field'] = '';
  4087. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => '23'));
  4088. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  4089. $result = $this->Form->hour('Model.field', array('format' => 12, 'value' => '23'));
  4090. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  4091. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4092. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4093. $expected = array(
  4094. array('select' => array('name' => 'Model[field][hour]')),
  4095. array('option' => array('value' => '')),
  4096. '/option',
  4097. array('option' => array('value' => '00', 'selected' => 'selected')),
  4098. '0',
  4099. '/option',
  4100. array('option' => array('value' => '01')),
  4101. '1',
  4102. '/option',
  4103. array('option' => array('value' => '02')),
  4104. '2',
  4105. '/option',
  4106. $hoursRegex,
  4107. '/select',
  4108. );
  4109. $this->assertTags($result, $expected);
  4110. unset($this->Form->request->data['Model']['field']);
  4111. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => 'now'));
  4112. $thisHour = date('H');
  4113. $optValue = date('G');
  4114. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  4115. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  4116. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4117. $expected = array(
  4118. array('select' => array('name' => 'Model[field][hour]')),
  4119. array('option' => array('value' => '')),
  4120. '/option',
  4121. array('option' => array('value' => '00')),
  4122. '0',
  4123. '/option',
  4124. array('option' => array('value' => '01', 'selected' => 'selected')),
  4125. '1',
  4126. '/option',
  4127. array('option' => array('value' => '02')),
  4128. '2',
  4129. '/option',
  4130. $hoursRegex,
  4131. '/select',
  4132. );
  4133. $this->assertTags($result, $expected);
  4134. }
  4135. /**
  4136. * testYear method
  4137. *
  4138. * @return void
  4139. */
  4140. public function testYear() {
  4141. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  4142. $expected = array(
  4143. array('select' => array('name' => 'Model[field][year]')),
  4144. array('option' => array('selected' => 'selected', 'value' => '')),
  4145. '/option',
  4146. array('option' => array('value' => '2007')),
  4147. '2007',
  4148. '/option',
  4149. array('option' => array('value' => '2006')),
  4150. '2006',
  4151. '/option',
  4152. '/select',
  4153. );
  4154. $this->assertTags($result, $expected);
  4155. $result = $this->Form->year('Model.field', [
  4156. 'value' => '',
  4157. 'minYear' => 2006,
  4158. 'maxYear' => 2007,
  4159. 'orderYear' => 'asc'
  4160. ]);
  4161. $expected = array(
  4162. array('select' => array('name' => 'Model[field][year]')),
  4163. array('option' => array('selected' => 'selected', 'value' => '')),
  4164. '/option',
  4165. array('option' => array('value' => '2006')),
  4166. '2006',
  4167. '/option',
  4168. array('option' => array('value' => '2007')),
  4169. '2007',
  4170. '/option',
  4171. '/select',
  4172. );
  4173. $this->assertTags($result, $expected);
  4174. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  4175. $result = $this->Form->year('Contact.published', [
  4176. 'empty' => false,
  4177. 'minYear' => 2006,
  4178. 'maxYear' => 2007,
  4179. ]);
  4180. $expected = array(
  4181. array('select' => array('name' => 'Contact[published][year]')),
  4182. array('option' => array('value' => '2007')),
  4183. '2007',
  4184. '/option',
  4185. array('option' => array('value' => '2006', 'selected' => 'selected')),
  4186. '2006',
  4187. '/option',
  4188. '/select',
  4189. );
  4190. $this->assertTags($result, $expected);
  4191. }
  4192. /**
  4193. * testYearAutoExpandRange method
  4194. *
  4195. * @return void
  4196. */
  4197. public function testYearAutoExpandRange() {
  4198. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  4199. $result = $this->Form->year('User.birthday');
  4200. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4201. $result = $matches[1];
  4202. $expected = range(date('Y') + 5, 1930);
  4203. $this->assertEquals($expected, $result);
  4204. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4205. $result = $this->Form->year('Project.release');
  4206. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4207. $result = $matches[1];
  4208. $expected = range(2050, date('Y') - 5);
  4209. $this->assertEquals($expected, $result);
  4210. $this->Form->request->data['Project']['release'] = '1881-10-10';
  4211. $result = $this->Form->year('Project.release', [
  4212. 'minYear' => 1890,
  4213. 'maxYear' => 1900
  4214. ]);
  4215. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4216. $result = $matches[1];
  4217. $expected = range(1900, 1881);
  4218. $this->assertEquals($expected, $result);
  4219. }
  4220. /**
  4221. * Test that input() accepts the type of date and passes options in.
  4222. *
  4223. * @return void
  4224. */
  4225. public function testInputDate() {
  4226. $this->Form->request->data = array(
  4227. 'month_year' => array('month' => date('m')),
  4228. );
  4229. $this->Form->create($this->article);
  4230. $result = $this->Form->input('month_year', array(
  4231. 'label' => false,
  4232. 'type' => 'date',
  4233. 'minYear' => 2006,
  4234. 'maxYear' => 2008
  4235. ));
  4236. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  4237. $this->assertNotContains('value="2008" selected="selected"', $result);
  4238. }
  4239. /**
  4240. * testInputDateMaxYear method
  4241. *
  4242. * Let's say we want to only allow users born from 2006 to 2008 to register
  4243. * This being the first singup page, we still don't have any data
  4244. *
  4245. * @return void
  4246. */
  4247. public function testInputDateMaxYear() {
  4248. $this->Form->request->data = [];
  4249. $this->Form->create($this->article);
  4250. $result = $this->Form->input('birthday', array(
  4251. 'label' => false,
  4252. 'type' => 'date',
  4253. 'minYear' => 2006,
  4254. 'maxYear' => 2008
  4255. ));
  4256. $this->assertContains('value="2008" selected="selected"', $result);
  4257. }
  4258. /**
  4259. * testTextArea method
  4260. *
  4261. * @return void
  4262. */
  4263. public function testTextArea() {
  4264. $this->Form->request->data = array('field' => 'some test data');
  4265. $result = $this->Form->textarea('field');
  4266. $expected = array(
  4267. 'textarea' => array('name' => 'field'),
  4268. 'some test data',
  4269. '/textarea',
  4270. );
  4271. $this->assertTags($result, $expected);
  4272. $result = $this->Form->textarea('user.bio');
  4273. $expected = array(
  4274. 'textarea' => array('name' => 'user[bio]'),
  4275. '/textarea',
  4276. );
  4277. $this->assertTags($result, $expected);
  4278. $this->Form->request->data = array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  4279. $result = $this->Form->textarea('field');
  4280. $expected = array(
  4281. 'textarea' => array('name' => 'field'),
  4282. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  4283. '/textarea',
  4284. );
  4285. $this->assertTags($result, $expected);
  4286. $this->Form->request->data = [
  4287. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  4288. ];
  4289. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  4290. $expected = array(
  4291. 'textarea' => array('name' => 'Model[field]'),
  4292. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  4293. '/textarea',
  4294. );
  4295. $this->assertTags($result, $expected);
  4296. $result = $this->Form->textarea('0.OtherModel.field');
  4297. $expected = array(
  4298. 'textarea' => array('name' => '0[OtherModel][field]'),
  4299. '/textarea'
  4300. );
  4301. $this->assertTags($result, $expected);
  4302. }
  4303. /**
  4304. * testTextAreaWithStupidCharacters method
  4305. *
  4306. * test text area with non-ascii characters
  4307. *
  4308. * @return void
  4309. */
  4310. public function testTextAreaWithStupidCharacters() {
  4311. $result = $this->Form->textarea('Post.content', [
  4312. 'value' => "GREAT®",
  4313. 'rows' => '15',
  4314. 'cols' => '75'
  4315. ]);
  4316. $expected = [
  4317. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  4318. 'GREAT®',
  4319. '/textarea',
  4320. ];
  4321. $this->assertTags($result, $expected);
  4322. }
  4323. /**
  4324. * testHiddenField method
  4325. *
  4326. * @return void
  4327. */
  4328. public function testHiddenField() {
  4329. $this->article['errors'] = [
  4330. 'field' => true
  4331. ];
  4332. $this->Form->request->data['field'] = 'test';
  4333. $this->Form->create($this->article);
  4334. $result = $this->Form->hidden('field', array('id' => 'theID'));
  4335. $this->assertTags($result, array(
  4336. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test'))
  4337. );
  4338. $result = $this->Form->hidden('field', ['value' => 'my value']);
  4339. $expected = [
  4340. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  4341. ];
  4342. $this->assertTags($result, $expected);
  4343. }
  4344. /**
  4345. * testFileUploadField method
  4346. *
  4347. * @return void
  4348. */
  4349. public function testFileUploadField() {
  4350. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  4351. $result = $this->Form->file('Model.upload');
  4352. $this->assertTags($result, $expected);
  4353. $this->Form->request->data['Model']['upload'] = [
  4354. 'name' => '', 'type' => '', 'tmp_name' => '',
  4355. 'error' => 4, 'size' => 0
  4356. ];
  4357. $result = $this->Form->file('Model.upload');
  4358. $this->assertTags($result, $expected);
  4359. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  4360. $result = $this->Form->file('Model.upload');
  4361. $this->assertTags($result, $expected);
  4362. }
  4363. /**
  4364. * test File upload input on a model not used in create();
  4365. *
  4366. * @return void
  4367. */
  4368. public function testFileUploadOnOtherModel() {
  4369. $this->Form->create($this->article, array('type' => 'file'));
  4370. $result = $this->Form->file('ValidateProfile.city');
  4371. $expected = array(
  4372. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]')
  4373. );
  4374. $this->assertTags($result, $expected);
  4375. }
  4376. /**
  4377. * testButton method
  4378. *
  4379. * @return void
  4380. */
  4381. public function testButton() {
  4382. $result = $this->Form->button('Hi');
  4383. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  4384. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  4385. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  4386. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  4387. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  4388. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  4389. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  4390. $result = $this->Form->button('No type', array('type' => false));
  4391. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  4392. $result = $this->Form->button('Upload Text', array(
  4393. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  4394. 'escape' => false
  4395. ));
  4396. $this->assertNotRegExp('/\&039/', $result);
  4397. }
  4398. /**
  4399. * Test that button() makes unlocked fields by default.
  4400. *
  4401. * @return void
  4402. */
  4403. public function testButtonUnlockedByDefault() {
  4404. $this->Form->request->params['_csrfToken'] = 'secured';
  4405. $this->Form->button('Save', array('name' => 'save'));
  4406. $this->Form->button('Clear');
  4407. $result = $this->Form->unlockField();
  4408. $this->assertEquals(array('save'), $result);
  4409. }
  4410. /**
  4411. * testPostButton method
  4412. *
  4413. * @return void
  4414. */
  4415. public function testPostButton() {
  4416. $result = $this->Form->postButton('Hi', '/controller/action');
  4417. $this->assertTags($result, array(
  4418. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  4419. 'div' => array('style' => 'display:none;'),
  4420. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4421. '/div',
  4422. 'button' => array('type' => 'submit'),
  4423. 'Hi',
  4424. '/button',
  4425. '/form'
  4426. ));
  4427. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  4428. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  4429. }
  4430. /**
  4431. * Test using postButton with N dimensional data.
  4432. *
  4433. * @return void
  4434. */
  4435. public function testPostButtonNestedData() {
  4436. $data = array(
  4437. 'one' => array(
  4438. 'two' => array(
  4439. 3, 4, 5
  4440. )
  4441. )
  4442. );
  4443. $result = $this->Form->postButton('Send', '/', array('data' => $data));
  4444. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  4445. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  4446. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  4447. }
  4448. /**
  4449. * Test that postButton adds _Token fields.
  4450. *
  4451. * @return void
  4452. */
  4453. public function testSecurePostButton() {
  4454. $this->Form->request->params['_csrfToken'] = 'testkey';
  4455. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  4456. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  4457. $expected = array(
  4458. 'form' => array(
  4459. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  4460. ),
  4461. array('div' => array('style' => 'display:none;')),
  4462. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  4463. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  4464. '/div',
  4465. 'button' => array('type' => 'submit'),
  4466. 'Delete',
  4467. '/button',
  4468. array('div' => array('style' => 'display:none;')),
  4469. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  4470. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  4471. '/div',
  4472. '/form',
  4473. );
  4474. $this->assertTags($result, $expected);
  4475. }
  4476. /**
  4477. * testPostLink method
  4478. *
  4479. * @return void
  4480. */
  4481. public function testPostLink() {
  4482. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  4483. $this->assertTags($result, array(
  4484. 'form' => array(
  4485. 'method' => 'post', 'action' => '/posts/delete/1',
  4486. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4487. ),
  4488. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4489. '/form',
  4490. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4491. 'Delete',
  4492. '/a'
  4493. ));
  4494. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  4495. $this->assertTags($result, array(
  4496. 'form' => array(
  4497. 'method' => 'post', 'action' => '/posts/delete/1',
  4498. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4499. ),
  4500. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  4501. '/form',
  4502. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4503. 'Delete',
  4504. '/a'
  4505. ));
  4506. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  4507. $this->assertTags($result, array(
  4508. 'form' => array(
  4509. 'method' => 'post', 'action' => '/posts/delete/1',
  4510. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4511. ),
  4512. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4513. '/form',
  4514. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  4515. 'Delete',
  4516. '/a'
  4517. ));
  4518. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  4519. $this->assertTags($result, array(
  4520. 'form' => array(
  4521. 'method' => 'post', 'action' => '/posts/delete/1',
  4522. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4523. ),
  4524. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4525. '/form',
  4526. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;&#039;Confirm&#039; this \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  4527. 'Delete',
  4528. '/a'
  4529. ));
  4530. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  4531. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  4532. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  4533. $this->assertTags($result, array(
  4534. 'form' => array(
  4535. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  4536. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4537. ),
  4538. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4539. '/form',
  4540. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4541. 'Delete',
  4542. '/a'
  4543. ));
  4544. $result = $this->Form->postLink(
  4545. '',
  4546. array('controller' => 'items', 'action' => 'delete', 10),
  4547. array('class' => 'btn btn-danger', 'escape' => false),
  4548. 'Confirm thing'
  4549. );
  4550. $this->assertTags($result, array(
  4551. 'form' => array(
  4552. 'method' => 'post', 'action' => '/items/delete/10',
  4553. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4554. ),
  4555. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4556. '/form',
  4557. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  4558. '/a'
  4559. ));
  4560. }
  4561. /**
  4562. * Test using postLink with N dimensional data.
  4563. *
  4564. * @return void
  4565. */
  4566. public function testPostLinkNestedData() {
  4567. $data = array(
  4568. 'one' => array(
  4569. 'two' => array(
  4570. 3, 4, 5
  4571. )
  4572. )
  4573. );
  4574. $result = $this->Form->postLink('Send', '/', array('data' => $data));
  4575. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  4576. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  4577. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  4578. }
  4579. /**
  4580. * test creating postLinks after a GET form.
  4581. *
  4582. * @return void
  4583. */
  4584. public function testPostLinkAfterGetForm() {
  4585. $this->Form->request->params['_csrfToken'] = 'testkey';
  4586. $this->Form->request->params['_Token'] = 'val';
  4587. $this->Form->create($this->article, array('type' => 'get'));
  4588. $this->Form->end();
  4589. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  4590. $this->assertTags($result, array(
  4591. 'form' => array(
  4592. 'method' => 'post', 'action' => '/posts/delete/1',
  4593. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4594. ),
  4595. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  4596. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  4597. 'div' => array('style' => 'display:none;'),
  4598. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  4599. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  4600. '/div',
  4601. '/form',
  4602. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4603. 'Delete',
  4604. '/a'
  4605. ));
  4606. }
  4607. /**
  4608. * Test that postLink adds form tags to view block
  4609. *
  4610. * @return void
  4611. */
  4612. public function testPostLinkFormBuffer() {
  4613. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
  4614. $this->assertTags($result, array(
  4615. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4616. 'Delete',
  4617. '/a'
  4618. ));
  4619. $result = $this->View->fetch('postLink');
  4620. $this->assertTags($result, array(
  4621. 'form' => array(
  4622. 'method' => 'post', 'action' => '/posts/delete/1',
  4623. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4624. ),
  4625. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4626. '/form'
  4627. ));
  4628. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  4629. array('inline' => false, 'method' => 'DELETE')
  4630. );
  4631. $this->assertTags($result, array(
  4632. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4633. 'Delete',
  4634. '/a'
  4635. ));
  4636. $result = $this->View->fetch('postLink');
  4637. $this->assertTags($result, array(
  4638. 'form' => array(
  4639. 'method' => 'post', 'action' => '/posts/delete/1',
  4640. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4641. ),
  4642. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4643. '/form',
  4644. array(
  4645. 'form' => array(
  4646. 'method' => 'post', 'action' => '/posts/delete/2',
  4647. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4648. ),
  4649. ),
  4650. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  4651. '/form'
  4652. ));
  4653. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  4654. $this->assertTags($result, array(
  4655. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4656. 'Delete',
  4657. '/a'
  4658. ));
  4659. $result = $this->View->fetch('foobar');
  4660. $this->assertTags($result, array(
  4661. 'form' => array(
  4662. 'method' => 'post', 'action' => '/posts/delete/1',
  4663. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4664. ),
  4665. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4666. '/form'
  4667. ));
  4668. }
  4669. /**
  4670. * testSubmitButton method
  4671. *
  4672. * @return void
  4673. */
  4674. public function testSubmitButton() {
  4675. $result = $this->Form->submit('');
  4676. $expected = array(
  4677. 'div' => array('class' => 'submit'),
  4678. 'input' => array('type' => 'submit', 'value' => ''),
  4679. '/div'
  4680. );
  4681. $this->assertTags($result, $expected);
  4682. $result = $this->Form->submit('Test Submit');
  4683. $expected = array(
  4684. 'div' => array('class' => 'submit'),
  4685. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  4686. '/div'
  4687. );
  4688. $this->assertTags($result, $expected);
  4689. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  4690. $expected = array(
  4691. 'span' => array('class' => 'submit'),
  4692. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  4693. '/span'
  4694. );
  4695. $this->assertTags($result, $expected);
  4696. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  4697. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  4698. $this->assertTags($result, $expected);
  4699. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  4700. $expected = array(
  4701. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  4702. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  4703. '/div'
  4704. );
  4705. $this->assertTags($result, $expected);
  4706. $result = $this->Form->submit('Next >');
  4707. $expected = array(
  4708. 'div' => array('class' => 'submit'),
  4709. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  4710. '/div'
  4711. );
  4712. $this->assertTags($result, $expected);
  4713. $result = $this->Form->submit('Next >', array('escape' => false));
  4714. $expected = array(
  4715. 'div' => array('class' => 'submit'),
  4716. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  4717. '/div'
  4718. );
  4719. $this->assertTags($result, $expected);
  4720. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  4721. $expected = array(
  4722. 'div' => array('class' => 'submit'),
  4723. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  4724. '/div'
  4725. );
  4726. $this->assertTags($result, $expected);
  4727. $before = '--before--';
  4728. $after = '--after--';
  4729. $result = $this->Form->submit('Test', array('before' => $before));
  4730. $expected = array(
  4731. 'div' => array('class' => 'submit'),
  4732. '--before--',
  4733. 'input' => array('type' => 'submit', 'value' => 'Test'),
  4734. '/div'
  4735. );
  4736. $this->assertTags($result, $expected);
  4737. $result = $this->Form->submit('Test', array('after' => $after));
  4738. $expected = array(
  4739. 'div' => array('class' => 'submit'),
  4740. 'input' => array('type' => 'submit', 'value' => 'Test'),
  4741. '--after--',
  4742. '/div'
  4743. );
  4744. $this->assertTags($result, $expected);
  4745. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  4746. $expected = array(
  4747. 'div' => array('class' => 'submit'),
  4748. '--before--',
  4749. 'input' => array('type' => 'submit', 'value' => 'Test'),
  4750. '--after--',
  4751. '/div'
  4752. );
  4753. $this->assertTags($result, $expected);
  4754. }
  4755. /**
  4756. * test image submit types.
  4757. *
  4758. * @return void
  4759. */
  4760. public function testSubmitImage() {
  4761. $result = $this->Form->submit('http://example.com/cake.power.gif');
  4762. $expected = array(
  4763. 'div' => array('class' => 'submit'),
  4764. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  4765. '/div'
  4766. );
  4767. $this->assertTags($result, $expected);
  4768. $result = $this->Form->submit('/relative/cake.power.gif');
  4769. $expected = array(
  4770. 'div' => array('class' => 'submit'),
  4771. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  4772. '/div'
  4773. );
  4774. $this->assertTags($result, $expected);
  4775. $result = $this->Form->submit('cake.power.gif');
  4776. $expected = array(
  4777. 'div' => array('class' => 'submit'),
  4778. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  4779. '/div'
  4780. );
  4781. $this->assertTags($result, $expected);
  4782. $result = $this->Form->submit('Not.an.image');
  4783. $expected = array(
  4784. 'div' => array('class' => 'submit'),
  4785. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  4786. '/div'
  4787. );
  4788. $this->assertTags($result, $expected);
  4789. $after = '--after--';
  4790. $before = '--before--';
  4791. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  4792. $expected = array(
  4793. 'div' => array('class' => 'submit'),
  4794. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  4795. '--after--',
  4796. '/div'
  4797. );
  4798. $this->assertTags($result, $expected);
  4799. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  4800. $expected = array(
  4801. 'div' => array('class' => 'submit'),
  4802. '--before--',
  4803. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  4804. '/div'
  4805. );
  4806. $this->assertTags($result, $expected);
  4807. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  4808. $expected = array(
  4809. 'div' => array('class' => 'submit'),
  4810. '--before--',
  4811. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  4812. '--after--',
  4813. '/div'
  4814. );
  4815. $this->assertTags($result, $expected);
  4816. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  4817. $expected = array(
  4818. 'div' => array('class' => 'submit'),
  4819. '--before--',
  4820. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  4821. '--after--',
  4822. '/div'
  4823. );
  4824. $this->assertTags($result, $expected);
  4825. }
  4826. /**
  4827. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  4828. * be submitted at a time.
  4829. *
  4830. * @return void
  4831. */
  4832. public function testSubmitUnlockedByDefault() {
  4833. $this->Form->request->params['_Token'] = 'secured';
  4834. $this->Form->submit('Go go');
  4835. $this->Form->submit('Save', array('name' => 'save'));
  4836. $result = $this->Form->unlockField();
  4837. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  4838. }
  4839. /**
  4840. * Test submit image with timestamps.
  4841. *
  4842. * @return void
  4843. */
  4844. public function testSubmitImageTimestamp() {
  4845. Configure::write('Asset.timestamp', 'force');
  4846. $result = $this->Form->submit('cake.power.gif');
  4847. $expected = array(
  4848. 'div' => array('class' => 'submit'),
  4849. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  4850. '/div'
  4851. );
  4852. $this->assertTags($result, $expected);
  4853. }
  4854. /**
  4855. * test that datetime() works with GET style forms.
  4856. *
  4857. * @return void
  4858. */
  4859. public function testDateTimeWithGetForms() {
  4860. extract($this->dateRegex);
  4861. $this->Form->create($this->article, array('type' => 'get'));
  4862. $result = $this->Form->datetime('created');
  4863. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  4864. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  4865. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  4866. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  4867. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  4868. $this->assertContains('name="created[meridian]"', $result, 'meridian name attribute is wrong.');
  4869. }
  4870. /**
  4871. * testForMagicInputNonExistingNorValidated method
  4872. *
  4873. * @return void
  4874. */
  4875. public function testForMagicInputNonExistingNorValidated() {
  4876. $result = $this->Form->create($this->article);
  4877. $this->Form->templates(['groupContainer' => '{{content}}']);
  4878. $result = $this->Form->input('non_existing_nor_validated');
  4879. $expected = array(
  4880. 'label' => array('for' => 'non-existing-nor-validated'),
  4881. 'Non Existing Nor Validated',
  4882. '/label',
  4883. 'input' => array(
  4884. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  4885. 'id' => 'non-existing-nor-validated'
  4886. )
  4887. );
  4888. $this->assertTags($result, $expected);
  4889. $result = $this->Form->input('non_existing_nor_validated', array(
  4890. 'val' => 'my value'
  4891. ));
  4892. $expected = array(
  4893. 'label' => array('for' => 'non-existing-nor-validated'),
  4894. 'Non Existing Nor Validated',
  4895. '/label',
  4896. 'input' => array(
  4897. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  4898. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  4899. )
  4900. );
  4901. $this->assertTags($result, $expected);
  4902. $this->Form->request->data = array('non_existing_nor_validated' => 'CakePHP magic');
  4903. $result = $this->Form->input('non_existing_nor_validated');
  4904. $expected = array(
  4905. 'label' => array('for' => 'non-existing-nor-validated'),
  4906. 'Non Existing Nor Validated',
  4907. '/label',
  4908. 'input' => array(
  4909. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  4910. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  4911. )
  4912. );
  4913. $this->assertTags($result, $expected);
  4914. }
  4915. /**
  4916. * testFormMagicInputLabel method
  4917. *
  4918. * @return void
  4919. */
  4920. public function testFormMagicInputLabel() {
  4921. TableRegistry::get('Contacts', [
  4922. 'className' => __NAMESPACE__ . '\ContactsTable'
  4923. ]);
  4924. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  4925. $this->Form->templates(['groupContainer' => '{{content}}']);
  4926. $result = $this->Form->input('Contacts.name', array('label' => 'My label'));
  4927. $expected = array(
  4928. 'label' => array('for' => 'contacts-name'),
  4929. 'My label',
  4930. '/label',
  4931. 'input' => array(
  4932. 'type' => 'text', 'name' => 'Contacts[name]',
  4933. 'id' => 'contacts-name', 'maxlength' => '255'
  4934. )
  4935. );
  4936. $this->assertTags($result, $expected);
  4937. $result = $this->Form->input('name', array(
  4938. 'label' => array('class' => 'mandatory')
  4939. ));
  4940. $expected = array(
  4941. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  4942. 'Name',
  4943. '/label',
  4944. 'input' => array(
  4945. 'type' => 'text', 'name' => 'name',
  4946. 'id' => 'name', 'maxlength' => '255'
  4947. )
  4948. );
  4949. $this->assertTags($result, $expected);
  4950. $result = $this->Form->input('name', array(
  4951. 'div' => false,
  4952. 'label' => array('class' => 'mandatory', 'text' => 'My label')
  4953. ));
  4954. $expected = array(
  4955. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  4956. 'My label',
  4957. '/label',
  4958. 'input' => array(
  4959. 'type' => 'text', 'name' => 'name',
  4960. 'id' => 'name', 'maxlength' => '255'
  4961. )
  4962. );
  4963. $this->assertTags($result, $expected);
  4964. $result = $this->Form->input('Contact.name', array(
  4965. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  4966. ));
  4967. $expected = array(
  4968. 'label' => array('for' => 'my_id'),
  4969. 'Name',
  4970. '/label',
  4971. 'input' => array(
  4972. 'type' => 'text', 'name' => 'Contact[name]',
  4973. 'id' => 'my_id', 'maxlength' => '255'
  4974. )
  4975. );
  4976. $this->assertTags($result, $expected);
  4977. $result = $this->Form->input('1.id');
  4978. $this->assertTags($result, array('input' => array(
  4979. 'type' => 'hidden', 'name' => '1[id]',
  4980. 'id' => '1-id'
  4981. )));
  4982. $result = $this->Form->input("1.name");
  4983. $expected = array(
  4984. 'label' => array('for' => '1-name'),
  4985. 'Name',
  4986. '/label',
  4987. 'input' => array(
  4988. 'type' => 'text', 'name' => '1[name]',
  4989. 'id' => '1-name', 'maxlength' => '255'
  4990. )
  4991. );
  4992. $this->assertTags($result, $expected);
  4993. }
  4994. /**
  4995. * testFormEnd method
  4996. *
  4997. * @return void
  4998. */
  4999. public function testFormEnd() {
  5000. $this->assertEquals('</form>', $this->Form->end());
  5001. }
  5002. /**
  5003. * Test the generation of fields for a multi record form.
  5004. *
  5005. * @return void
  5006. */
  5007. public function testMultiRecordForm() {
  5008. $this->loadFixtures('Article', 'Comment');
  5009. $articles = TableRegistry::get('Articles');
  5010. $articles->hasMany('Comments');
  5011. $comment = new Entity(['comment' => 'Value']);
  5012. $article = new Article(['comments' => [$comment]]);
  5013. $this->Form->create([$article]);
  5014. $result = $this->Form->input('0.comments.1.comment');
  5015. $expected = array(
  5016. 'div' => array('class' => 'input textarea'),
  5017. 'label' => array('for' => '0-comments-1-comment'),
  5018. 'Comment',
  5019. '/label',
  5020. 'textarea' => array(
  5021. 'name',
  5022. 'id' => '0-comments-1-comment'
  5023. ),
  5024. '/textarea',
  5025. '/div'
  5026. );
  5027. $this->assertTags($result, $expected);
  5028. $result = $this->Form->input('0.comments.0.comment');
  5029. $expected = array(
  5030. 'div' => array('class' => 'input textarea'),
  5031. 'label' => array('for' => '0-comments-0-comment'),
  5032. 'Comment',
  5033. '/label',
  5034. 'textarea' => array(
  5035. 'name',
  5036. 'id' => '0-comments-0-comment'
  5037. ),
  5038. 'Value',
  5039. '/textarea',
  5040. '/div'
  5041. );
  5042. $this->assertTags($result, $expected);
  5043. $comment->errors('comment', ['Not valid']);
  5044. $result = $this->Form->input('0.comments.0.comment');
  5045. $expected = array(
  5046. 'div' => array('class' => 'input textarea error'),
  5047. 'label' => array('for' => '0-comments-0-comment'),
  5048. 'Comment',
  5049. '/label',
  5050. 'textarea' => array(
  5051. 'name',
  5052. 'class' => 'form-error',
  5053. 'id' => '0-comments-0-comment'
  5054. ),
  5055. 'Value',
  5056. '/textarea',
  5057. array('div' => array('class' => 'error-message')),
  5058. 'Not valid',
  5059. '/div',
  5060. '/div'
  5061. );
  5062. $this->assertTags($result, $expected);
  5063. TableRegistry::get('Comments')
  5064. ->validator('default')
  5065. ->allowEmpty('comment', false);
  5066. $result = $this->Form->input('0.comments.1.comment');
  5067. $expected = array(
  5068. 'div' => array('class' => 'input textarea required'),
  5069. 'label' => array('for' => '0-comments-1-comment'),
  5070. 'Comment',
  5071. '/label',
  5072. 'textarea' => array(
  5073. 'name',
  5074. 'id' => '0-comments-1-comment'
  5075. ),
  5076. '/textarea',
  5077. '/div'
  5078. );
  5079. $this->assertTags($result, $expected);
  5080. }
  5081. /**
  5082. * test that some html5 inputs + FormHelper::__call() work
  5083. *
  5084. * @return void
  5085. */
  5086. public function testHtml5Inputs() {
  5087. $result = $this->Form->email('User.email');
  5088. $expected = array(
  5089. 'input' => array('type' => 'email', 'name' => 'User[email]')
  5090. );
  5091. $this->assertTags($result, $expected);
  5092. $result = $this->Form->search('User.query');
  5093. $expected = array(
  5094. 'input' => array('type' => 'search', 'name' => 'User[query]')
  5095. );
  5096. $this->assertTags($result, $expected);
  5097. $result = $this->Form->search('User.query', array('value' => 'test'));
  5098. $expected = array(
  5099. 'input' => array('type' => 'search', 'name' => 'User[query]', 'value' => 'test')
  5100. );
  5101. $this->assertTags($result, $expected);
  5102. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  5103. $expected = array(
  5104. 'input' => array('type' => 'text', 'name' => 'User[query]', 'value' => 'test')
  5105. );
  5106. $this->assertTags($result, $expected);
  5107. }
  5108. /**
  5109. * Test accessing html5 inputs through input().
  5110. *
  5111. * @return void
  5112. */
  5113. public function testHtml5InputWithInput() {
  5114. $this->Form->create();
  5115. $this->Form->templates(['groupContainer' => '{{content}}']);
  5116. $result = $this->Form->input('website', array(
  5117. 'type' => 'url',
  5118. 'val' => 'http://domain.tld',
  5119. 'label' => false
  5120. ));
  5121. $expected = array(
  5122. 'input' => array('type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld')
  5123. );
  5124. $this->assertTags($result, $expected);
  5125. }
  5126. /**
  5127. * Test errors when field name is missing.
  5128. *
  5129. * @expectedException \Cake\Error\Exception
  5130. * @return void
  5131. */
  5132. public function testHtml5InputException() {
  5133. $this->Form->email();
  5134. }
  5135. /**
  5136. * Tests that formhelper sets required attributes.
  5137. *
  5138. * @return void
  5139. */
  5140. public function testRequiredAttribute() {
  5141. $this->article['required'] = [
  5142. 'title' => true,
  5143. 'body' => false,
  5144. ];
  5145. $this->Form->create($this->article);
  5146. $result = $this->Form->input('title');
  5147. $expected = array(
  5148. 'div' => array('class' => 'input text required'),
  5149. 'label' => array('for' => 'title'),
  5150. 'Title',
  5151. '/label',
  5152. 'input' => array(
  5153. 'type' => 'text',
  5154. 'name' => 'title',
  5155. 'id' => 'title',
  5156. 'required' => 'required',
  5157. ),
  5158. '/div'
  5159. );
  5160. $this->assertTags($result, $expected);
  5161. $result = $this->Form->input('title', ['required' => false]);
  5162. $this->assertNotContains('required', $result);
  5163. $result = $this->Form->input('body');
  5164. $expected = array(
  5165. 'div' => array('class' => 'input text'),
  5166. 'label' => array('for' => 'body'),
  5167. 'Body',
  5168. '/label',
  5169. 'input' => array(
  5170. 'type' => 'text',
  5171. 'name' => 'body',
  5172. 'id' => 'body',
  5173. ),
  5174. '/div'
  5175. );
  5176. $this->assertTags($result, $expected);
  5177. $result = $this->Form->input('body', ['required' => true]);
  5178. $this->assertContains('required', $result);
  5179. }
  5180. }