You are here

public function ServicesResourceFileTests::testResourceFileCreate in Services 6.3

Same name and namespace in other branches
  1. 7.3 tests/functional/ServicesResourceFileTests.test \ServicesResourceFileTests::testResourceFileCreate()

Test create method.

File

tests/functional/ServicesResourceFileTests.test, line 63

Class

ServicesResourceFileTests
Test class.

Code

public function testResourceFileCreate() {

  // Create file argument with data.
  $file = array(
    'filesize' => filesize($this->testfiles[0]->filename),
    'filename' => str_replace('/simpletest/', '/' . $this
      ->randomName() . '/' . $this
      ->randomName() . '/', $this->testfiles[0]->filename),
    'file' => base64_encode(file_get_contents($this->testfiles[0]->filename)),
  );

  // Create file with call.
  $result = $this
    ->servicesPost($this->endpoint->path . '/file', $file);
  $this
    ->assertEqual($result['code'], 200, t('File created.'), 'FileResource: Create');

  // Load file and assert that it exists.

  //$file_load = file_load($result['body']['fid']);
  $dbresult = db_query('SELECT * FROM {files} WHERE fid = %d', $result['body']['fid']);
  $file_load = db_fetch_object($dbresult);
  $this
    ->assertTrue(is_file($file_load->filepath), t('New file saved to disk.'), 'FileResource: Create');
  $this
    ->assertEqual($file_load->uid, $this->privileged_user->uid, t('File author id is preserved.'), 'FileResource: Create');

  // Lets create another file with the same details
  // and ensure name and path are changed automatically.
  $result = $this
    ->servicesPost($this->endpoint->path . '/file', $file);
  $dbresult = db_query('SELECT * FROM {files} WHERE fid = %d', $result['body']['fid']);
  $file1 = db_fetch_object($dbresult);
  $file2 = db_fetch_object($dbresult);
  $this
    ->assertTrue($file1->filename != $file2->filename && $file1->filepath != $file2->filepath, t('Two identical files created end up with different names.'), 'FileResource: Create');
}