You are here

public function ServicesResourceNodetests::testAttachFileTargetedAction in Services 7.3

Testing targeted_action attach_file.

File

tests/functional/ServicesResourceNodeTests.test, line 277
Call the endpoint tests when no authentication is being used.

Class

ServicesResourceNodetests
Run test cases for the endpoint with no authentication turned on.

Code

public function testAttachFileTargetedAction() {

  // We will do test on the article node type.
  // Create and log in our privileged user.
  $account = $this
    ->drupalCreateUser(array(
    'bypass node access',
  ));
  $this
    ->drupalLogin($account);

  // Create article node.
  $settings = array(
    'type' => 'article',
  );
  $node = $this
    ->drupalCreateNode($settings);

  // Get a test file.
  $testfiles = $this
    ->drupalGetTestFiles('image');
  $testfile1 = array_pop($testfiles);
  $testfile2 = array_pop($testfiles);

  // Attach one file.
  $result = $this
    ->servicesPostFile($this->endpoint->path . '/node/' . $node->nid . '/attach_file', array(
    $testfile1->uri,
  ), array(), array(
    'field_name' => 'field_image',
  ));
  $node = node_load($node->nid, TRUE);
  $this
    ->assertEqual($testfile1->filename, $node->field_image[LANGUAGE_NONE][0]['filename'], 'One file has been attached.');

  // Replace the file on the article node.
  $result = $this
    ->servicesPostFile($this->endpoint->path . '/node/' . $node->nid . '/attach_file', array(
    $testfile2->uri,
  ), array(), array(
    'field_name' => 'field_image',
    'attach' => FALSE,
  ));
  $node = node_load($node->nid, TRUE);
  $this
    ->assertEqual($testfile2->filename, $node->field_image[LANGUAGE_NONE][0]['filename'], 'File has been replaced.');

  // Add another file to the article node. Get validation error.
  $result = $this
    ->servicesPostFile($this->endpoint->path . '/node/' . $node->nid . '/attach_file', array(
    $testfile1->uri,
  ), array(), array(
    'field_name' => 'field_image',
  ));
  $this
    ->assertEqual($result['body'], 'You cannot upload so many files.', 'Validation on cardinality works.');

  // Update field info. Set cardinality 2.
  $field_info = field_read_field('field_image');
  $field_info['cardinality'] = 2;
  field_update_field($field_info);

  // Upload multiple files.
  $result = $this
    ->servicesPostFile($this->endpoint->path . '/node/' . $node->nid . '/attach_file', array(
    $testfile1->uri,
    $testfile2->uri,
  ), array(), array(
    'field_name' => 'field_image',
    'attach' => FALSE,
  ));
  $node = node_load($node->nid, TRUE);
  $this
    ->assertTrue($testfile1->filename == $node->field_image[LANGUAGE_NONE][0]['filename'] && $testfile2->filename == $node->field_image[LANGUAGE_NONE][1]['filename'], 'Multiple files uploaded.');

  // Verify total file count == 2 and also proper delta sequence in db.
  $query = db_select('field_data_field_image', 'fd');
  $deltas = $query
    ->condition('entity_type', 'node')
    ->condition('bundle', $node->type)
    ->condition('entity_id', $node->nid)
    ->fields('fd', array(
    'delta',
  ))
    ->execute()
    ->fetchCol(0);
  $this
    ->assertTrue($deltas == array(
    0,
    1,
  ), 'Attached file deltas are sequential.');
}