You are here

public function MessageUiPermissionsTest::testMessageUiAccessHook in Message UI 8

Checking the alteration flow for other modules.

File

tests/src/Functional/MessageUiPermissionsTest.php, line 121

Class

MessageUiPermissionsTest
Testing the message access use case.

Namespace

Drupal\Tests\message_ui\Functional

Code

public function testMessageUiAccessHook() {

  // Install the message ui test dummy module.
  \Drupal::service('module_installer')
    ->install([
    'message_ui_test',
  ]);
  $this
    ->drupalLogin($this->account);

  // Setting up the operation and the expected value from the access callback.
  $permissions = [
    'create' => TRUE,
    'view' => TRUE,
    'delete' => FALSE,
    'update' => FALSE,
  ];

  // Get the message template and create an instance.
  $message_template = $this
    ->loadMessageTemplate('foo');

  /* @var $message Message */
  $message = Message::create([
    'template' => $message_template
      ->id(),
  ]);
  $message
    ->setOwner($this->account);
  $message
    ->save();
  foreach ($permissions as $op => $value) {

    // When the hook access of the dummy module will get in action it will
    // check which value need to return. If the access control function will
    // return the expected value then we know the hook got in action.
    if ($op == 'create') {
      $returned = $this->accessHandler
        ->createAccess($message_template
        ->id(), $this->account);
    }
    else {
      $message->{$op} = $value;
      $returned = $this->accessHandler
        ->access($message, $op, $this->account);
    }
    $params = [
      '@operation' => $op,
      '@value' => $value,
      '@returned' => $returned,
    ];
    $this
      ->assertEquals($value, $returned, new FormattableMarkup('The hook return @value for @operation when it need to return @returned', $params));
  }

  // Check dynamic template.
  $class = new MessagePermissions();
  $this
    ->assertEquals(count($class
    ->messageTemplatePermissions()), count($this->container
    ->get('entity_type.manager')
    ->getStorage('message_template')
    ->loadMultiple()) * 4);
}