1.20.x file.test FileFieldAnonymousSubmission::testAnonymousNodeWithFile()

Tests file submission for an anonymous visitor.

File

modules/file/tests/file.test, line 1827
Tests for file.module.

Class

FileFieldAnonymousSubmission
Confirm that file field submissions work correctly for anonymous visitors.

Code

function testAnonymousNodeWithFile() {
  $type_name = 'page';
  $bundle_label = 'Page';
  $node_title = 'Test page';

  $field_name = strtolower($this->randomName());
  $this->createFileField($field_name, $type_name);

  // Load the node form.
  $this->backdropGet('node/add/' . $type_name);
  $this->assertResponse(200, 'Loaded the page node form.');
  $this->assertText(strip_tags(t('Create @name', array('@name' => $bundle_label))));

  // Generate a file.
  $test_file = $this->getTestFile('text');

  // Submit the form.
  $edit = array(
    'title' => $node_title,
    'body[und][0][value]' => 'Test page',
    'body[und][0][format]' => 'filtered_html',
    'files[' . $field_name . '_und_0]' => backdrop_realpath($test_file->uri),
    'path[auto]' => FALSE,
    'path[alias]' => '',
  );
  $this->backdropPost(NULL, $edit, t('Save'));
  $this->assertResponse(200);
  $t_args = array('@type' => $bundle_label, '%title' => $node_title);
  $this->assertText(strip_tags(t('@type %title has been created.', $t_args)), 'The node was created.');
  $matches = array();
  if (preg_match('@node/(\d+)$@', $this->getUrl(), $matches)) {
    $nid = end($matches);
    $this->assertNotEqual($nid, 0, 'The node ID was extracted from the URL.');
    $node = node_load($nid);
    $this->assertNotEqual($node, NULL, 'The node was loaded successfully.');
    $this->assertEqual($node->{$field_name}[LANGUAGE_NONE][0]['filename'], $test_file->filename, 'The file was uploaded successfully.');
  }
}