summaryrefslogtreecommitdiffstats
path: root/lib/Drupal/services/Tests/AnnotationResourceExampleTest.php
blob: c982e5805bc14dbd792be30989e24712be8297ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php

namespace Drupal\services\Tests;

use Drupal\rest\Tests\RESTTestBase;

/**
 * Tests the Watchdog resource to retrieve log messages.
 */
class AnnotationResourceExampleTest extends RESTTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('hal', 'rest', 'services');

  public static function getInfo() {
    return array(
      'name' => 'Annotation Example resource',
      'description' => 'Tests the annotation resource.',
      'group' => 'Services',
    );
  }

  public function setUp() {
    parent::setUp();
    $this->enableService('annotationExample');
  }

  /**
   * Tests rest calls.
   */
  public function testRESTCalls() {
    // Create a user account that has the required permissions to read
    // the watchdog resource via the REST API.
    $account = $this->drupalCreateUser();
    $this->drupalLogin($account);

    $response = $this->httpRequest('exampleGetCall', 'GET', NULL, $this->defaultMimeType);
    $this->assertResponse(200);
    $this->assertHeader('content-type', $this->defaultMimeType);
    $decoded_response = drupal_json_decode($response);
    $decoded_expected = array('message' => 'Hello World!');
    $this->assertIdentical($decoded_expected, $decoded_response, 'exampleGetCall response expected');

    $response = $this->httpRequest('examplePostCall', 'POST', NULL, $this->defaultMimeType);
    $decoded_response = drupal_json_decode($response);
    $decoded_expected = array('message' => array('POST call'));
    $this->assertIdentical($decoded_expected, $decoded_response, 'examplePostCall response expected');

    $arg1 = $this->randomName();
    $arg2 = $this->randomName();
    $response = $this->httpRequest("getCallArguments/$arg1/$arg2", 'GET', NULL, $this->defaultMimeType);
    $decoded_response = drupal_json_decode($response);
    $decoded_expected = array('arg1' => $arg1, 'arg2' => $arg2);
    $this->assertIdentical($decoded_expected, $decoded_response, 'getCallArguments response expected');

    $body = json_encode($decoded_expected);
    $response = $this->httpRequest("postCallArguments", 'POST', $body, $this->defaultMimeType);
    $decoded_response = drupal_json_decode($response);
    $this->assertIdentical($decoded_response, $decoded_expected, 'postCallArguments response is correct');
  }
}