You are here

public function SimplenewsSynchronizeFieldsTest::testSynchronizeBaseFields in Simplenews 3.x

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/SimplenewsSynchronizeFieldsTest.php \Drupal\Tests\simplenews\Kernel\SimplenewsSynchronizeFieldsTest::testSynchronizeBaseFields()
  2. 8 tests/src/Kernel/SimplenewsSynchronizeFieldsTest.php \Drupal\Tests\simplenews\Kernel\SimplenewsSynchronizeFieldsTest::testSynchronizeBaseFields()

Tests that creating/updating User updates existing Subscriber base fields.

File

tests/src/Kernel/SimplenewsSynchronizeFieldsTest.php, line 43

Class

SimplenewsSynchronizeFieldsTest
Tests that fields shared by user account and subscribers are synchronized.

Namespace

Drupal\Tests\simplenews\Kernel

Code

public function testSynchronizeBaseFields() {

  // Create subscriber.

  /** @var \Drupal\simplenews\Entity\Subscriber $subscriber */
  $subscriber = Subscriber::create([
    'mail' => 'user@example.com',
  ]);
  $subscriber
    ->save();

  // Create user with same email.

  /** @var \Drupal\user\Entity\User $user */
  $user = User::create([
    'name' => 'user',
    'mail' => 'user@example.com',
    'preferred_langcode' => 'fr',
  ]);
  $user
    ->save();

  // Assert that subscriber's fields are updated.
  $subscriber = Subscriber::load($subscriber
    ->id());
  $this
    ->assertEqual($subscriber
    ->getUserId(), $user
    ->id());
  $this
    ->assertEqual($subscriber
    ->getLangcode(), 'fr');
  $this
    ->assertFalse($subscriber
    ->getStatus());

  // Update user fields.
  $user
    ->setEmail('user2@example.com');
  $user
    ->set('preferred_langcode', 'en');
  $user
    ->activate();
  $user
    ->save();

  // Assert that subscriber's fields are updated again.
  $subscriber = Subscriber::load($subscriber
    ->id());
  $this
    ->assertEqual($subscriber
    ->getMail(), 'user2@example.com');
  $this
    ->assertEqual($subscriber
    ->getLangcode(), 'en');
  $this
    ->assertTrue($subscriber
    ->getStatus());

  // Status is still synced even if sync_fields is not set.
  $this
    ->config('simplenews.settings')
    ->set('subscriber.sync_fields', FALSE)
    ->save();
  $user
    ->block();
  $user
    ->save();
  $subscriber = Subscriber::load($subscriber
    ->id());
  $this
    ->assertFalse($subscriber
    ->getStatus());
}