FormHelperTest.php 229 KB

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