-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRecordsTester.cfc
1084 lines (877 loc) · 34.8 KB
/
RecordsTester.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!--- 1.0 Beta 1 (Build 4) --->
<!--- Last Updated: 2010-11-23 --->
<!--- Created by Steve Bryant 2009-07-14 --->
<cfcomponent displayname="Records" extends="mxunit.framework.TestCase">
<cfinclude template="udfs.cfm">
<cfset request["isTesting"] = true>
<cfset Variables.sObserverChecks = {}>
<cfset Variables.sObserverEvents = {}>
<cffunction name="init" access="public" returntype="any" output="no">
<cfscript>
var key = "";
for (key in Arguments) {
variables[key] = Arguments[key];
}
</cfscript>
<cfreturn This>
</cffunction>
<cffunction name="setUp" access="public" returntype="void" output="no">
<cfscript>
var key = "";
for (key in arguments) {
variables[key] = arguments[key];
}
</cfscript>
</cffunction>
<cffunction name="assertRecent" access="public" returntype="void" output="no" hint="I assert that given date is recent, as defined by the arguments provided.">
<cfargument name="date" type="date" required="true">
<cfargument name="message" type="string" default="">
<cfargument name="range" type="numeric" default="3">
<cfargument name="interval" type="string" default="n">
<cfset assert("#arguments.date# GTE #getRecentDateTime(range=arguments.range,interval=arguments.interval)#",arguments.message)>
</cffunction>
<cffunction name="assertEmailTestable" access="public" returntype="void" output="no" hint="I assert that email can be tested using isEmailTestable().">
<cfif NOT isEmailTestable()>
<cfset fail("Email is not currently testable (DataMgr and Mailer must be available in the test component and logging email and DataMgr must be available and not in Simulation mode.)")>
</cfif>
</cffunction>
<cffunction name="assertEmailSent" access="public" returntype="void" output="no" hint="I assert than an email has been sent. Arguments will match the keys of the email.">
<cfargument name="when" type="date" default="#now()#">
<cfargument name="message" type="string" default="Email was not sent">
<cfset assertEmailTestable()>
<cfif NOT isEmailSent(argumentCollection=arguments)>
<cfset fail(Arguments.message)>
</cfif>
</cffunction>
<cffunction name="assertEmailNotSent" access="public" returntype="void" output="no">
<cfargument name="when" type="date" default="#now()#">
<cfargument name="message" type="string" default="Email was sent">
<cfif isEmailSent(argumentCollection=arguments)>
<cfset fail(Arguments.message)>
</cfif>
</cffunction>
<cffunction name="assertNoticeSent" access="public" returntype="void" output="no">
<cfargument name="notice" type="string" required="true">
<cfargument name="to" type="string" required="false">
<cfargument name="when" type="date" default="#now()#">
<cfset var message = "Notice (#arguments.notice#) was not sent">
<cfif StructKeyExists(arguments,"to")>
<cfset message = "#message# to #arguments.to#">
</cfif>
<cfset message = "#message#.">
<cfif NOT isNoticeSent(argumentCollection=arguments)>
<cfset fail(message)>
</cfif>
</cffunction>
<cffunction name="assertNoticeNotSent" access="public" returntype="void" output="no">
<cfargument name="notice" type="string" required="true">
<cfargument name="to" type="string" required="false">
<cfargument name="when" type="date" default="#now()#">
<cfset var message = "Notice (#arguments.notice#) was sent">
<cfif StructKeyExists(arguments,"to")>
<cfset message = "#message# to #arguments.to#">
</cfif>
<cfset message = "#message#.">
<cfif isNoticeSent(argumentCollection=arguments)>
<cfset fail(message)>
</cfif>
</cffunction>
<cffunction name="clearCaches" access="public" returntype="void" output="no">
<cfset var aCacheIDs = cacheGetAllIds()>
<cfset var ii = 0>
<cfset var CacheName = "">
<!--- Clear all cached queries. --->
<cfobjectcache action="clear">
<!--- Clear all EhCaches except for those used by the Rate Limiter (I'm cheating here by know the implementation details of Rate Limiter). --->
<cfloop index="ii" from="1" to="#ArrayLen(aCacheIDs)#">
<cfset CacheName = aCacheIDs[ii]>
<cfif ListFirst(CacheName,"_") NEQ "LIMIT">
<cfset cacheRemove(CacheName)>
</cfif>
</cfloop>
</cffunction>
<cffunction name="getRandomData" access="public" returntype="struct" output="no">
<cfargument name="comp" type="any" required="yes">
<cfargument name="data" type="struct" required="no">
<cfset var aFields = Arguments.comp.getFieldsArray()>
<cfset var sResult = StructNew()>
<cfset var ii = 0>
<cfset var sArgs = StructCopy(Arguments)>
<cfset var skiptypes = "DeletionMark,Sorter,DeletionDate,UUID">
<!--- Create test data --->
<cfloop index="ii" from="1" to="#ArrayLen(aFields)#" step="1">
<cfif
StructKeyExists(aFields[ii],"name")
AND NOT ( StructKeyExists(arguments,"data") AND StructKeyExists(arguments.data,aFields[ii]["name"]) )
>
<cfif
StructKeyExists(aFields[ii],"fentity")
AND StructKeyExists(Arguments.comp,"Parent")
AND StructKeyExists(Arguments.comp,"Manager")
AND StructKeyExists(Arguments.comp.Manager,"pluralize")
AND StructKeyExists(Arguments.comp.Parent,Arguments.comp.Manager.pluralize(aFields[ii].fentity))
AND isObject(Arguments.comp.Parent[Arguments.comp.Manager.pluralize(aFields[ii].fentity)])
>
<cfset sResult[aFields[ii]["name"]] = getRandomPrimaryKeyValue(
Arguments.comp.Parent[Arguments.comp.Manager.pluralize(aFields[ii].fentity)],
( StructKeyExists(aFields[ii],"jointype") AND aFields[ii].jointype CONTAINS "many" )
)>
<cfelseif
StructKeyExists(aFields[ii],"ftable")
AND Len(aFields[ii]["ftable"])
>
<cfset sResult[aFields[ii]["name"]] = getRandomTablePrimaryKeyValue(
aFields[ii].ftable,
( StructKeyExists(aFields[ii],"jointype") AND aFields[ii].jointype CONTAINS "many" )
)>
<cfelseif
StructKeyExists(aFields[ii],"datatype")
AND ListFindNoCase("pk,fk",ListFirst(aFields[ii].type,":"))
>
<!--- No value --->
<cfelseif
StructKeyExists(aFields[ii],"relation")
>
<!--- No value --->
<cfelseif
StructKeyExists(aFields[ii],"datatype")
AND ListFirst(aFields[ii].type,":") NEQ "pk"
AND ListFirst(aFields[ii].type,":") NEQ "fk"
AND NOT ListFindNoCase(skiptypes,aFields[ii].type)
AND NOT ( StructKeyExists(aFields[ii],"Special") AND ListFindNoCase(skiptypes,aFields[ii].Special) )
AND NOT ( StructKeyExists(aFields[ii],"test") AND aFields[ii].test IS false )
>
<cfset sResult[aFields[ii]["name"]] = getRandomFieldValue(aFields[ii])>
<cfelse>
<cfset sResult[aFields[ii]["name"]] = "">
</cfif>
</cfif>
</cfloop>
<cfif StructKeyExists(arguments,"data")>
<cfset StructAppend(sResult,arguments.data,"yes")>
</cfif>
<!--- Ability to pass in named arguments directly without loading into data struct --->
<cfset StructDelete(sArgs,"comp")>
<cfset StructDelete(sArgs,"data")>
<cfif StructCount(sArgs)>
<cfset StructAppend(sResult,sArgs,"yes")>
</cfif>
<cfreturn sResult>
</cffunction>
<cffunction name="getRandomFieldValue" access="public" returntype="string" output="no">
<cfargument name="field" type="any" required="yes">
<cfset var sField = arguments.field>
<cfset var result = getRandomValue(sField.datatype)>
<cfset var length = 0>
<cfset var email_suffix = "@example.com">
<cfif StructKeyExists(sField,"Length")>
<cfset length = sField.Length>
<cfif StructKeyExists(sField,"type") AND sField.type EQ "email">
<cfset length = length - Len(email_suffix)>
</cfif>
<cfif Len(result) GT length>
<cfset result = Left(result,length)>
</cfif>
</cfif>
<cfif StructKeyExists(sField,"type") AND sField.type EQ "email">
<cfset result = "#result##email_suffix#">
</cfif>
<cfreturn result>
</cffunction>
<cffunction name="getRandomPrimaryKeyValue" access="public" returntype="string" output="no">
<cfargument name="comp" type="any" required="yes">
<cfargument name="multi" type="boolean" default="false">
<cfset var keys = Arguments.comp.getPrimaryKeyValues()>
<cfset var result = "">
<cfset var times = 1>
<cfset var ii = 0>
<cfif ListLen(keys)>
<cfif Arguments.multi>
<cfset times = RandRange(0,Min(50,ListLen(keys)))>
</cfif>
<cfif Val(times)>
<cfloop index="ii" from="1" to="#times#">
<cfset result = ListAppend(
result,
ListGetAt(keys,RandRange(1,ListLen(keys)))
)>
</cfloop>
</cfif>
</cfif>
<cfreturn result>
</cffunction>
<cffunction name="getRandomTablePrimaryKeyValue" access="public" returntype="string" output="no">
<cfargument name="tablename" type="string" required="yes">
<cfargument name="multi" type="boolean" default="false">
<cfset var pklist = "">
<cfset var qRecords = 0>
<cfset var keys = "">
<cfset var times = 1>
<cfset var result = "">
<cf_service name="DataMgr">
<cfset pklist = Variables.DataMgr.getPrimaryKeyFieldNames(Arguments.tablename)>
<cfif ListLen(pklist) EQ 1>
<cfset qRecords = Variables.DataMgr.getRecords(tablename=Arguments.tablename,fieldlist=pklist,maxrows=50)>
<cfoutput query="qRecords">
<cfset keys = ListAppend(keys,qRecords[pklist][CurrentRow])>
</cfoutput>
</cfif>
<cfif ListLen(keys)>
<cfif Arguments.multi>
<cfset times = RandRange(0,ListLen(keys))>
</cfif>
<cfif Val(times)>
<cfloop index="ii" from="1" to="#times#">
<cfset result = ListAppend(
result,
ListGetAt(keys,RandRange(1,ListLen(keys)))
)>
</cfloop>
</cfif>
</cfif>
<cfreturn result>
</cffunction>
<cffunction name="getRandomValue" access="public" returntype="string" output="no">
<cfargument name="datatype" type="string" required="yes">
<cfargument name="allowNulls" type="boolean" default="yes">
<cfset var result = "">
<cfswitch expression="#arguments.datatype#">
<cfcase value="boolean">
<cfset result = RandRange(0,1)>
</cfcase>
<cfcase value="date">
<cfset result = DateFormat(DateAdd("d",RandRange(30,1095),now()),"yyyy-mm-dd")>
</cfcase>
<cfcase value="integer,number">
<cfset result = RandRange(0,100)>
</cfcase>
<cfcase value="text">
<cfset result = "Test#RandRange(1,1000000)#">
</cfcase>
<cfcase value="email">
<cfset result = "Test#RandRange(1,1000000)#@example.com">
</cfcase>
</cfswitch>
<!--- Add a 20% chance of a NULL/empty --->
<cfif Arguments.allowNulls AND RandRange(1,5) EQ 1>
<cfreturn "">
</cfif>
<cfreturn result>
</cffunction>
<cffunction name="isEmailTestable" access="public" returntype="boolean" output="no">
<cfset var result = false>
<cfif StructKeyExists(variables,"NoticeMgr")>
<cfif NOT StructKeyExists(variables,"DataMgr")>
<cfset variables.DataMgr = variables.NoticeMgr.getDataMgr()>
</cfif>
<cfif NOT StructKeyExists(variables,"Mailer")>
<cfset variables.Mailer = variables.NoticeMgr.getMailer()>
</cfif>
</cfif>
<cfif StructKeyExists(variables,"Manager") AND NOT StructKeyExists(Variables,"DataMgr")>
<cfset variables.DataMgr = variables.Manager.DataMgr>
</cfif>
<cfset result = (
StructKeyExists(variables,"DataMgr")
AND StructKeyExists(variables,"Mailer")
AND variables.DataMgr.getDatabase() NEQ "Sim"
)>
<cfif NOT variables.Mailer.getIsLogging()>
<cfset Variables.Mailer.startLogging(Variables.DataMgr)>
</cfif>
<cfreturn result>
</cffunction>
<cffunction name="isEmailSent" access="public" returntype="boolean" output="no">
<cfargument name="when" type="date" default="#now()#">
<cfset var result = false>
<cfif NumEmailsSent(argumentCollection=arguments)>
<cfset result = true>
</cfif>
<cfreturn result>
</cffunction>
<cffunction name="isNoticeSent" access="public" returntype="boolean" output="no">
<cfargument name="notice" type="string" required="true">
<cfargument name="to" type="string" required="false">
<cfargument name="when" type="date" default="#now()#">
<cfreturn isEmailSent(argumentCollection=arguments)>
</cffunction>
<cffunction name="getRecentDateTime" access="public" returntype="date" output="no">
<cfargument name="date" type="date" default="#now()#">
<cfargument name="range" type="numeric" default="3">
<cfargument name="interval" type="string" default="n">
<cfreturn DateAdd(arguments.interval,-Abs(arguments.range),arguments.date)>
</cffunction>
<cffunction name="NumEmailsSent" access="public" returntype="numeric" output="no">
<cfargument name="when" type="date" default="#now()#">
<cfset var result = 0>
<cfset var aFilters = ArrayNew(1)>
<cfset var sFilter = StructNew()>
<cfset var qSentMessages = 0>
<cfset var oDataMgr = 0>
<cfset var fieldlist = "LogID">
<cfset var sData = Duplicate(Arguments)>
<cfset var RecipientFields = "To,CC,BCC,From">
<cfset var ii = 0>
<cfset var key = "">
<cfset assertEmailTestable()>
<cfset oDataMgr = variables.DataMgr>
<cfset sFilter["field"] = "DateSent">
<cfset sFilter["operator"] = ">=">
<cfset sFilter["value"] = DateAdd("n",-3,arguments.when)>
<cfset ArrayAppend(aFilters,sFilter)>
<cfif StructKeyExists(arguments,"regex") AND Len(arguments.regex)>
<cfset fieldlist = "LogID,Subject,Contents,HTML,Text">
</cfif>
<cfset qSentMessages = oDataMgr.getRecords(tablename=variables.Mailer.getLogTable(),data=sData,filters=aFilters,fieldlist=fieldlist)>
<cfif qSentMessages.RecordCount EQ 0>
<!--- look more thoroughly for a match --->
<!--- Exclude recipient fields from the query --->
<cfloop list="#RecipientFields#" index="key">
<cfset StructDelete(sData,key)>
</cfloop>
<cfset fieldlist = ListAppend(fieldlist,RecipientFields)>
<cfset qSentMessages = oDataMgr.getRecords(tablename=variables.Mailer.getLogTable(),data=sData,filters=aFilters,fieldlist=fieldlist)>
<cfif qSentMessages.RecordCount>
<!--- Get just the email addresses themselves --->
<cfloop list="#RecipientFields#" index="key">
<cfif StructKeyExists(Arguments,key) AND Len(Arguments[key])>
<cfset Arguments[key] = getEmailAddresses(Arguments[key] ) />
</cfif>
</cfloop>
<cfscript>
//Find by just email address (must be in cfscript as CFCONTINUE is not available until CF9 and we need to support CF8)
for ( ii = qSentMessages.RecordCount; ii GTE 1; ii=ii-1 ) {
for ( key in Arguments ) {
if ( ListFindNoCase(RecipientFields,key) AND StructKeyExists(Arguments,key) AND Len(Arguments[key]) ) {
if ( StructKeyExists(Arguments,key) AND Len(Arguments[key]) ) {
//Are all emails that were passed in as arguments found in the query record?
if (
NOT (
Len(qSentMessages[key][ii])
AND isListInList(Arguments[key],getEmailAddresses(qSentMessages[key][ii]))
)
) {
qSentMessages = QueryDeleteRows(qSentMessages,ii);
continue;
}
}
}
}
}
</cfscript>
</cfif>
</cfif>
<cfset result = qSentMessages.RecordCount>
<cfif result AND StructKeyExists(arguments,"regex") AND Len(arguments.regex)>
<cfoutput query="qSentMessages">
<cfif NOT (
ReFindNoCase(arguments.regex,Subject)
OR ReFindNoCase(arguments.regex,Contents)
OR ReFindNoCase(arguments.regex,Text)
OR ReFindNoCase(arguments.regex,HTML)
)>
<cfset result = result - 1>
</cfif>
</cfoutput>
</cfif>
<cfreturn result>
</cffunction>
<cffunction name="getEmailAddresses">
<cfargument name="string" type="string">
<cfargument name="EmailAddresses" type="string" default="">
<cfset var sLenPos = 0>
<cfset var emailAddress = "">
<cfif REFind("([a-zA-Z0-9_\.=-]+@[a-zA-Z0-9_\.-]+\.[[:alpha:]]{2,6})",arguments.string)>
<cfset sLenPos = REFind("([a-zA-Z0-9_\.=-]+@[a-zA-Z0-9_\.-]+\.[[:alpha:]]{2,6})",arguments.string,1,true) />
<cfset emailAddress = mid(arguments.string, sLenPos.pos[1], sLenPos.len[1]) />
<cfif NOT ListFindNoCase(arguments.EmailAddresses,emailAddress)>
<cfset arguments.EmailAddresses = ListAppend(arguments.EmailAddresses, emailAddress)>
</cfif>
<cfset arguments.string = Mid(arguments.string, sLenPos.pos[1] + sLenPos.len[1], len(arguments.string))>
<cfif REFind("([a-zA-Z0-9_\.=-]+@[a-zA-Z0-9_\.-]+\.[[:alpha:]]{2,6})",arguments.string)>
<cfset arguments.EmailAddresses = getEmailAddresses(arguments.string, arguments.EmailAddresses)>
</cfif>
</cfif>
<cfreturn arguments.EmailAddresses>
</cffunction>
<cfscript>
/**
* Removes rows from a query.
* Added var col = "";
* No longer using Evaluate. Function is MUCH smaller now.
*
* @param Query Query to be modified
* @param Rows Either a number or a list of numbers
* @return This function returns a query.
* @author Raymond Camden ([email protected])
* @version 2, October 11, 2001
*/
function QueryDeleteRows(Query,Rows) {
var tmp = QueryNew(Query.ColumnList);
var i = 1;
var x = 1;
for(i=1;i lte Query.recordCount; i=i+1) {
if(not ListFind(Rows,i)) {
QueryAddRow(tmp,1);
for(x=1;x lte ListLen(tmp.ColumnList);x=x+1) {
QuerySetCell(tmp, ListGetAt(tmp.ColumnList,x), query[ListGetAt(tmp.ColumnList,x)][i]);
}
}
}
return tmp;
}
</cfscript>
<cfscript>
/**
* Checks is all elements of a list X is found in a list Y.
* v2 by Raymond Camden
* v3 idea by Bill King
* v4 fix by Chris Phillips
*
* @param l1 The first list. (Required)
* @param l2 The second list. UDF checks to see if all of l1 is in l2. (Required)
* @param delim1 List delimiter for l1. Defaults to a comma. (Optional)
* @param delim2 List delimiter for l2. Defaults to a comma. (Optional)
* @param matchany If true, UDF returns true if at least one item in l1 exists in l2. Defaults to false. (Optional)
* @return Returns a boolean.
* @author Daniel Chicayban ([email protected])
* @version 4, September 4, 2008
*/
function isListInList(l1,l2) {
var delim1 = ",";
var delim2 = ",";
var i = 1;
var matchany = false;
if(arrayLen(arguments) gte 3) delim1 = arguments[3];
if(arrayLen(arguments) gte 4) delim2 = arguments[4];
if(arrayLen(arguments) gte 5) matchany = arguments[5];
for(i=1; i lte listLen(l1,delim1); i=i+1) {
if(matchany and listFind(l2,listGetAt(l1,i,delim1),delim2)) return true;
if(not matchany and not listFind(l2,listGetAt(l1,i,delim1),delim2)) return false;
}
return not matchany;
}
</cfscript>
<cffunction name="loadExternalVars" access="public" returntype="void" output="no">
<cfargument name="varlist" type="string" required="true">
<cfargument name="scope" type="string" default="Application">
<cfargument name="skipmissing" type="boolean" default="false">
<cfset var varname = "">
<cfset var scopestruct = 0>
<cfset var OriginalScope = Arguments.scope>
<!--- Scopes that start with a dot are nested within a service. --->
<cfif Left(arguments.scope,1) EQ "." AND Len(arguments.scope) GTE 2>
<!--- To start, drop the leading dot from the scope name since we know what it is within this conditional block. --->
<cfset arguments.scope = Right(arguments.scope,Len(arguments.scope)-1)>
<!--- Get it from ServiceFactory if we can. --->
<cfif Application.Framework.Loader.hasService(arguments.scope)>
<cfset variables[arguments.scope] = Application.ServiceFactory.getService(arguments.scope)>
<cfelse>
<!--- If not, try to get it from Application scope (may result in an exception). --->
<cfset variables[arguments.scope] = Application[arguments.scope]>
</cfif>
<!--- Now we can just treat the service we got back as a scope. --->
<cfset arguments.scope = "Variables.#arguments.scope#">
</cfif>
<cfset scopestruct = StructGet(arguments.scope)>
<cfloop index="varname" list="#arguments.varlist#">
<cfif StructKeyExists(scopestruct,varname)>
<!--- Try to get it from the scope. --->
<cfset variables[varname] = scopestruct[varname]>
<cfelseif StructKeyExists(Application,"Framework") AND Application.Framework.Loader.hasService(varname)>
<!--- Get it from ServiceFactory if we can. --->
<cfset variables[varname] = Application.ServiceFactory.getService(varname)>
<cfelseif NOT arguments.skipmissing>
<cfthrow message="#scope#.#varname# is not available.">
</cfif>
</cfloop>
</cffunction>
<cffunction name="getService" access="public" returntype="any" output="no">
<cfargument name="ServiceName" type="string" required="yes">
<cfset loadServiceFactory()>
<cfif NOT StructKeyExists(Variables,Arguments.ServiceName)>
<!--- Get it from ServiceFactory if we can. --->
<cfif StructKeyExists(Variables,"ServiceFactory") AND Variables.ServiceFactory.hasService(Arguments.ServiceName)>
<cfset variables[Arguments.ServiceName] = Variables.ServiceFactory.getService(Arguments.ServiceName)>
<cfelse>
<!--- If not, try to get it from Application scope (may result in an exception). --->
<cfset variables[Arguments.ServiceName] = Application[Arguments.ServiceName]>
</cfif>
</cfif>
<cfreturn Variables[Arguments.ServiceName]>
</cffunction>
<cffunction name="loadServiceFactory" access="public" returntype="any" output="no">
<cfif NOT StructKeyExists(Variables,"ServiceFactory")>
<cfif NOT StructKeyExists(request,"TestServiceFactory")>
<cfset request.TestServiceFactory = getServiceFactory()>
</cfif>
<!--- Variable won't exist unless running on Neptune. --->
<cfif StructKeyExists(request,"TestServiceFactory")>
<cfset Variables.ServiceFactory = request.TestServiceFactory>
</cfif>
</cfif>
<!--- Variable won't exist unless running on Neptune. --->
<cfif StructKeyExists(Variables,"ServiceFactory")>
<cfreturn Variables.ServiceFactory>
</cfif>
</cffunction>
<cffunction name="getServiceFactory" access="public" returntype="any" output="no">
<cfset var RootPath = "">
<cfset var CompCFMPath = "">
<cfset var oConfigExisting = 0>
<cfset var oConfigNew = 0>
<cfset var sConfigData = 0>
<cfset var sConfigObject = 0>
<cfset var result = 0>
<cfif StructKeyExists(Application,"Framework") AND StructKeyExists(Application.Framework,"Config")>
<!--- Get reference to existing Config object --->
<cfset oConfigExisting = Application.Framework.Config>
<!--- Get meta data on existing Config object just so we can init it from the same path --->
<cfset sConfigObject = GetMetaData(oConfigExisting)>
<!--- Get existing settings so we can pass them (perhaps altered) into our new config object --->
<cfset sConfigData = StructCopy(oConfigExisting.getSettings())>
<!--- Get path to components.cfm file --->
<cfset RootPath = oConfigExisting.getSetting('RootPath')>
<cfset CompCFMPath = RootPath & "_config\components.cfm">
<!--- New config object (should only look in request, not Application) --->
<cfset oConfigNew = CreateObject("component","#sConfigObject.Name#").init("request")>
<!--- Copy existing settings in --->
<cfset oConfigNew.setSettings(ArgumentCollection=sConfigData)>
<!---
This is our hook to allow RecordsTester to be extended and have extension pass in whatever it needs to new Config.
Should be set up so that it will be the same for any test run in the same request.
--->
<cfset setSettings(oConfigNew)>
<!--- Create new service factory from existing one (with potentially changed data) --->
<cfset result = CreateObject("component","_framework.ServiceFactory").init(oConfigNew,CompCFMPath)>
<cfreturn result>
</cfif>
</cffunction>
<cffunction name="setSettings" access="public" returntype="any" output="no">
<cfargument name="oConfig" type="any" required="yes">
<!--- This method can be extended to pass in or change any configuration for testing. --->
</cffunction>
<cffunction name="RecordObject" access="public" returntype="any" output="no">
<cfargument name="Service" type="any" required="yes">
<cfargument name="Record" type="any" required="yes">
<cfargument name="fields" type="string" default="">
<cfset Arguments.Service = CreateObject("component","com.sebtools.TestRecords").init(Arguments.Service)>
<cfreturn CreateObject("component","RecordObject").init(ArgumentCollection=Arguments)>
</cffunction>
<cffunction name="runInRollbackTransaction" access="public" returntype="any" output="no">
<cfargument name="method" type="any" required="yes">
<cfargument name="comp" type="any" required="no">
<cfargument name="args" type="struct" default="#StructNew()#">
<cfset var result = 0>
<cfset var fMethod = 0>
<cfif StructKeyExists(arguments,"comp") AND isSimpleValue(arguments.method)>
<cfset fMethod = arguments.com[arguments.method]>
<cfelseif isCustomFunction(arguments.method)>
<cfset fMethod = arguments.method>
<cfelse>
<cfthrow message="Method must be either the name of a method in a component or the method itself.">
</cfif>
<cftransaction>
<cftry>
<cfset result = fMethod(argumentCollection=arguments.args)>
<cfcatch type="any">
<cftransaction action="rollback">
<cfrethrow>
</cfcatch>
</cftry>
<cftransaction action="rollback">
</cftransaction>
<cfif isDefined("result")>
<cfreturn result>
</cfif>
</cffunction>
<cffunction name="stub" access="public" returntype="void" output="no">
<cfset fail("No test written yet.")>
</cffunction>
<cffunction name="getTestRecord" access="public" returntype="query" output="no">
<cfargument name="comp" type="any" required="yes">
<cfargument name="data" type="struct" required="no">
<cfargument name="fieldlist" type="string" default="">
<cfset var sCompMeta = Arguments.comp.getMetaStruct()>
<cfset var id = saveTestRecord(argumentCollection=Arguments)>
<cfset var qRecord = 0>
<cfinvoke
returnvariable="qRecord"
component="#arguments.comp#"
method="#sCompMeta.method_get#"
>
<cfinvokeargument name="#sCompMeta.arg_pk#" value="#id#">
<cfinvokeargument name="fieldlist" value="#Arguments.fieldlist#">
</cfinvoke>
<cfreturn qRecord>
</cffunction>
<cffunction name="getTestRecords" access="public" returntype="query" output="no">
<cfargument name="comp" type="any" required="yes">
<cfargument name="records" type="numeric" required="no">
<cfargument name="data" type="struct" required="no">
<cfargument name="fieldlist" type="string" default="">
<cfset var sCompMeta = Arguments.comp.getMetaStruct()>
<cfset var ids = saveTestRecords(argumentCollection=Arguments)>
<cfset var qRecords = 0>
<cfinvoke
returnvariable="qRecords"
component="#arguments.comp#"
method="#sCompMeta.method_gets#"
>
<cfinvokeargument name="#LCase(sCompMeta.arg_sort)#" value="#ids#">
<cfinvokeargument name="fieldlist" value="#Arguments.fieldlist#">
</cfinvoke>
<cfreturn qRecords>
</cffunction>
<cffunction name="saveTestRecord" access="public" returntype="string" output="no">
<cfargument name="comp" type="any" required="yes">
<cfargument name="data" type="struct" required="no">
<cfset var sCompMeta = arguments.comp.getMetaStruct()>
<cfset var sData = getRandomData(argumentCollection=arguments)>
<cfset var result = 0>
<cfinvoke
returnvariable="result"
component="#arguments.comp#"
method="#sCompMeta.method_save#"
argumentCollection="#sData#"
>
</cfinvoke>
<cfreturn result>
</cffunction>
<cffunction name="loadTestRecords" access="public" returntype="string" output="no">
<cfargument name="comp" type="any" required="yes">
<cfargument name="records" type="numeric" required="no">
<cfargument name="data" type="struct" required="no">
<cfset var result = "">
<cfset Arguments = convertTestRecordsArgs(ArgumentCollection=Arguments)>
<cfset result = Arguments.comp.getPrimaryKeyValues(ArgumentCollection=Arguments.data,MaxRows=Arguments.records)>
<cfset Arguments.records = Arguments.records - ListLen(result)>
<cfif Arguments.records GT 0>
<cfset result = ListAppend(result,saveTestRecords(ArgumentCollection=Arguments))>
</cfif>
<cfreturn result>
</cffunction>
<cffunction name="convertTestRecordsArgs" access="public" returntype="struct" output="no">
<cfargument name="comp" type="any" required="yes">
<cfargument name="records" type="numeric" required="no">
<cfargument name="data" type="struct" required="no">
<!--- Handle if data is passed in to records slot or if arguments are reversed --->
<cfif
StructKeyExists(Arguments,"records")
AND isStruct(Arguments.records)
AND (
NOT StructKeyExists(Arguments,"data")
OR isNumeric(Arguments.data)
)
>
<cfif StructKeyExists(Arguments,"data")>
<cfset Arguments.temp = Arguments.data>
</cfif>
<cfset Arguments.data = Arguments.records>
<cfif StructKeyExists(Arguments,"temp")>
<cfset Arguments.records = Arguments.temp>
<cfset StructDelete(Arguments,"temp")>
</cfif>
</cfif>
<cfif
NOT (
StructKeyExists(Arguments,"records")
AND isSimpleValue(Arguments.records)
AND Val(Arguments.records)
)
>
<cfset Arguments.records = 0>
</cfif>
<cfif NOT ( StructKeyExists(Arguments,"data") AND isStruct(Arguments.data) )>
<cfset Arguments.data = StructNew()>
</cfif>
<cfreturn Arguments>
</cffunction>
<!--- *** Observer Tests *** --->
<cffunction name="assertAnnounced" access="public" returntype="void" output="no" hint="I assert that the given listener was called.">
<cfargument name="UUID" type="string" required="true">
<cfargument name="message" type="string" default="">
<cfset listened(Arguments.UUID)>
<cfif NOT ArrayLen(Variables.sObserverEvents[UUID])>
<cfset fail(Arguments.message)>
</cfif>
</cffunction>
<cffunction name="assertNotAnnounced" access="public" returntype="void" output="no" hint="I assert that the given listener was called.">
<cfargument name="UUID" type="string" required="true">
<cfargument name="message" type="string" default="">
<cfset listened(Arguments.UUID)>
<cfif ArrayLen(Variables.sObserverEvents[UUID])>
<cfset fail(Arguments.message)>
</cfif>
</cffunction>
<cffunction name="listen" access="public" returntype="string" output="no" hint="I make sure that the listener is listening for the given event.">
<cfargument name="EventName" type="string" required="true">
<cfargument name="Args" type="struct" required="false">
<cfset var UUID = createUUID()>
<cfset Variables.sObserverChecks[UUID] = Arguments>
<cfset Variables.sObserverEvents[UUID] = []>
<cf_service name="Observer">
<cfset Variables.Observer.registerListener(
Listener=This,
ListenerName=UUID,
ListenerMethod="listen_callback",
EventName=Arguments.EventName
)>
<cfreturn UUID>
</cffunction>
<cffunction name="listen_callback" access="public" returntype="any" output="no" hint="I respond to the an event from a listener.">
<cfset var UUID = "">
<cfscript>
var UUID = "";
var isCalled = false;
var arg = "";
for ( UUID in Variables.sObserverChecks ) {
isCalled = true;
for ( arg in Variables.sObserverChecks[UUID]["Args"] ) {
//Make sure every argument matches what is expected.
if ( NOT ( StructKeyExists(Arguments,arg) AND Arguments[arg] EQ Variables.sObserverChecks[UUID]["Args"][arg] ) ) {
isCalled = false;
}
}
if ( isCalled IS true ) {
ArrayAppend(Variables.sObserverEvents[UUID],Arguments);
}
}
</cfscript>
</cffunction>
<cffunction name="listened" access="public" returntype="void" output="no" hint="I run code to end listening for an event.">
<cfargument name="UUID" type="string" required="true">
<cfset Variables.Observer.runDelays()>
<cfif StructKeyExists(Variables.sObserverChecks,UUID)>
<cfset Variables.Observer.registerListener(
Listener=This,
ListenerName=UUID,
ListenerMethod="listen_callback",
EventName=Variables.sObserverChecks[UUID].EventName
)>
</cfif>
</cffunction>
<cffunction name="saveTestRecords" access="public" returntype="string" output="no">
<cfargument name="comp" type="any" required="yes">
<cfargument name="records" type="any" required="no">
<cfargument name="data" type="struct" required="no">
<cfset var ii = 0>
<cfset var result = "">
<cfset Arguments = convertTestRecordsArgs(ArgumentCollection=Arguments)>
<cfif NOT Val(Arguments.records)>
<cfset Arguments.records = RandRange(10,40)>
</cfif>
<cfif StructKeyExists(Arguments,"data") AND NOT isStruct(Arguments.data)>
<cfset StructDelete(Arguments,"data")>
</cfif>
<cfloop index="ii" from="1" to="#Arguments.records#">
<cfset result = ListAppend(result,saveTestRecord(ArgumentCollection=Arguments))>
</cfloop>
<cfreturn result>
</cffunction>
<cffunction name="saveTestRecordOnly" access="public" returntype="string" output="no">
<cfargument name="comp" type="any" required="yes">
<cfargument name="data" type="struct" required="no">
<cfset var sCompMeta = arguments.comp.getMetaStruct()>
<cfset var sData = getRandomData(argumentCollection=arguments)>
<cfset var result = 0>
<cfinvoke
returnvariable="result"
component="#arguments.comp#"
method="saveRecordOnly"
argumentCollection="#sData#"
>
</cfinvoke>
<cfreturn result>
</cffunction>
<cffunction name="QueryGetRandomRow" access="public" returntype="query" output="no">
<cfargument name="query" type="any" required="yes">
<cfset var cols = arguments.query.ColumnList>
<cfset var qResult = QueryNew(cols)>
<cfset var rownum = 0>
<cfset var col = "">
<cfif arguments.query.RecordCount>
<cfset rownum = RandRange(1,arguments.query.RecordCount)>
<cfset QueryAddRow(qResult)>
<cfloop list="#cols#" index="col">
<cfset QuerySetCell(qResult,col,arguments.query[col][rownum])>
</cfloop>
</cfif>
<cfreturn qResult>
</cffunction>
<cfscript>
function QueryFromArgs() {
return Struct2Query(arguments);
}
//By Charlie Griefer
function Struct2Query(struct) {