ReadTest.php 4.76 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Definition of Drupal\rest\test\ReadTest.
 */

namespace Drupal\rest\Tests;

10
use Drupal\Core\Language\Language;
11
12
13
14
15
16
17
18
19
20
21
22
use Drupal\rest\Tests\RESTTestBase;

/**
 * Tests resource read operations on test entities, nodes and users.
 */
class ReadTest extends RESTTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
23
  public static $modules = array('hal', 'rest', 'entity_test');
24
25
26
27
28
29
30
31
32
33
34
35
36

  public static function getInfo() {
    return array(
      'name' => 'Read resource',
      'description' => 'Tests the retrieval of resources.',
      'group' => 'REST',
    );
  }

  /**
   * Tests several valid and invalid read requests on all entity types.
   */
  public function testRead() {
37
    // @todo once EntityNG is implemented for other entity types expand this at
38
    // least to users.
39
    // Define the entity types we want to test.
40
    $entity_types = array('entity_test', 'node');
41
    foreach ($entity_types as $entity_type) {
42
      $this->enableService('entity:' . $entity_type, 'GET');
43
      // Create a user account that has the required permissions to read
44
      // resources via the REST API.
45
46
47
      $permissions = $this->entityPermissions($entity_type, 'view');
      $permissions[] = 'restful get entity:' . $entity_type;
      $account = $this->drupalCreateUser($permissions);
48
49
50
51
52
      $this->drupalLogin($account);

      // Create an entity programmatically.
      $entity = $this->entityCreate($entity_type);
      $entity->save();
53
      // Read it over the REST API.
54
      $response = $this->httpRequest('entity/' . $entity_type . '/' . $entity->id(), 'GET', NULL, $this->defaultMimeType);
55
      $this->assertResponse('200', 'HTTP response code is correct.');
56
      $this->assertHeader('content-type', $this->defaultMimeType);
57
58
59
      $data = drupal_json_decode($response);
      // Only assert one example property here, other properties should be
      // checked in serialization tests.
60
      $this->assertEqual($data['uuid'][0]['value'], $entity->uuid(), 'Entity UUID is correct');
61

62
      // Try to read the entity with an unsupported mime format.
63
      $response = $this->httpRequest('entity/' . $entity_type . '/' . $entity->id(), 'GET', NULL, 'application/wrongformat');
64
      $this->assertResponse(406);
65

66
      // Try to read an entity that does not exist.
67
      $response = $this->httpRequest('entity/' . $entity_type . '/9999', 'GET', NULL, $this->defaultMimeType);
68
      $this->assertResponse(404);
69
70
      $decoded = drupal_json_decode($response);
      $this->assertEqual($decoded['error'], 'Entity with ID 9999 not found', 'Response message is correct.');
71

72
      // Make sure that field level access works and that the according field is
73
      // not available in the response. Only applies to entity_test.
74
      // @see entity_test_entity_field_access()
75
76
77
78
79
80
81
82
83
      if ($entity_type == 'entity_test') {
        $entity->field_test_text->value = 'no access value';
        $entity->save();
        $response = $this->httpRequest('entity/' . $entity_type . '/' . $entity->id(), 'GET', NULL, $this->defaultMimeType);
        $this->assertResponse(200);
        $this->assertHeader('content-type', $this->defaultMimeType);
        $data = drupal_json_decode($response);
        $this->assertFalse(isset($data['field_test_text']), 'Field access protected field is not visible in the response.');
      }
84

85
86
      // Try to read an entity without proper permissions.
      $this->drupalLogout();
87
      $response = $this->httpRequest('entity/' . $entity_type . '/' . $entity->id(), 'GET', NULL, $this->defaultMimeType);
88
89
90
      $this->assertResponse(403);
      $this->assertNull(drupal_json_decode($response), 'No valid JSON found.');
    }
91
    // Try to read a resource which is not REST API enabled.
92
93
    $account = $this->drupalCreateUser();
    $this->drupalLogin($account);
94
    $response = $this->httpRequest('entity/user/' . $account->id(), 'GET', NULL, $this->defaultMimeType);
95
96
97
    $this->assertResponse(404);
    $this->assertNull(drupal_json_decode($response), 'No valid JSON found.');
  }
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

  /**
   * Tests the resource structure.
   */
  public function testResourceStructure() {
    // Enable a service with a format restriction but no authentication.
    $this->enableService('entity:node', 'GET', 'json');
    // Create a user account that has the required permissions to read
    // resources via the REST API.
    $permissions = $this->entityPermissions('node', 'view');
    $permissions[] = 'restful get entity:node';
    $account = $this->drupalCreateUser($permissions);
    $this->drupalLogin($account);

    // Create an entity programmatically.
    $entity = $this->entityCreate('node');
    $entity->save();

    // Read it over the REST API.
    $response = $this->httpRequest('entity/node/' . $entity->id(), 'GET', NULL, 'application/json');
    $this->assertResponse('200', 'HTTP response code is correct.');
  }

121
}