View source
<?php
namespace Drupal\Tests\content_moderation_notifications\Functional\Form;
use Drupal\content_moderation_notifications\Entity\ContentModerationNotification;
use Drupal\Tests\content_moderation_notifications\Kernel\ContentModerationNotificationTestTrait;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\workflows\Entity\Workflow;
class CrudFormTest extends BrowserTestBase {
use ContentTypeCreationTrait;
use ContentModerationNotificationTestTrait;
protected $defaultTheme = 'stark';
protected $adminUser;
protected static $modules = [
'block',
'content_moderation_notifications',
'node',
'filter_test',
];
protected function setUp() : void {
parent::setUp();
$this
->createContentType([
'type' => 'article',
]);
$this
->drupalCreateUser();
$this->adminUser = $this
->createUser([
'administer content moderation notifications',
'use text format filtered_html',
'use text format full_html',
]);
$this
->placeBlock('local_actions_block');
$this
->createEditorialWorkflow();
}
public function testCrud() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('/admin/config/workflow/notifications');
$this
->clickLink(t('Add notification'));
$edit = [
'label' => $this
->randomString(),
'id' => mb_strtolower($this
->randomMachineName()),
'workflow' => 'editorial',
'transitions[create_new_draft]' => TRUE,
'transitions[archived_published]' => TRUE,
'roles[authenticated]' => TRUE,
'subject' => $this
->randomString(),
'body[value]' => $this->randomGenerator
->paragraphs(2),
];
$this
->submitForm($edit, t('Create Notification'));
$notification = ContentModerationNotification::load($edit['id']);
$this
->assertSession()
->responseContains(t('Notification <a href=":url">%label</a> has been added.', [
'%label' => $edit['label'],
':url' => $notification
->toUrl('edit-form')
->toString(),
]));
$this
->assertEquals($edit['id'], $notification
->id());
$this
->assertEquals($edit['workflow'], $notification
->getWorkflowId());
$this
->assertEquals([
'authenticated' => 'authenticated',
], $notification
->getRoleIds());
$this
->assertEquals([
'create_new_draft' => 'create_new_draft',
'archived_published' => 'archived_published',
], $notification
->getTransitions());
$emails = [
$this
->randomMachineName(128) . '@example.com',
$this
->randomMachineName(128) . '@example.com',
$this
->randomMachineName(128) . '@example.com',
];
$edit = [
'subject' => $this
->randomString(),
'body[format]' => 'full_html',
'body[value]' => $this->randomGenerator
->paragraphs(3),
'emails' => $emails[0] . ",\r\n" . $emails[1] . "\n" . $emails[2],
];
$this
->drupalGet($notification
->toUrl('edit-form'));
$this
->submitForm($edit, t('Update Notification'));
$this
->assertSession()
->responseContains(t('Notification <a href=":url">%label</a> has been updated.', [
'%label' => $notification
->label(),
':url' => $notification
->toUrl('edit-form')
->toString(),
]));
$notification = ContentModerationNotification::load($notification
->id());
$this
->assertEquals($edit['subject'], $notification
->getSubject());
$this
->assertEquals($edit['body[value]'], $notification
->getMessage());
$this
->assertEquals('full_html', $notification
->getMessageFormat());
$this
->assertEquals($edit['emails'], $notification
->getEmails());
$this
->drupalGet($notification
->toUrl('disable-form'));
$this
->submitForm([], t('Confirm'));
$notification = ContentModerationNotification::load($notification
->id());
$this
->assertFalse($notification
->status());
$this
->drupalGet($notification
->toUrl('collection'));
$this
->assertSession()
->linkExists(t('Enable'));
$this
->drupalGet($notification
->toUrl('enable-form'));
$this
->submitForm([], t('Confirm'));
$notification = ContentModerationNotification::load($notification
->id());
$this
->assertTrue($notification
->status());
$this
->drupalGet($notification
->toUrl('collection'));
$this
->assertSession()
->linkExists(t('Disable'));
$this
->drupalGet($notification
->toUrl('delete-form'));
$this
->submitForm([], t('Delete Notification'));
$this
->assertSession()
->responseContains(t('Notification %label was deleted.', [
'%label' => $notification
->label(),
]));
$this
->assertSession()
->pageTextContains(t('There are no notifications yet.'));
}
public function testNoWorkflows() {
$workflow = Workflow::load('editorial');
$workflow
->delete();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('/admin/config/workflow/notifications');
$this
->clickLink(t('Add notification'));
$this
->assertSession()
->pageTextContains(t('No workflows available.'));
$this
->assertSession()
->linkExists(t('Manage workflows'));
$this
->assertSession()
->buttonNotExists(t('Create Notification'));
}
}