Skip to content

Commit

Permalink
Fixed CRM not mapping company custom fields for new companies
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhartless authored and escopecz committed Mar 21, 2018
1 parent bce8785 commit aea0732
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 15 deletions.
53 changes: 38 additions & 15 deletions plugins/MauticCrmBundle/Integration/CrmAbstractIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public function getLeadData(\DateTime $startDate = null, \DateTime $endDate = nu
* @param $data
* @param null $object
*
* @return Company|void
* @return Company|null
*/
public function getMauticCompany($data, $object = null)
{
Expand All @@ -305,23 +305,17 @@ public function getMauticCompany($data, $object = null)
// Assume JSON
$data = json_decode($data, true);
}
$config = $this->mergeConfigToFeatureSettings([]);

$config = $this->mergeConfigToFeatureSettings([]);
$matchedFields = $this->populateMauticLeadData($data, $config, 'company');

// Find unique identifier fields used by the integration
/** @var \Mautic\LeadBundle\Model\LeadModel $leadModel */
$companyModel = $this->factory->getModel('lead.company');

// Default to new company
$company = new Company();
$existingCompany = IdentifyCompanyHelper::identifyLeadsCompany($matchedFields, null, $companyModel);
$existingCompany = IdentifyCompanyHelper::identifyLeadsCompany($matchedFields, null, $this->companyModel);
if ($existingCompany[2]) {
$company = $existingCompany[2];
}

$companyFieldTypes = $this->fieldModel->getFieldListWithProperties('company');

foreach ($matchedFields as $companyField => $value) {
if (isset($companyFieldTypes[$companyField]['type']) && $companyFieldTypes[$companyField]['type'] == 'text') {
$matchedFields[$companyField] = substr($value, 0, 255);
Expand All @@ -332,15 +326,17 @@ public function getMauticCompany($data, $object = null)
$fieldsToUpdate = $this->getPriorityFieldsForMautic($config, $object, 'mautic_company');
$fieldsToUpdate = array_intersect_key($config['companyFields'], $fieldsToUpdate);
$matchedFields = array_intersect_key($matchedFields, array_flip($fieldsToUpdate));
if (!isset($matchedFields['companyname'])) {
if (isset($matchedFields['companywebsite'])) {
$matchedFields['companyname'] = $matchedFields['companywebsite'];
}
} else {
$matchedFields = $this->hydrateCompanyName($matchedFields);

// If we don't have an company name, don't create the company because it'll result in what looks like an "empty" company
if (empty($matchedFields['companyname'])) {
return null;
}
$companyModel->setFieldValues($company, $matchedFields, false, false);
}

$companyModel->saveEntity($company, false);
$this->companyModel->setFieldValues($company, $matchedFields, false);
$this->companyModel->saveEntity($company, false);

return $company;
}
Expand Down Expand Up @@ -617,4 +613,31 @@ protected function prepareFieldsForPush($fields)

return $fieldMappings;
}

/**
* @param array $matchedFields
*
* @return array
*/
private function hydrateCompanyName(array $matchedFields)
{
if (!empty($matchedFields['companyname'])) {
return $matchedFields;
}

if (!empty($matchedFields['companywebsite'])) {
$matchedFields['companyname'] = $matchedFields['companywebsite'];

return $matchedFields;
}

// We need something as company name so save whatever we have
if ($firstMatchedField = reset($matchedFields)) {
$matchedFields['companyname'] = $firstMatchedField;

return $matchedFields;
}

return $matchedFields;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace MauticPlugin\MauticCrmBundle\Integration;

use Mautic\LeadBundle\Entity\Company;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\PluginBundle\Entity\IntegrationEntity;
use Mautic\PluginBundle\Entity\IntegrationEntityRepository;
Expand Down
63 changes: 63 additions & 0 deletions plugins/MauticCrmBundle/Tests/CrmAbstractIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

namespace MauticPlugin\MauticCrmBundle\Tests;

use Mautic\EmailBundle\Helper\EmailValidator;
use Mautic\LeadBundle\Model\CompanyModel;
use Mautic\LeadBundle\Model\FieldModel;
use MauticPlugin\MauticCrmBundle\Tests\Stubs\StubIntegration;
use Symfony\Component\HttpFoundation\Session\Session;

class CrmAbstractIntegrationTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -56,4 +60,63 @@ public function testFieldMatchingPriority()
'Fields to update in the integration should return fields marked as 0 in the integration priority config.'
);
}

public function testCompanyDataIsMappedForNewCompanies()
{
$integration = $this->getMockBuilder(StubIntegration::class)
->disableOriginalConstructor()
->setMethodsExcept(['getMauticCompany', 'setCompanyModel', 'setFieldModel', 'hydrateCompanyName'])
->getMock();

$data = [
'custom_company_name' => 'Some Business',
'some_custom_field' => 'some value',
];

$integration->expects($this->once())
->method('populateMauticLeadData')
->willReturn($data);

$fieldModel = $this->getMockBuilder(FieldModel::class)
->disableOriginalConstructor()
->getMock();
$session = $this->getMockBuilder(Session::class)
->disableOriginalConstructor()
->getMock();
$emailValidator = $this->getMockBuilder(EmailValidator::class)
->disableOriginalConstructor()
->getMock();

$companyModel = $this->getMockBuilder(CompanyModel::class)
->setMethodsExcept(['setFieldValues'])
->setConstructorArgs([$fieldModel, $session, $emailValidator])
->getMock();
$companyModel->expects($this->once())
->method('organizeFieldsByGroup')
->willReturn([
'core' => [
'companyname' => [
'alias' => 'companyname',
'type' => 'text',
],
'custom_company_name' => [
'alias' => 'custom_company_name',
'type' => 'text',
],
'some_custom_field' => [
'alias' => 'some_custom_field',
'type' => 'text',
],
],
]);
$integration->setCompanyModel($companyModel);

$integration->setFieldModel($fieldModel);

$company = $integration->getMauticCompany($data);

$this->assertEquals('Some Business', $company->getName());
$this->assertEquals('Some Business', $company->getFieldValue('custom_company_name'));
$this->assertEquals('some value', $company->getFieldValue('some_custom_field'));
}
}

0 comments on commit aea0732

Please sign in to comment.