You are here

public function CsvController::nodes in Feeds 8.3

Generates a test feed and simulates last-modified.

This is used to test the following:

  • Ensure that the source is not refetched on a second import when the source did not change.
  • Ensure that the source *is* refetched on a second import when the source *did* change.

Return value

\Symfony\Component\HttpFoundation\Response A HTTP response.

1 string reference to 'CsvController::nodes'
feeds_test_files.routing.yml in tests/modules/feeds_test_files/feeds_test_files.routing.yml
tests/modules/feeds_test_files/feeds_test_files.routing.yml

File

tests/modules/feeds_test_files/src/Controller/CsvController.php, line 78

Class

CsvController
Generates CSV source files.

Namespace

Drupal\feeds_test_files\Controller

Code

public function nodes() {
  $last_modified = \Drupal::state()
    ->get('feeds_test_nodes_last_modified');
  if (!$last_modified) {
    $file = 'nodes.csv';
    $last_modified = strtotime('Sun, 19 Nov 1978 05:00:00 GMT');
  }
  else {
    $file = 'nodes_changes2.csv';
  }
  $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) : FALSE;
  $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : FALSE;
  $response = new Response();

  // Set header with last modified date.
  $response->headers
    ->set('Last-Modified', gmdate(static::DATE_RFC7231, $last_modified));

  // Return 304 not modified if last modified.
  if ($last_modified == $if_modified_since) {
    $response->headers
      ->set('Status', '304 Not Modified');
    return $response;
  }

  // The following headers force validation of cache:
  $response->headers
    ->set('Expires', $last_modified);
  $response->headers
    ->set('Cache-Control', 'must-revalidate');
  $response->headers
    ->set('Content-Type', 'text/plain; charset=utf-8');

  // Read actual feed from file.
  $csv = file_get_contents(drupal_get_path('module', 'feeds') . '/tests/resources/csv/' . $file);

  // And return the file contents.
  $response
    ->setContent($csv);
  return $response;
}