You are here

function RestfulDbQueryTestCase::testCrudOperations in RESTful 7

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

Test authenticating a user.

File

tests/RestfulDbQueryTestCase.test, line 38
Contains RestfulDbQueryTestCase.

Class

RestfulDbQueryTestCase
@file Contains RestfulDbQueryTestCase.

Code

function testCrudOperations() {
  $randomInt = intval(mt_rand(1, 100));
  $randomString = $this
    ->randomName();
  $randomSerialized = serialize(array(
    'key1' => $randomInt,
    'key2' => $randomString,
  ));

  // Populate the table with some values.
  $mock_data = array(
    'str_field' => $randomString,
    'int_field' => $randomInt,
    'serialized_field' => $randomSerialized,
  );
  $id = db_insert($this->tableName)
    ->fields($mock_data)
    ->execute();
  $id = intval($id);
  $this
    ->assertTrue(!empty($id), 'The manual record could be inserted');

  // Get the handler.
  $handler = restful_get_restful_handler('db_query_test');

  // Testing read context.
  $result = $handler
    ->get($id);
  $result = $result[0];
  $this
    ->assertEqual($result['string'], $mock_data['str_field'], 'The record was retrieved successfully.');
  $this
    ->assertEqual($result['integer'], $mock_data['int_field'], 'The record was retrieved successfully.');
  $this
    ->assertEqual($result['serialized'], $mock_data['serialized_field'], 'The record was retrieved successfully.');

  // Testing update context.
  $mock_data2 = array(
    'string' => $this
      ->randomName(),
  );
  $handler
    ->patch($id, $mock_data2);
  $result = $handler
    ->get($id);
  $expected = array(
    // ID should be unchanged.
    'id' => $id,
    // String should be the string that we updated.
    'string' => $mock_data2['string'],
    // Serialized value should be unchanged.
    'serialized' => $randomSerialized,
    // Integer value should be unchanged.
    'integer' => $randomInt,
  );

  // We expect that only the string field has changed.
  $this
    ->assertEqual($result[0], $expected, 'The record was updated with PUT successfully.');

  // Testing replace context.
  $mock_data3 = array(
    'string' => $this
      ->randomName(),
  );
  $handler
    ->put($id, $mock_data3);
  $result = $handler
    ->get($id);
  $expected = array(
    // ID should be unchanged.
    'id' => $id,
    // String should be the string that we PUT.
    'string' => $mock_data3['string'],
    // Serialized field should be null.
    'serialized' => 'N;',
    // Integer field should be default value from schema.
    'integer' => 0,
  );

  // We expect that only the supplied fields are present.
  $this
    ->assertEqual($result[0], $expected, 'The record was updated with PATCH successfully.');

  // Testing delete context.
  $handler
    ->delete($id);
  $count = db_select($this->tableName)
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($count, 0, 'The record was deleted successfully.');

  // Testing create context.
  $mock_data4 = array(
    'string' => $randomString,
    'integer' => $randomInt,
    'serialized' => array(
      'key1' => $randomInt,
      'key2' => $randomString,
    ),
  );
  $handler
    ->post('', $mock_data4);
  $count = db_select($this->tableName)
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($count, 1, 'The record was created.');

  // Testing listing for read context.
  $result = $handler
    ->get();

  // The created record should match our input.
  $expected = $mock_data4;

  // Account for serialization.
  $expected['serialized'] = $randomSerialized;

  // Account for not knowing the ID of the new entity beforehand.
  unset($result[0]['id']);
  $this
    ->assertEqual($result[0], $expected, 'All the content listed successfully.');

  // Testing filters.
  $mock_data5 = array(
    'str_field' => $this
      ->randomName(),
    'int_field' => 101,
  );
  $mock_data5['serialized_field'] = serialize($mock_data5);
  db_insert($this->tableName)
    ->fields($mock_data5)
    ->execute();
  $mock_data6 = array(
    'str_field' => $this
      ->randomName(),
    'int_field' => 102,
  );
  $mock_data6['serialized_field'] = serialize($mock_data6);
  db_insert($this->tableName)
    ->fields($mock_data6)
    ->execute();
  $request = array(
    'filter' => array(
      'integer' => array(
        'value' => array(
          101,
          102,
        ),
        'conjunction' => 'OR',
      ),
    ),
  );
  $result = $handler
    ->get('', $request);
  $this
    ->assertEqual(count($result), 2);
  $request = array(
    'filter' => array(
      'integer' => array(
        'value' => array(
          101,
          102,
        ),
        'operator' => array(
          '=',
          '>=',
        ),
        'conjunction' => 'OR',
      ),
    ),
  );
  $result = $handler
    ->get('', $request);
  $this
    ->assertEqual(count($result), 2);
}