From 08e37a2aff317467875ef8168e8be5b67205a213 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 6 Jul 2016 16:03:55 +1200 Subject: [PATCH] [NFC] CRM-19033 improve standardisation of tests NB jenkins is doing wierd things here so trying a small subset to see what jenkins does --- .../phpunit/CRM/Core/BAO/CustomFieldTest.php | 178 +++++++++--------- 1 file changed, 92 insertions(+), 86 deletions(-) diff --git a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php index e0159e8791..285aebc19e 100644 --- a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php +++ b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php @@ -1,7 +1,5 @@ customGroupCreate(array('extends' => 'Individual')); $fields = array( 'label' => 'testFld', 'data_type' => 'String', 'html_type' => 'Text', - 'custom_group_id' => $customGroup->id, + 'custom_group_id' => $customGroup['id'], ); - $customField = CRM_Core_BAO_CustomField::create($fields); - $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id', + CRM_Core_BAO_CustomField::create($fields); + $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id', 'Database check for created CustomField.' ); $fields = array( @@ -30,10 +28,10 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'is_active' => 1, 'data_type' => 'String', 'html_type' => 'Text', - 'custom_group_id' => $customGroup->id, + 'custom_group_id' => $customGroup['id'], ); - $customField = CRM_Core_BAO_CustomField::create($fields); + CRM_Core_BAO_CustomField::create($fields); $this->assertDBNotNull('CRM_Core_DAO_CustomField', 1, 'id', 'is_active', 'Database check for edited CustomField.'); $this->assertDBNotNull('CRM_Core_DAO_CustomField', $fields['label'], 'id', 'label', 'Database check for edited CustomField.'); @@ -42,60 +40,60 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { $this->assertEquals(strtolower("{$dbFieldName}_{$customFieldID}"), $dbColumnName, "Column name ends in ID"); - Custom::deleteGroup($customGroup); + $this->customGroupDelete($customGroup['id']); } - public function testCreateCustomfieldColumnName() { - $customGroup = Custom::createGroup(array(), 'Individual'); + public function testCreateCustomFieldColumnName() { + $customGroup = $this->customGroupCreate(array('extends' => 'Individual')); $fields = array( 'label' => 'testFld 2', 'column_name' => 'special_colname', 'data_type' => 'String', 'html_type' => 'Text', - 'custom_group_id' => $customGroup->id, + 'custom_group_id' => $customGroup['id'], ); - $customField = CRM_Core_BAO_CustomField::create($fields); - $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id', + CRM_Core_BAO_CustomField::create($fields); + $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id', 'Database check for created CustomField.' ); $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.'); $this->assertEquals($fields['column_name'], $dbColumnName, "Column name set as specified"); - Custom::deleteGroup($customGroup); + $this->customGroupDelete($customGroup['id']); } - public function testCreateCustomfieldName() { - $customGroup = Custom::createGroup(array(), 'Individual'); + public function testCreateCustomFieldName() { + $customGroup = $this->customGroupCreate(array('extends' => 'Individual')); $fields = array( 'label' => 'testFld 2', 'name' => 'special_fldlname', 'data_type' => 'String', 'html_type' => 'Text', - 'custom_group_id' => $customGroup->id, + 'custom_group_id' => $customGroup['id'], ); - $customField = CRM_Core_BAO_CustomField::create($fields); - $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id', + CRM_Core_BAO_CustomField::create($fields); + $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id', 'Database check for created CustomField.' ); $dbFieldName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'name', 'id', 'Database check for edited CustomField.'); $this->assertEquals($fields['name'], $dbFieldName, "Column name set as specified"); - Custom::deleteGroup($customGroup); + $this->customGroupDelete($customGroup['id']); } public function testGetFields() { - $customGroup = Custom::createGroup(array(), 'Individual'); + $customGroup = $this->customGroupCreate(array('extends' => 'Individual')); $fields = array( 'label' => 'testFld1', 'data_type' => 'String', 'html_type' => 'Text', 'is_active' => 1, - 'custom_group_id' => $customGroup->id, + 'custom_group_id' => $customGroup['id'], ); - $customField1 = CRM_Core_BAO_CustomField::create($fields); - $customFieldID1 = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id', + CRM_Core_BAO_CustomField::create($fields); + $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id', 'Database check for created CustomField.' ); $fields = array( @@ -103,22 +101,18 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'data_type' => 'String', 'html_type' => 'Text', 'is_active' => 1, - 'custom_group_id' => $customGroup->id, + 'custom_group_id' => $customGroup['id'], ); - $customField2 = CRM_Core_BAO_CustomField::create($fields); - $customFieldID2 = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id', + CRM_Core_BAO_CustomField::create($fields); + $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id', 'Database check for created CustomField.' ); - $getCustomFields = array(); - $getCustomFields = CRM_Core_BAO_CustomField::getFields('Individual', TRUE, TRUE); - //$this->assertEquals( 'testFld1', $getCustomFields[$customFieldID1][0], 'Confirm First Custom field label' ); - //$this->assertEquals( 'testFld2', $getCustomFields[$customFieldID2][0], 'Confirm Second Custom field label' ); - Custom::deleteGroup($customGroup); + $this->customGroupDelete($customGroup['id']); } public function testGetDisplayedValues() { - $customGroup = Custom::createGroup(array(), 'Individual'); + $customGroup = $this->customGroupCreate(array('extends' => 'Individual')); $fieldsToCreate = array( array( 'data_type' => 'Country', @@ -178,7 +172,7 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { foreach ($fieldsToCreate as $num => $field) { $params = $field + array( 'label' => 'test field ' . $num, - 'custom_group_id' => $customGroup->id, + 'custom_group_id' => $customGroup['id'], ); unset($params['tests']); $createdField = $this->callAPISuccess('customField', 'create', $params); @@ -187,36 +181,39 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { } } - Custom::deleteGroup($customGroup); + $this->customGroupDelete($customGroup['id']); } - public function testDeleteCustomfield() { - $customGroup = Custom::createGroup(array(), 'Individual'); + public function testDeleteCustomField() { + $customGroup = $this->customGroupCreate(array('extends' => 'Individual')); $fields = array( - 'groupId' => $customGroup->id, + 'custom_group_id' => $customGroup['id'], 'label' => 'Throwaway Field', 'dataType' => 'Memo', 'htmlType' => 'TextArea', ); - $customField = Custom::createField(array(), $fields); - $this->assertNotNull($customField); - CRM_Core_BAO_CustomField::deleteField($customField); - $this->assertDBNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', + $customField = $this->customFieldCreate($fields); + $fieldObject = new CRM_Core_BAO_CustomField(); + $fieldObject->id = $customField['id']; + $fieldObject->find(TRUE); + CRM_Core_BAO_CustomField::deleteField($fieldObject); + $this->assertDBNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id', 'Database check for deleted Custom Field.' ); - Custom::deleteGroup($customGroup); + $this->customGroupDelete($customGroup['id']); } /** - * Move a custom field from $groupA to $groupB. Make sure that data records are - * correctly matched and created. + * Move a custom field from $groupA to $groupB. + * + * Make sure that data records are correctly matched and created. */ public function testMoveField() { $countriesByName = array_flip(CRM_Core_PseudoConstant::country(FALSE, FALSE)); $this->assertTrue($countriesByName['ANDORRA'] > 0); $groups = array( - 'A' => Custom::createGroup(array( + 'A' => $this->customGroupCreate(array( 'title' => 'Test_Group A', 'name' => 'test_group_a', 'extends' => array('Individual'), @@ -225,7 +222,7 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'is_active' => 1, 'version' => 3, )), - 'B' => Custom::createGroup(array( + 'B' => $this->customGroupCreate(array( 'title' => 'Test_Group B', 'name' => 'test_group_b', 'extends' => array('Individual'), @@ -235,54 +232,63 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'version' => 3, )), ); + $groupA = $groups['A']['values'][$groups['A']['id']]; + $groupB = $groups['B']['values'][$groups['B']['id']]; + $countryA = $this->customFieldCreate(array( + 'custom_group_id' => $groups['A']['id'], + 'label' => 'Country A', + 'dataType' => 'Country', + 'htmlType' => 'Select Country', + 'default_value' => NULL, + )); + $countryB = $this->customFieldCreate(array( + 'custom_group_id' => $groups['A']['id'], + 'label' => 'Country B', + 'dataType' => 'Country', + 'htmlType' => 'Select Country', + 'default_value' => NULL, + )); + $countryC = $this->customFieldCreate(array( + 'custom_group_id' => $groups['B']['id'], + 'label' => 'Country C', + 'dataType' => 'Country', + 'htmlType' => 'Select Country', + 'default_value' => NULL, + )); + $fields = array( - 'countryA' => Custom::createField(array(), array( - 'groupId' => $groups['A']->id, - 'label' => 'Country A', - 'dataType' => 'Country', - 'htmlType' => 'Select Country', - )), - 'countryB' => Custom::createField(array(), array( - 'groupId' => $groups['A']->id, - 'label' => 'Country B', - 'dataType' => 'Country', - 'htmlType' => 'Select Country', - )), - 'countryC' => Custom::createField(array(), array( - 'groupId' => $groups['B']->id, - 'label' => 'Country C', - 'dataType' => 'Country', - 'htmlType' => 'Select Country', - )), + 'countryA' => $countryA['values'][$countryA['id']], + 'countryB' => $countryB['values'][$countryB['id']], + 'countryC' => $countryC['values'][$countryC['id']], ); $contacts = array( 'alice' => $this->individualCreate(array( 'first_name' => 'Alice', 'last_name' => 'Albertson', - 'custom_' . $fields['countryA']->id => $countriesByName['ANDORRA'], - 'custom_' . $fields['countryB']->id => $countriesByName['BARBADOS'], + 'custom_' . $fields['countryA']['id'] => $countriesByName['ANDORRA'], + 'custom_' . $fields['countryB']['id'] => $countriesByName['BARBADOS'], )), 'bob' => $this->individualCreate(array( 'first_name' => 'Bob', 'last_name' => 'Roberts', - 'custom_' . $fields['countryA']->id => $countriesByName['AUSTRIA'], - 'custom_' . $fields['countryB']->id => $countriesByName['BERMUDA'], - 'custom_' . $fields['countryC']->id => $countriesByName['CHAD'], + 'custom_' . $fields['countryA']['id'] => $countriesByName['AUSTRIA'], + 'custom_' . $fields['countryB']['id'] => $countriesByName['BERMUDA'], + 'custom_' . $fields['countryC']['id'] => $countriesByName['CHAD'], )), 'carol' => $this->individualCreate(array( 'first_name' => 'Carol', 'last_name' => 'Carolson', - 'custom_' . $fields['countryC']->id => $countriesByName['CAMBODIA'], + 'custom_' . $fields['countryC']['id'] => $countriesByName['CAMBODIA'], )), ); // Move! - CRM_Core_BAO_CustomField::moveField($fields['countryB']->id, $groups['B']->id); + CRM_Core_BAO_CustomField::moveField($fields['countryB']['id'], $groupB['id']); // Group[A] no longer has fields[countryB] $errorScope = CRM_Core_TemporaryErrorScope::useException(); try { - $this->assertDBQuery(1, "SELECT {$fields['countryB']->column_name} FROM {$groups['A']->table_name}"); + $this->assertDBQuery(1, "SELECT {$fields['countryB']['column_name']} FROM " . $groupA['table_name']); $this->fail('Expected exception when querying column on wrong table'); } catch (PEAR_Exception$e) { @@ -291,10 +297,10 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { // Alice: Group[B] has fields[countryB], but fields[countryC] did not exist before $this->assertDBQuery(1, - "SELECT count(*) FROM {$groups['B']->table_name} + "SELECT count(*) FROM {$groupB['table_name']} WHERE entity_id = %1 - AND {$fields['countryB']->column_name} = %3 - AND {$fields['countryC']->column_name} is null", + AND {$fields['countryB']['column_name']} = %3 + AND {$fields['countryC']['column_name']} is null", array( 1 => array($contacts['alice'], 'Integer'), 3 => array($countriesByName['BARBADOS'], 'Integer'), @@ -303,10 +309,10 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { // Bob: Group[B] has merged fields[countryB] and fields[countryC] on the same record $this->assertDBQuery(1, - "SELECT count(*) FROM {$groups['B']->table_name} + "SELECT count(*) FROM {$groupB['table_name']} WHERE entity_id = %1 - AND {$fields['countryB']->column_name} = %3 - AND {$fields['countryC']->column_name} = %4", + AND {$fields['countryB']['column_name']} = %3 + AND {$fields['countryC']['column_name']} = %4", array( 1 => array($contacts['bob'], 'Integer'), 3 => array($countriesByName['BERMUDA'], 'Integer'), @@ -316,18 +322,18 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { // Carol: Group[B] still has fields[countryC] but did not get fields[countryB] $this->assertDBQuery(1, - "SELECT count(*) FROM {$groups['B']->table_name} + "SELECT count(*) FROM {$groupB['table_name']} WHERE entity_id = %1 - AND {$fields['countryB']->column_name} is null - AND {$fields['countryC']->column_name} = %4", + AND {$fields['countryB']['column_name']} is null + AND {$fields['countryC']['column_name']} = %4", array( 1 => array($contacts['carol'], 'Integer'), 4 => array($countriesByName['CAMBODIA'], 'Integer'), ) ); - Custom::deleteGroup($groups['A']); - Custom::deleteGroup($groups['B']); + $this->customGroupDelete($groups['A']['id']); + $this->customGroupDelete($groupB['id']); } } -- 2.25.1