Skip to content

Commit

Permalink
Issue [#1051158] heyrocker, kylebrowning, voxpelli Convert files/node…
Browse files Browse the repository at this point in the history
…Files action to node/x/files relationship
  • Loading branch information
Kyle Browning authored and Kyle Browning committed May 5, 2011
1 parent 8efb667 commit 785fc18
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 61 deletions.
27 changes: 0 additions & 27 deletions resources/file_resource.inc
Original file line number Diff line number Diff line change
Expand Up @@ -73,33 +73,6 @@ function _file_resource_definition() {
),
),
),
'actions' => array(
'nodeFiles' => array(
'help' => t('This method returns the files on a given node.'),
'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/file_resource'),
'access callback' => '_file_resource_node_access',
'access arguments' => array('view'),
'access arguments append' => TRUE,
'callback' => '_file_resource_load_node_files',
'args' => array(
array(
'name' => 'nid',
'type' => 'int',
'description' => t('The node id to load files for.'),
'source' => 'data',
'optional' => FALSE,
),
array(
'name' => 'file_contents',
'type' => 'int',
'description' => t('To return file contents or not.'),
'source' => 'data',
'optional' => FALSE,
'default value' => TRUE,
),
),
),
),
),
);
}
Expand Down
70 changes: 70 additions & 0 deletions resources/node_resource.inc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,33 @@ function _node_resource_definition() {
),
'access arguments' => array('access content'),
),
'relationships' => array(
'nodeFiles' => array(
'help' => t('This method returns the files on a given node.'),
'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'),
'access callback' => '_node_resource_access',
'access arguments' => array('view'),
'access arguments append' => TRUE,
'callback' => '_node_resource_load_node_files',
'args' => array(
array(
'name' => 'nid',
'type' => 'int',
'description' => t('The node id to load files for.'),
'source' => 'data',
'optional' => FALSE,
),
array(
'name' => 'file_contents',
'type' => 'int',
'description' => t('To return file contents or not.'),
'source' => 'data',
'optional' => FALSE,
'default value' => TRUE,
),
),
),
),
),
);
}
Expand Down Expand Up @@ -321,6 +348,49 @@ function _node_resource_index($page, $fields, $parameters) {
return $nodes;
}

/**
* Generates an array of base64 encoded files attached to a node
*
* @param $nid
* Number. Node ID
* @param $include_file_contents
* Bool Whether or not to include the base64_encoded version of the file.
* @return
* Array. A list of all files from the given node
*/
function _node_resource_load_node_files($nid, $include_file_contents = TRUE) {
module_load_include('inc', 'services', 'resources/file_resource');
$node = node_load($nid);
if (!isset($node->files)) {
return services_error(t('There are no files on given node.'));
}

$return = array();
foreach ($node->files as $file) {
// Do not return files that are not listed.
if (!$file->list) {
continue;
}
$return[$file->fid] = array(
'filename' => $file->filename,
'uid' => $file->uid,
'filemime' => $file->filemime,
'filesize' => $file->filesize,
'status' => $file->status,
'timestamp' => $file->timestamp
);
// If to add content of the file.
if ($file_include_contents) {
$filepath = file_create_path($file->filepath);
$binaryfile = fopen($filepath, 'rb');
$return[$file->fid]['file'] = base64_encode(fread($binaryfile, filesize($filepath)));
fclose($binaryfile);
}

}
return $return;
}

/**
* Determine whether the current user can access a node resource.
*
Expand Down
34 changes: 1 addition & 33 deletions tests/functional/ServicesResourceFileTests.test
Original file line number Diff line number Diff line change
Expand Up @@ -127,39 +127,7 @@ class ServicesResourceFileTests extends ServicesWebTestCase {
$fid = db_result(db_query('SELECT fid FROM {files} WHERE fid = %d', $testfile['fid']));
$this->assertTrue(empty($fid), t('File deleted.'), 'FileResource: Delete');
}

/**
* Test nodeFiles action.
*/
public function testNodeFiles() {
$testfile1 = $this->uploadTestFile();
$testfile2 = $this->uploadTestFile();
$testfile3 = $this->uploadTestFile();

// Needed for upload module to create new records in {upload} table.
// @see upload_save()
$testfile1['new'] = TRUE;
$testfile2['new'] = TRUE;
$testfile3['new'] = TRUE;
// First two files are listed. Third is not listed.
$testfile1['list'] = 1;
$testfile2['list'] = 1;
$testfile3['list'] = 0;

// Create node with three files.
$settings = array('files' => array(
$testfile1['fid'] => (object)$testfile1,
$testfile2['fid'] => (object)$testfile2,
$testfile3['fid'] => (object)$testfile3,
));
$node = $this->drupalCreateNode($settings);

$result = $this->servicesPost($this->endpoint->path . '/file/nodeFiles', array('nid' => $node->nid));
$this->assertTrue(isset($result['body'][$testfile1['fid']]) && isset($result['body'][$testfile2['fid']]) && !isset($result['body'][$testfile3['fid']]),
t('Attached files listed.'), 'FileResource: nodeFiles');
}

