<?php

/**
 * @file
 * Tests for Userpoints Services integration.
 */

// Avoid issues when the ServicesWebTestCase does not exist.
if (!class_exists('ServicesWebTestCase')) {
  return;
}

class UserpointsServiceTestCase extends ServicesWebTestCase {
  /**
   * Class variables.
   */
  protected $privilegedUser = NULL;
  /**
   * Endpoint details.
   */
  protected $endpoint = NULL;

  /**
   * Implements getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => t('Services integration'),
      'description' => t('Tests the services resource userpoints and actions'),
      'group' => t('Userpoints'),
    );
  }

  /**
   * Implementation of setUp().
   */
  public function setUp() {
    parent::setUp(array('ctools', 'services', 'rest_server', 'userpoints', 'userpoints_service'));
    // Set up endpoint.
    $this->endpoint = $this->saveNewEndpoint();
    // Extend endpoint with userpoints resources.
    $this->endpoint->resources += array(
      'userpoints' => array(
        'alias' => '',
        'operations' => array(
          'retrieve' => array(
            'enabled' => 1,
          ),
          'index' => array(
            'enabled' => 1,
          ),
        ),
        'actions' => array(
          'add' => array(
            'enabled' => 1,
          ),
        ),
      ),
    );

    services_endpoint_save($this->endpoint);
  }

  /**
   * Basic tests for granting and retreiving points through a service.
   */
  public function testAddRetrievePoints() {
    // Create and log in our privileged user.
    $this->privilegedUser = $this->drupalCreateUser(array('view userpoints', 'administer userpoints'));
    $this->drupalLogin($this->privilegedUser);

    $normal_user = $this->drupalCreateUser(array());

    $total = 0;
    for ($i = 0; $i < 3; $i++) {
      $points = rand(-50, 50);
      $params = array(
        'uid' => $normal_user->uid,
        'points' => $points,
      );
      $this->servicesPost($this->endpoint->path . '/userpoints/add', $params);
      $total += $points;
    }

    $result = $this->servicesGet($this->endpoint->path . '/userpoints/' . $normal_user->uid);
    $this->assertEqual($total, $result['body']);

    // Give the admin user some points too.
    $points = rand(-50, 50);
    $params = array(
      'uid' => $this->privilegedUser->uid,
      'points' => $points,
    );
    $this->servicesPost($this->endpoint->path . '/userpoints/add', $params);

    $result = $this->servicesGet($this->endpoint->path . '/userpoints');
    $index = $result['body'];
    $this->assertEqual($index[0]->points, userpoints_get_current_points($index[0]->uid));
    $this->assertEqual($index[1]->points, userpoints_get_current_points($index[1]->uid));
    $this->assertEqual($index[0]->max_points, userpoints_get_max_points($index[0]->uid));
    $this->assertEqual($index[1]->max_points, userpoints_get_max_points($index[1]->uid));
  }
}