Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 12d5247

Browse files
cconard96cedric-anne
andauthoredMar 22, 2022
Fix importExternal for tree dropdowns (#11062)
* Fix importExternal for tree dropdowns * Merge all extra fields * Update src/CommonDropdown.php Co-authored-by: Cédric Anne <cedric.anne@gmail.com> Co-authored-by: Cédric Anne <cedric.anne@gmail.com>
1 parent a2b1e7e commit 12d5247

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
 

‎src/CommonDropdown.php

+3
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,9 @@ public function importExternal(
777777
$input["name"] = $res_rule["name"];
778778
}
779779
}
780+
// Merge extra input fields into $input
781+
$input += $external_params;
782+
780783
return ($add ? $this->import($input) : $this->findID($input));
781784
}
782785

‎tests/functionnal/Location.php

+105
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,109 @@ public function testInheritGeolocation()
7373
$this->string($location3->fields['longitude'])->isEqualTo('2.1734');
7474
$this->string($location3->fields['altitude'])->isEqualTo('39');
7575
}
76+
77+
public function testImportExternal()
78+
{
79+
$locations_id = \Dropdown::importExternal('Location', 'testImportExternal_1', getItemByTypeName('Entity', '_test_root_entity', true));
80+
$this->integer((int) $locations_id)->isGreaterThan(0);
81+
// Verify that the location was created
82+
$location = new \Location();
83+
$location->getFromDB($locations_id);
84+
$this->string($location->fields['name'])->isEqualTo('testImportExternal_1');
85+
86+
// Try importing a location as a child of the location we just created
87+
$locations_id_2 = \Dropdown::importExternal('Location', 'testImportExternal_2', getItemByTypeName('Entity', '_test_root_entity', true), [
88+
'locations_id' => $locations_id
89+
]);
90+
$this->integer((int) $locations_id_2)->isGreaterThan(0);
91+
// Verify that the location was created
92+
$location = new \Location();
93+
$location->getFromDB($locations_id_2);
94+
$this->string($location->fields['name'])->isEqualTo('testImportExternal_2');
95+
// Verify that the location is a child of the location we just created
96+
$this->integer($location->fields['locations_id'])->isEqualTo($locations_id);
97+
}
98+
99+
public function testFindIDByName()
100+
{
101+
$entities_id = getItemByTypeName('Entity', '_test_root_entity', true);
102+
103+
// Create a location
104+
$location = new \Location();
105+
$location_id = $location->add([
106+
'name' => 'testFindIDByName_1',
107+
'entities_id' => $entities_id,
108+
]);
109+
$this->integer((int) $location_id)->isGreaterThan(0);
110+
111+
// Find the location by name
112+
$params = [
113+
'name' => 'testFindIDByName_1',
114+
'entities_id' => $entities_id,
115+
];
116+
$found_location_id = $location->findID($params);
117+
$this->integer((int) $found_location_id)->isEqualTo($location_id);
118+
119+
// Add child location
120+
$location_id_2 = $location->add([
121+
'locations_id' => $location_id,
122+
'name' => 'testFindIDByName_2',
123+
'entities_id' => $entities_id,
124+
]);
125+
$this->integer((int) $location_id_2)->isGreaterThan(0);
126+
127+
// Find the location by name (and locations_id)
128+
$params = [
129+
'name' => 'testFindIDByName_2',
130+
'locations_id' => $location_id,
131+
'entities_id' => $entities_id,
132+
];
133+
$found_location_id = $location->findID($params);
134+
$this->integer((int) $found_location_id)->isEqualTo($location_id_2);
135+
136+
// Verify finding ID with just name won't work for child location
137+
$params = [
138+
'name' => 'testFindIDByName_2',
139+
'entities_id' => $entities_id,
140+
];
141+
$found_location_id = $location->findID($params);
142+
$this->integer((int) $found_location_id)->isEqualTo(-1);
143+
}
144+
145+
public function testFindIDByCompleteName()
146+
{
147+
$entities_id = getItemByTypeName('Entity', '_test_root_entity', true);
148+
149+
// Create a location
150+
$location = new \Location();
151+
$location_id = $location->add([
152+
'name' => 'testFindIDByCompleteName_1',
153+
'entities_id' => $entities_id,
154+
]);
155+
$this->integer((int) $location_id)->isGreaterThan(0);
156+
157+
// Find the location by completename
158+
$params = [
159+
'completename' => 'testFindIDByCompleteName_1',
160+
'entities_id' => $entities_id,
161+
];
162+
$found_location_id = $location->findID($params);
163+
$this->integer((int) $found_location_id)->isEqualTo($location_id);
164+
165+
// Create a child location
166+
$location_id_2 = $location->add([
167+
'locations_id' => $location_id,
168+
'name' => 'testFindIDByCompleteName_2',
169+
'entities_id' => $entities_id,
170+
]);
171+
$this->integer((int) $location_id_2)->isGreaterThan(0);
172+
173+
// Find the location by completename
174+
$params = [
175+
'completename' => 'testFindIDByCompleteName_1 > testFindIDByCompleteName_2',
176+
'entities_id' => $entities_id,
177+
];
178+
$found_location_id = $location->findID($params);
179+
$this->integer((int) $found_location_id)->isEqualTo($location_id_2);
180+
}
76181
}

0 commit comments

Comments
 (0)
Please sign in to comment.