You are here

public function SearchByPageAttachTest::attachFileUpload in Search by Page 8

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/src/Functional/search_by_page.test
Makes a node with an unreadable attachment, for search testing.
SearchbyPageAttachReindexTest::makeContent in tests/src/Functional/search_by_page.test
Makes several nodes with attachments, for search testing.
SearchByPageAttachTest::makeContent in tests/src/Functional/search_by_page.test
Makes 2 nodes with attachments, for search testing.

File

tests/src/Functional/search_by_page.test, line 2031
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.

Namespace

Drupal\Tests\search_by_page\Functional

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 = \Drupal\node\Entity\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 = \Drupal\node\Entity\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");
}