1.12.x comment.test CommentInterfaceTest::setEnvironment(array $info)
1.10.x comment.test CommentInterfaceTest::setEnvironment(array $info)
1.11.x comment.test CommentInterfaceTest::setEnvironment(array $info)
1.7.x comment.test CommentInterfaceTest::setEnvironment(array $info)
1.8.x comment.test CommentInterfaceTest::setEnvironment(array $info)
1.9.x comment.test CommentInterfaceTest::setEnvironment(array $info)

Re-configures the environment, module settings, and user permissions.

Parameters

$info: An associative array describing the environment to setup:

File

modules/comment/tests/comment.test, line 783
Tests for the Comment module.

Class

CommentInterfaceTest

Code

function setEnvironment(array $info) {
  static $current;

  // Apply defaults to initial environment.
  if (!isset($current)) {
    $current = array(
      'authenticated' => FALSE,
      'comment count' => FALSE,
      'form' => COMMENT_FORM_BELOW,
      'user_register' => USER_REGISTER_VISITORS,
      'contact' => COMMENT_ANONYMOUS_MAY_CONTACT,
      'comments' => COMMENT_NODE_OPEN,
      'access comments' => 0,
      'post comments' => 0,
      // Enabled by default, because it's irrelevant for this test.
      'skip comment approval' => 1,
      'edit own comments' => 0,
    );
  }
  // Complete new environment with current environment.
  $info = array_merge($current, $info);

  // Change environment conditions.
  if ($current['authenticated'] != $info['authenticated']) {
    if ($this->loggedInUser) {
      $this->backdropLogout();
    }
    else {
      $this->backdropLogin($this->web_user);
    }
  }
  if ($current['comment count'] != $info['comment count']) {
    if ($info['comment count']) {
      // Create a comment via CRUD API functionality, since
      // $this->postComment() relies on actual user permissions.
      $comment = entity_create('comment', array(
        'cid' => NULL,
        'nid' => $this->node->nid,
        'node_type' => $this->node->type,
        'pid' => 0,
        'uid' => 0,
        'status' => COMMENT_PUBLISHED,
        'subject' => $this->randomName(),
        'hostname' => ip_address(),
        'langcode' => LANGUAGE_NONE,
        'comment_body' => array(LANGUAGE_NONE => array($this->randomName())),
      ));
      comment_save($comment);
      $this->comment = $comment;

      // comment_num_new() relies on node_last_viewed(), so ensure that no one
      // has seen the node of this comment.
      db_delete('history')->condition('nid', $this->node->nid)->execute();
    }
    else {
      $cids = db_query("SELECT cid FROM {comment}")->fetchCol();
      comment_delete_multiple($cids);
      unset($this->comment);
    }
  }

  // Change comment settings.
  $node_type = node_type_get_type($this->node->type);
  $node_type->settings['comment_form_location'] = $info['form'];
  $node_type->settings['comment_anonymous'] = $info['contact'];
  node_type_save($node_type);
  if ($this->node->comment != $info['comments']) {
    $this->node->comment = $info['comments'];
    node_save($this->node);
  }

  // Change user settings.
  config_set('system.core', 'user_register', $info['user_register']);

  // Change user permissions.
  $role_name = ($this->loggedInUser ? BACKDROP_AUTHENTICATED_ROLE : BACKDROP_ANONYMOUS_ROLE);
  $perms = array_intersect_key($info, array('access comments' => 1, 'post comments' => 1, 'skip comment approval' => 1, 'edit own comments' => 1));
  user_role_change_permissions($role_name, $perms);

  // Output verbose debugging information.
  // @see BackdropTestCase::error()
  $t_form = array(
    COMMENT_FORM_BELOW => 'below',
    COMMENT_FORM_SEPARATE_PAGE => 'separate page',
  );
  $t_contact = array(
    COMMENT_ANONYMOUS_MAY_CONTACT => 'optional',
    COMMENT_ANONYMOUS_MAYNOT_CONTACT => 'disabled',
    COMMENT_ANONYMOUS_MUST_CONTACT => 'required',
  );
  $t_comments = array(
    COMMENT_NODE_OPEN => 'open',
    COMMENT_NODE_CLOSED => 'closed',
    COMMENT_NODE_HIDDEN => 'hidden',
  );
  $verbose = $info;
  $verbose['form'] = $t_form[$info['form']];
  $verbose['contact'] = $t_contact[$info['contact']];
  $verbose['comments'] = $t_comments[$info['comments']];
  $message = t('Changed environment:<pre>@verbose</pre>', array(
    '@verbose' => var_export($verbose, TRUE),
  ));
  $this->assert('debug', $message, 'Debug');

  // Update current environment.
  $current = $info;

  return $info;
}