View source
<?php
namespace Drupal\Tests\file_entity\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\file_entity\Entity\FileEntity;
use Drupal\node\Entity\Node;
use Drupal\user\Entity\User;
use Drupal\views\Entity\View;
class FileEntityAdminTest extends FileEntityTestBase {
protected $userAdmin;
protected $userBasic;
protected $userViewOwn;
protected $userViewPrivate;
protected $userEditDelete;
public static $modules = [
'block',
];
public function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('local_actions_block');
$this
->drupalPlaceBlock('local_tasks_block');
$roles = user_roles();
foreach ($roles as $rid => $role) {
user_role_revoke_permissions($rid, array(
'view files',
));
}
$this->userAdmin = $this
->drupalCreateUser(array(
'administer files',
'bypass file access',
));
$this->userBasic = $this
->drupalCreateUser(array(
'administer files',
));
$this->userViewOwn = $this
->drupalCreateUser(array(
'administer files',
'view own private files',
));
$this->userViewPrivate = $this
->drupalCreateUser(array(
'administer files',
'view private files',
));
$this->userEditDelete = $this
->drupalCreateUser(array(
'administer files',
'edit any document files',
'delete any document files',
'edit any image files',
'delete any image files',
));
View::load('files')
->disable()
->save();
View::load('file_entity_files')
->enable()
->save();
}
public function testFilesAdminSort() {
$this
->drupalLogin($this->userAdmin);
$i = 0;
foreach (array(
'dd',
'aa',
'DD',
'bb',
'cc',
'CC',
'AA',
'BB',
) as $prefix) {
$this
->createFileEntity(array(
'filename' => $prefix . $this
->randomMachineName(6),
'created' => $i * 90000,
));
$i++;
}
$files_query = array();
foreach (\Drupal::entityQuery('file')
->sort('created', 'DESC')
->execute() as $fid) {
$files_query[] = FileEntity::load($fid)
->label();
}
$this
->drupalGet('admin/content/files');
$xpath = '//form[@id="views-form-file-entity-files-overview"]/table[@class="cols-10 responsive-enabled"]/tbody//tr/td[contains(@class, "views-field-filename")]';
$list = $this
->xpath($xpath);
$entries = [];
foreach ($list as $entry) {
$entries[] = trim((string) $entry
->getText());
}
$this
->assertEqual($files_query, $entries, 'Files are sorted in the view according to the default query.');
$files_query = array();
foreach (\Drupal::entityQuery('file')
->sort('filename')
->execute() as $fid) {
$files_query[] = FileEntity::load($fid)
->label();
}
$this
->drupalGet('admin/content/files', array(
'query' => array(
'sort' => 'asc',
'order' => 'filename',
),
));
$list = $this
->xpath($xpath);
$entries = [];
foreach ($list as $entry) {
$entries[] = trim((string) $entry
->getText());
}
$this
->assertEqual($files_query, $entries, 'Files are sorted in the view the same as they are in the query.');
}
public function testFilesAdminPages() {
$this
->drupalLogin($this->userAdmin);
$files['public_image'] = $this
->createFileEntity(array(
'scheme' => 'public',
'uid' => $this->userBasic
->id(),
'type' => 'image',
));
$files['public_document'] = $this
->createFileEntity(array(
'scheme' => 'public',
'uid' => $this->userViewOwn
->id(),
'type' => 'document',
));
$files['private_image'] = $this
->createFileEntity(array(
'scheme' => 'private',
'uid' => $this->userBasic
->id(),
'type' => 'image',
));
$files['private_document'] = $this
->createFileEntity(array(
'scheme' => 'private',
'uid' => $this->userViewOwn
->id(),
'type' => 'document',
));
$this
->drupalGet('admin/content/files');
$this
->assertResponse(200);
$i = 0;
foreach ($files as $file) {
$this
->assertLinkByHref('file/' . $file
->id());
$this
->assertLinkByHref('file/' . $file
->id() . '/edit');
$this
->assertLinkByHref('file/' . $file
->id() . '/delete');
$this
->assertFieldByName("bulk_form[{$i}]", NULL, 'Bulk form checkbox found.');
}
$this
->drupalLogout();
$this
->drupalLogin($this->userBasic);
$this
->drupalGet('admin/content/files');
$this
->assertResponse(200);
$this
->assertLinkByHref('file/' . $files['public_image']
->id());
$this
->assertLinkByHref('file/' . $files['public_document']
->id());
$this
->assertLinkByHref('file/' . $files['public_document']
->id() . '/download');
$this
->assertLinkByHref('file/' . $files['public_document']
->id() . '/download');
$this
->assertNoLinkByHref('file/' . $files['public_image']
->id() . '/edit');
$this
->assertNoLinkByHref('file/' . $files['public_image']
->id() . '/delete');
$this
->assertNoLinkByHref('file/' . $files['public_document']
->id() . '/edit');
$this
->assertNoLinkByHref('file/' . $files['public_document']
->id() . '/delete');
$this
->drupalLogout();
$this
->drupalLogin($this->userViewOwn);
$this
->drupalGet('admin/content/files');
$this
->assertResponse(200);
$this
->assertLinkByHref($files['private_document']
->toUrl()
->toString());
$this
->drupalGet($files['private_document']
->toUrl('edit-form'));
$this
->assertResponse(403, 'User doesn\'t have permission to edit files');
$this
->drupalGet($files['private_document']
->toUrl('delete-form'));
$this
->assertResponse(403, 'User doesn\'t have permission to delete files');
$this
->assertNoLinkByHref($files['private_image']
->toUrl()
->toString());
$this
->assertNoLinkByHref($files['private_image']
->toUrl('edit-form')
->toString());
$this
->assertNoLinkByHref($files['private_image']
->toUrl('delete-form')
->toString());
$this
->assertNoLinkByHref($files['private_image']
->downloadUrl()
->toString());
$this
->assertNoFieldByName('bulk_form[' . $files['private_document']
->id() . ']', '', 'No bulk form checkbox found.');
$this
->drupalLogout();
$this
->drupalLogin($this->userViewPrivate);
$this
->drupalGet('admin/content/files');
$this
->assertResponse(200);
$this
->assertLinkByHref('file/' . $files['private_document']
->id());
$this
->assertLinkByHref('file/' . $files['private_image']
->id());
$this
->drupalLogout();
$this
->drupalLogin($this->userEditDelete);
$this
->drupalGet('admin/content/files');
$this
->assertResponse(200);
foreach ($files as $file) {
$this
->assertLinkByHref('file/' . $file
->id());
$this
->assertLinkByHref('file/' . $file
->id() . '/edit');
$this
->assertLinkByHref('file/' . $file
->id() . '/delete');
$this
->assertLinkByHref('file/' . $file
->id() . '/delete');
}
$this
->drupalLogout();
$this
->drupalLogin($this->userAdmin);
$this
->drupalGet('admin/content/files');
$this
->assertResponse(200);
foreach ($files as $file) {
$this
->assertLinkByHref('file/' . $file
->id());
$this
->assertLinkByHref('file/' . $file
->id() . '/edit');
$this
->assertLinkByHref('file/' . $file
->id() . '/delete');
$this
->assertLinkByHref('file/' . $file
->id() . '/download');
}
}
public function testFileOverviewOperations() {
$this
->setUpFiles();
$this
->drupalLogin($this->userEditDelete);
$this
->drupalGet('admin/content/files');
$this
->assertLinkByHref('file/1/delete');
$this
->assertLinkByHref('file/2/delete');
$this
->drupalGet('file/1/delete');
$this
->assertTitle(t('Are you sure you want to delete the file @filename? | Drupal', array(
'@filename' => FileEntity::load(1)
->label(),
)));
$this
->drupalPostForm(NULL, array(), 'Delete');
$this
->assertNoLinkByHref('file/1/delete');
$this
->assertLinkByHref('file/2/delete');
$this
->assertTrue(FileEntity::load(2)
->isPermanent());
$this
->assertTrue(FileEntity::load(3)
->isPermanent());
$this
->assertTrue(FileEntity::load(4)
->isPermanent());
$this
->assertTrue(FileEntity::load(5)
->isPermanent());
$this
->drupalGet('admin/content/files', array(
'query' => array(
'order' => 'fid',
),
));
$edit = array(
'action' => 'file_temporary_action',
'bulk_form[0]' => 1,
'bulk_form[1]' => 1,
'bulk_form[2]' => 1,
);
$this
->drupalPostForm(NULL, $edit, 'Apply to selected items');
\Drupal::entityTypeManager()
->getStorage('file')
->resetCache();
$this
->assertFalse(FileEntity::load(2)
->isPermanent());
$this
->assertFalse(FileEntity::load(3)
->isPermanent());
$this
->assertFalse(FileEntity::load(4)
->isPermanent());
$this
->assertTrue(FileEntity::load(5)
->isPermanent());
$this
->drupalGet('admin/content/files', array(
'query' => array(
'order' => 'fid',
),
));
$edit = array(
'action' => 'file_permanent_action',
'bulk_form[0]' => 1,
'bulk_form[1]' => 1,
);
$this
->drupalPostForm(NULL, $edit, 'Apply to selected items');
\Drupal::entityTypeManager()
->getStorage('file')
->resetCache();
$this
->assertTrue(FileEntity::load(2)
->isPermanent());
$this
->assertTrue(FileEntity::load(3)
->isPermanent());
$this
->assertFalse(FileEntity::load(4)
->isPermanent());
$this
->assertTrue(FileEntity::load(5)
->isPermanent());
$this
->drupalGet('admin/content/files', array(
'query' => array(
'order' => 'fid',
),
));
$edit = array(
'action' => 'file_delete_action',
'bulk_form[0]' => 1,
'bulk_form[1]' => 1,
);
$this
->drupalPostForm(NULL, $edit, 'Apply to selected items');
$this
->assertTitle(t('Are you sure you want to delete these files? | Drupal'));
$this
->assertLink('Cancel');
$this
->drupalPostForm(NULL, array(), 'Delete');
\Drupal::entityTypeManager()
->getStorage('file')
->resetCache();
$this
->assertNull(FileEntity::load(2), 'File 2 is deleted.');
$this
->assertNull(FileEntity::load(3), 'File 3 is deleted.');
$this
->assertNotNull(FileEntity::load(4), 'File 4 is not deleted.');
}
public function testUsageView() {
$this->container
->get('module_installer')
->install(array(
'node',
));
\Drupal::service('router.builder')
->rebuild();
$file = $this
->createFileEntity(array(
'uid' => $this->userAdmin,
));
$this
->drupalLogin($this->userAdmin);
$this
->drupalGet('admin/content/files');
$this
->assertLink('0 places');
$this
->assertNoLink('1 place');
$this
->clickLink('0 places');
$this
->assertText('This file is not currently used.');
$content_type = $this
->drupalCreateContentType();
$field_storage = FieldStorageConfig::create(array(
'field_name' => 'used_file',
'entity_type' => 'node',
'type' => 'file',
));
$field_storage
->save();
$field_instance = FieldConfig::create(array(
'field_storage' => $field_storage,
'entity_type' => 'node',
'bundle' => $content_type
->id(),
));
$field_instance
->save();
$node = Node::create(array(
'title' => 'An article that uses a file',
'type' => $content_type
->id(),
'used_file' => array(
'target_id' => $file
->id(),
'display' => 1,
'description' => '',
),
));
$node
->save();
\Drupal::entityTypeManager()
->getStorage('node')
->resetCache();
\Drupal::entityTypeManager()
->getStorage('file')
->resetCache();
$this
->drupalGet('admin/content/files');
$this
->assertLink('1 place');
$this
->clickLink('1 place');
$this
->assertLink('An article that uses a file');
$this
->clickLink('View');
$this
->assertResponse(200);
$this
->clickLink('Usage');
$this
->assertResponse(200);
}
}