Skip to content

Commit

Permalink
Merge pull request #18586 from eileenmcnaughton/blok
Browse files Browse the repository at this point in the history
[REF] Fix Event location to create it's locations directly rather than via shared methods
  • Loading branch information
colemanw authored Sep 26, 2020
2 parents d9c113b + aa06ad4 commit b1ef004
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 185 deletions.
1 change: 1 addition & 0 deletions CRM/Core/BAO/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public static function create(&$params, $fixAddress = TRUE) {
* @return int
*/
public static function createLocBlock($location, $entityElements) {
CRM_Core_Error::deprecatedFunctionWarning('Use LocBlock api');
$locId = self::findExisting($entityElements);
$locBlock = [];

Expand Down
35 changes: 19 additions & 16 deletions CRM/Event/Form/ManageEvent/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
*/

use Civi\Api4\Event;
use Civi\Api4\LocBlock;
use Civi\Api4\Email;
use Civi\Api4\Phone;
use Civi\Api4\Address;

/**
*
Expand Down Expand Up @@ -227,14 +231,10 @@ public function postProcess() {
CRM_Event_BAO_Event::deleteEventLocBlock($this->_oldLocBlockId, $this->_id);
}

// get ready with location block params
$params['entity_table'] = 'civicrm_event';
$params['entity_id'] = $this->_id;

$isUpdateToExistingLocationBlock = !empty($params['loc_event_id']) && (int) $params['loc_event_id'] === $this->locationBlock['loc_block_id'];
// It should be impossible for there to be no default location type. Consider removing this handling
$defaultLocationTypeID = CRM_Core_BAO_LocationType::getDefault()->id ?? 1;
$location = [];

foreach ([
'address' => $params['address'],
'phone' => $params['phone'],
Expand All @@ -249,18 +249,21 @@ public function postProcess() {
$params[$block][$index]['id'] = $this->locationBlock['loc_block.' . $block . $fieldKey];
}
}
if ($block !== 'address') {
$location[$block] = CRM_Core_BAO_Block::create($block, $params, 'event');
}
else {
$location[$block] = CRM_Core_BAO_Address::create($params, TRUE, 'event');
}
}

$params['loc_block_id'] = CRM_Core_BAO_Location::createLocBlock($location, [
'entity_table' => $params['entity_table'],
'entity_id' => $params['entity_id'],
]);
$addresses = Address::save(FALSE)->setRecords($params['address'])->execute();
$emails = Email::save(FALSE)->setRecords($params['email'])->execute();
$phones = Phone::save(FALSE)->setRecords($params['phone'])->execute();

$params['loc_block_id'] = LocBlock::save(FALSE)->setRecords([
[
'email_id' => $emails[0]['id'] ?? NULL,
'address_id' => $addresses[0]['id'] ?? NULL,
'phone_id' => $phones[0]['id'] ?? NULL,
'email_2_id' => $emails[1]['id'] ?? NULL,
'address_2_id' => $addresses[1]['id'] ?? NULL,
'phone_2_id' => $phones[1]['id'] ?? NULL,
],
])->execute()->first()['id'];

// finally update event params
$params['id'] = $this->_id;
Expand Down
22 changes: 22 additions & 0 deletions Civi/Api4/LocBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

namespace Civi\Api4;

/**
* ContributionPage entity.
*
* @package Civi\Api4
*/
class LocBlock extends Generic\DAOEntity {

}
159 changes: 0 additions & 159 deletions tests/phpunit/CRM/Core/BAO/LocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,165 +184,6 @@ public function testCreateWithoutLocBlock() {
$this->contactDelete($contactId);
}

/**
* Create() method
* create various elements of location block
* with civicrm_loc_block
*/
public function testCreateWithLocBlock() {
$this->_contactId = $this->individualCreate();
$event = $this->eventCreate();
$params = [
'address' => [
'1' => [
'street_address' => 'Saint Helier St',
'supplemental_address_1' => 'Hallmark Ct',
'supplemental_address_2' => 'Jersey Village',
'supplemental_address_3' => 'My Town',
'city' => 'Newark',
'postal_code' => '01903',
'country_id' => 1228,
'state_province_id' => 1029,
'geo_code_1' => '18.219023',
'geo_code_2' => '-105.00973',
'is_primary' => 1,
'location_type_id' => 1,
],
],
'email' => [
'1' => [
'email' => 'john.smith@example.org',
'is_primary' => 1,
'location_type_id' => 1,
],
],
'phone' => [
'1' => [
'phone_type_id' => 1,
'phone' => '303443689',
'is_primary' => 1,
'location_type_id' => 1,
],
'2' => [
'phone_type_id' => 2,
'phone' => '9833910234',
'location_type_id' => 1,
],
],
'im' => [
'1' => [
'name' => 'jane.doe',
'provider_id' => 1,
'location_type_id' => 1,
'is_primary' => 1,
],
],
];

$params['entity_id'] = $event['id'];
$params['entity_table'] = 'civicrm_event';

//create location block.
//with various element of location block
//like address, phone, email, im.
foreach (['phone', 'email', 'im', 'openid', 'address'] as $block) {
if ($block !== 'address') {
$location[$block] = CRM_Core_BAO_Block::create($block, $params, 'event');
}
else {
$location[$block] = CRM_Core_BAO_Address::create($params, TRUE, 'event');
}
}

//update event record with location block id
$eventParams = [
'id' => $event['id'],
'loc_block_id' => CRM_Core_BAO_Location::createLocBlock($location, [
'entity_id' => $event['id'],
'entity_table' => 'civicrm_event',
]),
];

CRM_Event_BAO_Event::add($eventParams);

//Now check DB for location block

$this->assertDBCompareValue('CRM_Event_DAO_Event',
$event['id'],
'loc_block_id',
'id',
$eventParams['loc_block_id'],
'Checking database for the record.'
);
$locElementIds = [];
$locParams = ['id' => $eventParams['loc_block_id']];
CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_LocBlock',
$locParams,
$locElementIds
);

//Now check DB for location elements.
$searchParams = [
'id' => $locElementIds['address_id'] ?? NULL,
'location_type_id' => 1,
'is_primary' => 1,
];
$compareParams = [
'street_address' => 'Saint Helier St',
'supplemental_address_1' => 'Hallmark Ct',
'supplemental_address_2' => 'Jersey Village',
'supplemental_address_3' => 'My Town',
'city' => 'Newark',
'postal_code' => '01903',
'country_id' => 1228,
'state_province_id' => 1029,
'geo_code_1' => '18.219023',
'geo_code_2' => '-105.00973',
];
$this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);

$searchParams = [
'id' => $locElementIds['email_id'] ?? NULL,
'location_type_id' => 1,
'is_primary' => 1,
];
$compareParams = ['email' => 'john.smith@example.org'];
$this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);

