FormHelperTest.php 236 KB

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