You are here

message_ui.test in Message UI 7

File

message_ui.test
View source
<?php

/**
 * Testing the message access use case.
 */
class MessageUiPermissions extends DrupalWebTestCase {

  /**
   * The user object.
   * @var
   */
  public $user;

  /**
   * @var
   * The user role.
   */
  public $rid;
  public static function getInfo() {
    return array(
      'name' => 'Message UI permissions',
      'description' => 'Testing the use case of message_access function.',
      'group' => 'Message UI',
    );
  }
  function setUp() {
    parent::setUp('message', 'message_ui');
    $this->user = $this
      ->drupalCreateUser();
    $message_type = message_type_create('foo', array(
      'message_text' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => 'Example text.',
          ),
        ),
      ),
    ));
    $message_type
      ->save();
    $role = user_role_load_by_name('authenticated user');
    $this->rid = $role->rid;
  }

  /**
   * Test message_access use case.
   */
  function testMessageUiPermissions() {

    // verify the user can't create the message.
    $this
      ->drupalLogin($this->user);
    $this
      ->drupalGet('admin/content/message/create/foo');
    $this
      ->assertResponse(403, t("The user can't create message."));

    // Create the message.
    $this
      ->grantMessageUiPermission('create');
    $this
      ->drupalPost('admin/content/message/create/foo', array(), t('Create'));

    // Verify the user now can see the text.
    $this
      ->grantMessageUiPermission('view');
    $this
      ->drupalGet('message/1');
    $this
      ->assertResponse(200, "The user can't view message.");

    // Verify can't edit the message.
    $this
      ->drupalGet('message/1/edit');
    $this
      ->assertResponse(403, "The user can't edit message.");

    // Grant permission to the user.
    $this
      ->grantMessageUiPermission('edit');
    $this
      ->drupalGet('message/1/edit');
    $this
      ->assertResponse(200, "The user can't edit message.");

    // Verify the user can't delete the message.
    $this
      ->drupalGet('message/1/delete');
    $this
      ->assertResponse(403, "The user can't delete the message");

    // Grant the permission to the user.
    $this
      ->grantMessageUiPermission('delete');
    $this
      ->drupalPost('message/1/delete', array(), t('Delete'));

    // The user did not have the permission to the overview page - verify access
    // denied.
    $this
      ->assertResponse(403, t("The user can't access the over view page."));
    user_role_grant_permissions($this->rid, array(
      'administer message types',
    ));
    $this
      ->drupalGet('admin/content/message');
    $this
      ->assertResponse(200, "The user can access the over view page.");

    // Create a new user with the bypass access permission and verify the bypass.
    $this
      ->drupalLogout();
    $user = $this
      ->drupalCreateUser(array(
      'bypass message access control',
    ));

    // Verify the user can by pass the message access control.
    $this
      ->drupalLogin($user);
    $this
      ->drupalGet('admin/content/message/create/foo');
    $this
      ->assertResponse(200, 'The user can bypass the message access control.');
  }

  /**
   * Grant to the user a specific permission.
   *
   * @param $operation
   *  The type of operation - create, update, delete or view.
   */
  private function grantMessageUiPermission($operation) {
    user_role_grant_permissions($this->rid, array(
      $operation . ' a foo message instance',
    ));
  }

  /**
   * Checking the alteration flow for other modules.
   */
  public function testMessageUIAccessHook() {
    module_enable(array(
      'message_ui_test',
    ));
    $this
      ->drupalLogin($this->user);

    // Setting up the operation and the expected value from the access callback.
    $permissions = array(
      'create' => TRUE,
      'view' => TRUE,
      'delete' => FALSE,
      'update' => FALSE,
    );
    $message = message_create('foo');
    $message->uid = $this->user->uid;
    $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.
      $message->{$op} = $value;
      $params = array(
        '@operation' => $op,
        '@value' => $value,
      );
      $this
        ->assertEqual(message_ui_access_control($op, $message, $this->user), $value, format_string('The hook return @value for @operation', $params));
    }
  }

}

/**
 * Testing the editing of the hard coded arguments.
 */
class MessageUiHardCodedArguments extends DrupalWebTestCase {

  /**
   * The first user object.
   * @var
   */
  public $user1;

