You are here

public function PublishContentTabTests::assertNodeOperationAccess in Publish Content 7

Perform a GET operation on a node.

This will check the response to access some operation via the URL of a node. In the case of 'publish' or 'unpublish' it will first visit the view of a node so that the relevant tabs can be generated.

Parameters

int $nid: The node nid

string $op: An operation such as 'view', 'edit', 'publish', 'unpublish'

int $expected_response: The expexted response code. If the user should not be able to see the 'publish' or 'unpublish' tabs, set this to 403, otherwise 200.

string $msg: (optional) An assertion log message.

Overrides PublishContentWebTestBase::assertNodeOperationAccess

File

tests/publishcontent.test, line 518
Unit tests for Publish Content module.

Class

PublishContentTabTests
Test permissions with the tab method.

Code

public function assertNodeOperationAccess($nid, $op, $expected_response, $msg = '') {
  if (in_array($op, array(
    'publish',
    'unpublish',
  ))) {
    $tab_link_text = ucfirst($op);

    // Visit the edit page first to generate the tab.
    $this
      ->drupalGet("node/{$nid}");
    $view_response = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
    if ($view_response != 200) {
      $msg .= t('Could not view the node. Response code: @response', array(
        '@response' => $view_response,
      ));
      $this
        ->assert($expected_response == $view_response, $msg);
      return;
    }

    // Check the tab exists.
    $links = $this
      ->xpath('//a[normalize-space(text())=:label]', array(
      ':label' => $tab_link_text,
    ));
    if (!isset($links[0])) {

      // No tab.
      $msg .= t('Could not find a tab called @tab', array(
        '@tab' => $tab_link_text,
      ));
      $this
        ->assert($expected_response != 200, $msg);
      return;
    }

    // Now visit the tab.
    $this
      ->clickLink($tab_link_text);

    // Submit the confirmation form.
    $this
      ->drupalPost($this->url, array(), 'Confirm');
    $this
      ->assertResponse(200);
    $node = node_load($nid);
    if ($op == 'publish') {
      $this
        ->assertText(_publishcontent_get_message($node->nid, $node->title, TRUE), 'Publish content message is visible.');
    }
    else {
      $this
        ->assertText(_publishcontent_get_message($node->nid, $node->title, FALSE), 'Unpublish message is visible.');
    }
  }
  else {
    $url = $op == 'view' ? "node/{$nid}" : "node/{$nid}/{$op}";
    $this
      ->drupalGet($url);
  }
  $this
    ->assertResponse($expected_response, $msg);
}