You are here

RestfulDbQueryTestCase.test in RESTful 7

Same filename and directory in other branches
  1. 7.2 tests/RestfulDbQueryTestCase.test

Contains RestfulDbQueryTestCase.

File

tests/RestfulDbQueryTestCase.test
View source
<?php

/**
 * @file
 * Contains RestfulDbQueryTestCase.
 */
class RestfulDbQueryTestCase extends DrupalWebTestCase {

  /**
   * The name of the test table.
   *
   * @var string
   */
  protected $tableName = 'restful_test_db_query';

  /**
   * Provides information about the test class.
   */
  public static function getInfo() {
    return array(
      'name' => 'DB Query',
      'description' => 'Test the DB Query data provider.',
      'group' => 'RESTful',
    );
  }

  /**
   * Operations before the testing begins.
   */
  function setUp() {
    parent::setUp('restful_test');
  }

  /**
   * Test authenticating a user.
   */
  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);
  }

  /**
   * Test the render cache.
   */
  public function testRenderCache() {
    $account = $this
      ->drupalCreateUser();

    // Populate the table with some values.
    $mock_data = array(
      'str_field' => $this
        ->randomName(),
      'int_field' => intval(mt_rand(1, 100)),
    );
    $mock_data['serialized_field'] = serialize($mock_data);
    $id = db_insert($this->tableName)
      ->fields($mock_data)
      ->execute();
    $id = intval($id);

    // Get the handler.
    $handler = restful_get_restful_handler('db_query_test');
    $handler
      ->setAccount($account);
    $cache = $handler
      ->getCacheController();

    // Populate the cache entry.
    $handler
      ->get($id);
    $version = $handler
      ->getVersion();
    $cid = 'v' . $version['major'] . '.' . $version['minor'] . '::db_query_test::uu' . $account->uid . '::patb:restful_test_db_query::cl:id::id:' . $id;
    $cache_data = $cache
      ->get($cid);
    $this
      ->assertNotNull($cache_data->data, 'Cache data is present.');
    $this
      ->assertEqual($cache_data->data[0]['string'], $mock_data['str_field'], 'The record was retrieved successfully.');
    $this
      ->assertEqual($cache_data->data[0]['integer'], $mock_data['int_field'], 'The record was retrieved successfully.');
    $this
      ->assertEqual($cache_data->data[0]['serialized'], $mock_data['serialized_field'], 'The record was retrieved successfully.');
  }

  /**
   * Test using joins and filters for the DB query data provider.
   */
  public function testColumnForQuery() {
    $user1 = $this
      ->drupalCreateUser();
    $user2 = $this
      ->drupalCreateUser();
    $settings = array(
      'type' => 'article',
      'uid' => $user1->uid,
    );
    $node1 = $this
      ->drupalCreateNode($settings);
    $settings['uid'] = $user2->uid;
    $node2 = $this
      ->drupalCreateNode($settings);
    $handler = restful_get_restful_handler('node_user');
    $request = array(
      'filter' => array(
        'author' => $user1->name,
      ),
    );
    $result = $handler
      ->get('', $request);
    $this
      ->assertEqual(count($result), 1, 'Filter on a joined table returned correct items.');
    $this
      ->assertEqual($result[0]['author'], $user1->name, 'Joined column appears correctly.');
    $this
      ->assertEqual($result[0]['id'], $node1->nid, 'Correct item was filtered.');
  }

}

Classes

Namesort descending Description
RestfulDbQueryTestCase @file Contains RestfulDbQueryTestCase.