View source
<?php
namespace Drupal\Tests\auditfiles\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\RoleInterface;
use Drupal\Core\Url;
use Drupal\Tests\TestFileCreationTrait;
class AuditFilesUsedNotManagedTest extends BrowserTestBase {
use TestFileCreationTrait;
protected static $modules = [
'node',
'file',
'user',
'auditfiles',
];
protected $user;
protected $rid;
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this->user = $this
->drupalCreateUser([
'access audit files reports',
]);
$all_rids = $this->user
->getRoles();
unset($all_rids[array_search(RoleInterface::AUTHENTICATED_ID, $all_rids)]);
$this->rid = reset($all_rids);
$values = [
[
1,
'file',
'media',
1,
1,
],
[
2,
'file',
'media',
3,
1,
],
[
3,
'file',
'media',
5,
1,
],
];
foreach ($values as $value) {
\Drupal::database()
->insert('file_usage')
->fields([
'fid' => $value[0],
'module' => $value[1],
'type' => $value[2],
'id' => $value[3],
'count' => $value[4],
])
->execute();
}
}
public function testReportPage() {
$path = URL::fromRoute('auditfiles.audit_files_usednotmanaged');
$session = $this
->assertSession();
$this
->drupalGet($path);
$session
->pageTextContains('Access denied');
$session
->statusCodeEquals(403);
$this
->drupalLogin($this->user);
$this
->drupalGet($path);
$session
->pageTextContains('Used not managed');
$session
->statusCodeEquals(200);
}
public function testFileEntityCanBeDeleted() {
$path = URL::fromRoute('auditfiles.audit_files_usednotmanaged');
$session = $this
->assertSession();
$this
->drupalLogin($this->user);
$this
->drupalGet($path);
$session
->pageTextContains("Used not managed");
$session
->pageTextContains("Found at least 3 files in the file_usage table that are not in the file_managed table.");
$session
->elementExists('css', '#audit-files-used-not-managed');
$session
->elementExists('css', '#edit-files-1');
$edit = [
'edit-files-1' => TRUE,
];
$this
->submitForm($edit, 'Delete selected items from the file_usage table');
$session
->pageTextContains("Delete these items from the file_usage table?");
$session
->pageTextContains("File ID 1 will be deleted from the file_usage table.");
$edit = [];
$this
->submitForm($edit, 'Confirm');
$session
->pageTextContains("Used not managed");
$session
->pageTextContains("Sucessfully deleted File ID : 1 from the file_usages table.");
$session
->pageTextContains("Found at least 2 files in the file_usage table that are not in the file_managed table.");
$session
->elementNotExists('css', '#edit-files-1');
}
}