/**
/**
* Emulate uploaded file.
*
* Copy file from simpletest file samples and create record in files table.
Expand Down
73 changes: 72 additions & 1 deletion tests/functional/ServicesResourceNodeTests.test
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class ServicesResourceNodetests extends ServicesWebtestCase {
'services',
'rest_server',
'services_sessauth',
'inputstream'
'inputstream',
'upload'
);
// Set up endpoint.
$this->endpoint = $this->saveNewEndpoint();
Expand Down Expand Up @@ -201,7 +202,45 @@ class ServicesResourceNodetests extends ServicesWebtestCase {
$terms_number = db_result(db_query('SELECT COUNT(*) result FROM {term_node} r INNER JOIN {node} n ON n.vid = r.vid WHERE n.nid = %d', $response['body']['nid']));
$this->assertTrue($terms_number == 2, t('Node with two terms was successfully created.'), 'NodeResource: Create');
}
/**
* Test nodeFiles action.
*/
public function testNodeFiles() {
$this->privileged_user = $this->drupalCreateUser(array(
'get own binary files',
'save file information',
'administer services',
'administer site configuration',
'upload files',
));
$this->drupalLogin($this->privileged_user);
$testfiles = $this->drupalGetTestFiles('text');
$testfile1 = $this->uploadTestFile($testfiles);
$testfile2 = $this->uploadTestFile($testfiles);
$testfile3 = $this->uploadTestFile($testfiles);

// Needed for upload module to create new records in {upload} table.
// @see upload_save()
$testfile1['new'] = TRUE;
$testfile2['new'] = TRUE;
$testfile3['new'] = TRUE;
// First two files are listed. Third is not listed.
$testfile1['list'] = 1;
$testfile2['list'] = 1;
$testfile3['list'] = 0;

// Create node with three files.
$settings = array('files' => array(
$testfile1['fid'] => (object)$testfile1,
$testfile2['fid'] => (object)$testfile2,
$testfile3['fid'] => (object)$testfile3,
));
$node = $this->drupalCreateNode($settings);

$result = $this->servicesPost($this->endpoint->path . '/node/nodeFiles', array('nid' => $node->nid));
$this->assertTrue(isset($result['body'][$testfile1['fid']]) && isset($result['body'][$testfile2['fid']]) && !isset($result['body'][$testfile3['fid']]),
t('Attached files listed.'), 'FileResource: nodeFiles');
}
/**
* testing node_resource Update
*/
Expand Down Expand Up @@ -299,4 +338,36 @@ class ServicesResourceNodetests extends ServicesWebtestCase {

$this->assertFalse($responseArray['code'] == 200, t('Node was deleted. It shoudlnt have been because it doesnt exist'), 'NodeResource: Deleted');
}
/**
* Emulate uploaded file.
*
* Copy file from simpletest file samples and create record in files table.
*
* @return array
* File data.
*/
public function uploadTestFile($file = NULL) {
if (empty($file)) {
$file = next($this->testfiles);
}
$testfile = array(
'fid' => NULL,
'uid' => $this->privileged_user->uid,
'filename' => trim(basename($file->filename), '.'),
'filepath' => $file->filename,
'filemime' => file_get_mimetype($file->filename),
'filesize' => filesize($file->filename),
'status' => FILE_STATUS_PERMANENT,
'timestamp' => time(),
);
$source = $testfile['filepath'];
$destination = file_directory_path() . '/' . $testfile['filepath'];
$dirname = dirname($destination);
file_check_directory($dirname, FILE_CREATE_DIRECTORY);
file_copy($source, $destination);

drupal_write_record('files', $testfile);

return $testfile;
}
}

0 comments on commit 785fc18

Please sign in to comment.