View source
<?php
namespace Drupal\simplenews\Tests;
use Drupal\Component\Utility\Html;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
class SimplenewsPersonalizationFormsTest extends SimplenewsTestBase {
protected $admin;
public 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(array(
'administer users',
));
}
public function testSynchronizeSubscribeRegister() {
$email = $this
->randomEmail();
$this
->subscribe('default', $email, array(
'field_shared[0][value]' => $this
->randomString(10),
));
$new_value = $this
->randomString(20);
$uid = $this
->registerUser($email, array(
'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, array(
'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', array(
'%mail' => $email,
)));
$this
->resetPassLogin($user);
$new_value = $this
->randomString(20);
$this
->subscribe('default', NULL, array(
'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', array(
'name' => $email,
), t('Submit'));
$this
->assertRaw(t('%name is not recognized as a username or an email address.', array(
'%name' => $email,
)));
}
public function testDisableAccount() {
$email = $this
->randomEmail();
$uid = $this
->registerUser($email);
$this
->resetPassLogin(User::load($uid));
$this
->subscribe('default', NULL, array(), t('Update'));
$this
->drupalLogout();
$this
->drupalLogin($this->admin);
$this
->drupalPostForm("user/{$uid}/cancel", array(), 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", array(
'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", array(
'status' => 0,
), t('Save'));
$this
->drupalLogout();
$this
->subscribe('default', $email);
$this
->assertRaw(t('The email address %mail belongs to a blocked user.', array(
'%mail' => $email,
)));
}
}