You are here

protected function FileFieldAnonymousSubmissionTest::doTestNodeWithFileWithoutTitle in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/file/tests/src/Functional/FileFieldAnonymousSubmissionTest.php \Drupal\Tests\file\Functional\FileFieldAnonymousSubmissionTest::doTestNodeWithFileWithoutTitle()
  2. 10 core/modules/file/tests/src/Functional/FileFieldAnonymousSubmissionTest.php \Drupal\Tests\file\Functional\FileFieldAnonymousSubmissionTest::doTestNodeWithFileWithoutTitle()

Helper method to test file submissions with missing node titles.

2 calls to FileFieldAnonymousSubmissionTest::doTestNodeWithFileWithoutTitle()
FileFieldAnonymousSubmissionTest::testAnonymousNodeWithFileWithoutTitle in core/modules/file/tests/src/Functional/FileFieldAnonymousSubmissionTest.php
Tests file submission for an anonymous visitor with a missing node title.
FileFieldAnonymousSubmissionTest::testAuthenticatedNodeWithFileWithoutTitle in core/modules/file/tests/src/Functional/FileFieldAnonymousSubmissionTest.php
Tests file submission for an authenticated user with a missing node title.

File

core/modules/file/tests/src/Functional/FileFieldAnonymousSubmissionTest.php, line 124

Class

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

Namespace

Drupal\Tests\file\Functional

Code

protected function doTestNodeWithFileWithoutTitle() {
  $bundle_label = 'Article';
  $node_title = 'Test page';
  $this
    ->createFileField('field_image', 'node', 'article', [], [
    'file_extensions' => 'txt png',
  ]);

  // Load the node form.
  $this
    ->drupalGet('node/add/article');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertText(strip_tags(t('Create @name', [
    '@name' => $bundle_label,
  ])));

  // Generate an image file.
  $image = $this
    ->getTestFile('image');

  // Submit the form but exclude the title field.
  $edit = [
    'body[0][value]' => 'Test article',
    'files[field_image_0]' => $this->container
      ->get('file_system')
      ->realpath($image
      ->getFileUri()),
  ];
  if (!$this->loggedInUser) {
    $label = 'Save';
  }
  else {
    $label = 'Save';
  }
  $this
    ->drupalPostForm(NULL, $edit, $label);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $t_args = [
    '@type' => $bundle_label,
    '%title' => $node_title,
  ];
  $this
    ->assertNoText(strip_tags(t('@type %title has been created.', $t_args)), 'The node was created.');
  $this
    ->assertText('Title field is required.');

  // Submit the form again but this time with the missing title field. This
  // should still work.
  $edit = [
    'title[0][value]' => $node_title,
  ];
  $this
    ->drupalPostForm(NULL, $edit, $label);

  // Confirm the final submission actually worked.
  $t_args = [
    '@type' => $bundle_label,
    '%title' => $node_title,
  ];
  $this
    ->assertText(strip_tags(t('@type %title has been created.', $t_args)), 'The node was created.');
  $matches = [];
  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
      ->assertFileExists(File::load($node->field_image->target_id)
      ->getFileUri());
  }
}