-
-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ManagedEntity - Add update mode 'unmodified' and fix cleanup mode 'un…
…used' for APIv4 Update mode 'unmodified' will only update a record if it has not been locally edited. This new setting works only for entities opted-in to the APIv4 ManagedEntity trait, and will emit a warning and fall back on 'always' for others. Cleanup mode 'unmodified' now works for APIv4 managed entities, and they are cleaned up in reverse order to ensure references are deleted before their parents.
- Loading branch information
Showing
3 changed files
with
271 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
ext/search_kit/tests/phpunit/api/v4/SearchDisplay/ManagedSearchTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
namespace api\v4\SearchDisplay; | ||
|
||
use Civi\Api4\SavedSearch; | ||
use Civi\Test\HeadlessInterface; | ||
use Civi\Test\HookInterface; | ||
use Civi\Test\TransactionalInterface; | ||
|
||
/** | ||
* @group headless | ||
*/ | ||
class SearchDownloadTest extends \PHPUnit\Framework\TestCase implements HeadlessInterface, TransactionalInterface, HookInterface { | ||
|
||
/** | ||
* @var array[] | ||
*/ | ||
private $_managedEntities = []; | ||
|
||
public function setUp() { | ||
$this->_managedEntities = []; | ||
parent::setUp(); | ||
} | ||
|
||
public function setUpHeadless() { | ||
// Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile(). | ||
// See: https://docs.civicrm.org/dev/en/latest/testing/phpunit/#civitest | ||
return \Civi\Test::headless() | ||
->installMe(__DIR__) | ||
->apply(); | ||
} | ||
|
||
public function hook_civicrm_managed(array &$entities): void { | ||
$entities = array_merge($entities, $this->_managedEntities); | ||
} | ||
|
||
public function testDeleteUnusedSearch() { | ||
$savedSearch = [ | ||
'module' => 'civicrm', | ||
'name' => 'testDeleteUnusedSearch', | ||
'entity' => 'SavedSearch', | ||
'cleanup' => 'unused', | ||
'update' => 'unmodified', | ||
'params' => [ | ||
'version' => 4, | ||
'values' => [ | ||
'name' => 'testDeleteUnusedSearch', | ||
'label' => 'Test Search', | ||
'description' => 'Original state', | ||
'api_entity' => 'Contact', | ||
'api_params' => [ | ||
'version' => 4, | ||
'select' => ['id'], | ||
], | ||
], | ||
], | ||
]; | ||
$searchDisplay = [ | ||
'module' => 'civicrm', | ||
'name' => 'testDeleteUnusedDisplay', | ||
'entity' => 'SearchDisplay', | ||
'cleanup' => 'unused', | ||
'update' => 'unmodified', | ||
'params' => [ | ||
'version' => 4, | ||
'values' => [ | ||
'type' => 'table', | ||
'name' => 'testDeleteUnusedDisplay', | ||
'label' => 'testDeleteUnusedDisplay', | ||
'saved_search_id.name' => 'testDeleteUnusedSearch', | ||
'settings' => [ | ||
'limit' => 20, | ||
'pager' => TRUE, | ||
'columns' => [ | ||
[ | ||
'key' => 'id', | ||
'label' => 'Contact ID', | ||
'dataType' => 'Integer', | ||
'type' => 'field', | ||
], | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
// Add managed search + display | ||
$this->_managedEntities[] = $savedSearch; | ||
$this->_managedEntities[] = $searchDisplay; | ||
\CRM_Core_ManagedEntities::singleton(TRUE)->reconcile(); | ||
|
||
$search = SavedSearch::get(FALSE) | ||
->selectRowCount() | ||
->addWhere('name', '=', 'testDeleteUnusedSearch') | ||
->execute(); | ||
$this->assertCount(1, $search); | ||
|
||
$this->_managedEntities = []; | ||
\CRM_Core_ManagedEntities::singleton(TRUE)->reconcile(); | ||
|
||
$search = SavedSearch::get(FALSE) | ||
->selectRowCount() | ||
->addWhere('name', '=', 'testDeleteUnusedSearch') | ||
->execute(); | ||
$this->assertCount(0, $search); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters