You are here

shoutbox_user_access.test in Shoutbox 5

Same filename and directory in other branches
  1. 6 tests/shoutbox_user_access.test

shoutbox unit test for shoutbox_user_access and shoutbox_is_user_owned

File

tests/shoutbox_user_access.test
View source
<?php

/**
 * @file
 * shoutbox unit test for shoutbox_user_access
 * and shoutbox_is_user_owned
 *
 */
class ShoutboxUserAccessTest extends DrupalTestCase {

  /**
   * A global basic user who is subject to moderation.
   */
  var $basic_user;

  /**
   * A global basic user who is not subject to moderation.
   */
  var $unmoderated_user;

  /**
   * A global administrative user who may bypass all restrictions.
   */
  var $admin_user;

  /**
   * A global administrative user who may bypass all restrictions.
   */
  var $moderator;

  /**
   * A global variable to hold the shout id of the shout created.
   * This enables deletion of the shout during tear down.
   */
  var $shout_id;
  function get_info() {
    return array(
      'name' => t('User access'),
      'desc' => t('Verify that the correct user access blah'),
      'group' => t('Shoutbox Tests'),
    );
  }
  function setUp() {

    // TODO: Put your code here.
    // enable module
    $this
      ->drupalModuleEnable('shoutbox');

    // enable nickname field
    $this
      ->drupalVariableSet('shoutbox_shownamefield', 1);
    $permissions = array(
      'post shouts',
      'delete own shouts',
      'edit own shouts',
    );
    $basic_user = $this
      ->drupalCreateUserRolePerm($permissions);

    // unmoderated permission
    $permissions[] = 'post shouts without approval';
    $unmoderated_user = $this
      ->drupalCreateUserRolePerm($permissions);
    $permissions = array(
      'administer shoutbox',
    );
    $admin_user = $this
      ->drupalCreateUserRolePerm($permissions);
    $permissions = array(
      'moderate shoutbox',
    );
    $moderator = $this
      ->drupalCreateUserRolePerm($permissions);

    // Assign users to their test suite-wide properties.
    $this->basic_user = $basic_user;
    $this->unmoderated_user = $unmoderated_user;
    $this->admin_user = $admin_user;
    $this->moderator = $moderator;

    // Always call the setUp() function from the parent class.
    parent::setUp();
  }

