CRM-16701 - Saved Search API: unit tests.
[civicrm-core.git] / tests / phpunit / api / v3 / SavedSearchTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
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
58 $this->params = array(
59 'form_values' => 'a:36:{s:5:"qfKey";s:37:"4b50e233dcbe77cced4bd8fd8df90567_9974";s:8:"entryURL";s:64:"' . CIVICRM_UF_BASEURL . '/civicrm/contact/search/advanced?reset=1";s:12:"hidden_basic";s:1:"1";s:12:"contact_type";a:0:{}s:5:"group";a:0:{}s:10:"group_type";a:0:{}s:21:"group_search_selected";s:5:"group";s:9:"sort_name";s:0:"";s:5:"email";s:0:"";s:14:"contact_source";s:0:"";s:9:"job_title";s:0:"";s:10:"contact_id";s:0:"";s:19:"external_identifier";s:0:"";s:7:"uf_user";s:0:"";s:10:"tag_search";s:0:"";s:11:"uf_group_id";s:0:"";s:14:"component_mode";s:1:"1";s:8:"operator";s:3:"AND";s:25:"display_relationship_type";s:0:"";s:15:"privacy_options";a:0:{}s:16:"privacy_operator";s:2:"OR";s:14:"privacy_toggle";s:1:"1";s:13:"email_on_hold";a:1:{s:7:"on_hold";s:0:"";}s:30:"preferred_communication_method";a:5:{i:1;s:0:"";i:2;s:0:"";i:3;s:0:"";i:4;s:0:"";i:5;s:0:"";}s:18:"preferred_language";s:0:"";s:13:"phone_numeric";s:0:"";s:22:"phone_location_type_id";s:0:"";s:19:"phone_phone_type_id";s:0:"";s:4:"task";s:2:"13";s:8:"radio_ts";s:6:"ts_sel";s:12:"toggleSelect";s:1:"1";s:10:"mark_x_165";s:1:"1";s:10:"mark_x_155";s:1:"1";s:10:"mark_x_124";s:1:"1";s:9:"mark_x_35";s:1:"1";s:12:"contact_tags";a:1:{i:2;i:1;}}',
60 'where_clause' => '( `civicrm_entity_tag-2`.tag_id IN (2) ) ',
61 'select_tables' => "a:8:{s:15:\"civicrm_contact\";i:1;s:15:\"civicrm_address\";i:1;s:15:\"civicrm_country\";i:1;s:13:\"civicrm_email\";i:1;s:13:\"civicrm_phone\";i:1;s:10:\"civicrm_im\";i:1;s:19:\"civicrm_worldregion\";i:1;s:22:\"`civicrm_entity_tag-2`\";s:168:\" LEFT JOIN civicrm_entity_tag `civicrm_entity_tag-2` ON ( `civicrm_entity_tag-2`.entity_id = contact_a.id AND `civicrm_entity_tag-2`.entity_table = \'civicrm_contact\') \";} ",
62 'where_tables' => "a:2:{s:15:\"civicrm_contact\";i:1;s:22:\"`civicrm_entity_tag-2`\";s:168:\" LEFT JOIN civicrm_entity_tag `civicrm_entity_tag-2` ON ( `civicrm_entity_tag-2`.entity_id = contact_a.id AND `civicrm_entity_tag-2`.entity_table = \'civicrm_contact\') \";}",
63 );
64 }
65
66 public function testCreateSavedSearch() {
67 // Act:
68 $result = $this->callAPIAndDocument(
69 $this->_entity, 'create', $this->params, __FUNCTION__, __FILE__);
70 $this->assertEquals(1, $result['count']);
71
72 // Assert:
73 // Get the entity with the id returned in $result['id'], and check whether
74 // the parameters are set correctly:
75 $this->getAndCheck($this->params, $result['id'], $this->_entity);
76
77 // Check whether the new ID is correctly returned by the API.
78 $this->assertNotNull($result['values'][$result['id']]['id']);
79 }
80
81 public function testGetSavedSearch() {
82 // Arrange:
83 // (create a saved search)
84 $create_result = $this->callAPISuccess(
85 $this->_entity, 'create', $this->params);
86
87 // Act:
88 $get_result = $this->callAPIAndDocument(
89 $this->_entity, 'get', array('id' => $create_result['id']), __FUNCTION__, __FILE__);
90
91 // Assert:
92 $this->assertEquals(1, $get_result['count']);
93 $this->assertNotNull($get_result['values'][$get_result['id']]['id']);
94 }
95
96 public function testDeleteSavedSearch() {
97 // Create saved search, delete it again, and try to get it
98 $create_result = $this->callAPISuccess($this->_entity, 'create', $this->params);
99 $delete_params = array('id' => $create_result['id']);
100 $this->callAPIAndDocument(
101 $this->_entity, 'delete', $delete_params, __FUNCTION__, __FILE__);
102 $get_result = $this->callAPISuccess($this->_entity, 'get', array());
103
104 $this->assertEquals(0, $get_result['count']);
105 }
106
107 }