You are here

function FlagAdminTestCase::testFlagAdmin in Flag 7.3

Create a flag through the UI and ensure that it is saved properly.

File

tests/flag.test, line 246
Tests for the Flag module.

Class

FlagAdminTestCase
Test Flag admin UI.

Code

function testFlagAdmin() {

  // Add a new flag using the UI.
  $this
    ->drupalGet(FLAG_ADMIN_PATH . '/add/node');

  // Check the form has the expected defaults.
  $this
    ->assertFieldByName('flag_short', 'Flag this item', "The flag message default value shows in the form.");
  $this
    ->assertFieldByName('unflag_short', 'Unflag this item', "The unflag message default value shows in the form.");
  $this
    ->assertFieldByName('show_in_links[full]', 'full', "The view mode option is set to the node 'full' view mode by default.");
  $this
    ->assertFieldByName('show_in_links[teaser]', 'teaser', "The view mode option is set to the node 'teaser' view mode by default.");
  $edit = array(
    'name' => drupal_strtolower($this
      ->randomName()),
    'title' => $this
      ->randomName(),
    'flag_short' => 'flag short [node:nid]',
    'flag_long' => 'flag long [node:nid]',
    'flag_message' => 'flag message [node:nid]',
    'unflag_short' => 'unflag short [node:nid]',
    'unflag_long' => 'unflag long [node:nid]',
    'unflag_message' => 'unflag message [node:nid]',
    'roles[flag][2]' => TRUE,
    'roles[unflag][2]' => TRUE,
    'types[article]' => FALSE,
    'types[page]' => TRUE,
    'show_in_links[full]' => FALSE,
    'show_in_links[teaser]' => FALSE,
    'show_in_links[rss]' => FALSE,
    'show_in_links[search_index]' => FALSE,
    'show_in_links[search_result]' => FALSE,
    'show_on_form' => FALSE,
    'link_type' => 'toggle',
  );
  $saved = $edit;
  $saved['roles'] = array(
    'flag' => array(
      2,
    ),
    'unflag' => array(
      2,
    ),
  );
  $saved['types'] = array(
    'page',
  );
  $saved['show_in_links'] = array(
    'full' => 0,
    'teaser' => 0,
    'rss' => 0,
    'search_index' => 0,
    'search_result' => 0,
  );
  unset($saved['roles[flag][2]'], $saved['roles[unflag][2]'], $saved['types[article]'], $saved['types[page]'], $saved['show_in_links[full]'], $saved['show_in_links[teaser]'], $saved['show_in_links[rss]'], $saved['show_in_links[search_index]'], $saved['show_in_links[search_result]']);
  $this
    ->drupalPost(FLAG_ADMIN_PATH . '/add/node', $edit, t('Save flag'));
  drupal_static_reset('flag_get_flags');
  $flag = flag_get_flag($edit['name']);

  // Load the roles array for checking it matches.
  $flag
    ->fetch_roles();

  // Check that the flag object is in the database.
  $this
    ->assertTrue($flag != FALSE, t('Flag object found in database'));

  // Check each individual property of the flag and make sure it was set.
  foreach ($saved as $property => $value) {
    $this
      ->assertEqual($flag->{$property}, $value, t('Flag property %property properly saved.', array(
      '%property' => $property,
    )));
  }

  // Check permissions.
  $permissions = user_role_permissions(user_roles());
  foreach ($saved['roles'] as $action => $rids) {
    foreach ($rids as $rid) {
      $permission_string = "{$action} " . $saved['name'];
      $this
        ->assertTrue(isset($permissions[$rid][$permission_string]), t('Permission %perm set for flag.', array(
        '%perm' => $permission_string,
      )));
    }
  }

  // Edit the flag through the UI.
  $edit = array(
    'name' => drupal_strtolower($this
      ->randomName()),
    'title' => $this
      ->randomName(),
    'flag_short' => 'flag 2 short [node:nid]',
    'flag_long' => 'flag 2 long [node:nid]',
    'flag_message' => 'flag 2 message [node:nid]',
    'unflag_short' => 'unflag 2 short [node:nid]',
    'unflag_long' => 'unflag 2 long [node:nid]',
    'unflag_message' => 'unflag 2 message [node:nid]',
    'roles[flag][2]' => TRUE,
    'roles[unflag][2]' => TRUE,
    'types[article]' => TRUE,
    'types[page]' => FALSE,
    'show_in_links[full]' => TRUE,
    'show_in_links[teaser]' => TRUE,
    'show_in_links[rss]' => FALSE,
    'show_in_links[search_index]' => FALSE,
    'show_in_links[search_result]' => FALSE,
    'show_on_form' => TRUE,
    'link_type' => 'normal',
  );
  $saved = $edit;
  $saved['roles'] = array(
    'flag' => array(
      2,
    ),
    'unflag' => array(
      2,
    ),
  );
  $saved['types'] = array(
    'article',
  );
  $saved['show_in_links'] = array(
    'full' => TRUE,
    'teaser' => TRUE,
    'rss' => 0,
    'search_index' => 0,
    'search_result' => 0,
  );
  unset($saved['roles[flag][2]'], $saved['roles[unflag][2]'], $saved['types[article]'], $saved['types[page]'], $saved['show_in_links[full]'], $saved['show_in_links[teaser]'], $saved['show_in_links[rss]'], $saved['show_in_links[search_index]'], $saved['show_in_links[search_result]']);
  $this
    ->drupalPost(FLAG_ADMIN_PATH . '/manage/' . $flag->name, $edit, t('Save flag'));
  drupal_static_reset('flag_get_flags');
  $flag = flag_get_flag($edit['name']);

  // Load the roles array for checking it matches.
  $flag
    ->fetch_roles();

  // Check that the flag object is in the database.
  $this
    ->assertTrue($flag != FALSE, t('Flag object found in database'));

  // Check each individual property of the flag and make sure it was set.
  foreach ($saved as $property => $value) {
    $this
      ->assertEqual($flag->{$property}, $value, t('Flag property %property properly saved.', array(
      '%property' => $property,
    )));
  }

  // Clear the user access cache so our changes to permissions are noticed.
  drupal_static_reset('user_access');
  drupal_static_reset('user_role_permissions');

  // Check permissions.
  $permissions = user_role_permissions(user_roles());
  foreach ($saved['roles'] as $action => $rids) {
    foreach ($rids as $rid) {
      $permission_string = "{$action} " . $saved['name'];
      $this
        ->assertTrue(isset($permissions[$rid][$permission_string]), t('Permission %perm set for flag.', array(
        '%perm' => $permission_string,
      )));
    }
  }

  // Delete the flag through the UI.
  $this
    ->drupalPost(FLAG_ADMIN_PATH . '/manage/' . $flag->name . '/delete', array(), t('Delete'));
  drupal_static_reset('flag_get_flags');
  $this
    ->assertFalse(flag_get_flag($flag->name), t('Flag successfully deleted.'));
}