View source
<?php
namespace Drupal\Tests\workbench_email\Functional;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;
use Drupal\node\NodeTypeInterface;
use Drupal\Tests\block\Traits\BlockCreationTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\workbench_email\Entity\Template;
abstract class WorkbenchEmailTestBase extends BrowserTestBase {
use AssertMailTrait;
use NodeCreationTrait;
use BlockCreationTrait;
protected $defaultTheme = 'stark';
protected $nodeType;
protected $approverRole;
protected $editorRole;
protected $approver1;
protected $approver2;
protected $approver3;
protected $approver4;
protected $editor;
protected $admin;
public static $modules = [
'workbench_email',
'node',
'options',
'user',
'system',
'filter',
'block',
'field',
];
public function setUp() {
parent::setUp();
$this
->placeBlock('local_tasks_block', [
'id' => 'tabs_block',
]);
$this
->placeBlock('page_title_block');
$this
->placeBlock('local_actions_block', [
'id' => 'actions_block',
]);
$this->nodeType = NodeType::create([
'type' => 'test',
'name' => 'Test',
]);
$this
->setupModerationForNodeType($this->nodeType);
$this->nodeType = NodeType::create([
'type' => 'another',
'name' => 'Another Test',
]);
$this
->setupModerationForNodeType($this->nodeType);
$this->approverRole = $this
->drupalCreateRole($this
->getApproverPermissions(), 'approver', 'Approver');
$this->approver1 = $this
->drupalCreateUser();
$this->approver1
->addRole('approver');
$this->approver1
->save();
$this->approver2 = $this
->drupalCreateUser();
$this->approver2
->addRole('approver');
$this->approver2
->save();
$this->approver3 = $this
->drupalCreateUser();
$this->approver3
->addRole('approver');
$this->approver3
->block();
$this->approver3
->save();
$this->approver4 = $this
->drupalCreateUser();
$this->approver4
->addRole('approver');
$this->approver4
->setEmail(NULL);
$this->approver4
->save();
$this->editorRole = $this
->drupalCreateRole($this
->getEditorPermissions(), 'editor', 'Editor');
$this->editor = $this
->drupalCreateUser();
$this->editor
->addRole('editor');
$this->editor
->save();
$this->admin = $this
->drupalCreateUser($this
->getAdminPermissions());
FieldStorageConfig::create([
'cardinality' => 1,
'entity_type' => 'node',
'field_name' => 'field_email',
'type' => 'email',
])
->save();
FieldConfig::create([
'field_name' => 'field_email',
'bundle' => 'test',
'label' => 'Notify',
'entity_type' => 'node',
])
->save();
if (!($entity_form_display = EntityFormDisplay::load('node.test.default'))) {
$entity_form_display = EntityFormDisplay::create([
'targetEntityType' => 'node',
'bundle' => 'test',
'mode' => 'default',
'status' => TRUE,
]);
}
$entity_form_display
->setComponent('field_email', [
'type' => 'email_default',
])
->save();
}
protected abstract function setupModerationForNodeType(NodeTypeInterface $nodeType);
protected abstract function getApproverPermissions();
protected abstract function getEditorPermissions();
protected abstract function getAdminPermissions();
protected abstract function enableTemplateForTransitionOrWorkflow($transition_name, $template_name);
protected abstract function getSubmitButtonTitleForTransition($from_state, $to_state);
protected abstract function getAdditionalFieldsForTransition($from_state, $to_state);
public function testEndToEnd() {
$this
->drupalLogin($this->admin);
$this
->visitAdminMenuParent();
$page = $this
->getSession()
->getPage();
$page
->clickLink('Email Templates');
$assert = $this
->assertSession();
$this
->assertEquals($this
->getSession()
->getCurrentUrl(), Url::fromUri('internal:/admin/structure/workbench-moderation/workbench-email-template')
->setOption('absolute', TRUE)
->toString());
$assert
->pageTextContains('Email Template');
$page
->clickLink('Add Email Template');
$this
->submitForm([
'id' => 'approved',
'label' => 'Content approved',
'body[value]' => 'Content with title [node:title] was approved. You can view it at [node:url].',
'subject' => 'Content approved: [node:title]',
'enabled_recipient_types[author]' => TRUE,
'enabled_recipient_types[email]' => TRUE,
'enabled_recipient_types[role]' => TRUE,
'recipient_types[email][settings][fields][node:field_email]' => TRUE,
'recipient_types[role][settings][roles][editor]' => TRUE,
], t('Save'));
$assert
->pageTextContains('Created the Content approved Email Template');
$page
->clickLink('Add Email Template');
$this
->submitForm([
'id' => 'needs_review',
'label' => 'Content needs review',
'body[value]' => 'Content with title [node:title] needs review. You can view it at [node:url].',
'subject' => 'Content needs review',
'replyTo' => '[node:author:mail]',
'enabled_recipient_types[role]' => TRUE,
'recipient_types[role][settings][roles][approver]' => TRUE,
'bundles[node:test]' => TRUE,
], t('Save'));
$assert
->pageTextContains('Created the Content needs review Email Template');
$approver = Template::load('needs_review');
$dependencies = $approver
->calculateDependencies()
->getDependencies()['config'];
$this
->assertTrue(in_array('user.role.approver', $dependencies, TRUE));
$this
->assertTrue(in_array('node.type.test', $dependencies, TRUE));
$approver = Template::load('approved');
$dependencies = $approver
->calculateDependencies()
->getDependencies()['config'];
$this
->assertTrue(in_array('field.storage.node.field_email', $dependencies, TRUE));
$page
->clickLink('Content approved');
$assert
->checkboxChecked('Notify (Content)');
$this
->getSession()
->back();
$page
->clickLink('Content needs review');
$assert
->checkboxChecked('Approver', $page
->find('css', '#edit-recipient-types-role-settings-roles--wrapper'));
$this
->submitForm([
'label' => 'Content needs review',
'body[value]' => 'Content with title [node:title] needs review. You can view it at [node:url].',
'subject' => 'Content needs review: [node:title]',
'replyTo' => '[node:author:mail]',
], t('Save'));
$assert
->pageTextContains('Saved the Content needs review Email Template');
$transitionOrWorkflow = $this
->enableTemplateForTransitionOrWorkflow('needs_review_published', 'approved');
$this
->assertEquals($this
->getExpectedThirdPartySetting('needs_review_published', 'approved'), $transitionOrWorkflow
->getThirdPartySetting('workbench_email', 'workbench_email_templates'));
$dependencies = $transitionOrWorkflow
->calculateDependencies()
->getDependencies()['config'];
$this
->assertTrue(in_array('workbench_email.workbench_email_template.approved', $dependencies, TRUE));
$this
->enableTemplateForTransitionOrWorkflow('draft_needs_review', 'needs_review');
$this
->drupalLogin($this->editor);
$this
->drupalGet('node/add/test');
$this
->submitForm([
'title[0][value]' => 'Test node',
'field_email[0][value]' => 'foo@example.com',
] + $this
->getAdditionalFieldsForTransition('draft', 'draft'), $this
->getSubmitButtonTitleForTransition('draft', 'draft'));
$node = $this
->getNodeByTitle('Test node');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this->container
->get('state')
->set('system.test_mail_collector', []);
$this
->submitForm([] + $this
->getAdditionalFieldsForTransition('draft', 'needs_review'), $this
->getSubmitButtonTitleForTransition('draft', 'needs_review'));
$this
->assertNeedsReviewNotifications($node);
$this->container
->get('state')
->set('system.test_mail_collector', []);
$this
->drupalGet('node/add/test');
$this
->submitForm([
'title[0][value]' => 'Test node 2',
] + $this
->getAdditionalFieldsForTransition('draft', 'needs_review'), $this
->getSubmitButtonTitleForTransition('draft', 'needs_review'));
$node2 = $this
->getNodeByTitle('Test node 2');
$this
->assertNeedsReviewNotifications($node2);
$this->container
->get('state')
->set('system.test_mail_collector', []);
$this
->drupalLogin($this->approver1);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm([] + $this
->getAdditionalFieldsForTransition('needs_review', 'published'), $this
->getSubmitButtonTitleForTransition('needs_review', 'published'));
$captured_emails = $this->container
->get('state')
->get('system.test_mail_collector') ?: [];
$last = end($captured_emails);
$prev = prev($captured_emails);
$mails = [
$last['to'],
$prev['to'],
];
sort($mails);
$expected = [
$this->editor
->getEmail(),
'foo@example.com',
];
sort($expected);
$this
->assertEquals($expected, $mails);
$this
->assertEquals(sprintf('Content approved: %s', $node
->getTitle()), $last['subject']);
$this
->assertEquals(sprintf('Content approved: %s', $node
->getTitle()), $prev['subject']);
$this
->assertStringContainsString(sprintf('Content with title %s was approved. You can view it at', $node
->label()), preg_replace('/\\s+/', ' ', $prev['body']));
$this
->assertStringContainsString(sprintf('Content with title %s was approved. You can view it at', $node
->label()), preg_replace('/\\s+/', ' ', $last['body']));
$this
->assertStringContainsString($node
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString(), preg_replace('/\\s+/', ' ', $prev['body']));
$this
->assertStringContainsString($node
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString(), preg_replace('/\\s+/', ' ', $last['body']));
$this
->drupalLogin($this->editor);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm([] + $this
->getAdditionalFieldsForTransition('published', 'draft'), $this
->getSubmitButtonTitleForTransition('published', 'draft'));
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this->container
->get('state')
->set('system.test_mail_collector', []);
$this
->submitForm([] + $this
->getAdditionalFieldsForTransition('draft', 'needs_review'), $this
->getSubmitButtonTitleForTransition('draft', 'needs_review'));
$this
->assertNeedsReviewNotifications($node);
$this->container
->get('state')
->set('system.test_mail_collector', []);
$this
->drupalGet('node/add/another');
$this
->submitForm([
'title[0][value]' => 'Another test node',
] + $this
->getAdditionalFieldsForTransition('draft', 'draft'), $this
->getSubmitButtonTitleForTransition('draft', 'draft'));
$node = $this
->getNodeByTitle('Another test node');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm([] + $this
->getAdditionalFieldsForTransition('draft', 'needs_review'), $this
->getSubmitButtonTitleForTransition('draft', 'needs_review'));
$captured_emails = $this->container
->get('state')
->get('system.test_mail_collector') ?: [];
$this
->assertEmpty($captured_emails);
}
protected abstract function visitAdminMenuParent();
protected abstract function getExpectedThirdPartySetting($transition_name, $template);
protected function assertNeedsReviewNotifications(NodeInterface $node) {
$captured_emails = $this->container
->get('state')
->get('system.test_mail_collector') ?: [];
$this
->assertCount(2, $captured_emails);
$last = end($captured_emails);
$prev = prev($captured_emails);
$mails = [
$last['to'],
$prev['to'],
];
sort($mails);
$expected = [
$this->approver1
->getEmail(),
$this->approver2
->getEmail(),
];
sort($expected);
$this
->assertEquals($expected, $mails);
$this
->assertEquals(sprintf('Content needs review: %s', $node
->getTitle()), $last['subject']);
$this
->assertEquals(sprintf('Content needs review: %s', $node
->getTitle()), $prev['subject']);
$this
->assertEquals($this->editor
->getEmail(), $last['reply-to']);
$this
->assertEquals($this->editor
->getEmail(), $prev['reply-to']);
$this
->assertStringContainsString(sprintf('Content with title %s needs review. You can view it at', $node
->label()), preg_replace('/\\s+/', ' ', $prev['body']));
$this
->assertStringContainsString(sprintf('Content with title %s needs review. You can view it at', $node
->label()), preg_replace('/\\s+/', ' ', $last['body']));
$this
->assertStringContainsString($node
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString(), preg_replace('/\\s+/', ' ', $prev['body']));
$this
->assertStringContainsString($node
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString(), preg_replace('/\\s+/', ' ', $last['body']));
}
}