function deleteTestObjects() { // Note: You might argue that the FK relations between test // objects could make this problematic; however, it should // behave intuitively as long as we mentally split our // test-objects between the "manual/primary records" // and the "automatic/secondary records" foreach ($this->_testObjects as $daoName => $daoIds) { foreach ($daoIds as $daoId) { CRM_Core_DAO::deleteTestObjects($daoName, array('id' => $daoId)); } } $this->_testObjects = array(); }
/** * @dataProvider entities_updatesingle * * limitations include the problem with avoiding loops when creating test objects - * hence FKs only set by createTestObject when required. e.g parent_id on campaign is not being followed through * Currency - only seems to support US */ public function testCreateSingleValueAlter($entityName) { if (in_array($entityName, $this->toBeImplemented['create'])) { // $this->markTestIncomplete("civicrm_api3_{$Entity}_create to be implemented"); return; } $baoString = _civicrm_api3_get_DAO($entityName); $this->assertNotEmpty($baoString, $entityName); $this->assertNotEmpty($entityName, $entityName); $fieldsget = $fields = $this->callAPISuccess($entityName, 'getfields', array('action' => 'get')); if ($entityName != 'Pledge') { $fields = $this->callAPISuccess($entityName, 'getfields', array('action' => 'create')); } $fields = $fields['values']; $return = array_keys($fieldsget['values']); $valuesNotToReturn = $this->getKnownUnworkablesUpdateSingle($entityName, 'break_return'); // these can't be requested as return values $entityValuesThatDontWork = array_merge($this->getKnownUnworkablesUpdateSingle($entityName, 'cant_update'), $this->getKnownUnworkablesUpdateSingle($entityName, 'cant_return'), $valuesNotToReturn); $return = array_diff($return, $valuesNotToReturn); $baoObj = new CRM_Core_DAO(); $baoObj->createTestObject($baoString, array('currency' => 'USD'), 2, 0); $getentities = $this->callAPISuccess($entityName, 'get', array('sequential' => 1, 'return' => $return, 'options' => array('sort' => 'id DESC', 'limit' => 2))); // lets use first rather than assume only one exists $entity = $getentities['values'][0]; $entity2 = $getentities['values'][1]; foreach ($fields as $field => $specs) { $fieldName = $field; if (!empty($specs['uniquename'])) { $fieldName = $specs['uniquename']; } if ($field == 'currency' || $field == 'id' || $field == strtolower($entityName) . '_id' || in_array($field, $entityValuesThatDontWork)) { //@todo id & entity_id are correct but we should fix currency & frequency_day continue; } switch ($specs['type']) { case CRM_Utils_Type::T_DATE: case CRM_Utils_Type::T_TIMESTAMP: $entity[$fieldName] = '2012-05-20'; break; //case CRM_Utils_Type::T_DATETIME: //case CRM_Utils_Type::T_DATETIME: case 12: $entity[$fieldName] = '2012-05-20 03:05:20'; break; case CRM_Utils_Type::T_STRING: case CRM_Utils_Type::T_BLOB: case CRM_Utils_Type::T_MEDIUMBLOB: case CRM_Utils_Type::T_TEXT: case CRM_Utils_Type::T_LONGTEXT: case CRM_Utils_Type::T_EMAIL: $entity[$fieldName] = substr('New String', 0, CRM_Utils_Array::Value('maxlength', $specs, 100)); break; case CRM_Utils_Type::T_INT: // probably created with a 1 $entity[$fieldName] = '6'; if (!empty($specs['FKClassName'])) { if ($specs['FKClassName'] == $baoString) { $entity[$fieldName] = (string) $entity2['id']; } else { $uniqueName = CRM_Utils_Array::value('uniqueName', $specs); $entity[$fieldName] = (string) empty($entity2[$field]) ? CRM_Utils_Array::value($uniqueName, $entity2) : $entity2[$field]; //todo - there isn't always something set here - & our checking on unset values is limited if (empty($entity[$field])) { unset($entity[$field]); } } } break; case CRM_Utils_Type::T_BOOLEAN: // probably created with a 1 $entity[$fieldName] = '0'; break; case CRM_Utils_Type::T_FLOAT: case CRM_Utils_Type::T_MONEY: $entity[$field] = '222'; break; case CRM_Utils_Type::T_URL: $entity[$field] = 'warm.beer.com'; } if (!empty($specs['pseudoconstant']) || !empty($specs['enumValues'])) { $options = $this->callAPISuccess($entityName, 'getoptions', array('context' => 'create', 'field' => $field)); if (empty($options['values'])) { } $entity[$field] = array_rand($options['values']); } $updateParams = array('id' => $entity['id'], $field => isset($entity[$field]) ? $entity[$field] : NULL); $update = $this->callAPISuccess($entityName, 'create', $updateParams); $checkParams = array('id' => $entity['id'], 'sequential' => 1, 'return' => $return, 'options' => array('sort' => 'id DESC', 'limit' => 2)); $checkEntity = $this->callAPISuccess($entityName, 'getsingle', $checkParams); $this->assertAPIArrayComparison($entity, $checkEntity, array(), "checking if {$fieldName} was correctly updaetd\n" . print_r(array('update-params' => $updateParams, 'update-result' => $update, 'getsingle-params' => $checkParams, 'getsingle-result' => $checkEntity, 'expected entity' => $entity), TRUE)); } $baoObj->deleteTestObjects($baoString); $baoObj->free(); }
/** * Deletes the this object plus any dependent objects that are associated with it. * ONLY USE FOR TESTING * * @param string $daoName * @param array $params */ public static function deleteTestObjects($daoName, $params = array()) { //this is a test function also backtrace is set for the test suite it sometimes unsets itself // so we re-set here in case $config = CRM_Core_Config::singleton(); $config->backtrace = TRUE; $object = new $daoName(); $object->id = CRM_Utils_Array::value('id', $params); $deletions = array(); // array(array(0 => $daoName, 1 => $daoParams)) if ($object->find(TRUE)) { $fields =& $object->fields(); foreach ($fields as $name => $value) { $dbName = $value['name']; $FKClassName = CRM_Utils_Array::value('FKClassName', $value); $required = CRM_Utils_Array::value('required', $value); if ($FKClassName != NULL && $object->{$dbName} && !in_array($FKClassName, CRM_Core_DAO::$_testEntitiesToSkip) && ($required || $dbName == 'contact_id') && $dbName != 'member_of_contact_id') { $deletions[] = array($FKClassName, array('id' => $object->{$dbName})); // x } } } $object->delete(); foreach ($deletions as $deletion) { CRM_Core_DAO::deleteTestObjects($deletion[0], $deletion[1]); } }
/** * Delete test objects. * * Note: You might argue that the FK relations between test * objects could make this problematic; however, it should * behave intuitively as long as we mentally split our * test-objects between the "manual/primary records" * and the "automatic/secondary records" */ public function deleteTestObjects() { foreach ($this->_testObjects as $daoName => $daoIds) { foreach ($daoIds as $daoId) { CRM_Core_DAO::deleteTestObjects($daoName, array('id' => $daoId)); } } $this->_testObjects = array(); }
/** * Ensure that the "get" operation accepts limiting the #result records. * * TODO Consider making a separate entity list ("entities_getlimit") * For the moment, the "entities_updatesingle" list should give a good * sense for which entities support createTestObject * * @dataProvider entities_getlimit * * @param string $entityName */ public function testLimit($entityName) { $cases = array(); // each case is array(0 => $inputtedApiOptions, 1 => $expectedResultCount) $cases[] = array(array('options' => array('limit' => NULL)), 30, 'check that a NULL limit returns unlimited'); $cases[] = array(array('options' => array('limit' => FALSE)), 30, 'check that a FALSE limit returns unlimited'); $cases[] = array(array('options' => array('limit' => 0)), 30, 'check that a 0 limit returns unlimited'); $cases[] = array(array('options' => array('limit' => 5)), 5, 'check that a 5 limit returns 5'); $cases[] = array(array(), 25, 'check that no limit returns 25'); $baoString = _civicrm_api3_get_BAO($entityName); if (empty($baoString)) { $this->markTestIncomplete("Entity [{$entityName}] cannot be mocked - no known DAO"); return; } // make 30 test items -- 30 > 25 (the default limit) $ids = array(); for ($i = 0; $i < 30; $i++) { $baoObj = CRM_Core_DAO::createTestObject($baoString, array('currency' => 'USD')); $ids[] = $baoObj->id; } // each case is array(0 => $inputtedApiOptions, 1 => $expectedResultCount) foreach ($cases as $case) { $this->checkLimitAgainstExpected($entityName, $case[0], $case[1], $case[2]); //non preferred / legacy syntax if (isset($case[0]['options']['limit'])) { $this->checkLimitAgainstExpected($entityName, array('rowCount' => $case[0]['options']['limit']), $case[1], $case[2]); $this->checkLimitAgainstExpected($entityName, array('option_limit' => $case[0]['options']['limit']), $case[1], $case[2]); $this->checkLimitAgainstExpected($entityName, array('option.limit' => $case[0]['options']['limit']), $case[1], $case[2]); } } foreach ($ids as $id) { CRM_Core_DAO::deleteTestObjects($baoString, array('id' => $id)); } $baoObj->free(); }
static function deleteTestObjects($daoName, $params = array()) { $object = new $daoName(); $object->id = CRM_Utils_Array::value('id', $params); $deletions = array(); // array(array(0 => $daoName, 1 => $daoParams)) if ($object->find(TRUE)) { $fields =& $object->fields(); foreach ($fields as $name => $value) { $dbName = $value['name']; $FKClassName = CRM_Utils_Array::value('FKClassName', $value); $required = CRM_Utils_Array::value('required', $value); if ($FKClassName != NULL && $object->{$dbName} && !in_array($FKClassName, CRM_Core_DAO::$_testEntitiesToSkip) && ($required || $dbName == 'contact_id')) { $deletions[] = array($FKClassName, array('id' => $object->{$dbName})); // x } } } $object->delete(); foreach ($deletions as $deletion) { CRM_Core_DAO::deleteTestObjects($deletion[0], $deletion[1]); } }
static function deleteTestObjects($daoName, $params = array()) { require_once "CRM/Utils/Type.php"; require_once str_replace('_', DIRECTORY_SEPARATOR, $daoName) . ".php"; eval('$object =& new ' . $daoName . '( );'); $object->id = CRM_Utils_Array::value('id', $params); if ($object->find(true)) { $fields =& $object->fields(); foreach ($fields as $name => $value) { $dbName = $value['name']; $FKClassName = CRM_Utils_Array::value('FKClassName', $value); if ($FKClassName != null && $object->{$dbName}) { //if it is required we need to generate the dependency object first CRM_Core_DAO::deleteTestObjects($FKClassName, array('id' => $object->{$dbName})); } } } $object->delete(); }
/** * This is a test to check if setting fields one at a time alters other fields. * * Issues Hit so far = * 1) Currency keeps getting reset to USD - BUT this may be the only enabled currency * - in which case it is valid * 2) */ public function testCreateAutoGrant() { $entityName = $this->_entity; $baoString = 'CRM_Grant_BAO_Grant'; $fields = $this->callAPISuccess($entityName, 'getfields', array('action' => 'create')); $fields = $fields['values']; $return = array_keys($fields); $baoObj = new CRM_Core_DAO(); $baoObj->createTestObject($baoString, array('currency' => 'USD'), 2, 0); $getentities = $this->callAPISuccess($entityName, 'get', array('sequential' => 1, 'return' => $return)); // lets use first rather than assume only one exists $entity = $getentities['values'][0]; $entity2 = $getentities['values'][1]; foreach ($fields as $field => $specs) { if ($field == 'currency' || $field == 'id') { continue; } switch ($specs['type']) { case CRM_Utils_Type::T_DATE: case CRM_Utils_Type::T_TIMESTAMP: $entity[$field] = '2012-05-20'; break; case CRM_Utils_Type::T_STRING: case CRM_Utils_Type::T_BLOB: case CRM_Utils_Type::T_MEDIUMBLOB: case CRM_Utils_Type::T_TEXT: case CRM_Utils_Type::T_LONGTEXT: case CRM_Utils_Type::T_EMAIL: $entity[$field] = 'New String'; break; case CRM_Utils_Type::T_INT: // probably created with a 1 $entity[$field] = 2; if (!empty($specs['FKClassName'])) { $entity[$field] = empty($entity2[$field]) ? $entity2[$specs]['uniqueName'] : $entity2[$field]; } break; case CRM_Utils_Type::T_BOOLEAN: // probably created with a 1 $entity[$field] = 0; break; case CRM_Utils_Type::T_FLOAT: case CRM_Utils_Type::T_MONEY: $entity[$field] = 222; break; case CRM_Utils_Type::T_URL: $entity[$field] = 'warm.beer.com'; } $updateParams = array('id' => $entity['id'], $field => $entity[$field], 'debug' => 1); $update = $this->callAPISuccess($entityName, 'create', $updateParams); $this->assertAPISuccess($update, "setting {$field} to {$entity[$field]} in line " . __LINE__); $checkParams = array('id' => $entity['id'], 'sequential' => 1); $checkEntity = $this->callAPISuccess($entityName, 'getsingle', $checkParams); $this->assertAPIArrayComparison((array) $entity, $checkEntity); } $baoObj->deleteTestObjects($baoString); $baoObj->free(); }
static function deleteTestObjects($daoName, $params = array()) { require_once str_replace('_', DIRECTORY_SEPARATOR, $daoName) . ".php"; eval('$object = new ' . $daoName . '( );'); $object->id = CRM_Utils_Array::value('id', $params); $deletions = array(); // array(array(0 => $daoName, 1 => $daoParams)) if ($object->find(TRUE)) { $fields =& $object->fields(); foreach ($fields as $name => $value) { $dbName = $value['name']; $FKClassName = CRM_Utils_Array::value('FKClassName', $value); if ($FKClassName != NULL && $object->{$dbName} && !in_array($FKClassName, CRM_Core_DAO::$_testEntitiesToSkip)) { $deletions[] = array($FKClassName, array('id' => $object->{$dbName})); // x } } } $object->delete(); foreach ($deletions as $deletion) { CRM_Core_DAO::deleteTestObjects($deletion[0], $deletion[1]); } }