FormHelperTest.php 168 KB

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