View source
<?php
namespace Drupal\Tests\auditfiles\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\user\RoleInterface;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\node\Entity\Node;
class AuditFilesReferencedNotUsedTest extends WebDriverTestBase {
protected $profile = 'standard';
protected static $modules = [
'auditfiles',
];
protected $user;
protected $rid;
protected $defaultTheme = 'stark';
protected function setUp() {
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 = [
[
'file',
'node',
1,
1,
],
[
'file',
'node',
2,
1,
],
[
'file',
'node',
3,
1,
],
];
foreach ($values as $key => $value) {
\Drupal::database()
->insert('file_usage')
->fields([
'fid' => $key + 1,
'module' => $value[0],
'type' => $value[1],
'id' => $value[2],
'count' => $value[3],
])
->execute();
$fileno = $key + 1;
$path = "public://example_{$fileno}.png";
$image = File::create([
'uri' => $path,
'status' => TRUE,
]);
$image
->save();
$node = Node::create([
'type' => 'article',
'title' => 'Sample Node',
'field_image' => [
'target_id' => $key + 1,
'alt' => 'Sample',
'title' => 'Sample File',
],
]);
$node
->save();
}
}
public function testReportPage() {
$path = URL::fromRoute('auditfiles.audit_files_referencednotused');
$session = $this
->assertSession();
$this
->drupalGet($path);
$session
->pageTextContains('Access denied');
$this
->drupalLogin($this->user);
$this
->drupalGet($path);
$session
->pageTextContains('Referenced not used');
}
public function testFileEntityCanBeAddedToFileUsageTable() {
\Drupal::database()
->query("DELETE FROM {file_usage} WHERE type='node' AND fid='1'")
->execute();
$path = URL::fromRoute('auditfiles.audit_files_referencednotused');
$session = $this
->assertSession();
$this
->drupalLogin($this->user);
$this
->drupalGet($path);
$session
->pageTextContains("Referenced not used");
$session
->elementExists('css', '#audit-files-referenced-not-used');
$session
->elementExists('xpath', '//*[@id="edit-files"]/tbody/tr[1]/td[1]/div/input');
$edit = [
'edit-files-node-field-imagefield-image-target-id1node1' => TRUE,
];
$this
->submitForm($edit, 'Add selected items to the file_usage table');
$session
->pageTextContains("Add these files to the database?");
$session
->pageTextContains("File ID 1 will be added to the file_usage table.");
$edit = [];
$this
->submitForm($edit, 'Confirm');
$session
->waitForId('#audit-files-referenced-not-used');
$session
->elementNotExists('css', '#edit-files-node-field-imagefield-image-target-id1node1');
}
public function testFileEntityCanBeDeletedFromFileUsageTable() {
\Drupal::database()
->query("DELETE FROM {file_usage} WHERE type='node' AND fid='1'")
->execute();
$path = URL::fromRoute('auditfiles.audit_files_referencednotused');
$session = $this
->assertSession();
$this
->drupalLogin($this->user);
$this
->drupalGet($path);
$session
->pageTextContains("Referenced not used");
$session
->elementExists('css', '#audit-files-referenced-not-used');
$session
->elementExists('xpath', '//*[@id="edit-files"]/tbody/tr[1]/td[1]/div/input');
$edit = [
'edit-files-node-field-imagefield-image-target-id1node1' => TRUE,
];
$this
->submitForm($edit, 'Delete selected references');
$session
->pageTextContains("Delete these files from the server?");
$session
->pageTextContains("File ID 1 will be deleted from the content.");
$edit = [];
$this
->submitForm($edit, 'Confirm');
$session
->waitForId('#audit-files-referenced-not-used');
$session
->pageTextContains("Referenced not used");
$session
->elementNotExists('css', '#edit-files-node-field-imagefield-image-target-id1node1');
$session
->pageTextContains("file ID 1 deleted successfully.");
$session
->pageTextContains("No items found.");
}
}