Remove unnecessary project from test data

This commit is contained in:
Alessandro Pasotti 2018-11-03 11:28:55 +01:00
parent 7a259c6ea5
commit a4182c1e04
8 changed files with 51 additions and 26 deletions

View File

@ -1237,7 +1237,7 @@ Returns true if the refresh on provider nofification is enabled
.. versionadded:: 3.0 .. versionadded:: 3.0
%End %End
QDomDocument originalXmlProperties() const; QString originalXmlProperties() const;
%Docstring %Docstring
Returns the XML properties of the original layer as they were when the layer Returns the XML properties of the original layer as they were when the layer
was first read from the project file. In case of new layers this is normally empty. was first read from the project file. In case of new layers this is normally empty.
@ -1247,7 +1247,7 @@ The storage format for the XML is qlr
.. versionadded:: 3.6 .. versionadded:: 3.6
%End %End
void setOriginalXmlProperties( const QDomDocument &originalXmlProperties ); void setOriginalXmlProperties( const QString &originalXmlProperties );
%Docstring %Docstring
Sets the original XML properties for the layer to ``originalXmlProperties`` Sets the original XML properties for the layer to ``originalXmlProperties``

View File

@ -6955,17 +6955,26 @@ void QgisApp::changeDataSource( QgsMapLayer *layer )
bool layerIsValid( layer->isValid() ); bool layerIsValid( layer->isValid() );
layer->setDataSource( uri.uri, layer->name(), uri.providerKey, QgsDataProvider::ProviderOptions() ); layer->setDataSource( uri.uri, layer->name(), uri.providerKey, QgsDataProvider::ProviderOptions() );
// Re-apply style // Re-apply style
if ( !( layerIsValid || layer->originalXmlProperties().isNull() ) ) if ( !( layerIsValid || layer->originalXmlProperties().isEmpty() ) )
{ {
QgsReadWriteContext context; QgsReadWriteContext context;
context.setPathResolver( QgsProject::instance()->pathResolver() ); context.setPathResolver( QgsProject::instance()->pathResolver() );
context.setProjectTranslator( QgsProject::instance() ); context.setProjectTranslator( QgsProject::instance() );
QString errorMsg; QString errorMsg;
QDomDocument doc( layer->originalXmlProperties() ); QDomDocument doc;
QDomNode layer_node( doc.firstChild( ) ); if ( doc.setContent( layer->originalXmlProperties() ) )
if ( ! layer->readSymbology( layer_node, errorMsg, context ) )
{ {
QgsDebugMsg( QStringLiteral( "Failed to restore original layer style from stored XML for layer %1: %2" ) QDomNode layer_node( doc.firstChild( ) );
if ( ! layer->readSymbology( layer_node, errorMsg, context ) )
{
QgsDebugMsg( QStringLiteral( "Failed to restore original layer style from stored XML for layer %1: %2" )
.arg( layer->name( ) )
.arg( errorMsg ) );
}
}
else
{
QgsDebugMsg( QStringLiteral( "Failed to create XML QDomDocument for layer %1: %2" )
.arg( layer->name( ) ) .arg( layer->name( ) )
.arg( errorMsg ) ); .arg( errorMsg ) );
} }

View File

@ -329,7 +329,10 @@ void QgsLayerTreeUtils::storeOriginalLayersProperties( QgsLayerTreeGroup *group,
QDomDocument document( documentType ); QDomDocument document( documentType );
QDomElement element = mlNode.toElement(); QDomElement element = mlNode.toElement();
document.appendChild( element ); document.appendChild( element );
l->setOriginalXmlProperties( document ); QString str;
QTextStream stream( &str );
document.save( stream, 4 /*indent*/ );
l->setOriginalXmlProperties( str );
} }
} }
} }

View File

@ -1833,12 +1833,12 @@ bool QgsMapLayer::isReadOnly() const
return true; return true;
} }
QDomDocument QgsMapLayer::originalXmlProperties() const QString QgsMapLayer::originalXmlProperties() const
{ {
return mOriginalXmlProperties; return mOriginalXmlProperties;
} }
void QgsMapLayer::setOriginalXmlProperties( const QDomDocument &originalXmlProperties ) void QgsMapLayer::setOriginalXmlProperties( const QString &originalXmlProperties )
{ {
mOriginalXmlProperties = originalXmlProperties; mOriginalXmlProperties = originalXmlProperties;
} }

