FormHelperTest.php 169 KB

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