You are here

public function AccessSchemeInterfaceTest::testSchemeInterface in Access Control Kit 7

Create and edit an access scheme via the user interface.

File

./access.test, line 230
Tests for the access control kit module.

Class

AccessSchemeInterfaceTest
Tests the access scheme interface.

Code

public function testSchemeInterface() {

  // Visit the scheme admin overview page.
  $this
    ->drupalGet('admin/structure/access');

  // Create a new scheme through the admin form.
  $this
    ->clickLink(t('Add access scheme'));
  $this
    ->clickLink(t('Boolean'));
  $this
    ->assertText(t('Add access scheme: @type', array(
    '@type' => t('Boolean'),
  )));
  $edit = array();
  $machine_name = drupal_strtolower($this
    ->randomName());
  $edit['machine_name'] = $machine_name;
  $edit['name'] = $this
    ->randomName();
  $edit['description'] = $this
    ->randomName();
  $edit['roles[' . $this->ackRoleRid . ']'] = TRUE;
  $this
    ->drupalPost(NULL, $edit, t('Save access scheme and continue'));
  $this
    ->assertRaw(t('Added access scheme %name.', array(
    '%name' => $edit['name'],
  )), 'Scheme created successfully.');
  $this
    ->drupalGet('admin/structure/access');
  $this
    ->assertText(t('@name (Machine name: @machine_name)', array(
    '@name' => $edit['name'],
    '@machine_name' => $machine_name,
  )), 'Scheme found in the scheme admin overview listing.');

  // Confirm that all submitted values were saved.
  $this
    ->clickLink(t('edit'));
  $this
    ->assertFieldByName('name', $edit['name'], 'The name field is correctly set to ' . $edit['name']);
  $this
    ->assertFieldByName('machine_name', $edit['machine_name'], 'The machine_name field is correctly set to ' . $edit['machine_name']);
  $machine_name_disabled = $this
    ->xpath('//input[@id=:id and @disabled="disabled"]', array(
    ':id' => 'edit-machine-name',
  ));
  $this
    ->assertTrue($machine_name_disabled, 'The machine name cannot be changed.');
  $this
    ->assertFieldByName('description', $edit['description'], 'The description field is correctly set to ' . $edit['description']);
  $this
    ->assertFieldChecked('edit-roles-' . $this->ackRoleRid, 'The role is correctly selected.');
  $this
    ->assertText(t('No access-controllable objects available.'), 'Informs that no access-controllable object types have been defined.');

  // Edit the scheme.
  $edit = array();
  $edit['name'] = $this
    ->randomName();
  $this
    ->drupalPost(NULL, $edit, t('Save access scheme'));
  $this
    ->assertRaw(t('Updated access scheme %name.', array(
    '%name' => $edit['name'],
  )), 'Scheme updated successfully.');
  $this
    ->assertText(t('@name (Machine name: @machine_name)', array(
    '@name' => $edit['name'],
    '@machine_name' => $machine_name,
  )), 'Updated scheme found in the scheme admin overview listing.');

  // Check integration with the Field UI.
  $this
    ->clickLink(t('manage fields'));
  $this
    ->assertText(t('Manage fields'));
  $this
    ->assertText(t('Access control kit user reference'));
  $this
    ->assertText(t('Access control kit role reference'));
  $this
    ->assertText('ack_' . $machine_name);
  $this
    ->drupalGet('admin/structure/access');
  $this
    ->clickLink(t('manage display'));
  $this
    ->assertText(t('Manage display'));
  $this
    ->assertText(t('User'));
  $this
    ->assertText(t('Role'));

  // Try to submit a new scheme with a duplicate human-readable name.
  $edit['machine_name'] = drupal_strtolower($this
    ->randomName());
  $this
    ->drupalPost('admin/structure/access/add/boolean', $edit, t('Save access scheme and continue'));
  $this
    ->assertRaw(t('The name %name is already in use.', array(
    '%name' => $edit['name'],
  )));

  // Try to submit a new scheme with a duplicate machine name.
  $edit['name'] = $this
    ->randomName();
  $edit['machine_name'] = $machine_name;
  $this
    ->drupalPost('admin/structure/access/add/boolean', $edit, t('Save access scheme and continue'));
  $this
    ->assertText(t('The machine-readable name is already in use. It must be unique.'));

  // Try to submit an invalid machine name.
  $edit['machine_name'] = '!&^%';
  $this
    ->drupalPost('admin/structure/access/add/boolean', $edit, t('Save access scheme and continue'));
  $this
    ->assertText(t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));

  // Try to submit some disallowed machine names.
  foreach (array(
    'add',
    '0',
  ) as $bad_name) {
    $edit['machine_name'] = $bad_name;
    $this
      ->drupalPost('admin/structure/access/add/boolean', $edit, t('Save access scheme and continue'));
    $this
      ->assertText(t('Invalid machine-readable name. Enter a name other than @invalid.', array(
      '@invalid' => $bad_name,
    )));
  }

  // Ensure that scheme names and descriptions are escaped properly.
  $edit = array();
  $description = '<strong>' . $this
    ->randomName() . '</strong>';
  $edit['machine_name'] = 'don_t_panic';
  $edit['name'] = 'Don\'t Panic & Carry a Towel';
  $edit['description'] = '<script>' . $description . '</script>';
  $this
    ->drupalPost('admin/structure/access/add/boolean', $edit, t('Save access scheme and continue'));

  // Check that the name isn't double-filtered when used as the page title.
  $site_name = variable_get('site_name', 'Drupal');
  $this
    ->assertTitle(t("Don't Panic & Carry a Towel | @site-name", array(
    '@site-name' => $site_name,
  )));
  $this
    ->assertNoTitle(t('Don&#039;t Panic &amp; Carry a Towel | @site-name', array(
    '@site-name' => $site_name,
  )));

  // Check that the name is sanitized in overview.
  $this
    ->drupalGet('admin/structure/access');
  $this
    ->assertRaw('Don&#039;t Panic &amp; Carry a Towel');
  $this
    ->assertNoRaw($edit['name']);

  // Check that the description is sanitized in overview.
  $this
    ->assertRaw($description);
  $this
    ->assertNoRaw($edit['description']);
}