View File

@ -1109,7 +1109,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
* *
* \since QGIS 3.6 * \since QGIS 3.6
*/ */
QDomDocument originalXmlProperties() const; QString originalXmlProperties() const;
/** /**
* Sets the original XML properties for the layer to \a originalXmlProperties * Sets the original XML properties for the layer to \a originalXmlProperties
@ -1118,7 +1118,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
* *
* \since QGIS 3.6 * \since QGIS 3.6
*/ */
void setOriginalXmlProperties( const QDomDocument &originalXmlProperties ); void setOriginalXmlProperties( const QString &originalXmlProperties );
public slots: public slots:
@ -1529,7 +1529,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
* *
* This information can be used to pass through the bad layers or to reset changes on a good layer * This information can be used to pass through the bad layers or to reset changes on a good layer
*/ */
QDomDocument mOriginalXmlProperties; QString mOriginalXmlProperties;
}; };

View File

@ -1774,10 +1774,17 @@ bool QgsProject::writeProjectFile( const QString &filename )
maplayerElem = doc->createElement( QStringLiteral( "maplayer" ) ); maplayerElem = doc->createElement( QStringLiteral( "maplayer" ) );
ml->writeLayerXml( maplayerElem, *doc, context ); ml->writeLayerXml( maplayerElem, *doc, context );
} }
else if ( ! ml->originalXmlProperties().isNull() ) else if ( ! ml->originalXmlProperties().isEmpty() )
{ {
QDomDocument document( ml->originalXmlProperties() ); QDomDocument document;
maplayerElem = document.firstChildElement(); if ( document.setContent( ml->originalXmlProperties() ) )
{
maplayerElem = document.firstChildElement();
}
else
{
QgsDebugMsg( QStringLiteral( "Could not restore layer properties for layer %1" ).arg( ml->id() ) );
}
} }
emit writeMapLayer( ml, maplayerElem, *doc ); emit writeMapLayer( ml, maplayerElem, *doc );

View File

