CaseTypeForkTest.php 4.31 KB
Newer Older
1 2 3 4 5
<?php

/**
 * Case Types support an optional forking mechanism wherein the local admin
 * creates a custom DB-based definition that deviates from the file-based definition.
6
 * @group headless
7 8
 */
class CRM_Case_BAO_CaseTypeForkTest extends CiviCaseTestCase {
9

10 11 12 13 14
  public function setUp() {
    parent::setUp();
    CRM_Core_ManagedEntities::singleton(TRUE)->reconcile();
  }

15
  public function tearDown() {
16 17 18 19
    parent::tearDown();
    CRM_Core_ManagedEntities::singleton(TRUE)->reconcile();
  }

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  /**
   * Test Manager contact is correctly assigned via case type def.
   */
  public function testManagerContact() {
    $caseTypeId = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', 'ForkableCaseType', 'id', 'name');
    $this->assertTrue(is_numeric($caseTypeId) && $caseTypeId > 0);

    $this->callAPISuccess('CaseType', 'create', [
      'id' => $caseTypeId,
      'definition' => [
        'caseRoles' => [
          ['name' => 'First role', 'manager' => 0],
          ['name' => 'Second role', 'creator' => 1, 'manager' => 1],
        ],
      ],
    ]);
    $relTypeID = $this->callAPISuccessGetValue('RelationshipType', [
      'return' => "id",
      'name_b_a' => "Second role",
    ]);
    //Check if manager is correctly retrieved from xml processor.
    $xmlProcessor = new CRM_Case_XMLProcessor_Process();
    $this->assertEquals($relTypeID, $xmlProcessor->getCaseManagerRoleId('ForkableCaseType'));
  }

45
  /**
46
   * Edit the definition of ForkableCaseType.
47
   */
48
  public function testForkable() {
49 50 51 52 53 54 55 56 57 58 59 60 61 62
    $caseTypeId = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', 'ForkableCaseType', 'id', 'name');
    $this->assertTrue(is_numeric($caseTypeId) && $caseTypeId > 0);

    $this->assertDBNull('CRM_Case_BAO_CaseType', $caseTypeId, 'definition', 'id', "Should not have DB-based definition");
    $this->assertTrue(CRM_Case_BAO_CaseType::isForkable($caseTypeId));
    $this->assertFalse(CRM_Case_BAO_CaseType::isForked($caseTypeId));

    $this->callAPISuccess('CaseType', 'create', array(
      'id' => $caseTypeId,
      'definition' => array(
        'activityTypes' => array(
          array('name' => 'First act'),
          array('name' => 'Second act'),
        ),
63
      ),
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    ));

    $this->assertTrue(CRM_Case_BAO_CaseType::isForkable($caseTypeId));
    $this->assertTrue(CRM_Case_BAO_CaseType::isForked($caseTypeId));
    $this->assertDBNotNull('CRM_Case_BAO_CaseType', $caseTypeId, 'definition', 'id', "Should not have DB-based definition");

    $this->callAPISuccess('CaseType', 'create', array(
      'id' => $caseTypeId,
      'definition' => 'null',
    ));

    $this->assertDBNull('CRM_Case_BAO_CaseType', $caseTypeId, 'definition', 'id', "Should not have DB-based definition");
    $this->assertTrue(CRM_Case_BAO_CaseType::isForkable($caseTypeId));
    $this->assertFalse(CRM_Case_BAO_CaseType::isForked($caseTypeId));
  }

  /**
   * Attempt to edit the definition of UnforkableCaseType. This fails.
   */
83
  public function testUnforkable() {
84 85 86 87 88 89 90 91 92 93 94 95 96 97
    $caseTypeId = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', 'UnforkableCaseType', 'id', 'name');
    $this->assertTrue(is_numeric($caseTypeId) && $caseTypeId > 0);
    $this->assertDBNull('CRM_Case_BAO_CaseType', $caseTypeId, 'definition', 'id', "Should not have DB-based definition");

    $this->assertFalse(CRM_Case_BAO_CaseType::isForkable($caseTypeId));
    $this->assertFalse(CRM_Case_BAO_CaseType::isForked($caseTypeId));

    $this->callAPISuccess('CaseType', 'create', array(
      'id' => $caseTypeId,
      'definition' => array(
        'activityTypes' => array(
          array('name' => 'First act'),
          array('name' => 'Second act'),
        ),
98
      ),
99 100 101 102 103 104 105 106 107 108 109
    ));

    $this->assertFalse(CRM_Case_BAO_CaseType::isForkable($caseTypeId));
    $this->assertFalse(CRM_Case_BAO_CaseType::isForked($caseTypeId));
    $this->assertDBNull('CRM_Case_BAO_CaseType', $caseTypeId, 'definition', 'id', "Should not have DB-based definition");
  }

  /**
   * @param $caseTypes
   * @see \CRM_Utils_Hook::caseTypes
   */
110
  public function hook_caseTypes(&$caseTypes) {
111 112 113 114 115 116 117 118 119 120 121 122
    $caseTypes['ForkableCaseType'] = array(
      'module' => 'civicrm',
      'name' => 'ForkableCaseType',
      'file' => __DIR__ . '/ForkableCaseType.xml',
    );
    $caseTypes['UnforkableCaseType'] = array(
      'module' => 'civicrm',
      'name' => 'UnforkableCaseType',
      'file' => __DIR__ . '/UnforkableCaseType.xml',
    );
  }

123
}