Skip to content

Commit 3ff97be

Browse files
committed
use new method GetOwnerOrganizationAttCode in Attachment
+ test
1 parent add7743 commit 3ff97be

File tree

4 files changed

+66
-56
lines changed

4 files changed

+66
-56
lines changed

datamodels/2.x/itop-attachments/datamodel.itop-attachments.xml

+12-25
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,16 @@
160160
$this->Set('item_class', $sClass);
161161
$this->Set('item_id', $iItemId);
162162
163-
164163
$sAttCode = UserRights::GetOwnerOrganizationAttCode( $sClass);
165164
if (is_null($sAttCode)) {
166165
// No need for silos
167166
return;
168167
}
169168
$iOrgId = $oItem->Get($sAttCode);
170-
if ($iOrgId > 0)
171-
{
172-
if ($iOrgId != $this->Get('item_org_id'))
173-
{
169+
if ($iOrgId > 0) {
170+
if ($iOrgId != $this->Get('item_org_id')) {
174171
$this->Set('item_org_id', $iOrgId);
175-
if ($bUpdateOnChange)
176-
{
172+
if ($bUpdateOnChange) {
177173
$this->DBUpdate();
178174
}
179175
}
@@ -191,24 +187,15 @@
191187
<code><![CDATA[ public function SetDefaultOrgId()
192188
{
193189
// Check that the organization CAN be fetched from the current user
194-
//
195-
if (MetaModel::IsValidClass('Person'))
196-
{
197-
$aCallSpec = array('Person', 'MapContextParam');
198-
if (is_callable($aCallSpec))
199-
{
200-
$sAttCode = call_user_func($aCallSpec, 'org_id'); // Returns null when there is no mapping for this parameter
201-
if (MetaModel::IsValidAttCode('Person', $sAttCode))
202-
{
203-
// OK - try it
204-
//
205-
$oCurrentPerson = MetaModel::GetObject('Person', UserRights::GetContactId(), false);
206-
if ($oCurrentPerson)
207-
{
208-
$this->Set('item_org_id', $oCurrentPerson->Get($sAttCode));
209-
}
210-
}
211-
}
190+
$sOrgAttrCodeForPerson = UserRights::GetOwnerOrganizationAttCode('Person');
191+
if (is_null($sOrgAttrCodeForPerson)) {
192+
// No need for silos
193+
return;
194+
}
195+
196+
$oCurrentPerson = MetaModel::GetObject('Person', UserRights::GetContactId(), false);
197+
if ($oCurrentPerson) {
198+
$this->Set('item_org_id', $oCurrentPerson->Get($sOrgAttrCodeForPerson));
212199
}
213200
}]]></code>
214201
</method>

tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php

+33
Original file line numberDiff line numberDiff line change
@@ -1406,4 +1406,37 @@ protected function SkipIfModuleNotPresent(string $sModule): void
14061406
self::markTestSkipped("Test skipped: module '$sModule' is not present");
14071407
}
14081408
}
1409+
1410+
protected function GivenUserLoggedInWithContact(int $iContactOrgId)
1411+
{
1412+
$iContactId = $this->GivenObjectInDB('Person', [
1413+
'first_name' => 'TestContact',
1414+
'name' => 'TestContact',
1415+
'org_id' => $iContactOrgId]);
1416+
$sLogin = 'demo_test_'.uniqid(__CLASS__, true);
1417+
$iUser = $this->GivenObjectInDB('UserLocal', [
1418+
'login' => $sLogin,
1419+
'password' => 'tagada-Secret,007',
1420+
'language' => 'EN US',
1421+
'contactid' => $iContactId,
1422+
'profile_list' => [
1423+
'profileid:'.self::$aURP_Profiles['Configuration Manager']
1424+
]
1425+
]);
1426+
\UserRights::Login($sLogin);
1427+
}
1428+
1429+
protected function GivenUserLoggedInWithoutContact()
1430+
{
1431+
$sLogin = 'demo_test_'.uniqid(__CLASS__, true);
1432+
$iUser = $this->GivenObjectInDB('UserLocal', [
1433+
'login' => $sLogin,
1434+
'password' => 'tagada-Secret,007',
1435+
'language' => 'EN US',
1436+
'profile_list' => [
1437+
'profileid:'.self::$aURP_Profiles['Configuration Manager']
1438+
]
1439+
]);
1440+
\UserRights::Login($sLogin);
1441+
}
14091442
}

tests/php-unit-tests/unitary-tests/core/InlineImageTest.php

-31
Original file line numberDiff line numberDiff line change
@@ -88,35 +88,4 @@ public function testSetDefaultOrgIdWhenLoggedInWithoutContact()
8888
$this->assertEquals(0, $oInlineImage->Get('item_org_id'),'The org_id should be left undefined');
8989
}
9090

91-
private function GivenUserLoggedInWithContact(int $iContactOrgId)
92-
{
93-
$iContactId = $this->GivenObjectInDB('Person', [
94-
'first_name' => 'TestContact',
95-
'name' => 'TestContact',
96-
'org_id' => $iContactOrgId]);
97-
$sLogin = 'demo_test_'.uniqid(__CLASS__, true);
98-
$iUser = $this->GivenObjectInDB('UserLocal', [
99-
'login' => $sLogin,
100-
'password' => 'tagada-Secret,007',
101-
'language' => 'EN US',
102-
'contactid' => $iContactId,
103-
'profile_list' => [
104-
'profileid:'.self::$aURP_Profiles['Configuration Manager']
105-
]
106-
]);
107-
\UserRights::Login($sLogin);
108-
}
109-
private function GivenUserLoggedInWithoutContact()
110-
{
111-
$sLogin = 'demo_test_'.uniqid(__CLASS__, true);
112-
$iUser = $this->GivenObjectInDB('UserLocal', [
113-
'login' => $sLogin,
114-
'password' => 'tagada-Secret,007',
115-
'language' => 'EN US',
116-
'profile_list' => [
117-
'profileid:'.self::$aURP_Profiles['Configuration Manager']
118-
]
119-
]);
120-
\UserRights::Login($sLogin);
121-
}
12291
}

tests/php-unit-tests/unitary-tests/datamodels/2.x/itop-attachments/TestAttachment.php

+21
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,25 @@ public function testSetItemOnObjectWithoutDefinedOrganization()
9090
$oAttachment->SetItem($oUserRequest);
9191
$this->assertEquals(0, $oAttachment->Get('item_org_id'),'The org_id should be the one of the contact');
9292
}
93+
94+
95+
public function testSetDefaultOrgIdWhenLoggedInWithContact()
96+
{
97+
$iContactOrgId = $this->GivenObjectInDB('Organization', ['name' => 'TestOrg']);
98+
$this->GivenUserLoggedInWithContact($iContactOrgId);
99+
100+
$oAttachment = new \Attachment();
101+
$oAttachment->SetDefaultOrgId();
102+
$this->assertEquals($iContactOrgId, $oAttachment->Get('item_org_id'),'The org_id should be the one of the contact');
103+
}
104+
105+
106+
public function testSetDefaultOrgIdWhenLoggedInWithoutContact()
107+
{
108+
$this->GivenUserLoggedInWithoutContact();
109+
110+
$oAttachment = new \Attachment();
111+
$oAttachment->SetDefaultOrgId();
112+
$this->assertEquals(0, $oAttachment->Get('item_org_id'),'The org_id should be left undefined');
113+
}
93114
}

0 commit comments

Comments
 (0)