$searchParams = [
'id' => $locElementIds['phone_id'] ?? NULL,
'location_type_id' => 1,
'is_primary' => 1,
'phone_type_id' => 1,
];
$compareParams = ['phone' => '303443689'];
$this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);

$searchParams = [
'id' => $locElementIds['phone_2_id'] ?? NULL,
'location_type_id' => 1,
'phone_type_id' => 2,
];
$compareParams = ['phone' => '9833910234'];
$this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);

$searchParams = [
'id' => $locElementIds['im_id'] ?? NULL,
'location_type_id' => 1,
'is_primary' => 1,
];
$compareParams = [
'name' => 'jane.doe',
'provider_id' => 1,
];
$this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);

// Cleanup.
CRM_Core_BAO_Location::deleteLocBlock($eventParams['loc_block_id']);
$this->eventDelete($event['id']);
$this->contactDelete($this->_contactId);
}

/**
* GetValues() method
* get the values of various location elements
Expand Down
108 changes: 98 additions & 10 deletions tests/phpunit/CRM/Event/Form/ManageEvent/LocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,96 @@ class CRM_Event_Form_ManageEvent_LocationTest extends CiviUnitTestCase {
*/
public function testSubmit() {
$eventID = (int) $this->eventCreate()['id'];
$form = $this->getFormObject('CRM_Event_Form_ManageEvent_Location', $this->getFormValues());
$form->set('id', $eventID);
$form->preProcess();
$form->buildQuickForm();
$form->postProcess();
$this->submitForm([], $eventID);
$this->assertCorrectEmails($eventID);

// Now do it again to see if it gets messed with.
$form = $this->getFormObject('CRM_Event_Form_ManageEvent_Location', array_merge($this->getFormValues(), ['loc_event_id' => $this->ids['LocBlock'][0]]));
$form->set('id', $eventID);
$form->preProcess();
$form->buildQuickForm();
$form->postProcess();
$this->submitForm(['loc_event_id' => $this->ids['LocBlock'][0]], $eventID);
$this->assertCorrectEmails($eventID);
}

