View source
<?php
class MessageUiPermissions extends DrupalWebTestCase {
public $user;
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;
}
function testMessageUiPermissions() {
$this
->drupalLogin($this->user);
$this
->drupalGet('admin/content/message/create/foo');
$this
->assertResponse(403, t("The user can't create message."));
$this
->grantMessageUiPermission('create');
$this
->drupalPost('admin/content/message/create/foo', array(), t('Create'));
$this
->grantMessageUiPermission('view');
$this
->drupalGet('message/1');
$this
->assertResponse(200, "The user can't view message.");
$this
->drupalGet('message/1/edit');
$this
->assertResponse(403, "The user can't edit message.");
$this
->grantMessageUiPermission('edit');
$this
->drupalGet('message/1/edit');
$this
->assertResponse(200, "The user can't edit message.");
$this
->drupalGet('message/1/delete');
$this
->assertResponse(403, "The user can't delete the message");
$this
->grantMessageUiPermission('delete');
$this
->drupalPost('message/1/delete', array(), t('Delete'));
$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.");
$this
->drupalLogout();
$user = $this
->drupalCreateUser(array(
'bypass message access control',
));
$this
->drupalLogin($user);
$this
->drupalGet('admin/content/message/create/foo');
$this
->assertResponse(200, 'The user can bypass the message access control.');
}
private function grantMessageUiPermission($operation) {
user_role_grant_permissions($this->rid, array(
$operation . ' a foo message instance',
));
}
public function testMessageUIAccessHook() {
module_enable(array(
'message_ui_test',
));
$this
->drupalLogin($this->user);
$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) {
$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));
}
}
}
class MessageUiHardCodedArguments extends DrupalWebTestCase {
public $user1;
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',
));
}
public function testHardCoded() {
$this
->drupalLogin($this->user1);
$message = message_create('dummy_message');
$message->uid = $this->user1->uid;
$message
->save();
$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.');
$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.');
$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.');
}
}
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();
variable_set('update_tokens_update_tokens', TRUE);
variable_set('update_tokens_how_update', 'update_when_item');
}
public function testRemoveAddingArguments() {
$this->user1 = $this
->drupalCreateUser();
$message = message_create('dummy_message');
$message->uid = $this->user1->uid;
$message
->save();
$original_arguments = $message->arguments;
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();
$queue = DrupalQueue::get('message_ui_arguments');
$item = $queue
->claimItem();
message_ui_arguments_worker($item->data);
$message = message_load($message->mid);
$this
->assertTrue($original_arguments != $message->arguments, 'The message arguments has changed during the queue worker work.');
$message = message_create('dummy_message');
$message->uid = $this->user1->uid;
$message
->save();
$original_arguments = $message->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();
$queue = DrupalQueue::get('message_ui_arguments');
$item = $queue
->claimItem();
message_ui_arguments_worker($item->data);
$message = message_load($message->mid);
$this
->assertTrue($original_arguments == $message->arguments, 'The message arguments has changed during the queue worker work.');
}
}