Commit | Line | Data |
---|---|---|
4df0eb30 JV |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
81621fee | 5 | | CiviCRM version 4.7 | |
4df0eb30 JV |
6 | +--------------------------------------------------------------------+ |
7 | | Copyright Chirojeugd-Vlaanderen vzw 2015 | | |
8 | +--------------------------------------------------------------------+ | |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
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. | | |
14 | | | | |
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. | | |
19 | | | | |
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 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
30 | ||
31 | /** | |
32 | * Class api_v3_SavedSearchTest | |
33 | * | |
34 | * @package CiviCRM_APIv3 | |
35 | */ | |
36 | class api_v3_SavedSearchTest extends CiviUnitTestCase { | |
37 | ||
38 | protected $_apiversion = 3; | |
39 | protected $params; | |
40 | protected $id; | |
41 | protected $_entity; | |
42 | public $DBResetRequired = FALSE; | |
43 | ||
44 | public function setUp() { | |
45 | parent::setUp(); | |
46 | ||
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); | |
51 | ||
52 | $this->_entity = 'SavedSearch'; | |
53 | ||
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. | |
57 | ||
6c8d4050 JV |
58 | $url = CIVICRM_UF_BASEURL . "/civicrm/contact/search/advanced?reset=1"; |
59 | $serialized_url = serialize($url); | |
60 | ||
d30f435f JV |
61 | // params for saved search that returns all volunteers for the |
62 | // default organization. | |
4df0eb30 | 63 | $this->params = array( |
d30f435f JV |
64 | 'form_values' => array( |
65 | // Is volunteer for | |
66 | 'relation_type_id' => '6_a_b', | |
67 | 'relation_target_name' => 'Default Organization', | |
68 | ), | |
4df0eb30 JV |
69 | ); |
70 | } | |
71 | ||
6c8d4050 JV |
72 | /** |
73 | * Create a saved search, and see whether the returned values make sense. | |
74 | */ | |
4df0eb30 JV |
75 | public function testCreateSavedSearch() { |
76 | // Act: | |
77 | $result = $this->callAPIAndDocument( | |
78 | $this->_entity, 'create', $this->params, __FUNCTION__, __FILE__); | |
79 | $this->assertEquals(1, $result['count']); | |
80 | ||
81 | // Assert: | |
d30f435f JV |
82 | // getAndCheck fails, I think because form_values is an array. |
83 | //$this->getAndCheck($this->params, $result['id'], $this->_entity); | |
4df0eb30 JV |
84 | // Check whether the new ID is correctly returned by the API. |
85 | $this->assertNotNull($result['values'][$result['id']]['id']); | |
d30f435f JV |
86 | |
87 | // Check whether the relation type ID is correctly returned. | |
88 | $this->assertEquals( | |
89 | $this->params['form_values']['relation_type_id'], | |
90 | $result['values'][$result['id']]['form_values']['relation_type_id']); | |
4df0eb30 JV |
91 | } |
92 | ||
d30f435f JV |
93 | /** |
94 | * Create a saved search, retrieve it again, and check for ID and one of | |
95 | * the field values. | |
96 | */ | |
97 | public function testCreateAndGetSavedSearch() { | |
4df0eb30 JV |
98 | // Arrange: |
99 | // (create a saved search) | |
100 | $create_result = $this->callAPISuccess( | |
101 | $this->_entity, 'create', $this->params); | |
102 | ||
103 | // Act: | |
104 | $get_result = $this->callAPIAndDocument( | |
105 | $this->_entity, 'get', array('id' => $create_result['id']), __FUNCTION__, __FILE__); | |
106 | ||
107 | // Assert: | |
108 | $this->assertEquals(1, $get_result['count']); | |
109 | $this->assertNotNull($get_result['values'][$get_result['id']]['id']); | |
d30f435f JV |
110 | |
111 | // just check the relationship type ID of the form values. | |
6c8d4050 | 112 | $this->assertEquals( |
d30f435f JV |
113 | $this->params['form_values']['relation_type_id'], |
114 | $get_result['values'][$get_result['id']]['form_values']['relation_type_id']); | |
115 | } | |
116 | ||
117 | /** | |
118 | * Create a saved search, and test whether it can be used for a smart | |
119 | * group. | |
120 | */ | |
121 | public function testCreateSavedSearchWithSmartGroup() { | |
122 | // First create a volunteer for the default organization | |
123 | ||
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: | |
131 | 'contact_id_b' => 1, | |
132 | // volunteer relationship: | |
133 | 'relationship_type_id' => 6, | |
134 | 'is_active' => 1, | |
135 | ), | |
136 | )); | |
137 | $contact_id = $result['id']; | |
138 | ||
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', | |
146 | 'is_active' => 1, | |
147 | 'visibility' => 'User and User Admin Only', | |
148 | 'is_hidden' => 0, | |
149 | 'is_reserved' => 0, | |
150 | ); | |
151 | ||
152 | $create_result = $this->callAPIAndDocument( | |
153 | $this->_entity, 'create', $params, __FUNCTION__, __FILE__); | |
154 | ||
155 | $created_search = CRM_Utils_Array::first($create_result['values']); | |
156 | $group_id = $created_search['api.Group.create']['id']; | |
157 | ||
158 | // Search for contacts in our new smart group | |
159 | $get_result = $this->callAPISuccess( | |
160 | 'Contact', 'get', array('group' => $group_id), __FUNCTION__, __FILE__); | |
161 | ||
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']); | |
4df0eb30 JV |
165 | } |
166 | ||
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()); | |
174 | ||
175 | $this->assertEquals(0, $get_result['count']); | |
176 | } | |
177 | ||
178 | } |