View source
<?php
namespace Drupal\Tests\file_entity\Functional;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
class FileEntityEditTest extends FileEntityTestBase {
protected $web_user;
protected $admin_user;
public static $modules = [
'block',
];
function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('local_actions_block');
$this
->drupalPlaceBlock('local_tasks_block');
$this->web_user = $this
->drupalCreateUser(array(
'edit own document files',
'create files',
));
$this->admin_user = $this
->drupalCreateUser(array(
'bypass file access',
'administer files',
));
}
function testFileEntityEdit() {
$this
->drupalLogin($this->web_user);
$test_file = $this
->getTestFile('text');
$name_key = "filename[0][value]";
$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'));
}
$file = $this
->getFileByFilename('text-0_0.txt');
$this
->assertInstanceof(FileInterface::class, $file, t('File found in database.'));
$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.'));
$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.'));
$this
->assertNoFieldByName('op', t('Delete'), 'Delete button not found.');
$edit = array();
$edit[$name_key] = $this
->randomMachineName(8);
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertText($edit[$name_key], t('Name displayed.'));
}
function testFileEntityAssociatedUser() {
$this
->drupalLogin($this->admin_user);
$test_file = $this
->getTestFile('text');
$edit = array();
$edit['files[upload]'] = \Drupal::service('file_system')
->realpath($test_file->uri);
$this
->drupalPostForm('file/add', $edit, t('Next'));
$this
->drupalPostForm(NULL, array(), t('Next'));
$file = $this
->getFileByFilename('text-0_0.txt');
$this
->assertIdentical($file
->getOwnerId(), $this->admin_user
->id(), 'File associated with admin user.');
$edit = array(
'uid[0][target_id]' => 'invalid-name',
);
$this
->drupalPostForm('file/' . $file
->id() . '/edit', $edit, t('Save'));
if (\version_compare(\Drupal::VERSION, '9.2', '<')) {
$this
->assertSession()
->pageTextContains('There are no entities matching "invalid-name".');
}
else {
$this
->assertSession()
->pageTextContains('There are no users matching "invalid-name".');
}
$edit = array();
$edit['uid[0][target_id]'] = 'Anonymous (0)';
$this
->drupalPostForm('file/' . $file
->id() . '/edit', $edit, t('Save'));
\Drupal::entityTypeManager()
->getStorage('file')
->resetCache();
$file = File::load($file
->id());
$this
->assertIdentical($file
->getOwnerId(), '0', 'File associated with anonymous user.');
$edit = array();
$edit['uid[0][target_id]'] = $this->web_user
->label();
$this
->drupalPostForm('file/' . $file
->id() . '/edit', $edit, t('Save'));
\Drupal::entityTypeManager()
->getStorage('file')
->resetCache();
$file = File::load($file
->id());
$this
->assertIdentical($file
->getOwnerId(), $this->web_user
->id(), 'File associated with normal user.');
$this
->drupalLogin($this->web_user);
$this
->drupalGet('file/' . $file
->id() . '/edit');
$this
->assertNoFieldByName('uid[0][target_id]');
}
}