View source
<?php
namespace Drupal\Tests\message_subscribe_email\Functional;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\BrowserTestBase;
class ViewsTest extends BrowserTestBase {
use ContentTypeCreationTrait;
use NodeCreationTrait;
public static $modules = [
'message_subscribe_email',
'node',
'system',
];
protected $flagService;
protected $messageSubscribers;
public function setUp() {
parent::setUp();
$this->flagService = $this->container
->get('flag');
$this->messageSubscribers = $this->container
->get('message_subscribe.subscribers');
foreach ([
'node',
'og',
'term',
'user',
] as $entity_type) {
foreach ([
'email',
'subscribe',
] as $flag_type) {
if ($flag = $this->flagService
->getFlagById($flag_type . '_' . $entity_type)) {
$flag
->enable();
$flag
->save();
}
}
}
}
public function testViews() {
foreach ($this->messageSubscribers
->getFlags() as $flag_name => $flag) {
$expected = $flag_name . '_email:default';
$this
->assertEquals($expected, $flag
->getThirdPartySetting('message_subscribe_ui', 'view_name'));
}
$permissions = [
'access content',
'access user profiles',
'flag subscribe_node',
'unflag subscribe_node',
'flag email_node',
'unflag email_node',
'flag subscribe_user',
'unflag subscribe_user',
'flag email_user',
'unflag email_user',
];
foreach (range(1, 3) as $i) {
$users[$i] = $this
->drupalCreateUser($permissions);
}
$permissions[] = 'administer message subscribe';
$admin = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($admin);
foreach ($users as $user) {
$this
->assertTrue((bool) $user->message_subscribe_email->value, 'User defaults to getting email subscriptions');
$this
->drupalGet('user/' . $user
->id() . '/message-subscribe');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('user/' . $user
->id() . '/message-subscribe/subscribe_node');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains(t('You are not subscribed to any items.'));
}
$this
->drupalLogin($users[2]);
$type = $this
->createContentType();
$node = $this
->createNode([
'type' => $type
->id(),
]);
$flag = $this->flagService
->getFlagById('subscribe_node');
$this->flagService
->flag($flag, $node, $users[2]);
$this
->drupalGet('user/' . $users[2]
->id() . '/message-subscribe/subscribe_node');
$this
->assertSession()
->pageTextContains($node
->label());
$this
->assertSession()
->pageTextContains(t("Don't send email"));
$flag = $this->flagService
->getFlagById('subscribe_user');
$this->flagService
->flag($flag, $users[1], $users[2]);
$this
->drupalGet('user/' . $users[2]
->id() . '/message-subscribe/subscribe_user');
$this
->assertSession()
->pageTextContains($users[1]
->label());
$this
->assertSession()
->pageTextContains(t("Don't send email"));
$this
->drupalLogin($users[3]);
$this
->drupalGet('user/' . $users[3]
->id() . '/message-subscribe');
$this
->assertSession()
->pageTextContains(t('You are not subscribed to any items.'));
$flag = $this->flagService
->getFlagById('subscribe_node');
$this->flagService
->flag($flag, $node, $users[3]);
$this
->drupalGet('user/' . $users[3]
->id() . '/message-subscribe');
$this
->assertSession()
->pageTextContains($node
->label());
}
}