From: Eileen McNaughton Date: Fri, 14 May 2021 02:02:00 +0000 (+1200) Subject: Fix test to support invalidate change X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=80723927636e468f5ddee00596b3ff99bc9785ff;p=civicrm-core.git Fix test to support invalidate change --- diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index dfd7c3db30..de13fa74ab 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -84,6 +84,13 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { */ static protected $_dbName; + /** + * API version in use. + * + * @var int + */ + protected $_apiversion = 3; + /** * Track tables we have modified during a test. * diff --git a/tests/phpunit/api/v3/SavedSearchTest.php b/tests/phpunit/api/v3/SavedSearchTest.php index ed303c8ae4..f9e0d660a6 100644 --- a/tests/phpunit/api/v3/SavedSearchTest.php +++ b/tests/phpunit/api/v3/SavedSearchTest.php @@ -34,7 +34,6 @@ */ class api_v3_SavedSearchTest extends CiviUnitTestCase { - protected $_apiversion = 3; protected $params; protected $id; protected $_entity; @@ -46,7 +45,7 @@ class api_v3_SavedSearchTest extends CiviUnitTestCase { // The line below makes it unneccessary to do cleanup after a test, // because the transaction of the test will be rolled back. // see http://forum.civicrm.org/index.php/topic,35627.0.html - $this->useTransaction(TRUE); + $this->useTransaction(); $this->_entity = 'SavedSearch'; @@ -72,7 +71,7 @@ class api_v3_SavedSearchTest extends CiviUnitTestCase { $contactID = $this->createLoggedInUser(); $result = $this->callAPIAndDocument( $this->_entity, 'create', $this->params, __FUNCTION__, __FILE__)['values']; - $this->assertEquals(1, count($result)); + $this->assertCount(1, $result); $savedSearch = reset($result); $this->assertEquals($contactID, $savedSearch['created_id']); @@ -90,6 +89,8 @@ class api_v3_SavedSearchTest extends CiviUnitTestCase { /** * Create a saved search, retrieve it again, and check for ID and one of * the field values. + * + * @throws \CRM_Core_Exception */ public function testCreateAndGetSavedSearch(): void { // Arrange: @@ -114,37 +115,13 @@ class api_v3_SavedSearchTest extends CiviUnitTestCase { /** * Create a saved search, and test whether it can be used for a smart * group. + * + * @throws \CRM_Core_Exception */ public function testCreateSavedSearchWithSmartGroup(): void { // First create a volunteer for the default organization - $result = $this->callAPISuccess('Contact', 'create', [ - 'first_name' => 'Joe', - 'last_name' => 'Schmoe', - 'contact_type' => 'Individual', - 'api.Relationship.create' => [ - 'contact_id_a' => '$value.id', - // default organization: - 'contact_id_b' => 1, - // volunteer relationship: - 'relationship_type_id' => 6, - 'is_active' => 1, - ], - ]); - $contact_id = $result['id']; - - // Now create our saved search, and chain the creation of a smart group. - $params = $this->params; - $params['api.Group.create'] = [ - 'name' => 'my_smartgroup', - 'title' => 'my smartgroup', - 'description' => 'Volunteers for the default organization', - 'saved_search_id' => '$value.id', - 'is_active' => 1, - 'visibility' => 'User and User Admin Only', - 'is_hidden' => 0, - 'is_reserved' => 0, - ]; + [$contact_id, $params] = $this->setupContactInSmartGroup(); $create_result = $this->callAPIAndDocument( $this->_entity, 'create', $params, __FUNCTION__, __FILE__); @@ -153,8 +130,7 @@ class api_v3_SavedSearchTest extends CiviUnitTestCase { $group_id = $created_search['api.Group.create']['id']; // Search for contacts in our new smart group - $get_result = $this->callAPISuccess( - 'Contact', 'get', ['group' => $group_id], __FUNCTION__, __FILE__); + $get_result = $this->callAPISuccess('Contact', 'get', ['group' => $group_id]); // Expect our contact to be there. $this->assertEquals(1, $get_result['count']); @@ -163,13 +139,53 @@ class api_v3_SavedSearchTest extends CiviUnitTestCase { /** * Create a saved search, and test whether it can be used for a smart - * group. Also check that when the Group is deleted the associated saved search gets deleted. + * group. Also check that when the Group is deleted the associated saved + * search gets deleted. + * * @dataProvider versionThreeAndFour + * @throws \CRM_Core_Exception */ public function testSavedSearchIsDeletedWhenSmartGroupIs($apiVersion): void { $this->_apiVersion = $apiVersion; // First create a volunteer for the default organization + [$contact_id, $params] = $this->setupContactInSmartGroup(); + + $create_result = $this->callAPISuccess($this->_entity, 'create', $params); + + $created_search = CRM_Utils_Array::first($create_result['values']); + $group_id = $created_search['api.Group.create']['id']; + + $get_result = $this->callAPISuccess('Contact', 'get', ['group' => $group_id]); + + // Expect our contact to be there. + $this->assertEquals(1, $get_result['count']); + $this->assertEquals($contact_id, $get_result['values'][$contact_id]['id']); + + $this->callAPISuccess('Group', 'delete', ['id' => $group_id]); + $savedSearch = $this->callAPISuccess('SavedSearch', 'get', ['id' => $created_search['id']]); + $this->assertCount(0, $savedSearch['values']); + } + + /** + * @throws \CRM_Core_Exception + */ + public function testDeleteSavedSearch(): void { + // Create saved search, delete it again, and try to get it + $create_result = $this->callAPISuccess($this->_entity, 'create', $this->params); + $delete_params = ['id' => $create_result['id']]; + $this->callAPIAndDocument( + $this->_entity, 'delete', $delete_params, __FUNCTION__, __FILE__); + $get_result = $this->callAPISuccess($this->_entity, 'get', []); + + $this->assertEquals(0, $get_result['count']); + } + + /** + * @return array + * @throws \CRM_Core_Exception + */ + protected function setupContactInSmartGroup(): array { $result = $this->callAPISuccess('Contact', 'create', [ 'first_name' => 'Joe', 'last_name' => 'Schmoe', @@ -197,33 +213,7 @@ class api_v3_SavedSearchTest extends CiviUnitTestCase { 'is_hidden' => 0, 'is_reserved' => 0, ]; - - $create_result = $this->callAPISuccess($this->_entity, 'create', $params); - - $created_search = CRM_Utils_Array::first($create_result['values']); - $group_id = $created_search['api.Group.create']['id']; - - // Search for contacts in our new smart group - $get_result = $this->callAPISuccess('Contact', 'get', ['group' => $group_id]); - - // Expect our contact to be there. - $this->assertEquals(1, $get_result['count']); - $this->assertEquals($contact_id, $get_result['values'][$contact_id]['id']); - - $this->callAPISuccess('Group', 'delete', ['id' => $group_id]); - $savedSearch = $this->callAPISuccess('SavedSearch', 'get', ['id' => $created_search['id']]); - $this->assertCount(0, $savedSearch['values']); - } - - public function testDeleteSavedSearch(): void { - // Create saved search, delete it again, and try to get it - $create_result = $this->callAPISuccess($this->_entity, 'create', $this->params); - $delete_params = ['id' => $create_result['id']]; - $this->callAPIAndDocument( - $this->_entity, 'delete', $delete_params, __FUNCTION__, __FILE__); - $get_result = $this->callAPISuccess($this->_entity, 'get', []); - - $this->assertEquals(0, $get_result['count']); + return [$contact_id, $params]; } }