You are here

public function AckMenuAccessTest::testNodeMenuUI in Access Control Kit 7

Test controlling access to the node menu options.

File

ack_menu/ack_menu.test, line 630
Tests for the ACK menu module.

Class

AckMenuAccessTest
Tests the menu access functions.

Code

public function testNodeMenuUI() {
  $this
    ->setUpMenu();

  // Test as the scheme menu admin, but with no usable menus on the node.
  $this
    ->drupalGet('node/add/article');
  $this
    ->assertNoText(t('Provide a menu link'));
  variable_set('menu_options_article', array(
    'main-menu',
    'management',
  ));
  $this
    ->drupalGet('node/add/article');
  $this
    ->assertNoText(t('Provide a menu link'));

  // Add the node's menu to the scheme and retest.
  $edit = array(
    'handlers[menu_link][AckMenuMap][menus][main-menu]' => TRUE,
  );
  $this
    ->drupalPost('admin/structure/access/' . $this->schemeMachineName, $edit, 'Save access scheme');
  $this
    ->assertText(t('Updated access scheme'), 'Access scheme configured.');
  $this
    ->drupalGet('node/add/article');
  $this
    ->assertText(t('Provide a menu link'));
  $this
    ->assertNoFieldByXPath('//option[@value="management:0"]', TRUE, 'Removed parent option: management');
  $edit = array(
    'title' => $this
      ->randomName(),
    'menu[enabled]' => TRUE,
    'menu[link_title]' => $this
      ->randomName(),
    'menu[parent]' => 'main-menu:0',
  );
  $this
    ->drupalPost(NULL, $edit, 'Save');
  $this
    ->assertLink($edit['menu[link_title]'], 0, 'The scheme menu admin can create a link from the node form.');
  $this
    ->clickLink(t('Edit'));
  $this
    ->assertFieldChecked('edit-menu-enabled');
  $this
    ->assertOptionSelected('edit-menu-parent', 'main-menu:0');

  // Test as the test user, without an access grant.
  $this
    ->drupalLogin($this->ackUser);
  $this
    ->drupalGet('node/add/article');
  $this
    ->assertNoText(t('Provide a menu link'));
  $this
    ->drupalPost(NULL, array(
    'title' => $this
      ->randomName(),
  ), 'Save');
  $this
    ->clickLink(t('Edit'));
  $this
    ->assertNoText(t('Provide a menu link'), 'The ACK user cannot create a link from the node form without a grant.');

  // Grant access to the test user.
  $this
    ->drupalLogin($this->ackAdmin);
  $field = 'ack_' . $this->schemeMachineName . '[und]';
  $edit = array(
    'user' => $this->ackUser->name,
    'role' => $this->ackRole->rid,
    $field . '[' . $this->ackUser->uid . ']' => TRUE,
    $field . '[' . $this->ackAdmin->uid . ']' => TRUE,
  );
  $this
    ->drupalPost('admin/access/add/' . $this->schemeMachineName, $edit, 'Save');

  // Retest as the test user, but with no usable menus on the node.
  $this
    ->drupalLogin($this->ackUser);
  variable_set('menu_options_article', array(
    'main-menu',
  ));
  $this
    ->drupalGet('node/add/article');
  $this
    ->assertNoText(t('Provide a menu link'));
  $this
    ->drupalPost(NULL, array(
    'title' => $this
      ->randomName(),
  ), 'Save');
  $this
    ->clickLink(t('Edit'));
  $this
    ->assertNoText(t('Provide a menu link'), 'The ACK user cannot create a link from the node form without a usable menu.');

  // Retest with a usable menu.
  variable_set('menu_options_article', array(
    'main-menu',
    'navigation',
  ));
  $this
    ->drupalGet('node/add/article');
  $this
    ->assertText(t('Provide a menu link'));

  // Check parent option filtering before save.
  $options = array(
    'mapped top link' => TRUE,
    'mapped child link' => TRUE,
    'no access child link' => FALSE,
    'no access grandchild link' => FALSE,
    'mapped child link with no access parent' => TRUE,
    'not mapped' => FALSE,
    'no access top link' => FALSE,
  );
  foreach ($options as $key => $allowed) {
    $xpath = '//option[@value="navigation:' . $this->items[$key]['mlid'] . '"]';
    if ($allowed) {
      $this
        ->assertFieldByXPath($xpath, TRUE, 'Allowed parent option: ' . $key);
    }
    else {
      $this
        ->assertNoFieldByXPath($xpath, TRUE, 'Removed parent option: ' . $key);
    }
  }
  $this
    ->assertNoFieldByXPath('//option[@value="navigation:0"]', TRUE, 'Removed parent option: navigation');
  $this
    ->assertNoFieldByXPath('//option[@value="main-menu:0"]', TRUE, 'Removed parent option: main menu');

  // Check parent option filtering after save.
  $parent = 'navigation:' . $this->items['mapped top link']['mlid'];
  $edit = array(
    'title' => $this
      ->randomName(),
    'menu[enabled]' => TRUE,
    'menu[link_title]' => $this
      ->randomName(),
    'menu[parent]' => $parent,
  );
  $this
    ->drupalPost(NULL, $edit, 'Save');
  $this
    ->assertLink($edit['menu[link_title]'], 0, 'The ACK user can create a link from the node form.');
  $this
    ->clickLink(t('Edit'));
  $this
    ->assertFieldChecked('edit-menu-enabled');
  $this
    ->assertOptionSelected('edit-menu-parent', $parent);

  // Links from realms other than the one that currently owns this link should
  // be excluded from the parent options.
  $options['mapped child link'] = FALSE;
  $options['mapped child link with no access parent'] = FALSE;
  foreach ($options as $key => $allowed) {
    $xpath = '//option[@value="navigation:' . $this->items[$key]['mlid'] . '"]';
    if ($allowed) {
      $this
        ->assertFieldByXPath($xpath, TRUE, 'Allowed parent option: ' . $key);
    }
    else {
      $this
        ->assertNoFieldByXPath($xpath, TRUE, 'Removed parent option: ' . $key);
    }
  }
  $this
    ->assertNoFieldByXPath('//option[@value="navigation:0"]', TRUE, 'Removed parent option: navigation');
  $this
    ->assertNoFieldByXPath('//option[@value="main-menu:0"]', TRUE, 'Removed parent option: main menu');

  // Test a node with an inaccessible link.
  $this
    ->drupalLogin($this->ackAdmin);
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'article',
    'uid' => $this->ackUser->uid,
  ));
  $edit = array(
    'link_title' => $this
      ->randomName(),
    'link_path' => 'node/' . $node->nid,
  );
  $this
    ->drupalPost('ack_menu/manage/' . $this->schemeMachineName . '/' . $this->noAccessUser->uid . '/add', $edit, 'Save');
  $this
    ->drupalLogin($this->ackUser);
  $this
    ->drupalGet('node/' . $node->nid . '/edit');
  $this
    ->assertText(t('Edit Article @title', array(
    '@title' => $node->title,
  )));
  $this
    ->assertNoText(t('Provide a menu link'), 'The ACK user cannot edit a link owned by a foreign realm.');

  // Test as a user with no menu-related access.
  $this
    ->drupalLogin($this->noAccessUser);
  $this
    ->drupalGet('node/add/article');
  $this
    ->assertNoText(t('Provide a menu link'));
  $this
    ->drupalPost(NULL, array(
    'title' => $this
      ->randomName(),
  ), 'Save');
  $this
    ->clickLink(t('Edit'));
  $this
    ->assertNoText(t('Provide a menu link'), 'A user with no menu-related access cannot create a link from the node form.');

  // Test as the menu administrator.
  $this
    ->drupalLogin($this->menuAdmin);
  $this
    ->drupalGet('node/add/article');
  $this
    ->assertText(t('Provide a menu link'));
  $this
    ->assertFieldByXPath('//option[@value="navigation:0"]', TRUE, 'Allowed parent option: navigation');
  $this
    ->assertFieldByXPath('//option[@value="main-menu:0"]', TRUE, 'Allowed parent option: main menu');
  $edit = array(
    'title' => $this
      ->randomName(),
    'menu[enabled]' => TRUE,
    'menu[link_title]' => $this
      ->randomName(),
    'menu[parent]' => 'main-menu:0',
  );
  $this
    ->drupalPost(NULL, $edit, 'Save');
  $this
    ->assertLink($edit['menu[link_title]'], 0, 'The menu admin can create a link from the node form.');
  $this
    ->clickLink(t('Edit'));
  $this
    ->assertFieldChecked('edit-menu-enabled');
  $this
    ->assertOptionSelected('edit-menu-parent', 'main-menu:0');
}