View source
<?php
namespace Drupal\Tests\simplenews\Functional;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\simplenews\Entity\Newsletter;
use Drupal\simplenews\Entity\Subscriber;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\Traits\Core\CronRunTrait;
use Drupal\user\UserInterface;
abstract class SimplenewsTestBase extends BrowserTestBase {
use AssertMailTrait;
use CronRunTrait;
public static $modules = [
'simplenews',
'simplenews_test',
'block',
];
protected $defaultTheme = 'classy';
protected $config;
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('local_tasks_block');
$this
->drupalPlaceBlock('local_actions_block');
$this
->drupalPlaceBlock('page_title_block');
$this->config = $this
->config('simplenews.settings');
$site_config = $this
->config('system.site');
$site_config
->set('site_mail', 'simpletest@example.com');
$newsletter = Newsletter::load('default');
$newsletter->from_address = $site_config
->get('site_mail');
$newsletter
->save();
}
protected function randomEmail($number = 4, $prefix = 'simpletest_', $domain = 'example.com') {
$mail = mb_strtolower($this
->randomMachineName($number, $prefix) . '@' . $domain);
return $mail;
}
protected function getRandomNewsletter() {
if ($newsletters = array_keys(simplenews_newsletter_get_all())) {
return $newsletters[array_rand($newsletters)];
}
return 0;
}
protected function setupSubscriptionBlock(array $settings = []) {
$settings += [
'newsletters' => [],
'message' => t('Select the newsletter(s) to which you want to subscribe or unsubscribe.'),
'unique_id' => \Drupal::service('uuid')
->generate(),
];
$settings['visibility']['request_path']['pages'] = "newsletter/confirm/*\nnewsletter/subscriptions";
$settings['visibility']['request_path']['negate'] = TRUE;
$settings['region'] = 'sidebar_first';
return $this
->drupalPlaceBlock('simplenews_subscription_block', $settings);
}
protected function setUpSubscribers($count = 100, $newsletter_id = 'default') {
$this->subscribers = [];
for ($i = 0; $i < $count; $i++) {
$mail = $this
->randomEmail();
$this->subscribers[$mail] = $mail;
}
$this
->drupalGet('admin/people/simplenews');
$this
->clickLink(t('Mass subscribe'));
$edit = [
'emails' => implode(',', $this->subscribers),
'newsletters[' . $newsletter_id . ']' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, t('Subscribe'));
}
protected function addField($type, $field_name, $entity_type, $bundle = NULL) {
if (!isset($bundle)) {
$bundle = $entity_type;
}
FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => $entity_type,
'type' => $type,
])
->save();
FieldConfig::create([
'field_name' => $field_name,
'entity_type' => $entity_type,
'bundle' => $bundle,
])
->save();
\Drupal::service('entity_display.repository')
->getFormDisplay($entity_type, $bundle)
->setComponent($field_name, [
'type' => 'string_textfield',
])
->save();
\Drupal::service('entity_display.repository')
->getViewDisplay($entity_type, $bundle)
->setComponent($field_name, [
'type' => 'string',
])
->save();
}
protected function subscribe($newsletter_ids, $email = NULL, array $edit = [], $submit = NULL, $path = 'newsletter/subscriptions', $response = 200) {
if (isset($email)) {
$edit += [
'mail[0][value]' => $email,
];
}
if (!is_array($newsletter_ids)) {
$newsletter_ids = [
$newsletter_ids,
];
}
foreach ($newsletter_ids as $newsletter_id) {
$edit["subscriptions[{$newsletter_id}]"] = $newsletter_id;
}
$this
->drupalPostForm($path, $edit, $submit ?: t('Subscribe'));
$this
->assertResponse($response);
}
protected function registerUser($email = NULL, array $edit = []) {
$edit += [
'mail' => $email ?: $this
->randomEmail(),
'name' => $this
->randomMachineName(),
];
$this
->drupalPostForm('user/register', $edit, t('Create new account'));
$uids = \Drupal::entityQuery('user')
->sort('created', 'DESC')
->range(0, 1)
->execute();
return array_shift($uids);
}
protected function resetPassLogin(UserInterface $user) {
$uid = $user
->id();
$timestamp = REQUEST_TIME;
$hash = user_pass_rehash($user, $timestamp);
$this
->drupalPostForm("/user/reset/{$uid}/{$timestamp}/{$hash}", [], t('Log in'));
}
protected function getLatestSubscriber() {
$snids = \Drupal::entityQuery('simplenews_subscriber')
->sort('created', 'DESC')
->range(0, 1)
->execute();
return empty($snids) ? NULL : Subscriber::load(array_shift($snids));
}
protected function getMail($offset) {
$mails = $this
->getMails();
$this
->assertTrue(isset($mails[$offset]), t('Valid mails offset %offset (%count mails sent).', [
'%offset' => $offset,
'%count' => count($mails),
]));
return $mails[$offset]['body'];
}
protected function assertMailText($needle, $offset, $exist = TRUE) {
$body = preg_replace('/\\s+/', ' ', $this
->getMail($offset));
$this
->verbose($body);
$pos = strpos($body, (string) $needle);
$this
->assertEquals($pos !== FALSE, $exist, "{$needle} found in mail");
}
}