You are here

public function DiffPluginFileTest::testFilePlugin in Diff 8

Tests the File plugin.

See also

\Drupal\diff\Plugin\diff\Field\FileFieldBuilder

File

tests/src/Functional/DiffPluginFileTest.php, line 53

Class

DiffPluginFileTest
Tests the Diff module entity plugins.

Namespace

Drupal\Tests\diff\Functional

Code

public function testFilePlugin() {

  // Add file field to the article content type.
  $file_field_name = 'field_file';
  $field_storage = FieldStorageConfig::create(array(
    'field_name' => $file_field_name,
    'entity_type' => 'node',
    'type' => 'file',
  ));
  $field_storage
    ->save();
  FieldConfig::create([
    'entity_type' => 'node',
    'field_storage' => $field_storage,
    'bundle' => 'article',
    'label' => 'File',
  ])
    ->save();

  // Make the field visible in the form and desfault display.
  $this->viewDisplay
    ->load('node.article.default')
    ->setComponent('test_field')
    ->setComponent($file_field_name)
    ->save();
  $this->formDisplay
    ->load('node.article.default')
    ->setComponent('test_field', [
    'type' => 'entity_reference_autocomplete',
  ])
    ->setComponent($file_field_name, [
    'type' => 'file_generic',
  ])
    ->save();

  // Create an article.
  $node = $this
    ->drupalCreateNode([
    'type' => 'article',
    'title' => 'Test article',
  ]);
  $revision1 = $node
    ->getRevisionId();

  // Upload a file to the article.
  $test_files = $this
    ->drupalGetTestFiles('text');
  $edit['files[field_file_0]'] = $this->fileSystem
    ->realpath($test_files['0']->uri);
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/edit', $edit, 'Upload');
  $edit['revision'] = TRUE;
  $this
    ->drupalPostNodeForm('node/' . $node
    ->id() . '/edit', $edit, t('Save and keep published'));
  $node = $this
    ->drupalGetNodeByTitle('Test article', TRUE);
  $revision2 = $node
    ->getRevisionId();

  // Replace the file by a different one.
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/edit', [], 'Remove');
  $this
    ->drupalPostNodeForm(NULL, [
    'revision' => FALSE,
  ], t('Save and keep published'));
  $edit['files[field_file_0]'] = $this->fileSystem
    ->realpath($test_files['1']->uri);
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/edit', $edit, 'Upload');
  $edit['revision'] = TRUE;
  $this
    ->drupalPostNodeForm('node/' . $node
    ->id() . '/edit', $edit, t('Save and keep published'));
  $node = $this
    ->drupalGetNodeByTitle('Test article', TRUE);
  $revision3 = $node
    ->getRevisionId();

  // Check differences between revisions.
  $this
    ->clickLink(t('Revisions'));
  $edit = [
    'radios_left' => $revision1,
    'radios_right' => $revision3,
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Compare selected revisions'));
  $this
    ->assertText('File');
  $this
    ->assertText('File: text-1_0.txt');
  $this
    ->assertText('File ID: 4');

  // Use the unified fields layout.
  $this
    ->clickLink('Unified fields');
  $this
    ->assertResponse(200);
  $this
    ->assertText('File');
  $this
    ->assertText('File: text-1_0.txt');
  $this
    ->assertText('File ID: 4');
}