You are here

public function SimplenewsSynchronizeFieldsTest::testSetSharedFieldAutomatically in Simplenews 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/SimplenewsSynchronizeFieldsTest.php \Drupal\Tests\simplenews\Kernel\SimplenewsSynchronizeFieldsTest::testSetSharedFieldAutomatically()
  2. 3.x 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(array(
    'name' => 'user',
    'field_on_both' => 'foo',
    'mail' => 'user@example.com',
  ));
  $user
    ->save();

  // Create a subscriber.

  /** @var \Drupal\simplenews\Entity\Subscriber $subscriber */
  $subscriber = Subscriber::create(array(
    '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(array(
    'field_on_both' => 'bar',
    'mail' => 'user@example.com',
  ));
  $subscriber
    ->save();

  // Create a user.
  $user = User::create(array(
    '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);
}