FormHelperTest.php 224 KB

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