You are here

public function SimplenewsSynchronizeFieldsTest::testSetSharedFieldAutomatically in Simplenews 3.x

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

Tests that new entities copy values from corresponding user/subscriber.

File

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

Class

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

Namespace

Drupal\Tests\simplenews\Kernel

Code

public function testSetSharedFieldAutomatically() {

  // 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 with values for the fields.

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

  // Create a subscriber.

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

  // Assert that the shared field already has a value.
  $this
    ->assertEqual($subscriber
    ->get('field_on_both')->value, $user
    ->get('field_on_both')->value);

  // Create a subscriber with values for the fields.
  $subscriber = Subscriber::create([
    'field_on_both' => 'bar',
    'mail' => 'user@example.com',
  ]);
  $subscriber
    ->save();

  // Create a user.
  $user = User::create([
    'name' => 'user',
    'mail' => 'user@example.com',
  ]);

  // Assert that the shared field already has a value.
  $this
    ->assertEqual($user
    ->get('field_on_both')->value, $subscriber
    ->get('field_on_both')->value);
}