  /**
   * The second user object.
   * @var
   */
  public $user2;
  public static function getInfo() {
    return array(
      'name' => 'Message UI arguments single update',
      'description' => 'Testing the editing of the hard coded arguments.',
      'group' => 'Message UI',
      'dependencies' => array(
        'entity_token',
      ),
    );
  }
  public function setUp() {
    parent::setUp('message', 'message_ui', 'entity_token');
    $message_type = message_type_create('dummy_message', array(
      'message_text' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => '@{message:user:name}.',
          ),
        ),
      ),
    ));
    $message_type
      ->save();
    $this->user1 = $this
      ->drupalCreateUser();
    $this->user2 = $this
      ->drupalCreateUser();
    $role = user_role_load_by_name('authenticated user');
    user_role_grant_permissions($role->rid, array(
      'bypass message access control',
    ));
  }

  /**
   * Verify that a user can update the arguments for each instance.
   */
  public function testHardCoded() {
    $this
      ->drupalLogin($this->user1);
    $message = message_create('dummy_message');
    $message->uid = $this->user1->uid;
    $message
      ->save();

    // Verifying the message hard coded value is set to the user 1.
    $this
      ->drupalGet('message/' . $message->mid);
    $this
      ->assertText($this->user1->name, 'The message token is set to the user 1.');
    $message->uid = $this->user2->uid;
    $message
      ->save();
    $this
      ->drupalGet('message/' . $message->mid);
    $this
      ->assertNoText($this->user2->name, 'The message token is set to the user 1 after editing the message.');

    // Update the message arguments automatically.
    $edit = array(
      'name' => $this->user2->name,
      'replace_tokens' => 'update',
    );
    $this
      ->drupalPost('message/' . $message->mid . '/edit', $edit, t('Update'));
    $this
      ->assertText($this->user2->name, 'The message token as updated automatically.');

    // Update the message arguments manually.
    $edit = array(
      'name' => $this->user2->name,
      'replace_tokens' => 'update_manually',
      '@{message:user:name}' => 'Dummy name',
    );
    $this
      ->drupalPost('message/' . $message->mid . '/edit', $edit, t('Update'));
    $this
      ->assertText('Dummy name', 'The hard coded token was updated with a custom value.');
  }

}

/**
 * Testing the update of the hard coded arguments in massive way.
 */
class MessageUiMassiveHardCodedArguments extends DrupalWebTestCase {
  public $user1;
  public static function getInfo() {
    return array(
      'name' => 'Message UI arguments massive update',
      'description' => 'Testing the removing/updating an hard coded arguments.',
      'group' => 'Message UI',
      'dependencies' => array(
        'entity_token',
      ),
    );
  }
  public function setUp() {
    parent::setUp('message', 'message_ui', 'entity_token');
    $message_type = message_type_create('dummy_message', array(
      'message_text' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => '@{message:user:name}.',
          ),
        ),
      ),
    ));
    $message_type
      ->save();

    // Set a queue worker for the update arguments when updating a message type.
    variable_set('update_tokens_update_tokens', TRUE);
    variable_set('update_tokens_how_update', 'update_when_item');
  }
  public function testRemoveAddingArguments() {

    // Create a message.
    $this->user1 = $this
      ->drupalCreateUser();
    $message = message_create('dummy_message');
    $message->uid = $this->user1->uid;
    $message
      ->save();
    $original_arguments = $message->arguments;

    // Update message instance when removing a hard coded argument.
    variable_set('update_tokens_how_to_act', 'update_when_removed');
    $message_type = message_type_load('dummy_message');
    $message_type->message_text[LANGUAGE_NONE][0]['value'] = '[message:user:name].';
    $message_type
      ->save();

    // Fire the queue worker.
    $queue = DrupalQueue::get('message_ui_arguments');
    $item = $queue
      ->claimItem();
    message_ui_arguments_worker($item->data);

    // Verify the arguments has changed.
    $message = message_load($message->mid);
    $this
      ->assertTrue($original_arguments != $message->arguments, 'The message arguments has changed during the queue worker work.');

    // Creating a new message and her hard coded arguments.
    $message = message_create('dummy_message');
    $message->uid = $this->user1->uid;
    $message
      ->save();
    $original_arguments = $message->arguments;

    // Process the message instance when adding hard coded arguments.
    variable_set('update_tokens_how_to_act', 'update_when_added');
    $message_type = message_type_load('dummy_message');
    $message_type->message_text[LANGUAGE_NONE][0]['value'] = '@{message:user:name}.';
    $message_type
      ->save();

    // Fire the queue worker.
    $queue = DrupalQueue::get('message_ui_arguments');
    $item = $queue
      ->claimItem();
    message_ui_arguments_worker($item->data);

    // Verify the arguments has changed.
    $message = message_load($message->mid);
    $this
      ->assertTrue($original_arguments == $message->arguments, 'The message arguments has changed during the queue worker work.');
  }

}

Classes

Namesort descending Description
MessageUiHardCodedArguments Testing the editing of the hard coded arguments.
MessageUiMassiveHardCodedArguments Testing the update of the hard coded arguments in massive way.
MessageUiPermissions Testing the message access use case.