INFRA-132 - @param type fixes
[civicrm-core.git] / tests / phpunit / api / v3 / CaseTypeTest.php
index 432fa29d6bac55f478ccaa61e41f10548036ad3b..da563fdda47e5d1e8d326ac95167dfc0d40c83a5 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  +--------------------------------------------------------------------+
 */
 
-require_once 'CiviTest/CiviUnitTestCase.php';
+require_once 'CiviTest/CiviCaseTestCase.php';
 
 /**
  * Class api_v3_CaseTypeTest
  */
-class api_v3_CaseTypeTest extends CiviUnitTestCase {
-  protected $_apiversion = 3;
-
-  function setUp() {
-    $this->_entity = 'CaseType';
+class api_v3_CaseTypeTest extends CiviCaseTestCase {
 
+  public function setUp() {
+    $this->quickCleanup(array('civicrm_case_type'));
     parent::setUp();
-    $this->_apiversion = 3;
-    $this->tablesToTruncate = array(
-      'civicrm_case_type',
-    );
-    $this->quickCleanup($this->tablesToTruncate);
-    $this->createLoggedInUser();
-    $session = CRM_Core_Session::singleton();
-    $this->_loggedInUser = $session->get('userID');
 
+    $this->fixtures['Application_with_Definition'] = array(
+      'title' => 'Application with Definition',
+      'name' => 'Application_with_Definition',
+      'is_active' => 1,
+      'weight' => 4,
+      'definition' => array(
+        'activityTypes' => array(
+          array('name' => 'First act'),
+        ),
+        'activitySets' => array(
+          array(
+            'name' => 'set1',
+            'label' => 'Label 1',
+            'timeline' => 1,
+            'activityTypes' => array(
+              array('name' => 'Open Case', 'status' => 'Completed'),
+            ),
+          ),
+        ),
+        'caseRoles' => array(
+          array('name' => 'First role', 'creator' => 1, 'manager' => 1),
+        ),
+      ),
+    );
   }
 
   /**
@@ -53,21 +67,22 @@ class api_v3_CaseTypeTest extends CiviUnitTestCase {
    * This method is called after a test is executed.
    *
    */
-  function tearDown() {
-    $this->quickCleanup($this->tablesToTruncate, TRUE);
+  public function tearDown() {
+    parent::tearDown();
+    $this->quickCleanup(array('civicrm_case_type'));
   }
 
   /**
-   * check with empty array
+   * Check with empty array
    */
-  function testCaseTypeCreateEmpty() {
+  public function testCaseTypeCreateEmpty() {
     $this->callAPIFailure('CaseType', 'create', array());
   }
 
   /**
-   * check if required fields are not passed
+   * Check if required fields are not passed
    */
-  function testCaseTypeCreateWithoutRequired() {
+  public function testCaseTypeCreateWithoutRequired() {
     $params = array(
       'name' => 'this case should fail',
     );
@@ -81,10 +96,10 @@ class api_v3_CaseTypeTest extends CiviUnitTestCase {
   }
 
   /**
-   * test create methods with valid data
+   * Test create methods with valid data
    * success expected
    */
-  function testCaseTypeCreate() {
+  public function testCaseTypeCreate() {
     // Create Case Type
     $params = array(
       'title' => 'Application',
@@ -105,7 +120,7 @@ class api_v3_CaseTypeTest extends CiviUnitTestCase {
   /**
    * Create a case with an invalid name
    */
-  function testCaseTypeCreate_invalidName() {
+  public function testCaseTypeCreate_invalidName() {
     // Create Case Type
     $params = array(
       'title' => 'Application',
@@ -121,9 +136,9 @@ class api_v3_CaseTypeTest extends CiviUnitTestCase {
   /**
    * Test update (create with id) function with valid parameters
    */
-  function testCaseTypeUpdate() {
+  public function testCaseTypeUpdate() {
     // Create Case Type
-    $params =  array(
+    $params = array(
       'title' => 'Application',
       'name' => 'Application',
       'is_active' => 1,
@@ -147,9 +162,9 @@ class api_v3_CaseTypeTest extends CiviUnitTestCase {
   /**
    * Test delete function with valid parameters
    */
-  function testCaseTypeDelete() {
+  public function testCaseTypeDelete_New() {
     // Create Case Type
-    $params =  array(
+    $params = array(
       'title' => 'Application',
       'name' => 'Application',
       'is_active' => 1,
@@ -166,36 +181,12 @@ class api_v3_CaseTypeTest extends CiviUnitTestCase {
   }
 
   /**
-   * test create methods with xml file
+   * Test create methods with xml file
    * success expected
    */
-  function testCaseTypeCreateWithDefinition() {
+  public function testCaseTypeCreateWithDefinition() {
     // Create Case Type
-    $params = array(
-      'title' => 'Application with Definition',
-      'name' => 'Application_with_Definition',
-      'is_active' => 1,
-      'weight' => 4,
-      'definition' => array(
-        'activityTypes' => array(
-          array('name' => 'First act'),
-        ),
-        'activitySets' => array(
-          array(
-            'name' => 'set1',
-            'label' => 'Label 1',
-            'timeline' => 1,
-            'activityTypes' => array(
-              array('name' => 'Open Case', 'status' => 'Completed'),
-            ),
-          ),
-        ),
-        'caseRoles' => array(
-          array('name' => 'First role', 'creator' => 1, 'manager' => 1),
-        ),
-      )
-    );
-
+    $params = $this->fixtures['Application_with_Definition'];
     $result = $this->callAPISuccess('CaseType', 'create', $params);
     $id = $result['id'];
 
@@ -204,6 +195,37 @@ class api_v3_CaseTypeTest extends CiviUnitTestCase {
     $this->assertEquals($result['values'][$id]['id'], $id, 'in line ' . __LINE__);
     $this->assertEquals($result['values'][$id]['title'], $params['title'], 'in line ' . __LINE__);
     $this->assertEquals($result['values'][$id]['definition'], $params['definition'], 'in line ' . __LINE__);
+
+    $caseXml = CRM_Case_XMLRepository::singleton()->retrieve('Application_with_Definition');
+    $this->assertTrue($caseXml instanceof SimpleXMLElement);
   }
-}
 
+  /**
+   * Create a CaseType+case then delete the CaseType.
+   */
+  public function testCaseTypeDelete_InUse() {
+    // Create Case Type
+    $params = $this->fixtures['Application_with_Definition'];
+    $createCaseType = $this->callAPISuccess('CaseType', 'create', $params);
+
+    $createCase = $this->callAPISuccess('Case', 'create', array(
+      'case_type_id' => $createCaseType['id'],
+      'contact_id' => $this->_loggedInUser,
+      'subject' => 'Example',
+    ));
+
+    // Deletion fails while case-type is in-use
+    $deleteCaseType = $this->callAPIFailure('CaseType', 'delete', array('id' => $createCaseType['id']));
+    $this->assertEquals("You can not delete this case type -- it is assigned to 1 existing case record(s). If you do not want this case type to be used going forward, consider disabling it instead.", $deleteCaseType['error_message']);
+    $getCaseType = $this->callAPISuccess('CaseType', 'get', array('id' => $createCaseType['id']));
+    $this->assertEquals(1, $getCaseType['count']);
+
+    // Deletion succeeds when it's not in-use
+    $this->callAPISuccess('Case', 'delete', array('id' => $createCase['id']));
+
+    // Check result - case type should no longer exist
+    $this->callAPISuccess('CaseType', 'delete', array('id' => $createCaseType['id']));
+    $getCaseType = $this->callAPISuccess('CaseType', 'get', array('id' => $createCaseType['id']));
+    $this->assertEquals(0, $getCaseType['count']);
+  }
+}