@ -35,7 +35,6 @@ from qgis.gui import (QgsLayerTreeMapCanvasBridge,
from qgis.PyQt.QtGui import QFont, QColor from qgis.PyQt.QtGui import QFont, QColor
from qgis.PyQt.QtTest import QSignalSpy from qgis.PyQt.QtTest import QSignalSpy
from qgis.PyQt.QtCore import QT_VERSION_STR, QTemporaryDir, QSize from qgis.PyQt.QtCore import QT_VERSION_STR, QTemporaryDir, QSize
from qgis.PyQt.QtXml import QDomDocument, QDomNode, QDomImplementation
from qgis.testing import start_app, unittest from qgis.testing import start_app, unittest
from utilities import (unitTestDataPath, renderMapToImage) from utilities import (unitTestDataPath, renderMapToImage)
@ -100,12 +99,8 @@ class TestQgsProjectBadLayers(unittest.TestCase):
self.assertTrue(raster.originalXmlProperties() != '') self.assertTrue(raster.originalXmlProperties() != '')
self.assertTrue(raster_copy.originalXmlProperties() != '') self.assertTrue(raster_copy.originalXmlProperties() != '')
# Test setter # Test setter
domimp = QDomImplementation() raster.setOriginalXmlProperties('pippo')
documentType = domimp.createDocumentType("qgis", "http://mrcc.com/qgis.dtd", "SYSTEM") self.assertEqual(raster.originalXmlProperties(), 'pippo')
document = QDomDocument(documentType)
document.setContent("<pippo>pluto</pippo>")
raster.setOriginalXmlProperties(document)
self.assertEqual(raster.originalXmlProperties(), document)
# Now create and invalid project: # Now create and invalid project:
bad_project_path = os.path.join(temp_dir.path(), 'project_bad.qgs') bad_project_path = os.path.join(temp_dir.path(), 'project_bad.qgs')
@ -149,7 +144,7 @@ class TestQgsProjectBadLayers(unittest.TestCase):
self.assertTrue(raster_copy.isValid()) self.assertTrue(raster_copy.isValid())
def test_project_relations(self): def test_project_relations(self):
"""Tests that a project with bad layers and relations can be maintained""" """Tests that a project with bad layers and relations can be saved with relations"""
temp_dir = QTemporaryDir() temp_dir = QTemporaryDir()
p = QgsProject.instance() p = QgsProject.instance()
@ -240,12 +235,13 @@ class TestQgsProjectBadLayers(unittest.TestCase):
p = QgsProject.instance() p = QgsProject.instance()
project_path = os.path.join(temp_dir.path(), 'good_layers_test.qgs') project_path = os.path.join(temp_dir.path(), 'good_layers_test.qgs')
copyfile(os.path.join(TEST_DATA_DIR, 'projects', 'good_layers_test.qgs'), project_path) copyfile(os.path.join(TEST_DATA_DIR, 'projects', 'good_layers_test.qgs'), project_path)
copyfile(os.path.join(TEST_DATA_DIR, 'projects', 'bad_layers_test.gpkg'), os.path.join(temp_dir.path(), 'bad_layers_test.gpkg'))
for f in ( for f in (
'bad_layer_raster_test.tfw', 'bad_layer_raster_test.tfw',
'bad_layer_raster_test.tiff', 'bad_layer_raster_test.tiff',
'bad_layer_raster_test.tiff.aux.xml', 'bad_layer_raster_test.tiff.aux.xml',
'bad_layers_test.gpkg', 'bad_layers_test.gpkg',
'bad_layers_test.qgs'): 'good_layers_test.qgs'):
copyfile(os.path.join(TEST_DATA_DIR, 'projects', f), os.path.join(temp_dir.path(), f)) copyfile(os.path.join(TEST_DATA_DIR, 'projects', f), os.path.join(temp_dir.path(), f))
p = QgsProject().instance() p = QgsProject().instance()

View File

@ -20,6 +20,7 @@ from qgis.core import (QgsVectorLayer,
QgsGeometry, QgsGeometry,
QgsPointXY, QgsPointXY,
QgsAttributeEditorElement, QgsAttributeEditorElement,
QgsAttributeEditorRelation,
QgsProject QgsProject
) )
from utilities import unitTestDataPath from utilities import unitTestDataPath
@ -162,7 +163,10 @@ class TestQgsRelation(unittest.TestCase):
def testValidRelationAfterChangingStyle(self): def testValidRelationAfterChangingStyle(self):
# load project # load project
myPath = os.path.join(unitTestDataPath(), 'relations.qgs') myPath = os.path.join(unitTestDataPath(), 'relations.qgs')
QgsProject.instance().read(myPath) p = QgsProject.instance()
self.assertTrue(p.read(myPath))
for l in p.mapLayers().values():
self.assertTrue(l.isValid())
# get referenced layer # get referenced layer
relations = QgsProject.instance().relationManager().relations() relations = QgsProject.instance().relationManager().relations()
@ -171,6 +175,7 @@ class TestQgsRelation(unittest.TestCase):
# check that the relation is valid # check that the relation is valid
valid = False valid = False
self.assertEqual(len(referencedLayer.editFormConfig().tabs()[0].children()), 7)
for tab in referencedLayer.editFormConfig().tabs(): for tab in referencedLayer.editFormConfig().tabs():
for t in tab.children(): for t in tab.children():
if (t.type() == QgsAttributeEditorElement.AeTypeRelation): if (t.type() == QgsAttributeEditorElement.AeTypeRelation):
@ -180,6 +185,11 @@ class TestQgsRelation(unittest.TestCase):
# update style # update style
referencedLayer.styleManager().setCurrentStyle("custom") referencedLayer.styleManager().setCurrentStyle("custom")
for l in p.mapLayers().values():
self.assertTrue(l.isValid())
self.assertEqual(len(referencedLayer.editFormConfig().tabs()[0].children()), 7)
# check that the relation is still valid # check that the relation is still valid
referencedLayer = relation.referencedLayer() referencedLayer = relation.referencedLayer()
valid = False valid = False