You are here

services_views_upgrade_path.test in Services Views 7

Security upgrade tests for the Services Views module.

File

tests/services_views_upgrade_path.test
View source
<?php

/**
 * @file
 * Security upgrade tests for the Services Views module.
 */

/**
 * Api call tests for the security upgrade from 1.2 to 1.3.
 */
class ServicesViewsSecurityUpgradePathTest extends ServicesViewsApiTestCase {

  /**
   * Privledged user used to hit the api.
   *
   * @var stdobj
   */
  protected $priviledgedUser = NULL;

  /**
   * Services endpoint.
   *
   * @var stdobj
   */
  protected $endpoint = NULL;

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Services Views Security Upgrade Path Test',
      'description' => 'Tests the security upgrade path from 1.2 to 1.3',
      'group' => 'Services Views',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp(array $modules = array()) {
    parent::setUp($modules);
    $this->endpoint = $this
      ->saveNewEndpoint();
    $this->priviledgedUser = $this
      ->drupalCreateUser(array(
      'administer services',
      'perform unlimited index queries',
      'view own unpublished content',
    ));
    $this
      ->drupalLogin($this->priviledgedUser);
  }

  /**
   * Test the upgraded clone to see if it produces the same output.
   */
  public function testClone() {
    $alphabet = range('A', 'Z');
    $nodes = array();
    $count = 5;
    $results = array();

    // TODO: set view as whitelisted.
    for ($i = 0; $i < $count; $i++) {
      $node = $this
        ->drupalCreateNode(array(
        'title' => $alphabet[$i],
      ));
      $nodes[$node->nid] = $node;
      $results[] = array(
        'title' => $node->title,
        'nid' => $node->nid,
      );
    }
    $view = views_get_view('services_views_sorted_nodes');
    $this
      ->executeView($view, 'page');
    $this
      ->assertIdenticalResultset($view, $results, array(
      'node_title' => 'title',
      'nid' => 'nid',
    ));
    $new_display_name = services_views_clone_display($view, 'page');
    $new_display = $view->display[$new_display_name];
    $this
      ->verbose('<pre>' . print_r($new_display, TRUE) . '</pre>');
    $this
      ->assertTrue($new_display->display_options['path'] == 'services_views_sorted_nodes/page');
    $this
      ->assertTrue($new_display->display_options['access']['type'] == 'perm' && $new_display->display_options['access']['perm'] == 'view own unpublished content');
    $this
      ->executeView($view, $new_display_name);
    $this
      ->assertIdenticalResultset($view, $results, array(
      'node_title' => 'title',
      'nid' => 'nid',
    ));

    // Test the new display to make sure it works.
    $responseArray = $this
      ->servicesGet($this->endpoint->path . '/views/services_views_sorted_nodes', array(
      'display_id' => $new_display_name,
    ));
    $api_results = json_decode(json_encode($responseArray['body']), TRUE);
    $results = array(
      array(
        'node_title' => 'A',
        'nid' => '1',
      ),
      array(
        'node_title' => 'B',
        'nid' => '2',
      ),
      array(
        'node_title' => 'C',
        'nid' => '3',
      ),
      array(
        'node_title' => 'D',
        'nid' => '4',
      ),
      array(
        'node_title' => 'E',
        'nid' => '5',
      ),
    );
    $this
      ->assertEqual($results, $api_results);

    // Test the old display name to make sure backward compatibility works.
    $responseArray = $this
      ->servicesGet($this->endpoint->path . '/views/services_views_sorted_nodes', array(
      'display_id' => $new_display_name,
    ));
    $api_results = json_decode(json_encode($responseArray['body']), TRUE);
    $this
      ->assertEqual($results, $api_results);

    // TODO: Test that the custom resource and views resource output the same
    // thing. Currently this isn't the case on 9-3-17. @see #1647360.
  }

  /**
   * Test cloning the default display.
   */
  public function testCloneDefault() {
    $view = views_get_view('services_views_sorted_nodes');
    $new_display_name = services_views_clone_display($view, 'default');
    $this
      ->assertEqual($new_display_name, 'services_clone_default');
    $this
      ->assertEqual($view->display[$new_display_name]->display_plugin, 'services');
  }

  /**
   * Test the upgrade complains when no displays are blacklisted.
   */
  public function testCloneUpgradeComplain() {
    $prefix = "services_views_" . $this->endpoint->name;
    variable_set($prefix . '_white_list', 0);
    variable_set($prefix . "_view_displays", array());
    services_views_convert_white_list_to_clones();
    $this
      ->assertEqual($_SESSION['messages']['warning'][0], 'Cowardly refusing to make a clone of all view displays. Please configure the specific views you would like to have cloned in the ' . $this->endpoint->name . ' endpoint. There will be no new views cloned over to a Services display. If all displays need to be cloned, restore to the latest backup, and update your settings white list them all and try again.');
  }

