Merge branch 4.6 into master
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / GroupTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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
29require_once 'CiviTest/CiviUnitTestCase.php';
30
31/**
32 * Test class for CRM_Contact_BAO_Group BAO
33 *
6c6e6187 34 * @package CiviCRM
6a488035
TO
35 */
36class CRM_Contact_BAO_GroupTest extends CiviUnitTestCase {
37
38 /**
39 * Sets up the fixture, for example, opens a network connection.
40 * This method is called before a test is executed.
6a488035
TO
41 */
42 protected function setUp() {
43 parent::setUp();
44 }
45
46 /**
47 * Tears down the fixture, for example, closes a network connection.
48 * This method is called after a test is executed.
6a488035 49 */
959528d2
EM
50 protected function tearDown() {
51 $this->quickCleanup(array('civicrm_mapping_field', 'civicrm_mapping', 'civicrm_group', 'civicrm_saved_search'));
52 }
6a488035
TO
53
54 /**
100fef9d 55 * Test case for add( )
6a488035 56 */
00be9182 57 public function testAddSimple() {
6a488035
TO
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
00be9182 75 public function testAddSmart() {
6a488035
TO
76
77 $checkParams = $params = array(
78 'title' => 'Group Dos',
79 'description' => 'Group Two',
80 'visibility' => 'User and User Admin Only',
81 'is_active' => 1,
82 'formValues' => array('sort_name' => 'Adams'),
83 );
84
85 $group = CRM_Contact_BAO_Group::createSmartGroup($params);
86
87 unset($checkParams['formValues']);
88 $this->assertDBCompareValues(
89 'CRM_Contact_DAO_Group',
90 array('id' => $group->id),
91 $checkParams
92 );
93 }
959528d2
EM
94
95 /**
96 * Load all sql data sets & return an array of saved searches
97 * @return array
98 */
00be9182 99 public function dataProviderSavedSearch() {
959528d2
EM
100
101 $this->loadSavedSearches();
102 $results = CRM_Core_DAO::singleValueQuery('SELECT GROUP_CONCAT(id) FROM civicrm_group WHERE saved_search_id IS NOT NULL');
103 return array(explode(',', $results));
104 }
105
106 /**
eceb18cc 107 * Load saved search sql files into the DB.
959528d2 108 */
00be9182 109 public function loadSavedSearches() {
959528d2
EM
110 $dsn = CRM_Core_Config::singleton()->dsn;
111 foreach (glob(dirname(__FILE__) . "/SavedSearchDataSets/*.sql") as $file) {
112 CRM_Utils_File::sourceSQLFile($dsn, $file);
113 }
114 }
115
116 /**
117 * Check we can load smart groups based on config from 'real DBs' without fatal errors - note that we are only testing lack of errors at this stage
118 * @todo - for some reason the data was getting truncated from the group table using dataprovider - would be preferable to get that working
119 * //@notdataProvider dataProviderSavedSearch
120 * //@notparam integer $groupID
121 *
122 * To add to this dataset do
123 *
124 * SET @groupID = x;
125 * 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;
959528d2
EM
126 * SELECT * FROM civicrm_mapping WHERE id = @mappingID;
127 * SELECT * FROM civicrm_mapping_field WHERE mapping_id = @mappingID;
128 * SELECT * FROM civicrm_saved_search WHERE mapping_id = @mappingID;
129 * SELECT g.* FROM civicrm_saved_search s LEFT JOIN civicrm_group g ON g.saved_search_id = s.id WHERE mapping_id = @mappingID;
130 *
a455be55 131 * Copy the output to a single sql file and place in the SavedSearchDataSets folder - use the group number as the prefix.
959528d2
EM
132 * 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
133 */
00be9182 134 public function testGroupData() {
959528d2
EM
135 $groups = $this->dataProviderSavedSearch();
136 foreach ($groups[0] as $groupID) {
137 $group = new CRM_Contact_BAO_Group();
138 $group->id = $groupID;
139 $group->find(TRUE);
a455be55 140
959528d2
EM
141 CRM_Contact_BAO_GroupContactCache::load($group, TRUE);
142 }
143 }
96025800 144
6a488035 145}