diff --git a/crm_core/contact/src/Tests/ProcessContactTest.php b/crm_core/contact/src/Tests/ProcessContactTest.php index cbdb991..e9c1395 100644 --- a/crm_core/contact/src/Tests/ProcessContactTest.php +++ b/crm_core/contact/src/Tests/ProcessContactTest.php @@ -7,12 +7,10 @@ namespace Drupal\collect_crm_core_contact\Tests; use Drupal\collect\Entity\Container; -use Drupal\Core\Language\LanguageInterface; use Drupal\entity_test\Entity\EntityTest; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\simpletest\WebTestBase; -use Drupal\user\Entity\User; /** * Tests a processing workflow for creating CRM Contacts from a container. @@ -28,7 +26,6 @@ class ProcessContactTest extends WebTestBase { */ public static $modules = [ 'collect_crm_core_contact', - 'crm_core_default_matching_engine', 'crm_core_contact_ui', 'entity_test', // @todo Remove node dependency after https://www.drupal.org/node/2308745 @@ -47,6 +44,12 @@ class ProcessContactTest extends WebTestBase { 'type' => 'email_mailto', ]) ->save(); + + // Add fields to the test entity type: one for some kind of originator, one + // for some kind of recipient. One is a name and one is an email, to test + // different aspects of matching. + $this->createField('entity_test', 'gift', 'donor', 'email', 'Donor email'); + $this->createField('entity_test', 'gift', 'recipient', 'string', 'Recipient name'); } /** @@ -58,25 +61,12 @@ class ProcessContactTest extends WebTestBase { * triggered, and the resulting Contact is asserted. */ public function testContactProcessing() { - // Add an email field to the entity type. - FieldStorageConfig::create([ - 'field_name' => 'email', - 'type' => 'email', - 'entity_type' => 'entity_test', - ])->save(); - FieldConfig::create([ - 'field_name' => 'email', - 'field_type' => 'email', - 'entity_type' => 'entity_test', - 'bundle' => 'artist', - 'label' => 'Email address', - ])->save(); - // Create an entity. $entity = EntityTest::create([ - 'type' => 'artist', - 'name' => 'John Lennon', - 'email' => 'jlennon@example.com', + 'type' => 'gift', + 'name' => 'Cookie', + 'donor' => 'jlennon@example.com', + 'recipient' => 'Yoko', ]); $entity->save(); @@ -84,57 +74,80 @@ class ProcessContactTest extends WebTestBase { $admin_user = $this->drupalCreateUser(['administer collect', 'view any crm_core_contact entity', 'view test entity', 'administer default matching engine']); $this->drupalLogin($admin_user); + // Enable CRM matching rule. + $this->drupalPostForm('admin/config/crm-core/match/default/edit/individual', [ + 'status' => TRUE, + 'rules[name:value][status]' => TRUE, + 'rules[name:value][operator]' => 'CONTAINS', + 'rules[contact_mail:value][status]' => TRUE, + 'rules[contact_mail:value][operator]' => '=', + ], t('Save')); + // Capture the created entity. $this->drupalPostForm('admin/content/collect/capture', ['entity_type' => 'entity_test'], t('Select entity type')); - $this->drupalPostForm(NULL, ['operation' => 'single', 'entity' => 'John Lennon (' . $entity->id() . ')'], t('Capture')); + $this->drupalPostForm(NULL, ['operation' => 'single', 'entity' => 'Cookie (' . $entity->id() . ')'], t('Capture')); $this->assertText('The Test entity entity has been captured'); // Create suggested schema. $this->clickLink('Collect JSON schema'); $this->drupalPostForm(NULL, ['label' => 'User entity', 'id' => 'user_entity'], t('Save')); - // Edit schema workflow. - $this->drupalGet('admin/structure/collect-schemas'); - $this->clickLink('Manage processing'); - $this->assertUrl('admin/structure/collect-schemas/user_entity/processing'); - // Add processors out of order to challenge weight handling. + // Edit schema processing workflow. + $this->drupalGet('admin/structure/collect-schemas/user_entity/processing'); + $this->drupalPostForm(NULL, ['processor_add_select' => 'contact_matcher'], t('Add')); $this->drupalPostForm(NULL, ['processor_add_select' => 'contact_matcher'], t('Add')); $this->assertText('Matches or creates a CRM Core Contact entity.'); - $this->drupalPostForm(NULL, ['processor_add_select' => 'identify_contacts'], t('Add')); - $this->assertText('Identifies a contact involved in container data.'); + // Form submission is divided because field list is populated after + // selecting contact_type. $this->drupalPostForm(NULL, [ + 'processors[0][settings][relation]' => 'from', 'processors[0][settings][contact_type]' => 'individual', - 'processors[1][settings][mail_property]' => 'email', - 'processors[1][settings][name_property]' => 'name', - // Set correct order. - 'processors[0][weight]' => 1, - 'processors[1][weight]' => 0, + 'processors[1][settings][relation]' => 'to', + 'processors[1][settings][contact_type]' => 'individual', ], t('Save')); - - // Enable CRM matching rule. - $this->drupalPostForm('admin/config/crm-core/match/default/edit/individual', [ - 'status' => TRUE, - 'rules[name:value][status]' => TRUE, - 'rules[name:value][operator]' => 'CONTAINS', - 'rules[contact_mail:value][status]' => TRUE, - 'rules[contact_mail:value][operator]' => '=', + $this->drupalPostForm(NULL, [ + // The donor is identified by email. + 'processors[0][settings][fields][contact_mail][schema_property]' => 'donor', + // The recipient is identified by name. + 'processors[1][settings][fields][name][schema_property]' => 'recipient', ], t('Save')); // Execute processing on the entity container. $containers = Container::loadMultiple(); - \Drupal::service('collect.postprocessor')->process(current($containers)); + $user_container = end($containers); + \Drupal::service('collect.postprocessor')->process($user_container); // Assert new CRM Contact was created. $this->drupalGet('crm-core/contact'); - $this->assertText('Individual'); - $this->clickLink('John Lennon'); + // Find recipient's name. + $this->assertLink('Yoko'); + // Click the nameless donor and find its email address. + $this->clickLink('(no name)'); $this->assertText('jlennon@example.com'); - // The next processing should match the existing contact, and not create a - // new one. - \Drupal::service('collect.postprocessor')->process(current($containers)); + // The next processing should match the existing contacts, and not create + // new ones. + \Drupal::service('collect.postprocessor')->process($user_container); $this->drupalGet('crm-core/contact'); - $this->assertEqual(1, count($this->xpath('//tbody/tr'))); + $this->assertEqual(2, count($this->xpath('//tbody/tr'))); + } + + /** + * Creates a field on a given entity type. + */ + protected function createField($entity_type, $bundle, $name, $type, $label) { + FieldStorageConfig::create([ + 'field_name' => $name, + 'type' => $type, + 'entity_type' => $entity_type, + ])->save(); + FieldConfig::create([ + 'field_name' => $name, + 'field_type' => $type, + 'entity_type' => $entity_type, + 'bundle' => $bundle, + 'label' => $label, + ])->save(); } }