You are here

public function PublishContentButtonTests::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 expected 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 611
Unit tests for Publish Content module.

Class

PublishContentButtonTests
Test permissions with the button method.

Code

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

    // Visit the edit page first to generate the tab.
    $this
      ->drupalGet("node/{$nid}/edit");
    $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;
    }

    // Find the button.
    $buttons = $this
      ->xpath('//input[@value=:label]', array(
      ':label' => $button_text,
    ));
    if (!isset($buttons[0])) {

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

    // Submit the form.
    $this
      ->drupalPost("node/{$nid}/edit", array(), $button_text);
  }
  else {
    $url = $op == 'view' ? "node/{$nid}" : "node/{$nid}/{$op}";
    $this
      ->drupalGet($url);
  }
  $this
    ->assertResponse($expected_response, $msg);
}