You are here

public function SimplenewsSynchronizeFieldsTest::testSynchronizeConfigurableFields in Simplenews 8.2

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

Tests that shared fields are synchronized.

File

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

Class

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

Namespace

Drupal\Tests\simplenews\Kernel

Code

public function testSynchronizeConfigurableFields() {

  // Create and attach a field to both.
  $this
    ->addField('string', 'field_on_both', 'simplenews_subscriber');
  $this
    ->addField('string', 'field_on_both', 'user');

  // Create a user and a subscriber.

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

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

  // Update the fields on the subscriber.
  $subscriber = Subscriber::load($subscriber
    ->id());
  $subscriber
    ->set('field_on_both', 'bar');
  $subscriber
    ->set('created', 3000);
  $subscriber
    ->save();

  // Assert that (only) the shared field is also updated on the user.
  $user = User::load($user
    ->id());
  $this
    ->assertEqual($user
    ->get('field_on_both')->value, 'bar');
  $this
    ->assertEqual($user
    ->get('created')->value, 1000);

  // Update the fields on the user.
  $user
    ->set('field_on_both', 'baz');
  $user
    ->set('created', 4000);
  $user
    ->save();

  // Assert that (only) the shared field is also updated on the subscriber.
  $subscriber = Subscriber::load($subscriber
    ->id());
  $this
    ->assertEqual($subscriber
    ->get('field_on_both')->value, 'baz');
  $this
    ->assertEqual($subscriber
    ->get('created')->value, 3000);
}