View source
<?php
namespace Drupal\Tests\simplenews\Functional;
use Drupal\Component\Utility\Html;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
class SimplenewsPersonalizationFormsTest extends SimplenewsTestBase {
protected $admin;
protected function setUp() {
parent::setUp();
$this
->addField('string', 'field_shared', 'user');
$this
->addField('string', 'field_shared', 'simplenews_subscriber');
Role::load('anonymous')
->grantPermission('subscribe to newsletters')
->grantPermission('access user profiles')
->save();
Role::load('authenticated')
->grantPermission('subscribe to newsletters')
->save();
$this->admin = $this
->drupalCreateUser([
'administer users',
]);
}
public function testSynchronizeSubscribeRegister() {
$email = $this
->randomEmail();
$this
->subscribe('default', $email, [
'field_shared[0][value]' => $this
->randomString(10),
]);
$new_value = $this
->randomString(20);
$uid = $this
->registerUser($email, [
'field_shared[0][value]' => $new_value,
]);
$this
->drupalGet("user/{$uid}");
$this
->assertText(Html::escape($new_value));
$subscriber = $this
->getLatestSubscriber();
$this
->assertEqual($subscriber->subscriptions
->get(0)->status, SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED);
}
public function testSynchronizeRegisterSubscribe() {
$email = $this
->randomEmail();
$uid = $this
->registerUser($email, [
'field_shared[0][value]' => $this
->randomString(10),
]);
$user = User::load($uid);
$this
->subscribe('default', $email);
$this
->assertRaw(t('There is an account registered for the e-mail address %mail. Please log in to manage your newsletter subscriptions', [
'%mail' => $email,
]));
$this
->resetPassLogin($user);
$new_value = $this
->randomString(20);
$this
->subscribe('default', NULL, [
'field_shared[0][value]' => $new_value,
], t('Update'));
$this
->drupalGet("user/{$uid}");
$this
->assertText(Html::escape($new_value));
}
public function testSubscribeRequestPassword() {
$email = $this
->randomEmail();
$this
->subscribe('default', $email);
$this
->drupalPostForm('user/password', [
'name' => $email,
], t('Submit'));
$this
->assertRaw(t('%name is not recognized as a username or an email address.', [
'%name' => $email,
]));
}
public function testDisableAccount() {
$email = $this
->randomEmail();
$uid = $this
->registerUser($email);
$this
->resetPassLogin(User::load($uid));
$this
->subscribe('default', NULL, [], t('Update'));
$this
->drupalLogout();
$this
->drupalLogin($this->admin);
$this
->drupalPostForm("user/{$uid}/cancel", [], t('Cancel account'));
$subscriber = $this
->getLatestSubscriber();
$this
->assertFalse($subscriber
->getStatus());
}
public function testDeleteAccount() {
$email = $this
->randomEmail();
$uid = $this
->registerUser($email);
$this
->subscribe('default', $email);
$this
->drupalLogin($this->admin);
$this
->drupalPostForm("user/{$uid}/cancel", [
'user_cancel_method' => 'user_cancel_reassign',
], t('Cancel account'));
$subscriber = $this
->getLatestSubscriber();
$this
->assertNull($subscriber, 'No subscriber found');
}
public function testBlockedSubscribe() {
$email = $this
->randomEmail();
$uid = $this
->registerUser($email);
$this
->drupalLogin($this->admin);
$this
->drupalPostForm("user/{$uid}/edit", [
'status' => 0,
], t('Save'));
$this
->drupalLogout();
$this
->subscribe('default', $email);
$this
->assertRaw(t('The email address %mail belongs to a blocked user.', [
'%mail' => $email,
]));
}
}