| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * Test class for CRM_Contact_BAO_Group BAO |
| 30 | * |
| 31 | * @package CiviCRM |
| 32 | * @group headless |
| 33 | */ |
| 34 | class CRM_Contact_BAO_GroupTest extends CiviUnitTestCase { |
| 35 | |
| 36 | /** |
| 37 | * Sets up the fixture, for example, opens a network connection. |
| 38 | * |
| 39 | * This method is called before a test is executed. |
| 40 | */ |
| 41 | protected function setUp() { |
| 42 | parent::setUp(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Tears down the fixture, for example, closes a network connection. |
| 47 | * |
| 48 | * This method is called after a test is executed. |
| 49 | */ |
| 50 | protected function tearDown() { |
| 51 | $this->quickCleanup(array('civicrm_mapping_field', 'civicrm_mapping', 'civicrm_group', 'civicrm_saved_search')); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Test case for add( ). |
| 56 | */ |
| 57 | public function testAddSimple() { |
| 58 | |
| 59 | $checkParams = $params = array( |
| 60 | 'title' => 'Group Uno', |
| 61 | 'description' => 'Group One', |
| 62 | 'visibility' => 'User and User Admin Only', |
| 63 | 'is_active' => 1, |
| 64 | ); |
| 65 | |
| 66 | $group = CRM_Contact_BAO_Group::create($params); |
| 67 | |
| 68 | $this->assertDBCompareValues( |
| 69 | 'CRM_Contact_DAO_Group', |
| 70 | array('id' => $group->id), |
| 71 | $checkParams |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Test adding a smart group. |
| 77 | */ |
| 78 | public function testAddSmart() { |
| 79 | |
| 80 | $checkParams = $params = array( |
| 81 | 'title' => 'Group Dos', |
| 82 | 'description' => 'Group Two', |
| 83 | 'visibility' => 'User and User Admin Only', |
| 84 | 'is_active' => 1, |
| 85 | 'formValues' => array('sort_name' => 'Adams'), |
| 86 | ); |
| 87 | |
| 88 | $group = CRM_Contact_BAO_Group::createSmartGroup($params); |
| 89 | |
| 90 | unset($checkParams['formValues']); |
| 91 | $this->assertDBCompareValues( |
| 92 | 'CRM_Contact_DAO_Group', |
| 93 | array('id' => $group->id), |
| 94 | $checkParams |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Load all sql data sets & return an array of saved searches. |
| 100 | * |
| 101 | * @return array |
| 102 | */ |
| 103 | public function dataProviderSavedSearch() { |
| 104 | |
| 105 | $this->loadSavedSearches(); |
| 106 | $results = CRM_Core_DAO::singleValueQuery('SELECT GROUP_CONCAT(id) FROM civicrm_group WHERE saved_search_id IS NOT NULL'); |
| 107 | return array(explode(',', $results)); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Load saved search sql files into the DB. |
| 112 | */ |
| 113 | public function loadSavedSearches() { |
| 114 | $dsn = CRM_Core_Config::singleton()->dsn; |
| 115 | foreach (glob(dirname(__FILE__) . "/SavedSearchDataSets/*.sql") as $file) { |
| 116 | CRM_Utils_File::sourceSQLFile($dsn, $file); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Check we can load smart groups based on config from 'real DBs' without fatal errors. |
| 122 | * |
| 123 | * Note that we are only testing lack of errors at this stage |
| 124 | * @todo - for some reason the data was getting truncated from the group table using dataprovider - would be preferable to get that working |
| 125 | * //@notdataProvider dataProviderSavedSearch |
| 126 | * //@notparam integer $groupID |
| 127 | * |
| 128 | * To add to this dataset do |
| 129 | * |
| 130 | * SET @groupID = x; |
| 131 | * SELECT mapping_id FROM civicrm_group g LEFT JOIN civicrm_saved_search s ON saved_search_id = s.id WHERE g.id = @groupID INTO @mappingID; |
| 132 | * SELECT * FROM civicrm_mapping WHERE id = @mappingID; |
| 133 | * SELECT * FROM civicrm_mapping_field WHERE mapping_id = @mappingID; |
| 134 | * SELECT * FROM civicrm_saved_search WHERE mapping_id = @mappingID; |
| 135 | * SELECT g.* FROM civicrm_saved_search s LEFT JOIN civicrm_group g ON g.saved_search_id = s.id WHERE mapping_id = @mappingID; |
| 136 | * |
| 137 | * Copy the output to a single sql file and place in the SavedSearchDataSets folder - use the group number as the prefix. |
| 138 | * Try to keep as much of the real world irregular glory as you can! Don't change the table ids to be number 1 as this can hide errors |
| 139 | */ |
| 140 | public function testGroupData() { |
| 141 | $groups = $this->dataProviderSavedSearch(); |
| 142 | foreach ($groups[0] as $groupID) { |
| 143 | $group = new CRM_Contact_BAO_Group(); |
| 144 | $group->id = $groupID; |
| 145 | $group->find(TRUE); |
| 146 | |
| 147 | CRM_Contact_BAO_GroupContactCache::load($group, TRUE); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | } |