[NFC] CRM-19033 improve standardisation of tests
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / ContactType / RelationshipTest.php
1 <?php
2
3 require_once 'CiviTest/Contact.php';
4
5 /**
6 * Class CRM_Contact_BAO_ContactType_RelationshipTest
7 * @group headless
8 */
9 class CRM_Contact_BAO_ContactType_RelationshipTest extends CiviUnitTestCase {
10
11 public function setUp() {
12 parent::setUp();
13
14 //create contact subtypes
15 $params = array(
16 'label' => 'indivi_student',
17 'name' => 'indivi_student',
18 // Individual
19 'parent_id' => 1,
20 'is_active' => 1,
21 );
22 $result = CRM_Contact_BAO_ContactType::add($params);
23 $this->student = $params['name'];
24
25 $params = array(
26 'label' => 'indivi_parent',
27 'name' => 'indivi_parent',
28 // Individual
29 'parent_id' => 1,
30 'is_active' => 1,
31 );
32 $result = CRM_Contact_BAO_ContactType::add($params);
33 $this->parent = $params['name'];
34
35 $params = array(
36 'label' => 'org_sponsor',
37 'name' => 'org_sponsor',
38 // Organization
39 'parent_id' => 3,
40 'is_active' => 1,
41 );
42 $result = CRM_Contact_BAO_ContactType::add($params);
43 $this->sponsor = $params['name'];
44
45 //create contacts
46 $params = array(
47 'first_name' => 'Anne',
48 'last_name' => 'Grant',
49 'contact_type' => 'Individual',
50 );
51 $this->individual = $this->individualCreate($params);
52
53 $params = array(
54 'first_name' => 'Bill',
55 'last_name' => 'Adams',
56 'contact_type' => 'Individual',
57 'contact_sub_type' => $this->student,
58 );
59 $this->indivi_student = $this->individualCreate($params);
60
61 $params = array(
62 'first_name' => 'Alen',
63 'last_name' => 'Adams',
64 'contact_type' => 'Individual',
65 'contact_sub_type' => $this->parent,
66 );
67 $this->indivi_parent = $this->individualCreate($params);
68
69 $params = array(
70 'organization_name' => 'Compumentor',
71 'contact_type' => 'Organization',
72 );
73 $this->organization = $this->organizationCreate($params);
74
75 $params = array(
76 'organization_name' => 'Conservation Corp',
77 'contact_type' => 'Organization',
78 'contact_sub_type' => $this->sponsor,
79 );
80 $this->organization_sponsor = $this->organizationCreate($params);
81 }
82
83 public function tearDown() {
84 $this->quickCleanup(array('civicrm_contact'));
85
86 $query = "
87 DELETE FROM civicrm_contact_type
88 WHERE name IN ('{$this->student}','{$this->parent}','{$this->sponsor}');
89 ";
90 CRM_Core_DAO::executeQuery($query);
91 }
92
93 /**
94 * Methods create relationshipType with valid data.
95 * success expected
96 */
97 public function testRelationshipTypeAddIndiviParent() {
98 //check Individual to Parent RelationshipType
99 $params = array(
100 'name_a_b' => 'indivToparent',
101 'name_b_a' => 'parentToindiv',
102 'contact_type_a' => 'Individual',
103 'contact_type_b' => 'Individual',
104 'contact_sub_type_b' => $this->parent,
105 );
106 $ids = array();
107 $result = CRM_Contact_BAO_RelationshipType::add($params, $ids);
108 $this->assertEquals($result->name_a_b, 'indivToparent');
109 $this->assertEquals($result->contact_type_a, 'Individual');
110 $this->assertEquals($result->contact_type_b, 'Individual');
111 $this->assertEquals($result->contact_sub_type_b, $this->parent);
112 $this->relationshipTypeDelete($result->id);
113 }
114
115 public function testRelationshipTypeAddSponcorIndivi() {
116 //check Sponcor to Individual RelationshipType
117 $params = array(
118 'name_a_b' => 'SponsorToIndiv',
119 'name_b_a' => 'IndivToSponsor',
120 'contact_type_a' => 'Organization',
121 'contact_sub_type_a' => $this->sponsor,
122 'contact_type_b' => 'Individual',
123 );
124 $ids = array();
125 $result = CRM_Contact_BAO_RelationshipType::add($params, $ids);
126 $this->assertEquals($result->name_a_b, 'SponsorToIndiv');
127 $this->assertEquals($result->contact_type_a, 'Organization');
128 $this->assertEquals($result->contact_sub_type_a, $this->sponsor);
129 $this->assertEquals($result->contact_type_b, 'Individual');
130 $this->relationshipTypeDelete($result->id);
131 }
132
133 public function testRelationshipTypeAddStudentSponcor() {
134 //check Student to Sponcer RelationshipType
135 $params = array(
136 'name_a_b' => 'StudentToSponser',
137 'name_b_a' => 'SponsorToStudent',
138 'contact_type_a' => 'Individual',
139 'contact_sub_type_a' => $this->student,
140 'contact_type_b' => 'Organization',
141 'contact_sub_type_b' => $this->sponsor,
142 );
143 $ids = array();
144 $result = CRM_Contact_BAO_RelationshipType::add($params, $ids);
145 $this->assertEquals($result->name_a_b, 'StudentToSponser');
146 $this->assertEquals($result->contact_type_a, 'Individual');
147 $this->assertEquals($result->contact_sub_type_a, $this->student);
148 $this->assertEquals($result->contact_type_b, 'Organization');
149 $this->assertEquals($result->contact_sub_type_b, $this->sponsor);
150 $this->relationshipTypeDelete($result->id);
151 }
152
153 /**
154 * Methods create relationshipe within same contact type with invalid Relationships.
155 */
156 public function testRelationshipCreateInvalidWithinSameType() {
157 //check for Individual to Parent
158 $relTypeParams = array(
159 'name_a_b' => 'indivToparent',
160 'name_b_a' => 'parentToindiv',
161 'contact_type_a' => 'Individual',
162 'contact_type_b' => 'Individual',
163 'contact_sub_type_b' => $this->parent,
164 );
165 $relTypeIds = array();
166 $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds);
167 $params = array(
168 'relationship_type_id' => $relType->id . '_a_b',
169 'contact_check' => array($this->indivi_student => 1),
170 );
171 $ids = array('contact' => $this->individual);
172
173 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids);
174
175 $this->assertEquals($invalid, 1);
176 $this->assertEquals(empty($relationshipIds), TRUE);
177 $this->relationshipTypeDelete($relType->id);
178 }
179
180 /**
181 * Methods create relationshipe within diff contact type with invalid Relationships.
182 */
183 public function testRelCreateInvalidWithinDiffTypeSpocorIndivi() {
184 //check for Sponcer to Individual
185 $relTypeParams = array(
186 'name_a_b' => 'SponsorToIndiv',
187 'name_b_a' => 'IndivToSponsor',
188 'contact_type_a' => 'Organization',
189 'contact_sub_type_a' => $this->sponsor,
190 'contact_type_b' => 'Individual',
191 );
192 $relTypeIds = array();
193 $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds);
194 $params = array(
195 'relationship_type_id' => $relType->id . '_a_b',
196 'contact_check' => array($this->individual => 1),
197 );
198 $ids = array('contact' => $this->indivi_parent);
199
200 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids);
201
202 $this->assertEquals($invalid, 1);
203 $this->assertEquals(empty($relationshipIds), TRUE);
204 $this->relationshipTypeDelete($relType->id);
205 }
206
207 public function testRelCreateInvalidWithinDiffTypeStudentSponcor() {
208 //check for Student to Sponcer
209 $relTypeParams = array(
210 'name_a_b' => 'StudentToSponser',
211 'name_b_a' => 'SponsorToStudent',
212 'contact_type_a' => 'Individual',
213 'contact_sub_type_a' => $this->student,
214 'contact_type_b' => 'Organization',
215 'contact_sub_type_b' => 'Sponser',
216 );
217 $relTypeIds = array();
218 $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds);
219 $params = array(
220 'relationship_type_id' => $relType->id . '_a_b',
221 'contact_check' => array($this->individual => 1),
222 );
223 $ids = array('contact' => $this->indivi_parent);
224
225 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids);
226
227 $this->assertEquals($invalid, 1);
228 $this->assertEquals(empty($relationshipIds), TRUE);
229 $this->relationshipTypeDelete($relType->id);
230 }
231
232 /**
233 * Methods create relationshipe within same contact type with valid data.
234 * success expected
235 */
236 public function testRelationshipCreateWithinSameType() {
237 //check for Individual to Parent
238 $relTypeParams = array(
239 'name_a_b' => 'indivToparent',
240 'name_b_a' => 'parentToindiv',
241 'contact_type_a' => 'Individual',
242 'contact_type_b' => 'Individual',
243 'contact_sub_type_b' => $this->parent,
244 );
245 $relTypeIds = array();
246 $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds);
247 $params = array(
248 'relationship_type_id' => $relType->id . '_a_b',
249 'is_active' => 1,
250 'contact_check' => array($this->indivi_parent => $this->indivi_parent),
251 );
252 $ids = array('contact' => $this->individual);
253 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids);
254
255 $this->assertEquals($valid, 1);
256 $this->assertEquals(empty($relationshipIds), FALSE);
257 $this->relationshipTypeDelete($relType->id);
258 }
259
260 /**
261 * Methods create relationshipe within different contact type with valid data.
262 * success expected
263 */
264 public function testRelCreateWithinDiffTypeSponsorIndivi() {
265 //check for Sponcer to Individual
266 $relTypeParams = array(
267 'name_a_b' => 'SponsorToIndiv',
268 'name_b_a' => 'IndivToSponsor',
269 'contact_type_a' => 'Organization',
270 'contact_sub_type_a' => $this->sponsor,
271 'contact_type_b' => 'Individual',
272 );
273 $relTypeIds = array();
274 $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds);
275 $params = array(
276 'relationship_type_id' => $relType->id . '_a_b',
277 'is_active' => 1,
278 'contact_check' => array($this->indivi_student => 1),
279 );
280 $ids = array('contact' => $this->organization_sponsor);
281 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids);
282
283 $this->assertEquals($valid, 1);
284 $this->assertEquals(empty($relationshipIds), FALSE);
285 $this->relationshipTypeDelete($relType->id);
286 }
287
288 public function testRelCreateWithinDiffTypeStudentSponsor() {
289 //check for Student to Sponcer
290 $relTypeParams = array(
291 'name_a_b' => 'StudentToSponsor',
292 'name_b_a' => 'SponsorToStudent',
293 'contact_type_a' => 'Individual',
294 'contact_sub_type_a' => $this->student,
295 'contact_type_b' => 'Organization',
296 'contact_sub_type_b' => $this->sponsor,
297 );
298 $relTypeIds = array();
299 $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds);
300 $params = array(
301 'relationship_type_id' => $relType->id . '_a_b',
302 'is_active' => 1,
303 'contact_check' => array($this->organization_sponsor => 1),
304 );
305 $ids = array('contact' => $this->indivi_student);
306 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids);
307
308 $this->assertEquals($valid, 1);
309 $this->assertEquals(empty($relationshipIds), FALSE);
310 $this->relationshipTypeDelete($relType->id);
311 }
312
313 }