/**
* Create() method
* create various elements of location block
* with civicrm_loc_block
*/
public function testCreateWithLocBlock() {
$eventID = (int) $this->eventCreate()['id'];
$this->submitForm([
'address' => [
'1' => [
'street_address' => 'Saint Helier St',
'supplemental_address_1' => 'Hallmark Ct',
'supplemental_address_2' => 'Jersey Village',
'supplemental_address_3' => 'My Town',
'city' => 'Newark',
'postal_code' => '01903',
'country_id' => 1228,
'state_province_id' => 1029,
'geo_code_1' => '18.219023',
'geo_code_2' => '-105.00973',
'is_primary' => 1,
'location_type_id' => 1,
],
],
'email' => [
'1' => [
'email' => 'john.smith@example.org',
'is_primary' => 1,
'location_type_id' => 1,
],
],
'phone' => [
'1' => [
'phone_type_id' => 1,
'phone' => '303443689',
'is_primary' => 1,
'location_type_id' => 1,
],
'2' => [
'phone_type_id' => 2,
'phone' => '9833910234',
'location_type_id' => 1,
],
],
], $eventID);

//Now check DB for location block

$locationBlock = Event::get()
->addWhere('id', '=', $eventID)
->setSelect(['loc_block.*', 'loc_block_id'])
->execute()->first();

$address = $this->callAPISuccessGetSingle('Address', ['id' => $locationBlock['loc_block.address_id']]);

$this->assertEquals([
'id' => $address['id'],
'location_type_id' => '1',
'is_primary' => '1',
'is_billing' => '0',
'street_address' => 'Saint Helier St',
'supplemental_address_1' => 'Hallmark Ct',
'supplemental_address_2' => 'Jersey Village',
'supplemental_address_3' => 'My Town',
'city' => 'Newark',
'postal_code' => '01903',
'country_id' => 1228,
'state_province_id' => 1029,
'geo_code_1' => '18.219023',
'geo_code_2' => '-105.00973',
'manual_geo_code' => '0',
], $address);

$this->callAPISuccessGetSingle('Email', ['id' => $locationBlock['loc_block.email_id'], 'email' => 'john.smith@example.org']);
$this->callAPISuccessGetSingle('Phone', ['id' => $locationBlock['loc_block.phone_id'], 'phone' => '303443689']);
$this->callAPISuccessGetSingle('Phone', ['id' => $locationBlock['loc_block.phone_2_id'], 'phone' => '9833910234']);

// Cleanup.
CRM_Core_BAO_Location::deleteLocBlock($locationBlock['loc_block_id']);
$this->eventDelete($eventID);
}

/**
* Get the values to submit for the form.
*
Expand Down Expand Up @@ -120,4 +194,18 @@ protected function assertCorrectEmails($eventID) {
return $emails;
}

/**
* @param array $formValues
* @param int $eventID
*
* @throws \CRM_Core_Exception
*/
protected function submitForm(array $formValues, int $eventID): void {
$form = $this->getFormObject('CRM_Event_Form_ManageEvent_Location', array_merge($this->getFormValues(), $formValues));
$form->set('id', $eventID);
$form->preProcess();
$form->buildQuickForm();
$form->postProcess();
}

}
1 change: 1 addition & 0 deletions tests/phpunit/api/v3/ACLPermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function tearDown() {
$this->cleanUpAfterACLs();
$tablesToTruncate = [
'civicrm_contact',
'civicrm_address',
'civicrm_group_contact',
'civicrm_group',
'civicrm_acl',
Expand Down

0 comments on commit b1ef004

Please sign in to comment.