You are here

public function SearchByPageAttachTest::attachFileUpload in Search by Page 7

Attaches a file to a node, using File field.

Parameters

$node: Node object to attach to.

$filename: File name, not including path (assumed to be in test directory).

$listed: Whether to mark as displayed (1) or not (0).

$desc: Description to put on the file upload.

3 calls to SearchByPageAttachTest::attachFileUpload()
SearchbyPageAttachNotReadable::makeContent in tests/search_by_page.test
Makes a node with an unreadable attachment, for search testing.
SearchbyPageAttachReindexTest::makeContent in tests/search_by_page.test
Makes several nodes with attachments, for search testing.
SearchByPageAttachTest::makeContent in tests/search_by_page.test
Makes 2 nodes with attachments, for search testing.

File

tests/search_by_page.test, line 2345
Tests for the Search by Page module. By Jennifer Hodgdon of Poplar ProductivityWare, www.poplarware.com

Class

SearchByPageAttachTest
Functionality test 1 for Search by Page Attachments.

Code

public function attachFileUpload($node, $filename, $listed, $desc) {
  $path = realpath(drupal_get_path('module', 'search_by_page') . "/tests/" . $filename);

  // Add the file to the files table and file downloads directory.
  $file = file_get_contents($path);
  $file = file_save_data($file, 'public://' . $filename);
  $this
    ->assertTrue($file->fid, "File record was created");
  $info = array(
    'display' => $listed,
    'description' => $desc,
    'fid' => $file->fid,
  );

  // Load the node, add the file to it, and save.
  $node = node_load($node->nid, NULL, TRUE);
  $done = FALSE;
  foreach ($node->field_myfile as $lang => $langstuff) {
    $node->field_myfile[$lang][] = $info;
    $done = TRUE;
    break;
  }
  if (!$done) {
    $node->field_myfile['und'] = array(
      $info,
    );
  }
  node_save($node);

  // Verify the file is attached
  $node = node_load($node->nid, NULL, TRUE);
  $found = FALSE;
  foreach ($node->field_myfile as $lang => $langstuff) {
    foreach ($langstuff as $stuff) {
      if ($stuff['fid'] == $file->fid) {
        $found = TRUE;
        break;
      }
    }
  }
  $this
    ->assertTrue($found, "File was actually attached");
}