You are here

function FileEntityEditTest::testFileEntityEdit in File Entity (fieldable files) 8.2

Check file edit functionality.

File

tests/src/Functional/FileEntityEditTest.php, line 32

Class

FileEntityEditTest
Create a file and test file edit functionality.

Namespace

Drupal\Tests\file_entity\Functional

Code

function testFileEntityEdit() {
  $this
    ->drupalLogin($this->web_user);
  $test_file = $this
    ->getTestFile('text');
  $name_key = "filename[0][value]";

  // Create file to edit.
  $edit = array();
  $edit['files[upload]'] = \Drupal::service('file_system')
    ->realpath($test_file->uri);
  $this
    ->drupalPostForm('file/add', $edit, t('Next'));
  if ($this
    ->xpath('//input[@name="scheme"]')) {
    $this
      ->drupalPostForm(NULL, array(), t('Next'));
  }

  // Check that the file exists in the database.
  $file = $this
    ->getFileByFilename('text-0_0.txt');
  $this
    ->assertInstanceof(FileInterface::class, $file, t('File found in database.'));

  // Check that "edit" link points to correct page.
  $this
    ->clickLink(t('Edit'));
  $edit_url = $file
    ->toUrl('edit-form', [
    'absolute' => TRUE,
  ])
    ->toString();
  $actual_url = $this
    ->getURL();
  $this
    ->assertEqual($edit_url, $actual_url, t('On edit page.'));

  // Check that the name field is displayed with the correct value.
  $active = t('(active tab)');
  $link_text = t('@local-task-title<span class="element-invisible">@active</span>', array(
    '@local-task-title' => t('Edit'),
    '@active' => $active,
  ));
  $this
    ->assertText(strip_tags($link_text), 0, t('Edit tab found and marked active.'));
  $this
    ->assertFieldByName($name_key, $file
    ->label(), t('Name field displayed.'));

  // The user does not have "delete" permissions so no delete button should be found.
  $this
    ->assertNoFieldByName('op', t('Delete'), 'Delete button not found.');

  // Edit the content of the file.
  $edit = array();
  $edit[$name_key] = $this
    ->randomMachineName(8);

  // Stay on the current page, without reloading.
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));

  // Check that the name field is displayed with the updated values.
  $this
    ->assertText($edit[$name_key], t('Name displayed.'));
}