You are here

public function RestfulVariableTestCase::testCrudOperations in RESTful 7.2

Same name and namespace in other branches
  1. 7 tests/RestfulVariableTestCase.test \RestfulVariableTestCase::testCrudOperations()

Test authenticating a user.

File

tests/RestfulVariableTestCase.test, line 33
Contains RestfulVariableTestCase.

Class

RestfulVariableTestCase

Code

public function testCrudOperations() {

  // Set up random content and resource handler.
  $random_string = $this
    ->randomName();
  $handler = restful()
    ->getResourceManager()
    ->getPlugin('variables:1.0');
  $formatter = restful()
    ->getFormatterManager()
    ->negotiateFormatter(NULL, 'json');
  $formatter
    ->setResource($handler);

  // Populate the test environment with variables.
  $random_numbers = array();
  for ($i = 0; $i < 6; $i++) {
    $random_numbers[] = intval(mt_rand(1, 100));
    variable_set('variable_' . $i, array(
      'test_data' => $random_numbers[$i],
    ));
  }
  $this
    ->assertTrue(variable_get('variable_5'), 'The variables have been set.');

  // Testing read.
  $results = $formatter
    ->prepare($handler
    ->doGet('variable_5'));
  $expected = array(
    'test_data' => $random_numbers[5],
  );
  $this
    ->assertEqual($results['data'][0]['variable_name'], 'variable_5', 'The variable name was successfully retrieved.');
  $this
    ->assertEqual($results['data'][0]['variable_value'], $expected, 'The variable value was successfully retrieved.');

  // Testing read context listing.
  $results = $formatter
    ->prepare($handler
    ->doGet());
  $in_results = FALSE;
  foreach ($results['data'] as $result) {
    if ($result['variable_name'] == 'variable_5') {
      $in_results = TRUE;
    }
  }
  $this
    ->assertTrue($in_results, 'All the content listed successfully.');

  // Testing sort for read context.
  // Set a variable that will probably sort last.
  variable_set('zzzzz', 'some value');

  // Find the last variable name, which will probably be the one we just set.
  $query = array(
    'sort' => '-variable_name',
  );
  $results = $formatter
    ->prepare($handler
    ->doGet('', $query));
  $last_variable_name = $results['data'][0]['variable_name'];

  // Generate a variable name that will always sort last.
  $new_variable_name = 'zzz';
  while (strcmp($new_variable_name, $last_variable_name) <= 0) {
    $new_variable_name .= 'z';
  }
  variable_set($new_variable_name, array(
    'key' => $random_string,
  ));
  $query = array(
    'sort' => '-variable_name',
  );
  $results = $formatter
    ->prepare($handler
    ->doGet('', $query));
  $expected = array(
    'variable_name' => $new_variable_name,
    'variable_value' => array(
      'key' => $random_string,
    ),
  );
  $this
    ->assertEqual($results['data'][0], $expected, 'List is sorted correctly.');

  // Testing create.
  $parsed_body = array(
    'variable_name' => 'created_variable',
    'variable_value' => $random_string,
  );
  $handler
    ->doPost($parsed_body);
  $results = $formatter
    ->prepare($handler
    ->doGet('created_variable'));
  $this
    ->assertEqual($results['data'][0]['variable_name'], 'created_variable', 'The variable was created.');
  $this
    ->assertEqual($results['data'][0]['variable_value'], $random_string, 'The created variable value is present.');

  // Testing update.
  $parsed_body = array(
    'variable_name' => 'created_variable',
  );
  $handler
    ->doPatch('created_variable', $parsed_body);
  $results = $formatter
    ->prepare($handler
    ->doGet('created_variable'));

  // Fields that are not supplied should not be updated.
  $this
    ->assertEqual($results['data'][0]['variable_value'], $random_string, 'The variable value was not updated.');

  // Testing replace.
  $handler
    ->doPut('created_variable', $parsed_body);
  $results = $formatter
    ->prepare($handler
    ->doGet('created_variable'));

  // Fields that are not supplied should be NULL.
  $this
    ->assertFalse($results['data'][0]['variable_value'], 'The variable value was removed.');

  // Testing delete.
  $handler
    ->doDelete('created_variable');
  $deleted = !variable_get('created_variable');
  $this
    ->assertTrue($deleted);
}