FormHelperTest.php 178 KB

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