FormHelperTest.php 181 KB

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