  /**
   * Helper function to get the shout id
   *
   */
  function _get_shout_id($shout) {
    $shout_id = db_result(db_query("SELECT shout_id FROM {shoutbox} WHERE nick = '%s' AND shout = '%s' ", $shout['nick'], $shout['message']));
    return $shout_id;
  }
  function testShoutboxUserAccess() {

    // test add, edit and delete for a regular unmoderated user
    // Login as basic user to perform initial content creation.
    $this
      ->drupalLoginUser($this->unmoderated_user);

    // Create an  unmoderated piece of content.
    $shout = array();
    $shout['nick'] = $this
      ->randomName(2, 'shout');
    $shout['message'] = $this
      ->randomName(10, 'shout');
    $shout['url'] = 'http://slashdot.org';
    $this
      ->drupalPostRequest('node', $shout, 'Shout');
    $this
      ->assertWantedRaw(t('Your shout has been submitted.'));
    $this
      ->assertWantedRaw(t($shout['message']));

    // get the shout id
    $sid = $this
      ->_get_shout_id($shout);

    // edit the shout
    $edited_shout = array();
    $edited_shout['shout'] = $this
      ->randomName(10, 'shout');
    $url = 'shoutbox/' . $sid . '/edit';
    $this
      ->drupalPostRequest($url, $edited_shout, 'Update');
    $this
      ->assertWantedRaw(t('Your shout has been saved.'));
    $this
      ->assertWantedRaw(t($edited_shout['shout']));

    // delete the shout
    $url = 'shoutbox/' . $sid . '/delete';
    $this
      ->drupalPostRequest($url, NULL, 'Confirm');
    $this
      ->assertWantedRaw(t('Your shout was deleted.'));
    $this
      ->assertNoText(t($edited_shout['shout']));
    $url = url('logout', NULL, NULL, TRUE);
    $this
      ->get($url);
    $this
      ->cleanup($sid);

    // test add, edit and delete for a regular moderated user
    $this
      ->drupalLoginUser($this->basic_user);

    // Create an  unmoderated piece of content.
    $shout = array();
    $shout['nick'] = $this
      ->randomName(2, 'shout');
    $shout['message'] = $this
      ->randomName(10, 'shout');
    $shout['url'] = 'http://slashdot.org';
    $this
      ->drupalPostRequest('node', $shout, 'Shout');
    $this
      ->assertWantedRaw(t('Your shout has been submitted for approval by a moderator. Others will not see this shout until it is approved.'));

    // get the shout id
    $sid = $this
      ->_get_shout_id($shout);

    // edit the shout
    $edited_shout = array();
    $edited_shout['shout'] = $this
      ->randomName(10, 'shout');
    $url = 'shoutbox/' . $sid . '/edit';
    $this
      ->drupalPostRequest($url, $edited_shout, 'Update');
    $this
      ->assertWantedRaw(t('Your shout has been saved.'));
    $this
      ->assertWantedRaw(t($edited_shout['shout']));

    // delete the shout
    $url = 'shoutbox/' . $sid . '/delete';
    $this
      ->drupalPostRequest($url, NULL, 'Confirm');
    $this
      ->assertWantedRaw(t('Your shout was deleted.'));
    $this
      ->assertNoText(t($edited_shout['shout']));
    $url = url('logout', NULL, NULL, TRUE);
    $this
      ->get($url);
    $this
      ->cleanup($sid);

    // now test moderation
    // create a moderated shout
    $this
      ->drupalLoginUser($this->basic_user);

    // Create an  unmoderated piece of content.
    $shout = array();
    $shout['nick'] = $this
      ->randomName(2, 'shout');
    $shout['message'] = $this
      ->randomName(10, 'shout');
    $shout['url'] = 'http://slashdot.org';
    $this
      ->drupalPostRequest('node', $shout, 'Shout');
    $this
      ->assertWantedRaw(t('Your shout has been submitted for approval by a moderator. Others will not see this shout until it is approved.'));

    // get the shout id
    $sid = $this
      ->_get_shout_id($shout);

    // Try to moderate  it
    $path = 'shoutbox/' . $sid . '/publish';
    $url = url($path, NULL, NULL, TRUE);
    $this
      ->get($url);
    $this
      ->assertResponse(403);

    // logout
    $url = url('logout', NULL, NULL, TRUE);
    $this
      ->get($url);

    // login as moderator
    $this
      ->drupalLoginUser($this->moderator);

    // moderate content
    $path = 'shoutbox/' . $sid . '/publish';
    $url = url($path, NULL, NULL, TRUE);

    // verify confirmation page
    $this
      ->get($url);
    $this
      ->assertText(t('Are you sure you want to publish this shout?'));
    $this
      ->drupalPostRequest($path, NULL, 'Confirm');
    $this
      ->assertText(t('The shout was published.'));

    // now unpublish it
    $path = 'shoutbox/' . $sid . '/unpublish';
    $url = url($path, NULL, NULL, TRUE);

    // verify confirmation page
    $this
      ->get($url);
    $this
      ->assertText(t('Are you sure you want to unpublish this shout?'));
    $this
      ->drupalPostRequest($path, NULL, 'Confirm');
    $this
      ->assertText(t('The shout was unpublished.'));

    // try and edit it
    $path = 'shoutbox/' . $sid . '/edit';
    $url = url($path, NULL, NULL, TRUE);
    $this
      ->get($url);
    $this
      ->assertResponse(403);

    // try and delete it
    $path = 'shoutbox/' . $sid . '/delete';
    $url = url($path, NULL, NULL, TRUE);
    $this
      ->get($url);
    $this
      ->assertResponse(403);
    $this
      ->cleanup($sid);
  }
  function testAdminUserAccess() {

    // test admin access
    $this
      ->drupalLoginUser($this->admin_user);

    // Create an unmoderated piece of content.
    $shout = array();
    $shout['nick'] = $this
      ->randomName(2, 'shout');
    $shout['message'] = $this
      ->randomName(10, 'shout');
    $shout['url'] = 'http://ghanaweb.com';
    $this
      ->drupalPostRequest('node', $shout, 'Shout');
    $this
      ->assertWantedRaw(t('Your shout has been submitted.'));
    $this
      ->assertWantedRaw(t($shout['message']));

    // get the shout id
    $sid = $this
      ->_get_shout_id($shout);

    // moderate content
    $path = 'shoutbox/' . $sid . '/unpublish';

    // verify confirmation page
    $url = url($path, NULL, NULL, TRUE);
    $this
      ->get($url);
    $this
      ->assertWantedRaw(t('Are you sure you want to unpublish this shout?'));
    $this
      ->drupalPostRequest($path, NULL, 'Confirm');
    $this
      ->assertText(t('The shout was unpublished.'));

    // un moderate it
    $path = 'shoutbox/' . $sid . '/publish';

    // verify confirmation page
    $url = url($path, NULL, NULL, TRUE);
    $this
      ->get($url);
    $this
      ->assertText(t('Are you sure you want to publish this shout?'));
    $this
      ->drupalPostRequest($path, NULL, 'Confirm');
    $this
      ->assertText(t('The shout was published.'));

    // try and edit it
    // edit the shout
    $edited_shout = array();
    $edited_shout['shout'] = $this
      ->randomName(10, 'shout');
    $path = 'shoutbox/' . $sid . '/edit';
    $this
      ->drupalPostRequest($path, $edited_shout, 'Update');
    $this
      ->assertWantedRaw(t('The shout has been saved.'));
    $this
      ->assertWantedRaw(t($edited_shout['shout']));

    //delete it

    // delete the shout
    $path = 'shoutbox/' . $sid . '/delete';
    $this
      ->drupalPostRequest($path, NULL, 'Confirm');
    $this
      ->assertWantedRaw(t('Your shout was deleted.'));
    $this
      ->assertNoText(t($edited_shout['shout']));

    // logout
    $url = url('logout', NULL, NULL, TRUE);
    $this
      ->get($url);
    $this
      ->cleanup($sid);

    // create a regular user content
    // Login as basic user to perform initial content creation.
    $this
      ->drupalLoginUser($this->unmoderated_user);

    // Create an  unmoderated piece of content.
    $shout = array();
    $shout['nick'] = $this
      ->randomName(2, 'shout');
    $shout['message'] = $this
      ->randomName(10, 'shout');
    $shout['url'] = 'http://slashdot.org';
    $this
      ->drupalPostRequest('node', $shout, 'Shout');
    $this
      ->assertWantedRaw(t('Your shout has been submitted.'));
    $this
      ->assertWantedRaw(t($shout['message']));

    // get the shout id
    $sid = $this
      ->_get_shout_id($shout);

    // logout
    $url = url('logout', NULL, NULL, TRUE);
    $this
      ->get($url);

    // login as admin
    $this
      ->drupalLoginUser($this->admin_user);

    // moderate content
    $path = 'shoutbox/' . $sid . '/unpublish';

    // verify confirmation page
    $url = url($path, NULL, NULL, TRUE);
    $this
      ->get($url);
    $this
      ->assertText(t('Are you sure you want to unpublish this shout?'));
    $this
      ->drupalPostRequest($path, NULL, 'Confirm');
    $this
      ->assertText(t('The shout was unpublished.'));

    // un moderate it
    $path = 'shoutbox/' . $sid . '/publish';

    // verify confirmation page
    $url = url($path, NULL, NULL, TRUE);
    $this
      ->get($url);
    $this
      ->assertText(t('Are you sure you want to publish this shout?'));
    $this
      ->drupalPostRequest($path, NULL, 'Confirm');
    $this
      ->assertText(t('The shout was published.'));

    // try and edit it
    // edit the shout
    $edited_shout = array();
    $edited_shout['shout'] = $this
      ->randomName(10, 'shout');
    $path = 'shoutbox/' . $sid . '/edit';
    $this
      ->drupalPostRequest($path, $edited_shout, 'Update');
    $this
      ->assertWantedRaw(t('The shout has been saved.'));
    $this
      ->assertWantedRaw(t($edited_shout['shout']));

    //delete it

    // delete the shout
    $path = 'shoutbox/' . $sid . '/delete';
    $this
      ->drupalPostRequest($path, NULL, 'Confirm');
    $this
      ->assertWantedRaw(t('Your shout was deleted.'));
    $this
      ->assertNoText(t($edited_shout['shout']));

    // delete shout
    $this
      ->cleanup($sid);
  }
  function cleanup($shout_id) {
    db_query("DELETE FROM {shoutbox} WHERE shout_id = %d", $shout_id);
  }

}

Classes

Namesort descending Description
ShoutboxUserAccessTest @file shoutbox unit test for shoutbox_user_access and shoutbox_is_user_owned