You are here

public function SyncMappingHelperTests::testSyncValidatorIsSynced in Lightweight Directory Access Protocol (LDAP) 8.3

Prove that field syncs work and provide the demo data here.

File

ldap_user/tests/src/Unit/SyncMappingHelperTests.php, line 49

Class

SyncMappingHelperTests
@coversDefaultClass \Drupal\ldap_user\Helper\SyncMappingHelper @group ldap

Namespace

Drupal\Tests\ldap_user\Unit

Code

public function testSyncValidatorIsSynced() {
  $syncTestData = [
    'drupal' => [
      '[field.ldap_user_puid_sid]' => [
        // Actually TranslatableMarkup.
        'name' => 'SID',
        'configurable_to_drupal' => 0,
        'configurable_to_ldap' => 1,
        'notes' => 'not configurable',
        'direction' => 'drupal',
        'enabled' => TRUE,
        'prov_events' => [
          self::EVENT_CREATE_DRUPAL_USER,
        ],
      ],
      '[property.name]' => [
        // Actually TranslatableMarkup.
        'name' => 'Name',
        'source' => '[cn]',
        'direction' => 'drupal',
        'enabled' => TRUE,
        'prov_events' => [
          self::EVENT_CREATE_DRUPAL_USER,
          self::EVENT_SYNC_TO_DRUPAL_USER,
        ],
      ],
    ],
    'ldap' => [
      '[property.name]' => [
        // Actually TranslatableMarkup.
        'name' => 'Name',
        'source' => '',
        'direction' => 'ldap',
        'enabled' => TRUE,
        'prov_events' => [
          self::EVENT_CREATE_LDAP_ENTRY,
          self::EVENT_SYNC_TO_LDAP_ENTRY,
        ],
        'configurable_to_ldap' => TRUE,
      ],
    ],
  ];
  $processor = $this
    ->getMockBuilder('Drupal\\ldap_user\\Helper\\SyncMappingHelper')
    ->setMethods([
    'processSyncMappings',
  ])
    ->disableOriginalConstructor()
    ->getMock();
  $reflection = new ReflectionClass(get_class($processor));
  $method = $reflection
    ->getMethod('setAllSyncMappings');
  $method
    ->setAccessible(TRUE);
  $method
    ->invoke($processor, $syncTestData);

  /** @var \Drupal\ldap_user\Helper\SyncMappingHelper $processor */
  $isSynced = $processor
    ->isSynced('[field.ldap_user_puid_sid]', [
    self::EVENT_CREATE_DRUPAL_USER,
  ], self::PROVISION_TO_DRUPAL);
  $this
    ->assertTrue($isSynced);
  $isSynced = $processor
    ->isSynced('[field.ldap_user_puid_sid]', [
    self::EVENT_CREATE_DRUPAL_USER,
  ], self::PROVISION_TO_LDAP);
  $this
    ->assertFalse($isSynced);
  $isSynced = $processor
    ->isSynced('[field.ldap_user_puid_sid]', [
    self::EVENT_CREATE_LDAP_ENTRY,
  ], self::PROVISION_TO_LDAP);
  $this
    ->assertFalse($isSynced);
  $isSynced = $processor
    ->isSynced('[field.ldap_user_puid_sid]', [
    self::EVENT_CREATE_LDAP_ENTRY,
  ], self::PROVISION_TO_LDAP);
  $this
    ->assertFalse($isSynced);
  $isSynced = $processor
    ->isSynced('[property.name]', [
    self::EVENT_CREATE_LDAP_ENTRY,
  ], self::PROVISION_TO_LDAP);
  $this
    ->assertTrue($isSynced);
  $isSynced = $processor
    ->isSynced('[property.xyz]', [
    self::EVENT_CREATE_DRUPAL_USER,
  ], self::PROVISION_TO_DRUPAL);
  $this
    ->assertFalse($isSynced);

  // TODO: Review behaviour. Should this actually be allowed that one of many
  // events returns true?
  $isSynced = $processor
    ->isSynced('[field.ldap_user_puid_sid]', [
    self::EVENT_CREATE_DRUPAL_USER,
    self::EVENT_SYNC_TO_DRUPAL_USER,
  ], self::PROVISION_TO_DRUPAL);
  $this
    ->assertTrue($isSynced);
}