You are here

public function FieldMatcherTest::testEmail in CRM Core 8.2

Same name and namespace in other branches
  1. 8.3 modules/crm_core_match/tests/src/Kernel/FieldMatcherTest.php \Drupal\Tests\crm_core_match\Kernel\FieldMatcherTest::testEmail()
  2. 8 modules/crm_core_match/tests/src/Kernel/FieldMatcherTest.php \Drupal\Tests\crm_core_match\Kernel\FieldMatcherTest::testEmail()

Test the email field.

File

modules/crm_core_match/tests/src/Kernel/FieldMatcherTest.php, line 192

Class

FieldMatcherTest
Tests the field matchers of the default matching engine.

Namespace

Drupal\Tests\crm_core_match\Kernel

Code

public function testEmail() {
  FieldStorageConfig::create(array(
    'entity_type' => 'crm_core_individual',
    'type' => 'email',
    'field_name' => 'individual_mail',
  ))
    ->save();
  FieldConfig::create(array(
    'field_name' => 'individual_mail',
    'entity_type' => 'crm_core_individual',
    'bundle' => 'customer',
    'label' => t('Email'),
    'required' => FALSE,
  ))
    ->save();
  $config = array(
    'value' => array(
      'operator' => '=',
      'score' => 42,
    ),
  );

  /** @var Individual $individual_needle */
  $individual_needle = Individual::create([
    'type' => 'customer',
  ]);
  $individual_needle
    ->set('individual_mail', 'boomer@example.com');
  $individual_needle
    ->save();

  /** @var Individual $individual_match */
  $individual_match = Individual::create([
    'type' => 'customer',
  ]);
  $individual_match
    ->set('individual_mail', 'boomer@example.com');
  $individual_match
    ->save();
  $config['field'] = $individual_needle
    ->getFieldDefinition('individual_mail');

  /* @var \Drupal\crm_core_match\Plugin\crm_core_match\field\FieldHandlerInterface $text */
  $text = $this->pluginManager
    ->createInstance('email', $config);
  $ids = $text
    ->match($individual_needle);
  $this
    ->assertTrue(array_key_exists($individual_match
    ->id(), $ids), 'Text match returns expected match');
  $this
    ->assertEqual(42, $ids[$individual_match
    ->id()]['individual_mail.value'], 'Got expected match score');
}