FormHelperTest.php 178 KB

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