  /**
   * Test the upgrade clones displays not black listed into Services displays.
   */
  public function testCloneBlacklisted() {
    $prefix = "services_views_" . $this->endpoint->name;
    variable_set($prefix . '_white_list', 0);
    variable_set($prefix . "_view_displays", array(
      'services_views_sorted_nodes|default' => '0',
      'services_views_sorted_nodes|page' => '0',
      'services_views_sorted_nodes|block_1' => 'services_views_sorted_nodes|block_1',
      'services_views_sorted_nodes|services_1' => '0',
    ));
    services_views_convert_white_list_to_clones();

    // Should disregard Services type displays but clone others.
    $display_exceptions = variable_get($prefix . "_view_displays", NULL);
    $this
      ->assertEqual($display_exceptions, array(
      'services_views_sorted_nodes|default' => 'services_views_sorted_nodes|default',
      'services_views_sorted_nodes|page' => 'services_views_sorted_nodes|page',
      'services_views_sorted_nodes|block_1' => 'services_views_sorted_nodes|block_1',
      'services_views_sorted_nodes|services_1' => '0',
      'services_views_sorted_nodes|services_clone_default' => '0',
      'services_views_sorted_nodes|services_clone_page' => '0',
    ));
    $view = views_get_view('services_views_sorted_nodes', TRUE);
    $this
      ->assertEqual(array_keys($view->display), array(
      'default',
      'page',
      'block_1',
      'services_1',
      'services_clone_default',
      'services_clone_page',
    ));
    $this
      ->assertEqual($view->display['services_clone_default']->display_plugin, 'services');
    $this
      ->assertEqual($view->display['services_clone_page']->display_plugin, 'services');
  }

  /**
   * Test the upgrade creates only 1 display when master matches other displays.
   */
  public function testCloneWhitelisted() {
    $prefix = "services_views_" . $this->endpoint->name;
    variable_set($prefix . '_white_list', 1);
    variable_set($prefix . "_view_displays", array(
      'services_views_sorted_nodes|default' => 'services_views_sorted_nodes|default',
      'services_views_sorted_nodes|page' => 'services_views_sorted_nodes|page',
      'services_views_sorted_nodes|block_1' => '0',
      'services_views_sorted_nodes|services_1' => 'services_views_sorted_nodes|services_1',
    ));
    services_views_convert_white_list_to_clones();

    // Should disregard Services type displays but clone others.
    $display_exceptions = variable_get($prefix . "_view_displays", NULL);
    $this
      ->assertEqual($display_exceptions, array(
      'services_views_sorted_nodes|default' => '0',
      'services_views_sorted_nodes|page' => '0',
      'services_views_sorted_nodes|block_1' => '0',
      'services_views_sorted_nodes|services_1' => 'services_views_sorted_nodes|services_1',
      'services_views_sorted_nodes|services_clone_default' => 'services_views_sorted_nodes|services_clone_default',
      'services_views_sorted_nodes|services_clone_page' => 'services_views_sorted_nodes|services_clone_page',
    ));
    $view = views_get_view('services_views_sorted_nodes', TRUE);
    $this
      ->assertEqual(array_keys($view->display), array(
      'default',
      'page',
      'block_1',
      'services_1',
      'services_clone_default',
      'services_clone_page',
    ));
    $this
      ->assertEqual($view->display['services_clone_default']->display_plugin, 'services');
    $this
      ->assertEqual($view->display['services_clone_page']->display_plugin, 'services');
  }

  /**
   * Insure path backward compatibility when views resource is enabled.
   */
  function testViewsResourceBackwardCompatibility() {
    $alphabet = range('A', 'Z');
    $nodes = array();
    $count = 5;
    $results = array();

    // TODO: set view as whitelisted.
    for ($i = 0; $i < $count; $i++) {
      $node = $this
        ->drupalCreateNode(array(
        'title' => $alphabet[$i],
      ));
      $nodes[$node->nid] = $node;
      $results[] = array(
        'title' => $node->title,
        'nid' => $node->nid,
      );
    }
    $prefix = "services_views_" . $this->endpoint->name;
    variable_set($prefix . '_white_list', 1);
    variable_set($prefix . "_view_displays", array(
      'services_views_sorted_nodes|default' => '0',
      'services_views_sorted_nodes|page' => 'services_views_sorted_nodes|page',
      'services_views_sorted_nodes|block_1' => '0',
      'services_views_sorted_nodes|services_1' => '0',
    ));
    services_views_convert_white_list_to_clones();

    // Test the old display to make sure it works.
    $responseArray = $this
      ->servicesGet($this->endpoint->path . '/views/services_views_sorted_nodes', array(
      'display_id' => 'services_clone_page',
    ));
    $api_results = json_decode(json_encode($responseArray['body']), TRUE);
    $results = array(
      array(
        'node_title' => 'A',
        'nid' => '1',
      ),
      array(
        'node_title' => 'B',
        'nid' => '2',
      ),
      array(
        'node_title' => 'C',
        'nid' => '3',
      ),
      array(
        'node_title' => 'D',
        'nid' => '4',
      ),
      array(
        'node_title' => 'E',
        'nid' => '5',
      ),
    );
    $this
      ->assertEqual($results, $api_results);
    $responseArray = $this
      ->servicesGet($this->endpoint->path . '/views/services_views_sorted_nodes', array(
      'display_id' => 'page',
    ));
    $api_results = json_decode(json_encode($responseArray['body']), TRUE);
    $results = array(
      array(
        'node_title' => 'A',
        'nid' => '1',
      ),
      array(
        'node_title' => 'B',
        'nid' => '2',
      ),
      array(
        'node_title' => 'C',
        'nid' => '3',
      ),
      array(
        'node_title' => 'D',
        'nid' => '4',
      ),
      array(
        'node_title' => 'E',
        'nid' => '5',
      ),
    );
    $this
      ->assertEqual($results, $api_results);
  }

}

Classes

Namesort descending Description
ServicesViewsSecurityUpgradePathTest Api call tests for the security upgrade from 1.2 to 1.3.