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 | ||
4df0eb30 JV |
29 | /** |
30 | * Class api_v3_SavedSearchTest | |
31 | * | |
32 | * @package CiviCRM_APIv3 | |
33 | */ | |
34 | class api_v3_SavedSearchTest extends CiviUnitTestCase { | |
35 | ||
36 | protected $_apiversion = 3; | |
37 | protected $params; | |
38 | protected $id; | |
39 | protected $_entity; | |
40 | public $DBResetRequired = FALSE; | |
41 | ||
42 | public function setUp() { | |
43 | parent::setUp(); | |
44 | ||
45 | // The line below makes it unneccessary to do cleanup after a test, | |
46 | // because the transaction of the test will be rolled back. | |
47 | // see http://forum.civicrm.org/index.php/topic,35627.0.html | |
48 | $this->useTransaction(TRUE); | |
49 | ||
50 | $this->_entity = 'SavedSearch'; | |
51 | ||
52 | // I created a smart group using the CiviCRM gui. The smart group contains | |
53 | // all contacts tagged with 'company'. | |
54 | // I got the params below from the database. | |
55 | ||
6c8d4050 JV |
56 | $url = CIVICRM_UF_BASEURL . "/civicrm/contact/search/advanced?reset=1"; |
57 | $serialized_url = serialize($url); | |
58 | ||
d30f435f JV |
59 | // params for saved search that returns all volunteers for the |
60 | // default organization. | |
4df0eb30 | 61 | $this->params = array( |
d30f435f JV |
62 | 'form_values' => array( |
63 | // Is volunteer for | |
64 | 'relation_type_id' => '6_a_b', | |
65 | 'relation_target_name' => 'Default Organization', | |
66 | ), | |
4df0eb30 JV |
67 | ); |
68 | } | |
69 | ||
6c8d4050 JV |
70 | /** |
71 | * Create a saved search, and see whether the returned values make sense. | |
72 | */ | |
4df0eb30 JV |
73 | public function testCreateSavedSearch() { |
74 | // Act: | |
75 | $result = $this->callAPIAndDocument( | |
76 | $this->_entity, 'create', $this->params, __FUNCTION__, __FILE__); | |
77 | $this->assertEquals(1, $result['count']); | |
78 | ||
79 | // Assert: | |
d30f435f JV |
80 | // getAndCheck fails, I think because form_values is an array. |
81 | //$this->getAndCheck($this->params, $result['id'], $this->_entity); | |
4df0eb30 JV |
82 | // Check whether the new ID is correctly returned by the API. |
83 | $this->assertNotNull($result['values'][$result['id']]['id']); | |
d30f435f JV |
84 | |
85 | // Check whether the relation type ID is correctly returned. | |
86 | $this->assertEquals( | |
87 | $this->params['form_values']['relation_type_id'], | |
88 | $result['values'][$result['id']]['form_values']['relation_type_id']); | |
4df0eb30 JV |
89 | } |
90 | ||
d30f435f JV |
91 | /** |
92 | * Create a saved search, retrieve it again, and check for ID and one of | |
93 | * the field values. | |
94 | */ | |
95 | public function testCreateAndGetSavedSearch() { | |
4df0eb30 JV |
96 | // Arrange: |
97 | // (create a saved search) | |
98 | $create_result = $this->callAPISuccess( | |
99 | $this->_entity, 'create', $this->params); | |
100 | ||
101 | // Act: | |
102 | $get_result = $this->callAPIAndDocument( | |
103 | $this->_entity, 'get', array('id' => $create_result['id']), __FUNCTION__, __FILE__); | |
104 | ||
105 | // Assert: | |
106 | $this->assertEquals(1, $get_result['count']); | |
107 | $this->assertNotNull($get_result['values'][$get_result['id']]['id']); | |
d30f435f JV |
108 | |
109 | // just check the relationship type ID of the form values. | |
6c8d4050 | 110 | $this->assertEquals( |
d30f435f JV |
111 | $this->params['form_values']['relation_type_id'], |
112 | $get_result['values'][$get_result['id']]['form_values']['relation_type_id']); | |
113 | } | |
114 | ||
115 | /** | |
116 | * Create a saved search, and test whether it can be used for a smart | |
117 | * group. | |
118 | */ | |
119 | public function testCreateSavedSearchWithSmartGroup() { | |
120 | // First create a volunteer for the default organization | |
121 | ||
122 | $result = $this->callAPISuccess('Contact', 'create', array( | |
123 | 'first_name' => 'Joe', | |
124 | 'last_name' => 'Schmoe', | |
125 | 'contact_type' => 'Individual', | |
126 | 'api.Relationship.create' => array( | |
127 | 'contact_id_a' => '$value.id', | |
128 | // default organization: | |
129 | 'contact_id_b' => 1, | |
130 | // volunteer relationship: | |
131 | 'relationship_type_id' => 6, | |
132 | 'is_active' => 1, | |
133 | ), | |
134 | )); | |
135 | $contact_id = $result['id']; | |
136 | ||
137 | // Now create our saved search, and chain the creation of a smart group. | |
138 | $params = $this->params; | |
139 | $params['api.Group.create'] = array( | |
140 | 'name' => 'my_smartgroup', | |
141 | 'title' => 'my smartgroup', | |
142 | 'description' => 'Volunteers for the default organization', | |
143 | 'saved_search_id' => '$value.id', | |
144 | 'is_active' => 1, | |
145 | 'visibility' => 'User and User Admin Only', | |
146 | 'is_hidden' => 0, | |
147 | 'is_reserved' => 0, | |
148 | ); | |
149 | ||
150 | $create_result = $this->callAPIAndDocument( | |
151 | $this->_entity, 'create', $params, __FUNCTION__, __FILE__); | |
152 | ||
153 | $created_search = CRM_Utils_Array::first($create_result['values']); | |
154 | $group_id = $created_search['api.Group.create']['id']; | |
155 | ||
156 | // Search for contacts in our new smart group | |
157 | $get_result = $this->callAPISuccess( | |
158 | 'Contact', 'get', array('group' => $group_id), __FUNCTION__, __FILE__); | |
159 | ||
160 | // Expect our contact to be there. | |
161 | $this->assertEquals(1, $get_result['count']); | |
162 | $this->assertEquals($contact_id, $get_result['values'][$contact_id]['id']); | |
4df0eb30 JV |
163 | } |
164 | ||
165 | public function testDeleteSavedSearch() { | |
166 | // Create saved search, delete it again, and try to get it | |
167 | $create_result = $this->callAPISuccess($this->_entity, 'create', $this->params); | |
168 | $delete_params = array('id' => $create_result['id']); | |
169 | $this->callAPIAndDocument( | |
170 | $this->_entity, 'delete', $delete_params, __FUNCTION__, __FILE__); | |
171 | $get_result = $this->callAPISuccess($this->_entity, 'get', array()); | |
172 | ||
173 | $this->assertEquals(0, $get_result['count']); | |
174 | } | |
175 | ||
176 | } |