From 2438ab707cf33a5095d453acad434a5157e2dc58 Mon Sep 17 00:00:00 2001
From: ygerasimov <yuri.gerasimov@gmail.com>
Date: Tue, 1 Mar 2011 17:51:23 +0200
Subject: [PATCH] Port file resource to d7. Add tests.

---
 resources/file_resource.inc                     |   35 ++------
 services.info                                   |    1 +
 tests/functional/ServicesResourceFileTests.test |  111 +++++++++++++++++++++++
 3 files changed, 121 insertions(+), 26 deletions(-)
 create mode 100644 tests/functional/ServicesResourceFileTests.test

diff --git a/resources/file_resource.inc b/resources/file_resource.inc
index 7f75e25..ba55292 100644
--- a/resources/file_resource.inc
+++ b/resources/file_resource.inc
@@ -94,16 +94,10 @@ function _file_resource_create($file) {
     return FALSE;
   }
 
-  // If the submitted file is an update, then set the update parameter for
-  // drupal_write_record(), indicating such. Otherwise we can just pass the
-  // object in and it will be treated as an insert.
-  $update = array();
-  if (!empty($file->fid)) {
-    $update = 'fid';
-  }
+  // Make sure we create new file.
+  $file->fid = NULL;
 
   // Get the directory name for the location of the file:
-
   if (isset($file->filename) && !isset($file->filepath)) {
     $file->filepath = file_default_scheme() . '://' . $file->filename;
   }
@@ -122,28 +116,15 @@ function _file_resource_create($file) {
     $file->filename .= '.txt';
   }
 
-/*
-  //The filepath that ends up in the node must contain the filename
-  $file->filepath .= '/'. $file->filename;
- */
-
   // Write the file
   if (!$file = file_save_data(base64_decode($file->file), $file->filepath)) {
     return services_error("Could not write file to destination", 500);
   }
 
-  //Required to be able to reference this file.
+  // Required to be able to reference this file.
   file_usage_add($file, 'services', 'files', $file->fid);
 
-  // hook_file_insert() requires an object
-  if (empty($update)) {
-    foreach (module_implements('file_insert') as $module) {
-      $function =  $module . '_file_insert';
-      $function($file);
-    }
-  }
-
-  return (object)array(
+  return array(
     'fid' => $file->fid,
     'uri' => services_resource_uri(array('file', $file->fid)),
   );
@@ -178,9 +159,11 @@ function _file_resource_retrieve($fid, $include_file_contents = TRUE) {
  *   Whether or not the delete was successful.
  */
 function _file_resource_delete($fid) {
-  $file = db_fetch_array(db_query('SELECT * FROM {files} WHERE fid = %d', $fid));
-  file_usage_delete($file, 'services', 'files', $file['fid']);
-  return file_delete($file['filepath']);
+  if ($file = file_load($fid)) {
+    file_usage_delete($file, 'services');
+    return file_delete($file);
+  }
+  return FALSE;
 }
 
 /**
diff --git a/services.info b/services.info
index 4b79c43..a8f353a 100644
--- a/services.info
+++ b/services.info
@@ -14,6 +14,7 @@ files[] = services.servers.api.php
 files[] = serviers.services.api.php
 files[] = tests/functional/NoAuthEndpointTestRunner.test
 files[] = tests/functional/ServicesResourceNodeTests.test
+files[] = tests/functional/ServicesResourceFileTests.test
 files[] = tests/functional/ServicesResourceSystemTests.test
 files[] = tests/functional/ServicesEndpointTests.test
 files[] = tests/unit/TestServicesModule.test
diff --git a/tests/functional/ServicesResourceFileTests.test b/tests/functional/ServicesResourceFileTests.test
new file mode 100644
index 0000000..7a899d7
--- /dev/null
+++ b/tests/functional/ServicesResourceFileTests.test
@@ -0,0 +1,111 @@
+<?php
+
+/**
+ * @file
+ * Test for file resource.
+ *
+ */
+
+/**
+ * Test class.
+ */
+class ServicesResourceFileTests extends ServicesWebTestCase {
+  // Class variables
+  protected $privileged_user = NULL ;
+  // Endpoint details.
+  protected $endpoint = NULL;
+  // Test file.
+  protected $testfile;
+
+  /**
+   * Implements setUp().
+   */
+  public function setUp() {
+    parent::setUp(
+      'ctools',
+      'services',
+      'rest_server'
+    );
+    // Set up endpoint.
+    $this->endpoint =  $this->saveNewEndpoint();
+    // Create and log in our privileged user.
+    $this->privileged_user = $this->drupalCreateUser(array(
+      'get own binary files',
+      'save file information',
+    ));
+    $this->drupalLogin($this->privileged_user);
+    // Get a test file.
+    $this->testfile = current($this->drupalGetTestFiles('text'));
+  }
+
+  /**
+   * Implements getInfo().
+   */
+  public static function getInfo() {
+    return array(
+      'name' => t('Resource File'),
+      'description' => t('Test the resource File methods.'),
+      'group' => t('Services'),
+    );
+  }
+
+  /**
+   * Test create method.
+   */
+  public function testResourceFileCreate() {
+    // Create file argument with data.
+    $file = new stdClass();
+    $file->filesize = filesize($this->testfile->uri);
+    $file->filename = $this->testfile->filename;
+    $file->file = base64_encode(file_get_contents($this->testfile->uri));
+    $file->uid = $this->privileged_user->uid;
+
+    // Create file with call.
+    $result = $this->servicesPost($this->endpoint->path . '/file', array('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']);
+    $this->assertTrue(is_file($file_load->uri), t('New file saved to disk.'), 'FileResource: Create');
+  }
+
+  /**
+   * Test retrieve method.
+   */
+  public function testResourceFileRetrieve() {
+    $testfile = $this->testfile;
+
+    $testfile->fid = NULL;
+    $testfile->uid = $this->privileged_user->uid;
+    file_save($testfile);
+
+    // Retrieve file.
+    $result = $this->servicesGet($this->endpoint->path . '/file/' . $testfile->fid);
+    $file = $result['body'];
+
+    // Assert that fid, filesize and uri are the same.
+    $this->assertTrue($file->fid == $testfile->fid
+                   && $file->filesize == $testfile->filesize
+                   && $file->uri == $testfile->uri,
+      t('File retrieved.'), 'FileResource: Retrieve');
+  }
+
+  /**
+   * Test delete method.
+   */
+  public function testResourceFileDelete() {
+    $testfile = $this->testfile;
+
+    $testfile->fid = NULL;
+    $testfile->uid = $this->privileged_user->uid;
+    file_save($testfile);
+
+    // Delete file via call.
+    $result = $this->servicesDelete($this->endpoint->path . '/file/' . $testfile->fid);
+
+    // Try to load file.
+    $file_load = file_load($testfile->fid);
+    $this->assertTrue(empty($file_load), t('File deleted.'), 'FileResource: Delete');
+  }
+
+}
-- 
1.7.0.4

