You are here

public function FileHashTest::testFileHashFieldDuplicate in File Hash 8

Tests a file field with dedupe enabled.

File

tests/src/Functional/FileHashTest.php, line 88

Class

FileHashTest
File Hash tests.

Namespace

Drupal\Tests\filehash\Functional

Code

public function testFileHashFieldDuplicate() {
  $this
    ->drupalGet('admin/config/media/filehash');
  $fields = [
    'dedupe' => TRUE,
  ];
  $this
    ->submitForm($fields, $this
    ->t('Save configuration'));
  $field_name = strtolower($this
    ->randomMachineName());
  $type_name = 'article';
  $this
    ->createFileField($field_name, 'node', $type_name, [], [
    'required' => '1',
  ]);
  $test_file = $this
    ->getTestFile('text');
  $nid = $this
    ->uploadNodeFile($test_file, $field_name, $type_name);
  $this
    ->assertSession()
    ->addressEquals("node/{$nid}");
  $nid = $this
    ->uploadNodeFile($test_file, $field_name, $type_name);
  $this
    ->assertSession()
    ->addressEquals("node/{$nid}/edit");
  $this
    ->assertSession()
    ->responseContains($this
    ->t('The specified file %name could not be uploaded.', [
    '%name' => $test_file
      ->getFilename(),
  ]));
  $this
    ->assertSession()
    ->pageTextContains($this
    ->t('Sorry, duplicate files are not permitted.'));
  $this
    ->drupalGet('admin/config/media/filehash');
  $fields = [
    'dedupe' => FALSE,
  ];
  $this
    ->submitForm($fields, $this
    ->t('Save configuration'));
  $nid = $this
    ->uploadNodeFile($test_file, $field_name, $type_name);
  $this
    ->assertSession()
    ->addressEquals("node/{$nid}");
  $this
    ->drupalGet('admin/config/media/filehash');
  $fields = [
    'dedupe' => TRUE,
  ];
  $this
    ->submitForm($fields, $this
    ->t('Save configuration'));

  // Test that a node with duplicate file already attached can be saved.
  $this
    ->drupalGet("node/{$nid}/edit");
  $this
    ->submitForm([], $this
    ->t('Save'));
  $this
    ->assertSession()
    ->addressEquals("node/{$nid}");
}