Skip to content

Commit ad43c60

Browse files
committed
Updated tests to work on ACF, again more issues with interfaces and ACF
1 parent 95b0e6c commit ad43c60

File tree

4 files changed

+108
-69
lines changed

4 files changed

+108
-69
lines changed

box.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
},
1414
"installPaths":{
1515
"cbi18n":"modules/cbi18n/",
16-
"coldbox":"coldbox",
17-
"testbox":"testbox",
18-
"workbench":"workbench/"
16+
"coldbox":"coldbox/",
17+
"testbox":"testbox/",
18+
"workbench":"workbench"
1919
},
2020
"testbox":{
2121
"runner":"http://localhost:49616"

modules/cbvalidation/models/IValidationManager.cfc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,24 @@ interface{
2424
* Retrieve the shared constraints
2525
* @name.hint Filter by name or not
2626
*/
27-
struct function getSharedConstraints(string name);
27+
struct function getSharedConstraints( string name );
2828

2929
/**
3030
* Check if a shared constraint exists by name
3131
* @name.hint The shared constraint to check
3232
*/
33-
boolean function sharedConstraintsExists(required string name);
33+
boolean function sharedConstraintsExists( required string name );
3434

3535
/**
3636
* Set the shared constraints into the validation manager, usually these are described in the ColdBox configuraiton file
3737
* @constraints.hint Filter by name or not
3838
*/
39-
IValidationManager function setSharedConstraints(struct constraints);
39+
IValidationManager function setSharedConstraints( struct constraints );
4040

4141
/**
4242
* Store a shared constraint
4343
* @name.hint Filter by name or not
4444
* @constraint.hint The constraint to store.
4545
*/
46-
IValidationManager function addSharedConstraint(required string name, required struct constraint);
46+
IValidationManager function addSharedConstraint( required string name, required struct constraint );
4747
}

modules/cbvalidation/models/ValidationManager.cfc

Lines changed: 93 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
7171

7272
/**
7373
* Constructor
74-
* @sharedConstraints.hint A structure of shared constraints
74+
*
75+
* @sharedConstraints A structure of shared constraints
7576
*/
7677
ValidationManager function init( struct sharedConstraints=structNew() ){
7778
// valid validator registrations
@@ -83,26 +84,34 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
8384
}
8485

8586
/**
86-
* Validate an object
87-
* @target.hint The target object to validate or a structure like a form or collection. If it is a collection, we will build a generic object for you so we can validate the structure of name-value pairs.
88-
* @fields.hint One or more fields to validate on, by default it validates all fields in the constraints. This can be a simple list or an array.
89-
* @constraints.hint An optional shared constraints name or an actual structure of constraints to validate on.
90-
* @locale.hint An optional locale to use for i18n messages
91-
* @excludeFields.hint An optional list of fields to exclude from the validation.
92-
* @IncludeFields.hint An optional list of fields to include in the validation.
87+
* Validate an object using constraints
88+
*
89+
* @target The target object to validate or a structure like a form or collection. If it is a collection, we will build a generic object for you so we can validate the structure of name-value pairs.
90+
* @fields One or more fields to validate on, by default it validates all fields in the constraints. This can be a simple list or an array.
91+
* @constraints An optional shared constraints name or an actual structure of constraints to validate on.
92+
* @locale An optional locale to use for i18n messages
93+
* @excludeFields An optional list of fields to exclude from the validation.
94+
* @IncludeFields An optional list of fields to include in the validation.
9395
*/
94-
IValidationResult function validate( required any target, string fields="*", any constraints="", string locale="", string excludeFields="", string includeFields="" ){
96+
IValidationResult function validate(
97+
required any target,
98+
string fields="*",
99+
any constraints="",
100+
string locale="",
101+
string excludeFields="",
102+
string includeFields=""
103+
){
95104
var targetName = "";
96105

97106
// Do we have a real object or a structure?
98107
if( !isObject( arguments.target ) ){
99108
arguments.target = new GenericObject( arguments.target );
100-
if( isSimpleValue( arguments.constraints ) and len( arguments.constraints ) )
109+
if( isSimpleValue( arguments.constraints ) and len( arguments.constraints ) ){
101110
targetName = arguments.constraints;
102-
else
111+
} else {
103112
targetName = "GenericForm";
104-
}
105-
else{
113+
}
114+
} else {
106115
targetName = listLast( getMetadata( arguments.target ).name, ".");
107116
}
108117

@@ -117,10 +126,9 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
117126
constraints = allConstraints
118127
};
119128
var results = wirebox.getInstance( name="cbvalidation.models.result.ValidationResult", initArguments=initArgs );
129+
120130
// iterate over constraints defined
121-
var thisField = "";
122-
for( thisField in allConstraints ){
123-
131+
for( var thisField in allConstraints ){
124132
var validateField = true;
125133
if( len( arguments.includeFields ) AND NOT listFindNoCase( arguments.includeFields, thisField ) ){
126134
validateField = false;
@@ -144,8 +152,18 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
144152

145153
/**
146154
* Process validation rules on a target object and field
155+
*
156+
* @results The validation result object
157+
* @rules The structure containing validation rules
158+
* @target The target object to do validation on
159+
* @field The field to validate
147160
*/
148-
ValidationManager function processRules(required cbvalidation.models.result.IValidationResult results, required struct rules, required any target, required any field){
161+
ValidationManager function processRules(
162+
required cbvalidation.models.result.IValidationResult results,
163+
required struct rules,
164+
required any target,
165+
required any field
166+
){
149167
// process the incoming rules
150168
for( var key in arguments.rules ){
151169
// if message validators, just ignore
@@ -158,7 +176,7 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
158176
target = arguments.target,
159177
field = arguments.field,
160178
targetValue = evaluate( "arguments.target.get#arguments.field#()" ),
161-
validationData = arguments.rules[key]
179+
validationData = arguments.rules[ key ]
162180
);
163181

164182
}
@@ -167,44 +185,61 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
167185

168186
/**
169187
* Create validators according to types and validation data
188+
*
189+
* @validatorType The type of validator to retrieve, either internal or class path or wirebox ID
190+
* @validationData The validation data that is used for custom validators
191+
*
192+
* @throws ValidationManager.InvalidValidatorType
170193
*/
171-
cbvalidation.models.validators.IValidator function getValidator(required string validatorType, required any validationData){
194+
cbvalidation.models.validators.IValidator function getValidator(
195+
required string validatorType,
196+
required any validationData
197+
){
172198
switch( arguments.validatorType ){
173-
case "required" : { return wirebox.getInstance("cbvalidation.models.validators.RequiredValidator"); }
174-
case "type" : { return wirebox.getInstance("cbvalidation.models.validators.TypeValidator"); }
175-
case "size" : { return wirebox.getInstance("cbvalidation.models.validators.SizeValidator"); }
176-
case "range" : { return wirebox.getInstance("cbvalidation.models.validators.RangeValidator"); }
177-
case "regex" : { return wirebox.getInstance("cbvalidation.models.validators.RegexValidator"); }
178-
case "sameAs" : { return wirebox.getInstance("cbvalidation.models.validators.SameAsValidator"); }
179-
case "sameAsNoCase" : { return wirebox.getInstance("cbvalidation.models.validators.SameAsNoCaseValidator"); }
180-
case "inList" : { return wirebox.getInstance("cbvalidation.models.validators.InListValidator"); }
181-
case "discrete" : { return wirebox.getInstance("cbvalidation.models.validators.DiscreteValidator"); }
182-
case "min" : { return wirebox.getInstance("cbvalidation.models.validators.MinValidator"); }
183-
case "max" : { return wirebox.getInstance("cbvalidation.models.validators.MaxValidator"); }
184-
case "udf" : { return wirebox.getInstance("cbvalidation.models.validators.UDFValidator"); }
185-
case "method" : { return wirebox.getInstance("cbvalidation.models.validators.MethodValidator"); }
199+
case "required" : { return wirebox.getInstance( "cbvalidation.models.validators.RequiredValidator" ); }
200+
case "type" : { return wirebox.getInstance( "cbvalidation.models.validators.TypeValidator" ); }
201+
case "size" : { return wirebox.getInstance( "cbvalidation.models.validators.SizeValidator" ); }
202+
case "range" : { return wirebox.getInstance( "cbvalidation.models.validators.RangeValidator" ); }
203+
case "regex" : { return wirebox.getInstance( "cbvalidation.models.validators.RegexValidator" ); }
204+
case "sameAs" : { return wirebox.getInstance( "cbvalidation.models.validators.SameAsValidator" ); }
205+
case "sameAsNoCase" : { return wirebox.getInstance( "cbvalidation.models.validators.SameAsNoCaseValidator" ); }
206+
case "inList" : { return wirebox.getInstance( "cbvalidation.models.validators.InListValidator" ); }
207+
case "discrete" : { return wirebox.getInstance( "cbvalidation.models.validators.DiscreteValidator" ); }
208+
case "min" : { return wirebox.getInstance( "cbvalidation.models.validators.MinValidator" ); }
209+
case "max" : { return wirebox.getInstance( "cbvalidation.models.validators.MaxValidator" ); }
210+
case "udf" : { return wirebox.getInstance( "cbvalidation.models.validators.UDFValidator" ); }
211+
case "method" : { return wirebox.getInstance( "cbvalidation.models.validators.MethodValidator" ); }
186212
case "validator" : {
187-
if( find(":", arguments.validationData) ){ return wirebox.getInstance( getToken( arguments.validationData, 2, ":" ) ); }
213+
if( find( ":", arguments.validationData ) ){
214+
return wirebox.getInstance( getToken( arguments.validationData, 2, ":" ) );
215+
}
188216
return wirebox.getInstance( arguments.validationData );
189217
}
190218
default : {
191-
if ( wirebox.getBinder().mappingExists( validatorType ) ) { return wirebox.getInstance( validatorType ); }
192-
throw(message="The validator you requested #arguments.validatorType# is not a valid validator",type="ValidationManager.InvalidValidatorType");
219+
if ( wirebox.getBinder().mappingExists( validatorType ) ) {
220+
return wirebox.getInstance( validatorType );
221+
}
222+
throw(
223+
message = "The validator you requested #arguments.validatorType# is not a valid validator",
224+
type = "ValidationManager.InvalidValidatorType"
225+
);
193226
}
194227
}
195228
}
196229

197230
/**
198231
* Retrieve the shared constraints, all of them or by name
199-
* @name.hint Filter by name or not
232+
*
233+
* @name Filter by name or not
200234
*/
201235
struct function getSharedConstraints( string name ){
202236
return ( structKeyExists( arguments, "name" ) ? variables.sharedConstraints[ arguments.name ] : variables.sharedConstraints );
203237
}
204238

205239
/**
206240
* Check if a shared constraint exists by name
207-
* @name.hint The shared constraint to check
241+
*
242+
* @name The shared constraint to check
208243
*/
209244
boolean function sharedConstraintsExists( required string name ){
210245
return structKeyExists( variables.sharedConstraints, arguments.name );
@@ -213,7 +248,8 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
213248

214249
/**
215250
* Set the entire shared constraints structure
216-
* @constraints.hint Filter by name or not
251+
*
252+
* @constraints Filter by name or not
217253
*/
218254
IValidationManager function setSharedConstraints( struct constraints ){
219255
variables.sharedConstraints = arguments.constraints;
@@ -222,8 +258,9 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
222258

223259
/**
224260
* Store a shared constraint
225-
* @name.hint Filter by name or not
226-
* @constraint.hint The constraint to store.
261+
*
262+
* @name Filter by name or not
263+
* @constraint The constraint to store.
227264
*/
228265
IValidationManager function addSharedConstraint( required string name, required struct constraint ){
229266
variables.sharedConstraints[ arguments.name ] = arguments.constraints;
@@ -233,19 +270,26 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
233270

234271
/**
235272
* Determine from where to take the constraints from
273+
*
274+
* @target The target object
275+
* @constraints The constraints rules
276+
*
277+
* @throws ValidationManager.InvalidSharedConstraint
236278
*/
237-
private struct function determineConstraintsDefinition(required any target, any constraints=""){
279+
private struct function determineConstraintsDefinition( required any target, any constraints="" ){
238280
var thisConstraints = {};
239281

240282
// if structure, just return it back
241283
if( isStruct( arguments.constraints ) ){ return arguments.constraints; }
242284

243285
// simple value means shared lookup
244-
if( isSimpleValue(arguments.constraints) AND len( arguments.constraints ) ){
245-
if( !sharedConstraintsExists(arguments.constraints) ){
246-
throw(message="The shared constraint you requested (#arguments.constraints#) does not exist",
247-
detail="Valid constraints are: #structKeyList(sharedConstraints)#",
248-
type="ValidationManager.InvalidSharedConstraint");
286+
if( isSimpleValue( arguments.constraints ) AND len( arguments.constraints ) ){
287+
if( !sharedConstraintsExists( arguments.constraints ) ){
288+
throw(
289+
message = "The shared constraint you requested (#arguments.constraints#) does not exist",
290+
detail = "Valid constraints are: #structKeyList(sharedConstraints)#",
291+
type = "ValidationManager.InvalidSharedConstraint"
292+
);
249293
}
250294
// retrieve the shared constraint and return, they are already processed.
251295
return getSharedConstraints( arguments.constraints );
@@ -257,8 +301,10 @@ component accessors="true" serialize="false" implements="IValidationManager" sin
257301

258302
/**
259303
* Get the constraints structure from target objects, if none, it returns an empty structure
304+
*
305+
* @target The target object
260306
*/
261-
private struct function discoverConstraints(required any target){
307+
private struct function discoverConstraints( required any target ){
262308
if( structKeyExists(arguments.target,"constraints") ){
263309
return arguments.target.constraints;
264310
}

tests/specs/ValidationManagerTest.cfc

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.mod
88

99
function setup(){
1010
super.setup();
11-
mockRB = getMockBox().createEmptyMock("cbi18n.models.ResourceService");
11+
mockRB = createEmptyMock("cbi18n.models.ResourceService");
1212
model.init();
1313
model.setWireBox( mockWireBox );
1414
model.setResourceService( mockRB );
1515
}
1616

1717
function testProcessRules(){
18-
results = getMockBox().createMock( "cbvalidation.models.result.ValidationResult" ).init();
18+
results = createMock( "cbvalidation.models.result.ValidationResult" ).init();
1919
//mockValidator = getMockBox().createMock("coldbox.test.specs.validation.resources.MockValidator");
2020

2121
//model.$("getValidator", mockValidator);
@@ -26,17 +26,15 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.mod
2626
udf = variables._validateit
2727
};
2828

29-
getMockBox().prepareMock( this ).$( "getName","luis" ).$( "getJoe", "luis" );
29+
prepareMock( this ).$( "getName","luis" ).$( "getJoe", "luis" );
3030
model.processRules( results=results, rules=mockRules, target=this, field="name" );
3131

3232
assertEquals( 0, results.getErrorCount() );
3333

3434
}
3535

3636
function testIgnoresAllKeysEndingInMessage(){
37-
var results = getMockBox()
38-
.createMock( "cbvalidation.models.result.ValidationResult" )
39-
.init();
37+
var results = createMock( "cbvalidation.models.result.ValidationResult" ).init();
4038

4139
var mockRule = {
4240
required = true,
@@ -52,16 +50,12 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.mod
5250
}
5351

5452
function testProcessRulesLooksForWireBoxMappingOfKeyIfNotAValidValidator() {
55-
var results = getMockBox()
56-
.createMock( "cbvalidation.models.result.ValidationResult" )
57-
.init();
53+
var results = createMock( "cbvalidation.models.result.ValidationResult" ).init();
5854

59-
var customValidatorMock = getMockBox()
60-
.createStub( implements = "cbvalidation.models.validators.IValidator" );
55+
var customValidatorMock = createMock( "cbvalidation.models.validators.RequiredValidator" );
6156
customValidatorMock.$( "validate", true );
6257

63-
var mockBinder = getMockBox()
64-
.createMock( "coldbox.system.ioc.config.Binder" );
58+
var mockBinder = createMock( "coldbox.system.ioc.config.Binder" );
6559
mockBinder.$( "mappingExists" )
6660
.$args( "customValidator" )
6761
.$results( true );
@@ -77,10 +71,9 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbvalidation.mod
7771
customField = "hi"
7872
}
7973
};
80-
8174
var mock = createStub().$( "getName","luis" );
8275
model.processRules( results=results, rules=mockRule, target=mock, field="name" );
83-
76+
8477
assertTrue( customValidatorMock.$once( "validate" ), "[validate] should have been called on [customValidator]" );
8578
var args = customValidatorMock.$callLog().validate[ 1 ];
8679
assertEquals( args.validationData, { customField = "hi" }, "validationData was not passed through correctly to [customValidator]" );

0 commit comments

Comments
 (0)