CRM-13244 - ActionSchedule - Extract entity-specific logic for tokens and dropdowns.
[civicrm-core.git] / tests / phpunit / api / v3 / ActionScheduleTest.php
index b3dc5e2e47d604643015a25c81aa7e89cf59277a..0c7da114d6c266cd8b0d5c71364f8f2264978d21 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 /**
- *  File for the TestActionSchedule class
+ * @file
+ * File for the TestActionSchedule class
  *
  *  (PHP 5)
  *
@@ -28,53 +29,36 @@ require_once 'CiviTest/CiviUnitTestCase.php';
 /**
  *  Test APIv3 civicrm_action_schedule functions
  *
- *  @package CiviCRM_APIv3
- *  @subpackage API_ActionSchedule
+ * @package CiviCRM_APIv3
+ * @subpackage API_ActionSchedule
  */
-
 class api_v3_ActionScheduleTest extends CiviUnitTestCase {
   protected $_params;
   protected $_params2;
   protected $_entity = 'action_schedule';
   protected $_apiversion = 3;
 
-
-
   /**
-   *  Test setup for every test
-   *
-   *  Connect to the database, truncate the tables that will be used
-   *  and redirect stdin to a temporary file
+   * Test setup for every test.
    */
   public function setUp() {
-    //  Connect to the database
     parent::setUp();
-
+    $this->useTransaction(TRUE);
   }
 
   /**
-   * Tears down the fixture, for example, closes a network connection.
-   * This method is called after a test is executed.
-   *
-   * @access protected
+   * Test simple create action schedule.
    */
-  function tearDown() {
-    $tablesToTruncate = array(
-      'civicrm_action_schedule',
-    );
-    $this->quickCleanup($tablesToTruncate, TRUE);
-  }
-
-
-  function testSimpleActionScheduleCreate() {
+  public function testSimpleActionScheduleCreate() {
     $oldCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
     $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
     $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
-    $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
     $scheduledStatus = CRM_Core_OptionGroup::getValue('activity_status', 'Scheduled', 'name');
-    $mappingId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', 'activity_type', 'id', 'entity_value');
+    $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings(array(
+      'entity_value' => 'activity_type',
+    )));
     $activityTypeId = CRM_Core_OptionGroup::getValue('activity_type', "Meeting", 'name');
-    $title = "simpleActionSchedule".substr(sha1(rand()), 0, 7);
+    $title = "simpleActionSchedule" . substr(sha1(rand()), 0, 7);
     $params = array(
       'title' => $title,
       'recipient' => $assigneeID,
@@ -84,35 +68,39 @@ class api_v3_ActionScheduleTest extends CiviUnitTestCase {
       'is_active' => 1,
       'record_activity' => 1,
       'start_action_date' => 'activity_date_time',
-      'mapping_id' => $mappingId,
+      'mapping_id' => $mapping->id,
     );
     $actionSchedule = $this->callAPISuccess('action_schedule', 'create', $params);
-    $this->assertTrue(is_numeric($actionSchedule['id']), "In line " . __LINE__);
-    $this->assertTrue($actionSchedule['id'] > 0, "In line " . __LINE__);
+    $this->assertTrue(is_numeric($actionSchedule['id']));
+    $this->assertTrue($actionSchedule['id'] > 0);
     $newCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
-    $this->assertEquals($oldCount+1, $newCount);
+    $this->assertEquals($oldCount + 1, $newCount);
   }
 
   /**
-   * Check if required fields are not passed
+   * Check if required fields are not passed.
    */
-  function testActionScheduleCreateWithoutRequired() {
+  public function testActionScheduleCreateWithoutRequired() {
     $params = array(
-        'subject' => 'this case should fail',
-        'scheduled_date_time' => date('Ymd'),
+      'subject' => 'this case should fail',
+      'scheduled_date_time' => date('Ymd'),
     );
-    $result = $this->callAPIFailure('activity', 'create', $params);
+    $this->callAPIFailure('activity', 'create', $params);
   }
 
-  function testActionScheduleWithScheduledDatesCreate() {
+  /**
+   * Test create with scheduled dates.
+   */
+  public function testActionScheduleWithScheduledDatesCreate() {
     $oldCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
     $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
     $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
-    $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
     $scheduledStatus = CRM_Core_OptionGroup::getValue('activity_status', 'Scheduled', 'name');
-    $mappingId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', 'activity_type', 'id', 'entity_value');
+    $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings(array(
+      'entity_value' => 'activity_type',
+    )));
     $activityTypeId = CRM_Core_OptionGroup::getValue('activity_type', "Meeting", 'name');
-    $title = "simpleActionSchedule".substr(sha1(rand()), 0, 7);
+    $title = "simpleActionSchedule" . substr(sha1(rand()), 0, 7);
     $params = array(
       'title' => $title,
       'recipient' => $assigneeID,
@@ -121,27 +109,27 @@ class api_v3_ActionScheduleTest extends CiviUnitTestCase {
       'entity_status' => $scheduledStatus,
       'is_active' => 1,
       'record_activity' => 1,
-      'mapping_id' => $mappingId,
+      'mapping_id' => $mapping->id,
       'start_action_offset' => 3,
       'start_action_unit' => 'day',
       'start_action_condition' => 'before',
       'start_action_date' => 'activity_date_time',
       'is_repeat' => 1,
-      'repetition_frequency_unit'=> 'day',
+      'repetition_frequency_unit' => 'day',
       'repetition_frequency_interval' => 3,
       'end_frequency_unit' => 'hour',
       'end_frequency_interval' => 0,
       'end_action' => 'before',
       'end_date' => 'activity_date_time',
       'body_html' => 'Test description',
-      'subject' => 'Test subject'
+      'subject' => 'Test subject',
     );
     $actionSchedule = $this->callAPISuccess('action_schedule', 'create', $params);
-    $this->assertTrue(is_numeric($actionSchedule['id']), "In line " . __LINE__);
-    $this->assertTrue($actionSchedule['id'] > 0, "In line " . __LINE__);
+    $this->assertTrue(is_numeric($actionSchedule['id']));
+    $this->assertTrue($actionSchedule['id'] > 0);
     $this->assertEquals($actionSchedule['values'][$actionSchedule['id']]['start_action_offset'][0], $params['start_action_offset']);
     $newCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
-    $this->assertEquals($oldCount+1, $newCount);
+    $this->assertEquals($oldCount + 1, $newCount);
 
   }