FormHelperTest.php 177 KB

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