(NFC) Update version headers in `tests/`
[civicrm-core.git] / tests / phpunit / api / v3 / GroupTest.php
index f03f05a25e624b54aa3377c9ad71c9d9b80179a1..732a143a90a73d64c33dd5c18b3d57a3d2577a62 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.7                                                |
+ | CiviCRM version 5                                                  |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2017                                |
+ | Copyright CiviCRM LLC (c) 2004-2018                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -150,7 +150,7 @@ class api_v3_GroupTest extends CiviUnitTestCase {
       //check for empty parent
       'parents' => "",
       'visibility' => 'Public Pages',
-      'group_type' => array(1, 2),
+      'group_type' => [1, 2],
     );
 
     $result = $this->callAPISuccess('Group', 'create', $params);
@@ -178,12 +178,47 @@ class api_v3_GroupTest extends CiviUnitTestCase {
         'title' => 'Test Group 2',
         'group_type' => 2,
         'parents' => $result['id'],
+        'sequential' => 1,
       )
     );
-    $result = $this->callAPISuccess('Group', 'create', $params);
-    $group = $result["values"][$result['id']];
-    $this->assertEquals($group['group_type'], array($params['group_type']));
-    $this->assertEquals($group['parents'], $params['parents']);
+    $group2 = $this->callAPISuccess('Group', 'create', $params)['values'][0];
+
+    $this->assertEquals($group2['group_type'], array($params['group_type']));
+    $this->assertEquals($params['parents'], $group2['parents']);
+
+    // Test array format for parents.
+    $params = array_merge($params, array(
+        'name' => 'Test Group 3',
+        'title' => 'Test Group 3',
+        'parents' => [$result['id'], $group2['id']],
+      )
+    );
+    $group3 = $this->callAPISuccess('Group', 'create', $params)['values'][0];
+    $parents = $this->callAPISuccess('Group', 'getvalue', ['return' => 'parents', 'id' => $group3['id']]);
+
+    $this->assertAPIArrayComparison("{$result['id']},{$group2['id']}", $parents);
+
+    $groupNesting = $this->callAPISuccess('GroupNesting', 'get', ['child_group_id' => $group3['id']]);
+    // 2 Group nesting entries - one for direct parent & one for grandparent.
+    $this->assertEquals(2, $groupNesting['count']);
+    $this->groupDelete($group2['id']);
+    $this->groupDelete($group3['id']);
+  }
+
+  /**
+   * Test that an array of valid values works for group_type field.
+   */
+  public function testGroupTypeWithPseudoconstantArray() {
+    $params = [
+      'name' => 'Test Group 2',
+      'title' => 'Test Group 2',
+      'group_type' => ['Mailing List', 'Access Control'],
+      'sequential' => 1,
+    ];
+    $group = $this->callAPISuccess('Group', 'create', $params);
+    $groupType = $this->callAPISuccess('Group', 'getvalue', ['return' => 'group_type', 'id' => $group['id']]);
+
+    $this->assertAPIArrayComparison([2, 1], $groupType);
   }
 
   public function testGetNonExistingGroup() {
@@ -226,8 +261,7 @@ class api_v3_GroupTest extends CiviUnitTestCase {
     $params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"] = "Test";
     $this->callAPIFailure('group', 'create', $params);
     unset($params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"]);
-    $group2 = $this->callAPISuccess('group', 'create', $params);
-    $this->assertEquals(count($group2['values'][$group2['id']]['parents']), 1);
+    $this->callAPIFailure('group', 'create', $params, '\'test Group\' is not a valid option for field parents');
   }
 
   /**