View source
<?php
namespace Drupal\Tests\auditfiles\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\RoleInterface;
use Drupal\Core\Url;
use Drupal\Tests\TestFileCreationTrait;
use Drupal\file\Entity\File;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\file\Functional\FileFieldCreationTrait;
class AuditFilesUsedNotReferencedTest extends BrowserTestBase {
use TestFileCreationTrait;
use FileFieldCreationTrait;
protected static $modules = [
'node',
'field',
'file',
'image',
'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);
$bundle = 'article';
$fieldName = 'field_image';
$content_type = NodeType::create([
'type' => $bundle,
'name' => 'Test Article',
]);
$content_type
->save();
$settings = [
'cardinality' => 1,
'file_directory' => 'test_images',
'file_extensions' => 'png gif jpg jpeg txt',
];
$field_storage = FieldStorageConfig::create([
'entity_type' => 'node',
'field_name' => $fieldName,
'type' => 'image',
'settings' => $settings,
'cardinality' => $settings['cardinality'],
]);
$field_storage
->save();
$this
->attachFileField($fieldName, 'node', $bundle, $settings, []);
$files = $this
->getTestFiles('image');
$counter = 0;
foreach ($files as $file) {
$file->filesize = filesize($file->uri);
$file->status = TRUE;
$file->filename = $file->uri;
$newfile = File::create((array) $file);
$newfile
->save();
$counter++;
$node = Node::create([
'type' => 'article',
'title' => 'Sample Node ' . $counter,
'field_image' => [
'target_id' => $counter,
'alt' => 'Sample ' . $counter,
],
]);
$node
->save();
}
}
public function testReportPage() {
$path = URL::fromRoute('auditfiles.audit_files_usednotreferenced');
$session = $this
->assertSession();
$this
->drupalGet($path);
$session
->pageTextContains('Access denied');
$session
->statusCodeEquals(403);
$this
->drupalLogin($this->user);
$this
->drupalGet($path);
$session
->pageTextContains("Used not referenced");
$session
->statusCodeEquals(200);
}
public function testFileEntityCanBeDeleted() {
\Drupal::database()
->query("DELETE FROM {node__field_image} WHERE field_image_target_id='1'")
->execute();
\Drupal::database()
->query("DELETE FROM {node_revision__field_image} WHERE field_image_target_id='1'")
->execute();
$path = URL::fromRoute('auditfiles.audit_files_usednotreferenced');
$session = $this
->assertSession();
$this
->drupalLogin($this->user);
$this
->drupalGet($path);
$session
->pageTextContains("Used not referenced");
$session
->elementExists('css', '#audit-files-used-not-referenced');
$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?");
$edit = [];
$this
->submitForm($edit, 'Confirm');
$session
->pageTextContains("Used not referenced");
$session
->pageTextContains("Sucessfully deleted File ID : 1 from the file_usage table.");
$session
->elementNotExists('css', '#edit-files-1');
$session
->pageTextContains("No items found.");
}
}