Skip to content

Commit

Permalink
[processing] More stringent testing of parameter validity
Browse files Browse the repository at this point in the history
- Check static QgsProperty parameter values
- Properly check QgsProcessingFeatureSourceDefinition and
QgsProcessingOutputLayerDefinition values
  • Loading branch information
nyalldawson committed Apr 27, 2018
1 parent e25fd4c commit c314639
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 9 deletions.
76 changes: 67 additions & 9 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -2582,14 +2582,24 @@ QgsProcessingParameterDefinition *QgsProcessingParameterVectorLayer::clone() con
return new QgsProcessingParameterVectorLayer( *this );
}

bool QgsProcessingParameterVectorLayer::checkValueIsAcceptable( const QVariant &var, QgsProcessingContext *context ) const
bool QgsProcessingParameterVectorLayer::checkValueIsAcceptable( const QVariant &v, QgsProcessingContext *context ) const
{
if ( !var.isValid() )
if ( !v.isValid() )
return mFlags & FlagOptional;

QVariant var = v;

if ( var.canConvert<QgsProperty>() )
{
return true;
QgsProperty p = var.value< QgsProperty >();
if ( p.propertyType() == QgsProperty::StaticProperty )
{
var = p.staticValue();
}
else
{
return true;
}
}

if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( var ) ) )
Expand Down Expand Up @@ -2899,7 +2909,15 @@ bool QgsProcessingParameterFeatureSource::checkValueIsAcceptable( const QVariant

if ( var.canConvert<QgsProperty>() )
{
return true;
QgsProperty p = var.value< QgsProperty >();
if ( p.propertyType() == QgsProperty::StaticProperty )
{
var = p.staticValue();
}
else
{
return true;
}
}
if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( input ) ) )
{
Expand Down Expand Up @@ -3081,7 +3099,15 @@ bool QgsProcessingParameterFeatureSink::checkValueIsAcceptable( const QVariant &

if ( var.canConvert<QgsProperty>() )
{
return true;
QgsProperty p = var.value< QgsProperty >();
if ( p.propertyType() == QgsProperty::StaticProperty )
{
var = p.staticValue();
}
else
{
return true;
}
}

if ( var.type() != QVariant::String )
Expand Down Expand Up @@ -3274,7 +3300,15 @@ bool QgsProcessingParameterRasterDestination::checkValueIsAcceptable( const QVar

if ( var.canConvert<QgsProperty>() )
{
return true;
QgsProperty p = var.value< QgsProperty >();
if ( p.propertyType() == QgsProperty::StaticProperty )
{
var = p.staticValue();
}
else
{
return true;
}
}

if ( var.type() != QVariant::String )
Expand Down Expand Up @@ -3357,7 +3391,15 @@ bool QgsProcessingParameterFileDestination::checkValueIsAcceptable( const QVaria

if ( var.canConvert<QgsProperty>() )
{
return true;
QgsProperty p = var.value< QgsProperty >();
if ( p.propertyType() == QgsProperty::StaticProperty )
{
var = p.staticValue();
}
else
{
return true;
}
}

if ( var.type() != QVariant::String )
Expand Down Expand Up @@ -3465,7 +3507,15 @@ bool QgsProcessingParameterFolderDestination::checkValueIsAcceptable( const QVar

if ( var.canConvert<QgsProperty>() )
{
return true;
QgsProperty p = var.value< QgsProperty >();
if ( p.propertyType() == QgsProperty::StaticProperty )
{
var = p.staticValue();
}
else
{
return true;
}
}

if ( var.type() != QVariant::String )
Expand Down Expand Up @@ -3555,7 +3605,15 @@ bool QgsProcessingParameterVectorDestination::checkValueIsAcceptable( const QVar

if ( var.canConvert<QgsProperty>() )
{
return true;
QgsProperty p = var.value< QgsProperty >();
if ( p.propertyType() == QgsProperty::StaticProperty )
{
var = p.staticValue();
}
else
{
return true;
}
}

if ( var.type() != QVariant::String )
Expand Down
19 changes: 19 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -4006,6 +4006,8 @@ void TestQgsProcessing::parameterVectorLayer()
QVERIFY( !def->checkValueIsAcceptable( QgsProcessingFeatureSourceDefinition( "layer1231123" ) ) );
QVERIFY( def->checkValueIsAcceptable( QVariant::fromValue( v1 ) ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant::fromValue( r1 ) ) );
QVERIFY( def->checkValueIsAcceptable( QgsProperty::fromValue( QStringLiteral( "layer12312312" ) ) ) );
QVERIFY( !def->checkValueIsAcceptable( QgsProperty::fromValue( QString() ) ) );

// should be OK
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp" ) );
Expand Down Expand Up @@ -4114,8 +4116,11 @@ void TestQgsProcessing::parameterFeatureSource()
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
QVERIFY( def->checkValueIsAcceptable( QgsProcessingFeatureSourceDefinition( "layer1231123" ) ) );
QVERIFY( !def->checkValueIsAcceptable( QgsProcessingFeatureSourceDefinition( "" ) ) );
QVERIFY( def->checkValueIsAcceptable( QVariant::fromValue( v1 ) ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant::fromValue( r1 ) ) );
QVERIFY( def->checkValueIsAcceptable( QgsProperty::fromValue( QStringLiteral( "layer12312312" ) ) ) );
QVERIFY( !def->checkValueIsAcceptable( QgsProperty::fromValue( QString() ) ) );

// should be OK
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp" ) );
Expand Down Expand Up @@ -4252,6 +4257,9 @@ void TestQgsProcessing::parameterFeatureSink()
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
QVERIFY( def->checkValueIsAcceptable( QgsProcessingOutputLayerDefinition( "layer1231123" ) ) );
QVERIFY( !def->checkValueIsAcceptable( QgsProcessingOutputLayerDefinition( "" ) ) );
QVERIFY( def->checkValueIsAcceptable( QgsProperty::fromValue( QStringLiteral( "layer12312312" ) ) ) );
QVERIFY( !def->checkValueIsAcceptable( QgsProperty::fromValue( QString() ) ) );

// should be OK with or without context - it's an output layer!
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp" ) );
Expand Down Expand Up @@ -4376,6 +4384,9 @@ void TestQgsProcessing::parameterVectorOut()
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
QVERIFY( def->checkValueIsAcceptable( QgsProcessingOutputLayerDefinition( "layer1231123" ) ) );
QVERIFY( !def->checkValueIsAcceptable( QgsProcessingOutputLayerDefinition( "" ) ) );
QVERIFY( def->checkValueIsAcceptable( QgsProperty::fromValue( QStringLiteral( "layer1231123" ) ) ) );
QVERIFY( !def->checkValueIsAcceptable( QgsProperty::fromValue( QString() ) ) );

// should be OK with or without context - it's an output layer!
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp" ) );
Expand Down Expand Up @@ -4478,6 +4489,9 @@ void TestQgsProcessing::parameterRasterOut()
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
QVERIFY( def->checkValueIsAcceptable( QgsProcessingOutputLayerDefinition( "layer1231123" ) ) );
QVERIFY( !def->checkValueIsAcceptable( QgsProcessingOutputLayerDefinition( "" ) ) );
QVERIFY( def->checkValueIsAcceptable( QgsProperty::fromValue( QStringLiteral( "layer12312312" ) ) ) );
QVERIFY( !def->checkValueIsAcceptable( QgsProperty::fromValue( QString() ) ) );

// should be OK with or without context - it's an output layer!
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.tif" ) );
Expand Down Expand Up @@ -4601,6 +4615,9 @@ void TestQgsProcessing::parameterFileOut()
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
QVERIFY( def->checkValueIsAcceptable( QgsProcessingOutputLayerDefinition( "layer1231123" ) ) );
QVERIFY( !def->checkValueIsAcceptable( QgsProcessingOutputLayerDefinition( "" ) ) );
QVERIFY( def->checkValueIsAcceptable( QgsProperty::fromValue( QStringLiteral( "layer12312312" ) ) ) );
QVERIFY( !def->checkValueIsAcceptable( QgsProperty::fromValue( QString() ) ) );

// should be OK with or without context - it's an output file!
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.txt" ) );
Expand Down Expand Up @@ -4694,6 +4711,8 @@ void TestQgsProcessing::parameterFolderOut()
QVERIFY( def->checkValueIsAcceptable( "asdasd" ) );
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
QVERIFY( def->checkValueIsAcceptable( QgsProperty::fromValue( QStringLiteral( "asdasdas" ) ) ) );
QVERIFY( !def->checkValueIsAcceptable( QgsProperty::fromValue( QString() ) ) );

// should be OK with or without context - it's an output folder!
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/" ) );
Expand Down

0 comments on commit c314639

Please sign in to comment.