| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178 |
- <?php
- namespace Illuminate\Tests\Validation;
- use DateTime;
- use DateTimeImmutable;
- use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
- use Illuminate\Container\Container;
- use Illuminate\Contracts\Auth\Authenticatable;
- use Illuminate\Contracts\Auth\Guard;
- use Illuminate\Contracts\Hashing\Hasher;
- use Illuminate\Contracts\Translation\Translator as TranslatorContract;
- use Illuminate\Contracts\Validation\DataAwareRule;
- use Illuminate\Contracts\Validation\ImplicitRule;
- use Illuminate\Contracts\Validation\Rule;
- use Illuminate\Contracts\Validation\ValidatorAwareRule;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Carbon;
- use Illuminate\Translation\ArrayLoader;
- use Illuminate\Translation\Translator;
- use Illuminate\Validation\DatabasePresenceVerifierInterface;
- use Illuminate\Validation\Rules\Exists;
- use Illuminate\Validation\Rules\Unique;
- use Illuminate\Validation\ValidationData;
- use Illuminate\Validation\ValidationException;
- use Illuminate\Validation\Validator;
- use InvalidArgumentException;
- use Mockery as m;
- use PHPUnit\Framework\TestCase;
- use stdClass;
- use Symfony\Component\HttpFoundation\File\File;
- use Symfony\Component\HttpFoundation\File\UploadedFile;
- class ValidationValidatorTest extends TestCase
- {
- protected function tearDown(): void
- {
- Carbon::setTestNow();
- m::close();
- }
- public function testNestedErrorMessagesAreRetrievedFromLocalArray()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [
- 'users' => [
- [
- 'name' => 'Taylor Otwell',
- 'posts' => [
- [
- 'name' => '',
- ],
- ],
- ],
- ],
- ], [
- 'users.*.name' => ['required'],
- 'users.*.posts.*.name' => ['required'],
- ], [
- 'users.*.name.required' => 'user name is required',
- 'users.*.posts.*.name.required' => 'post name is required',
- ]);
- $this->assertFalse($v->passes());
- $this->assertSame('post name is required', $v->errors()->all()[0]);
- }
- public function testSometimesWorksOnNestedArrays()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => ['bar' => ['baz' => '']]], ['foo.bar.baz' => 'sometimes|required']);
- $this->assertFalse($v->passes());
- $this->assertEquals(['foo.bar.baz' => ['Required' => []]], $v->failed());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => ['bar' => ['baz' => 'nonEmpty']]], ['foo.bar.baz' => 'sometimes|required']);
- $this->assertTrue($v->passes());
- }
- public function testAfterCallbacksAreCalledWithValidatorInstance()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'boom'], ['foo' => 'Same:baz']);
- $v->setContainer(new Container);
- $v->after(function ($validator) {
- $_SERVER['__validator.after.test'] = true;
- // For asserting we can actually work with the instance
- $validator->errors()->add('bar', 'foo');
- });
- $this->assertFalse($v->passes());
- $this->assertTrue($_SERVER['__validator.after.test']);
- $this->assertTrue($v->errors()->has('bar'));
- unset($_SERVER['__validator.after.test']);
- }
- public function testSometimesWorksOnArrays()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => ['bar', 'baz', 'moo']], ['foo' => 'sometimes|required|between:5,10']);
- $this->assertFalse($v->passes());
- $this->assertNotEmpty($v->failed());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => ['bar', 'baz', 'moo', 'pew', 'boom']], ['foo' => 'sometimes|required|between:5,10']);
- $this->assertTrue($v->passes());
- }
- public function testValidateThrowsOnFail()
- {
- $this->expectException(ValidationException::class);
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'bar'], ['baz' => 'required']);
- $v->validate();
- }
- public function testValidateDoesntThrowOnPass()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'bar'], ['foo' => 'required']);
- $this->assertSame(['foo' => 'bar'], $v->validate());
- }
- public function testHasFailedValidationRules()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'boom'], ['foo' => 'Same:baz']);
- $this->assertFalse($v->passes());
- $this->assertEquals(['foo' => ['Same' => ['baz']]], $v->failed());
- }
- public function testFailingOnce()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'boom'], ['foo' => 'Bail|Same:baz|In:qux']);
- $this->assertFalse($v->passes());
- $this->assertEquals(['foo' => ['Same' => ['baz']]], $v->failed());
- }
- public function testHasNotFailedValidationRules()
- {
- $trans = $this->getTranslator();
- $trans->shouldReceive('get')->never();
- $v = new Validator($trans, ['foo' => 'taylor'], ['name' => 'Confirmed']);
- $this->assertTrue($v->passes());
- $this->assertEmpty($v->failed());
- }
- public function testSometimesCanSkipRequiredRules()
- {
- $trans = $this->getTranslator();
- $trans->shouldReceive('get')->never();
- $v = new Validator($trans, [], ['name' => 'sometimes|required']);
- $this->assertTrue($v->passes());
- $this->assertEmpty($v->failed());
- }
- public function testInValidatableRulesReturnsValid()
- {
- $trans = $this->getTranslator();
- $trans->shouldReceive('get')->never();
- $v = new Validator($trans, ['foo' => 'taylor'], ['name' => 'Confirmed']);
- $this->assertTrue($v->passes());
- }
- public function testValidateEmptyStringsAlwaysPasses()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => ''], ['x' => 'size:10|array|integer|min:5']);
- $this->assertTrue($v->passes());
- }
- public function testEmptyExistingAttributesAreValidated()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => ''], ['x' => 'array']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => []], ['x' => 'boolean']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => []], ['x' => 'numeric']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => []], ['x' => 'integer']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => []], ['x' => 'string']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, [], ['x' => 'string', 'y' => 'numeric', 'z' => 'integer', 'a' => 'boolean', 'b' => 'array']);
- $this->assertTrue($v->passes());
- }
- public function testNullable()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [
- 'x' => null, 'y' => null, 'z' => null, 'a' => null, 'b' => null,
- ], [
- 'x' => 'string|nullable', 'y' => 'integer|nullable', 'z' => 'numeric|nullable', 'a' => 'array|nullable', 'b' => 'bool|nullable',
- ]);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, [
- 'x' => null, 'y' => null, 'z' => null, 'a' => null, 'b' => null,
- ], [
- 'x' => 'string', 'y' => 'integer', 'z' => 'numeric', 'a' => 'array', 'b' => 'bool',
- ]);
- $this->assertTrue($v->fails());
- $this->assertSame('validation.string', $v->messages()->get('x')[0]);
- $this->assertSame('validation.integer', $v->messages()->get('y')[0]);
- $this->assertSame('validation.numeric', $v->messages()->get('z')[0]);
- $this->assertSame('validation.array', $v->messages()->get('a')[0]);
- $this->assertSame('validation.boolean', $v->messages()->get('b')[0]);
- }
- public function testNullableMakesNoDifferenceIfImplicitRuleExists()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [
- 'x' => null, 'y' => null,
- ], [
- 'x' => 'nullable|required_with:y|integer',
- 'y' => 'nullable|required_with:x|integer',
- ]);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, [
- 'x' => 'value', 'y' => null,
- ], [
- 'x' => 'nullable|required_with:y|integer',
- 'y' => 'nullable|required_with:x|integer',
- ]);
- $this->assertTrue($v->fails());
- $this->assertSame('validation.integer', $v->messages()->get('x')[0]);
- $v = new Validator($trans, [
- 'x' => 123, 'y' => null,
- ], [
- 'x' => 'nullable|required_with:y|integer',
- 'y' => 'nullable|required_with:x|integer',
- ]);
- $this->assertTrue($v->fails());
- $this->assertSame('validation.required_with', $v->messages()->get('y')[0]);
- }
- public function testProperLanguageLineIsSet()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required' => 'required!'], 'en');
- $v = new Validator($trans, ['name' => ''], ['name' => 'Required']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('required!', $v->messages()->first('name'));
- }
- public function testCustomReplacersAreCalled()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required' => 'foo bar'], 'en');
- $v = new Validator($trans, ['name' => ''], ['name' => 'Required']);
- $v->addReplacer('required', function ($message, $attribute, $rule, $parameters) {
- return str_replace('bar', 'taylor', $message);
- });
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('foo taylor', $v->messages()->first('name'));
- }
- public function testClassBasedCustomReplacers()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.foo' => 'foo!'], 'en');
- $v = new Validator($trans, [], ['name' => 'required']);
- $v->setContainer($container = m::mock(Container::class));
- $v->addReplacer('required', 'Foo@bar');
- $container->shouldReceive('make')->once()->with('Foo')->andReturn($foo = m::mock(stdClass::class));
- $foo->shouldReceive('bar')->once()->andReturn('replaced!');
- $v->passes();
- $v->messages()->setFormat(':message');
- $this->assertSame('replaced!', $v->messages()->first('name'));
- }
- public function testNestedAttributesAreReplacedInDimensions()
- {
- // Knowing that demo image.png has width = 3 and height = 2
- $uploadedFile = new UploadedFile(__DIR__.'/fixtures/image.png', '', null, null, true);
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.dimensions' => ':min_width :max_height :ratio'], 'en');
- $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_width=10,max_height=20,ratio=1']);
- $v->messages()->setFormat(':message');
- $this->assertTrue($v->fails());
- $this->assertSame('10 20 1', $v->messages()->first('x'));
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.dimensions' => ':width :height :ratio'], 'en');
- $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_width=10,max_height=20,ratio=1']);
- $v->messages()->setFormat(':message');
- $this->assertTrue($v->fails());
- $this->assertSame(':width :height 1', $v->messages()->first('x'));
- }
- public function testAttributeNamesAreReplaced()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required' => ':attribute is required!'], 'en');
- $v = new Validator($trans, ['name' => ''], ['name' => 'Required']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('name is required!', $v->messages()->first('name'));
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required' => ':attribute is required!', 'validation.attributes.name' => 'Name'], 'en');
- $v = new Validator($trans, ['name' => ''], ['name' => 'Required']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('Name is required!', $v->messages()->first('name'));
- // set customAttributes by setter
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required' => ':attribute is required!'], 'en');
- $customAttributes = ['name' => 'Name'];
- $v = new Validator($trans, ['name' => ''], ['name' => 'Required']);
- $v->addCustomAttributes($customAttributes);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('Name is required!', $v->messages()->first('name'));
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required' => ':attribute is required!'], 'en');
- $v = new Validator($trans, ['name' => ''], ['name' => 'Required']);
- $v->setAttributeNames(['name' => 'Name']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('Name is required!', $v->messages()->first('name'));
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required' => ':Attribute is required!'], 'en');
- $v = new Validator($trans, ['name' => ''], ['name' => 'Required']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('Name is required!', $v->messages()->first('name'));
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required' => ':ATTRIBUTE is required!'], 'en');
- $v = new Validator($trans, ['name' => ''], ['name' => 'Required']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('NAME is required!', $v->messages()->first('name'));
- }
- public function testAttributeNamesAreReplacedInArrays()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required' => ':attribute is required!'], 'en');
- $v = new Validator($trans, ['users' => [['country_code' => 'US'], ['country_code' => null]]], ['users.*.country_code' => 'Required']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('users.1.country_code is required!', $v->messages()->first('users.1.country_code'));
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines([
- 'validation.string' => ':attribute must be a string!',
- 'validation.attributes.name.*' => 'Any name',
- ], 'en');
- $v = new Validator($trans, ['name' => ['Jon', 2]], ['name.*' => 'string']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('Any name must be a string!', $v->messages()->first('name.1'));
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.string' => ':attribute must be a string!'], 'en');
- $v = new Validator($trans, ['name' => ['Jon', 2]], ['name.*' => 'string']);
- $v->setAttributeNames(['name.*' => 'Any name']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('Any name must be a string!', $v->messages()->first('name.1'));
- $v = new Validator($trans, ['users' => [['name' => 'Jon'], ['name' => 2]]], ['users.*.name' => 'string']);
- $v->setAttributeNames(['users.*.name' => 'Any name']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('Any name must be a string!', $v->messages()->first('users.1.name'));
- $trans->addLines(['validation.required' => ':attribute is required!'], 'en');
- $v = new Validator($trans, ['title' => ['nl' => '', 'en' => 'Hello']], ['title.*' => 'required'], [], ['title.nl' => 'Titel', 'title.en' => 'Title']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('Titel is required!', $v->messages()->first('title.nl'));
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required' => ':attribute is required!'], 'en');
- $trans->addLines(['validation.attributes' => ['names.*' => 'names']], 'en');
- $v = new Validator($trans, ['names' => [null, 'name']], ['names.*' => 'Required']);
- $v->messages()->setFormat(':message');
- $this->assertSame('names is required!', $v->messages()->first('names.0'));
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required' => ':attribute is required!'], 'en');
- $trans->addLines(['validation.attributes' => ['names.*' => 'names']], 'en');
- $trans->addLines(['validation.attributes' => ['names.0' => 'First name']], 'en');
- $v = new Validator($trans, ['names' => [null, 'name']], ['names.*' => 'Required']);
- $v->messages()->setFormat(':message');
- $this->assertSame('First name is required!', $v->messages()->first('names.0'));
- }
- public function testInputIsReplaced()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.email' => ':input is not a valid email'], 'en');
- $v = new Validator($trans, ['email' => 'a@@s'], ['email' => 'email']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('a@@s is not a valid email', $v->messages()->first('email'));
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.email' => ':input is not a valid email'], 'en');
- $v = new Validator($trans, ['email' => null], ['email' => 'email']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('empty is not a valid email', $v->messages()->first('email'));
- }
- public function testInputIsReplacedByItsDisplayableValue()
- {
- $frameworks = [
- 1 => 'Laravel',
- 2 => 'Symfony',
- 3 => 'Rails',
- ];
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.framework_php' => ':input is not a valid PHP Framework'], 'en');
- $v = new Validator($trans, ['framework' => 3], ['framework' => 'framework_php']);
- $v->addExtension('framework_php', function ($attribute, $value, $parameters, $validator) {
- return in_array($value, [1, 2]);
- });
- $v->addCustomValues(['framework' => $frameworks]);
- $this->assertFalse($v->passes());
- $this->assertSame('Rails is not a valid PHP Framework', $v->messages()->first('framework'));
- }
- public function testDisplayableValuesAreReplaced()
- {
- // required_if:foo,bar
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en');
- $trans->addLines(['validation.values.color.1' => 'red'], 'en');
- $v = new Validator($trans, ['color' => '1', 'bar' => ''], ['bar' => 'RequiredIf:color,1']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('The bar field is required when color is red.', $v->messages()->first('bar'));
- // required_if:foo,boolean
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en');
- $trans->addLines(['validation.values.subscribe.false' => 'false'], 'en');
- $v = new Validator($trans, ['subscribe' => false, 'bar' => ''], ['bar' => 'RequiredIf:subscribe,false']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('The bar field is required when subscribe is false.', $v->messages()->first('bar'));
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en');
- $trans->addLines(['validation.values.subscribe.true' => 'true'], 'en');
- $v = new Validator($trans, ['subscribe' => true, 'bar' => ''], ['bar' => 'RequiredIf:subscribe,true']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('The bar field is required when subscribe is true.', $v->messages()->first('bar'));
- // required_unless:foo,bar
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required_unless' => 'The :attribute field is required unless :other is in :values.'], 'en');
- $trans->addLines(['validation.values.color.1' => 'red'], 'en');
- $v = new Validator($trans, ['color' => '2', 'bar' => ''], ['bar' => 'RequiredUnless:color,1']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('The bar field is required unless color is in red.', $v->messages()->first('bar'));
- // in:foo,bar,...
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.in' => ':attribute must be included in :values.'], 'en');
- $trans->addLines(['validation.values.type.5' => 'Short'], 'en');
- $trans->addLines(['validation.values.type.300' => 'Long'], 'en');
- $v = new Validator($trans, ['type' => '4'], ['type' => 'in:5,300']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('type must be included in Short, Long.', $v->messages()->first('type'));
- // date_equals:tomorrow
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.date_equals' => 'The :attribute must be a date equal to :date.'], 'en');
- $trans->addLines(['validation.values.date.tomorrow' => 'the day after today'], 'en');
- $v = new Validator($trans, ['date' => date('Y-m-d')], ['date' => 'date_equals:tomorrow']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('The date must be a date equal to the day after today.', $v->messages()->first('date'));
- // test addCustomValues
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.in' => ':attribute must be included in :values.'], 'en');
- $customValues = [
- 'type' => [
- '5' => 'Short',
- '300' => 'Long',
- ],
- ];
- $v = new Validator($trans, ['type' => '4'], ['type' => 'in:5,300']);
- $v->addCustomValues($customValues);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('type must be included in Short, Long.', $v->messages()->first('type'));
- // set custom values by setter
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.in' => ':attribute must be included in :values.'], 'en');
- $customValues = [
- 'type' => [
- '5' => 'Short',
- '300' => 'Long',
- ],
- ];
- $v = new Validator($trans, ['type' => '4'], ['type' => 'in:5,300']);
- $v->setValueNames($customValues);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('type must be included in Short, Long.', $v->messages()->first('type'));
- }
- public function testDisplayableAttributesAreReplacedInCustomReplacers()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.alliteration' => ':attribute needs to begin with the same letter as :other'], 'en');
- $trans->addLines(['validation.attributes.firstname' => 'Firstname'], 'en');
- $trans->addLines(['validation.attributes.lastname' => 'Lastname'], 'en');
- $v = new Validator($trans, ['firstname' => 'Bob', 'lastname' => 'Smith'], ['lastname' => 'alliteration:firstname']);
- $v->addExtension('alliteration', function ($attribute, $value, $parameters, $validator) {
- $other = Arr::get($validator->getData(), $parameters[0]);
- return $value[0] == $other[0];
- });
- $v->addReplacer('alliteration', function ($message, $attribute, $rule, $parameters, $validator) {
- return str_replace(':other', $validator->getDisplayableAttribute($parameters[0]), $message);
- });
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('Lastname needs to begin with the same letter as Firstname', $v->messages()->first('lastname'));
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.alliteration' => ':attribute needs to begin with the same letter as :other'], 'en');
- $customAttributes = ['firstname' => 'Firstname', 'lastname' => 'Lastname'];
- $v = new Validator($trans, ['firstname' => 'Bob', 'lastname' => 'Smith'], ['lastname' => 'alliteration:firstname']);
- $v->addCustomAttributes($customAttributes);
- $v->addExtension('alliteration', function ($attribute, $value, $parameters, $validator) {
- $other = Arr::get($validator->getData(), $parameters[0]);
- return $value[0] == $other[0];
- });
- $v->addReplacer('alliteration', function ($message, $attribute, $rule, $parameters, $validator) {
- return str_replace(':other', $validator->getDisplayableAttribute($parameters[0]), $message);
- });
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('Lastname needs to begin with the same letter as Firstname', $v->messages()->first('lastname'));
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.alliteration' => ':attribute needs to begin with the same letter as :other'], 'en');
- new Validator($trans, ['firstname' => 'Bob', 'lastname' => 'Smith'], ['lastname' => 'alliteration:firstname']);
- }
- public function testCustomValidationLinesAreRespected()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->getLoader()->addMessages('en', 'validation', [
- 'required' => 'required!',
- 'custom' => [
- 'name' => [
- 'required' => 'really required!',
- ],
- ],
- ]);
- $v = new Validator($trans, ['name' => ''], ['name' => 'Required']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('really required!', $v->messages()->first('name'));
- }
- public function testCustomValidationLinesAreRespectedWithAsterisks()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->getLoader()->addMessages('en', 'validation', [
- 'required' => 'required!',
- 'custom' => [
- 'name.*' => [
- 'required' => 'all are really required!',
- ],
- 'lang.en' => [
- 'required' => 'english is required!',
- ],
- ],
- ]);
- $v = new Validator($trans, ['name' => ['', ''], 'lang' => ['en' => '']], [
- 'name.*' => 'required|max:255',
- 'lang.*' => 'required|max:255',
- ]);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('all are really required!', $v->messages()->first('name.0'));
- $this->assertSame('all are really required!', $v->messages()->first('name.1'));
- $this->assertSame('english is required!', $v->messages()->first('lang.en'));
- }
- public function testCustomException()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['name' => ''], ['name' => 'required']);
- $exception = new class($v) extends ValidationException {};
- $v->setException($exception);
- try {
- $v->validate();
- } catch (ValidationException $e) {
- $this->assertSame($exception, $e);
- }
- }
- public function testCustomExceptionMustExtendValidationException()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [], []);
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Exception [RuntimeException] is invalid. It must extend [Illuminate\Validation\ValidationException].');
- $v->setException(\RuntimeException::class);
- }
- public function testValidationDotCustomDotAnythingCanBeTranslated()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->getLoader()->addMessages('en', 'validation', [
- 'required' => 'required!',
- 'custom' => [
- 'validation' => [
- 'custom.*' => [
- 'integer' => 'should be integer!',
- ],
- ],
- ],
- ]);
- $v = new Validator($trans, ['validation' => ['custom' => ['string', 'string']]], ['validation.custom.*' => 'integer']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('should be integer!', $v->messages()->first('validation.custom.0'));
- $this->assertSame('should be integer!', $v->messages()->first('validation.custom.1'));
- }
- public function testInlineValidationMessagesAreRespected()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['name' => ''], ['name' => 'Required'], ['name.required' => 'require it please!']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('require it please!', $v->messages()->first('name'));
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['name' => ''], ['name' => 'Required'], ['required' => 'require it please!']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('require it please!', $v->messages()->first('name'));
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['name' => 'foobarba'], ['name' => 'size:9'], ['size' => ['string' => ':attribute should be of length :size']]);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('name should be of length 9', $v->messages()->first('name'));
- }
- public function testInlineValidationMessagesAreRespectedWithAsterisks()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['name' => ['', '']], ['name.*' => 'required|max:255'], ['name.*.required' => 'all must be required!']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('all must be required!', $v->messages()->first('name.0'));
- $this->assertSame('all must be required!', $v->messages()->first('name.1'));
- }
- public function testIfRulesAreSuccessfullyAdded()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [], ['foo' => 'Required']);
- // foo has required rule
- $this->assertTrue($v->hasRule('foo', 'Required'));
- // foo doesn't have array rule
- $this->assertFalse($v->hasRule('foo', 'Array'));
- // bar doesn't exists
- $this->assertFalse($v->hasRule('bar', 'Required'));
- }
- public function testValidateArray()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => [1, 2, 3]], ['foo' => 'Array']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => new File('/tmp/foo', false)], ['foo' => 'Array']);
- $this->assertFalse($v->passes());
- }
- public function testValidateArrayKeys()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $rules = ['user' => 'array:name,username'];
- $v = new Validator($trans, ['user' => ['name' => 'Duilio', 'username' => 'duilio']], $rules);
- $this->assertTrue($v->passes());
- // The array is valid if there's a missing key.
- $v = new Validator($trans, ['user' => ['name' => 'Duilio']], $rules);
- $this->assertTrue($v->passes());
- // But it's not valid if there's an unexpected key.
- $v = new Validator($trans, ['user' => ['name' => 'Duilio', 'username' => 'duilio', 'is_admin' => true]], $rules);
- $this->assertFalse($v->passes());
- }
- public function testValidateCurrentPassword()
- {
- // Fails when user is not logged in.
- $auth = m::mock(Guard::class);
- $auth->shouldReceive('guard')->andReturn($auth);
- $auth->shouldReceive('guest')->andReturn(true);
- $hasher = m::mock(Hasher::class);
- $container = m::mock(Container::class);
- $container->shouldReceive('make')->with('auth')->andReturn($auth);
- $container->shouldReceive('make')->with('hash')->andReturn($hasher);
- $trans = $this->getTranslator();
- $trans->shouldReceive('get')->andReturnArg(0);
- $v = new Validator($trans, ['password' => 'foo'], ['password' => 'current_password']);
- $v->setContainer($container);
- $this->assertFalse($v->passes());
- // Fails when password is incorrect.
- $user = m::mock(Authenticatable::class);
- $user->shouldReceive('getAuthPassword');
- $auth = m::mock(Guard::class);
- $auth->shouldReceive('guard')->andReturn($auth);
- $auth->shouldReceive('guest')->andReturn(false);
- $auth->shouldReceive('user')->andReturn($user);
- $hasher = m::mock(Hasher::class);
- $hasher->shouldReceive('check')->andReturn(false);
- $container = m::mock(Container::class);
- $container->shouldReceive('make')->with('auth')->andReturn($auth);
- $container->shouldReceive('make')->with('hash')->andReturn($hasher);
- $trans = $this->getTranslator();
- $trans->shouldReceive('get')->andReturnArg(0);
- $v = new Validator($trans, ['password' => 'foo'], ['password' => 'current_password']);
- $v->setContainer($container);
- $this->assertFalse($v->passes());
- // Succeeds when password is correct.
- $user = m::mock(Authenticatable::class);
- $user->shouldReceive('getAuthPassword');
- $auth = m::mock(Guard::class);
- $auth->shouldReceive('guard')->andReturn($auth);
- $auth->shouldReceive('guest')->andReturn(false);
- $auth->shouldReceive('user')->andReturn($user);
- $hasher = m::mock(Hasher::class);
- $hasher->shouldReceive('check')->andReturn(true);
- $container = m::mock(Container::class);
- $container->shouldReceive('make')->with('auth')->andReturn($auth);
- $container->shouldReceive('make')->with('hash')->andReturn($hasher);
- $trans = $this->getTranslator();
- $trans->shouldReceive('get')->andReturnArg(0);
- $v = new Validator($trans, ['password' => 'foo'], ['password' => 'current_password']);
- $v->setContainer($container);
- $this->assertTrue($v->passes());
- // We can use a specific guard.
- $user = m::mock(Authenticatable::class);
- $user->shouldReceive('getAuthPassword');
- $auth = m::mock(Guard::class);
- $auth->shouldReceive('guard')->with('custom')->andReturn($auth);
- $auth->shouldReceive('guest')->andReturn(false);
- $auth->shouldReceive('user')->andReturn($user);
- $hasher = m::mock(Hasher::class);
- $hasher->shouldReceive('check')->andReturn(true);
- $container = m::mock(Container::class);
- $container->shouldReceive('make')->with('auth')->andReturn($auth);
- $container->shouldReceive('make')->with('hash')->andReturn($hasher);
- $trans = $this->getTranslator();
- $trans->shouldReceive('get')->andReturnArg(0);
- $v = new Validator($trans, ['password' => 'foo'], ['password' => 'current_password:custom']);
- $v->setContainer($container);
- $this->assertTrue($v->passes());
- }
- public function testValidateFilled()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [], ['name' => 'filled']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['name' => ''], ['name' => 'filled']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => [['id' => 1], []]], ['foo.*.id' => 'filled']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => [['id' => '']]], ['foo.*.id' => 'filled']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => [['id' => null]]], ['foo.*.id' => 'filled']);
- $this->assertFalse($v->passes());
- }
- public function testValidationStopsAtFailedPresenceCheck()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['name' => null], ['name' => 'Required|string']);
- $v->passes();
- $this->assertEquals(['validation.required'], $v->errors()->get('name'));
- $v = new Validator($trans, ['name' => null, 'email' => 'email'], ['name' => 'required_with:email|string']);
- $v->passes();
- $this->assertEquals(['validation.required_with'], $v->errors()->get('name'));
- $v = new Validator($trans, ['name' => null, 'email' => ''], ['name' => 'required_with:email|string']);
- $v->passes();
- $this->assertEquals(['validation.string'], $v->errors()->get('name'));
- $v = new Validator($trans, [], ['name' => 'present|string']);
- $v->passes();
- $this->assertEquals(['validation.present'], $v->errors()->get('name'));
- }
- public function testValidatePassword()
- {
- // Fails when user is not logged in.
- $auth = m::mock(Guard::class);
- $auth->shouldReceive('guard')->andReturn($auth);
- $auth->shouldReceive('guest')->andReturn(true);
- $hasher = m::mock(Hasher::class);
- $container = m::mock(Container::class);
- $container->shouldReceive('make')->with('auth')->andReturn($auth);
- $container->shouldReceive('make')->with('hash')->andReturn($hasher);
- $trans = $this->getTranslator();
- $trans->shouldReceive('get')->andReturnArg(0);
- $v = new Validator($trans, ['password' => 'foo'], ['password' => 'password']);
- $v->setContainer($container);
- $this->assertFalse($v->passes());
- // Fails when password is incorrect.
- $user = m::mock(Authenticatable::class);
- $user->shouldReceive('getAuthPassword');
- $auth = m::mock(Guard::class);
- $auth->shouldReceive('guard')->andReturn($auth);
- $auth->shouldReceive('guest')->andReturn(false);
- $auth->shouldReceive('user')->andReturn($user);
- $hasher = m::mock(Hasher::class);
- $hasher->shouldReceive('check')->andReturn(false);
- $container = m::mock(Container::class);
- $container->shouldReceive('make')->with('auth')->andReturn($auth);
- $container->shouldReceive('make')->with('hash')->andReturn($hasher);
- $trans = $this->getTranslator();
- $trans->shouldReceive('get')->andReturnArg(0);
- $v = new Validator($trans, ['password' => 'foo'], ['password' => 'password']);
- $v->setContainer($container);
- $this->assertFalse($v->passes());
- // Succeeds when password is correct.
- $user = m::mock(Authenticatable::class);
- $user->shouldReceive('getAuthPassword');
- $auth = m::mock(Guard::class);
- $auth->shouldReceive('guard')->andReturn($auth);
- $auth->shouldReceive('guest')->andReturn(false);
- $auth->shouldReceive('user')->andReturn($user);
- $hasher = m::mock(Hasher::class);
- $hasher->shouldReceive('check')->andReturn(true);
- $container = m::mock(Container::class);
- $container->shouldReceive('make')->with('auth')->andReturn($auth);
- $container->shouldReceive('make')->with('hash')->andReturn($hasher);
- $trans = $this->getTranslator();
- $trans->shouldReceive('get')->andReturnArg(0);
- $v = new Validator($trans, ['password' => 'foo'], ['password' => 'password']);
- $v->setContainer($container);
- $this->assertTrue($v->passes());
- // We can use a specific guard.
- $user = m::mock(Authenticatable::class);
- $user->shouldReceive('getAuthPassword');
- $auth = m::mock(Guard::class);
- $auth->shouldReceive('guard')->with('custom')->andReturn($auth);
- $auth->shouldReceive('guest')->andReturn(false);
- $auth->shouldReceive('user')->andReturn($user);
- $hasher = m::mock(Hasher::class);
- $hasher->shouldReceive('check')->andReturn(true);
- $container = m::mock(Container::class);
- $container->shouldReceive('make')->with('auth')->andReturn($auth);
- $container->shouldReceive('make')->with('hash')->andReturn($hasher);
- $trans = $this->getTranslator();
- $trans->shouldReceive('get')->andReturnArg(0);
- $v = new Validator($trans, ['password' => 'foo'], ['password' => 'password:custom']);
- $v->setContainer($container);
- $this->assertTrue($v->passes());
- }
- public function testValidatePresent()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [], ['name' => 'present']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, [], ['name' => 'present|nullable']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['name' => null], ['name' => 'present|nullable']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['name' => ''], ['name' => 'present']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => [['id' => 1], ['name' => 'a']]], ['foo.*.id' => 'present']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => [['id' => 1], []]], ['foo.*.id' => 'present']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => [['id' => 1], ['id' => '']]], ['foo.*.id' => 'present']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => [['id' => 1], ['id' => null]]], ['foo.*.id' => 'present']);
- $this->assertTrue($v->passes());
- }
- public function testValidateRequired()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [], ['name' => 'Required']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['name' => ''], ['name' => 'Required']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['name' => 'foo'], ['name' => 'Required']);
- $this->assertTrue($v->passes());
- $file = new File('', false);
- $v = new Validator($trans, ['name' => $file], ['name' => 'Required']);
- $this->assertFalse($v->passes());
- $file = new File(__FILE__, false);
- $v = new Validator($trans, ['name' => $file], ['name' => 'Required']);
- $this->assertTrue($v->passes());
- $file = new File(__FILE__, false);
- $file2 = new File(__FILE__, false);
- $v = new Validator($trans, ['files' => [$file, $file2]], ['files.0' => 'Required', 'files.1' => 'Required']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['files' => [$file, $file2]], ['files' => 'Required']);
- $this->assertTrue($v->passes());
- }
- public function testValidateRequiredWith()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'Taylor'], ['last' => 'required_with:first']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['first' => 'Taylor', 'last' => ''], ['last' => 'required_with:first']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['first' => ''], ['last' => 'required_with:first']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, [], ['last' => 'required_with:first']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['first' => 'Taylor', 'last' => 'Otwell'], ['last' => 'required_with:first']);
- $this->assertTrue($v->passes());
- $file = new File('', false);
- $v = new Validator($trans, ['file' => $file, 'foo' => ''], ['foo' => 'required_with:file']);
- $this->assertTrue($v->passes());
- $file = new File(__FILE__, false);
- $foo = new File(__FILE__, false);
- $v = new Validator($trans, ['file' => $file, 'foo' => $foo], ['foo' => 'required_with:file']);
- $this->assertTrue($v->passes());
- $file = new File(__FILE__, false);
- $foo = new File('', false);
- $v = new Validator($trans, ['file' => $file, 'foo' => $foo], ['foo' => 'required_with:file']);
- $this->assertFalse($v->passes());
- }
- public function testRequiredWithAll()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'foo'], ['last' => 'required_with_all:first,foo']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['first' => 'foo'], ['last' => 'required_with_all:first']);
- $this->assertFalse($v->passes());
- }
- public function testValidateRequiredWithout()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'Taylor'], ['last' => 'required_without:first']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['first' => 'Taylor', 'last' => ''], ['last' => 'required_without:first']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['first' => ''], ['last' => 'required_without:first']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, [], ['last' => 'required_without:first']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['first' => 'Taylor', 'last' => 'Otwell'], ['last' => 'required_without:first']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['last' => 'Otwell'], ['last' => 'required_without:first']);
- $this->assertTrue($v->passes());
- $file = new File('', false);
- $v = new Validator($trans, ['file' => $file], ['foo' => 'required_without:file']);
- $this->assertFalse($v->passes());
- $foo = new File('', false);
- $v = new Validator($trans, ['foo' => $foo], ['foo' => 'required_without:file']);
- $this->assertFalse($v->passes());
- $foo = new File(__FILE__, false);
- $v = new Validator($trans, ['foo' => $foo], ['foo' => 'required_without:file']);
- $this->assertTrue($v->passes());
- $file = new File(__FILE__, false);
- $foo = new File(__FILE__, false);
- $v = new Validator($trans, ['file' => $file, 'foo' => $foo], ['foo' => 'required_without:file']);
- $this->assertTrue($v->passes());
- $file = new File(__FILE__, false);
- $foo = new File('', false);
- $v = new Validator($trans, ['file' => $file, 'foo' => $foo], ['foo' => 'required_without:file']);
- $this->assertTrue($v->passes());
- $file = new File('', false);
- $foo = new File(__FILE__, false);
- $v = new Validator($trans, ['file' => $file, 'foo' => $foo], ['foo' => 'required_without:file']);
- $this->assertTrue($v->passes());
- $file = new File('', false);
- $foo = new File('', false);
- $v = new Validator($trans, ['file' => $file, 'foo' => $foo], ['foo' => 'required_without:file']);
- $this->assertFalse($v->passes());
- }
- public function testRequiredWithoutMultiple()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $rules = [
- 'f1' => 'required_without:f2,f3',
- 'f2' => 'required_without:f1,f3',
- 'f3' => 'required_without:f1,f2',
- ];
- $v = new Validator($trans, [], $rules);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['f1' => 'foo'], $rules);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['f2' => 'foo'], $rules);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['f3' => 'foo'], $rules);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['f1' => 'foo', 'f2' => 'bar'], $rules);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['f1' => 'foo', 'f3' => 'bar'], $rules);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['f2' => 'foo', 'f3' => 'bar'], $rules);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['f1' => 'foo', 'f2' => 'bar', 'f3' => 'baz'], $rules);
- $this->assertTrue($v->passes());
- }
- public function testRequiredWithoutAll()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $rules = [
- 'f1' => 'required_without_all:f2,f3',
- 'f2' => 'required_without_all:f1,f3',
- 'f3' => 'required_without_all:f1,f2',
- ];
- $v = new Validator($trans, [], $rules);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['f1' => 'foo'], $rules);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['f2' => 'foo'], $rules);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['f3' => 'foo'], $rules);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['f1' => 'foo', 'f2' => 'bar'], $rules);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['f1' => 'foo', 'f3' => 'bar'], $rules);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['f2' => 'foo', 'f3' => 'bar'], $rules);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['f1' => 'foo', 'f2' => 'bar', 'f3' => 'baz'], $rules);
- $this->assertTrue($v->passes());
- }
- public function testRequiredIf()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'taylor'], ['last' => 'required_if:first,taylor']);
- $this->assertTrue($v->fails());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'taylor', 'last' => 'otwell'], ['last' => 'required_if:first,taylor']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'taylor', 'last' => 'otwell'], ['last' => 'required_if:first,taylor,dayle']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'dayle', 'last' => 'rees'], ['last' => 'required_if:first,taylor,dayle']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => true], ['bar' => 'required_if:foo,false']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => true], ['bar' => 'required_if:foo,null']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 0], ['bar' => 'required_if:foo,0']);
- $this->assertTrue($v->fails());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => '0'], ['bar' => 'required_if:foo,0']);
- $this->assertTrue($v->fails());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 1], ['bar' => 'required_if:foo,1']);
- $this->assertTrue($v->fails());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => '1'], ['bar' => 'required_if:foo,1']);
- $this->assertTrue($v->fails());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => true], ['bar' => 'required_if:foo,true']);
- $this->assertTrue($v->fails());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => false], ['bar' => 'required_if:foo,false']);
- $this->assertTrue($v->fails());
- // error message when passed multiple values (required_if:foo,bar,baz)
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en');
- $v = new Validator($trans, ['first' => 'dayle', 'last' => ''], ['last' => 'RequiredIf:first,taylor,dayle']);
- $this->assertFalse($v->passes());
- $this->assertSame('The last field is required when first is dayle.', $v->messages()->first('last'));
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en');
- $v = new Validator($trans, ['foo' => 0], [
- 'foo' => 'nullable|required|boolean',
- 'bar' => 'required_if:foo,true',
- 'baz' => 'required_if:foo,false',
- ]);
- $this->assertTrue($v->fails());
- $this->assertCount(1, $v->messages());
- $this->assertSame('The baz field is required when foo is 0.', $v->messages()->first('baz'));
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [], [
- 'foo' => 'nullable|boolean',
- 'baz' => 'nullable|required_if:foo,false',
- ]);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => null], [
- 'foo' => 'nullable|boolean',
- 'baz' => 'nullable|required_if:foo,false',
- ]);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [], [
- 'foo' => 'nullable|boolean',
- 'baz' => 'nullable|required_if:foo,null',
- ]);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en');
- $v = new Validator($trans, ['foo' => null], [
- 'foo' => 'nullable|boolean',
- 'baz' => 'nullable|required_if:foo,null',
- ]);
- $this->assertTrue($v->fails());
- $this->assertCount(1, $v->messages());
- $this->assertSame('The baz field is required when foo is empty.', $v->messages()->first('baz'));
- }
- public function testRequiredUnless()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'sven'], ['last' => 'required_unless:first,taylor']);
- $this->assertTrue($v->fails());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'taylor'], ['last' => 'required_unless:first,taylor']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'sven', 'last' => 'wittevrongel'], ['last' => 'required_unless:first,taylor']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'taylor'], ['last' => 'required_unless:first,taylor,sven']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'sven'], ['last' => 'required_unless:first,taylor,sven']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => false], ['bar' => 'required_unless:foo,false']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => false], ['bar' => 'required_unless:foo,true']);
- $this->assertTrue($v->fails());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['bar' => '1'], ['bar' => 'required_unless:foo,true']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [], ['bar' => 'required_unless:foo,true']);
- $this->assertTrue($v->fails());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [], ['bar' => 'required_unless:foo,null']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => true], ['bar' => 'required_unless:foo,null']);
- $this->assertTrue($v->fails());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => '0'], ['bar' => 'required_unless:foo,0']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 0], ['bar' => 'required_unless:foo,0']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => '1'], ['bar' => 'required_unless:foo,1']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 1], ['bar' => 'required_unless:foo,1']);
- $this->assertTrue($v->passes());
- // error message when passed multiple values (required_unless:foo,bar,baz)
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required_unless' => 'The :attribute field is required unless :other is in :values.'], 'en');
- $v = new Validator($trans, ['first' => 'dayle', 'last' => ''], ['last' => 'RequiredUnless:first,taylor,sven']);
- $this->assertFalse($v->passes());
- $this->assertSame('The last field is required unless first is in taylor, sven.', $v->messages()->first('last'));
- }
- public function testProhibited()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [], ['name' => 'prohibited']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['last' => 'bar'], ['name' => 'prohibited']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['name' => 'foo'], ['name' => 'prohibited']);
- $this->assertTrue($v->fails());
- $file = new File('', false);
- $v = new Validator($trans, ['name' => $file], ['name' => 'prohibited']);
- $this->assertTrue($v->fails());
- $file = new File(__FILE__, false);
- $v = new Validator($trans, ['name' => $file], ['name' => 'prohibited']);
- $this->assertTrue($v->fails());
- $file = new File(__FILE__, false);
- $file2 = new File(__FILE__, false);
- $v = new Validator($trans, ['files' => [$file, $file2]], ['files.0' => 'prohibited', 'files.1' => 'prohibited']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['files' => [$file, $file2]], ['files' => 'prohibited']);
- $this->assertTrue($v->fails());
- }
- public function testProhibitedIf()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'taylor', 'last' => 'otwell'], ['last' => 'prohibited_if:first,taylor']);
- $this->assertTrue($v->fails());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'taylor'], ['last' => 'prohibited_if:first,taylor']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'taylor', 'last' => 'otwell'], ['last' => 'prohibited_if:first,taylor,jess']);
- $this->assertTrue($v->fails());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'taylor'], ['last' => 'prohibited_if:first,taylor,jess']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => true, 'bar' => 'baz'], ['bar' => 'prohibited_if:foo,false']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => true, 'bar' => 'baz'], ['bar' => 'prohibited_if:foo,true']);
- $this->assertTrue($v->fails());
- // error message when passed multiple values (prohibited_if:foo,bar,baz)
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.prohibited_if' => 'The :attribute field is prohibited when :other is :value.'], 'en');
- $v = new Validator($trans, ['first' => 'jess', 'last' => 'archer'], ['last' => 'prohibited_if:first,taylor,jess']);
- $this->assertFalse($v->passes());
- $this->assertSame('The last field is prohibited when first is jess.', $v->messages()->first('last'));
- }
- public function testProhibitedUnless()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'jess', 'last' => 'archer'], ['last' => 'prohibited_unless:first,taylor']);
- $this->assertTrue($v->fails());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'taylor', 'last' => 'otwell'], ['last' => 'prohibited_unless:first,taylor']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'jess'], ['last' => 'prohibited_unless:first,taylor']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'taylor', 'last' => 'otwell'], ['last' => 'prohibited_unless:first,taylor,jess']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['first' => 'jess', 'last' => 'archer'], ['last' => 'prohibited_unless:first,taylor,jess']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => false, 'bar' => 'baz'], ['bar' => 'prohibited_unless:foo,false']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => false, 'bar' => 'baz'], ['bar' => 'prohibited_unless:foo,true']);
- $this->assertTrue($v->fails());
- // error message when passed multiple values (prohibited_unless:foo,bar,baz)
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.prohibited_unless' => 'The :attribute field is prohibited unless :other is in :values.'], 'en');
- $v = new Validator($trans, ['first' => 'tim', 'last' => 'macdonald'], ['last' => 'prohibitedUnless:first,taylor,jess']);
- $this->assertFalse($v->passes());
- $this->assertSame('The last field is prohibited unless first is in taylor, jess.', $v->messages()->first('last'));
- }
- public function testProhibits()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['email' => 'foo', 'emails' => ['foo']], ['email' => 'prohibits:emails']);
- $this->assertTrue($v->fails());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['email' => 'foo', 'emails' => []], ['email' => 'prohibits:emails']);
- $this->assertTrue($v->fails());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['email' => 'foo', 'emails' => ''], ['email' => 'prohibits:emails']);
- $this->assertTrue($v->fails());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['email' => 'foo', 'emails' => null], ['email' => 'prohibits:emails']);
- $this->assertTrue($v->fails());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['email' => 'foo', 'emails' => false], ['email' => 'prohibits:emails']);
- $this->assertTrue($v->fails());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['email' => 'foo', 'emails' => ['foo']], ['email' => 'prohibits:email_address,emails']);
- $this->assertTrue($v->fails());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['email' => 'foo'], ['email' => 'prohibits:emails']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['email' => 'foo', 'other' => 'foo'], ['email' => 'prohibits:email_address,emails']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.prohibits' => 'The :attribute field prohibits :other being present.'], 'en');
- $v = new Validator($trans, ['email' => 'foo', 'emails' => 'bar', 'email_address' => 'baz'], ['email' => 'prohibits:emails,email_address']);
- $this->assertFalse($v->passes());
- $this->assertSame('The email field prohibits emails / email address being present.', $v->messages()->first('email'));
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [
- 'foo' => [
- ['email' => 'foo', 'emails' => 'foo'],
- ['emails' => 'foo'],
- ],
- ], ['foo.*.email' => 'prohibits:foo.*.emails']);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.email'));
- $this->assertFalse($v->messages()->has('foo.1.email'));
- }
- public function testFailedFileUploads()
- {
- $trans = $this->getIlluminateArrayTranslator();
- // If file is not successfully uploaded validation should fail with a
- // 'uploaded' error message instead of the original rule.
- $file = m::mock(UploadedFile::class);
- $file->shouldReceive('isValid')->andReturn(false);
- $file->shouldNotReceive('getSize');
- $v = new Validator($trans, ['photo' => $file], ['photo' => 'Max:10']);
- $this->assertTrue($v->fails());
- $this->assertEquals(['validation.uploaded'], $v->errors()->get('photo'));
- // Even "required" will not run if the file failed to upload.
- $file = m::mock(UploadedFile::class);
- $file->shouldReceive('isValid')->once()->andReturn(false);
- $v = new Validator($trans, ['photo' => $file], ['photo' => 'required']);
- $this->assertTrue($v->fails());
- $this->assertEquals(['validation.uploaded'], $v->errors()->get('photo'));
- // It should only fail with that rule if a validation rule implies it's
- // a file. Otherwise it should fail with the regular rule.
- $file = m::mock(UploadedFile::class);
- $file->shouldReceive('isValid')->andReturn(false);
- $v = new Validator($trans, ['photo' => $file], ['photo' => 'string']);
- $this->assertTrue($v->fails());
- $this->assertEquals(['validation.string'], $v->errors()->get('photo'));
- // Validation shouldn't continue if a file failed to upload.
- $file = m::mock(UploadedFile::class);
- $file->shouldReceive('isValid')->once()->andReturn(false);
- $v = new Validator($trans, ['photo' => $file], ['photo' => 'file|mimes:pdf|min:10']);
- $this->assertTrue($v->fails());
- $this->assertEquals(['validation.uploaded'], $v->errors()->get('photo'));
- }
- public function testValidateInArray()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => [1, 2, 3], 'bar' => [1, 2]], ['foo.*' => 'in_array:bar.*']);
- $this->assertFalse($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => [1, 2], 'bar' => [1, 2, 3]], ['foo.*' => 'in_array:bar.*']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => [['bar_id' => 5], ['bar_id' => 2]], 'bar' => [['id' => 1, ['id' => 2]]]], ['foo.*.bar_id' => 'in_array:bar.*.id']);
- $this->assertFalse($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => [['bar_id' => 1], ['bar_id' => 2]], 'bar' => [['id' => 1, ['id' => 2]]]], ['foo.*.bar_id' => 'in_array:bar.*.id']);
- $this->assertTrue($v->passes());
- $trans->addLines(['validation.in_array' => 'The value of :attribute does not exist in :other.'], 'en');
- $v = new Validator($trans, ['foo' => [1, 2, 3], 'bar' => [1, 2]], ['foo.*' => 'in_array:bar.*']);
- $this->assertSame('The value of foo.2 does not exist in bar.*.', $v->messages()->first('foo.2'));
- }
- public function testValidateConfirmed()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['password' => 'foo'], ['password' => 'Confirmed']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['password' => 'foo', 'password_confirmation' => 'bar'], ['password' => 'Confirmed']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['password' => 'foo', 'password_confirmation' => 'foo'], ['password' => 'Confirmed']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['password' => '1e2', 'password_confirmation' => '100'], ['password' => 'Confirmed']);
- $this->assertFalse($v->passes());
- }
- public function testValidateSame()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'boom'], ['foo' => 'Same:baz']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'bar'], ['foo' => 'Same:baz']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'bar'], ['foo' => 'Same:baz']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '1e2', 'baz' => '100'], ['foo' => 'Same:baz']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => null, 'baz' => null], ['foo' => 'Same:baz']);
- $this->assertTrue($v->passes());
- }
- public function testValidateDifferent()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'boom'], ['foo' => 'Different:baz']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 'bar', 'baz' => null], ['foo' => 'Different:baz']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 'bar'], ['foo' => 'Different:baz']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'bar'], ['foo' => 'Different:baz']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => '1e2', 'baz' => '100'], ['foo' => 'Different:baz']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 'bar', 'fuu' => 'baa', 'baz' => 'boom'], ['foo' => 'Different:fuu,baz']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'boom'], ['foo' => 'Different:fuu,baz']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 'bar', 'fuu' => 'bar', 'baz' => 'boom'], ['foo' => 'Different:fuu,baz']);
- $this->assertFalse($v->passes());
- }
- public function testGreaterThan()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['lhs' => 15, 'rhs' => 10], ['lhs' => 'numeric|gt:rhs']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['lhs' => 15, 'rhs' => 'string'], ['lhs' => 'numeric|gt:rhs']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['lhs' => 15.0, 'rhs' => 10], ['lhs' => 'numeric|gt:rhs']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['lhs' => '15', 'rhs' => 10], ['lhs' => 'numeric|gt:rhs']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|gt:rhs']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['lhs' => 15.0], ['lhs' => 'numeric|gt:10']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['lhs' => 5, 10 => 1], ['lhs' => 'numeric|gt:10']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['lhs' => '15'], ['lhs' => 'numeric|gt:10']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['lhs' => 'longer string', 'rhs' => 'string'], ['lhs' => 'gt:rhs']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['lhs' => ['string'], 'rhs' => [1, 'string']], ['lhs' => 'gt:rhs']);
- $this->assertTrue($v->fails());
- $fileOne = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
- $fileOne->expects($this->any())->method('getSize')->willReturn(5472);
- $fileTwo = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
- $fileTwo->expects($this->any())->method('getSize')->willReturn(3151);
- $v = new Validator($trans, ['lhs' => $fileOne, 'rhs' => $fileTwo], ['lhs' => 'gt:rhs']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|gt:10']);
- $this->assertTrue($v->passes());
- }
- public function testLessThan()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['lhs' => 15, 'rhs' => 10], ['lhs' => 'numeric|lt:rhs']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['lhs' => 15, 'rhs' => 'string'], ['lhs' => 'numeric|lt:rhs']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['lhs' => 15.0, 'rhs' => 10], ['lhs' => 'numeric|lt:rhs']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['lhs' => '15', 'rhs' => 10], ['lhs' => 'numeric|lt:rhs']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|lt:rhs']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['lhs' => 15.0], ['lhs' => 'numeric|lt:10']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['lhs' => '15'], ['lhs' => 'numeric|lt:10']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['lhs' => 'longer string', 'rhs' => 'string'], ['lhs' => 'lt:rhs']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['lhs' => ['string'], 'rhs' => [1, 'string']], ['lhs' => 'lt:rhs']);
- $this->assertTrue($v->passes());
- $fileOne = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
- $fileOne->expects($this->any())->method('getSize')->willReturn(5472);
- $fileTwo = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
- $fileTwo->expects($this->any())->method('getSize')->willReturn(3151);
- $v = new Validator($trans, ['lhs' => $fileOne, 'rhs' => $fileTwo], ['lhs' => 'lt:rhs']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|lt:10']);
- $this->assertTrue($v->fails());
- }
- public function testGreaterThanOrEqual()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['lhs' => 15, 'rhs' => 15], ['lhs' => 'numeric|gte:rhs']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['lhs' => 15, 'rhs' => 'string'], ['lhs' => 'numeric|gte:rhs']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['lhs' => 15.0, 'rhs' => 15], ['lhs' => 'numeric|gte:rhs']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['lhs' => '15', 'rhs' => 15], ['lhs' => 'numeric|gte:rhs']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|gte:rhs']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['lhs' => 15.0], ['lhs' => 'numeric|gte:15']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['lhs' => '15'], ['lhs' => 'numeric|gte:15']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['lhs' => 'longer string', 'rhs' => 'string'], ['lhs' => 'gte:rhs']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['lhs' => ['string'], 'rhs' => [1, 'string']], ['lhs' => 'gte:rhs']);
- $this->assertTrue($v->fails());
- $fileOne = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
- $fileOne->expects($this->any())->method('getSize')->willReturn(5472);
- $fileTwo = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
- $fileTwo->expects($this->any())->method('getSize')->willReturn(5472);
- $v = new Validator($trans, ['lhs' => $fileOne, 'rhs' => $fileTwo], ['lhs' => 'gte:rhs']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|gte:15']);
- $this->assertTrue($v->passes());
- }
- public function testLessThanOrEqual()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['lhs' => 15, 'rhs' => 15], ['lhs' => 'numeric|lte:rhs']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['lhs' => 15, 'rhs' => 'string'], ['lhs' => 'numeric|lte:rhs']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['lhs' => 15.0, 'rhs' => 15], ['lhs' => 'numeric|lte:rhs']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['lhs' => '15', 'rhs' => 15], ['lhs' => 'numeric|lte:rhs']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|lte:rhs']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['lhs' => 15.0], ['lhs' => 'numeric|lte:10']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['lhs' => '15'], ['lhs' => 'numeric|lte:10']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['lhs' => 'longer string', 'rhs' => 'string'], ['lhs' => 'lte:rhs']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['lhs' => ['string'], 'rhs' => [1, 'string']], ['lhs' => 'lte:rhs']);
- $this->assertTrue($v->passes());
- $fileOne = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
- $fileOne->expects($this->any())->method('getSize')->willReturn(5472);
- $fileTwo = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
- $fileTwo->expects($this->any())->method('getSize')->willReturn(5472);
- $v = new Validator($trans, ['lhs' => $fileOne, 'rhs' => $fileTwo], ['lhs' => 'lte:rhs']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|lte:10']);
- $this->assertTrue($v->fails());
- }
- public function testValidateAccepted()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'no'], ['foo' => 'Accepted']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'off'], ['foo' => 'Accepted']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => null], ['foo' => 'Accepted']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, [], ['foo' => 'Accepted']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 0], ['foo' => 'Accepted']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => '0'], ['foo' => 'Accepted']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => false], ['foo' => 'Accepted']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'false'], ['foo' => 'Accepted']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'yes'], ['foo' => 'Accepted']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 'on'], ['foo' => 'Accepted']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '1'], ['foo' => 'Accepted']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 1], ['foo' => 'Accepted']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => true], ['foo' => 'Accepted']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 'true'], ['foo' => 'Accepted']);
- $this->assertTrue($v->passes());
- }
- public function testValidateAcceptedIf()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'no', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'off', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => null, 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 0, 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => '0', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => false, 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'false', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'yes', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 'on', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '1', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 1, 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => true, 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 'true', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
- $this->assertTrue($v->passes());
- // accepted_if:bar,aaa
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.accepted_if' => 'The :attribute field must be accepted when :other is :value.'], 'en');
- $v = new Validator($trans, ['foo' => 'no', 'bar' => 'aaa'], ['foo' => 'accepted_if:bar,aaa']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('The foo field must be accepted when bar is aaa.', $v->messages()->first('foo'));
- // accepted_if:bar,aaa,...
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.accepted_if' => 'The :attribute field must be accepted when :other is :value.'], 'en');
- $v = new Validator($trans, ['foo' => 'no', 'bar' => 'abc'], ['foo' => 'accepted_if:bar,aaa,bbb,abc']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('The foo field must be accepted when bar is abc.', $v->messages()->first('foo'));
- // accepted_if:bar,boolean
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.accepted_if' => 'The :attribute field must be accepted when :other is :value.'], 'en');
- $v = new Validator($trans, ['foo' => 'no', 'bar' => false], ['foo' => 'accepted_if:bar,false']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('The foo field must be accepted when bar is false.', $v->messages()->first('foo'));
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.accepted_if' => 'The :attribute field must be accepted when :other is :value.'], 'en');
- $v = new Validator($trans, ['foo' => 'no', 'bar' => true], ['foo' => 'accepted_if:bar,true']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('The foo field must be accepted when bar is true.', $v->messages()->first('foo'));
- }
- public function testValidateDeclined()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'yes'], ['foo' => 'Declined']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'on'], ['foo' => 'Declined']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => null], ['foo' => 'Declined']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, [], ['foo' => 'Declined']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 1], ['foo' => 'Declined']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => '1'], ['foo' => 'Declined']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => true], ['foo' => 'Declined']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'true'], ['foo' => 'Declined']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'no'], ['foo' => 'Declined']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 'off'], ['foo' => 'Declined']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '0'], ['foo' => 'Declined']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 0], ['foo' => 'Declined']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => false], ['foo' => 'Declined']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 'false'], ['foo' => 'Declined']);
- $this->assertTrue($v->passes());
- }
- public function testValidateDeclinedIf()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'yes', 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'on', 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => null, 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 1, 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => '1', 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => true, 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'true', 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'no', 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 'off', 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 0, 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '0', 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => false, 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 'false', 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
- $this->assertTrue($v->passes());
- // declined_if:bar,aaa
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.declined_if' => 'The :attribute field must be declined when :other is :value.'], 'en');
- $v = new Validator($trans, ['foo' => 'yes', 'bar' => 'aaa'], ['foo' => 'declined_if:bar,aaa']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('The foo field must be declined when bar is aaa.', $v->messages()->first('foo'));
- // declined_if:bar,aaa,...
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.declined_if' => 'The :attribute field must be declined when :other is :value.'], 'en');
- $v = new Validator($trans, ['foo' => 'yes', 'bar' => 'abc'], ['foo' => 'declined_if:bar,aaa,bbb,abc']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('The foo field must be declined when bar is abc.', $v->messages()->first('foo'));
- // declined_if:bar,boolean
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.declined_if' => 'The :attribute field must be declined when :other is :value.'], 'en');
- $v = new Validator($trans, ['foo' => 'yes', 'bar' => false], ['foo' => 'declined_if:bar,false']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('The foo field must be declined when bar is false.', $v->messages()->first('foo'));
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.declined_if' => 'The :attribute field must be declined when :other is :value.'], 'en');
- $v = new Validator($trans, ['foo' => 'yes', 'bar' => true], ['foo' => 'declined_if:bar,true']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('The foo field must be declined when bar is true.', $v->messages()->first('foo'));
- }
- public function testValidateEndsWith()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'hello world'], ['x' => 'ends_with:hello']);
- $this->assertFalse($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'hello world'], ['x' => 'ends_with:world']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'hello world'], ['x' => 'ends_with:world,hello']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.ends_with' => 'The :attribute must end with one of the following values :values'], 'en');
- $v = new Validator($trans, ['url' => 'laravel.com'], ['url' => 'ends_with:http']);
- $this->assertFalse($v->passes());
- $this->assertSame('The url must end with one of the following values http', $v->messages()->first('url'));
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.ends_with' => 'The :attribute must end with one of the following values :values'], 'en');
- $v = new Validator($trans, ['url' => 'laravel.com'], ['url' => 'ends_with:http,https']);
- $this->assertFalse($v->passes());
- $this->assertSame('The url must end with one of the following values http, https', $v->messages()->first('url'));
- }
- public function testValidateStartsWith()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'hello world'], ['x' => 'starts_with:hello']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'hello world'], ['x' => 'starts_with:world']);
- $this->assertFalse($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'hello world'], ['x' => 'starts_with:world,hello']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.starts_with' => 'The :attribute must start with one of the following values :values'], 'en');
- $v = new Validator($trans, ['url' => 'laravel.com'], ['url' => 'starts_with:http']);
- $this->assertFalse($v->passes());
- $this->assertSame('The url must start with one of the following values http', $v->messages()->first('url'));
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.starts_with' => 'The :attribute must start with one of the following values :values'], 'en');
- $v = new Validator($trans, ['url' => 'laravel.com'], ['url' => 'starts_with:http,https']);
- $this->assertFalse($v->passes());
- $this->assertSame('The url must start with one of the following values http, https', $v->messages()->first('url'));
- }
- public function testValidateString()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => 'string']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => ['blah' => 'test']], ['x' => 'string']);
- $this->assertFalse($v->passes());
- }
- public function testValidateJson()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'aslksd'], ['foo' => 'json']);
- $this->assertFalse($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => '[]'], ['foo' => 'json']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => '{"name":"John","age":"34"}'], ['foo' => 'json']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => ['array']], ['foo' => 'json']);
- $this->assertFalse($v->passes());
- }
- public function testValidateBoolean()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'no'], ['foo' => 'Boolean']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'yes'], ['foo' => 'Boolean']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'false'], ['foo' => 'Boolean']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'true'], ['foo' => 'Boolean']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, [], ['foo' => 'Boolean']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => false], ['foo' => 'Boolean']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => true], ['foo' => 'Boolean']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '1'], ['foo' => 'Boolean']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 1], ['foo' => 'Boolean']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '0'], ['foo' => 'Boolean']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 0], ['foo' => 'Boolean']);
- $this->assertTrue($v->passes());
- }
- public function testValidateBool()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'no'], ['foo' => 'Bool']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'yes'], ['foo' => 'Bool']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'false'], ['foo' => 'Bool']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'true'], ['foo' => 'Bool']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, [], ['foo' => 'Bool']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => false], ['foo' => 'Bool']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => true], ['foo' => 'Bool']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '1'], ['foo' => 'Bool']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 1], ['foo' => 'Bool']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '0'], ['foo' => 'Bool']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 0], ['foo' => 'Bool']);
- $this->assertTrue($v->passes());
- }
- public function testValidateNumeric()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'asdad'], ['foo' => 'Numeric']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => '1.23'], ['foo' => 'Numeric']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '-1'], ['foo' => 'Numeric']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '1'], ['foo' => 'Numeric']);
- $this->assertTrue($v->passes());
- }
- public function testValidateInteger()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'asdad'], ['foo' => 'Integer']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => '1.23'], ['foo' => 'Integer']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => '-1'], ['foo' => 'Integer']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '1'], ['foo' => 'Integer']);
- $this->assertTrue($v->passes());
- }
- public function testValidateInt()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'asdad'], ['foo' => 'Int']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => '1.23'], ['foo' => 'Int']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => '-1'], ['foo' => 'Int']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '1'], ['foo' => 'Int']);
- $this->assertTrue($v->passes());
- }
- public function testValidateDigits()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => '12345'], ['foo' => 'Digits:5']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '123'], ['foo' => 'Digits:200']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => '+2.37'], ['foo' => 'Digits:5']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['foo' => '2e7'], ['foo' => 'Digits:3']);
- $this->assertTrue($v->fails());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => '12345'], ['foo' => 'digits_between:1,6']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 'bar'], ['foo' => 'digits_between:1,10']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => '123'], ['foo' => 'digits_between:4,5']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => '+12.3'], ['foo' => 'digits_between:1,6']);
- $this->assertFalse($v->passes());
- }
- public function testValidateSize()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'asdad'], ['foo' => 'Size:3']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'anc'], ['foo' => 'Size:3']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '123'], ['foo' => 'Numeric|Size:3']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => '3'], ['foo' => 'Numeric|Size:3']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => [1, 2, 3]], ['foo' => 'Array|Size:3']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => [1, 2, 3]], ['foo' => 'Array|Size:4']);
- $this->assertFalse($v->passes());
- $file = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
- $file->expects($this->any())->method('getSize')->willReturn(3072);
- $v = new Validator($trans, ['photo' => $file], ['photo' => 'Size:3']);
- $this->assertTrue($v->passes());
- $file = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
- $file->expects($this->any())->method('getSize')->willReturn(4072);
- $v = new Validator($trans, ['photo' => $file], ['photo' => 'Size:3']);
- $this->assertFalse($v->passes());
- }
- public function testValidateBetween()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'asdad'], ['foo' => 'Between:3,4']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'anc'], ['foo' => 'Between:3,5']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 'ancf'], ['foo' => 'Between:3,5']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 'ancfs'], ['foo' => 'Between:3,5']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '123'], ['foo' => 'Numeric|Between:50,100']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => '3'], ['foo' => 'Numeric|Between:1,5']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => [1, 2, 3]], ['foo' => 'Array|Between:1,5']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => [1, 2, 3]], ['foo' => 'Array|Between:1,2']);
- $this->assertFalse($v->passes());
- $file = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
- $file->expects($this->any())->method('getSize')->willReturn(3072);
- $v = new Validator($trans, ['photo' => $file], ['photo' => 'Between:1,5']);
- $this->assertTrue($v->passes());
- $file = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
- $file->expects($this->any())->method('getSize')->willReturn(4072);
- $v = new Validator($trans, ['photo' => $file], ['photo' => 'Between:1,2']);
- $this->assertFalse($v->passes());
- }
- public function testValidateMin()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => '3'], ['foo' => 'Min:3']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'anc'], ['foo' => 'Min:3']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '2'], ['foo' => 'Numeric|Min:3']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => '5'], ['foo' => 'Numeric|Min:3']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => [1, 2, 3, 4]], ['foo' => 'Array|Min:3']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => [1, 2]], ['foo' => 'Array|Min:3']);
- $this->assertFalse($v->passes());
- $file = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
- $file->expects($this->any())->method('getSize')->willReturn(3072);
- $v = new Validator($trans, ['photo' => $file], ['photo' => 'Min:2']);
- $this->assertTrue($v->passes());
- $file = $this->getMockBuilder(File::class)->onlyMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
- $file->expects($this->any())->method('getSize')->willReturn(4072);
- $v = new Validator($trans, ['photo' => $file], ['photo' => 'Min:10']);
- $this->assertFalse($v->passes());
- }
- public function testValidateMax()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'aslksd'], ['foo' => 'Max:3']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'anc'], ['foo' => 'Max:3']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '211'], ['foo' => 'Numeric|Max:100']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => '22'], ['foo' => 'Numeric|Max:33']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => [1, 2, 3]], ['foo' => 'Array|Max:4']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => [1, 2, 3]], ['foo' => 'Array|Max:2']);
- $this->assertFalse($v->passes());
- $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['isValid', 'getSize'])->setConstructorArgs([__FILE__, basename(__FILE__)])->getMock();
- $file->method('isValid')->willReturn(true);
- $file->method('getSize')->willReturn(3072);
- $v = new Validator($trans, ['photo' => $file], ['photo' => 'Max:10']);
- $this->assertTrue($v->passes());
- $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['isValid', 'getSize'])->setConstructorArgs([__FILE__, basename(__FILE__)])->getMock();
- $file->method('isValid')->willReturn(true);
- $file->method('getSize')->willReturn(4072);
- $v = new Validator($trans, ['photo' => $file], ['photo' => 'Max:2']);
- $this->assertFalse($v->passes());
- $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['isValid'])->setConstructorArgs([__FILE__, basename(__FILE__)])->getMock();
- $file->expects($this->any())->method('isValid')->willReturn(false);
- $v = new Validator($trans, ['photo' => $file], ['photo' => 'Max:10']);
- $this->assertFalse($v->passes());
- }
- /**
- * @param mixed $input
- * @param mixed $allowed
- * @param bool $passes
- *
- * @dataProvider multipleOfDataProvider
- */
- public function testValidateMutlpleOf($input, $allowed, $passes)
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.multiple_of' => 'The :attribute must be a multiple of :value'], 'en');
- $v = new Validator($trans, ['foo' => $input], ['foo' => "multiple_of:{$allowed}"]);
- $this->assertSame($passes, $v->passes());
- if ($v->fails()) {
- $this->assertSame("The foo must be a multiple of {$allowed}", $v->messages()->first('foo'));
- } else {
- $this->assertSame('', $v->messages()->first('foo'));
- }
- }
- public function multipleOfDataProvider()
- {
- return [
- [0, 0, false], // zero (same)
- [0, 10, true], // zero + integer
- [10, 0, false],
- [0, 10.1, true], // zero + float
- [10.1, 0, false],
- [0, -10, true], // zero + -integer
- [-10, 0, false],
- [0, -10.1, true], // zero + -float
- [-10.1, 0, false],
- [10, 10, true], // integer (same)
- [10, 5, true], // integer + integer
- [10, 4, false],
- [20, 10, true],
- [5, 10, false],
- [10, -5, true], // integer + -integer
- [10, -4, false],
- [-20, 10, true],
- [-5, 10, false],
- [-10, -10, true], // -integer (same)
- [-10, -5, true], // -integer + -integer
- [-10, -4, false],
- [-20, -10, true],
- [-5, -10, false],
- [10, 10.0, true], // integer + float (same)
- [10, 5.0, true], // integer + float
- [10, 4.0, false],
- [20.0, 10, true],
- [5.0, 10, false],
- [10.0, -10.0, true], // integer + -float (same)
- [10, -5.0, true], // integer + -float
- [10, -4.0, false],
- [-20.0, 10, true],
- [-5.0, 10, false],
- [10.0, -10.0, true], // -integer + float (same)
- [-10, 5.0, true], // -integer + float
- [-10, 4.0, false],
- [20.0, -10, true],
- [5.0, -10, false],
- [10.5, 10.5, true], // float (same)
- [10.5, 0.5, true], // float + float
- [10.5, 0.3, true], // 10.5/.3 = 35, tricky for floating point division
- [31.5, 10.5, true],
- [31.6, 10.5, false],
- [10.5, -0.5, true], // float + -float
- [10.5, -0.3, true], // 10.5/.3 = 35, tricky for floating point division
- [-31.5, 10.5, true],
- [-31.6, 10.5, false],
- [-10.5, -10.5, true], // -float (same)
- [-10.5, -0.5, true], // -float + -float
- [-10.5, -0.3, true], // 10.5/.3 = 35, tricky for floating point division
- [-31.5, -10.5, true],
- [-31.6, -10.5, false],
- [2, .1, true], // fmod does this "wrong", it should be 0, but fmod(2, .1) = .1
- [.75, .05, true], // fmod does this "wrong", it should be 0, but fmod(.75, .05) = .05
- [.9, .3, true], // .9/.3 = 3, tricky for floating point division
- ['foo', 1, false], // invalid values
- [1, 'foo', false],
- ['foo', 'foo', false],
- [1, '', false],
- [1, null, false],
- ];
- }
- public function testProperMessagesAreReturnedForSizes()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.min.numeric' => 'numeric', 'validation.size.string' => 'string', 'validation.max.file' => 'file'], 'en');
- $v = new Validator($trans, ['name' => '3'], ['name' => 'Numeric|Min:5']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('numeric', $v->messages()->first('name'));
- $v = new Validator($trans, ['name' => 'asasdfadsfd'], ['name' => 'Size:2']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('string', $v->messages()->first('name'));
- $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
- $file->expects($this->any())->method('getSize')->willReturn(4072);
- $file->expects($this->any())->method('isValid')->willReturn(true);
- $v = new Validator($trans, ['photo' => $file], ['photo' => 'Max:3']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('file', $v->messages()->first('photo'));
- }
- public function testValidateGtPlaceHolderIsReplacedProperly()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines([
- 'validation.gt.numeric' => ':value',
- 'validation.gt.string' => ':value',
- 'validation.gt.file' => ':value',
- 'validation.gt.array' => ':value',
- ], 'en');
- $v = new Validator($trans, ['items' => '3'], ['items' => 'gt:4']);
- $this->assertFalse($v->passes());
- $this->assertEquals(4, $v->messages()->first('items'));
- $v = new Validator($trans, ['items' => 3, 'more' => 5], ['items' => 'numeric|gt:more']);
- $this->assertFalse($v->passes());
- $this->assertEquals(5, $v->messages()->first('items'));
- $v = new Validator($trans, ['items' => 'abc', 'more' => 'abcde'], ['items' => 'gt:more']);
- $this->assertFalse($v->passes());
- $this->assertEquals(5, $v->messages()->first('items'));
- $v = new Validator($trans, ['max' => 10], ['min' => 'numeric', 'max' => 'numeric|gt:min'], [], ['min' => 'minimum value', 'max' => 'maximum value']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('minimum value', $v->messages()->first('max'));
- $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
- $file->expects($this->any())->method('getSize')->willReturn(4072);
- $file->expects($this->any())->method('isValid')->willReturn(true);
- $biggerFile = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
- $biggerFile->expects($this->any())->method('getSize')->willReturn(5120);
- $biggerFile->expects($this->any())->method('isValid')->willReturn(true);
- $v = new Validator($trans, ['photo' => $file, 'bigger' => $biggerFile], ['photo' => 'file|gt:bigger']);
- $this->assertFalse($v->passes());
- $this->assertEquals(5, $v->messages()->first('photo'));
- $v = new Validator($trans, ['items' => [1, 2, 3], 'more' => [0, 1, 2, 3]], ['items' => 'gt:more']);
- $this->assertFalse($v->passes());
- $this->assertEquals(4, $v->messages()->first('items'));
- }
- public function testValidateLtPlaceHolderIsReplacedProperly()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines([
- 'validation.lt.numeric' => ':value',
- 'validation.lt.string' => ':value',
- 'validation.lt.file' => ':value',
- 'validation.lt.array' => ':value',
- ], 'en');
- $v = new Validator($trans, ['items' => '3'], ['items' => 'lt:2']);
- $this->assertFalse($v->passes());
- $this->assertEquals(2, $v->messages()->first('items'));
- $v = new Validator($trans, ['items' => 3, 'less' => 2], ['items' => 'numeric|lt:less']);
- $this->assertFalse($v->passes());
- $this->assertEquals(2, $v->messages()->first('items'));
- $v = new Validator($trans, ['items' => 'abc', 'less' => 'ab'], ['items' => 'lt:less']);
- $this->assertFalse($v->passes());
- $this->assertEquals(2, $v->messages()->first('items'));
- $v = new Validator($trans, ['min' => 1], ['min' => 'numeric|lt:max', 'max' => 'numeric'], [], ['min' => 'minimum value', 'max' => 'maximum value']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('maximum value', $v->messages()->first('min'));
- $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
- $file->expects($this->any())->method('getSize')->willReturn(4072);
- $file->expects($this->any())->method('isValid')->willReturn(true);
- $smallerFile = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
- $smallerFile->expects($this->any())->method('getSize')->willReturn(2048);
- $smallerFile->expects($this->any())->method('isValid')->willReturn(true);
- $v = new Validator($trans, ['photo' => $file, 'smaller' => $smallerFile], ['photo' => 'file|lt:smaller']);
- $this->assertFalse($v->passes());
- $this->assertEquals(2, $v->messages()->first('photo'));
- $v = new Validator($trans, ['items' => [1, 2, 3], 'less' => [0, 1]], ['items' => 'lt:less']);
- $this->assertFalse($v->passes());
- $this->assertEquals(2, $v->messages()->first('items'));
- }
- public function testValidateGtePlaceHolderIsReplacedProperly()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines([
- 'validation.gte.numeric' => ':value',
- 'validation.gte.string' => ':value',
- 'validation.gte.file' => ':value',
- 'validation.gte.array' => ':value',
- ], 'en');
- $v = new Validator($trans, ['items' => '3'], ['items' => 'gte:4']);
- $this->assertFalse($v->passes());
- $this->assertEquals(4, $v->messages()->first('items'));
- $v = new Validator($trans, ['items' => 3, 'more' => 5], ['items' => 'numeric|gte:more']);
- $this->assertFalse($v->passes());
- $this->assertEquals(5, $v->messages()->first('items'));
- $v = new Validator($trans, ['items' => 'abc', 'more' => 'abcde'], ['items' => 'gte:more']);
- $this->assertFalse($v->passes());
- $this->assertEquals(5, $v->messages()->first('items'));
- $v = new Validator($trans, ['max' => 10], ['min' => 'numeric', 'max' => 'numeric|gte:min'], [], ['min' => 'minimum value', 'max' => 'maximum value']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('minimum value', $v->messages()->first('max'));
- $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
- $file->expects($this->any())->method('getSize')->willReturn(4072);
- $file->expects($this->any())->method('isValid')->willReturn(true);
- $biggerFile = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
- $biggerFile->expects($this->any())->method('getSize')->willReturn(5120);
- $biggerFile->expects($this->any())->method('isValid')->willReturn(true);
- $v = new Validator($trans, ['photo' => $file, 'bigger' => $biggerFile], ['photo' => 'file|gte:bigger']);
- $this->assertFalse($v->passes());
- $this->assertEquals(5, $v->messages()->first('photo'));
- $v = new Validator($trans, ['items' => [1, 2, 3], 'more' => [0, 1, 2, 3]], ['items' => 'gte:more']);
- $this->assertFalse($v->passes());
- $this->assertEquals(4, $v->messages()->first('items'));
- }
- public function testValidateLtePlaceHolderIsReplacedProperly()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines([
- 'validation.lte.numeric' => ':value',
- 'validation.lte.string' => ':value',
- 'validation.lte.file' => ':value',
- 'validation.lte.array' => ':value',
- ], 'en');
- $v = new Validator($trans, ['items' => '3'], ['items' => 'lte:2']);
- $this->assertFalse($v->passes());
- $this->assertEquals(2, $v->messages()->first('items'));
- $v = new Validator($trans, ['items' => 3, 'less' => 2], ['items' => 'numeric|lte:less']);
- $this->assertFalse($v->passes());
- $this->assertEquals(2, $v->messages()->first('items'));
- $v = new Validator($trans, ['items' => 'abc', 'less' => 'ab'], ['items' => 'lte:less']);
- $this->assertFalse($v->passes());
- $this->assertEquals(2, $v->messages()->first('items'));
- $v = new Validator($trans, ['min' => 1], ['min' => 'numeric|lte:max', 'max' => 'numeric'], [], ['min' => 'minimum value', 'max' => 'maximum value']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('maximum value', $v->messages()->first('min'));
- $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
- $file->expects($this->any())->method('getSize')->willReturn(4072);
- $file->expects($this->any())->method('isValid')->willReturn(true);
- $smallerFile = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
- $smallerFile->expects($this->any())->method('getSize')->willReturn(2048);
- $smallerFile->expects($this->any())->method('isValid')->willReturn(true);
- $v = new Validator($trans, ['photo' => $file, 'smaller' => $smallerFile], ['photo' => 'file|lte:smaller']);
- $this->assertFalse($v->passes());
- $this->assertEquals(2, $v->messages()->first('photo'));
- $v = new Validator($trans, ['items' => [1, 2, 3], 'less' => [0, 1]], ['items' => 'lte:less']);
- $this->assertFalse($v->passes());
- $this->assertEquals(2, $v->messages()->first('items'));
- }
- public function testValidateIn()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['name' => 'foo'], ['name' => 'In:bar,baz']);
- $this->assertFalse($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['name' => 0], ['name' => 'In:bar,baz']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['name' => 'foo'], ['name' => 'In:foo,baz']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['name' => ['foo', 'bar']], ['name' => 'Array|In:foo,baz']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['name' => ['foo', 'qux']], ['name' => 'Array|In:foo,baz,qux']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['name' => ['foo,bar', 'qux']], ['name' => 'Array|In:"foo,bar",baz,qux']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['name' => 'f"o"o'], ['name' => 'In:"f""o""o",baz,qux']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['name' => "a,b\nc,d"], ['name' => "in:\"a,b\nc,d\""]);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['name' => ['foo', 'bar']], ['name' => 'Alpha|In:foo,bar']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['name' => ['foo', []]], ['name' => 'Array|In:foo,bar']);
- $this->assertFalse($v->passes());
- }
- public function testValidateNotIn()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['name' => 'foo'], ['name' => 'NotIn:bar,baz']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['name' => 'foo'], ['name' => 'NotIn:foo,baz']);
- $this->assertFalse($v->passes());
- }
- public function testValidateDistinct()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => ['foo', 'foo']], ['foo.*' => 'distinct']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => ['à', 'À']], ['foo.*' => 'distinct:ignore_case']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => ['f/oo', 'F/OO']], ['foo.*' => 'distinct:ignore_case']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => ['1', '1']], ['foo.*' => 'distinct:ignore_case']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => ['1', '11']], ['foo.*' => 'distinct:ignore_case']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => ['foo', 'bar']], ['foo.*' => 'distinct']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => ['bar' => ['id' => 1], 'baz' => ['id' => 1]]], ['foo.*.id' => 'distinct']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => ['bar' => ['id' => 'qux'], 'baz' => ['id' => 'QUX']]], ['foo.*.id' => 'distinct']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => ['bar' => ['id' => 'qux'], 'baz' => ['id' => 'QUX']]], ['foo.*.id' => 'distinct:ignore_case']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => ['bar' => ['id' => 1], 'baz' => ['id' => 2]]], ['foo.*.id' => 'distinct']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => ['bar' => ['id' => 2], 'baz' => ['id' => 425]]], ['foo.*.id' => 'distinct:ignore_case']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => [['id' => 1, 'nested' => ['id' => 1]]]], ['foo.*.id' => 'distinct']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => [['id' => 1], ['id' => 1]]], ['foo.*.id' => 'distinct']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => [['id' => 1], ['id' => 2]]], ['foo.*.id' => 'distinct']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['cat' => [['prod' => [['id' => 1]]], ['prod' => [['id' => 1]]]]], ['cat.*.prod.*.id' => 'distinct']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['cat' => [['prod' => [['id' => 1]]], ['prod' => [['id' => 2]]]]], ['cat.*.prod.*.id' => 'distinct']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['cat' => ['sub' => [['prod' => [['id' => 1]]], ['prod' => [['id' => 2]]]]]], ['cat.sub.*.prod.*.id' => 'distinct']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['cat' => ['sub' => [['prod' => [['id' => 2]]], ['prod' => [['id' => 2]]]]]], ['cat.sub.*.prod.*.id' => 'distinct']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => ['foo', 'foo'], 'bar' => ['bar', 'baz']], ['foo.*' => 'distinct', 'bar.*' => 'distinct']);
- $this->assertFalse($v->passes());
- $this->assertCount(2, $v->messages());
- $v = new Validator($trans, ['foo' => ['foo', 'foo'], 'bar' => ['bar', 'bar']], ['foo.*' => 'distinct', 'bar.*' => 'distinct']);
- $this->assertFalse($v->passes());
- $this->assertCount(4, $v->messages());
- $v->setData(['foo' => ['foo', 'bar'], 'bar' => ['foo', 'bar']]);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => ['foo', 'foo']], ['foo.*' => 'distinct'], ['foo.*.distinct' => 'There is a duplication!']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('There is a duplication!', $v->messages()->first('foo.0'));
- $this->assertSame('There is a duplication!', $v->messages()->first('foo.1'));
- $v = new Validator($trans, ['foo' => ['0100', '100']], ['foo.*' => 'distinct'], ['foo.*.distinct' => 'There is a duplication!']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('There is a duplication!', $v->messages()->first('foo.0'));
- $this->assertSame('There is a duplication!', $v->messages()->first('foo.1'));
- $v = new Validator($trans, ['foo' => ['0100', '100']], ['foo.*' => 'distinct:strict']);
- $this->assertTrue($v->passes());
- }
- public function testValidateDistinctForTopLevelArrays()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo', 'foo'], ['*' => 'distinct']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, [['foo' => 1], ['foo' => 1]], ['*' => 'array', '*.foo' => 'distinct']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, [['foo' => 'a'], ['foo' => 'A']], ['*' => 'array', '*.foo' => 'distinct:ignore_case']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, [['foo' => [['id' => 1]]], ['foo' => [['id' => 1]]]], ['*' => 'array', '*.foo' => 'array', '*.foo.*.id' => 'distinct']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo', 'foo'], ['*' => 'distinct'], ['*.distinct' => 'There is a duplication!']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('There is a duplication!', $v->messages()->first('0'));
- $this->assertSame('There is a duplication!', $v->messages()->first('1'));
- }
- public function testValidateUnique()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Unique:users']);
- $mock = m::mock(DatabasePresenceVerifierInterface::class);
- $mock->shouldReceive('setConnection')->once()->with(null);
- $mock->shouldReceive('getCount')->once()->with('users', 'email', 'foo', null, null, [])->andReturn(0);
- $v->setPresenceVerifier($mock);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Unique:connection.users']);
- $mock = m::mock(DatabasePresenceVerifierInterface::class);
- $mock->shouldReceive('setConnection')->once()->with('connection');
- $mock->shouldReceive('getCount')->once()->with('users', 'email', 'foo', null, null, [])->andReturn(0);
- $v->setPresenceVerifier($mock);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Unique:users,email_addr,1']);
- $mock = m::mock(DatabasePresenceVerifierInterface::class);
- $mock->shouldReceive('setConnection')->once()->with(null);
- $mock->shouldReceive('getCount')->once()->with('users', 'email_addr', 'foo', '1', 'id', [])->andReturn(1);
- $v->setPresenceVerifier($mock);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Unique:users,email_addr,1,id_col']);
- $mock = m::mock(DatabasePresenceVerifierInterface::class);
- $mock->shouldReceive('setConnection')->once()->with(null);
- $mock->shouldReceive('getCount')->once()->with('users', 'email_addr', 'foo', '1', 'id_col', [])->andReturn(2);
- $v->setPresenceVerifier($mock);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['users' => [['id' => 1, 'email' => 'foo']]], ['users.*.email' => 'Unique:users,email,[users.*.id]']);
- $mock = m::mock(DatabasePresenceVerifierInterface::class);
- $mock->shouldReceive('setConnection')->once()->with(null);
- $mock->shouldReceive('getCount')->once()->with('users', 'email', 'foo', '1', 'id', [])->andReturn(1);
- $v->setPresenceVerifier($mock);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Unique:users,email_addr,NULL,id_col,foo,bar']);
- $mock = m::mock(DatabasePresenceVerifierInterface::class);
- $mock->shouldReceive('setConnection')->once()->with(null);
- $mock->shouldReceive('getCount')->once()->withArgs(function () {
- return func_get_args() === ['users', 'email_addr', 'foo', null, 'id_col', ['foo' => 'bar']];
- })->andReturn(2);
- $v->setPresenceVerifier($mock);
- $this->assertFalse($v->passes());
- }
- public function testValidateUniqueAndExistsSendsCorrectFieldNameToDBWithArrays()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [['email' => 'foo', 'type' => 'bar']], [
- '*.email' => 'unique:users', '*.type' => 'exists:user_types',
- ]);
- $mock = m::mock(DatabasePresenceVerifierInterface::class);
- $mock->shouldReceive('setConnection')->twice()->with(null);
- $mock->shouldReceive('getCount')->with('users', 'email', 'foo', null, null, [])->andReturn(0);
- $mock->shouldReceive('getCount')->with('user_types', 'type', 'bar', null, null, [])->andReturn(1);
- $v->setPresenceVerifier($mock);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $closure = function () {
- //
- };
- $v = new Validator($trans, [['email' => 'foo', 'type' => 'bar']], [
- '*.email' => (new Unique('users'))->where($closure),
- '*.type' => (new Exists('user_types'))->where($closure),
- ]);
- $mock = m::mock(DatabasePresenceVerifierInterface::class);
- $mock->shouldReceive('setConnection')->twice()->with(null);
- $mock->shouldReceive('getCount')->with('users', 'email', 'foo', null, 'id', [$closure])->andReturn(0);
- $mock->shouldReceive('getCount')->with('user_types', 'type', 'bar', null, null, [$closure])->andReturn(1);
- $v->setPresenceVerifier($mock);
- $this->assertTrue($v->passes());
- }
- public function testValidationExists()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Exists:users']);
- $mock = m::mock(DatabasePresenceVerifierInterface::class);
- $mock->shouldReceive('setConnection')->once()->with(null);
- $mock->shouldReceive('getCount')->once()->with('users', 'email', 'foo', null, null, [])->andReturn(1);
- $v->setPresenceVerifier($mock);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Exists:users,email,account_id,1,name,taylor']);
- $mock = m::mock(DatabasePresenceVerifierInterface::class);
- $mock->shouldReceive('setConnection')->once()->with(null);
- $mock->shouldReceive('getCount')->once()->with('users', 'email', 'foo', null, null, ['account_id' => 1, 'name' => 'taylor'])->andReturn(1);
- $v->setPresenceVerifier($mock);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Exists:users,email_addr']);
- $mock = m::mock(DatabasePresenceVerifierInterface::class);
- $mock->shouldReceive('setConnection')->once()->with(null);
- $mock->shouldReceive('getCount')->once()->with('users', 'email_addr', 'foo', null, null, [])->andReturn(0);
- $v->setPresenceVerifier($mock);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['email' => ['foo']], ['email' => 'Exists:users,email_addr']);
- $mock = m::mock(DatabasePresenceVerifierInterface::class);
- $mock->shouldReceive('setConnection')->once()->with(null);
- $mock->shouldReceive('getMultiCount')->once()->with('users', 'email_addr', ['foo'], [])->andReturn(0);
- $v->setPresenceVerifier($mock);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Exists:connection.users']);
- $mock = m::mock(DatabasePresenceVerifierInterface::class);
- $mock->shouldReceive('setConnection')->once()->with('connection');
- $mock->shouldReceive('getCount')->once()->with('users', 'email', 'foo', null, null, [])->andReturn(1);
- $v->setPresenceVerifier($mock);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['email' => ['foo', 'foo']], ['email' => 'exists:users,email_addr']);
- $mock = m::mock(DatabasePresenceVerifierInterface::class);
- $mock->shouldReceive('setConnection')->once()->with(null);
- $mock->shouldReceive('getMultiCount')->once()->with('users', 'email_addr', ['foo', 'foo'], [])->andReturn(1);
- $v->setPresenceVerifier($mock);
- $this->assertTrue($v->passes());
- }
- public function testValidationExistsIsNotCalledUnnecessarily()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['id' => 'foo'], ['id' => 'Integer|Exists:users,id']);
- $mock = m::mock(DatabasePresenceVerifierInterface::class);
- $mock->shouldReceive('getCount')->never();
- $v->setPresenceVerifier($mock);
- $this->assertFalse($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['id' => '1'], ['id' => 'Integer|Exists:users,id']);
- $mock = m::mock(DatabasePresenceVerifierInterface::class);
- $mock->shouldReceive('setConnection')->once()->with(null);
- $mock->shouldReceive('getCount')->once()->with('users', 'id', '1', null, null, [])->andReturn(1);
- $v->setPresenceVerifier($mock);
- $this->assertTrue($v->passes());
- }
- public function testValidateIp()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['ip' => 'aslsdlks'], ['ip' => 'Ip']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['ip' => '127.0.0.1'], ['ip' => 'Ip']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['ip' => '127.0.0.1'], ['ip' => 'Ipv4']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['ip' => '::1'], ['ip' => 'Ipv6']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['ip' => '127.0.0.1'], ['ip' => 'Ipv6']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['ip' => '::1'], ['ip' => 'Ipv4']);
- $this->assertTrue($v->fails());
- }
- public function testValidateMacAddress()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['mac' => 'foo'], ['mac' => 'mac_address']);
- $this->assertFalse($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['mac' => '01-23-45-67-89-ab'], ['mac' => 'mac_address']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['mac' => '01-23-45-67-89-AB'], ['mac' => 'mac_address']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['mac' => '01-23-45-67-89-aB'], ['mac' => 'mac_address']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['mac' => '01:23:45:67:89:ab'], ['mac' => 'mac_address']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['mac' => '01:23:45:67:89:AB'], ['mac' => 'mac_address']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['mac' => '01:23:45:67:89:aB'], ['mac' => 'mac_address']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['mac' => '01:23:45-67:89:aB'], ['mac' => 'mac_address']);
- $this->assertFalse($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['mac' => 'xx:23:45:67:89:aB'], ['mac' => 'mac_address']);
- $this->assertFalse($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['mac' => '0123.4567.89ab'], ['mac' => 'mac_address']);
- $this->assertTrue($v->passes());
- }
- public function testValidateEmail()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => 'Email']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['x' => ['not a string']], ['x' => 'Email']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, [
- 'x' => new class
- {
- public function __toString()
- {
- return 'aslsdlks';
- }
- },
- ], ['x' => 'Email']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, [
- 'x' => new class
- {
- public function __toString()
- {
- return 'foo@gmail.com';
- }
- },
- ], ['x' => 'Email']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => 'foo@gmail.com'], ['x' => 'Email']);
- $this->assertTrue($v->passes());
- }
- public function testValidateEmailWithInternationalCharacters()
- {
- $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'foo@gmäil.com'], ['x' => 'email']);
- $this->assertTrue($v->passes());
- }
- public function testValidateEmailWithStrictCheck()
- {
- $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'foo@bar '], ['x' => 'email:strict']);
- $this->assertFalse($v->passes());
- }
- public function testValidateEmailWithFilterCheck()
- {
- $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'foo@bar'], ['x' => 'email:filter']);
- $this->assertFalse($v->passes());
- $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'example@example.com'], ['x' => 'email:filter']);
- $this->assertTrue($v->passes());
- // Unicode characters are not allowed
- $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'exämple@example.com'], ['x' => 'email:filter']);
- $this->assertFalse($v->passes());
- $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'exämple@exämple.com'], ['x' => 'email:filter']);
- $this->assertFalse($v->passes());
- }
- public function testValidateEmailWithFilterUnicodeCheck()
- {
- $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'foo@bar'], ['x' => 'email:filter_unicode']);
- $this->assertFalse($v->passes());
- $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'example@example.com'], ['x' => 'email:filter_unicode']);
- $this->assertTrue($v->passes());
- // Any unicode characters are allowed only in local-part
- $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'exämple@example.com'], ['x' => 'email:filter_unicode']);
- $this->assertTrue($v->passes());
- $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'exämple@exämple.com'], ['x' => 'email:filter_unicode']);
- $this->assertFalse($v->passes());
- }
- public function testValidateEmailWithCustomClassCheck()
- {
- $container = m::mock(Container::class);
- $container->shouldReceive('make')->with(NoRFCWarningsValidation::class)->andReturn(new NoRFCWarningsValidation);
- $v = new Validator($this->getIlluminateArrayTranslator(), ['x' => 'foo@bar '], ['x' => 'email:'.NoRFCWarningsValidation::class]);
- $v->setContainer($container);
- $this->assertFalse($v->passes());
- }
- /**
- * @dataProvider validUrls
- */
- public function testValidateUrlWithValidUrls($validUrl)
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => $validUrl], ['x' => 'Url']);
- $this->assertTrue($v->passes());
- }
- /**
- * @dataProvider invalidUrls
- */
- public function testValidateUrlWithInvalidUrls($invalidUrl)
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => $invalidUrl], ['x' => 'Url']);
- $this->assertFalse($v->passes());
- }
- public function validUrls()
- {
- return [
- ['aaa://fully.qualified.domain/path'],
- ['aaas://fully.qualified.domain/path'],
- ['about://fully.qualified.domain/path'],
- ['acap://fully.qualified.domain/path'],
- ['acct://fully.qualified.domain/path'],
- ['acr://fully.qualified.domain/path'],
- ['adiumxtra://fully.qualified.domain/path'],
- ['afp://fully.qualified.domain/path'],
- ['afs://fully.qualified.domain/path'],
- ['aim://fully.qualified.domain/path'],
- ['apt://fully.qualified.domain/path'],
- ['attachment://fully.qualified.domain/path'],
- ['aw://fully.qualified.domain/path'],
- ['barion://fully.qualified.domain/path'],
- ['beshare://fully.qualified.domain/path'],
- ['bitcoin://fully.qualified.domain/path'],
- ['blob://fully.qualified.domain/path'],
- ['bolo://fully.qualified.domain/path'],
- ['callto://fully.qualified.domain/path'],
- ['cap://fully.qualified.domain/path'],
- ['chrome://fully.qualified.domain/path'],
- ['chrome-extension://fully.qualified.domain/path'],
- ['cid://fully.qualified.domain/path'],
- ['coap://fully.qualified.domain/path'],
- ['coaps://fully.qualified.domain/path'],
- ['com-eventbrite-attendee://fully.qualified.domain/path'],
- ['content://fully.qualified.domain/path'],
- ['crid://fully.qualified.domain/path'],
- ['cvs://fully.qualified.domain/path'],
- ['data://fully.qualified.domain/path'],
- ['dav://fully.qualified.domain/path'],
- ['dict://fully.qualified.domain/path'],
- ['dlna-playcontainer://fully.qualified.domain/path'],
- ['dlna-playsingle://fully.qualified.domain/path'],
- ['dns://fully.qualified.domain/path'],
- ['dntp://fully.qualified.domain/path'],
- ['dtn://fully.qualified.domain/path'],
- ['dvb://fully.qualified.domain/path'],
- ['ed2k://fully.qualified.domain/path'],
- ['example://fully.qualified.domain/path'],
- ['facetime://fully.qualified.domain/path'],
- ['fax://fully.qualified.domain/path'],
- ['feed://fully.qualified.domain/path'],
- ['feedready://fully.qualified.domain/path'],
- ['file://fully.qualified.domain/path'],
- ['filesystem://fully.qualified.domain/path'],
- ['finger://fully.qualified.domain/path'],
- ['fish://fully.qualified.domain/path'],
- ['ftp://fully.qualified.domain/path'],
- ['geo://fully.qualified.domain/path'],
- ['gg://fully.qualified.domain/path'],
- ['git://fully.qualified.domain/path'],
- ['gizmoproject://fully.qualified.domain/path'],
- ['go://fully.qualified.domain/path'],
- ['gopher://fully.qualified.domain/path'],
- ['gtalk://fully.qualified.domain/path'],
- ['h323://fully.qualified.domain/path'],
- ['ham://fully.qualified.domain/path'],
- ['hcp://fully.qualified.domain/path'],
- ['http://fully.qualified.domain/path'],
- ['https://fully.qualified.domain/path'],
- ['iax://fully.qualified.domain/path'],
- ['icap://fully.qualified.domain/path'],
- ['icon://fully.qualified.domain/path'],
- ['im://fully.qualified.domain/path'],
- ['imap://fully.qualified.domain/path'],
- ['info://fully.qualified.domain/path'],
- ['iotdisco://fully.qualified.domain/path'],
- ['ipn://fully.qualified.domain/path'],
- ['ipp://fully.qualified.domain/path'],
- ['ipps://fully.qualified.domain/path'],
- ['irc://fully.qualified.domain/path'],
- ['irc6://fully.qualified.domain/path'],
- ['ircs://fully.qualified.domain/path'],
- ['iris://fully.qualified.domain/path'],
- ['iris.beep://fully.qualified.domain/path'],
- ['iris.lwz://fully.qualified.domain/path'],
- ['iris.xpc://fully.qualified.domain/path'],
- ['iris.xpcs://fully.qualified.domain/path'],
- ['itms://fully.qualified.domain/path'],
- ['jabber://fully.qualified.domain/path'],
- ['jar://fully.qualified.domain/path'],
- ['jms://fully.qualified.domain/path'],
- ['keyparc://fully.qualified.domain/path'],
- ['lastfm://fully.qualified.domain/path'],
- ['ldap://fully.qualified.domain/path'],
- ['ldaps://fully.qualified.domain/path'],
- ['magnet://fully.qualified.domain/path'],
- ['mailserver://fully.qualified.domain/path'],
- ['mailto://fully.qualified.domain/path'],
- ['maps://fully.qualified.domain/path'],
- ['market://fully.qualified.domain/path'],
- ['message://fully.qualified.domain/path'],
- ['mid://fully.qualified.domain/path'],
- ['mms://fully.qualified.domain/path'],
- ['modem://fully.qualified.domain/path'],
- ['ms-help://fully.qualified.domain/path'],
- ['ms-settings://fully.qualified.domain/path'],
- ['ms-settings-airplanemode://fully.qualified.domain/path'],
- ['ms-settings-bluetooth://fully.qualified.domain/path'],
- ['ms-settings-camera://fully.qualified.domain/path'],
- ['ms-settings-cellular://fully.qualified.domain/path'],
- ['ms-settings-cloudstorage://fully.qualified.domain/path'],
- ['ms-settings-emailandaccounts://fully.qualified.domain/path'],
- ['ms-settings-language://fully.qualified.domain/path'],
- ['ms-settings-location://fully.qualified.domain/path'],
- ['ms-settings-lock://fully.qualified.domain/path'],
- ['ms-settings-nfctransactions://fully.qualified.domain/path'],
- ['ms-settings-notifications://fully.qualified.domain/path'],
- ['ms-settings-power://fully.qualified.domain/path'],
- ['ms-settings-privacy://fully.qualified.domain/path'],
- ['ms-settings-proximity://fully.qualified.domain/path'],
- ['ms-settings-screenrotation://fully.qualified.domain/path'],
- ['ms-settings-wifi://fully.qualified.domain/path'],
- ['ms-settings-workplace://fully.qualified.domain/path'],
- ['msnim://fully.qualified.domain/path'],
- ['msrp://fully.qualified.domain/path'],
- ['msrps://fully.qualified.domain/path'],
- ['mtqp://fully.qualified.domain/path'],
- ['mumble://fully.qualified.domain/path'],
- ['mupdate://fully.qualified.domain/path'],
- ['mvn://fully.qualified.domain/path'],
- ['news://fully.qualified.domain/path'],
- ['nfs://fully.qualified.domain/path'],
- ['ni://fully.qualified.domain/path'],
- ['nih://fully.qualified.domain/path'],
- ['nntp://fully.qualified.domain/path'],
- ['notes://fully.qualified.domain/path'],
- ['oid://fully.qualified.domain/path'],
- ['opaquelocktoken://fully.qualified.domain/path'],
- ['pack://fully.qualified.domain/path'],
- ['palm://fully.qualified.domain/path'],
- ['paparazzi://fully.qualified.domain/path'],
- ['pkcs11://fully.qualified.domain/path'],
- ['platform://fully.qualified.domain/path'],
- ['pop://fully.qualified.domain/path'],
- ['pres://fully.qualified.domain/path'],
- ['prospero://fully.qualified.domain/path'],
- ['proxy://fully.qualified.domain/path'],
- ['psyc://fully.qualified.domain/path'],
- ['query://fully.qualified.domain/path'],
- ['redis://fully.qualified.domain/path'],
- ['rediss://fully.qualified.domain/path'],
- ['reload://fully.qualified.domain/path'],
- ['res://fully.qualified.domain/path'],
- ['resource://fully.qualified.domain/path'],
- ['rmi://fully.qualified.domain/path'],
- ['rsync://fully.qualified.domain/path'],
- ['rtmfp://fully.qualified.domain/path'],
- ['rtmp://fully.qualified.domain/path'],
- ['rtsp://fully.qualified.domain/path'],
- ['rtsps://fully.qualified.domain/path'],
- ['rtspu://fully.qualified.domain/path'],
- ['s3://fully.qualified.domain/path'],
- ['secondlife://fully.qualified.domain/path'],
- ['service://fully.qualified.domain/path'],
- ['session://fully.qualified.domain/path'],
- ['sftp://fully.qualified.domain/path'],
- ['sgn://fully.qualified.domain/path'],
- ['shttp://fully.qualified.domain/path'],
- ['sieve://fully.qualified.domain/path'],
- ['sip://fully.qualified.domain/path'],
- ['sips://fully.qualified.domain/path'],
- ['skype://fully.qualified.domain/path'],
- ['smb://fully.qualified.domain/path'],
- ['sms://fully.qualified.domain/path'],
- ['smtp://fully.qualified.domain/path'],
- ['snews://fully.qualified.domain/path'],
- ['snmp://fully.qualified.domain/path'],
- ['soap.beep://fully.qualified.domain/path'],
- ['soap.beeps://fully.qualified.domain/path'],
- ['soldat://fully.qualified.domain/path'],
- ['spotify://fully.qualified.domain/path'],
- ['ssh://fully.qualified.domain/path'],
- ['steam://fully.qualified.domain/path'],
- ['stun://fully.qualified.domain/path'],
- ['stuns://fully.qualified.domain/path'],
- ['submit://fully.qualified.domain/path'],
- ['svn://fully.qualified.domain/path'],
- ['tag://fully.qualified.domain/path'],
- ['teamspeak://fully.qualified.domain/path'],
- ['tel://fully.qualified.domain/path'],
- ['teliaeid://fully.qualified.domain/path'],
- ['telnet://fully.qualified.domain/path'],
- ['tftp://fully.qualified.domain/path'],
- ['things://fully.qualified.domain/path'],
- ['thismessage://fully.qualified.domain/path'],
- ['tip://fully.qualified.domain/path'],
- ['tn3270://fully.qualified.domain/path'],
- ['turn://fully.qualified.domain/path'],
- ['turns://fully.qualified.domain/path'],
- ['tv://fully.qualified.domain/path'],
- ['udp://fully.qualified.domain/path'],
- ['unreal://fully.qualified.domain/path'],
- ['urn://fully.qualified.domain/path'],
- ['ut2004://fully.qualified.domain/path'],
- ['vemmi://fully.qualified.domain/path'],
- ['ventrilo://fully.qualified.domain/path'],
- ['videotex://fully.qualified.domain/path'],
- ['view-source://fully.qualified.domain/path'],
- ['wais://fully.qualified.domain/path'],
- ['webcal://fully.qualified.domain/path'],
- ['ws://fully.qualified.domain/path'],
- ['wss://fully.qualified.domain/path'],
- ['wtai://fully.qualified.domain/path'],
- ['wyciwyg://fully.qualified.domain/path'],
- ['xcon://fully.qualified.domain/path'],
- ['xcon-userid://fully.qualified.domain/path'],
- ['xfire://fully.qualified.domain/path'],
- ['xmlrpc.beep://fully.qualified.domain/path'],
- ['xmlrpc.beeps://fully.qualified.domain/path'],
- ['xmpp://fully.qualified.domain/path'],
- ['xri://fully.qualified.domain/path'],
- ['ymsgr://fully.qualified.domain/path'],
- ['z39.50://fully.qualified.domain/path'],
- ['z39.50r://fully.qualified.domain/path'],
- ['z39.50s://fully.qualified.domain/path'],
- ['http://a.pl'],
- ['http://localhost/url.php'],
- ['http://local.dev'],
- ['http://google.com'],
- ['http://www.google.com'],
- ['http://goog_le.com'],
- ['https://google.com'],
- ['http://illuminate.dev'],
- ['http://localhost'],
- ['https://laravel.com/?'],
- ['http://президент.рф/'],
- ['http://스타벅스코리아.com'],
- ['http://xn--d1abbgf6aiiy.xn--p1ai/'],
- ['https://laravel.com?'],
- ['https://laravel.com?q=1'],
- ['https://laravel.com/?q=1'],
- ['https://laravel.com#'],
- ['https://laravel.com#fragment'],
- ['https://laravel.com/#fragment'],
- ['https://domain1'],
- ['https://domain12/'],
- ['https://domain12#fragment'],
- ['https://domain1/path'],
- ['https://domain.com/path/%2528failed%2526?param=1#fragment'],
- ];
- }
- public function invalidUrls()
- {
- return [
- ['aslsdlks'],
- ['google.com'],
- ['://google.com'],
- ['http ://google.com'],
- ['http:/google.com'],
- ['http://google.com::aa'],
- ['http://google.com:aa'],
- ['http://127.0.0.1:aa'],
- ['http://[::1'],
- ['foo://bar'],
- ['javascript://test%0Aalert(321)'],
- ];
- }
- public function testValidateActiveUrl()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => 'active_url']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['x' => ['fdsfs', 'fdsfds']], ['x' => 'active_url']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['x' => 'http://google.com'], ['x' => 'active_url']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => 'http://www.google.com'], ['x' => 'active_url']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => 'http://www.google.com/about'], ['x' => 'active_url']);
- $this->assertTrue($v->passes());
- }
- public function testValidateImage()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $uploadedFile = [__FILE__, '', null, null, true];
- $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
- $file->expects($this->any())->method('guessExtension')->willReturn('php');
- $file->expects($this->any())->method('getClientOriginalExtension')->willReturn('php');
- $v = new Validator($trans, ['x' => $file], ['x' => 'image']);
- $this->assertFalse($v->passes());
- $file2 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
- $file2->expects($this->any())->method('guessExtension')->willReturn('jpeg');
- $file2->expects($this->any())->method('getClientOriginalExtension')->willReturn('jpeg');
- $v = new Validator($trans, ['x' => $file2], ['x' => 'image']);
- $this->assertTrue($v->passes());
- $file2 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
- $file2->expects($this->any())->method('guessExtension')->willReturn('jpg');
- $file2->expects($this->any())->method('getClientOriginalExtension')->willReturn('jpg');
- $v = new Validator($trans, ['x' => $file2], ['x' => 'image']);
- $this->assertTrue($v->passes());
- $file2 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
- $file2->expects($this->any())->method('guessExtension')->willReturn('jpg');
- $file2->expects($this->any())->method('getClientOriginalExtension')->willReturn('jpg');
- $v = new Validator($trans, ['x' => $file2], ['x' => 'image']);
- $this->assertTrue($v->passes());
- $file3 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
- $file3->expects($this->any())->method('guessExtension')->willReturn('gif');
- $file3->expects($this->any())->method('getClientOriginalExtension')->willReturn('gif');
- $v = new Validator($trans, ['x' => $file3], ['x' => 'image']);
- $this->assertTrue($v->passes());
- $file4 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
- $file4->expects($this->any())->method('guessExtension')->willReturn('bmp');
- $file4->expects($this->any())->method('getClientOriginalExtension')->willReturn('bmp');
- $v = new Validator($trans, ['x' => $file4], ['x' => 'image']);
- $this->assertTrue($v->passes());
- $file5 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
- $file5->expects($this->any())->method('guessExtension')->willReturn('png');
- $file5->expects($this->any())->method('getClientOriginalExtension')->willReturn('png');
- $v = new Validator($trans, ['x' => $file5], ['x' => 'image']);
- $this->assertTrue($v->passes());
- $file6 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
- $file6->expects($this->any())->method('guessExtension')->willReturn('svg');
- $file6->expects($this->any())->method('getClientOriginalExtension')->willReturn('svg');
- $v = new Validator($trans, ['x' => $file6], ['x' => 'image']);
- $this->assertTrue($v->passes());
- $file7 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
- $file7->expects($this->any())->method('guessExtension')->willReturn('webp');
- $file7->expects($this->any())->method('getClientOriginalExtension')->willReturn('webp');
- $v = new Validator($trans, ['x' => $file7], ['x' => 'Image']);
- $this->assertTrue($v->passes());
- $file2 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
- $file2->expects($this->any())->method('guessExtension')->willReturn('jpg');
- $file2->expects($this->any())->method('getClientOriginalExtension')->willReturn('jpg');
- $v = new Validator($trans, ['x' => $file2], ['x' => 'Image']);
- $this->assertTrue($v->passes());
- }
- public function testValidateImageDoesNotAllowPhpExtensionsOnImageMime()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $uploadedFile = [__FILE__, '', null, null, true];
- $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
- $file->expects($this->any())->method('guessExtension')->willReturn('jpeg');
- $file->expects($this->any())->method('getClientOriginalExtension')->willReturn('php');
- $v = new Validator($trans, ['x' => $file], ['x' => 'image']);
- $this->assertFalse($v->passes());
- }
- public function testValidateImageDimensions()
- {
- // Knowing that demo image.png has width = 3 and height = 2
- $uploadedFile = new UploadedFile(__DIR__.'/fixtures/image.png', '', null, null, true);
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'file'], ['x' => 'dimensions']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_width=1']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_width=5']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:max_width=10']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:max_width=1']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_height=1']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_height=5']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:max_height=10']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:max_height=1']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:width=3']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:height=2']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_height=2,ratio=3/2']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=1.5']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=1/1']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=1']);
- $this->assertTrue($v->fails());
- // Knowing that demo image2.png has width = 4 and height = 2
- $uploadedFile = new UploadedFile(__DIR__.'/fixtures/image2.png', '', null, null, true);
- $trans = $this->getIlluminateArrayTranslator();
- // Ensure validation doesn't erroneously fail when ratio has no fractional part
- $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=2/1']);
- $this->assertTrue($v->passes());
- // This test fails without suppressing warnings on getimagesize() due to a read error.
- $emptyUploadedFile = new UploadedFile(__DIR__.'/fixtures/empty.png', '', null, null, true);
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => $emptyUploadedFile], ['x' => 'dimensions:min_width=1']);
- $this->assertTrue($v->fails());
- // Knowing that demo image3.png has width = 7 and height = 10
- $uploadedFile = new UploadedFile(__DIR__.'/fixtures/image3.png', '', null, null, true);
- $trans = $this->getIlluminateArrayTranslator();
- // Ensure validation doesn't erroneously fail when ratio has no fractional part
- $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=2/3']);
- $this->assertTrue($v->passes());
- // Ensure svg images always pass as size is irrelevant (image/svg+xml)
- $svgXmlUploadedFile = new UploadedFile(__DIR__.'/fixtures/image.svg', '', 'image/svg+xml', null, true);
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => $svgXmlUploadedFile], ['x' => 'dimensions:max_width=1,max_height=1']);
- $this->assertTrue($v->passes());
- $svgXmlFile = new File(__DIR__.'/fixtures/image.svg', '', 'image/svg+xml', null, true);
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => $svgXmlFile], ['x' => 'dimensions:max_width=1,max_height=1']);
- $this->assertTrue($v->passes());
- // Ensure svg images always pass as size is irrelevant (image/svg)
- $svgUploadedFile = new UploadedFile(__DIR__.'/fixtures/image2.svg', '', 'image/svg', null, true);
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => $svgUploadedFile], ['x' => 'dimensions:max_width=1,max_height=1']);
- $this->assertTrue($v->passes());
- $svgFile = new File(__DIR__.'/fixtures/image2.svg', '', 'image/svg', null, true);
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => $svgFile], ['x' => 'dimensions:max_width=1,max_height=1']);
- $this->assertTrue($v->passes());
- // Knowing that demo image4.png has width = 64 and height = 65
- $uploadedFile = new UploadedFile(__DIR__.'/fixtures/image4.png', '', null, null, true);
- $trans = $this->getIlluminateArrayTranslator();
- // Ensure validation doesn't erroneously fail when ratio doesn't matches
- $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=1']);
- $this->assertFalse($v->passes());
- }
- public function testValidateMimetypes()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $uploadedFile = [__DIR__.'/ValidationRuleTest.php', '', null, null, true];
- $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
- $file->expects($this->any())->method('guessExtension')->willReturn('rtf');
- $file->expects($this->any())->method('getClientOriginalExtension')->willReturn('rtf');
- $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getMimeType'])->setConstructorArgs($uploadedFile)->getMock();
- $file->expects($this->any())->method('getMimeType')->willReturn('text/rtf');
- $v = new Validator($trans, ['x' => $file], ['x' => 'mimetypes:text/*']);
- $this->assertTrue($v->passes());
- $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getMimeType'])->setConstructorArgs($uploadedFile)->getMock();
- $file->expects($this->any())->method('getMimeType')->willReturn('application/pdf');
- $v = new Validator($trans, ['x' => $file], ['x' => 'mimetypes:text/rtf']);
- $this->assertFalse($v->passes());
- $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getMimeType'])->setConstructorArgs($uploadedFile)->getMock();
- $file->expects($this->any())->method('getMimeType')->willReturn('image/jpeg');
- $v = new Validator($trans, ['x' => $file], ['x' => 'mimetypes:image/jpeg']);
- $this->assertTrue($v->passes());
- }
- public function testValidateMime()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $uploadedFile = [__FILE__, '', null, null, true];
- $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
- $file->expects($this->any())->method('guessExtension')->willReturn('pdf');
- $file->expects($this->any())->method('getClientOriginalExtension')->willReturn('pdf');
- $v = new Validator($trans, ['x' => $file], ['x' => 'mimes:pdf']);
- $this->assertTrue($v->passes());
- $file2 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'isValid'])->setConstructorArgs($uploadedFile)->getMock();
- $file2->expects($this->any())->method('guessExtension')->willReturn('pdf');
- $file2->expects($this->any())->method('isValid')->willReturn(false);
- $v = new Validator($trans, ['x' => $file2], ['x' => 'mimes:pdf']);
- $this->assertFalse($v->passes());
- $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
- $file->expects($this->any())->method('guessExtension')->willReturn('jpg');
- $file->expects($this->any())->method('getClientOriginalExtension')->willReturn('jpg');
- $v = new Validator($trans, ['x' => $file], ['x' => 'mimes:jpeg']);
- $this->assertTrue($v->passes());
- $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
- $file->expects($this->any())->method('guessExtension')->willReturn('jpg');
- $file->expects($this->any())->method('getClientOriginalExtension')->willReturn('jpeg');
- $v = new Validator($trans, ['x' => $file], ['x' => 'mimes:jpg']);
- $this->assertTrue($v->passes());
- }
- public function testValidateMimeEnforcesPhpCheck()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $uploadedFile = [__FILE__, '', null, null, true];
- $file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
- $file->expects($this->any())->method('guessExtension')->willReturn('pdf');
- $file->expects($this->any())->method('getClientOriginalExtension')->willReturn('php');
- $v = new Validator($trans, ['x' => $file], ['x' => 'mimes:pdf']);
- $this->assertFalse($v->passes());
- $file2 = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['guessExtension', 'getClientOriginalExtension'])->setConstructorArgs($uploadedFile)->getMock();
- $file2->expects($this->any())->method('guessExtension')->willReturn('php');
- $file2->expects($this->any())->method('getClientOriginalExtension')->willReturn('php');
- $v = new Validator($trans, ['x' => $file2], ['x' => 'mimes:pdf,php']);
- $this->assertTrue($v->passes());
- }
- /**
- * @requires extension fileinfo
- */
- public function testValidateFile()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $file = new UploadedFile(__FILE__, '', null, null, true);
- $v = new Validator($trans, ['x' => '1'], ['x' => 'file']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => $file], ['x' => 'file']);
- $this->assertTrue($v->passes());
- }
- public function testEmptyRulesSkipped()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => ['alpha', [], '']]);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => '|||required|']);
- $this->assertTrue($v->passes());
- }
- public function testAlternativeFormat()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => ['alpha', ['min', 3], ['max', 10]]]);
- $this->assertTrue($v->passes());
- }
- public function testValidateAlpha()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => 'Alpha']);
- $this->assertTrue($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [
- 'x' => 'aslsdlks
- 1
- 1',
- ], ['x' => 'Alpha']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['x' => 'http://google.com'], ['x' => 'Alpha']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['x' => 'ユニコードを基盤技術と'], ['x' => 'Alpha']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => 'ユニコード を基盤技術と'], ['x' => 'Alpha']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['x' => 'नमस्कार'], ['x' => 'Alpha']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => 'आपका स्वागत है'], ['x' => 'Alpha']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['x' => 'Continuación'], ['x' => 'Alpha']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => 'ofreció su dimisión'], ['x' => 'Alpha']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['x' => '❤'], ['x' => 'Alpha']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['x' => '123'], ['x' => 'Alpha']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['x' => 123], ['x' => 'Alpha']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['x' => 'abc123'], ['x' => 'Alpha']);
- $this->assertFalse($v->passes());
- }
- public function testValidateAlphaNum()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'asls13dlks'], ['x' => 'AlphaNum']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => 'http://g232oogle.com'], ['x' => 'AlphaNum']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['x' => '१२३'], ['x' => 'AlphaNum']); // numbers in Hindi
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '٧٨٩'], ['x' => 'AlphaNum']); // eastern arabic numerals
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => 'नमस्कार'], ['x' => 'AlphaNum']);
- $this->assertTrue($v->passes());
- }
- public function testValidateAlphaDash()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'asls1-_3dlks'], ['x' => 'AlphaDash']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => 'http://-g232oogle.com'], ['x' => 'AlphaDash']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['x' => 'नमस्कार-_'], ['x' => 'AlphaDash']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '٧٨٩'], ['x' => 'AlphaDash']); // eastern arabic numerals
- $this->assertTrue($v->passes());
- }
- public function testValidateTimezone()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => 'India'], ['foo' => 'Timezone']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'Cairo'], ['foo' => 'Timezone']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'UTC'], ['foo' => 'Timezone']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 'Africa/Windhoek'], ['foo' => 'Timezone']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => 'africa/windhoek'], ['foo' => 'Timezone']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'GMT'], ['foo' => 'Timezone']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => 'GB'], ['foo' => 'Timezone']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['foo' => ['this_is_not_a_timezone']], ['foo' => 'Timezone']);
- $this->assertFalse($v->passes());
- }
- public function testValidateRegex()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'asdasdf'], ['x' => 'Regex:/^[a-z]+$/i']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => 'aasd234fsd1'], ['x' => 'Regex:/^[a-z]+$/i']);
- $this->assertFalse($v->passes());
- // Ensure commas are not interpreted as parameter separators
- $v = new Validator($trans, ['x' => 'a,b'], ['x' => 'Regex:/^a,b$/i']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '12'], ['x' => 'Regex:/^12$/i']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => 12], ['x' => 'Regex:/^12$/i']);
- $this->assertTrue($v->passes());
- }
- public function testValidateNotRegex()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'foo bar'], ['x' => 'NotRegex:/[xyz]/i']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => 'foo xxx bar'], ['x' => 'NotRegex:/[xyz]/i']);
- $this->assertFalse($v->passes());
- // Ensure commas are not interpreted as parameter separators
- $v = new Validator($trans, ['x' => 'foo bar'], ['x' => 'NotRegex:/x{3,}/i']);
- $this->assertTrue($v->passes());
- }
- public function testValidateDateAndFormat()
- {
- date_default_timezone_set('UTC');
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => '2000-01-01'], ['x' => 'date']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '01/01/2000'], ['x' => 'date']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '1325376000'], ['x' => 'date']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => 'Not a date'], ['x' => 'date']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => ['Not', 'a', 'date']], ['x' => 'date']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => new DateTime], ['x' => 'date']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => new DateTimeImmutable], ['x' => 'date']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '2000-01-01'], ['x' => 'date_format:Y-m-d']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '01/01/2001'], ['x' => 'date_format:Y-m-d']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '22000-01-01'], ['x' => 'date_format:Y-m-d']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '00-01-01'], ['x' => 'date_format:Y-m-d']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => ['Not', 'a', 'date']], ['x' => 'date_format:Y-m-d']);
- $this->assertTrue($v->fails());
- // Set current machine date to 31/xx/xxxx
- $v = new Validator($trans, ['x' => '2013-02'], ['x' => 'date_format:Y-m']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '2000-01-01T00:00:00Atlantic/Azores'], ['x' => 'date_format:Y-m-d\TH:i:se']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '2000-01-01T00:00:00Z'], ['x' => 'date_format:Y-m-d\TH:i:sT']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '2000-01-01T00:00:00+0000'], ['x' => 'date_format:Y-m-d\TH:i:sO']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '2000-01-01T00:00:00+00:30'], ['x' => 'date_format:Y-m-d\TH:i:sP']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '2000-01-01 17:43:59'], ['x' => 'date_format:Y-m-d H:i:s']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '2000-01-01 17:43:59'], ['x' => 'date_format:H:i:s']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '2000-01-01 17:43:59'], ['x' => 'date_format:Y-m-d H:i:s,H:i:s']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '17:43:59'], ['x' => 'date_format:Y-m-d H:i:s,H:i:s']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '17:43:59'], ['x' => 'date_format:H:i:s']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '17:43:59'], ['x' => 'date_format:H:i']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '17:43'], ['x' => 'date_format:H:i']);
- $this->assertTrue($v->passes());
- }
- public function testDateEquals()
- {
- date_default_timezone_set('UTC');
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => '2000-01-01'], ['x' => 'date_equals:2000-01-01']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => new Carbon('2000-01-01')], ['x' => 'date_equals:2000-01-01']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => new Carbon('2000-01-01')], ['x' => 'date_equals:2001-01-01']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['start' => new DateTime('2000-01-01'), 'ends' => new DateTime('2000-01-01')], ['ends' => 'date_equals:start']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => date('Y-m-d')], ['x' => 'date_equals:today']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => date('Y-m-d')], ['x' => 'date_equals:yesterday']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => date('Y-m-d')], ['x' => 'date_equals:tomorrow']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|date_equals:today']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|date_equals:yesterday']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|date_equals:tomorrow']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|date_equals:2012-01-01 17:44:00']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|date_equals:2012-01-01 17:43:59']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|date_equals:2012-01-01 17:44:01']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|date_equals:17:44:00']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|date_equals:17:43:59']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|date_equals:17:44:01']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|date_equals:17:44']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|date_equals:17:43']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|date_equals:17:45']);
- $this->assertTrue($v->fails());
- }
- public function testDateEqualsRespectsCarbonTestNowWhenParameterIsRelative()
- {
- date_default_timezone_set('UTC');
- $trans = $this->getIlluminateArrayTranslator();
- Carbon::setTestNow(new Carbon('2018-01-01'));
- $v = new Validator($trans, ['x' => '2018-01-01 00:00:00'], ['x' => 'date_equals:now']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '2018-01-01'], ['x' => 'date_equals:today']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '2018-01-01'], ['x' => 'date_equals:yesterday']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '2018-01-01'], ['x' => 'date_equals:tomorrow']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '01/01/2018'], ['x' => 'date_format:d/m/Y|date_equals:today']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '01/01/2018'], ['x' => 'date_format:d/m/Y|date_equals:yesterday']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '01/01/2018'], ['x' => 'date_format:d/m/Y|date_equals:tomorrow']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => new DateTime('2018-01-01')], ['x' => 'date_equals:today']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => new DateTime('2018-01-01')], ['x' => 'date_equals:yesterday']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => new DateTime('2018-01-01')], ['x' => 'date_equals:tomorrow']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => new Carbon('2018-01-01')], ['x' => 'date_equals:today|after:yesterday|before:tomorrow']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => new Carbon('2018-01-01')], ['x' => 'date_equals:yesterday']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => new Carbon('2018-01-01')], ['x' => 'date_equals:tomorrow']);
- $this->assertTrue($v->fails());
- }
- public function testBeforeAndAfter()
- {
- date_default_timezone_set('UTC');
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => '2000-01-01'], ['x' => 'Before:2012-01-01']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => ['2000-01-01']], ['x' => 'Before:2012-01-01']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['x' => new Carbon('2000-01-01')], ['x' => 'Before:2012-01-01']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => [new Carbon('2000-01-01')]], ['x' => 'Before:2012-01-01']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['x' => '2012-01-01'], ['x' => 'After:2000-01-01']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => ['2012-01-01']], ['x' => 'After:2000-01-01']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['x' => new Carbon('2012-01-01')], ['x' => 'After:2000-01-01']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => [new Carbon('2012-01-01')]], ['x' => 'After:2000-01-01']);
- $this->assertFalse($v->passes());
- $v = new Validator($trans, ['start' => '2012-01-01', 'ends' => '2013-01-01'], ['start' => 'After:2000-01-01', 'ends' => 'After:start']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['start' => '2012-01-01', 'ends' => '2000-01-01'], ['start' => 'After:2000-01-01', 'ends' => 'After:start']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['start' => '2012-01-01', 'ends' => '2013-01-01'], ['start' => 'Before:ends', 'ends' => 'After:start']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['start' => '2012-01-01', 'ends' => '2000-01-01'], ['start' => 'Before:ends', 'ends' => 'After:start']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => new DateTime('2000-01-01')], ['x' => 'Before:2012-01-01']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['start' => new DateTime('2012-01-01'), 'ends' => new Carbon('2013-01-01')], ['start' => 'Before:ends', 'ends' => 'After:start']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['start' => '2012-01-01', 'ends' => new DateTime('2013-01-01')], ['start' => 'Before:ends', 'ends' => 'After:start']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['start' => new DateTime('2012-01-01'), 'ends' => new DateTime('2000-01-01')], ['start' => 'After:2000-01-01', 'ends' => 'After:start']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['start' => 'today', 'ends' => 'tomorrow'], ['start' => 'Before:ends', 'ends' => 'After:start']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '2012-01-01 17:43:59'], ['x' => 'Before:2012-01-01 17:44|After:2012-01-01 17:43:58']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '2012-01-01 17:44:01'], ['x' => 'Before:2012-01-01 17:44:02|After:2012-01-01 17:44']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '2012-01-01 17:44'], ['x' => 'Before:2012-01-01 17:44:00']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '2012-01-01 17:44'], ['x' => 'After:2012-01-01 17:44:00']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '17:43:59'], ['x' => 'Before:17:44|After:17:43:58']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '17:44:01'], ['x' => 'Before:17:44:02|After:17:44']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '17:44'], ['x' => 'Before:17:44:00']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '17:44'], ['x' => 'After:17:44:00']);
- $this->assertTrue($v->fails());
- }
- public function testBeforeAndAfterWithFormat()
- {
- date_default_timezone_set('UTC');
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => '31/12/2000'], ['x' => 'before:31/02/2012']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => ['31/12/2000']], ['x' => 'before:31/02/2012']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '31/12/2000'], ['x' => 'date_format:d/m/Y|before:31/12/2012']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '31/12/2012'], ['x' => 'after:31/12/2000']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => ['31/12/2012']], ['x' => 'after:31/12/2000']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '31/12/2012'], ['x' => 'date_format:d/m/Y|after:31/12/2000']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => '31/12/2013'], ['start' => 'after:01/01/2000', 'ends' => 'after:start']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => '31/12/2013'], ['start' => 'date_format:d/m/Y|after:31/12/2000', 'ends' => 'date_format:d/m/Y|after:start']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => '31/12/2000'], ['start' => 'after:31/12/2000', 'ends' => 'after:start']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => '31/12/2000'], ['start' => 'date_format:d/m/Y|after:31/12/2000', 'ends' => 'date_format:d/m/Y|after:start']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => '31/12/2013'], ['start' => 'before:ends', 'ends' => 'after:start']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => '31/12/2013'], ['start' => 'date_format:d/m/Y|before:ends', 'ends' => 'date_format:d/m/Y|after:start']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => '31/12/2000'], ['start' => 'before:ends', 'ends' => 'after:start']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => '31/12/2000'], ['start' => 'date_format:d/m/Y|before:ends', 'ends' => 'date_format:d/m/Y|after:start']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['start' => 'invalid', 'ends' => 'invalid'], ['start' => 'date_format:d/m/Y|before:ends', 'ends' => 'date_format:d/m/Y|after:start']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => null], ['start' => 'date_format:d/m/Y|before:ends', 'ends' => 'date_format:d/m/Y|after:start']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => null], ['start' => 'date_format:d/m/Y|before:ends', 'ends' => 'nullable|date_format:d/m/Y|after:start']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => null], ['start' => 'before:ends']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['start' => '31/12/2012', 'ends' => null], ['start' => 'before:ends', 'ends' => 'nullable']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|after:yesterday|before:tomorrow']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|after:today']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|before:today']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => date('Y-m-d')], ['x' => 'after:yesterday|before:tomorrow']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => date('Y-m-d')], ['x' => 'after:today']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => date('Y-m-d')], ['x' => 'before:today']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|before:2012-01-01 17:44:01|after:2012-01-01 17:43:59']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|before:2012-01-01 17:44:00']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|after:2012-01-01 17:44:00']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|before:17:44:01|after:17:43:59']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|before:17:44:00']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|after:17:44:00']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|before:17:45|after:17:43']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|before:17:44']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|after:17:44']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '2038-01-18', '2018-05-12' => '2038-01-19'], ['x' => 'date_format:Y-m-d|before:2018-05-12']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '1970-01-02', '2018-05-12' => '1970-01-01'], ['x' => 'date_format:Y-m-d|after:2018-05-12']);
- $this->assertTrue($v->fails());
- }
- public function testWeakBeforeAndAfter()
- {
- date_default_timezone_set('UTC');
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => '2012-01-15'], ['x' => 'before_or_equal:2012-01-15']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '2012-01-15'], ['x' => 'before_or_equal:2012-01-16']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '2012-01-15'], ['x' => 'before_or_equal:2012-01-14']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '15/01/2012'], ['x' => 'date_format:d/m/Y|before_or_equal:15/01/2012']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '15/01/2012'], ['x' => 'date_format:d/m/Y|before_or_equal:14/01/2012']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|before_or_equal:today']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|before_or_equal:tomorrow']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|before_or_equal:yesterday']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '2012-01-15'], ['x' => 'after_or_equal:2012-01-15']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '2012-01-15'], ['x' => 'after_or_equal:2012-01-14']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '2012-01-15'], ['x' => 'after_or_equal:2012-01-16']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '15/01/2012'], ['x' => 'date_format:d/m/Y|after_or_equal:15/01/2012']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '15/01/2012'], ['x' => 'date_format:d/m/Y|after_or_equal:16/01/2012']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|after_or_equal:today']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|after_or_equal:yesterday']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => date('d/m/Y')], ['x' => 'date_format:d/m/Y|after_or_equal:tomorrow']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|before_or_equal:2012-01-01 17:44:00|after_or_equal:2012-01-01 17:44:00']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|before_or_equal:2012-01-01 17:43:59']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '2012-01-01 17:44:00'], ['x' => 'date_format:Y-m-d H:i:s|after_or_equal:2012-01-01 17:44:01']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|before_or_equal:17:44:00|after_or_equal:17:44:00']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|before_or_equal:17:43:59']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '17:44:00'], ['x' => 'date_format:H:i:s|after_or_equal:17:44:01']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|before_or_equal:17:44|after_or_equal:17:44']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|before_or_equal:17:43']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['x' => '17:44'], ['x' => 'date_format:H:i|after_or_equal:17:45']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['foo' => '2012-01-14', 'bar' => '2012-01-15'], ['foo' => 'before_or_equal:bar']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '2012-01-15', 'bar' => '2012-01-15'], ['foo' => 'before_or_equal:bar']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '2012-01-15 13:00', 'bar' => '2012-01-15 12:00'], ['foo' => 'before_or_equal:bar']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['foo' => '2012-01-15 11:00', 'bar' => null], ['foo' => 'date_format:Y-m-d H:i|before_or_equal:bar', 'bar' => 'date_format:Y-m-d H:i']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['foo' => '2012-01-15 11:00', 'bar' => null], ['foo' => 'date_format:Y-m-d H:i|before_or_equal:bar', 'bar' => 'date_format:Y-m-d H:i|nullable']);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, ['foo' => '2012-01-15 11:00', 'bar' => null], ['foo' => 'before_or_equal:bar']);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, ['foo' => '2012-01-15 11:00', 'bar' => null], ['foo' => 'before_or_equal:bar', 'bar' => 'nullable']);
- $this->assertTrue($v->fails());
- }
- public function testSometimesAddingRules()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'foo'], ['x' => 'Required']);
- $v->sometimes('x', 'Confirmed', function ($i) {
- return $i->x === 'foo';
- });
- $this->assertEquals(['x' => ['Required', 'Confirmed']], $v->getRules());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => ''], ['y' => 'Required']);
- $v->sometimes('x', 'Required', function ($i) {
- return true;
- });
- $this->assertEquals(['x' => ['Required'], 'y' => ['Required']], $v->getRules());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'foo'], ['x' => 'Required']);
- $v->sometimes('x', 'Confirmed', function ($i) {
- return $i->x === 'bar';
- });
- $this->assertEquals(['x' => ['Required']], $v->getRules());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'foo'], ['x' => 'Required']);
- $v->sometimes('x', 'Foo|Bar', function ($i) {
- return $i->x === 'foo';
- });
- $this->assertEquals(['x' => ['Required', 'Foo', 'Bar']], $v->getRules());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['x' => 'foo'], ['x' => 'Required']);
- $v->sometimes('x', ['Foo', 'Bar:Baz'], function ($i) {
- return $i->x === 'foo';
- });
- $this->assertEquals(['x' => ['Required', 'Foo', 'Bar:Baz']], $v->getRules());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => [['name' => 'first', 'title' => null]]], []);
- $v->sometimes('foo.*.name', 'Required|String', function ($i) {
- return is_null($i['foo'][0]['title']);
- });
- $this->assertEquals(['foo.0.name' => ['Required', 'String']], $v->getRules());
- }
- public function testItemAwareSometimesAddingRules()
- {
- // ['users'] -> if users is not empty it must be validated as array
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['users' => [['name' => 'Taylor'], ['name' => 'Abigail']]], ['users.*.name'=> 'required|string']);
- $v->sometimes(['users'], 'array', function ($i, $item) {
- return $item !== null;
- });
- $this->assertEquals(['users' => ['array'], 'users.0.name' => ['required', 'string'], 'users.1.name' => ['required', 'string']], $v->getRules());
- // ['users'] -> if users is null no rules will be applied
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['users' => null], ['users.*.name'=> 'required|string']);
- $v->sometimes(['users'], 'array', function ($i, $item) {
- return (bool) $item;
- });
- $this->assertEquals([], $v->getRules());
- // ['company.users'] -> if users is not empty it must be validated as array
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['company' => ['users' => [['name' => 'Taylor'], ['name' => 'Abigail']]]], ['company.users.*.name'=> 'required|string']);
- $v->sometimes(['company.users'], 'array', function ($i, $item) {
- return $item->users !== null;
- });
- $this->assertEquals(['company.users' => ['array'], 'company.users.0.name' => ['required', 'string'], 'company.users.1.name' => ['required', 'string']], $v->getRules());
- // ['company.users'] -> if users is null no rules will be applied
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['company' => ['users' => null]], ['company'=> 'required', 'company.users.*.name'=> 'required|string']);
- $v->sometimes(['company.users'], 'array', function ($i, $item) {
- return (bool) $item->users;
- });
- $this->assertEquals(['company' => ['required']], $v->getRules());
- // ['company.*'] -> if users is not empty it must be validated as array
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['company' => ['users' => [['name' => 'Taylor'], ['name' => 'Abigail']]]], ['company.users.*.name'=> 'required|string']);
- $v->sometimes(['company.*'], 'array', function ($i, $item) {
- return $item !== null;
- });
- $this->assertEquals(['company.users' => ['array'], 'company.users.0.name' => ['required', 'string'], 'company.users.1.name' => ['required', 'string']], $v->getRules());
- // ['company.*'] -> if users is null no rules will be applied
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['company' => ['users' => null]], ['company'=> 'required', 'company.users.*.name'=> 'required|string']);
- $v->sometimes(['company.*'], 'array', function ($i, $item) {
- return (bool) $item;
- });
- $this->assertEquals(['company' => ['required']], $v->getRules());
- // ['users.*'] -> all nested array items in users must be validated as array
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['users' => [['name' => 'Taylor'], ['name' => 'Abigail']]], ['users.*.name'=> 'required|string']);
- $v->sometimes(['users.*'], 'array', function ($i, $item) {
- return (bool) $item;
- });
- $this->assertEquals(['users.0' => ['array'], 'users.1' => ['array'], 'users.0.name' => ['required', 'string'], 'users.1.name' => ['required', 'string']], $v->getRules());
- // ['company.users.*'] -> all nested array items in users must be validated as array
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['company' => ['users' => [['name' => 'Taylor'], ['name' => 'Abigail']]]], ['company.users.*.name'=> 'required|string']);
- $v->sometimes(['company.users.*'], 'array', function () {
- return true;
- });
- $this->assertEquals(['company.users.0' => ['array'], 'company.users.1' => ['array'], 'company.users.0.name' => ['required', 'string'], 'company.users.1.name' => ['required', 'string']], $v->getRules());
- // ['company.*.*'] -> all nested array items in users must be validated as array
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['company' => ['users' => [['name' => 'Taylor'], ['name' => 'Abigail']]]], ['company.users.*.name'=> 'required|string']);
- $v->sometimes(['company.*.*'], 'array', function ($i, $item) {
- return true;
- });
- $this->assertEquals(['company.users.0' => ['array'], 'company.users.1' => ['array'], 'company.users.0.name' => ['required', 'string'], 'company.users.1.name' => ['required', 'string']], $v->getRules());
- // ['user.profile.value'] -> multiple true cases, the item based condition does match and the optional validation is added
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['user' => ['profile' => ['photo' => 'image.jpg', 'type' => 'email', 'value' => 'test@test.com']]], ['user.profile.*' => ['required']]);
- $v->sometimes(['user.profile.value'], 'email', function ($i, $item) {
- return $item->type === 'email';
- });
- $v->sometimes('user.profile.photo', 'mimes:jpg,bmp,png', function ($i, $item) {
- return $item->photo;
- });
- $this->assertEquals(['user.profile.value' => ['required', 'email'], 'user.profile.photo' => ['required', 'mimes:jpg,bmp,png'], 'user.profile.type' => ['required']], $v->getRules());
- // ['user.profile.value'] -> multiple true cases with middle wildcard, the item based condition does match and the optional validation is added
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['user' => ['profile' => ['photo' => 'image.jpg', 'type' => 'email', 'value' => 'test@test.com']]], ['user.profile.*' => ['required']]);
- $v->sometimes('user.*.value', 'email', function ($i, $item) {
- return $item->type === 'email';
- });
- $v->sometimes('user.*.photo', 'mimes:jpg,bmp,png', function ($i, $item) {
- return $item->photo;
- });
- $this->assertEquals(['user.profile.value' => ['required', 'email'], 'user.profile.photo' => ['required', 'mimes:jpg,bmp,png'], 'user.profile.type' => ['required']], $v->getRules());
- // ['profiles.*.value'] -> true and false cases for the same field with middle wildcard, the item based condition does match and the optional validation is added
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['profiles' => [['type' => 'email'], ['type' => 'string']]], ['profiles.*.value' => ['required']]);
- $v->sometimes(['profiles.*.value'], 'email', function ($i, $item) {
- return $item->type === 'email';
- });
- $v->sometimes('profiles.*.value', 'url', function ($i, $item) {
- return $item->type !== 'email';
- });
- $this->assertEquals(['profiles.0.value' => ['required', 'email'], 'profiles.1.value' => ['required', 'url']], $v->getRules());
- // ['profiles.*.value'] -> true case with middle wildcard, the item based condition does match and the optional validation is added
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['profiles' => [['type' => 'email'], ['type' => 'string']]], ['profiles.*.value' => ['required']]);
- $v->sometimes(['profiles.*.value'], 'email', function ($i, $item) {
- return $item->type === 'email';
- });
- $this->assertEquals(['profiles.0.value' => ['required', 'email'], 'profiles.1.value' => ['required']], $v->getRules());
- // ['profiles.*.value'] -> false case with middle wildcard, the item based condition does not match and the optional validation is not added
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['profiles' => [['type' => 'string'], ['type' => 'string']]], ['profiles.*.value' => ['required']]);
- $v->sometimes(['profiles.*.value'], 'email', function ($i, $item) {
- return $item->type === 'email';
- });
- $this->assertEquals(['profiles.0.value' => ['required'], 'profiles.1.value' => ['required']], $v->getRules());
- // ['users.profiles.*.value'] -> true case nested and with middle wildcard, the item based condition does match and the optional validation is added
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['users' => ['profiles' => [['type' => 'email'], ['type' => 'string']]]], ['users.profiles.*.value' => ['required']]);
- $v->sometimes(['users.profiles.*.value'], 'email', function ($i, $item) {
- return $item->type === 'email';
- });
- $this->assertEquals(['users.profiles.0.value' => ['required', 'email'], 'users.profiles.1.value' => ['required']], $v->getRules());
- // ['users.*.*.value'] -> true case nested and with double middle wildcard, the item based condition does match and the optional validation is added
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['users' => ['profiles' => [['type' => 'email'], ['type' => 'string']]]], ['users.profiles.*.value' => ['required']]);
- $v->sometimes(['users.*.*.value'], 'email', function ($i, $item) {
- return $item->type === 'email';
- });
- $this->assertEquals(['users.profiles.0.value' => ['required', 'email'], 'users.profiles.1.value' => ['required']], $v->getRules());
- // 'user.value' -> true case nested with string, the item based condition does match and the optional validation is added
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['user' => ['name' => 'username', 'type' => 'email', 'value' => 'test@test.com']], ['user.*' => ['required']]);
- $v->sometimes('user.value', 'email', function ($i, $item) {
- return $item->type === 'email';
- });
- $this->assertEquals(['user.name' => ['required'], 'user.type' => ['required'], 'user.value' => ['required', 'email']], $v->getRules());
- // 'user.value' -> standard true case with string, the INPUT based condition does match and the optional validation is added
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['name' => 'username', 'type' => 'email', 'value' => 'test@test.com'], ['*' => ['required']]);
- $v->sometimes('value', 'email', function ($i) {
- return $i->type === 'email';
- });
- $this->assertEquals(['name' => ['required'], 'type' => ['required'], 'value' => ['required', 'email']], $v->getRules());
- // ['value'] -> standard true case with array, the INPUT based condition does match and the optional validation is added
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['name' => 'username', 'type' => 'email', 'value' => 'test@test.com'], ['*' => ['required']]);
- $v->sometimes(['value'], 'email', function ($i, $item) {
- return $i->type === 'email';
- });
- $this->assertEquals(['name' => ['required'], 'type' => ['required'], 'value' => ['required', 'email']], $v->getRules());
- // ['email'] -> if value is set, it will be validated as string
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['email' => 'test@test.com'], ['*' => ['required']]);
- $v->sometimes(['email'], 'email', function ($i, $item) {
- return $item;
- });
- $this->assertEquals(['email' => ['required', 'email']], $v->getRules());
- // ['attendee.*'] -> if attendee name is set, all other fields will be required as well
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['attendee' => ['name' => 'Taylor', 'title' => 'Creator of Laravel', 'type' => 'Developer']], ['attendee.*'=> 'string']);
- $v->sometimes(['attendee.*'], 'required', function ($i, $item) {
- return (bool) $item;
- });
- $this->assertEquals(['attendee.name' => ['string', 'required'], 'attendee.title' => ['string', 'required'], 'attendee.type' => ['string', 'required']], $v->getRules());
- }
- public function testValidateSometimesImplicitEachWithAsterisksBeforeAndAfter()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [
- 'foo' => [
- ['start' => '2016-04-19', 'end' => '2017-04-19'],
- ],
- ], []);
- $v->sometimes('foo.*.start', ['before:foo.*.end'], function () {
- return true;
- });
- $this->assertTrue($v->passes());
- $v = new Validator($trans, [
- 'foo' => [
- ['start' => '2016-04-19', 'end' => '2017-04-19'],
- ],
- ], []);
- $v->sometimes('foo.*.start', 'before:foo.*.end', function () {
- return true;
- });
- $this->assertTrue($v->passes());
- $v = new Validator($trans, [
- 'foo' => [
- ['start' => '2016-04-19', 'end' => '2017-04-19'],
- ],
- ], []);
- $v->sometimes('foo.*.end', ['before:foo.*.start'], function () {
- return true;
- });
- $this->assertTrue($v->fails());
- $v = new Validator($trans, [
- 'foo' => [
- ['start' => '2016-04-19', 'end' => '2017-04-19'],
- ],
- ], []);
- $v->sometimes('foo.*.end', ['after:foo.*.start'], function () {
- return true;
- });
- $this->assertTrue($v->passes());
- $v = new Validator($trans, [
- 'foo' => [
- ['start' => '2016-04-19', 'end' => '2017-04-19'],
- ],
- ], []);
- $v->sometimes('foo.*.start', ['after:foo.*.end'], function () {
- return true;
- });
- $this->assertTrue($v->fails());
- }
- public function testCustomValidators()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.foo' => 'foo!'], 'en');
- $v = new Validator($trans, ['name' => 'taylor'], ['name' => 'foo']);
- $v->addExtension('foo', function () {
- return false;
- });
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('foo!', $v->messages()->first('name'));
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.foo_bar' => 'foo!'], 'en');
- $v = new Validator($trans, ['name' => 'taylor'], ['name' => 'foo_bar']);
- $v->addExtension('FooBar', function () {
- return false;
- });
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('foo!', $v->messages()->first('name'));
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['name' => 'taylor'], ['name' => 'foo_bar']);
- $v->addExtension('FooBar', function () {
- return false;
- });
- $v->setFallbackMessages(['foo_bar' => 'foo!']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('foo!', $v->messages()->first('name'));
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['name' => 'taylor'], ['name' => 'foo_bar']);
- $v->addExtensions([
- 'FooBar' => function () {
- return false;
- },
- ]);
- $v->setFallbackMessages(['foo_bar' => 'foo!']);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('foo!', $v->messages()->first('name'));
- }
- public function testClassBasedCustomValidators()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.foo' => 'foo!'], 'en');
- $v = new Validator($trans, ['name' => 'taylor'], ['name' => 'foo']);
- $v->setContainer($container = m::mock(Container::class));
- $v->addExtension('foo', 'Foo@bar');
- $container->shouldReceive('make')->once()->with('Foo')->andReturn($foo = m::mock(stdClass::class));
- $foo->shouldReceive('bar')->once()->andReturn(false);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('foo!', $v->messages()->first('name'));
- }
- public function testClassBasedCustomValidatorsUsingConventionalMethod()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.foo' => 'foo!'], 'en');
- $v = new Validator($trans, ['name' => 'taylor'], ['name' => 'foo']);
- $v->setContainer($container = m::mock(Container::class));
- $v->addExtension('foo', 'Foo');
- $container->shouldReceive('make')->once()->with('Foo')->andReturn($foo = m::mock(stdClass::class));
- $foo->shouldReceive('validate')->once()->andReturn(false);
- $this->assertFalse($v->passes());
- $v->messages()->setFormat(':message');
- $this->assertSame('foo!', $v->messages()->first('name'));
- }
- public function testCustomImplicitValidators()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [], ['implicit_rule' => 'foo']);
- $v->addImplicitExtension('implicit_rule', function () {
- return true;
- });
- $this->assertTrue($v->passes());
- }
- public function testCustomDependentValidators()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans,
- [
- ['name' => 'Jamie', 'age' => 27],
- ],
- ['*.name' => 'dependent_rule:*.age']
- );
- $v->addDependentExtension('dependent_rule', function ($name) use ($v) {
- return Arr::get($v->getData(), $name) === 'Jamie';
- });
- $this->assertTrue($v->passes());
- }
- public function testExceptionThrownOnIncorrectParameterCount()
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Validation rule required_if requires at least 2 parameters.');
- $trans = $this->getTranslator();
- $v = new Validator($trans, [], ['foo' => 'required_if:foo']);
- $v->passes();
- }
- public function testValidateImplicitEachWithAsterisks()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $data = ['foo' => [5, 10, 15]];
- // pipe rules fails
- $v = new Validator($trans, $data, [
- 'foo' => 'Array',
- 'foo.*' => 'Numeric|Min:6|Max:16',
- ]);
- $this->assertFalse($v->passes());
- // pipe passes
- $v = new Validator($trans, $data, [
- 'foo' => 'Array',
- 'foo.*' => 'Numeric|Min:4|Max:16',
- ]);
- $this->assertTrue($v->passes());
- // array rules fails
- $v = new Validator($trans, $data, [
- 'foo' => 'Array',
- 'foo.*' => ['Numeric', 'Min:6', 'Max:16'],
- ]);
- $this->assertFalse($v->passes());
- // array rules passes
- $v = new Validator($trans, $data, [
- 'foo' => 'Array',
- 'foo.*' => ['Numeric', 'Min:4', 'Max:16'],
- ]);
- $this->assertTrue($v->passes());
- // string passes
- $v = new Validator($trans,
- ['foo' => [['name' => 'first'], ['name' => 'second']]],
- ['foo' => 'Array', 'foo.*.name' => 'Required|String']);
- $this->assertTrue($v->passes());
- // numeric fails
- $v = new Validator($trans,
- ['foo' => [['name' => 'first'], ['name' => 'second']]],
- ['foo' => 'Array', 'foo.*.name' => 'Required|Numeric']);
- $this->assertFalse($v->passes());
- // nested array fails
- $v = new Validator($trans,
- ['foo' => [['name' => 'first', 'votes' => [1, 2]], ['name' => 'second', 'votes' => ['something', 2]]]],
- ['foo' => 'Array', 'foo.*.name' => 'Required|String', 'foo.*.votes.*' => 'Required|Integer']);
- $this->assertFalse($v->passes());
- // multiple items passes
- $v = new Validator($trans, ['foo' => [['name' => 'first'], ['name' => 'second']]],
- ['foo' => 'Array', 'foo.*.name' => ['Required', 'String']]);
- $this->assertTrue($v->passes());
- // multiple items fails
- $v = new Validator($trans, ['foo' => [['name' => 'first'], ['name' => 'second']]],
- ['foo' => 'Array', 'foo.*.name' => ['Required', 'Numeric']]);
- $this->assertFalse($v->passes());
- // nested arrays fails
- $v = new Validator($trans,
- ['foo' => [['name' => 'first', 'votes' => [1, 2]], ['name' => 'second', 'votes' => ['something', 2]]]],
- ['foo' => 'Array', 'foo.*.name' => ['Required', 'String'], 'foo.*.votes.*' => ['Required', 'Integer']]);
- $this->assertFalse($v->passes());
- }
- public function testSometimesOnArraysInImplicitRules()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [['bar' => 'baz']], ['*.foo' => 'sometimes|required|string']);
- $this->assertTrue($v->passes());
- // $data = ['names' => [['second' => []]]];
- // $v = new Validator($trans, $data, ['names.*.second' => 'sometimes|required']);
- // $this->assertFalse($v->passes());
- $data = ['names' => [['second' => ['Taylor']]]];
- $v = new Validator($trans, $data, ['names.*.second' => 'sometimes|required|string']);
- $this->assertFalse($v->passes());
- $this->assertEquals(['validation.string'], $v->errors()->get('names.0.second'));
- }
- public function testValidateImplicitEachWithAsterisksForRequiredNonExistingKey()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $data = ['companies' => ['spark']];
- $v = new Validator($trans, $data, ['companies.*.name' => 'required']);
- $this->assertFalse($v->passes());
- $data = ['names' => [['second' => 'I have no first']]];
- $v = new Validator($trans, $data, ['names.*.first' => 'required']);
- $this->assertFalse($v->passes());
- $data = [];
- $v = new Validator($trans, $data, ['names.*.first' => 'required']);
- $this->assertTrue($v->passes());
- $data = ['names' => [['second' => 'I have no first']]];
- $v = new Validator($trans, $data, ['names.*.first' => 'required']);
- $this->assertFalse($v->passes());
- $data = [
- 'people' => [
- ['cars' => [['model' => 2005], []]],
- ],
- ];
- $v = new Validator($trans, $data, ['people.*.cars.*.model' => 'required']);
- $this->assertFalse($v->passes());
- $data = [
- 'people' => [
- ['name' => 'test', 'cars' => [['model' => 2005], ['name' => 'test2']]],
- ],
- ];
- $v = new Validator($trans, $data, ['people.*.cars.*.model' => 'required']);
- $this->assertFalse($v->passes());
- $data = [
- 'people' => [
- ['phones' => ['iphone', 'android'], 'cars' => [['model' => 2005], ['name' => 'test2']]],
- ],
- ];
- $v = new Validator($trans, $data, ['people.*.cars.*.model' => 'required']);
- $this->assertFalse($v->passes());
- $data = ['names' => [['second' => '2']]];
- $v = new Validator($trans, $data, ['names.*.first' => 'sometimes|required']);
- $this->assertTrue($v->passes());
- $data = [
- 'people' => [
- ['name' => 'Jon', 'email' => 'a@b.c'],
- ['name' => 'Jon'],
- ],
- ];
- $v = new Validator($trans, $data, ['people.*.email' => 'required']);
- $this->assertFalse($v->passes());
- $data = [
- 'people' => [
- [
- 'name' => 'Jon',
- 'cars' => [
- ['model' => 2014],
- ],
- ],
- [
- 'name' => 'Arya',
- 'cars' => [
- ['name' => 'test'],
- ],
- ],
- ],
- ];
- $v = new Validator($trans, $data, ['people.*.cars.*.model' => 'required']);
- $this->assertFalse($v->passes());
- }
- public function testParsingArrayKeysWithDot()
- {
- $trans = $this->getIlluminateArrayTranslator();
- // Interpreted dot fails on empty value
- $v = new Validator($trans, ['foo' => ['bar' => ''], 'foo.bar' => 'valid'], ['foo.bar' => 'required']);
- $this->assertTrue($v->fails());
- // Escaped dot fails on empty value
- $v = new Validator($trans, ['foo' => ['bar' => 'valid'], 'foo.bar' => ''], ['foo\.bar' => 'required']);
- $this->assertTrue($v->fails());
- // Interpreted dot succeeds
- $v = new Validator($trans, ['foo' => ['bar' => 'valid'], 'foo.bar' => 'zxc'], ['foo\.bar' => 'required']);
- $this->assertFalse($v->fails());
- // Interpreted dot followed by escaped dot fails on empty value
- $v = new Validator($trans, ['foo' => ['bar.baz' => '']], ['foo.bar\.baz' => 'required']);
- $this->assertTrue($v->fails());
- // Interpreted dot followed by escaped dot fails on empty value
- $v = new Validator($trans, ['foo' => [['bar.baz' => ''], ['bar.baz' => '']]], ['foo.*.bar\.baz' => 'required']);
- $this->assertTrue($v->fails());
- }
- public function testParsingArrayKeysWithDotWhenTestingExistence()
- {
- $trans = $this->getIlluminateArrayTranslator();
- // RequiredWith using escaped dot in a nested array
- $v = new Validator($trans, ['foo' => '', 'bar' => ['foo.bar' => 'valid']], ['foo' => 'required_with:bar.foo\.bar']);
- $this->assertFalse($v->passes());
- // RequiredWithAll using escaped dot in a nested array
- $v = new Validator($trans, ['foo' => '', 'bar' => ['foo.bar' => 'valid']], ['foo' => 'required_with_all:bar.foo\.bar']);
- $this->assertFalse($v->passes());
- // RequiredWithout using escaped dot in a nested array
- $v = new Validator($trans, ['foo' => 'valid', 'bar' => ['foo.bar' => 'valid']], ['foo' => 'required_without:bar.foo\.bar']);
- $this->assertTrue($v->passes());
- // RequiredWithoutAll using escaped dot in a nested array
- $v = new Validator($trans, ['foo' => 'valid', 'bar' => ['foo.bar' => 'valid']], ['foo' => 'required_without_all:bar.foo\.bar']);
- $this->assertTrue($v->passes());
- // Same using escaped dot in a nested array
- $v = new Validator($trans, ['foo' => 'valid', 'bar' => ['foo.bar' => 'valid']], ['foo' => 'same:bar.foo\.bar']);
- $this->assertTrue($v->passes());
- // RequiredUnless using escaped dot in a nested array
- $v = new Validator($trans, ['foo' => '', 'bar' => ['foo.bar' => 'valid']], ['foo' => 'required_unless:bar.foo\.bar,valid']);
- $this->assertTrue($v->passes());
- }
- public function testPassingSlashVulnerability()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [
- 'matrix' => ['\\' => ['invalid'], '1\\' => ['invalid']],
- ], [
- 'matrix.*.*' => 'integer',
- ]);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, [
- 'matrix' => ['\\' => [1], '1\\' => [1]],
- ], [
- 'matrix.*.*' => 'integer',
- ]);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, [
- 'foo' => ['bar' => 'valid'], 'foo.bar' => 'invalid', 'foo->bar' => 'valid',
- ], [
- 'foo\.bar' => 'required|in:valid',
- ]);
- $this->assertTrue($v->fails());
- }
- public function testPlaceholdersAreReplaced()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [
- 'matrix' => ['\\' => ['invalid'], '1\\' => ['invalid']],
- ], [
- 'matrix.*.*' => 'integer',
- ]);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, [
- 'matrix' => ['\\' => [1], '1\\' => [1]],
- ], [
- 'matrix.*.*' => 'integer',
- ]);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, [
- 'foo' => ['bar' => 'valid'], 'foo.bar' => 'invalid', 'foo->bar' => 'valid',
- ], [
- 'foo\.bar' => 'required|in:valid',
- ]);
- $this->assertTrue($v->fails());
- $this->assertArrayHasKey('foo.bar', $v->errors()->getMessages());
- $v = new Validator($trans, [
- 'foo.bar' => 'valid',
- ], [
- 'foo\.bar' => 'required|in:valid',
- ]);
- $this->assertTrue($v->passes());
- $this->assertArrayHasKey('foo.bar', $v->validated());
- }
- public function testCoveringEmptyKeys()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => ['' => ['bar' => '']]], ['foo.*.bar' => 'required']);
- $this->assertTrue($v->fails());
- }
- public function testImplicitEachWithAsterisksWithArrayValues()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => ['bar.baz' => '']], ['foo' => 'required']);
- $this->assertEquals(['foo' => ['bar.baz' => '']], $v->validated());
- }
- public function testValidateNestedArrayWithCommonParentChildKey()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $data = [
- 'products' => [
- [
- 'price' => 2,
- 'options' => [
- ['price' => 1],
- ],
- ],
- [
- 'price' => 2,
- 'options' => [
- ['price' => 0],
- ],
- ],
- ],
- ];
- $v = new Validator($trans, $data, ['products.*.price' => 'numeric|min:1']);
- $this->assertTrue($v->passes());
- }
- public function testValidateNestedArrayWithNonNumericKeys()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $data = [
- 'item_amounts' => [
- 'item_123' => 2,
- ],
- ];
- $v = new Validator($trans, $data, ['item_amounts.*' => 'numeric|min:5']);
- $this->assertFalse($v->passes());
- }
- public function testValidateImplicitEachWithAsterisksConfirmed()
- {
- $trans = $this->getIlluminateArrayTranslator();
- // confirmed passes
- $v = new Validator($trans, [
- 'foo' => [
- ['password' => 'foo0', 'password_confirmation' => 'foo0'],
- ['password' => 'foo1', 'password_confirmation' => 'foo1'],
- ],
- ], ['foo.*.password' => 'confirmed']);
- $this->assertTrue($v->passes());
- // nested confirmed passes
- $v = new Validator($trans, [
- 'foo' => [
- [
- 'bar' => [
- ['password' => 'bar0', 'password_confirmation' => 'bar0'],
- ['password' => 'bar1', 'password_confirmation' => 'bar1'],
- ],
- ],
- [
- 'bar' => [
- ['password' => 'bar2', 'password_confirmation' => 'bar2'],
- ['password' => 'bar3', 'password_confirmation' => 'bar3'],
- ],
- ],
- ],
- ], ['foo.*.bar.*.password' => 'confirmed']);
- $this->assertTrue($v->passes());
- // confirmed fails
- $v = new Validator($trans, [
- 'foo' => [
- ['password' => 'foo0', 'password_confirmation' => 'bar0'],
- ['password' => 'foo1'],
- ],
- ], ['foo.*.password' => 'confirmed']);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.password'));
- $this->assertTrue($v->messages()->has('foo.1.password'));
- // nested confirmed fails
- $v = new Validator($trans, [
- 'foo' => [
- [
- 'bar' => [
- ['password' => 'bar0'],
- ['password' => 'bar1', 'password_confirmation' => 'bar2'],
- ],
- ],
- ],
- ], ['foo.*.bar.*.password' => 'confirmed']);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.bar.0.password'));
- $this->assertTrue($v->messages()->has('foo.0.bar.1.password'));
- }
- public function testValidateImplicitEachWithAsterisksDifferent()
- {
- $trans = $this->getIlluminateArrayTranslator();
- // different passes
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => 'foo', 'last' => 'bar'],
- ['name' => 'bar', 'last' => 'foo'],
- ],
- ], ['foo.*.name' => ['different:foo.*.last']]);
- $this->assertTrue($v->passes());
- // nested different passes
- $v = new Validator($trans, [
- 'foo' => [
- [
- 'bar' => [
- ['name' => 'foo', 'last' => 'bar'],
- ['name' => 'bar', 'last' => 'foo'],
- ],
- ],
- ],
- ], ['foo.*.bar.*.name' => ['different:foo.*.bar.*.last']]);
- $this->assertTrue($v->passes());
- // different fails
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => 'foo', 'last' => 'foo'],
- ['name' => 'bar', 'last' => 'bar'],
- ],
- ], ['foo.*.name' => ['different:foo.*.last']]);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.name'));
- $this->assertTrue($v->messages()->has('foo.1.name'));
- // nested different fails
- $v = new Validator($trans, [
- 'foo' => [
- [
- 'bar' => [
- ['name' => 'foo', 'last' => 'foo'],
- ['name' => 'bar', 'last' => 'bar'],
- ],
- ],
- ],
- ], ['foo.*.bar.*.name' => ['different:foo.*.bar.*.last']]);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.bar.0.name'));
- $this->assertTrue($v->messages()->has('foo.0.bar.1.name'));
- }
- public function testValidateImplicitEachWithAsterisksSame()
- {
- $trans = $this->getIlluminateArrayTranslator();
- // same passes
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => 'foo', 'last' => 'foo'],
- ['name' => 'bar', 'last' => 'bar'],
- ],
- ], ['foo.*.name' => ['same:foo.*.last']]);
- $this->assertTrue($v->passes());
- // nested same passes
- $v = new Validator($trans, [
- 'foo' => [
- [
- 'bar' => [
- ['name' => 'foo', 'last' => 'foo'],
- ['name' => 'bar', 'last' => 'bar'],
- ],
- ],
- ],
- ], ['foo.*.bar.*.name' => ['same:foo.*.bar.*.last']]);
- $this->assertTrue($v->passes());
- // same fails
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => 'foo', 'last' => 'bar'],
- ['name' => 'bar', 'last' => 'foo'],
- ],
- ], ['foo.*.name' => ['same:foo.*.last']]);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.name'));
- $this->assertTrue($v->messages()->has('foo.1.name'));
- // nested same fails
- $v = new Validator($trans, [
- 'foo' => [
- [
- 'bar' => [
- ['name' => 'foo', 'last' => 'bar'],
- ['name' => 'bar', 'last' => 'foo'],
- ],
- ],
- ],
- ], ['foo.*.bar.*.name' => ['same:foo.*.bar.*.last']]);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.bar.0.name'));
- $this->assertTrue($v->messages()->has('foo.0.bar.1.name'));
- }
- public function testValidateImplicitEachWithAsterisksRequired()
- {
- $trans = $this->getIlluminateArrayTranslator();
- // required passes
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => 'first'],
- ['name' => 'second'],
- ],
- ], ['foo.*.name' => ['Required']]);
- $this->assertTrue($v->passes());
- // nested required passes
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => 'first'],
- ['name' => 'second'],
- ],
- ], ['foo.*.name' => ['Required']]);
- $this->assertTrue($v->passes());
- // required fails
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => null],
- ['name' => null, 'last' => 'last'],
- ],
- ], ['foo.*.name' => ['Required']]);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.name'));
- $this->assertTrue($v->messages()->has('foo.1.name'));
- // nested required fails
- $v = new Validator($trans, [
- 'foo' => [
- [
- 'bar' => [
- ['name' => null],
- ['name' => null],
- ],
- ],
- ],
- ], ['foo.*.bar.*.name' => ['Required']]);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.bar.0.name'));
- $this->assertTrue($v->messages()->has('foo.0.bar.1.name'));
- }
- public function testValidateImplicitEachWithAsterisksRequiredIf()
- {
- $trans = $this->getIlluminateArrayTranslator();
- // required_if passes
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => 'first', 'last' => 'foo'],
- ['last' => 'bar'],
- ],
- ], ['foo.*.name' => ['Required_if:foo.*.last,foo']]);
- $this->assertTrue($v->passes());
- // nested required_if passes
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => 'first', 'last' => 'foo'],
- ['last' => 'bar'],
- ],
- ], ['foo.*.name' => ['Required_if:foo.*.last,foo']]);
- $this->assertTrue($v->passes());
- // required_if fails
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => null, 'last' => 'foo'],
- ['name' => null, 'last' => 'foo'],
- ],
- ], ['foo.*.name' => ['Required_if:foo.*.last,foo']]);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.name'));
- $this->assertTrue($v->messages()->has('foo.1.name'));
- // nested required_if fails
- $v = new Validator($trans, [
- 'foo' => [
- [
- 'bar' => [
- ['name' => null, 'last' => 'foo'],
- ['name' => null, 'last' => 'foo'],
- ],
- ],
- ],
- ], ['foo.*.bar.*.name' => ['Required_if:foo.*.bar.*.last,foo']]);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.bar.0.name'));
- $this->assertTrue($v->messages()->has('foo.0.bar.1.name'));
- }
- public function testValidateImplicitEachWithAsterisksRequiredUnless()
- {
- $trans = $this->getIlluminateArrayTranslator();
- // required_unless passes
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => null, 'last' => 'foo'],
- ['name' => 'second', 'last' => 'bar'],
- ],
- ], ['foo.*.name' => ['Required_unless:foo.*.last,foo']]);
- $this->assertTrue($v->passes());
- // nested required_unless passes
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => null, 'last' => 'foo'],
- ['name' => 'second', 'last' => 'foo'],
- ],
- ], ['foo.*.bar.*.name' => ['Required_unless:foo.*.bar.*.last,foo']]);
- $this->assertTrue($v->passes());
- // required_unless fails
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => null, 'last' => 'baz'],
- ['name' => null, 'last' => 'bar'],
- ],
- ], ['foo.*.name' => ['Required_unless:foo.*.last,foo']]);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.name'));
- $this->assertTrue($v->messages()->has('foo.1.name'));
- // nested required_unless fails
- $v = new Validator($trans, [
- 'foo' => [
- [
- 'bar' => [
- ['name' => null, 'last' => 'bar'],
- ['name' => null, 'last' => 'bar'],
- ],
- ],
- ],
- ], ['foo.*.bar.*.name' => ['Required_unless:foo.*.bar.*.last,foo']]);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.bar.0.name'));
- $this->assertTrue($v->messages()->has('foo.0.bar.1.name'));
- }
- public function testValidateImplicitEachWithAsterisksRequiredWith()
- {
- $trans = $this->getIlluminateArrayTranslator();
- // required_with passes
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => 'first', 'last' => 'last'],
- ['name' => 'second', 'last' => 'last'],
- ],
- ], ['foo.*.name' => ['Required_with:foo.*.last']]);
- $this->assertTrue($v->passes());
- // nested required_with passes
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => 'first', 'last' => 'last'],
- ['name' => 'second', 'last' => 'last'],
- ],
- ], ['foo.*.name' => ['Required_with:foo.*.last']]);
- $this->assertTrue($v->passes());
- // required_with fails
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => null, 'last' => 'last'],
- ['name' => null, 'last' => 'last'],
- ],
- ], ['foo.*.name' => ['Required_with:foo.*.last']]);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.name'));
- $this->assertTrue($v->messages()->has('foo.1.name'));
- $v = new Validator($trans, [
- 'fields' => [
- 'fr' => ['name' => '', 'content' => 'ragnar'],
- 'es' => ['name' => '', 'content' => 'lagertha'],
- ],
- ], ['fields.*.name' => 'required_with:fields.*.content']);
- $this->assertFalse($v->passes());
- // nested required_with fails
- $v = new Validator($trans, [
- 'foo' => [
- [
- 'bar' => [
- ['name' => null, 'last' => 'last'],
- ['name' => null, 'last' => 'last'],
- ],
- ],
- ],
- ], ['foo.*.bar.*.name' => ['Required_with:foo.*.bar.*.last']]);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.bar.0.name'));
- $this->assertTrue($v->messages()->has('foo.0.bar.1.name'));
- }
- public function testValidateImplicitEachWithAsterisksRequiredWithAll()
- {
- $trans = $this->getIlluminateArrayTranslator();
- // required_with_all passes
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => 'first', 'last' => 'last', 'middle' => 'middle'],
- ['name' => 'second', 'last' => 'last', 'middle' => 'middle'],
- ],
- ], ['foo.*.name' => ['Required_with_all:foo.*.last,foo.*.middle']]);
- $this->assertTrue($v->passes());
- // nested required_with_all passes
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => 'first', 'last' => 'last', 'middle' => 'middle'],
- ['name' => 'second', 'last' => 'last', 'middle' => 'middle'],
- ],
- ], ['foo.*.name' => ['Required_with_all:foo.*.last,foo.*.middle']]);
- $this->assertTrue($v->passes());
- // required_with_all fails
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => null, 'last' => 'last', 'middle' => 'middle'],
- ['name' => null, 'last' => 'last', 'middle' => 'middle'],
- ],
- ], ['foo.*.name' => ['Required_with_all:foo.*.last,foo.*.middle']]);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.name'));
- $this->assertTrue($v->messages()->has('foo.1.name'));
- // nested required_with_all fails
- $v = new Validator($trans, [
- 'foo' => [
- [
- 'bar' => [
- ['name' => null, 'last' => 'last', 'middle' => 'middle'],
- ['name' => null, 'last' => 'last', 'middle' => 'middle'],
- ],
- ],
- ],
- ], ['foo.*.bar.*.name' => ['Required_with_all:foo.*.bar.*.last,foo.*.bar.*.middle']]);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.bar.0.name'));
- $this->assertTrue($v->messages()->has('foo.0.bar.1.name'));
- }
- public function testValidateImplicitEachWithAsterisksRequiredWithout()
- {
- $trans = $this->getIlluminateArrayTranslator();
- // required_without passes
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => 'first', 'middle' => 'middle'],
- ['name' => 'second', 'last' => 'last'],
- ],
- ], ['foo.*.name' => ['Required_without:foo.*.last,foo.*.middle']]);
- $this->assertTrue($v->passes());
- // nested required_without passes
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => 'first', 'middle' => 'middle'],
- ['name' => 'second', 'last' => 'last'],
- ],
- ], ['foo.*.name' => ['Required_without:foo.*.last,foo.*.middle']]);
- $this->assertTrue($v->passes());
- // required_without fails
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => null, 'last' => 'last'],
- ['name' => null, 'middle' => 'middle'],
- ],
- ], ['foo.*.name' => ['Required_without:foo.*.last,foo.*.middle']]);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.name'));
- $this->assertTrue($v->messages()->has('foo.1.name'));
- // nested required_without fails
- $v = new Validator($trans, [
- 'foo' => [
- [
- 'bar' => [
- ['name' => null, 'last' => 'last'],
- ['name' => null, 'middle' => 'middle'],
- ],
- ],
- ],
- ], ['foo.*.bar.*.name' => ['Required_without:foo.*.bar.*.last,foo.*.bar.*.middle']]);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.bar.0.name'));
- $this->assertTrue($v->messages()->has('foo.0.bar.1.name'));
- }
- public function testValidateImplicitEachWithAsterisksRequiredWithoutAll()
- {
- $trans = $this->getIlluminateArrayTranslator();
- // required_without_all passes
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => 'first'],
- ['name' => null, 'middle' => 'middle'],
- ['name' => null, 'middle' => 'middle', 'last' => 'last'],
- ],
- ], ['foo.*.name' => ['Required_without_all:foo.*.last,foo.*.middle']]);
- $this->assertTrue($v->passes());
- // required_without_all fails
- // nested required_without_all passes
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => 'first'],
- ['name' => null, 'middle' => 'middle'],
- ['name' => null, 'middle' => 'middle', 'last' => 'last'],
- ],
- ], ['foo.*.name' => ['Required_without_all:foo.*.last,foo.*.middle']]);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, [
- 'foo' => [
- ['name' => null, 'foo' => 'foo', 'bar' => 'bar'],
- ['name' => null, 'foo' => 'foo', 'bar' => 'bar'],
- ],
- ], ['foo.*.name' => ['Required_without_all:foo.*.last,foo.*.middle']]);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.name'));
- $this->assertTrue($v->messages()->has('foo.1.name'));
- // nested required_without_all fails
- $v = new Validator($trans, [
- 'foo' => [
- [
- 'bar' => [
- ['name' => null, 'foo' => 'foo', 'bar' => 'bar'],
- ['name' => null, 'foo' => 'foo', 'bar' => 'bar'],
- ],
- ],
- ],
- ], ['foo.*.bar.*.name' => ['Required_without_all:foo.*.bar.*.last,foo.*.bar.*.middle']]);
- $this->assertFalse($v->passes());
- $this->assertTrue($v->messages()->has('foo.0.bar.0.name'));
- $this->assertTrue($v->messages()->has('foo.0.bar.1.name'));
- }
- public function testValidateImplicitEachWithAsterisksBeforeAndAfter()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [
- 'foo' => [
- ['start' => '2016-04-19', 'end' => '2017-04-19'],
- ],
- ], ['foo.*.start' => ['before:foo.*.end']]);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, [
- 'foo' => [
- ['start' => '2016-04-19', 'end' => '2017-04-19'],
- ],
- ], ['foo.*.end' => ['before:foo.*.start']]);
- $this->assertTrue($v->fails());
- $v = new Validator($trans, [
- 'foo' => [
- ['start' => '2016-04-19', 'end' => '2017-04-19'],
- ],
- ], ['foo.*.end' => ['after:foo.*.start']]);
- $this->assertTrue($v->passes());
- $v = new Validator($trans, [
- 'foo' => [
- ['start' => '2016-04-19', 'end' => '2017-04-19'],
- ],
- ], ['foo.*.start' => ['after:foo.*.end']]);
- $this->assertTrue($v->fails());
- }
- public function testGetLeadingExplicitAttributePath()
- {
- $this->assertNull(ValidationData::getLeadingExplicitAttributePath('*.email'));
- $this->assertSame('foo', ValidationData::getLeadingExplicitAttributePath('foo.*'));
- $this->assertSame('foo.bar', ValidationData::getLeadingExplicitAttributePath('foo.bar.*.baz'));
- $this->assertSame('foo.bar.1', ValidationData::getLeadingExplicitAttributePath('foo.bar.1'));
- }
- public function testExtractDataFromPath()
- {
- $data = [['email' => 'mail'], ['email' => 'mail2']];
- $this->assertEquals([['email' => 'mail'], ['email' => 'mail2']], ValidationData::extractDataFromPath(null, $data));
- $data = ['cat' => ['cat1' => ['name']], ['cat2' => ['name2']]];
- $this->assertEquals(['cat' => ['cat1' => ['name']]], ValidationData::extractDataFromPath('cat.cat1', $data));
- $data = ['cat' => ['cat1' => ['name' => '1', 'price' => 1]], ['cat2' => ['name' => 2]]];
- $this->assertEquals(['cat' => ['cat1' => ['name' => '1']]], ValidationData::extractDataFromPath('cat.cat1.name', $data));
- }
- public function testParsingTablesFromModels()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [], []);
- $implicit_no_connection = $v->parseTable(ImplicitTableModel::class);
- $this->assertNull($implicit_no_connection[0]);
- $this->assertSame('implicit_table_models', $implicit_no_connection[1]);
- $explicit_no_connection = $v->parseTable(ExplicitTableModel::class);
- $this->assertNull($explicit_no_connection[0]);
- $this->assertSame('explicits', $explicit_no_connection[1]);
- $explicit_model_with_prefix = $v->parseTable(ExplicitPrefixedTableModel::class);
- $this->assertNull($explicit_model_with_prefix[0]);
- $this->assertSame('prefix.explicits', $explicit_model_with_prefix[1]);
- $explicit_table_with_connection_prefix = $v->parseTable('connection.table');
- $this->assertSame('connection', $explicit_table_with_connection_prefix[0]);
- $this->assertSame('table', $explicit_table_with_connection_prefix[1]);
- $noneloquent_no_connection = $v->parseTable(NonEloquentModel::class);
- $this->assertNull($noneloquent_no_connection[0]);
- $this->assertEquals(NonEloquentModel::class, $noneloquent_no_connection[1]);
- $raw_no_connection = $v->parseTable('table');
- $this->assertNull($raw_no_connection[0]);
- $this->assertSame('table', $raw_no_connection[1]);
- $implicit_connection = $v->parseTable('connection.'.ImplicitTableModel::class);
- $this->assertSame('connection', $implicit_connection[0]);
- $this->assertSame('implicit_table_models', $implicit_connection[1]);
- $explicit_connection = $v->parseTable('connection.'.ExplicitTableModel::class);
- $this->assertSame('connection', $explicit_connection[0]);
- $this->assertSame('explicits', $explicit_connection[1]);
- $explicit_model_implicit_connection = $v->parseTable(ExplicitTableAndConnectionModel::class);
- $this->assertSame('connection', $explicit_model_implicit_connection[0]);
- $this->assertSame('explicits', $explicit_model_implicit_connection[1]);
- $noneloquent_connection = $v->parseTable('connection.'.NonEloquentModel::class);
- $this->assertSame('connection', $noneloquent_connection[0]);
- $this->assertEquals(NonEloquentModel::class, $noneloquent_connection[1]);
- $raw_connection = $v->parseTable('connection.table');
- $this->assertSame('connection', $raw_connection[0]);
- $this->assertSame('table', $raw_connection[1]);
- }
- public function testUsingSettersWithImplicitRules()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => ['a', 'b', 'c']], ['foo.*' => 'string']);
- $v->setData(['foo' => ['a', 'b', 'c', 4]]);
- $this->assertFalse($v->passes());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => ['a', 'b', 'c']], ['foo.*' => 'string']);
- $v->setRules(['foo.*' => 'integer']);
- $this->assertFalse($v->passes());
- }
- public function testInvalidMethod()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans,
- [
- ['name' => 'John'],
- ['name' => null],
- ['name' => ''],
- ],
- [
- '*.name' => 'required',
- ]);
- $this->assertEquals(
- [
- 1 => ['name' => null],
- 2 => ['name' => ''],
- ],
- $v->invalid()
- );
- $v = new Validator($trans,
- [
- 'name' => '',
- ],
- [
- 'name' => 'required',
- ]);
- $this->assertEquals(
- [
- 'name' => '',
- ],
- $v->invalid()
- );
- }
- public function testValidMethod()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans,
- [
- ['name' => 'John'],
- ['name' => null],
- ['name' => ''],
- ['name' => 'Doe'],
- ],
- [
- '*.name' => 'required',
- ]);
- $this->assertEquals(
- [
- 0 => ['name' => 'John'],
- 3 => ['name' => 'Doe'],
- ],
- $v->valid()
- );
- $v = new Validator($trans,
- [
- 'name' => 'Carlos',
- 'age' => 'unknown',
- 'gender' => 'male',
- ],
- [
- 'name' => 'required',
- 'gender' => 'in:male,female',
- 'age' => 'required|int',
- ]);
- $this->assertEquals(
- [
- 'name' => 'Carlos',
- 'gender' => 'male',
- ],
- $v->valid()
- );
- }
- public function testNestedInvalidMethod()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [
- 'testvalid' => 'filled',
- 'testinvalid' => '',
- 'records' => [
- 'ABC123',
- 'ABC122',
- 'ABB132',
- 'ADCD23',
- ],
- ], [
- 'testvalid' => 'filled',
- 'testinvalid' => 'filled',
- 'records.*' => [
- 'required',
- 'regex:/[A-F]{3}[0-9]{3}/',
- ],
- ]);
- $this->assertEquals(
- [
- 'testinvalid' => '',
- 'records' => [
- 3 => 'ADCD23',
- ],
- ],
- $v->invalid()
- );
- }
- public function testMultipleFileUploads()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $file = new File(__FILE__, false);
- $file2 = new File(__FILE__, false);
- $v = new Validator($trans, ['file' => [$file, $file2]], ['file.*' => 'Required|mimes:xls']);
- $this->assertFalse($v->passes());
- }
- public function testFileUploads()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $file = new File(__FILE__, false);
- $v = new Validator($trans, ['file' => $file], ['file' => 'Required|mimes:xls']);
- $this->assertFalse($v->passes());
- }
- public function testCustomValidationObject()
- {
- // Test passing case...
- $v = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['name' => 'taylor'],
- [
- 'name' => new class implements Rule
- {
- public function passes($attribute, $value)
- {
- return $value === 'taylor';
- }
- public function message()
- {
- return ':attribute must be taylor';
- }
- },
- ]
- );
- $this->assertTrue($v->passes());
- // Test failing case...
- $v = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['name' => 'adam'],
- [
- 'name' => [
- new class implements Rule
- {
- public function passes($attribute, $value)
- {
- return $value === 'taylor';
- }
- public function message()
- {
- return ':attribute must be taylor';
- }
- },
- ],
- ]
- );
- $this->assertTrue($v->fails());
- $this->assertSame('name must be taylor', $v->errors()->all()[0]);
- // Test passing case with Closure...
- $v = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['name' => 'taylor'],
- [
- 'name.*' => function ($attribute, $value, $fail) {
- if ($value !== 'taylor') {
- $fail(':attribute was '.$value.' instead of taylor');
- }
- },
- ]
- );
- $this->assertTrue($v->passes());
- // Test failing case with Closure...
- $v = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['name' => 'adam'],
- [
- 'name' => function ($attribute, $value, $fail) {
- if ($value !== 'taylor') {
- $fail(':attribute was '.$value.' instead of taylor');
- }
- },
- ]
- );
- $this->assertTrue($v->fails());
- $this->assertSame('name was adam instead of taylor', $v->errors()->all()[0]);
- // Test complex failing case...
- $v = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['name' => 'taylor', 'states' => ['AR', 'TX'], 'number' => 9],
- [
- 'states.*' => new class implements Rule
- {
- public function passes($attribute, $value)
- {
- return in_array($value, ['AK', 'HI']);
- }
- public function message()
- {
- return ':attribute must be AR or TX';
- }
- },
- 'name' => function ($attribute, $value, $fail) {
- if ($value !== 'taylor') {
- $fail(':attribute must be taylor');
- }
- },
- 'number' => [
- 'required',
- 'integer',
- function ($attribute, $value, $fail) {
- if ($value % 4 !== 0) {
- $fail(':attribute must be divisible by 4');
- }
- },
- ],
- ]
- );
- $this->assertFalse($v->passes());
- $this->assertSame('states.0 must be AR or TX', $v->errors()->get('states.0')[0]);
- $this->assertSame('states.1 must be AR or TX', $v->errors()->get('states.1')[0]);
- $this->assertSame('number must be divisible by 4', $v->errors()->get('number')[0]);
- // Test array of messages with failing case...
- $v = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['name' => 42],
- [
- 'name' => new class implements Rule
- {
- public function passes($attribute, $value)
- {
- return $value === 'taylor';
- }
- public function message()
- {
- return [':attribute must be taylor', ':attribute must be a first name'];
- }
- },
- ]
- );
- $this->assertTrue($v->fails());
- $this->assertSame('name must be taylor', $v->errors()->get('name')[0]);
- $this->assertSame('name must be a first name', $v->errors()->get('name')[1]);
- // Test array of messages with multiple rules for one attribute case...
- $v = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['name' => 42],
- [
- 'name' => [
- new class implements Rule
- {
- public function passes($attribute, $value)
- {
- return $value === 'taylor';
- }
- public function message()
- {
- return [':attribute must be taylor', ':attribute must be a first name'];
- }
- }, 'string',
- ],
- ]
- );
- $this->assertTrue($v->fails());
- $this->assertSame('name must be taylor', $v->errors()->get('name')[0]);
- $this->assertSame('name must be a first name', $v->errors()->get('name')[1]);
- $this->assertSame('validation.string', $v->errors()->get('name')[2]);
- // Test access to the validator data
- $v = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['password' => 'foo', 'password_confirmation' => 'foo'],
- [
- 'password' => [
- new class implements Rule, DataAwareRule
- {
- protected $data;
- public function setData($data)
- {
- $this->data = $data;
- }
- public function passes($attribute, $value)
- {
- return $value === $this->data['password_confirmation'];
- }
- public function message()
- {
- return ['The :attribute confirmation does not match.'];
- }
- }, 'string',
- ],
- ]
- );
- $this->assertTrue($v->passes());
- $v = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['password' => 'foo', 'password_confirmation' => 'bar'],
- [
- 'password' => [
- new class implements Rule, DataAwareRule
- {
- protected $data;
- public function setData($data)
- {
- $this->data = $data;
- }
- public function passes($attribute, $value)
- {
- return $value === $this->data['password_confirmation'];
- }
- public function message()
- {
- return ['The :attribute confirmation does not match.'];
- }
- }, 'string',
- ],
- ]
- );
- $this->assertTrue($v->fails());
- $this->assertSame('The password confirmation does not match.', $v->errors()->get('password')[0]);
- // Test access to the validator
- $v = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['base' => 21, 'double' => 42],
- [
- 'base' => ['integer'],
- 'double' => [
- 'integer',
- new class implements Rule, ValidatorAwareRule
- {
- protected $validator;
- public function setValidator($validator)
- {
- $this->validator = $validator;
- }
- public function passes($attribute, $value)
- {
- if ($this->validator->errors()->hasAny(['base', $attribute])) {
- return true;
- }
- return $value === 2 * $this->validator->getData()['base'];
- }
- public function message()
- {
- return ['The :attribute must be the double of base.'];
- }
- },
- ],
- ]
- );
- $this->assertTrue($v->passes());
- $v = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['base' => 21, 'double' => 10],
- [
- 'base' => ['integer'],
- 'double' => [
- 'integer',
- new class implements Rule, ValidatorAwareRule
- {
- protected $validator;
- public function setValidator($validator)
- {
- $this->validator = $validator;
- }
- public function passes($attribute, $value)
- {
- if ($this->validator->errors()->hasAny(['base', $attribute])) {
- return true;
- }
- return $value === 2 * $this->validator->getData()['base'];
- }
- public function message()
- {
- return ['The :attribute must be the double of base.'];
- }
- },
- ],
- ]
- );
- $this->assertTrue($v->fails());
- $this->assertSame('The double must be the double of base.', $v->errors()->get('double')[0]);
- $v = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['base' => 21, 'double' => 'foo'],
- [
- 'base' => ['integer'],
- 'double' => [
- 'integer',
- new class implements Rule, ValidatorAwareRule
- {
- protected $validator;
- public function setValidator($validator)
- {
- $this->validator = $validator;
- }
- public function passes($attribute, $value)
- {
- if ($this->validator->errors()->hasAny(['base', $attribute])) {
- return true;
- }
- return $value === 2 * $this->validator->getData()['base'];
- }
- public function message()
- {
- return ['The :attribute must be the double of base.'];
- }
- },
- ],
- ]
- );
- $this->assertTrue($v->fails());
- $this->assertCount(1, $v->errors()->get('double'));
- $this->assertSame('validation.integer', $v->errors()->get('double')[0]);
- }
- public function testCustomValidationObjectWithDotKeysIsCorrectlyPassedValue()
- {
- $v = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['foo' => ['foo.bar' => 'baz']],
- [
- 'foo' => new class implements Rule
- {
- public function passes($attribute, $value)
- {
- return $value === ['foo.bar' => 'baz'];
- }
- public function message()
- {
- return ':attribute must be baz';
- }
- },
- ]
- );
- $this->assertTrue($v->passes());
- // Test failed attributes contains proper entries
- $v = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['foo' => ['foo.bar' => 'baz']],
- [
- 'foo.foo\.bar' => new class implements Rule
- {
- public function passes($attribute, $value)
- {
- return false;
- }
- public function message()
- {
- return ':attribute must be baz';
- }
- },
- ]
- );
- $this->assertFalse($v->passes());
- $this->assertIsArray($v->failed()['foo.foo.bar']);
- }
- public function testImplicitCustomValidationObjects()
- {
- // Test passing case...
- $v = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['name' => ''],
- [
- 'name' => $rule = new class implements ImplicitRule
- {
- public $called = false;
- public function passes($attribute, $value)
- {
- $this->called = true;
- return true;
- }
- public function message()
- {
- return 'message';
- }
- },
- ]
- );
- $this->assertTrue($v->passes());
- $this->assertTrue($rule->called);
- }
- public function testValidateReturnsValidatedData()
- {
- $post = ['first' => 'john', 'preferred' => 'john', 'last' => 'doe', 'type' => 'admin'];
- $v = new Validator($this->getIlluminateArrayTranslator(), $post, ['first' => 'required', 'preferred' => 'required']);
- $v->sometimes('type', 'required', function () {
- return false;
- });
- $data = $v->validate();
- $this->assertEquals(['first' => 'john', 'preferred' => 'john'], $data);
- }
- public function testValidateReturnsValidatedDataNestedRules()
- {
- $post = ['nested' => ['foo' => 'bar', 'baz' => ''], 'array' => [1, 2]];
- $rules = ['nested.foo' => 'required', 'array.*' => 'integer'];
- $v = new Validator($this->getIlluminateArrayTranslator(), $post, $rules);
- $v->sometimes('type', 'required', function () {
- return false;
- });
- $data = $v->validate();
- $this->assertEquals(['nested' => ['foo' => 'bar'], 'array' => [1, 2]], $data);
- }
- public function testValidateReturnsValidatedDataNestedChildRules()
- {
- $post = ['nested' => ['foo' => 'bar', 'with' => 'extras', 'type' => 'admin']];
- $v = new Validator($this->getIlluminateArrayTranslator(), $post, ['nested.foo' => 'required']);
- $v->sometimes('nested.type', 'required', function () {
- return false;
- });
- $data = $v->validate();
- $this->assertEquals(['nested' => ['foo' => 'bar']], $data);
- }
- public function testValidateReturnsValidatedDataNestedArrayRules()
- {
- $post = ['nested' => [['bar' => 'baz', 'with' => 'extras', 'type' => 'admin'], ['bar' => 'baz2', 'with' => 'extras', 'type' => 'admin']]];
- $v = new Validator($this->getIlluminateArrayTranslator(), $post, ['nested.*.bar' => 'required']);
- $v->sometimes('nested.*.type', 'required', function () {
- return false;
- });
- $data = $v->validate();
- $this->assertEquals(['nested' => [['bar' => 'baz'], ['bar' => 'baz2']]], $data);
- }
- public function testValidateAndValidatedData()
- {
- $post = ['first' => 'john', 'preferred' => 'john', 'last' => 'doe', 'type' => 'admin'];
- $v = new Validator($this->getIlluminateArrayTranslator(), $post, ['first' => 'required', 'preferred' => 'required']);
- $v->sometimes('type', 'required', function () {
- return false;
- });
- $data = $v->validate();
- $validatedData = $v->validated();
- $this->assertEquals(['first' => 'john', 'preferred' => 'john'], $data);
- $this->assertEquals($data, $validatedData);
- }
- public function testValidatedNotValidateTwiceData()
- {
- $post = ['first' => 'john', 'preferred' => 'john', 'last' => 'doe', 'type' => 'admin'];
- $validateCount = 0;
- $v = new Validator($this->getIlluminateArrayTranslator(), $post, ['first' => 'required', 'preferred' => 'required']);
- $v->after(function () use (&$validateCount) {
- $validateCount++;
- });
- $data = $v->validate();
- $v->validated();
- $this->assertEquals(['first' => 'john', 'preferred' => 'john'], $data);
- $this->assertEquals(1, $validateCount);
- }
- public function testMultiplePassesCalls()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, [], ['foo' => 'string|required']);
- $this->assertFalse($v->passes());
- $this->assertFalse($v->passes());
- }
- /**
- * @dataProvider validUuidList
- */
- public function testValidateWithValidUuid($uuid)
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => $uuid], ['foo' => 'uuid']);
- $this->assertTrue($v->passes());
- }
- /**
- * @dataProvider invalidUuidList
- */
- public function testValidateWithInvalidUuid($uuid)
- {
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['foo' => $uuid], ['foo' => 'uuid']);
- $this->assertFalse($v->passes());
- }
- public function validUuidList()
- {
- return [
- ['a0a2a2d2-0b87-4a18-83f2-2529882be2de'],
- ['145a1e72-d11d-11e8-a8d5-f2801f1b9fd1'],
- ['00000000-0000-0000-0000-000000000000'],
- ['e60d3f48-95d7-4d8d-aad0-856f29a27da2'],
- ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66'],
- ['ff6f8cb0-c57d-21e1-9b21-0800200c9a66'],
- ['ff6f8cb0-c57d-31e1-9b21-0800200c9a66'],
- ['ff6f8cb0-c57d-41e1-9b21-0800200c9a66'],
- ['ff6f8cb0-c57d-51e1-9b21-0800200c9a66'],
- ['FF6F8CB0-C57D-11E1-9B21-0800200C9A66'],
- ];
- }
- public function invalidUuidList()
- {
- return [
- ['not a valid uuid so we can test this'],
- ['zf6f8cb0-c57d-11e1-9b21-0800200c9a66'],
- ['145a1e72-d11d-11e8-a8d5-f2801f1b9fd1'.PHP_EOL],
- ['145a1e72-d11d-11e8-a8d5-f2801f1b9fd1 '],
- [' 145a1e72-d11d-11e8-a8d5-f2801f1b9fd1'],
- ['145a1e72-d11d-11e8-a8d5-f2z01f1b9fd1'],
- ['3f6f8cb0-c57d-11e1-9b21-0800200c9a6'],
- ['af6f8cb-c57d-11e1-9b21-0800200c9a66'],
- ['af6f8cb0c57d11e19b210800200c9a66'],
- ['ff6f8cb0-c57da-51e1-9b21-0800200c9a66'],
- ];
- }
- public function providesPassingExcludeIfData()
- {
- return [
- [
- [
- 'has_appointment' => ['required', 'bool'],
- 'appointment_date' => ['exclude_if:has_appointment,false', 'required', 'date'],
- ], [
- 'has_appointment' => false,
- 'appointment_date' => 'should be excluded',
- ], [
- 'has_appointment' => false,
- ],
- ],
- [
- [
- 'cat' => ['required', 'string'],
- 'mouse' => ['exclude_if:cat,Tom', 'required', 'file'],
- ], [
- 'cat' => 'Tom',
- 'mouse' => 'should be excluded',
- ], [
- 'cat' => 'Tom',
- ],
- ],
- [
- [
- 'has_appointment' => ['required', 'bool'],
- 'appointment_date' => ['exclude_if:has_appointment,false', 'required', 'date'],
- ], [
- 'has_appointment' => false,
- ], [
- 'has_appointment' => false,
- ],
- ],
- [
- [
- 'has_appointment' => ['nullable', 'bool'],
- 'appointment_date' => ['exclude_if:has_appointment,null', 'required', 'date'],
- ],
- [
- 'has_appointment' => true,
- 'appointment_date' => '2021-03-08',
- ],
- [
- 'has_appointment' => true,
- 'appointment_date' => '2021-03-08',
- ],
- ],
- [
- [
- 'has_appointment' => ['required', 'bool'],
- 'appointment_date' => ['exclude_if:has_appointment,false', 'required', 'date'],
- ], [
- 'has_appointment' => true,
- 'appointment_date' => '2019-12-13',
- ], [
- 'has_appointment' => true,
- 'appointment_date' => '2019-12-13',
- ],
- ],
- [
- [
- 'has_no_appointments' => ['required', 'bool'],
- 'has_doctor_appointment' => ['exclude_if:has_no_appointments,true', 'required', 'bool'],
- 'doctor_appointment_date' => ['exclude_if:has_no_appointments,true', 'exclude_if:has_doctor_appointment,false', 'required', 'date'],
- ], [
- 'has_no_appointments' => true,
- 'has_doctor_appointment' => true,
- 'doctor_appointment_date' => '2019-12-13',
- ], [
- 'has_no_appointments' => true,
- ],
- ],
- [
- [
- 'has_no_appointments' => ['required', 'bool'],
- 'has_doctor_appointment' => ['exclude_if:has_no_appointments,true', 'required', 'bool'],
- 'doctor_appointment_date' => ['exclude_if:has_no_appointments,true', 'exclude_if:has_doctor_appointment,false', 'required', 'date'],
- ], [
- 'has_no_appointments' => false,
- 'has_doctor_appointment' => false,
- 'doctor_appointment_date' => 'should be excluded',
- ], [
- 'has_no_appointments' => false,
- 'has_doctor_appointment' => false,
- ],
- ],
- 'nested-01' => [
- [
- 'has_appointments' => ['required', 'bool'],
- 'appointments.*' => ['exclude_if:has_appointments,false', 'required', 'date'],
- ], [
- 'has_appointments' => false,
- 'appointments' => ['2019-05-15', '2020-05-15'],
- ], [
- 'has_appointments' => false,
- ],
- ],
- 'nested-02' => [
- [
- 'has_appointments' => ['required', 'bool'],
- 'appointments.*.date' => ['exclude_if:has_appointments,false', 'required', 'date'],
- 'appointments.*.name' => ['exclude_if:has_appointments,false', 'required', 'string'],
- ], [
- 'has_appointments' => false,
- 'appointments' => [
- ['date' => 'should be excluded', 'name' => 'should be excluded'],
- ],
- ], [
- 'has_appointments' => false,
- ],
- ],
- 'nested-03' => [
- [
- 'has_appointments' => ['required', 'bool'],
- 'appointments' => ['exclude_if:has_appointments,false', 'required', 'array'],
- 'appointments.*.date' => ['required', 'date'],
- 'appointments.*.name' => ['required', 'string'],
- ], [
- 'has_appointments' => false,
- 'appointments' => [
- ['date' => 'should be excluded', 'name' => 'should be excluded'],
- ],
- ], [
- 'has_appointments' => false,
- ],
- ],
- 'nested-04' => [
- [
- 'has_appointments' => ['required', 'bool'],
- 'appointments.*.date' => ['required', 'date'],
- 'appointments' => ['exclude_if:has_appointments,false', 'required', 'array'],
- ], [
- 'has_appointments' => false,
- 'appointments' => [
- ['date' => 'should be excluded', 'name' => 'should be excluded'],
- ],
- ], [
- 'has_appointments' => false,
- ],
- ],
- 'nested-05' => [
- [
- 'vehicles.*.type' => 'required|in:car,boat',
- 'vehicles.*.wheels' => 'exclude_if:vehicles.*.type,boat|required|numeric',
- ], [
- 'vehicles' => [
- ['type' => 'car', 'wheels' => 4],
- ['type' => 'boat', 'wheels' => 'should be excluded'],
- ],
- ], [
- 'vehicles' => [
- ['type' => 'car', 'wheels' => 4],
- ['type' => 'boat'],
- ],
- ],
- ],
- 'nested-06' => [
- [
- 'vehicles.*.type' => 'required|in:car,boat',
- 'vehicles.*.wheels' => 'exclude_if:vehicles.*.type,boat|required|numeric',
- ], [
- 'vehicles' => [
- ['type' => 'car', 'wheels' => 4],
- ['type' => 'boat'],
- ],
- ], [
- 'vehicles' => [
- ['type' => 'car', 'wheels' => 4],
- ['type' => 'boat'],
- ],
- ],
- ],
- 'nested-07' => [
- [
- 'vehicles.*.type' => 'required|in:car,boat',
- 'vehicles.*.wheels' => 'exclude_if:vehicles.*.type,boat|required|array',
- 'vehicles.*.wheels.*.color' => 'required|in:red,blue',
- // In this bizzaro world example you can choose a custom shape for your wheels if they are red
- 'vehicles.*.wheels.*.shape' => 'exclude_unless:vehicles.*.wheels.*.color,red|required|in:square,round',
- ], [
- 'vehicles' => [
- [
- 'type' => 'car', 'wheels' => [
- ['color' => 'red', 'shape' => 'square'],
- ['color' => 'blue', 'shape' => 'hexagon'],
- ['color' => 'red', 'shape' => 'round', 'junk' => 'no rule, still present'],
- ['color' => 'blue', 'shape' => 'triangle'],
- ],
- ],
- ['type' => 'boat'],
- ],
- ], [
- 'vehicles' => [
- [
- 'type' => 'car', 'wheels' => [
- ['color' => 'red', 'shape' => 'square'],
- ['color' => 'blue'],
- ['color' => 'red', 'shape' => 'round', 'junk' => 'no rule, still present'],
- ['color' => 'blue'],
- ],
- ],
- ['type' => 'boat'],
- ],
- ],
- ],
- ];
- }
- /**
- * @dataProvider providesPassingExcludeIfData
- */
- public function testExcludeIf($rules, $data, $expectedValidatedData)
- {
- $validator = new Validator(
- $this->getIlluminateArrayTranslator(),
- $data,
- $rules
- );
- $passes = $validator->passes();
- if (! $passes) {
- $message = sprintf("Validation unexpectedly failed:\nRules: %s\nData: %s\nValidation error: %s",
- json_encode($rules, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES),
- json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES),
- json_encode($validator->messages()->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
- );
- }
- $this->assertTrue($passes, $message ?? '');
- $this->assertSame($expectedValidatedData, $validator->validated());
- }
- public function providesFailingExcludeIfData()
- {
- return [
- [
- [
- 'has_appointment' => ['required', 'bool'],
- 'appointment_date' => ['exclude_if:has_appointment,false', 'required', 'date'],
- ], [
- 'has_appointment' => true,
- ], [
- 'appointment_date' => ['validation.required'],
- ],
- ],
- [
- [
- 'cat' => ['required', 'string'],
- 'mouse' => ['exclude_if:cat,Tom', 'required', 'file'],
- ], [
- 'cat' => 'Bob',
- 'mouse' => 'not a file',
- ], [
- 'mouse' => ['validation.file'],
- ],
- ],
- [
- [
- 'has_appointments' => ['required', 'bool'],
- 'appointments' => ['exclude_if:has_appointments,false', 'required', 'array'],
- 'appointments.*.date' => ['required', 'date'],
- 'appointments.*.name' => ['required', 'string'],
- ], [
- 'has_appointments' => true,
- 'appointments' => [
- ['date' => 'invalid', 'name' => 'Bob'],
- ['date' => '2019-05-15'],
- ],
- ], [
- 'appointments.0.date' => ['validation.date'],
- 'appointments.1.name' => ['validation.required'],
- ],
- ],
- [
- [
- 'vehicles.*.price' => 'required|numeric',
- 'vehicles.*.type' => 'required|in:car,boat',
- 'vehicles.*.wheels' => 'exclude_if:vehicles.*.type,boat|required|numeric',
- ], [
- 'vehicles' => [
- [
- 'price' => 100,
- 'type' => 'car',
- ],
- [
- 'price' => 500,
- 'type' => 'boat',
- ],
- ],
- ], [
- 'vehicles.0.wheels' => ['validation.required'],
- // vehicles.1.wheels is not required, because type is not "car"
- ],
- ],
- 'exclude-validation-error-01' => [
- [
- 'vehicles.*.type' => 'required|in:car,boat',
- 'vehicles.*.wheels' => 'exclude_if:vehicles.*.type,boat|required|array',
- 'vehicles.*.wheels.*.color' => 'required|in:red,blue',
- // In this bizzaro world example you can choose a custom shape for your wheels if they are red
- 'vehicles.*.wheels.*.shape' => 'exclude_unless:vehicles.*.wheels.*.color,red|required|in:square,round',
- ], [
- 'vehicles' => [
- [
- 'type' => 'car', 'wheels' => [
- ['color' => 'red', 'shape' => 'square'],
- ['color' => 'blue', 'shape' => 'hexagon'],
- ['color' => 'red', 'shape' => 'hexagon'],
- ['color' => 'blue', 'shape' => 'triangle'],
- ],
- ],
- ['type' => 'boat', 'wheels' => 'should be excluded'],
- ],
- ], [
- // The blue wheels are excluded and are therefor not validated against the "in:square,round" rule
- 'vehicles.0.wheels.2.shape' => ['validation.in'],
- ],
- ],
- ];
- }
- /**
- * @dataProvider providesFailingExcludeIfData
- */
- public function testExcludeIfWhenValidationFails($rules, $data, $expectedMessages)
- {
- $validator = new Validator(
- $this->getIlluminateArrayTranslator(),
- $data,
- $rules
- );
- $fails = $validator->fails();
- if (! $fails) {
- $message = sprintf("Validation unexpectedly passed:\nRules: %s\nData: %s",
- json_encode($rules, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES),
- json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
- );
- }
- $this->assertTrue($fails, $message ?? '');
- $this->assertSame($expectedMessages, $validator->messages()->toArray());
- }
- public function providesPassingExcludeData()
- {
- return [
- [
- [
- 'has_appointment' => ['required', 'bool'],
- 'appointment_date' => ['exclude'],
- ], [
- 'has_appointment' => false,
- 'appointment_date' => 'should be excluded',
- ], [
- 'has_appointment' => false,
- ],
- ],
- ];
- }
- /**
- * @dataProvider providesPassingExcludeData
- */
- public function testExclude($rules, $data, $expectedValidatedData)
- {
- $validator = new Validator(
- $this->getIlluminateArrayTranslator(),
- $data,
- $rules
- );
- $passes = $validator->passes();
- if (! $passes) {
- $message = sprintf("Validation unexpectedly failed:\nRules: %s\nData: %s\nValidation error: %s",
- json_encode($rules, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES),
- json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES),
- json_encode($validator->messages()->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
- );
- }
- $this->assertTrue($passes, $message ?? '');
- $this->assertSame($expectedValidatedData, $validator->validated());
- }
- public function testExcludingArrays()
- {
- $validator = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['users' => [['name' => 'Mohamed', 'location' => 'cairo']]],
- ['users' => 'array', 'users.*.name' => 'string']
- );
- $this->assertTrue($validator->passes());
- $this->assertSame(['users' => [['name' => 'Mohamed', 'location' => 'cairo']]], $validator->validated());
- $validator = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['users' => [['name' => 'Mohamed', 'location' => 'cairo']]],
- ['users' => 'array', 'users.*.name' => 'string']
- );
- $validator->excludeUnvalidatedArrayKeys = true;
- $this->assertTrue($validator->passes());
- $this->assertSame(['users' => [['name' => 'Mohamed']]], $validator->validated());
- $validator = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['admin' => ['name' => 'Mohamed', 'location' => 'cairo'], 'users' => [['name' => 'Mohamed', 'location' => 'cairo']]],
- ['admin' => 'array', 'admin.name' => 'string', 'users' => 'array', 'users.*.name' => 'string']
- );
- $validator->excludeUnvalidatedArrayKeys = true;
- $this->assertTrue($validator->passes());
- $this->assertSame(['admin' => ['name' => 'Mohamed'], 'users' => [['name' => 'Mohamed']]], $validator->validated());
- $validator = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['users' => [['name' => 'Mohamed', 'location' => 'cairo']]],
- ['users' => 'array']
- );
- $validator->excludeUnvalidatedArrayKeys = true;
- $this->assertTrue($validator->passes());
- $this->assertSame(['users' => [['name' => 'Mohamed', 'location' => 'cairo']]], $validator->validated());
- $validator = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['users' => ['mohamed', 'zain']],
- ['users' => 'array', 'users.*' => 'string']
- );
- $validator->excludeUnvalidatedArrayKeys = true;
- $this->assertTrue($validator->passes());
- $this->assertSame(['users' => ['mohamed', 'zain']], $validator->validated());
- $validator = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['users' => ['admins' => [['name' => 'mohamed', 'job' => 'dev']], 'unvalidated' => 'foobar']],
- ['users' => 'array', 'users.admins' => 'array', 'users.admins.*.name' => 'string']
- );
- $validator->excludeUnvalidatedArrayKeys = true;
- $this->assertTrue($validator->passes());
- $this->assertSame(['users' => ['admins' => [['name' => 'mohamed']]]], $validator->validated());
- $validator = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['users' => [1, 2, 3]],
- ['users' => 'array|max:10']
- );
- $validator->excludeUnvalidatedArrayKeys = true;
- $this->assertTrue($validator->passes());
- $this->assertSame(['users' => [1, 2, 3]], $validator->validated());
- }
- public function testExcludeUnless()
- {
- $validator = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['cat' => 'Felix', 'mouse' => 'Jerry'],
- ['cat' => 'required|string', 'mouse' => 'exclude_unless:cat,Tom|required|string']
- );
- $this->assertTrue($validator->passes());
- $this->assertSame(['cat' => 'Felix'], $validator->validated());
- $validator = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['cat' => 'Felix'],
- ['cat' => 'required|string', 'mouse' => 'exclude_unless:cat,Tom|required|string']
- );
- $this->assertTrue($validator->passes());
- $this->assertSame(['cat' => 'Felix'], $validator->validated());
- $validator = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['cat' => 'Tom', 'mouse' => 'Jerry'],
- ['cat' => 'required|string', 'mouse' => 'exclude_unless:cat,Tom|required|string']
- );
- $this->assertTrue($validator->passes());
- $this->assertSame(['cat' => 'Tom', 'mouse' => 'Jerry'], $validator->validated());
- $validator = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['cat' => 'Tom'],
- ['cat' => 'required|string', 'mouse' => 'exclude_unless:cat,Tom|required|string']
- );
- $this->assertTrue($validator->fails());
- $this->assertSame(['mouse' => ['validation.required']], $validator->messages()->toArray());
- $validator = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['foo' => true, 'bar' => 'baz'],
- ['foo' => 'nullable', 'bar' => 'exclude_unless:foo,null']
- );
- $this->assertTrue($validator->passes());
- $this->assertSame(['foo' => true], $validator->validated());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['bar' => 'Hello'], ['bar' => 'exclude_unless:foo,true']);
- $this->assertTrue($v->passes());
- $this->assertSame([], $v->validated());
- $trans = $this->getIlluminateArrayTranslator();
- $v = new Validator($trans, ['bar' => 'Hello'], ['bar' => 'exclude_unless:foo,null']);
- $this->assertTrue($v->passes());
- $this->assertSame(['bar' => 'Hello'], $v->validated());
- }
- public function testExcludeWithout()
- {
- $validator = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['region' => 'South'],
- [
- 'country' => 'exclude_without:region|nullable|required_with:region|string|min:3',
- 'region' => 'exclude_without:country|nullable|required_with:country|string|min:3',
- ]
- );
- $this->assertTrue($validator->fails());
- $this->assertSame(['country' => ['validation.required_with']], $validator->messages()->toArray());
- }
- public function testExcludeValuesAreReallyRemoved()
- {
- $validator = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['cat' => 'Tom', 'mouse' => 'Jerry'],
- ['cat' => 'required|string', 'mouse' => 'exclude_if:cat,Tom|required|string']
- );
- $this->assertTrue($validator->passes());
- $this->assertSame(['cat' => 'Tom'], $validator->validated());
- $this->assertSame(['cat' => 'Tom'], $validator->valid());
- $this->assertSame([], $validator->invalid());
- $validator = new Validator(
- $this->getIlluminateArrayTranslator(),
- ['cat' => 'Tom', 'mouse' => null],
- ['cat' => 'required|string', 'mouse' => 'exclude_if:cat,Felix|required|string']
- );
- $this->assertTrue($validator->fails());
- $this->assertSame(['cat' => 'Tom'], $validator->valid());
- $this->assertSame(['mouse' => null], $validator->invalid());
- }
- public function testValidateFailsWithAsterisksAsDataKeys()
- {
- $post = ['data' => [0 => ['date' => '2019-01-24'], 1 => ['date' => 'blah'], '*' => ['date' => 'blah']]];
- $rules = ['data.*.date' => 'required|date'];
- $validator = new Validator($this->getIlluminateArrayTranslator(), $post, $rules);
- $this->assertTrue($validator->fails());
- $this->assertSame(['data.1.date' => ['validation.date'], 'data.*.date' => ['validation.date']], $validator->messages()->toArray());
- }
- public function testFailOnFirstError()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $data = [
- 'foo' => 'bar',
- 'age' => 30,
- ];
- $rules = [
- 'foo' => ['required', 'string'],
- 'baz' => ['required'],
- 'age' => ['required', 'min:31'],
- ];
- $expectedFailOnFirstErrorDisableResult = [
- 'baz' => [
- 'validation.required',
- ],
- 'age' => [
- 'validation.min.string',
- ],
- ];
- $failOnFirstErrorDisable = new Validator($trans, $data, $rules);
- $this->assertFalse($failOnFirstErrorDisable->passes());
- $this->assertEquals($expectedFailOnFirstErrorDisableResult, $failOnFirstErrorDisable->getMessageBag()->getMessages());
- $expectedFailOnFirstErrorEnableResult = [
- 'baz' => [
- 'validation.required',
- ],
- ];
- $failOnFirstErrorEnable = new Validator($trans, $data, $rules, [], []);
- $failOnFirstErrorEnable->stopOnFirstFailure();
- $this->assertFalse($failOnFirstErrorEnable->passes());
- $this->assertEquals($expectedFailOnFirstErrorEnableResult, $failOnFirstErrorEnable->getMessageBag()->getMessages());
- }
- public function testArrayKeysValidationPassedWhenHasKeys()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $data = [
- 'baz' => [
- 'foo' => 'bar',
- 'fee' => 'faa',
- 'laa' => 'lee',
- ],
- ];
- $rules = [
- 'baz' => [
- 'array',
- 'required_array_keys:foo,fee,laa',
- ],
- ];
- $validator = new Validator($trans, $data, $rules, [], []);
- $this->assertTrue($validator->passes());
- }
- public function testArrayKeysValidationPassedWithPartialMatch()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $data = [
- 'baz' => [
- 'foo' => 'bar',
- 'fee' => 'faa',
- 'laa' => 'lee',
- ],
- ];
- $rules = [
- 'baz' => [
- 'array',
- 'required_array_keys:foo,fee',
- ],
- ];
- $validator = new Validator($trans, $data, $rules, [], []);
- $this->assertTrue($validator->passes());
- }
- public function testArrayKeysValidationFailsWithMissingKey()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required_array_keys' => 'The :attribute field must contain entries for :values'], 'en');
- $data = [
- 'baz' => [
- 'foo' => 'bar',
- 'fee' => 'faa',
- 'laa' => 'lee',
- ],
- ];
- $rules = [
- 'baz' => [
- 'array',
- 'required_array_keys:foo,fee,boo,bar',
- ],
- ];
- $validator = new Validator($trans, $data, $rules, [], []);
- $this->assertFalse($validator->passes());
- $this->assertSame(
- 'The baz field must contain entries for foo, fee, boo, bar',
- $validator->messages()->first('baz')
- );
- }
- public function testArrayKeysValidationFailsWithNotAnArray()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $trans->addLines(['validation.required_array_keys' => 'The :attribute field must contain entries for :values'], 'en');
- $data = [
- 'baz' => 'no an array',
- ];
- $rules = [
- 'baz' => [
- 'required_array_keys:foo,fee,boo,bar',
- ],
- ];
- $validator = new Validator($trans, $data, $rules, [], []);
- $this->assertFalse($validator->passes());
- $this->assertSame(
- 'The baz field must contain entries for foo, fee, boo, bar',
- $validator->messages()->first('baz')
- );
- }
- public function testArrayKeysWithDotIntegerMin()
- {
- $trans = $this->getIlluminateArrayTranslator();
- $data = [
- 'foo.bar' => -1,
- ];
- $rules = [
- 'foo\.bar' => 'integer|min:1',
- ];
- $expectedResult = [
- 'foo.bar' => [
- 'validation.min.numeric',
- ],
- ];
- $validator = new Validator($trans, $data, $rules, [], []);
- $this->assertEquals($expectedResult, $validator->getMessageBag()->getMessages());
- }
- protected function getTranslator()
- {
- return m::mock(TranslatorContract::class);
- }
- public function getIlluminateArrayTranslator()
- {
- return new Translator(
- new ArrayLoader, 'en'
- );
- }
- }
- class ImplicitTableModel extends Model
- {
- protected $guarded = [];
- public $timestamps = false;
- }
- class ExplicitTableModel extends Model
- {
- protected $table = 'explicits';
- protected $guarded = [];
- public $timestamps = false;
- }
- class ExplicitPrefixedTableModel extends Model
- {
- protected $table = 'prefix.explicits';
- protected $guarded = [];
- public $timestamps = false;
- }
- class ExplicitTableAndConnectionModel extends Model
- {
- protected $table = 'explicits';
- protected $connection = 'connection';
- protected $guarded = [];
- public $timestamps = false;
- }
- class NonEloquentModel
- {
- }
|