SimplenewsSynchronizeFieldsFormTest.php in Simplenews 3.x
File
tests/src/Functional/SimplenewsSynchronizeFieldsFormTest.php
View source
<?php
namespace Drupal\Tests\simplenews\Functional;
use Drupal\simplenews\Entity\Subscriber;
use Drupal\user\Entity\User;
class SimplenewsSynchronizeFieldsFormTest extends SimplenewsTestBase {
public static $modules = [
'field',
'simplenews',
];
protected $user;
protected function setUp() {
parent::setUp();
$this
->addField('string', 'field_shared', 'user');
$this
->addField('string', 'field_shared', 'simplenews_subscriber');
$this->user = $this
->drupalCreateUser([
'administer simplenews subscriptions',
'administer simplenews settings',
]);
$this->user
->setEmail('user@example.com');
$this->user
->set('field_shared', $this
->randomMachineName());
$this->user
->save();
}
public function testSubscriberFormFieldSync() {
$subscriber = Subscriber::create([
'mail' => 'user@example.com',
]);
$subscriber
->save();
$this
->assertEquals($this->user
->id(), $subscriber
->getUserId());
$this
->drupalLogin($this->user);
$this
->drupalGet('admin/people/simplenews/edit/' . $subscriber
->id());
$this
->assertField('field_shared[0][value]');
$this
->assertRaw($this->user->field_shared->value);
$new_value = $this
->randomMachineName();
$this
->submitForm([
'field_shared[0][value]' => $new_value,
], 'Save');
$this
->drupalGet('admin/people/simplenews/edit/' . $subscriber
->id());
$this
->assertRaw($new_value);
$this->user = User::load($this->user
->id());
$this
->assertEqual($this->user->field_shared->value, $new_value);
$this
->drupalGet('admin/config/people/simplenews/settings/subscriber');
$this
->submitForm([
'simplenews_sync_fields' => FALSE,
], 'Save configuration');
$unsynced_value = $this
->randomMachineName();
$this
->drupalGet('admin/people/simplenews/edit/' . $subscriber
->id());
$this
->submitForm([
'field_shared[0][value]' => $unsynced_value,
], 'Save');
$this
->drupalGet('admin/people/simplenews/edit/' . $subscriber
->id());
$this
->assertRaw($unsynced_value);
$this->user = User::load($this->user
->id());
$this
->assertEqual($this->user->field_shared->value, $new_value);
$this
->assertNotEqual($this->user->field_shared->value, $unsynced_value);
}
}