4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.7 |
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 +--------------------------------------------------------------------+
29 require_once 'CiviTest/CiviUnitTestCase.php';
32 * Class api_v3_SavedSearchTest
34 * @package CiviCRM_APIv3
36 class api_v3_SavedSearchTest
extends CiviUnitTestCase
{
38 protected $_apiversion = 3;
42 public $DBResetRequired = FALSE;
44 public function setUp() {
47 // The line below makes it unneccessary to do cleanup after a test,
48 // because the transaction of the test will be rolled back.
49 // see http://forum.civicrm.org/index.php/topic,35627.0.html
50 $this->useTransaction(TRUE);
52 $this->_entity
= 'SavedSearch';
54 // I created a smart group using the CiviCRM gui. The smart group contains
55 // all contacts tagged with 'company'.
56 // I got the params below from the database.
58 $url = CIVICRM_UF_BASEURL
. "/civicrm/contact/search/advanced?reset=1";
59 $serialized_url = serialize($url);
61 // params for saved search that returns all volunteers for the
62 // default organization.
63 $this->params
= array(
64 'form_values' => array(
66 'relation_type_id' => '6_a_b',
67 'relation_target_name' => 'Default Organization',
73 * Create a saved search, and see whether the returned values make sense.
75 public function testCreateSavedSearch() {
77 $result = $this->callAPIAndDocument(
78 $this->_entity
, 'create', $this->params
, __FUNCTION__
, __FILE__
);
79 $this->assertEquals(1, $result['count']);
82 // getAndCheck fails, I think because form_values is an array.
83 //$this->getAndCheck($this->params, $result['id'], $this->_entity);
84 // Check whether the new ID is correctly returned by the API.
85 $this->assertNotNull($result['values'][$result['id']]['id']);
87 // Check whether the relation type ID is correctly returned.
89 $this->params
['form_values']['relation_type_id'],
90 $result['values'][$result['id']]['form_values']['relation_type_id']);
94 * Create a saved search, retrieve it again, and check for ID and one of
97 public function testCreateAndGetSavedSearch() {
99 // (create a saved search)
100 $create_result = $this->callAPISuccess(
101 $this->_entity
, 'create', $this->params
);
104 $get_result = $this->callAPIAndDocument(
105 $this->_entity
, 'get', array('id' => $create_result['id']), __FUNCTION__
, __FILE__
);
108 $this->assertEquals(1, $get_result['count']);
109 $this->assertNotNull($get_result['values'][$get_result['id']]['id']);
111 // just check the relationship type ID of the form values.
113 $this->params
['form_values']['relation_type_id'],
114 $get_result['values'][$get_result['id']]['form_values']['relation_type_id']);
118 * Create a saved search, and test whether it can be used for a smart
121 public function testCreateSavedSearchWithSmartGroup() {
122 // First create a volunteer for the default organization
124 $result = $this->callAPISuccess('Contact', 'create', array(
125 'first_name' => 'Joe',
126 'last_name' => 'Schmoe',
127 'contact_type' => 'Individual',
128 'api.Relationship.create' => array(
129 'contact_id_a' => '$value.id',
130 // default organization:
132 // volunteer relationship:
133 'relationship_type_id' => 6,
137 $contact_id = $result['id'];
139 // Now create our saved search, and chain the creation of a smart group.
140 $params = $this->params
;
141 $params['api.Group.create'] = array(
142 'name' => 'my_smartgroup',
143 'title' => 'my smartgroup',
144 'description' => 'Volunteers for the default organization',
145 'saved_search_id' => '$value.id',
147 'visibility' => 'User and User Admin Only',
152 $create_result = $this->callAPIAndDocument(
153 $this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
155 $created_search = CRM_Utils_Array
::first($create_result['values']);
156 $group_id = $created_search['api.Group.create']['id'];
158 // Search for contacts in our new smart group
159 $get_result = $this->callAPISuccess(
160 'Contact', 'get', array('group' => $group_id), __FUNCTION__
, __FILE__
);
162 // Expect our contact to be there.
163 $this->assertEquals(1, $get_result['count']);
164 $this->assertEquals($contact_id, $get_result['values'][$contact_id]['id']);
167 public function testDeleteSavedSearch() {
168 // Create saved search, delete it again, and try to get it
169 $create_result = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
170 $delete_params = array('id' => $create_result['id']);
171 $this->callAPIAndDocument(
172 $this->_entity
, 'delete', $delete_params, __FUNCTION__
, __FILE__
);
173 $get_result = $this->callAPISuccess($this->_entity
, 'get', array());
175 $this->assertEquals(0, $get_result['count']);