You are here

CrudFormTest.php in Content Moderation Notifications 8.2

Same filename and directory in other branches
  1. 8.3 tests/src/Functional/Form/CrudFormTest.php

File

tests/src/Functional/Form/CrudFormTest.php
View source
<?php

namespace Drupal\Tests\content_moderation_notifications\Functional\Form;

use Drupal\Component\Utility\Unicode;
use Drupal\content_moderation_notifications\Entity\ContentModerationNotification;
use Drupal\simpletest\ContentTypeCreationTrait;
use Drupal\Tests\BrowserTestBase;

/**
 * Tests CRUD forms.
 *
 * @group content_moderation_notifications
 */
class CrudFormTest extends BrowserTestBase {
  use ContentTypeCreationTrait;

  /**
   * An admin user.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $adminUser;

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'block',
    'content_moderation_notifications',
    'node',
    'filter_test',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this
      ->createContentType([
      'type' => 'article',
    ]);

    // Skip UID 1.
    $this
      ->drupalCreateUser();

    // Admin user.
    $this->adminUser = $this
      ->createUser([
      'administer content moderation notifications',
      'use text format filtered_html',
      'use text format full_html',
    ]);

    // Add local actions block.
    $this
      ->placeBlock('local_actions_block');
  }

  /**
   * Test basic CRUD operations via the forms.
   */
  public function testCrud() {
    $this
      ->drupalLogin($this->adminUser);
    $this
      ->drupalGet('/admin/config/workflow/notifications');
    $this
      ->clickLink(t('Add notification'));

    // Test the add form.
    $edit = [
      'id' => Unicode::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
      ->drupalPostForm(NULL, $edit, t('Create Notification'));
    $this
      ->assertSession()
      ->pageTextContains(t('Notification has been added.'));

    /** @var \Drupal\content_moderation_notifications\ContentModerationNotificationInterface $notification */
    $notification = ContentModerationNotification::load($edit['id']);
    $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());

    // Test the edit form.
    $edit = [
      'subject' => $this
        ->randomString(),
      'body[format]' => 'full_html',
      'body[value]' => $this->randomGenerator
        ->paragraphs(3),
    ];
    $this
      ->drupalGet($notification
      ->toUrl('edit-form'));
    $this
      ->drupalPostForm(NULL, $edit, t('Update Notification'));
    $this
      ->assertSession()
      ->pageTextContains(t('Notification has been updated'));

    /** @var \Drupal\content_moderation_notifications\ContentModerationNotificationInterface $notification */
    $notification = ContentModerationNotification::load($notification
      ->id());
    $this
      ->assertEquals($edit['subject'], $notification
      ->getSubject());
    $this
      ->assertEquals($edit['body[value]'], $notification
      ->getMessage());
    $this
      ->assertEquals('full_html', $notification
      ->getMessageFormat());

    // Test the delete form.
    $this
      ->drupalGet($notification
      ->toUrl('delete-form'));
    $this
      ->drupalPostForm(NULL, [], t('Delete Notification'));
    $this
      ->assertSession()
      ->pageTextContains(t('Notification was deleted'));
    $this
      ->assertSession()
      ->pageTextContains(t('There is no Notification yet.'));
  }

}

Classes

Namesort descending Description
CrudFormTest Tests CRUD forms.