FormHelperTest.php 179 KB

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