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