Skip to content

Commit

Permalink
Merge branch 'pu/pm/AdbContactMakeGroupsPropertyWriteable' into 'main'
Browse files Browse the repository at this point in the history
tweak(Adb Contact) make groups property writeable)

See merge request tine20/tine20!4314
  • Loading branch information
paulmhh committed Feb 26, 2025
2 parents 88235b1 + b5e2a91 commit 4ab5b08
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
60 changes: 59 additions & 1 deletion tests/tine20/Addressbook/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,65 @@ public function testSearchContactCS()
['field' => 'adr_one_street', 'operator' => 'contains', 'value' => 'IsVeryS']
], '', [Tinebase_Model_Filter_Text::CASE_SENSITIVE => true]))->count(), 'cs search did not work');
}


public function testAddRemoveGroups()
{
$listCtrl = Addressbook_Controller_List::getInstance();
$sharedContainer = $this->_getTestContainer(Addressbook_Config::APP_NAME, Addressbook_Model_List::class, true);
$grants = Tinebase_Container::getInstance()->getGrantsOfContainer($sharedContainer->getId());
$grants->find('account_type', 'anyone')->adminGrant = true;
Tinebase_Container::getInstance()->setGrants($sharedContainer->getId(), $grants);
$publicList = $listCtrl->create(new Addressbook_Model_List([
'name' => 'unittest list public',
'container_id' => $sharedContainer->getId(),]));
$publicList2 = $listCtrl->create(new Addressbook_Model_List([
'name' => 'unittest list public2',
'container_id' => $sharedContainer->getId(),]));

$privateContainer = $this->_getTestContainer(Addressbook_Config::APP_NAME, Addressbook_Model_List::class);
$privateList = $listCtrl->create(new Addressbook_Model_List([
'name' => 'unittest list private',
'container_id' => $privateContainer->getId(),
]));

$container = $this->_getTestContainer(Addressbook_Config::APP_NAME, Addressbook_Model_Contact::class, true);
$grants = Tinebase_Container::getInstance()->getGrantsOfContainer($container->getId());
$grants->find('account_type', 'anyone')->adminGrant = true;
Tinebase_Container::getInstance()->setGrants($container->getId(), $grants);
$contact = $this->_instance->create(new Addressbook_Model_Contact([
'container_id' => $container->getId(),
'n_family' => 'unittest',
]));

$listCtrl->addListMember($publicList->getId(), $contact->getId());
$listCtrl->addListMember($privateList->getId(), $contact->getId());

Tinebase_Core::setUser($this->_personas['sclever']);

$contact = $this->_instance->get($contact->getId());

$groups = $contact->groups->getArrayOfIds();
$this->assertContains($publicList->getId(), $groups);
$this->assertNotContains($privateList->getId(), $groups);

unset($groups[array_search($publicList->getId(), $groups)]);
$groups[] = $publicList2->getId();
$contact->groups = $groups;

$contact = $this->_instance->update($contact);
$groups = $contact->groups->getArrayOfIds();
$this->assertContains($publicList2->getId(), $groups);
$this->assertNotContains($publicList->getId(), $groups);
$this->assertNotContains($privateList->getId(), $groups);

Tinebase_Core::setUser($this->_originalTestUser);
$contact = $this->_instance->get($contact->getId());
$groups = $contact->groups->getArrayOfIds();
$this->assertContains($publicList2->getId(), $groups);
$this->assertNotContains($publicList->getId(), $groups);
$this->assertContains($privateList->getId(), $groups);
}

/**
* test remove image
*/
Expand Down
47 changes: 47 additions & 0 deletions tine20/Addressbook/Controller/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,53 @@ public function updateUserProfile($_userProfile)
return $userProfile;
}

/**
* set relations / tags / alarms
*
* @param Tinebase_Record_Interface $updatedRecord the just updated record
* @param Tinebase_Record_Interface $record the update record
* @param Tinebase_Record_Interface $currentRecord the original record if one exists
* @param boolean $returnUpdatedRelatedData
* @param boolean $isCreate
* @return Tinebase_Record_Interface
* @throws Setup_Exception
* @throws Tinebase_Exception_InvalidArgument
* @throws Tinebase_Exception_Record_DefinitionFailure
* @throws Tinebase_Exception_Record_NotAllowed
*/
protected function _setRelatedData(Tinebase_Record_Interface $updatedRecord, Tinebase_Record_Interface $record, Tinebase_Record_Interface $currentRecord = null, $returnUpdatedRelatedData = false, $isCreate = false)
{
if (is_array($groups = $record->groups)) {
foreach ($groups as &$group) {
if ($group['id'] ?? false) {
$group = $group['id'];
}
}
} elseif ($groups instanceof Tinebase_Record_RecordSet) {
$groups = $groups->getArrayOfIds();
} else {
return parent::_setRelatedData($updatedRecord, $record, $currentRecord, $returnUpdatedRelatedData, $isCreate);
}

if ($currentRecord) {
$toAdd = array_diff($groups, $currentRecord->groups->getArrayOfIds());
$toDelete = array_diff($currentRecord->groups->getArrayOfIds(), $groups);
} else {
$toAdd = $groups;
$toDelete = [];
}

foreach ($toAdd as $groupId) {
Addressbook_Controller_List::getInstance()->addListMember($groupId, $updatedRecord->getId());
}
foreach ($toDelete as $groupId) {
Addressbook_Controller_List::getInstance()->removeListMember($groupId, $updatedRecord->getId());
}
$updatedRecord->groups = $groups;

return parent::_setRelatedData($updatedRecord, $record, $currentRecord, $returnUpdatedRelatedData, $isCreate);
}

/**
* inspect update of one record (after update)
*
Expand Down

0 comments on commit 4ab5b08

Please sign in to comment.