You are here

public function FieldMatcherTest::testText in CRM Core 8

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::testText()
  2. 8.2 modules/crm_core_match/tests/src/Kernel/FieldMatcherTest.php \Drupal\Tests\crm_core_match\Kernel\FieldMatcherTest::testText()

Test the text field.

File

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

Class

FieldMatcherTest
Tests the field matchers of the default matching engine.

Namespace

Drupal\Tests\crm_core_match\Kernel

Code

public function testText() {
  FieldStorageConfig::create([
    'entity_type' => 'crm_core_individual',
    'type' => 'string',
    'field_name' => 'individual_text',
  ])
    ->save();
  FieldConfig::create([
    'field_name' => 'individual_text',
    'entity_type' => 'crm_core_individual',
    'bundle' => 'customer',
    'label' => t('Text'),
    'required' => FALSE,
  ])
    ->save();
  $config = [
    'value' => [
      'operator' => '=',
      'score' => 42,
    ],
  ];

  /** @var \Drupal\crm_core_contact\Entity\Individual $individual_needle */
  $individual_needle = Individual::create([
    'type' => 'customer',
  ]);
  $individual_needle
    ->set('individual_text', 'Boomer');
  $individual_needle
    ->save();

  /** @var \Drupal\crm_core_contact\Entity\Individual $individual_match */
  $individual_match = Individual::create([
    'type' => 'customer',
  ]);
  $individual_match
    ->set('individual_text', 'Boomer');
  $individual_match
    ->save();
  $config['field'] = $individual_needle
    ->getFieldDefinition('individual_text');

  /* @var \Drupal\crm_core_match\Plugin\crm_core_match\field\FieldHandlerInterface $text */
  $text = $this->pluginManager
    ->createInstance('text', $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_text.value'], 'Got expected match score');
}