You are here

public function RestfulJsonApiTestCase::testSubRequestParsing in RESTful 7.2

Sub-request parsing.

File

tests/RestfulJsonApiTestCase.test, line 703
Contains \RestfulJsonApiTestCase.

Class

RestfulJsonApiTestCase
Class RestfulJsonApiTestCase.

Code

public function testSubRequestParsing() {
  $relationships = array(
    'rel1' => array(
      'data' => array(
        'type' => 'tags',
        'id' => 7,
      ),
    ),
    'rel2' => array(
      'data' => array(
        array(
          'type' => 'articles',
          'id' => 1,
        ),
        array(
          'type' => 'articles',
          'id' => 2,
          'meta' => array(
            'subrequest' => array(
              'method' => 'PUT',
            ),
          ),
        ),
        array(
          'type' => 'articles',
          'id' => 3,
          'meta' => array(
            'subrequest' => array(
              'method' => 'PATCH',
            ),
          ),
        ),
        array(
          'type' => 'articles',
          'id' => 'newRel2--1',
          'meta' => array(
            'subrequest' => array(
              'method' => 'POST',
              'headers' => array(
                'X-CSRF-Token' => 'my-token',
              ),
            ),
          ),
        ),
      ),
    ),
  );

  // Generate 3 random strings.
  $rnd_text = array_map(array(
    $this,
    'randomName',
  ), range(0, 4));
  $included = array(
    array(
      'type' => 'articles',
      'id' => 1,
      'attributes' => array(
        'label' => $rnd_text[0],
      ),
      'relationships' => array(
        'articles' => array(
          'data' => array(
            'type' => 'articles',
            'id' => 2,
          ),
        ),
      ),
    ),
    array(
      'type' => 'articles',
      'id' => 2,
      'attributes' => array(
        'label' => $rnd_text[1],
      ),
    ),
    array(
      'type' => 'articles',
      'id' => 3,
      'attributes' => array(
        'label' => $rnd_text[2],
      ),
    ),
    array(
      'type' => 'articles',
      'id' => 'newRel2--1',
      'attributes' => array(
        'label' => $rnd_text[3],
      ),
      'relationships' => array(
        'articles' => array(
          'data' => array(
            'type' => 'articles',
            'id' => 1,
          ),
        ),
      ),
    ),
  );
  $data = array(
    array(
      'type' => 'articles',
      'id' => 5,
      'attributes' => array(
        'title' => $rnd_text[4],
      ),
      'relationships' => $relationships,
    ),
  );
  $json_api = array(
    'data' => $data,
    'included' => $included,
  );
  $parsed_body = restful()
    ->getFormatterManager()
    ->getPlugin('json_api')
    ->parseBody(drupal_json_encode($json_api));
  $expected = array(
    array(
      'title' => $rnd_text[4],
      'rel1' => array(
        'id' => 7,
      ),
      'rel2' => array(
        array(
          'id' => 1,
        ),
        array(
          'id' => 2,
          'body' => array(
            'label' => $rnd_text[1],
          ),
          'request' => array(
            'method' => 'PUT',
          ),
        ),
        array(
          'id' => 3,
          'body' => array(
            'label' => $rnd_text[2],
          ),
          'request' => array(
            'method' => 'PATCH',
          ),
        ),
        array(
          'body' => array(
            'label' => $rnd_text[3],
            'articles' => array(
              'id' => 1,
            ),
          ),
          'request' => array(
            'method' => 'POST',
            'headers' => array(
              'X-CSRF-Token' => 'my-token',
            ),
          ),
        ),
      ),
    ),
  );
  $this
    ->assertEqual($expected, $parsed_body);
}