You are here

public function RestWSTestCase::testResourceArray in RESTful Web Services 7.2

Test entity references with an array which contains id, entity type.

File

./restws.test, line 227
RESTful web services tests.

Class

RestWSTestCase
@file RESTful web services tests.

Code

public function testResourceArray() {
  $account = $this
    ->drupalCreateUser(array(
    'access content',
    'bypass node access',
    'access resource node',
  ));
  $this
    ->drupalLogin($account);
  $this
    ->createTerm("foo");
  $this
    ->createTerm("bar");

  // Test json create.
  $title = $this
    ->randomName(8);
  $new_node = array(
    'body' => array(
      LANGUAGE_NONE => array(
        array(),
      ),
    ),
    'type' => 'article',
    'title' => 'foo',
    'field_tags' => array(
      array(
        'id' => '2',
        'resource' => 'taxonomy_term',
      ),
      array(
        'id' => '1',
        'resource' => 'taxonomy_term',
      ),
    ),
    'author' => array(
      'id' => $account->uid,
      'resource' => 'user',
    ),
  );
  $json = drupal_json_encode($new_node);
  $result = $this
    ->httpRequest('node', 'POST', $account, $json);
  $result_array = drupal_json_decode($result);
  $nid = $result_array['id'];
  $node = node_load($nid);
  $this
    ->assertEqual($node->field_tags[LANGUAGE_NONE][0]['tid'], 2, 'Taxonomy term 1 was correctly added.');
  $this
    ->assertEqual($node->field_tags[LANGUAGE_NONE][1]['tid'], 1, 'Taxonomy term 2 was correctly added.');

  // Test XML update.
  $xml = '
      <node>
        <title>bar</title>
        <type>article</type>
        <author resource="user" id="' . $account->uid . '">' . restws_resource_uri('user', $account->uid) . '</author>
        <field_tags>
          <item resource="taxonomy_term" id="1">' . restws_resource_uri('taxonomy_term', 1) . '</item>
          <item resource="taxonomy_term" id="2">' . restws_resource_uri('taxonomy_term', 2) . '</item>
        </field_tags>
      </node>';
  $result = $this
    ->httpRequest('node/' . $nid, 'PUT', $account, $xml, 'xml');
  $node = node_load($nid, NULL, TRUE);
  $this
    ->assertEqual($node->field_tags[LANGUAGE_NONE][0]['tid'], 1, 'Taxonomy term 1 was correctly updated.');
  $this
    ->assertEqual($node->field_tags[LANGUAGE_NONE][1]['tid'], 2, 'Taxonomy term 2 was correctly updated.');

  // Test XML create.
  $result = $this
    ->httpRequest('node', 'POST', $account, $xml, 'xml');
  $xml_element = simplexml_load_string($result);
  $nid = $xml_element
    ->attributes()->id;
  $node = node_load((int) $nid, NULL, TRUE);
  $this
    ->assertEqual($node->field_tags[LANGUAGE_NONE][0]['tid'], 1, 'Taxonomy term 1 was correctly added.');
  $this
    ->assertEqual($node->field_tags[LANGUAGE_NONE][1]['tid'], 2, 'Taxonomy term 2 was correctly added.');
}