@@ -1219,3 +1219,56 @@ void TestAttributeController::testPhotoSketchingSave()
12191219 QCOMPARE ( f.attribute ( testCaseResult.fieldIdx ), testCaseResult.expectedNewFieldValue );
12201220 }
12211221}
1222+
1223+ void TestAttributeController::testPrefillRelationReferenceField ()
1224+ {
1225+ QString projectDir = TestUtils::testDataDir () + " /planes" ;
1226+ QVERIFY ( QgsProject::instance ()->read ( projectDir + " /quickapp_project.qgs" ) );
1227+
1228+ QgsVectorLayer *airportsLayer = dynamic_cast <QgsVectorLayer *>(
1229+ QgsProject::instance ()->mapLayersByName ( QStringLiteral ( " airports" ) ).at ( 0 ) );
1230+ QVERIFY ( airportsLayer && airportsLayer->isValid () );
1231+
1232+ QgsVectorLayer *towersLayer = dynamic_cast <QgsVectorLayer *>(
1233+ QgsProject::instance ()->mapLayersByName ( QStringLiteral ( " airport-towers" ) ).at ( 0 ) );
1234+ QVERIFY ( towersLayer && towersLayer->isValid () );
1235+
1236+ QgsRelation relation = QgsProject::instance ()->relationManager ()->relation (
1237+ QStringLiteral ( " airport_to_airport_fk_airports_3_fid" ) );
1238+ QVERIFY ( relation.isValid () );
1239+
1240+ // parent controller holds an existing airports feature
1241+ QgsFeature parentFeature = airportsLayer->getFeature ( 1 );
1242+ QVERIFY ( parentFeature.isValid () );
1243+
1244+ AttributeController parentController;
1245+ parentController.setFeatureLayerPair ( FeatureLayerPair ( parentFeature, airportsLayer ) );
1246+
1247+ // child controller holds a new empty airport-towers feature
1248+ QgsFeature childFeature;
1249+ childFeature.setValid ( true );
1250+ childFeature.setFields ( towersLayer->fields (), true );
1251+
1252+ AttributeController childController;
1253+ childController.setFeatureLayerPair ( FeatureLayerPair ( childFeature, towersLayer ) );
1254+ childController.setLinkedRelation ( relation );
1255+ childController.setParentController ( &parentController ); // triggers prefillRelationReferenceField
1256+
1257+ // find the airport_fk FormItem in the child controller
1258+ const FormItem *fkItem = nullptr ;
1259+ const TabItem *tab = childController.tabItem ( 0 );
1260+ QVERIFY ( tab );
1261+ for ( const QUuid &id : tab->formItems () )
1262+ {
1263+ const FormItem *item = childController.formItem ( id );
1264+ if ( item && item->field ().name () == QLatin1String ( " airport_fk" ) )
1265+ {
1266+ fkItem = item;
1267+ break ;
1268+ }
1269+ }
1270+
1271+ // compare that the rawValue is updated after setting the linked relation
1272+ QVERIFY ( fkItem );
1273+ QCOMPARE ( fkItem->rawValue (), parentFeature.attribute ( QStringLiteral ( " fid" ) ) );
1274+ }
0 commit comments