Skip to content
Permalink
Browse files

Merge pull request #35939 from Gustry/project_title

check project root name in the server validator
  • Loading branch information
rldhont committed Apr 28, 2020
2 parents 232cf06 + 1276173 commit 19d2dc5b960a682840350d3e650f5fb68ad8c77b
@@ -31,8 +31,10 @@ Constructor for QgsProjectServerValidator.
enum ValidationError
{
DuplicatedNames,
ShortNames,
Encoding
LayerShortName,
LayerEncoding,
ProjectShortName,
ProjectRootNameConflict,
};

static QString displayValidationError( QgsProjectServerValidator::ValidationError error );
@@ -26,12 +26,16 @@ QString QgsProjectServerValidator::displayValidationError( QgsProjectServerValid
{
switch ( error )
{
case QgsProjectServerValidator::Encoding:
case QgsProjectServerValidator::LayerEncoding:
return QObject::tr( "Encoding is not correctly set. A non 'System' encoding is required" );
case QgsProjectServerValidator::ShortNames:
case QgsProjectServerValidator::LayerShortName:
return QObject::tr( "Layer short name is not valid. It must start with an unaccented alphabetical letter, followed by any alphanumeric letters, dot, dash or underscore" );
case QgsProjectServerValidator::DuplicatedNames:
return QObject::tr( "One or more layers or groups have the same name or short name. Both the 'name' and 'short name' for layers and groups must be unique" );
case QgsProjectServerValidator::ProjectShortName:
return QObject::tr( "The project root name (either the project short name or project title) is not valid. It must start with an unaccented alphabetical letter, followed by any alphanumeric letters, dot, dash or underscore" );
case QgsProjectServerValidator::ProjectRootNameConflict:
return QObject::tr( "The project root name (either the project short name or project title) is already used by a layer or a group" );
}
return QString();
}
@@ -119,13 +123,34 @@ bool QgsProjectServerValidator::validate( QgsProject *project, QList<QgsProjectS
if ( !regExpMessages.empty() )
{
result = false;
results << ValidationResult( QgsProjectServerValidator::ShortNames, regExpMessages.join( QStringLiteral( ", " ) ) );
results << ValidationResult( QgsProjectServerValidator::LayerShortName, regExpMessages.join( QStringLiteral( ", " ) ) );
}

if ( !encodingMessages.empty() )
{
result = false;
results << ValidationResult( QgsProjectServerValidator::Encoding, encodingMessages.join( QStringLiteral( ", " ) ) );
results << ValidationResult( QgsProjectServerValidator::LayerEncoding, encodingMessages.join( QStringLiteral( ", " ) ) );
}

// Determine the root layername
QString rootLayerName = project->readEntry( QStringLiteral( "WMSRootName" ), QStringLiteral( "/" ), "" );
if ( rootLayerName.isEmpty() && !project->title().isEmpty() )
{
rootLayerName = project->title();
}
if ( !rootLayerName.isEmpty() )
{
if ( owsNames.count( rootLayerName ) >= 1 )
{
result = false;
results << ValidationResult( QgsProjectServerValidator::ProjectRootNameConflict, rootLayerName );
}

if ( !snRegExp.exactMatch( rootLayerName ) )
{
result = false;
results << ValidationResult( QgsProjectServerValidator::ProjectShortName, rootLayerName );
}
}

return result;
@@ -45,9 +45,11 @@ class CORE_EXPORT QgsProjectServerValidator
*/
enum ValidationError
{
DuplicatedNames = 0, //!< Error related to a duplicated layer name in the layer tree.
ShortNames = 1, //!< Layer short name is well formatted.
Encoding = 2 //!< Encoding is not set correctly.
DuplicatedNames = 0, //!< A duplicated layer/group name in the layer tree.
LayerShortName = 1, //!< Layer/group short name is not valid.
LayerEncoding = 2, //!< Encoding is not correctly set on a vector layer.
ProjectShortName = 3, //!< The project short name is not valid.
ProjectRootNameConflict = 4, //!< The project root name is already used by a layer or a group.
};

/**
@@ -49,7 +49,7 @@ def test_project_server_validator(self):
valid, results = QgsProjectServerValidator.validate(project)
self.assertFalse(valid)
self.assertEqual(1, len(results))
self.assertEqual(QgsProjectServerValidator.ShortNames, results[0].error)
self.assertEqual(QgsProjectServerValidator.LayerShortName, results[0].error)

# Not valid, same short name as the first layer name
layer_1.setShortName('layer_1')
@@ -77,6 +77,40 @@ def test_project_server_validator(self):
self.assertTrue(valid)
self.assertEqual(0, len(results))

# Not valid, the project title is invalid
project.setTitle('@ layer 1')
valid, results = QgsProjectServerValidator.validate(project)
self.assertFalse(valid)
self.assertEqual(1, len(results))
self.assertEqual(QgsProjectServerValidator.ProjectShortName, results[0].error)

# Valid project title
project.setTitle('project_title')
valid, results = QgsProjectServerValidator.validate(project)
self.assertTrue(valid)
self.assertEqual(0, len(results))

# Valid despite the bad project title, use project short name
project.setTitle('@ layer 1')
project.writeEntry('WMSRootName', '/', 'project_short_name')
valid, results = QgsProjectServerValidator.validate(project)
self.assertTrue(valid)
self.assertEqual(0, len(results))

# Not valid project short name
project.setTitle('project_title')
project.writeEntry('WMSRootName', '/', 'project with space')
valid, results = QgsProjectServerValidator.validate(project)
self.assertFalse(valid)
self.assertEqual(1, len(results))
self.assertEqual(QgsProjectServerValidator.ProjectShortName, results[0].error)

# Not valid, duplicated project short name
project.writeEntry('WMSRootName', '/', 'layer_1')
valid, results = QgsProjectServerValidator.validate(project)
self.assertEqual(1, len(results))
self.assertEqual(QgsProjectServerValidator.ProjectRootNameConflict, results[0].error)


if __name__ == '__main__':
unittest.main()

0 comments on commit 19d2dc5

Please sign in to comment.
You can’t perform that action at this time.