You are here

public function WorkbenchEmailTestBase::testEndToEnd in Workbench Email 2.x

Same name and namespace in other branches
  1. 8 tests/src/Functional/WorkbenchEmailTestBase.php \Drupal\Tests\workbench_email\Functional\WorkbenchEmailTestBase::testEndToEnd()

Test administration.

File

tests/src/Functional/WorkbenchEmailTestBase.php, line 245

Class

WorkbenchEmailTestBase
Defines a base class for workbench email tests.

Namespace

Drupal\Tests\workbench_email\Functional

Code

public function testEndToEnd() {

  // Create some templates as admin.
  // - stuff got approved; and
  // - stuff needs review.
  $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 [node:field_does_not_exist]title [node:title] was approved. You can view it at [node:url].',
    'subject' => 'Content [node:field_does_not_exist]approved: [node:title][node:field_does_not_exist]',
    '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 [node:field_does_not_exist]title [node:title] needs review. You can view it at [node:url].[node:field_does_not_exist]',
    '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');

  // Test dependencies.
  $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));

  // Edit the template and test values persisted.
  $page
    ->clickLink('Content approved');
  $assert
    ->checkboxChecked('Notify (Content)');
  $this
    ->getSession()
    ->back();

  // Test editing a template.
  $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[node:field_does_not_exist] title [node:title] needs review. You can view it at [node:url].[node:field_does_not_exist]',
    'subject' => 'Content needs[node:field_does_not_exist] review: [node:title][node:field_does_not_exist]',
    'replyTo' => '[node:author:mail]',
  ], t('Save'));
  $assert
    ->pageTextContains('Saved the Content needs review Email Template');

  // Edit the transition from needs review to published and use the
  // 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));

  // Edit the transition from draft to needs review and add email config:
  // approver template.
  $this
    ->enableTemplateForTransitionOrWorkflow('draft_needs_review', 'needs_review');

  // Create a node and add to the notifier field.
  $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');

  // Transition to needs review.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');

  // Reset collected email.
  $this->container
    ->get('state')
    ->set('system.test_mail_collector', []);
  $this
    ->submitForm([] + $this
    ->getAdditionalFieldsForTransition('draft', 'needs_review'), $this
    ->getSubmitButtonTitleForTransition('draft', 'needs_review'));
  $this
    ->assertNeedsReviewNotifications($node);

  // Now try again going straight to needs review (no draft).
  // Reset collected email.
  $this->container
    ->get('state')
    ->set('system.test_mail_collector', []);

  // Create a node and add to the notifier field.
  $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);

  // Login as approver and transition to approved.
  $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'));

  // Check mail goes to author and notifier.
  $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);

  // The node id text is added to the email subject in the
  // workbench_email_test_mail_alter() function.
  // We check that it is set here.
  $this
    ->assertEquals(sprintf('Content approved: %s (node id: %s)', $node
    ->getTitle(), $node
    ->id()), $last['subject']);
  $this
    ->assertEquals(sprintf('Content approved: %s (node id: %s)', $node
    ->getTitle(), $node
    ->id()), $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']));

  // Check that empty tokens are removed.
  $this
    ->assertStringNotContainsString('[node:field_does_not_exist]', preg_replace('/\\s+/', ' ', $prev['body']));
  $this
    ->assertStringNotContainsString('[node:field_does_not_exist]', 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']));

  // Test again with node that was previously published.
  // Log back in as editor.
  $this
    ->drupalLogin($this->editor);
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->submitForm([] + $this
    ->getAdditionalFieldsForTransition('published', 'draft'), $this
    ->getSubmitButtonTitleForTransition('published', 'draft'));

  // And now request a review.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');

  // Reset collected email.
  $this->container
    ->get('state')
    ->set('system.test_mail_collector', []);
  $this
    ->submitForm([] + $this
    ->getAdditionalFieldsForTransition('draft', 'needs_review'), $this
    ->getSubmitButtonTitleForTransition('draft', 'needs_review'));
  $this
    ->assertNeedsReviewNotifications($node);

  // Try with the other node type, that isn't enabled.
  $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');

  // Transition to needs review.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->submitForm([] + $this
    ->getAdditionalFieldsForTransition('draft', 'needs_review'), $this
    ->getSubmitButtonTitleForTransition('draft', 'needs_review'));

  // No mail should be sent.
  $captured_emails = $this->container
    ->get('state')
    ->get('system.test_mail_collector') ?: [];
  $this
    ->assertEmpty($captured_emails);
}