4 +--------------------------------------------------------------------+
6 +--------------------------------------------------------------------+
7 | Copyright Chirojeugd-Vlaanderen vzw 2015 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
30 * Class api_v3_SavedSearchTest
32 * @package CiviCRM_APIv3
35 class api_v3_SavedSearchTest
extends CiviUnitTestCase
{
37 protected $_apiversion = 3;
41 public $DBResetRequired = FALSE;
43 public function setUp() {
46 // The line below makes it unneccessary to do cleanup after a test,
47 // because the transaction of the test will be rolled back.
48 // see http://forum.civicrm.org/index.php/topic,35627.0.html
49 $this->useTransaction(TRUE);
51 $this->_entity
= 'SavedSearch';
53 // I created a smart group using the CiviCRM gui. The smart group contains
54 // all contacts tagged with 'company'.
55 // I got the params below from the database.
57 $url = CIVICRM_UF_BASEURL
. "/civicrm/contact/search/advanced?reset=1";
58 $serialized_url = serialize($url);
60 // params for saved search that returns all volunteers for the
61 // default organization.
62 $this->params
= array(
63 'form_values' => array(
65 'relation_type_id' => '6_a_b',
66 'relation_target_name' => 'Default Organization',
72 * Create a saved search, and see whether the returned values make sense.
74 public function testCreateSavedSearch() {
76 $result = $this->callAPIAndDocument(
77 $this->_entity
, 'create', $this->params
, __FUNCTION__
, __FILE__
);
78 $this->assertEquals(1, $result['count']);
81 // getAndCheck fails, I think because form_values is an array.
82 //$this->getAndCheck($this->params, $result['id'], $this->_entity);
83 // Check whether the new ID is correctly returned by the API.
84 $this->assertNotNull($result['values'][$result['id']]['id']);
86 // Check whether the relation type ID is correctly returned.
88 $this->params
['form_values']['relation_type_id'],
89 $result['values'][$result['id']]['form_values']['relation_type_id']);
93 * Create a saved search, retrieve it again, and check for ID and one of
96 public function testCreateAndGetSavedSearch() {
98 // (create a saved search)
99 $create_result = $this->callAPISuccess(
100 $this->_entity
, 'create', $this->params
);
103 $get_result = $this->callAPIAndDocument(
104 $this->_entity
, 'get', array('id' => $create_result['id']), __FUNCTION__
, __FILE__
);
107 $this->assertEquals(1, $get_result['count']);
108 $this->assertNotNull($get_result['values'][$get_result['id']]['id']);
110 // just check the relationship type ID of the form values.
112 $this->params
['form_values']['relation_type_id'],
113 $get_result['values'][$get_result['id']]['form_values']['relation_type_id']);
117 * Create a saved search, and test whether it can be used for a smart
120 public function testCreateSavedSearchWithSmartGroup() {
121 // First create a volunteer for the default organization
123 $result = $this->callAPISuccess('Contact', 'create', array(
124 'first_name' => 'Joe',
125 'last_name' => 'Schmoe',
126 'contact_type' => 'Individual',
127 'api.Relationship.create' => array(
128 'contact_id_a' => '$value.id',
129 // default organization:
131 // volunteer relationship:
132 'relationship_type_id' => 6,
136 $contact_id = $result['id'];
138 // Now create our saved search, and chain the creation of a smart group.
139 $params = $this->params
;
140 $params['api.Group.create'] = array(
141 'name' => 'my_smartgroup',
142 'title' => 'my smartgroup',
143 'description' => 'Volunteers for the default organization',
144 'saved_search_id' => '$value.id',
146 'visibility' => 'User and User Admin Only',
151 $create_result = $this->callAPIAndDocument(
152 $this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
154 $created_search = CRM_Utils_Array
::first($create_result['values']);
155 $group_id = $created_search['api.Group.create']['id'];
157 // Search for contacts in our new smart group
158 $get_result = $this->callAPISuccess(
159 'Contact', 'get', array('group' => $group_id), __FUNCTION__
, __FILE__
);
161 // Expect our contact to be there.
162 $this->assertEquals(1, $get_result['count']);
163 $this->assertEquals($contact_id, $get_result['values'][$contact_id]['id']);
166 public function testDeleteSavedSearch() {
167 // Create saved search, delete it again, and try to get it
168 $create_result = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
169 $delete_params = array('id' => $create_result['id']);
170 $this->callAPIAndDocument(
171 $this->_entity
, 'delete', $delete_params, __FUNCTION__
, __FILE__
);
172 $get_result = $this->callAPISuccess($this->_entity
, 'get', array());
174 $this->assertEquals(0, $get_result['count']);